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. |
Gavin Howard | fa495ce | 2018-12-18 10:03:14 -0700 | [diff] [blame] | 4 | * Adapted from https://github.com/gavinhoward/bc |
| 5 | * Original code copyright (c) 2018 Gavin D. Howard and contributors. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6 | */ |
| 7 | //config:config BC |
| 8 | //config: bool "bc (45 kb; 49 kb when combined with dc)" |
| 9 | //config: default y |
| 10 | //config: help |
| 11 | //config: bc is a command-line, arbitrary-precision calculator with a |
| 12 | //config: Turing-complete language. See the GNU bc manual |
| 13 | //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] | 14 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 15 | //config: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 16 | //config: This bc has five differences to the GNU bc: |
| 17 | //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] | 18 | //config: 2) Arrays are copied before being passed as arguments to |
| 19 | //config: functions. This behavior is required by the bc spec. |
| 20 | //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] | 21 | //config: the number of elements in the array. This prints "1": |
| 22 | //config: a[0] = 0; length(a[]) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 23 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 24 | //config: that of the unary minus (-) negation operator. This still |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 25 | //config: allows POSIX-compliant scripts to work while somewhat |
| 26 | //config: preserving expected behavior (versus C) and making parsing |
| 27 | //config: easier. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 28 | //config: 5) "read()" accepts expressions, not only numeric literals. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 29 | //config: |
| 30 | //config: Options: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 31 | //config: -i --interactive force interactive mode |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 32 | //config: -q --quiet don't print version and copyright |
| 33 | //config: -s --standard error if any non-POSIX extensions are used |
| 34 | //config: -w --warn warn if any non-POSIX extensions are used |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 35 | //config: -l --mathlib use predefined math routines: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 36 | //config: s(expr) sine in radians |
| 37 | //config: c(expr) cosine in radians |
| 38 | //config: a(expr) arctangent, returning radians |
| 39 | //config: l(expr) natural log |
| 40 | //config: e(expr) raises e to the power of expr |
| 41 | //config: j(n, x) Bessel function of integer order n of x |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 42 | //config: |
| 43 | //config:config DC |
| 44 | //config: bool "dc (38 kb; 49 kb when combined with bc)" |
| 45 | //config: default y |
| 46 | //config: help |
| 47 | //config: dc is a reverse-polish notation command-line calculator which |
| 48 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 49 | //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] | 50 | //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] | 51 | //config: |
| 52 | //config: This dc has a few differences from the two above: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 53 | //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] | 54 | //config: the FreeBSD dc does. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 55 | //config: 2) Implements the GNU extensions for divmod ("~") and |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 56 | //config: modular exponentiation ("|"). |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 57 | //config: 3) Implements all FreeBSD extensions, except for "J" and "M". |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 58 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 59 | //config: However, they are implemented differently. When it encounters |
| 60 | //config: whitespace where a register should be, it skips the whitespace. |
| 61 | //config: If the character following is not a lowercase letter, an error |
| 62 | //config: is issued. Otherwise, the register name is parsed by the |
| 63 | //config: following regex: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 64 | //config: [a-z][a-z0-9_]* |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 65 | //config: This generally means that register names will be surrounded by |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 66 | //config: whitespace. Examples: |
| 67 | //config: l idx s temp L index S temp2 < do_thing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 68 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 69 | //config: allowed unless the "-x" option is given. |
| 70 | //config: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 71 | //config:config FEATURE_DC_SMALL |
| 72 | //config: bool "Minimal dc implementation (4.2 kb), not using bc code base" |
| 73 | //config: depends on DC && !BC |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 74 | //config: default n |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 75 | //config: |
| 76 | //config:config FEATURE_DC_LIBM |
| 77 | //config: bool "Enable power and exp functions (requires libm)" |
| 78 | //config: default y |
| 79 | //config: depends on FEATURE_DC_SMALL |
| 80 | //config: help |
| 81 | //config: Enable power and exp functions. |
| 82 | //config: NOTE: This will require libm to be present for linking. |
| 83 | //config: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 84 | //config:config FEATURE_BC_SIGNALS |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 85 | //config: bool "Interactive mode (+4kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 86 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 87 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 88 | //config: help |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 89 | //config: Enable interactive mode: when started on a tty, |
| 90 | //config: ^C interrupts execution and returns to command line, |
| 91 | //config: errors also return to command line instead of exiting, |
| 92 | //config: line editing with history is available. |
| 93 | //config: |
| 94 | //config: With this option off, input can still be taken from tty, |
| 95 | //config: but all errors are fatal, ^C is fatal, |
| 96 | //config: tty is treated exactly the same as any other |
| 97 | //config: standard input (IOW: no line editing). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 98 | //config: |
| 99 | //config:config FEATURE_BC_LONG_OPTIONS |
| 100 | //config: bool "Enable bc/dc long options" |
| 101 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 102 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 103 | //config: help |
| 104 | //config: Enable long options for bc and dc. |
| 105 | |
| 106 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 107 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 108 | |
| 109 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 110 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 111 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 112 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 113 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 09fe0aa | 2018-12-18 16:32:25 +0100 | [diff] [blame] | 114 | //usage: "[-sqlw] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 115 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 116 | //usage:#define bc_full_usage "\n" |
| 117 | //usage: "\nArbitrary precision calculator" |
| 118 | //usage: "\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 119 | ///////: "\n -i Interactive" - has no effect for now |
| 120 | //usage: "\n -q Quiet" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 121 | //usage: "\n -l Load standard math library" |
| 122 | //usage: "\n -s Be POSIX compatible" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 123 | //usage: "\n -w Warn if extensions are used" |
| 124 | ///////: "\n -v Version" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 125 | //usage: "\n" |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 126 | //usage: "\n$BC_LINE_LENGTH changes output width" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 127 | //usage: |
| 128 | //usage:#define bc_example_usage |
| 129 | //usage: "3 + 4.129\n" |
| 130 | //usage: "1903 - 2893\n" |
| 131 | //usage: "-129 * 213.28935\n" |
| 132 | //usage: "12 / -1932\n" |
| 133 | //usage: "12 % 12\n" |
| 134 | //usage: "34 ^ 189\n" |
| 135 | //usage: "scale = 13\n" |
| 136 | //usage: "ibase = 2\n" |
| 137 | //usage: "obase = A\n" |
| 138 | //usage: |
| 139 | //usage:#define dc_trivial_usage |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 140 | //usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 141 | //usage: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 142 | //usage:#define dc_full_usage "\n" |
| 143 | //usage: "\nTiny RPN calculator. Operations:" |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 144 | //usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,") |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 145 | //usage: "\np - print top of the stack without popping" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 146 | //usage: "\nf - print entire stack" |
| 147 | //usage: "\nk - pop the value and set the precision" |
| 148 | //usage: "\ni - pop the value and set input radix" |
| 149 | //usage: "\no - pop the value and set output radix" |
| 150 | //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] | 151 | //usage: |
| 152 | //usage:#define dc_example_usage |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 153 | //usage: "$ dc -e'2 2 + p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 154 | //usage: "4\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 155 | //usage: "$ dc -e'8 8 \\* 2 2 + / p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 156 | //usage: "16\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 157 | //usage: "$ dc -e'0 1 & p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 158 | //usage: "0\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 159 | //usage: "$ dc -e'0 1 | p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 160 | //usage: "1\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 161 | //usage: "$ echo '72 9 / 8 * p' | dc\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 162 | //usage: "64\n" |
| 163 | |
| 164 | #include "libbb.h" |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 165 | #include "common_bufsiz.h" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 166 | |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 167 | #if ENABLE_FEATURE_DC_SMALL |
| 168 | # include "dc.c" |
| 169 | #else |
| 170 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 171 | #define DEBUG_LEXER 0 |
| 172 | #define DEBUG_COMPILE 0 |
| 173 | #define DEBUG_EXEC 0 |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 174 | // This can be left enabled for production as well: |
| 175 | #define SANITY_CHECKS 1 |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 176 | |
| 177 | #if DEBUG_LEXER |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 178 | static uint8_t lex_indent; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 179 | #define dbg_lex(...) \ |
| 180 | do { \ |
| 181 | fprintf(stderr, "%*s", lex_indent, ""); \ |
| 182 | bb_error_msg(__VA_ARGS__); \ |
| 183 | } while (0) |
| 184 | #define dbg_lex_enter(...) \ |
| 185 | do { \ |
| 186 | dbg_lex(__VA_ARGS__); \ |
| 187 | lex_indent++; \ |
| 188 | } while (0) |
| 189 | #define dbg_lex_done(...) \ |
| 190 | do { \ |
| 191 | lex_indent--; \ |
| 192 | dbg_lex(__VA_ARGS__); \ |
| 193 | } while (0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 194 | #else |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 195 | # define dbg_lex(...) ((void)0) |
| 196 | # define dbg_lex_enter(...) ((void)0) |
| 197 | # define dbg_lex_done(...) ((void)0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 198 | #endif |
| 199 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 200 | #if DEBUG_COMPILE |
| 201 | # define dbg_compile(...) bb_error_msg(__VA_ARGS__) |
| 202 | #else |
| 203 | # define dbg_compile(...) ((void)0) |
| 204 | #endif |
| 205 | |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 206 | #if DEBUG_EXEC |
| 207 | # define dbg_exec(...) bb_error_msg(__VA_ARGS__) |
| 208 | #else |
| 209 | # define dbg_exec(...) ((void)0) |
| 210 | #endif |
| 211 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 212 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 213 | BC_STATUS_SUCCESS = 0, |
| 214 | BC_STATUS_FAILURE = 1, |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 215 | 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] | 216 | } BcStatus; |
| 217 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 218 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 219 | #define BC_VEC_START_CAP (1 << 5) |
| 220 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 221 | typedef void (*BcVecFree)(void *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 222 | |
| 223 | typedef struct BcVec { |
| 224 | char *v; |
| 225 | size_t len; |
| 226 | size_t cap; |
| 227 | size_t size; |
| 228 | BcVecFree dtor; |
| 229 | } BcVec; |
| 230 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 231 | typedef signed char BcDig; |
| 232 | |
| 233 | typedef struct BcNum { |
| 234 | BcDig *restrict num; |
| 235 | size_t rdx; |
| 236 | size_t len; |
| 237 | size_t cap; |
| 238 | bool neg; |
| 239 | } BcNum; |
| 240 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 241 | #define BC_NUM_MAX_IBASE ((unsigned long) 16) |
| 242 | // larger value might speed up BIGNUM calculations a bit: |
| 243 | #define BC_NUM_DEF_SIZE (16) |
| 244 | #define BC_NUM_PRINT_WIDTH (69) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 245 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 246 | #define BC_NUM_KARATSUBA_LEN (32) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 247 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 248 | typedef enum BcInst { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 249 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 250 | BC_INST_INC_PRE, |
| 251 | BC_INST_DEC_PRE, |
| 252 | BC_INST_INC_POST, |
| 253 | BC_INST_DEC_POST, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 254 | #endif |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 255 | XC_INST_NEG, // order |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 256 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 257 | XC_INST_REL_EQ, // should |
| 258 | XC_INST_REL_LE, // match |
| 259 | XC_INST_REL_GE, // LEX |
| 260 | XC_INST_REL_NE, // constants |
| 261 | XC_INST_REL_LT, // for |
| 262 | XC_INST_REL_GT, // these |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 263 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 264 | XC_INST_POWER, // operations |
| 265 | XC_INST_MULTIPLY, // | |
| 266 | XC_INST_DIVIDE, // | |
| 267 | XC_INST_MODULUS, // | |
| 268 | XC_INST_PLUS, // | |
| 269 | XC_INST_MINUS, // | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 270 | |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 271 | XC_INST_BOOL_NOT, // | |
| 272 | XC_INST_BOOL_OR, // | |
| 273 | XC_INST_BOOL_AND, // | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 274 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 275 | BC_INST_ASSIGN_POWER, // | |
| 276 | BC_INST_ASSIGN_MULTIPLY,// | |
| 277 | BC_INST_ASSIGN_DIVIDE, // | |
| 278 | BC_INST_ASSIGN_MODULUS, // | |
| 279 | BC_INST_ASSIGN_PLUS, // | |
| 280 | BC_INST_ASSIGN_MINUS, // | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 281 | #endif |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 282 | XC_INST_ASSIGN, // V |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 283 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 284 | XC_INST_NUM, |
| 285 | XC_INST_VAR, |
| 286 | XC_INST_ARRAY_ELEM, |
| 287 | XC_INST_ARRAY, |
| 288 | XC_INST_SCALE_FUNC, |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 289 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 290 | XC_INST_IBASE, // order of these constans should match other enums |
| 291 | XC_INST_OBASE, // order of these constans should match other enums |
| 292 | XC_INST_SCALE, // order of these constans should match other enums |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 293 | IF_BC(BC_INST_LAST,) // order of these constans should match other enums |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 294 | XC_INST_LENGTH, |
| 295 | XC_INST_READ, |
| 296 | XC_INST_SQRT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 297 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 298 | XC_INST_PRINT, |
| 299 | XC_INST_PRINT_POP, |
| 300 | XC_INST_STR, |
| 301 | XC_INST_PRINT_STR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 302 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 303 | #if ENABLE_BC |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 304 | BC_INST_HALT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 305 | BC_INST_JUMP, |
| 306 | BC_INST_JUMP_ZERO, |
| 307 | |
| 308 | BC_INST_CALL, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 309 | BC_INST_RET0, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 310 | #endif |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 311 | XC_INST_RET, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 312 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 313 | XC_INST_POP, |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 314 | #if ENABLE_DC |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 315 | DC_INST_POP_EXEC, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 316 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 317 | DC_INST_MODEXP, |
| 318 | DC_INST_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 319 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 320 | DC_INST_EXECUTE, |
| 321 | DC_INST_EXEC_COND, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 322 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 323 | DC_INST_ASCIIFY, |
| 324 | DC_INST_PRINT_STREAM, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 325 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 326 | DC_INST_PRINT_STACK, |
| 327 | DC_INST_CLEAR_STACK, |
| 328 | DC_INST_STACK_LEN, |
| 329 | DC_INST_DUPLICATE, |
| 330 | DC_INST_SWAP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 331 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 332 | DC_INST_LOAD, |
| 333 | DC_INST_PUSH_VAR, |
| 334 | DC_INST_PUSH_TO_VAR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 335 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 336 | DC_INST_QUIT, |
| 337 | DC_INST_NQUIT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 338 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 339 | DC_INST_INVALID = -1, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 340 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 341 | } BcInst; |
| 342 | |
| 343 | typedef struct BcId { |
| 344 | char *name; |
| 345 | size_t idx; |
| 346 | } BcId; |
| 347 | |
| 348 | typedef struct BcFunc { |
| 349 | BcVec code; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 350 | IF_BC(BcVec labels;) |
| 351 | IF_BC(BcVec autos;) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 352 | IF_BC(BcVec strs;) |
| 353 | IF_BC(BcVec consts;) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 354 | IF_BC(size_t nparams;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 355 | } BcFunc; |
| 356 | |
| 357 | typedef enum BcResultType { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 358 | XC_RESULT_TEMP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 359 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 360 | XC_RESULT_VAR, |
| 361 | XC_RESULT_ARRAY_ELEM, |
| 362 | XC_RESULT_ARRAY, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 363 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 364 | XC_RESULT_STR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 365 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 366 | //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct, |
| 367 | XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE |
| 368 | XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE |
| 369 | XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 370 | IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 371 | XC_RESULT_CONSTANT, |
| 372 | IF_BC(BC_RESULT_ONE,) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 373 | } BcResultType; |
| 374 | |
| 375 | typedef union BcResultData { |
| 376 | BcNum n; |
| 377 | BcVec v; |
| 378 | BcId id; |
| 379 | } BcResultData; |
| 380 | |
| 381 | typedef struct BcResult { |
| 382 | BcResultType t; |
| 383 | BcResultData d; |
| 384 | } BcResult; |
| 385 | |
| 386 | typedef struct BcInstPtr { |
| 387 | size_t func; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 388 | size_t inst_idx; |
| 389 | IF_BC(size_t results_len_before_call;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 390 | } BcInstPtr; |
| 391 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 392 | typedef enum BcLexType { |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 393 | XC_LEX_EOF, |
| 394 | XC_LEX_INVALID, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 395 | |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 396 | XC_LEX_NLINE, |
| 397 | XC_LEX_WHITESPACE, |
| 398 | XC_LEX_STR, |
| 399 | XC_LEX_NAME, |
| 400 | XC_LEX_NUMBER, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 401 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 402 | XC_LEX_1st_op, |
| 403 | XC_LEX_NEG = XC_LEX_1st_op, // order |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 404 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 405 | XC_LEX_OP_REL_EQ, // should |
| 406 | XC_LEX_OP_REL_LE, // match |
| 407 | XC_LEX_OP_REL_GE, // INST |
| 408 | XC_LEX_OP_REL_NE, // constants |
| 409 | XC_LEX_OP_REL_LT, // for |
| 410 | XC_LEX_OP_REL_GT, // these |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 411 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 412 | XC_LEX_OP_POWER, // operations |
| 413 | XC_LEX_OP_MULTIPLY, // | |
| 414 | XC_LEX_OP_DIVIDE, // | |
| 415 | XC_LEX_OP_MODULUS, // | |
| 416 | XC_LEX_OP_PLUS, // | |
| 417 | XC_LEX_OP_MINUS, // | |
| 418 | XC_LEX_OP_last = XC_LEX_OP_MINUS, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 419 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 420 | BC_LEX_OP_BOOL_NOT, // | |
| 421 | BC_LEX_OP_BOOL_OR, // | |
| 422 | BC_LEX_OP_BOOL_AND, // | |
| 423 | |
| 424 | BC_LEX_OP_ASSIGN_POWER, // | |
| 425 | BC_LEX_OP_ASSIGN_MULTIPLY, // | |
| 426 | BC_LEX_OP_ASSIGN_DIVIDE, // | |
| 427 | BC_LEX_OP_ASSIGN_MODULUS, // | |
| 428 | BC_LEX_OP_ASSIGN_PLUS, // | |
| 429 | BC_LEX_OP_ASSIGN_MINUS, // | |
| 430 | |
| 431 | BC_LEX_OP_ASSIGN, // V |
| 432 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 433 | BC_LEX_OP_INC, |
| 434 | BC_LEX_OP_DEC, |
| 435 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 436 | BC_LEX_LPAREN, // () are 0x28 and 0x29 |
| 437 | BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 438 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 439 | BC_LEX_LBRACKET, // [] are 0x5B and 5D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 440 | BC_LEX_COMMA, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 441 | BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 442 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 443 | BC_LEX_LBRACE, // {} are 0x7B and 0x7D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 444 | BC_LEX_SCOLON, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 445 | BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 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 | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 455 | // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct, |
| 456 | BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE |
| 457 | BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 458 | BC_LEX_KEY_IF, |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 459 | IF_BC(BC_LEX_KEY_LAST,) // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 460 | BC_LEX_KEY_LENGTH, |
| 461 | BC_LEX_KEY_LIMITS, |
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, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 469 | #endif // ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 470 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 471 | #if ENABLE_DC |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 472 | DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1, |
| 473 | DC_LEX_OP_ASSIGN, |
| 474 | |
| 475 | DC_LEX_LPAREN, |
| 476 | DC_LEX_SCOLON, |
| 477 | DC_LEX_READ, |
| 478 | DC_LEX_IBASE, |
| 479 | DC_LEX_SCALE, |
| 480 | DC_LEX_OBASE, |
| 481 | DC_LEX_LENGTH, |
| 482 | DC_LEX_PRINT, |
| 483 | DC_LEX_QUIT, |
| 484 | DC_LEX_SQRT, |
| 485 | DC_LEX_LBRACE, |
| 486 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 487 | DC_LEX_EQ_NO_REG, |
| 488 | DC_LEX_OP_MODEXP, |
| 489 | DC_LEX_OP_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 490 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 491 | DC_LEX_COLON, |
| 492 | DC_LEX_ELSE, |
| 493 | DC_LEX_EXECUTE, |
| 494 | DC_LEX_PRINT_STACK, |
| 495 | DC_LEX_CLEAR_STACK, |
| 496 | DC_LEX_STACK_LEVEL, |
| 497 | DC_LEX_DUPLICATE, |
| 498 | DC_LEX_SWAP, |
| 499 | DC_LEX_POP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 500 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 501 | DC_LEX_ASCIIFY, |
| 502 | DC_LEX_PRINT_STREAM, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 503 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 504 | // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct, |
| 505 | DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE |
| 506 | DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE |
| 507 | DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE |
| 508 | DC_LEX_LOAD, |
| 509 | DC_LEX_LOAD_POP, |
| 510 | DC_LEX_STORE_PUSH, |
| 511 | DC_LEX_PRINT_POP, |
| 512 | DC_LEX_NQUIT, |
| 513 | DC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 514 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 515 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 516 | // must match order of BC_LEX_KEY_foo etc above |
| 517 | #if ENABLE_BC |
| 518 | struct BcLexKeyword { |
| 519 | char name8[8]; |
| 520 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 521 | #define LEX_KW_ENTRY(a, b) \ |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 522 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 523 | static const struct BcLexKeyword bc_lex_kws[20] = { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 524 | LEX_KW_ENTRY("auto" , 1), // 0 |
| 525 | LEX_KW_ENTRY("break" , 1), // 1 |
| 526 | LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 527 | LEX_KW_ENTRY("define" , 1), // 3 |
| 528 | LEX_KW_ENTRY("else" , 0), // 4 |
| 529 | LEX_KW_ENTRY("for" , 1), // 5 |
| 530 | LEX_KW_ENTRY("halt" , 0), // 6 |
| 531 | LEX_KW_ENTRY("ibase" , 1), // 7 |
| 532 | LEX_KW_ENTRY("obase" , 1), // 8 |
| 533 | LEX_KW_ENTRY("if" , 1), // 9 |
| 534 | LEX_KW_ENTRY("last" , 0), // 10 |
| 535 | LEX_KW_ENTRY("length" , 1), // 11 |
| 536 | LEX_KW_ENTRY("limits" , 0), // 12 |
| 537 | LEX_KW_ENTRY("print" , 0), // 13 |
| 538 | LEX_KW_ENTRY("quit" , 1), // 14 |
| 539 | LEX_KW_ENTRY("read" , 0), // 15 |
| 540 | LEX_KW_ENTRY("return" , 1), // 16 |
| 541 | LEX_KW_ENTRY("scale" , 1), // 17 |
| 542 | LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 543 | LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 544 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 545 | #undef LEX_KW_ENTRY |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 546 | #define STRING_else (bc_lex_kws[4].name8) |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 547 | #define STRING_for (bc_lex_kws[5].name8) |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 548 | #define STRING_if (bc_lex_kws[9].name8) |
| 549 | #define STRING_while (bc_lex_kws[19].name8) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 550 | enum { |
| 551 | POSIX_KWORD_MASK = 0 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 552 | | (1 << 0) // 0 |
| 553 | | (1 << 1) // 1 |
| 554 | | (0 << 2) // 2 |
| 555 | | (1 << 3) // 3 |
| 556 | | (0 << 4) // 4 |
| 557 | | (1 << 5) // 5 |
| 558 | | (0 << 6) // 6 |
| 559 | | (1 << 7) // 7 |
| 560 | | (1 << 8) // 8 |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 561 | | (1 << 9) // 9 |
| 562 | | (0 << 10) // 10 |
| 563 | | (1 << 11) // 11 |
| 564 | | (0 << 12) // 12 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 565 | | (0 << 13) // 13 |
| 566 | | (1 << 14) // 14 |
| 567 | | (0 << 15) // 15 |
| 568 | | (1 << 16) // 16 |
| 569 | | (1 << 17) // 17 |
| 570 | | (1 << 18) // 18 |
| 571 | | (1 << 19) // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 572 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 573 | #define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 574 | |
| 575 | // This is a bit array that corresponds to token types. An entry is |
| 576 | // true if the token is valid in an expression, false otherwise. |
| 577 | // Used to figure out when expr parsing should stop *without error message* |
| 578 | // - 0 element indicates this condition. 1 means "this token is to be eaten |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 579 | // as part of the expression", it can then still be determined to be invalid |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 580 | // by later processing. |
| 581 | enum { |
| 582 | #define EXBITS(a,b,c,d,e,f,g,h) \ |
| 583 | ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7))) |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 584 | BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz: |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 585 | + (EXBITS(0,0,0,0,0,1,1,1) << (0*8)) // 0: EOF INVAL NL WS STR NAME NUM - |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 586 | + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ * |
| 587 | + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^= |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 588 | + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ -- |
| 589 | + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; } |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 590 | + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase |
| 591 | + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read |
| 592 | + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 593 | #undef EXBITS |
| 594 | }; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 595 | static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 596 | { |
| 597 | #if ULONG_MAX > 0xffffffff |
| 598 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 599 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 600 | #else |
| 601 | // 32-bit version |
| 602 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 603 | if (i >= 32) { |
| 604 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 605 | i &= 31; |
| 606 | } |
| 607 | return m & (1UL << i); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 608 | #endif |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | // This is an array of data for operators that correspond to |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 612 | // [XC_LEX_1st_op...] token types. |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 613 | static const uint8_t bc_parse_ops[] = { |
| 614 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 615 | OP(1, false), // neg |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 616 | OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < > |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 617 | OP(2, false), // pow |
| 618 | OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod |
| 619 | OP(4, true ), OP( 4, true ), // + - |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 620 | OP(1, false), // not |
| 621 | OP(7, true ), OP( 7, true ), // or and |
| 622 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 623 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 624 | OP(0, false), OP( 0, false ), // inc dec |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 625 | #undef OP |
| 626 | }; |
| 627 | #define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f) |
| 628 | #define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10) |
| 629 | #endif // ENABLE_BC |
| 630 | |
| 631 | #if ENABLE_DC |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 632 | static const //BcLexType - should be this type |
| 633 | uint8_t |
| 634 | dc_char_to_LEX[] = { |
| 635 | /* %&'( */ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 636 | XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 637 | /* )*+, */ |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 638 | XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 639 | /* -./ */ |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 640 | XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 641 | /* 0123456789 */ |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 642 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 643 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 644 | XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 645 | /* :;<=>?@ */ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 646 | DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ, |
| 647 | XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 648 | /* ABCDEFGH */ |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 649 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 650 | XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 651 | /* IJKLMNOP */ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 652 | DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP, |
| 653 | XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 654 | /* QRSTUVWXY */ |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 655 | DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID, |
| 656 | XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 657 | /* Z[\] */ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 658 | DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 659 | /* ^_` */ |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 660 | XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 661 | /* abcdefgh */ |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 662 | DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE, |
| 663 | DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 664 | /* ijklmnop */ |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 665 | DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 666 | XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 667 | /* qrstuvwx */ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 668 | DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID, |
| 669 | XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 670 | /* yz */ |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 671 | XC_LEX_INVALID, DC_LEX_STACK_LEVEL, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 672 | /* {|}~ */ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 673 | DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD, |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 674 | }; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 675 | static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1 |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 676 | int8_t |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 677 | dc_LEX_to_INST[] = { // starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz: |
| 678 | XC_INST_POWER, XC_INST_MULTIPLY, // OP_POWER OP_MULTIPLY |
| 679 | XC_INST_DIVIDE, XC_INST_MODULUS, // OP_DIVIDE OP_MODULUS |
| 680 | XC_INST_PLUS, XC_INST_MINUS, // OP_PLUS OP_MINUS |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 681 | XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT |
| 682 | DC_INST_INVALID, // DC_LEX_OP_ASSIGN |
| 683 | XC_INST_REL_GT, // DC_LEX_LPAREN |
| 684 | DC_INST_INVALID, // DC_LEX_SCOLON |
| 685 | DC_INST_INVALID, // DC_LEX_READ |
| 686 | XC_INST_IBASE, // DC_LEX_IBASE |
| 687 | XC_INST_SCALE, // DC_LEX_SCALE |
| 688 | XC_INST_OBASE, // DC_LEX_OBASE |
| 689 | XC_INST_LENGTH, // DC_LEX_LENGTH |
| 690 | XC_INST_PRINT, // DC_LEX_PRINT |
| 691 | DC_INST_QUIT, // DC_LEX_QUIT |
| 692 | XC_INST_SQRT, // DC_LEX_SQRT |
| 693 | XC_INST_REL_GE, // DC_LEX_LBRACE |
| 694 | XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG |
| 695 | DC_INST_MODEXP, DC_INST_DIVMOD, // OP_MODEXP OP_DIVMOD |
| 696 | DC_INST_INVALID, DC_INST_INVALID, // COLON ELSE |
| 697 | DC_INST_EXECUTE, // EXECUTE |
| 698 | DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // PRINT_STACK CLEAR_STACK |
| 699 | DC_INST_STACK_LEN, DC_INST_DUPLICATE, // STACK_LEVEL DUPLICATE |
| 700 | DC_INST_SWAP, XC_INST_POP, // SWAP POP |
| 701 | DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // ASCIIFY PRINT_STREAM |
| 702 | DC_INST_INVALID, DC_INST_INVALID, // STORE_IBASE STORE_OBASE |
| 703 | DC_INST_INVALID, DC_INST_INVALID, // STORE_SCALE LOAD |
| 704 | DC_INST_INVALID, DC_INST_INVALID, // LOAD_POP STORE_PUSH |
| 705 | XC_INST_PRINT, DC_INST_NQUIT, // PRINT_POP NQUIT |
| 706 | XC_INST_SCALE_FUNC, // SCALE_FACTOR |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 707 | // DC_INST_INVALID in this table either means that corresponding LEX |
| 708 | // is not possible for dc, or that it does not compile one-to-one |
| 709 | // to a single INST. |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 710 | }; |
| 711 | #endif // ENABLE_DC |
| 712 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 713 | typedef struct BcLex { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 714 | const char *lex_inbuf; |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 715 | const char *lex_next_at; // last lex_next() was called at this string |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 716 | size_t lex_i; |
| 717 | size_t lex_line; |
| 718 | size_t lex_len; |
| 719 | bool lex_newline; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 720 | smallint lex; // was BcLexType |
| 721 | smallint lex_last; // was BcLexType |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 722 | BcVec lex_strnumbuf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 723 | } BcLex; |
| 724 | |
Denys Vlasenko | 4796a1d | 2018-12-19 12:47:45 +0100 | [diff] [blame] | 725 | #define BC_PARSE_STREND (0xff) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 726 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 727 | #if ENABLE_BC |
| 728 | # define BC_PARSE_REL (1 << 0) |
| 729 | # define BC_PARSE_PRINT (1 << 1) |
| 730 | # define BC_PARSE_ARRAY (1 << 2) |
| 731 | # define BC_PARSE_NOCALL (1 << 3) |
| 732 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 733 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 734 | typedef struct BcParse { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 735 | BcLex l; |
| 736 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 737 | IF_BC(BcVec exits;) |
| 738 | IF_BC(BcVec conds;) |
| 739 | IF_BC(BcVec ops;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 740 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 741 | const char *filename; |
| 742 | FILE *input_fp; |
| 743 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 744 | BcFunc *func; |
| 745 | size_t fidx; |
| 746 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 747 | IF_BC(size_t in_funcdef;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 748 | } BcParse; |
| 749 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 750 | typedef struct BcProgram { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 751 | size_t len; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 752 | size_t nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 753 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 754 | size_t scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 755 | size_t ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 756 | size_t ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 757 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 758 | BcVec results; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 759 | BcVec exestack; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 760 | |
| 761 | BcVec fns; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 762 | IF_BC(BcVec fn_map;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 763 | |
| 764 | BcVec vars; |
| 765 | BcVec var_map; |
| 766 | |
| 767 | BcVec arrs; |
| 768 | BcVec arr_map; |
| 769 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 770 | IF_DC(BcVec strs;) |
| 771 | IF_DC(BcVec consts;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 772 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 773 | BcNum zero; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 774 | IF_BC(BcNum one;) |
| 775 | IF_BC(BcNum last;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 776 | } BcProgram; |
| 777 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 778 | #define BC_PROG_MAIN (0) |
| 779 | #define BC_PROG_READ (1) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 780 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 781 | #define BC_PROG_REQ_FUNCS (2) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 782 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 783 | |
| 784 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 785 | #define BC_PROG_NUM(r, n) \ |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 786 | ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 787 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 788 | #define BC_FLAG_W (1 << 0) |
| 789 | #define BC_FLAG_V (1 << 1) |
| 790 | #define BC_FLAG_S (1 << 2) |
| 791 | #define BC_FLAG_Q (1 << 3) |
| 792 | #define BC_FLAG_L (1 << 4) |
| 793 | #define BC_FLAG_I (1 << 5) |
| 794 | #define DC_FLAG_X (1 << 6) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 795 | |
| 796 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 797 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 798 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 799 | #define BC_MAX_OBASE ((unsigned) 999) |
| 800 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 801 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 802 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 803 | #define BC_MAX_NUM BC_MAX_STRING |
| 804 | // Unused apart from "limits" message. Just show a "biggish number" there. |
| 805 | //#define BC_MAX_NAME BC_MAX_STRING |
| 806 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 807 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 808 | #define BC_MAX_NAME_STR "999999999" |
| 809 | #define BC_MAX_EXP_STR "999999999" |
| 810 | #define BC_MAX_VARS_STR "999999999" |
| 811 | |
| 812 | #define BC_MAX_OBASE_STR "999" |
| 813 | |
| 814 | #if INT_MAX == 2147483647 |
| 815 | # define BC_MAX_DIM_STR "2147483647" |
| 816 | #elif INT_MAX == 9223372036854775807 |
| 817 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 818 | #else |
| 819 | # error Strange INT_MAX |
| 820 | #endif |
| 821 | |
| 822 | #if UINT_MAX == 4294967295 |
| 823 | # define BC_MAX_SCALE_STR "4294967295" |
| 824 | # define BC_MAX_STRING_STR "4294967294" |
| 825 | #elif UINT_MAX == 18446744073709551615 |
| 826 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 827 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 828 | #else |
| 829 | # error Strange UINT_MAX |
| 830 | #endif |
| 831 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 832 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 833 | struct globals { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 834 | IF_FEATURE_BC_SIGNALS(smallint ttyin;) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 835 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 836 | |
| 837 | BcParse prs; |
| 838 | BcProgram prog; |
| 839 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 840 | // For error messages. Can be set to current parsed line, |
| 841 | // or [TODO] to current executing line (can be before last parsed one) |
| 842 | unsigned err_line; |
| 843 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 844 | BcVec files; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 845 | BcVec input_buffer; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 846 | |
| 847 | char *env_args; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 848 | |
| 849 | #if ENABLE_FEATURE_EDITING |
| 850 | line_input_t *line_input_state; |
| 851 | #endif |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 852 | } FIX_ALIASING; |
| 853 | #define G (*ptr_to_globals) |
| 854 | #define INIT_G() do { \ |
| 855 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 856 | } while (0) |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 857 | #define FREE_G() do { \ |
| 858 | FREE_PTR_TO_GLOBALS(); \ |
| 859 | } while (0) |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 860 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 861 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 862 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 863 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 864 | # define G_interrupt bb_got_signal |
| 865 | # define G_ttyin G.ttyin |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 866 | #else |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 867 | # define G_interrupt 0 |
| 868 | # define G_ttyin 0 |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 869 | #endif |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 870 | #if ENABLE_FEATURE_CLEAN_UP |
| 871 | # define G_exiting G.exiting |
| 872 | #else |
| 873 | # define G_exiting 0 |
| 874 | #endif |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 875 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
Denys Vlasenko | 40534bb | 2018-12-13 16:59:24 +0100 | [diff] [blame] | 876 | #define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b')) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 877 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 878 | // In configurations where errors abort instead of propagating error |
| 879 | // return code up the call chain, functions returning BC_STATUS |
| 880 | // actually don't return anything, they always succeed and return "void". |
| 881 | // A macro wrapper is provided, which makes this statement work: |
| 882 | // s = zbc_func(...) |
| 883 | // and makes it visible to the compiler that s is always zero, |
| 884 | // allowing compiler to optimize dead code after the statement. |
| 885 | // |
| 886 | // To make code more readable, each such function has a "z" |
| 887 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. |
| 888 | // |
| 889 | #if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 890 | # define ERRORS_ARE_FATAL 0 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 891 | # define ERRORFUNC /*nothing*/ |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 892 | # define IF_ERROR_RETURN_POSSIBLE(a) a |
| 893 | # define BC_STATUS BcStatus |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 894 | # define RETURN_STATUS(v) return (v) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 895 | # define COMMA_SUCCESS /*nothing*/ |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 896 | #else |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 897 | # define ERRORS_ARE_FATAL 1 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 898 | # define ERRORFUNC NORETURN |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 899 | # define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/ |
| 900 | # define BC_STATUS void |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 901 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 902 | # define COMMA_SUCCESS ,BC_STATUS_SUCCESS |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 903 | #endif |
| 904 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 905 | // |
| 906 | // Utility routines |
| 907 | // |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 908 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 909 | static void fflush_and_check(void) |
| 910 | { |
| 911 | fflush_all(); |
| 912 | if (ferror(stdout) || ferror(stderr)) |
| 913 | bb_perror_msg_and_die("output error"); |
| 914 | } |
| 915 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 916 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 917 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 918 | do { \ |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 919 | 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] | 920 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 921 | return BC_STATUS_FAILURE; \ |
| 922 | } while (0) |
| 923 | #else |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 924 | #define QUIT_OR_RETURN_TO_MAIN quit() |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 925 | #endif |
| 926 | |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 927 | static void quit(void) NORETURN; |
| 928 | static void quit(void) |
| 929 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 930 | if (ferror(stdin)) |
| 931 | bb_perror_msg_and_die("input error"); |
| 932 | fflush_and_check(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 933 | dbg_exec("quit(): exiting with exitcode SUCCESS"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 934 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 935 | } |
| 936 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 937 | static void bc_verror_msg(const char *fmt, va_list p) |
| 938 | { |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 939 | const char *sv = sv; // for compiler |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 940 | if (G.prs.filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 941 | sv = applet_name; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 942 | applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.filename, G.err_line); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 943 | } |
| 944 | bb_verror_msg(fmt, p, NULL); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 945 | if (G.prs.filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 946 | free((char*)applet_name); |
| 947 | applet_name = sv; |
| 948 | } |
| 949 | } |
| 950 | |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 951 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 952 | { |
| 953 | va_list p; |
| 954 | |
| 955 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 956 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 957 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 958 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 959 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
| 960 | IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE); |
| 961 | exit(1); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 962 | } |
| 963 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 964 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 965 | static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 966 | { |
| 967 | va_list p; |
| 968 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 969 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 970 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 971 | RETURN_STATUS(BC_STATUS_SUCCESS); // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 972 | |
| 973 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 974 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 975 | va_end(p); |
| 976 | |
| 977 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 978 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 979 | RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning |
| 980 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 981 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 982 | RETURN_STATUS(BC_STATUS_FAILURE); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 983 | exit(1); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 984 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 985 | #define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 986 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 987 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 988 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 989 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 990 | // function must not have caller-cleaned parameters on stack. |
| 991 | // Unfortunately, vararg function API does exactly that on most arches. |
| 992 | // 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] | 993 | static ERRORFUNC int bc_error(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 994 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 995 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 996 | } |
| 997 | static ERRORFUNC int bc_error_bad_character(char c) |
| 998 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 999 | if (!c) |
| 1000 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1001 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1002 | } |
| 1003 | static ERRORFUNC int bc_error_bad_expression(void) |
| 1004 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1005 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1006 | } |
| 1007 | static ERRORFUNC int bc_error_bad_token(void) |
| 1008 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1009 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1010 | } |
| 1011 | static ERRORFUNC int bc_error_stack_has_too_few_elements(void) |
| 1012 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1013 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1014 | } |
| 1015 | static ERRORFUNC int bc_error_variable_is_wrong_type(void) |
| 1016 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1017 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1018 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1019 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1020 | static BC_STATUS zbc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1021 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1022 | RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg)); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1023 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1024 | #define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS) |
| 1025 | static BC_STATUS zbc_POSIX_does_not_allow(const char *msg) |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1026 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1027 | RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg)); |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1028 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1029 | #define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS) |
| 1030 | static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg) |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1031 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1032 | RETURN_STATUS(zbc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; this is bad:", msg)); |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1033 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1034 | #define zbc_POSIX_does_not_allow_bool_ops_this_is_bad(...) (zbc_POSIX_does_not_allow_bool_ops_this_is_bad(__VA_ARGS__) COMMA_SUCCESS) |
| 1035 | static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg) |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1036 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1037 | RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg)); |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1038 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1039 | #define zbc_POSIX_does_not_allow_empty_X_expression_in_for(...) (zbc_POSIX_does_not_allow_empty_X_expression_in_for(__VA_ARGS__) COMMA_SUCCESS) |
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 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1285 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1286 | { |
| 1287 | n->len = 0; |
| 1288 | n->neg = false; |
| 1289 | n->rdx = scale; |
| 1290 | } |
| 1291 | |
| 1292 | static void bc_num_zero(BcNum *n) |
| 1293 | { |
| 1294 | bc_num_setToZero(n, 0); |
| 1295 | } |
| 1296 | |
| 1297 | static void bc_num_one(BcNum *n) |
| 1298 | { |
| 1299 | bc_num_setToZero(n, 0); |
| 1300 | n->len = 1; |
| 1301 | n->num[0] = 1; |
| 1302 | } |
| 1303 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1304 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1305 | static void bc_num_init(BcNum *n, size_t req) |
| 1306 | { |
| 1307 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1308 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1309 | n->num = xmalloc(req); |
| 1310 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1311 | n->rdx = 0; |
| 1312 | n->len = 0; |
| 1313 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1316 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1317 | { |
| 1318 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1319 | } |
| 1320 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1321 | static void bc_num_expand(BcNum *n, size_t req) |
| 1322 | { |
| 1323 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1324 | if (req > n->cap) { |
| 1325 | n->num = xrealloc(n->num, req); |
| 1326 | n->cap = req; |
| 1327 | } |
| 1328 | } |
| 1329 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1330 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1331 | { |
| 1332 | free(((BcNum *) num)->num); |
| 1333 | } |
| 1334 | |
| 1335 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1336 | { |
| 1337 | if (d != s) { |
| 1338 | bc_num_expand(d, s->cap); |
| 1339 | d->len = s->len; |
| 1340 | d->neg = s->neg; |
| 1341 | d->rdx = s->rdx; |
| 1342 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1343 | } |
| 1344 | } |
| 1345 | |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1346 | 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] | 1347 | { |
| 1348 | size_t i; |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1349 | unsigned long result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1350 | |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1351 | result = 0; |
| 1352 | i = n->len; |
| 1353 | while (i > n->rdx) { |
| 1354 | unsigned long prev = result; |
| 1355 | result = result * 10 + n->num[--i]; |
| 1356 | // Even overflowed N*10 can still satisfy N*10>=N. For example, |
| 1357 | // 0x1ff00000 * 10 is 0x13f600000, |
| 1358 | // or 0x3f600000 truncated to 32 bits. Which is larger. |
| 1359 | // However, (N*10)/8 < N check is always correct. |
| 1360 | if ((result / 8) < prev) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1361 | RETURN_STATUS(bc_error("overflow")); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1362 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1363 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1364 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1365 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1366 | } |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1367 | #define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS) |
| 1368 | |
| 1369 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
| 1370 | { |
| 1371 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
| 1372 | |
| 1373 | RETURN_STATUS(zbc_num_ulong_abs(n, result_p)); |
| 1374 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1375 | #define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1376 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1377 | #if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295 |
| 1378 | # define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE) |
| 1379 | #elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615 |
| 1380 | # define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE) |
| 1381 | #endif |
| 1382 | // minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num() |
| 1383 | // would not hit realloc() code path - not good if num[] is not malloced |
| 1384 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1385 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1386 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1387 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1388 | |
| 1389 | bc_num_zero(n); |
| 1390 | |
| 1391 | if (val == 0) return; |
| 1392 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1393 | bc_num_expand(n, ULONG_NUM_BUFSIZE); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1394 | |
| 1395 | ptr = n->num; |
| 1396 | for (;;) { |
| 1397 | n->len++; |
| 1398 | *ptr++ = val % 10; |
| 1399 | val /= 10; |
| 1400 | if (val == 0) break; |
| 1401 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1402 | } |
| 1403 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1404 | 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] | 1405 | { |
| 1406 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1407 | for (i = 0; i < len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1408 | a[i] -= b[i]; |
| 1409 | for (j = i; a[j] < 0;) { |
| 1410 | a[j++] += 10; |
| 1411 | a[j] -= 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1412 | } |
| 1413 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1417 | { |
Denys Vlasenko | 4113e1f | 2018-12-18 00:39:24 +0100 | [diff] [blame] | 1418 | size_t i = len; |
| 1419 | for (;;) { |
| 1420 | int c; |
| 1421 | if (i == 0) |
| 1422 | return 0; |
| 1423 | i--; |
| 1424 | c = a[i] - b[i]; |
| 1425 | if (c != 0) { |
| 1426 | i++; |
| 1427 | if (c < 0) |
| 1428 | return -i; |
| 1429 | return i; |
| 1430 | } |
| 1431 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1432 | } |
| 1433 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1434 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 1435 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 1436 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 1437 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 1438 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 1439 | { |
| 1440 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 1441 | } |
| 1442 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 1443 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 1444 | { |
| 1445 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 1446 | } |
| 1447 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1448 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1449 | { |
| 1450 | size_t i, min, a_int, b_int, diff; |
| 1451 | BcDig *max_num, *min_num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1452 | bool a_max, neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1453 | ssize_t cmp; |
| 1454 | |
| 1455 | if (a == b) return 0; |
| 1456 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1457 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1458 | |
| 1459 | if (a->neg != b->neg) // signs of a and b differ |
| 1460 | // +a,-b = a>b = 1 or -a,+b = a<b = -1 |
| 1461 | return (int)b->neg - (int)a->neg; |
| 1462 | neg = a->neg; // 1 if both negative, 0 if both positive |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1463 | |
| 1464 | a_int = BC_NUM_INT(a); |
| 1465 | b_int = BC_NUM_INT(b); |
| 1466 | a_int -= b_int; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1467 | |
| 1468 | if (a_int != 0) return (ssize_t) a_int; |
| 1469 | |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1470 | a_max = (a->rdx > b->rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1471 | if (a_max) { |
| 1472 | min = b->rdx; |
| 1473 | diff = a->rdx - b->rdx; |
| 1474 | max_num = a->num + diff; |
| 1475 | min_num = b->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1476 | // neg = (a_max == neg); - NOP (maps 1->1 and 0->0) |
| 1477 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1478 | min = a->rdx; |
| 1479 | diff = b->rdx - a->rdx; |
| 1480 | max_num = b->num + diff; |
| 1481 | min_num = a->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1482 | neg = !neg; // same as "neg = (a_max == neg)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1486 | if (cmp != 0) return BC_NUM_NEG(cmp, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1487 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1488 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1489 | if (max_num[i]) return BC_NUM_NEG(1, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1496 | { |
| 1497 | if (places == 0) return; |
| 1498 | |
| 1499 | n->rdx -= places; |
| 1500 | |
| 1501 | if (n->len != 0) { |
| 1502 | n->len -= places; |
| 1503 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | static void bc_num_extend(BcNum *n, size_t places) |
| 1508 | { |
| 1509 | size_t len = n->len + places; |
| 1510 | |
| 1511 | if (places != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1512 | if (n->cap < len) bc_num_expand(n, len); |
| 1513 | |
| 1514 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1515 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1516 | |
| 1517 | n->len += places; |
| 1518 | n->rdx += places; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | static void bc_num_clean(BcNum *n) |
| 1523 | { |
| 1524 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1525 | if (n->len == 0) |
| 1526 | n->neg = false; |
| 1527 | else if (n->len < n->rdx) |
| 1528 | n->len = n->rdx; |
| 1529 | } |
| 1530 | |
| 1531 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1532 | { |
| 1533 | if (n->rdx < scale) |
| 1534 | bc_num_extend(n, scale - n->rdx); |
| 1535 | else |
| 1536 | bc_num_truncate(n, n->rdx - scale); |
| 1537 | |
| 1538 | bc_num_clean(n); |
| 1539 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1540 | } |
| 1541 | |
| 1542 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1543 | BcNum *restrict b) |
| 1544 | { |
| 1545 | if (idx < n->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1546 | b->len = n->len - idx; |
| 1547 | a->len = idx; |
| 1548 | a->rdx = b->rdx = 0; |
| 1549 | |
| 1550 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1551 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1552 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1553 | bc_num_zero(b); |
| 1554 | bc_num_copy(a, n); |
| 1555 | } |
| 1556 | |
| 1557 | bc_num_clean(a); |
| 1558 | bc_num_clean(b); |
| 1559 | } |
| 1560 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1561 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1562 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1563 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1564 | |
| 1565 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1566 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1567 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1568 | 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] | 1569 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1570 | |
| 1571 | if (n->rdx >= places) |
| 1572 | n->rdx -= places; |
| 1573 | else { |
| 1574 | bc_num_extend(n, places - n->rdx); |
| 1575 | n->rdx = 0; |
| 1576 | } |
| 1577 | |
| 1578 | bc_num_clean(n); |
| 1579 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1580 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1581 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1582 | #define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1583 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1584 | typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
| 1585 | |
| 1586 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 1587 | BcNumBinaryOp op, size_t req) |
| 1588 | { |
| 1589 | BcStatus s; |
| 1590 | BcNum num2, *ptr_a, *ptr_b; |
| 1591 | bool init = false; |
| 1592 | |
| 1593 | if (c == a) { |
| 1594 | ptr_a = &num2; |
| 1595 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 1596 | init = true; |
| 1597 | } else |
| 1598 | ptr_a = a; |
| 1599 | |
| 1600 | if (c == b) { |
| 1601 | ptr_b = &num2; |
| 1602 | if (c != a) { |
| 1603 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 1604 | init = true; |
| 1605 | } |
| 1606 | } else |
| 1607 | ptr_b = b; |
| 1608 | |
| 1609 | if (init) |
| 1610 | bc_num_init(c, req); |
| 1611 | else |
| 1612 | bc_num_expand(c, req); |
| 1613 | |
| 1614 | s = BC_STATUS_SUCCESS; |
| 1615 | IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale); |
| 1616 | |
| 1617 | if (init) bc_num_free(&num2); |
| 1618 | |
| 1619 | RETURN_STATUS(s); |
| 1620 | } |
| 1621 | #define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS) |
| 1622 | |
| 1623 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1624 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1625 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1626 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1627 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1628 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1629 | |
| 1630 | static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1631 | { |
| 1632 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s; |
| 1633 | (void) scale; |
| 1634 | RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b))); |
| 1635 | } |
| 1636 | |
| 1637 | static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1638 | { |
| 1639 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a; |
| 1640 | (void) scale; |
| 1641 | RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b))); |
| 1642 | } |
| 1643 | |
| 1644 | static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1645 | { |
| 1646 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1647 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req)); |
| 1648 | } |
| 1649 | |
| 1650 | static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1651 | { |
| 1652 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1653 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req)); |
| 1654 | } |
| 1655 | |
| 1656 | static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1657 | { |
| 1658 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1659 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req)); |
| 1660 | } |
| 1661 | |
| 1662 | static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1663 | { |
| 1664 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); |
| 1665 | } |
| 1666 | |
| 1667 | static const BcNumBinaryOp zbc_program_ops[] = { |
| 1668 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, |
| 1669 | }; |
| 1670 | #define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS) |
| 1671 | #define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS) |
| 1672 | #define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS) |
| 1673 | #define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS) |
| 1674 | #define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS) |
| 1675 | #define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS) |
| 1676 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1677 | 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] | 1678 | { |
| 1679 | BcNum one; |
| 1680 | BcDig num[2]; |
| 1681 | |
| 1682 | one.cap = 2; |
| 1683 | one.num = num; |
| 1684 | bc_num_one(&one); |
| 1685 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1686 | RETURN_STATUS(zbc_num_div(&one, a, b, scale)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1687 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1688 | #define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1689 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1690 | 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] | 1691 | { |
| 1692 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1693 | 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] | 1694 | unsigned carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1695 | |
| 1696 | // Because this function doesn't need to use scale (per the bc spec), |
| 1697 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1698 | |
| 1699 | if (a->len == 0) { |
| 1700 | bc_num_copy(c, b); |
| 1701 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1702 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1703 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1704 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1705 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1706 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | c->neg = a->neg; |
| 1710 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1711 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1712 | c->len = 0; |
| 1713 | |
| 1714 | if (a->rdx > b->rdx) { |
| 1715 | diff = a->rdx - b->rdx; |
| 1716 | ptr = a->num; |
| 1717 | ptr_a = a->num + diff; |
| 1718 | ptr_b = b->num; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1719 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1720 | diff = b->rdx - a->rdx; |
| 1721 | ptr = b->num; |
| 1722 | ptr_a = a->num; |
| 1723 | ptr_b = b->num + diff; |
| 1724 | } |
| 1725 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1726 | ptr_c = c->num; |
| 1727 | for (i = 0; i < diff; ++i, ++c->len) |
| 1728 | ptr_c[i] = ptr[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1729 | |
| 1730 | ptr_c += diff; |
| 1731 | a_int = BC_NUM_INT(a); |
| 1732 | b_int = BC_NUM_INT(b); |
| 1733 | |
| 1734 | if (a_int > b_int) { |
| 1735 | min_int = b_int; |
| 1736 | max = a_int; |
| 1737 | ptr = ptr_a; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1738 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1739 | min_int = a_int; |
| 1740 | max = b_int; |
| 1741 | ptr = ptr_b; |
| 1742 | } |
| 1743 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1744 | carry = 0; |
| 1745 | for (i = 0; i < min_rdx + min_int; ++i) { |
| 1746 | unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1747 | carry = in / 10; |
| 1748 | ptr_c[i] = (BcDig)(in % 10); |
| 1749 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1750 | for (; i < max + min_rdx; ++i) { |
| 1751 | unsigned in = (unsigned)ptr[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1752 | carry = in / 10; |
| 1753 | ptr_c[i] = (BcDig)(in % 10); |
| 1754 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1755 | c->len += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1756 | |
| 1757 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1758 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1759 | 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] | 1760 | } |
| 1761 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1762 | 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] | 1763 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1764 | ssize_t cmp; |
| 1765 | BcNum *minuend, *subtrahend; |
| 1766 | size_t start; |
| 1767 | bool aneg, bneg, neg; |
| 1768 | |
| 1769 | // Because this function doesn't need to use scale (per the bc spec), |
| 1770 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1771 | |
| 1772 | if (a->len == 0) { |
| 1773 | bc_num_copy(c, b); |
| 1774 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1775 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1776 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1777 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1778 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1779 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | aneg = a->neg; |
| 1783 | bneg = b->neg; |
| 1784 | a->neg = b->neg = false; |
| 1785 | |
| 1786 | cmp = bc_num_cmp(a, b); |
| 1787 | |
| 1788 | a->neg = aneg; |
| 1789 | b->neg = bneg; |
| 1790 | |
| 1791 | if (cmp == 0) { |
| 1792 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1793 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1794 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1795 | if (cmp > 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1796 | neg = a->neg; |
| 1797 | minuend = a; |
| 1798 | subtrahend = b; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1799 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1800 | neg = b->neg; |
| 1801 | if (sub) neg = !neg; |
| 1802 | minuend = b; |
| 1803 | subtrahend = a; |
| 1804 | } |
| 1805 | |
| 1806 | bc_num_copy(c, minuend); |
| 1807 | c->neg = neg; |
| 1808 | |
| 1809 | if (c->rdx < subtrahend->rdx) { |
| 1810 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1811 | start = 0; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1812 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1813 | start = c->rdx - subtrahend->rdx; |
| 1814 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1815 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1816 | |
| 1817 | bc_num_clean(c); |
| 1818 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1819 | 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] | 1820 | } |
| 1821 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1822 | 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] | 1823 | BcNum *restrict c) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1824 | #define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1825 | { |
| 1826 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1827 | 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] | 1828 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1829 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1830 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1831 | if (a->len == 0 || b->len == 0) { |
| 1832 | bc_num_zero(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1833 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1834 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1835 | aone = BC_NUM_ONE(a); |
| 1836 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1837 | bc_num_copy(c, aone ? b : a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1838 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1839 | } |
| 1840 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1841 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN |
| 1842 | || a->len < BC_NUM_KARATSUBA_LEN |
| 1843 | || b->len < BC_NUM_KARATSUBA_LEN |
| 1844 | ) { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1845 | size_t i, j, len; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1846 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1847 | bc_num_expand(c, a->len + b->len + 1); |
| 1848 | |
| 1849 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1850 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1851 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1852 | for (i = 0; i < b->len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1853 | unsigned carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1854 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1855 | unsigned in = c->num[i + j]; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1856 | in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1857 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1858 | carry = in / 10; |
| 1859 | c->num[i + j] = (BcDig)(in % 10); |
| 1860 | } |
| 1861 | |
| 1862 | c->num[i + j] += (BcDig) carry; |
| 1863 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1864 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1865 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1866 | // a=2^1000000 |
| 1867 | // a*a <- without check below, this will not be interruptible |
| 1868 | if (G_interrupt) return BC_STATUS_FAILURE; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1869 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | c->len = len; |
| 1873 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1874 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | bc_num_init(&l1, max); |
| 1878 | bc_num_init(&h1, max); |
| 1879 | bc_num_init(&l2, max); |
| 1880 | bc_num_init(&h2, max); |
| 1881 | bc_num_init(&m1, max); |
| 1882 | bc_num_init(&m2, max); |
| 1883 | bc_num_init(&z0, max); |
| 1884 | bc_num_init(&z1, max); |
| 1885 | bc_num_init(&z2, max); |
| 1886 | bc_num_init(&temp, max + max); |
| 1887 | |
| 1888 | bc_num_split(a, max2, &l1, &h1); |
| 1889 | bc_num_split(b, max2, &l2, &h2); |
| 1890 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1891 | s = zbc_num_add(&h1, &l1, &m1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1892 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1893 | s = zbc_num_add(&h2, &l2, &m2, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1894 | if (s) goto err; |
| 1895 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1896 | s = zbc_num_k(&h1, &h2, &z0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1897 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1898 | s = zbc_num_k(&m1, &m2, &z1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1899 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1900 | s = zbc_num_k(&l1, &l2, &z2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1901 | if (s) goto err; |
| 1902 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1903 | s = zbc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1904 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1905 | s = zbc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1906 | if (s) goto err; |
| 1907 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1908 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1909 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1910 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1911 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1912 | s = zbc_num_add(&z0, &z1, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1913 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1914 | s = zbc_num_add(&temp, &z2, c, 0); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1915 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1916 | bc_num_free(&temp); |
| 1917 | bc_num_free(&z2); |
| 1918 | bc_num_free(&z1); |
| 1919 | bc_num_free(&z0); |
| 1920 | bc_num_free(&m2); |
| 1921 | bc_num_free(&m1); |
| 1922 | bc_num_free(&h2); |
| 1923 | bc_num_free(&l2); |
| 1924 | bc_num_free(&h1); |
| 1925 | bc_num_free(&l1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1926 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1927 | } |
| 1928 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1929 | 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] | 1930 | { |
| 1931 | BcStatus s; |
| 1932 | BcNum cpa, cpb; |
| 1933 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1934 | |
| 1935 | scale = BC_MAX(scale, a->rdx); |
| 1936 | scale = BC_MAX(scale, b->rdx); |
| 1937 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1938 | maxrdx = BC_MAX(maxrdx, scale); |
| 1939 | |
| 1940 | bc_num_init(&cpa, a->len); |
| 1941 | bc_num_init(&cpb, b->len); |
| 1942 | |
| 1943 | bc_num_copy(&cpa, a); |
| 1944 | bc_num_copy(&cpb, b); |
| 1945 | cpa.neg = cpb.neg = false; |
| 1946 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1947 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1948 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1949 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1950 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1951 | s = zbc_num_k(&cpa, &cpb, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1952 | if (s) goto err; |
| 1953 | |
| 1954 | maxrdx += scale; |
| 1955 | bc_num_expand(c, c->len + maxrdx); |
| 1956 | |
| 1957 | if (c->len < maxrdx) { |
| 1958 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 1959 | c->len += maxrdx; |
| 1960 | } |
| 1961 | |
| 1962 | c->rdx = maxrdx; |
| 1963 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1964 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1965 | bc_num_free(&cpb); |
| 1966 | bc_num_free(&cpa); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1967 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1968 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1969 | #define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1970 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1971 | 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] | 1972 | { |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 1973 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1974 | size_t len, end, i; |
| 1975 | BcNum cp; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1976 | |
| 1977 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1978 | RETURN_STATUS(bc_error("divide by zero")); |
| 1979 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1980 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1981 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1982 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1983 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1984 | bc_num_copy(c, a); |
| 1985 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1986 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 1990 | bc_num_copy(&cp, a); |
| 1991 | len = b->len; |
| 1992 | |
| 1993 | if (len > cp.len) { |
| 1994 | bc_num_expand(&cp, len + 2); |
| 1995 | bc_num_extend(&cp, len - cp.len); |
| 1996 | } |
| 1997 | |
| 1998 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 1999 | cp.rdx -= b->rdx; |
| 2000 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 2001 | |
| 2002 | if (b->rdx == b->len) { |
Denys Vlasenko | 71c82d1 | 2018-12-18 12:43:21 +0100 | [diff] [blame] | 2003 | for (;;) { |
| 2004 | if (len == 0) break; |
| 2005 | len--; |
| 2006 | if (b->num[len] != 0) |
| 2007 | break; |
| 2008 | } |
| 2009 | len++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 2013 | |
| 2014 | // We want an extra zero in front to make things simpler. |
| 2015 | cp.num[cp.len++] = 0; |
| 2016 | end = cp.len - len; |
| 2017 | |
| 2018 | bc_num_expand(c, cp.len); |
| 2019 | |
| 2020 | bc_num_zero(c); |
| 2021 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2022 | c->rdx = cp.rdx; |
| 2023 | c->len = cp.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2024 | |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2025 | s = BC_STATUS_SUCCESS; |
| 2026 | for (i = end - 1; i < end; --i) { |
| 2027 | BcDig *n, q; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2028 | n = cp.num + i; |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2029 | for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q) |
| 2030 | bc_num_subArrays(n, b->num, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2031 | c->num[i] = q; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2032 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2033 | // a=2^100000 |
| 2034 | // scale=40000 |
| 2035 | // 1/a <- without check below, this will not be interruptible |
| 2036 | if (G_interrupt) { |
| 2037 | s = BC_STATUS_FAILURE; |
| 2038 | break; |
| 2039 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2040 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2041 | } |
| 2042 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2043 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2044 | bc_num_free(&cp); |
| 2045 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2046 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2047 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2048 | #define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2049 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2050 | 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] | 2051 | BcNum *restrict d, size_t scale, size_t ts) |
| 2052 | { |
| 2053 | BcStatus s; |
| 2054 | BcNum temp; |
| 2055 | bool neg; |
| 2056 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2057 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2058 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2059 | |
| 2060 | if (a->len == 0) { |
| 2061 | bc_num_setToZero(d, ts); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2062 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2066 | s = zbc_num_d(a, b, c, scale); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2067 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2068 | |
| 2069 | if (scale != 0) scale = ts; |
| 2070 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2071 | s = zbc_num_m(c, b, &temp, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2072 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2073 | s = zbc_num_sub(a, &temp, d, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2074 | if (s) goto err; |
| 2075 | |
| 2076 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2077 | |
| 2078 | neg = d->neg; |
| 2079 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2080 | d->neg = neg; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2081 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2082 | bc_num_free(&temp); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2083 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2084 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2085 | #define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2086 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2087 | 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] | 2088 | { |
| 2089 | BcStatus s; |
| 2090 | BcNum c1; |
| 2091 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2092 | |
| 2093 | bc_num_init(&c1, len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2094 | s = zbc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2095 | bc_num_free(&c1); |
| 2096 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2097 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2098 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2099 | #define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2100 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2101 | 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] | 2102 | { |
| 2103 | BcStatus s = BC_STATUS_SUCCESS; |
| 2104 | BcNum copy; |
| 2105 | unsigned long pow; |
| 2106 | size_t i, powrdx, resrdx; |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2107 | bool neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2108 | |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2109 | // GNU bc does not allow 2^2.0 - we do |
| 2110 | for (i = 0; i < b->rdx; i++) |
| 2111 | if (b->num[i] != 0) |
| 2112 | RETURN_STATUS(bc_error("not an integer")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2113 | |
| 2114 | if (b->len == 0) { |
| 2115 | bc_num_one(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2116 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2117 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2118 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2119 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2120 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2121 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2122 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2123 | if (!b->neg) |
| 2124 | bc_num_copy(c, a); |
| 2125 | else |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2126 | s = zbc_num_inv(a, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2127 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | neg = b->neg; |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 2131 | s = zbc_num_ulong_abs(b, &pow); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2132 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 2ea8ddf | 2018-12-22 21:45:18 +0100 | [diff] [blame] | 2133 | // b is not used beyond this point |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2134 | |
| 2135 | bc_num_init(©, a->len); |
| 2136 | bc_num_copy(©, a); |
| 2137 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2138 | if (!neg) { |
| 2139 | if (a->rdx > scale) |
| 2140 | scale = a->rdx; |
| 2141 | if (a->rdx * pow < scale) |
| 2142 | scale = a->rdx * pow; |
| 2143 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2144 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2145 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2146 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2147 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2148 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2149 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2150 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2151 | //if (G_interrupt) { |
| 2152 | // s = BC_STATUS_FAILURE; |
| 2153 | // goto err; |
| 2154 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2155 | } |
| 2156 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2157 | bc_num_copy(c, ©); |
| 2158 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2159 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2160 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2161 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2162 | if (s) goto err; |
| 2163 | |
| 2164 | if (pow & 1) { |
| 2165 | resrdx += powrdx; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2166 | s = zbc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2167 | if (s) goto err; |
| 2168 | } |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2169 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2170 | //if (G_interrupt) { |
| 2171 | // s = BC_STATUS_FAILURE; |
| 2172 | // goto err; |
| 2173 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | if (neg) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2177 | s = zbc_num_inv(c, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2178 | if (s) goto err; |
| 2179 | } |
| 2180 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2181 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2182 | |
| 2183 | // We can't use bc_num_clean() here. |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2184 | for (i = 0; i < c->len; ++i) |
| 2185 | if (c->num[i] != 0) |
| 2186 | goto skip; |
| 2187 | bc_num_setToZero(c, scale); |
| 2188 | skip: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2189 | |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2190 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2191 | bc_num_free(©); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2192 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2193 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2194 | #define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2195 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2196 | 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] | 2197 | { |
| 2198 | BcStatus s; |
| 2199 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
| 2200 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2201 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2202 | |
| 2203 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2204 | bc_num_expand(b, req); |
| 2205 | |
| 2206 | if (a->len == 0) { |
| 2207 | bc_num_setToZero(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2208 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2209 | } |
| 2210 | if (a->neg) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2211 | RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2212 | } |
| 2213 | if (BC_NUM_ONE(a)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2214 | bc_num_one(b); |
| 2215 | bc_num_extend(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2216 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2220 | len = a->len + scale; |
| 2221 | |
| 2222 | bc_num_init(&num1, len); |
| 2223 | bc_num_init(&num2, len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2224 | bc_num_init_DEF_SIZE(&half); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2225 | |
| 2226 | bc_num_one(&half); |
| 2227 | half.num[0] = 5; |
| 2228 | half.rdx = 1; |
| 2229 | |
| 2230 | bc_num_init(&f, len); |
| 2231 | bc_num_init(&fprime, len); |
| 2232 | |
| 2233 | x0 = &num1; |
| 2234 | x1 = &num2; |
| 2235 | |
| 2236 | bc_num_one(x0); |
| 2237 | pow = BC_NUM_INT(a); |
| 2238 | |
| 2239 | if (pow) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2240 | if (pow & 1) |
| 2241 | x0->num[0] = 2; |
| 2242 | else |
| 2243 | x0->num[0] = 6; |
| 2244 | |
| 2245 | pow -= 2 - (pow & 1); |
| 2246 | |
| 2247 | bc_num_extend(x0, pow); |
| 2248 | |
| 2249 | // Make sure to move the radix back. |
| 2250 | x0->rdx -= pow; |
| 2251 | } |
| 2252 | |
| 2253 | x0->rdx = digs = digs1 = 0; |
| 2254 | resrdx = scale + 2; |
| 2255 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2256 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2257 | while (cmp != 0 || digs < len) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2258 | s = zbc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2259 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2260 | s = zbc_num_add(x0, &f, &fprime, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2261 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2262 | s = zbc_num_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2263 | if (s) goto err; |
| 2264 | |
| 2265 | cmp = bc_num_cmp(x1, x0); |
| 2266 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2267 | |
| 2268 | if (cmp == cmp2 && digs == digs1) |
| 2269 | times += 1; |
| 2270 | else |
| 2271 | times = 0; |
| 2272 | |
| 2273 | resrdx += times > 4; |
| 2274 | |
| 2275 | cmp2 = cmp1; |
| 2276 | cmp1 = cmp; |
| 2277 | digs1 = digs; |
| 2278 | |
| 2279 | temp = x0; |
| 2280 | x0 = x1; |
| 2281 | x1 = temp; |
| 2282 | } |
| 2283 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2284 | bc_num_copy(b, x0); |
| 2285 | scale -= 1; |
| 2286 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2287 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2288 | bc_num_free(&fprime); |
| 2289 | bc_num_free(&f); |
| 2290 | bc_num_free(&half); |
| 2291 | bc_num_free(&num2); |
| 2292 | bc_num_free(&num1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2293 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2294 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2295 | #define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2296 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2297 | 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] | 2298 | size_t scale) |
| 2299 | { |
| 2300 | BcStatus s; |
| 2301 | BcNum num2, *ptr_a; |
| 2302 | bool init = false; |
| 2303 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2304 | |
| 2305 | if (c == a) { |
| 2306 | memcpy(&num2, c, sizeof(BcNum)); |
| 2307 | ptr_a = &num2; |
| 2308 | bc_num_init(c, len); |
| 2309 | init = true; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2310 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2311 | ptr_a = a; |
| 2312 | bc_num_expand(c, len); |
| 2313 | } |
| 2314 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2315 | s = zbc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2316 | |
| 2317 | if (init) bc_num_free(&num2); |
| 2318 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2319 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2320 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2321 | #define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2322 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2323 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2324 | 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] | 2325 | { |
| 2326 | BcStatus s; |
| 2327 | BcNum base, exp, two, temp; |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2328 | BcDig two_digs[2]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2329 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2330 | if (c->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2331 | RETURN_STATUS(bc_error("divide by zero")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2332 | if (a->rdx || b->rdx || c->rdx) |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2333 | RETURN_STATUS(bc_error("not an integer")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2334 | if (b->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2335 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2336 | |
| 2337 | bc_num_expand(d, c->len); |
| 2338 | bc_num_init(&base, c->len); |
| 2339 | bc_num_init(&exp, b->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2340 | bc_num_init(&temp, b->len); |
| 2341 | |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2342 | two.cap = ARRAY_SIZE(two_digs); |
| 2343 | two.num = two_digs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2344 | bc_num_one(&two); |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2345 | two_digs[0] = 2; |
| 2346 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2347 | bc_num_one(d); |
| 2348 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2349 | s = zbc_num_rem(a, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2350 | if (s) goto err; |
| 2351 | bc_num_copy(&exp, b); |
| 2352 | |
| 2353 | while (exp.len != 0) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2354 | s = zbc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2355 | if (s) goto err; |
| 2356 | |
| 2357 | if (BC_NUM_ONE(&temp)) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2358 | s = zbc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2359 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2360 | s = zbc_num_rem(&temp, c, d, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2361 | if (s) goto err; |
| 2362 | } |
| 2363 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2364 | s = zbc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2365 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2366 | s = zbc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2367 | if (s) goto err; |
| 2368 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2369 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2370 | bc_num_free(&temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2371 | bc_num_free(&exp); |
| 2372 | bc_num_free(&base); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2373 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2374 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2375 | #define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2376 | #endif // ENABLE_DC |
| 2377 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2378 | static FAST_FUNC void bc_string_free(void *string) |
| 2379 | { |
| 2380 | free(*(char**)string); |
| 2381 | } |
| 2382 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2383 | static void bc_func_init(BcFunc *f) |
| 2384 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2385 | bc_char_vec_init(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2386 | IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);) |
| 2387 | IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2388 | IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);) |
| 2389 | IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2390 | IF_BC(f->nparams = 0;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2391 | } |
| 2392 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2393 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2394 | { |
| 2395 | BcFunc *f = (BcFunc *) func; |
| 2396 | bc_vec_free(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2397 | IF_BC(bc_vec_free(&f->labels);) |
| 2398 | IF_BC(bc_vec_free(&f->autos);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2399 | IF_BC(bc_vec_free(&f->strs);) |
| 2400 | IF_BC(bc_vec_free(&f->consts);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2401 | } |
| 2402 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2403 | static void bc_array_expand(BcVec *a, size_t len); |
| 2404 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2405 | static void bc_array_init(BcVec *a, bool nums) |
| 2406 | { |
| 2407 | if (nums) |
| 2408 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2409 | else |
| 2410 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2411 | bc_array_expand(a, 1); |
| 2412 | } |
| 2413 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2414 | static void bc_array_expand(BcVec *a, size_t len) |
| 2415 | { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2416 | if (a->dtor == bc_num_free |
| 2417 | // && a->size == sizeof(BcNum) - always true |
| 2418 | ) { |
| 2419 | BcNum n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2420 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2421 | bc_num_init_DEF_SIZE(&n); |
| 2422 | bc_vec_push(a, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2423 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2424 | } else { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2425 | BcVec v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2426 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2427 | bc_array_init(&v, true); |
| 2428 | bc_vec_push(a, &v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2429 | } |
| 2430 | } |
| 2431 | } |
| 2432 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2433 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2434 | { |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2435 | BcNum *dnum, *snum; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2436 | size_t i; |
| 2437 | |
| 2438 | bc_vec_pop_all(d); |
| 2439 | bc_vec_expand(d, s->cap); |
| 2440 | d->len = s->len; |
| 2441 | |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2442 | dnum = (void*)d->v; |
| 2443 | snum = (void*)s->v; |
| 2444 | for (i = 0; i < s->len; i++, dnum++, snum++) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2445 | bc_num_init(dnum, snum->len); |
| 2446 | bc_num_copy(dnum, snum); |
| 2447 | } |
| 2448 | } |
| 2449 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2450 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2451 | static void dc_result_copy(BcResult *d, BcResult *src) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2452 | { |
| 2453 | d->t = src->t; |
| 2454 | |
| 2455 | switch (d->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2456 | case XC_RESULT_TEMP: |
| 2457 | case XC_RESULT_IBASE: |
| 2458 | case XC_RESULT_SCALE: |
| 2459 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2460 | bc_num_init(&d->d.n, src->d.n.len); |
| 2461 | bc_num_copy(&d->d.n, &src->d.n); |
| 2462 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2463 | case XC_RESULT_VAR: |
| 2464 | case XC_RESULT_ARRAY: |
| 2465 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2466 | d->d.id.name = xstrdup(src->d.id.name); |
| 2467 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2468 | case XC_RESULT_CONSTANT: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 2469 | IF_BC(case BC_RESULT_LAST:) |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2470 | IF_BC(case BC_RESULT_ONE:) |
| 2471 | case XC_RESULT_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2472 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2473 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | #endif // ENABLE_DC |
| 2477 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2478 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2479 | { |
| 2480 | BcResult *r = (BcResult *) result; |
| 2481 | |
| 2482 | switch (r->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2483 | case XC_RESULT_TEMP: |
| 2484 | case XC_RESULT_IBASE: |
| 2485 | case XC_RESULT_SCALE: |
| 2486 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2487 | bc_num_free(&r->d.n); |
| 2488 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2489 | case XC_RESULT_VAR: |
| 2490 | case XC_RESULT_ARRAY: |
| 2491 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2492 | free(r->d.id.name); |
| 2493 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2494 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2495 | // Do nothing. |
| 2496 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2497 | } |
| 2498 | } |
| 2499 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2500 | static int bad_input_byte(char c) |
| 2501 | { |
| 2502 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 2503 | || c > 0x7e |
| 2504 | ) { |
| 2505 | bc_error_fmt("illegal character 0x%02x", c); |
| 2506 | return 1; |
| 2507 | } |
| 2508 | return 0; |
| 2509 | } |
| 2510 | |
| 2511 | // Note: it _appends_ data from fp to vec. |
| 2512 | static void bc_read_line(BcVec *vec, FILE *fp) |
| 2513 | { |
| 2514 | again: |
| 2515 | fflush_and_check(); |
| 2516 | |
| 2517 | #if ENABLE_FEATURE_BC_SIGNALS |
| 2518 | if (G_interrupt) { // ^C was pressed |
| 2519 | intr: |
| 2520 | if (fp != stdin) { |
| 2521 | // ^C while running a script (bc SCRIPT): die. |
| 2522 | // We do not return to interactive prompt: |
| 2523 | // user might be running us from a shell, |
| 2524 | // and SCRIPT might be intended to terminate |
| 2525 | // (e.g. contain a "halt" stmt). |
| 2526 | // ^C dropping user into a bc prompt instead of |
| 2527 | // the shell would be unexpected. |
| 2528 | xfunc_die(); |
| 2529 | } |
| 2530 | // ^C while interactive input |
| 2531 | G_interrupt = 0; |
| 2532 | // GNU bc says "interrupted execution." |
| 2533 | // GNU dc says "Interrupt!" |
| 2534 | fputs("\ninterrupted execution\n", stderr); |
| 2535 | } |
| 2536 | |
| 2537 | # if ENABLE_FEATURE_EDITING |
| 2538 | if (G_ttyin && fp == stdin) { |
| 2539 | int n, i; |
| 2540 | # define line_buf bb_common_bufsiz1 |
| 2541 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
| 2542 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 2543 | if (n == 0) // ^C |
| 2544 | goto intr; |
| 2545 | bc_vec_pushZeroByte(vec); // ^D or EOF (or error) |
| 2546 | return; |
| 2547 | } |
| 2548 | i = 0; |
| 2549 | for (;;) { |
| 2550 | char c = line_buf[i++]; |
| 2551 | if (!c) break; |
| 2552 | if (bad_input_byte(c)) goto again; |
| 2553 | } |
| 2554 | bc_vec_concat(vec, line_buf); |
| 2555 | # undef line_buf |
| 2556 | } else |
| 2557 | # endif |
| 2558 | #endif |
| 2559 | { |
| 2560 | int c; |
| 2561 | bool bad_chars = 0; |
| 2562 | size_t len = vec->len; |
| 2563 | |
| 2564 | do { |
| 2565 | #if ENABLE_FEATURE_BC_SIGNALS |
| 2566 | if (G_interrupt) { |
| 2567 | // ^C was pressed: ignore entire line, get another one |
| 2568 | vec->len = len; |
| 2569 | goto intr; |
| 2570 | } |
| 2571 | #endif |
| 2572 | do c = fgetc(fp); while (c == '\0'); |
| 2573 | if (c == EOF) { |
| 2574 | if (ferror(fp)) |
| 2575 | bb_perror_msg_and_die("input error"); |
| 2576 | // Note: EOF does not append '\n' |
| 2577 | break; |
| 2578 | } |
| 2579 | bad_chars |= bad_input_byte(c); |
| 2580 | bc_vec_pushByte(vec, (char)c); |
| 2581 | } while (c != '\n'); |
| 2582 | |
| 2583 | if (bad_chars) { |
| 2584 | // Bad chars on this line |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 2585 | if (!G.prs.filename) { // stdin |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2586 | // ignore entire line, get another one |
| 2587 | vec->len = len; |
| 2588 | goto again; |
| 2589 | } |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 2590 | bb_perror_msg_and_die("file '%s' is not text", G.prs.filename); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2591 | } |
| 2592 | bc_vec_pushZeroByte(vec); |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | // |
| 2597 | // Parsing routines |
| 2598 | // |
| 2599 | |
| 2600 | static bool bc_num_strValid(const char *val, size_t base) |
| 2601 | { |
| 2602 | BcDig b; |
| 2603 | bool radix; |
| 2604 | |
| 2605 | b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A'); |
| 2606 | radix = false; |
| 2607 | for (;;) { |
| 2608 | BcDig c = *val++; |
| 2609 | if (c == '\0') |
| 2610 | break; |
| 2611 | if (c == '.') { |
| 2612 | if (radix) return false; |
| 2613 | radix = true; |
| 2614 | continue; |
| 2615 | } |
| 2616 | if (c < '0' || c >= b || (c > '9' && c < 'A')) |
| 2617 | return false; |
| 2618 | } |
| 2619 | return true; |
| 2620 | } |
| 2621 | |
| 2622 | // Note: n is already "bc_num_zero()"ed, |
| 2623 | // leading zeroes in "val" are removed |
| 2624 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2625 | { |
| 2626 | size_t len, i; |
| 2627 | const char *ptr; |
| 2628 | |
| 2629 | len = strlen(val); |
| 2630 | if (len == 0) |
| 2631 | return; |
| 2632 | |
| 2633 | bc_num_expand(n, len); |
| 2634 | |
| 2635 | ptr = strchr(val, '.'); |
| 2636 | |
| 2637 | n->rdx = 0; |
| 2638 | if (ptr != NULL) |
| 2639 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2640 | |
| 2641 | for (i = 0; val[i]; ++i) { |
| 2642 | if (val[i] != '0' && val[i] != '.') { |
| 2643 | // Not entirely zero value - convert it, and exit |
| 2644 | i = len - 1; |
| 2645 | for (;;) { |
| 2646 | n->num[n->len] = val[i] - '0'; |
| 2647 | ++n->len; |
| 2648 | skip_dot: |
| 2649 | if (i == 0) break; |
| 2650 | if (val[--i] == '.') goto skip_dot; |
| 2651 | } |
| 2652 | break; |
| 2653 | } |
| 2654 | } |
| 2655 | // if for() exits without hitting if(), the value is entirely zero |
| 2656 | } |
| 2657 | |
| 2658 | // Note: n is already "bc_num_zero()"ed, |
| 2659 | // leading zeroes in "val" are removed |
| 2660 | static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t) |
| 2661 | { |
| 2662 | BcStatus s; |
| 2663 | BcNum temp, mult, result; |
| 2664 | BcNum base; |
| 2665 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
| 2666 | BcDig c = '\0'; |
| 2667 | unsigned long v; |
| 2668 | size_t i, digits; |
| 2669 | |
| 2670 | for (i = 0; ; ++i) { |
| 2671 | if (val[i] == '\0') |
| 2672 | return; |
| 2673 | if (val[i] != '.' && val[i] != '0') |
| 2674 | break; |
| 2675 | } |
| 2676 | |
| 2677 | bc_num_init_DEF_SIZE(&temp); |
| 2678 | bc_num_init_DEF_SIZE(&mult); |
| 2679 | base.cap = ARRAY_SIZE(base_digs); |
| 2680 | base.num = base_digs; |
| 2681 | bc_num_ulong2num(&base, base_t); |
| 2682 | |
| 2683 | for (;;) { |
| 2684 | c = *val++; |
| 2685 | if (c == '\0') goto int_err; |
| 2686 | if (c == '.') break; |
| 2687 | |
| 2688 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2689 | |
| 2690 | s = zbc_num_mul(n, &base, &mult, 0); |
| 2691 | if (s) goto int_err; |
| 2692 | bc_num_ulong2num(&temp, v); |
| 2693 | s = zbc_num_add(&mult, &temp, n, 0); |
| 2694 | if (s) goto int_err; |
| 2695 | } |
| 2696 | |
| 2697 | bc_num_init(&result, base.len); |
| 2698 | //bc_num_zero(&result); - already is |
| 2699 | bc_num_one(&mult); |
| 2700 | |
| 2701 | digits = 0; |
| 2702 | for (;;) { |
| 2703 | c = *val++; |
| 2704 | if (c == '\0') break; |
| 2705 | digits++; |
| 2706 | |
| 2707 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2708 | |
| 2709 | s = zbc_num_mul(&result, &base, &result, 0); |
| 2710 | if (s) goto err; |
| 2711 | bc_num_ulong2num(&temp, v); |
| 2712 | s = zbc_num_add(&result, &temp, &result, 0); |
| 2713 | if (s) goto err; |
| 2714 | s = zbc_num_mul(&mult, &base, &mult, 0); |
| 2715 | if (s) goto err; |
| 2716 | } |
| 2717 | |
| 2718 | s = zbc_num_div(&result, &mult, &result, digits); |
| 2719 | if (s) goto err; |
| 2720 | s = zbc_num_add(n, &result, n, digits); |
| 2721 | if (s) goto err; |
| 2722 | |
| 2723 | if (n->len != 0) { |
| 2724 | if (n->rdx < digits) |
| 2725 | bc_num_extend(n, digits - n->rdx); |
| 2726 | } else |
| 2727 | bc_num_zero(n); |
| 2728 | err: |
| 2729 | bc_num_free(&result); |
| 2730 | int_err: |
| 2731 | bc_num_free(&mult); |
| 2732 | bc_num_free(&temp); |
| 2733 | } |
| 2734 | |
| 2735 | static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t) |
| 2736 | { |
| 2737 | if (!bc_num_strValid(val, base_t)) |
| 2738 | RETURN_STATUS(bc_error("bad number string")); |
| 2739 | |
| 2740 | bc_num_zero(n); |
| 2741 | while (*val == '0') val++; |
| 2742 | |
| 2743 | if (base_t == 10) |
| 2744 | bc_num_parseDecimal(n, val); |
| 2745 | else |
| 2746 | bc_num_parseBase(n, val, base_t); |
| 2747 | |
| 2748 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2749 | } |
| 2750 | #define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS) |
| 2751 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2752 | static void bc_lex_lineComment(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2753 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2754 | BcLex *l = &G.prs.l; |
Denys Vlasenko | 55f3cab | 2018-12-18 14:37:16 +0100 | [diff] [blame] | 2755 | // Try: echo -n '#foo' | bc |
| 2756 | size_t i; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 2757 | l->lex = XC_LEX_WHITESPACE; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2758 | i = l->lex_i; |
| 2759 | while (i < l->lex_len && l->lex_inbuf[i] != '\n') |
Denys Vlasenko | 55f3cab | 2018-12-18 14:37:16 +0100 | [diff] [blame] | 2760 | i++; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2761 | l->lex_i = i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2762 | } |
| 2763 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2764 | static void bc_lex_whitespace(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2765 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2766 | BcLex *l = &G.prs.l; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 2767 | l->lex = XC_LEX_WHITESPACE; |
Denys Vlasenko | 89198a9 | 2018-12-13 21:31:29 +0100 | [diff] [blame] | 2768 | for (;;) { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2769 | char c = l->lex_inbuf[l->lex_i]; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 2770 | if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 2771 | break; |
| 2772 | if (!isspace(c)) |
Denys Vlasenko | 89198a9 | 2018-12-13 21:31:29 +0100 | [diff] [blame] | 2773 | break; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2774 | l->lex_i++; |
Denys Vlasenko | 89198a9 | 2018-12-13 21:31:29 +0100 | [diff] [blame] | 2775 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2776 | } |
| 2777 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2778 | static BC_STATUS zbc_lex_number(char start) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2779 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2780 | BcLex *l = &G.prs.l; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2781 | const char *buf = l->lex_inbuf + l->lex_i; |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 2782 | size_t len, i, ccnt; |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2783 | bool pt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2784 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2785 | pt = (start == '.'); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 2786 | l->lex = XC_LEX_NUMBER; |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2787 | ccnt = i = 0; |
| 2788 | for (;;) { |
| 2789 | char c = buf[i]; |
| 2790 | if (c == '\0') |
| 2791 | break; |
| 2792 | if (c == '\\' && buf[i + 1] == '\n') { |
| 2793 | i += 2; |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 2794 | //number_of_backslashes++ - see comment below |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2795 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2796 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2797 | if (!isdigit(c) && (c < 'A' || c > 'F')) { |
| 2798 | if (c != '.') break; |
| 2799 | // if '.' was already seen, stop on second one: |
| 2800 | if (pt) break; |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 2801 | pt = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2802 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2803 | // buf[i] is one of "0-9A-F." |
| 2804 | i++; |
| 2805 | if (c != '.') |
| 2806 | ccnt = i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2807 | } |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 2808 | //ccnt is the number of chars in the number string, excluding possible |
| 2809 | //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions). |
| 2810 | //i is buf[i] index of the first not-yet-parsed char after that. |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2811 | l->lex_i += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2812 | |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 2813 | // This might overestimate the size, if there are "\<NL>"'s |
| 2814 | // in the number. Subtracting number_of_backslashes*2 correctly |
| 2815 | // is not that easy: consider that in the case of "NNN.\<NL>" |
| 2816 | // loop above will count "\<NL>" before it realizes it is not |
| 2817 | // in fact *inside* the number: |
| 2818 | len = ccnt + 1; // +1 byte for NUL termination |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2819 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2820 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 2821 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 2822 | if (len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2823 | 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] | 2824 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2825 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2826 | bc_vec_pop_all(&l->lex_strnumbuf); |
| 2827 | bc_vec_expand(&l->lex_strnumbuf, 1 + len); |
| 2828 | bc_vec_push(&l->lex_strnumbuf, &start); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2829 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2830 | while (ccnt != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2831 | // If we have hit a backslash, skip it. We don't have |
| 2832 | // to check for a newline because it's guaranteed. |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2833 | if (*buf == '\\') { |
| 2834 | buf += 2; |
| 2835 | ccnt -= 2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2836 | continue; |
| 2837 | } |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2838 | bc_vec_push(&l->lex_strnumbuf, buf); |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2839 | buf++; |
| 2840 | ccnt--; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2841 | } |
| 2842 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2843 | bc_vec_pushZeroByte(&l->lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2844 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2845 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2846 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2847 | #define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2848 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2849 | static void bc_lex_name(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2850 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2851 | BcLex *l = &G.prs.l; |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2852 | size_t i; |
| 2853 | const char *buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2854 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 2855 | l->lex = XC_LEX_NAME; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2856 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2857 | i = 0; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2858 | buf = l->lex_inbuf + l->lex_i - 1; |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2859 | for (;;) { |
| 2860 | char c = buf[i]; |
| 2861 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 2862 | i++; |
| 2863 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2864 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2865 | #if 0 // We do not protect against people with gigabyte-long names |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2866 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2867 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2868 | if (i > BC_MAX_STRING) |
| 2869 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2870 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2871 | #endif |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2872 | bc_vec_string(&l->lex_strnumbuf, i, buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2873 | |
| 2874 | // Increment the index. We minus 1 because it has already been incremented. |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2875 | l->lex_i += i - 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2876 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2877 | //return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2878 | } |
| 2879 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2880 | static void bc_lex_init(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2881 | { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2882 | bc_char_vec_init(&G.prs.l.lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2883 | } |
| 2884 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2885 | static void bc_lex_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2886 | { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2887 | bc_vec_free(&G.prs.l.lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2888 | } |
| 2889 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2890 | static void bc_lex_file(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2891 | { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2892 | G.err_line = G.prs.l.lex_line = 1; |
| 2893 | G.prs.l.lex_newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2894 | } |
| 2895 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2896 | static bool bc_lex_more_input(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2897 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2898 | BcLex *l = &G.prs.l; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2899 | size_t str; |
| 2900 | bool comment; |
| 2901 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2902 | bc_vec_pop_all(&G.input_buffer); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2903 | |
| 2904 | // This loop is complex because the vm tries not to send any lines that end |
| 2905 | // with a backslash to the parser. The reason for that is because the parser |
| 2906 | // treats a backslash+newline combo as whitespace, per the bc spec. In that |
| 2907 | // case, and for strings and comments, the parser will expect more stuff. |
| 2908 | comment = false; |
| 2909 | str = 0; |
| 2910 | for (;;) { |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2911 | size_t prevlen = G.input_buffer.len; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2912 | char *string; |
| 2913 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 2914 | bc_read_line(&G.input_buffer, G.prs.input_fp); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2915 | // No more input means EOF |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2916 | 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] | 2917 | break; |
| 2918 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2919 | string = G.input_buffer.v + prevlen; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2920 | while (*string) { |
| 2921 | char c = *string; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2922 | if (string == G.input_buffer.v || string[-1] != '\\') { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2923 | if (IS_BC) |
| 2924 | str ^= (c == '"'); |
| 2925 | else { |
| 2926 | if (c == ']') |
| 2927 | str -= 1; |
| 2928 | else if (c == '[') |
| 2929 | str += 1; |
| 2930 | } |
| 2931 | } |
| 2932 | string++; |
| 2933 | if (c == '/' && *string == '*') { |
| 2934 | comment = true; |
| 2935 | string++; |
| 2936 | continue; |
| 2937 | } |
| 2938 | if (c == '*' && *string == '/') { |
| 2939 | comment = false; |
| 2940 | string++; |
| 2941 | } |
| 2942 | } |
| 2943 | if (str != 0 || comment) { |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2944 | G.input_buffer.len--; // backstep over the trailing NUL byte |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2945 | continue; |
| 2946 | } |
| 2947 | |
| 2948 | // Check for backslash+newline. |
| 2949 | // we do not check that last char is '\n' - |
| 2950 | // if it is not, then it's EOF, and looping back |
| 2951 | // to bc_read_line() will detect it: |
| 2952 | string -= 2; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2953 | if (string >= G.input_buffer.v && *string == '\\') { |
| 2954 | G.input_buffer.len--; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2955 | continue; |
| 2956 | } |
| 2957 | |
| 2958 | break; |
| 2959 | } |
| 2960 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2961 | l->lex_inbuf = G.input_buffer.v; |
| 2962 | l->lex_i = 0; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2963 | // bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v); |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2964 | l->lex_len = G.input_buffer.len - 1; // do not include NUL |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2965 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2966 | return l->lex_len != 0; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2967 | } |
| 2968 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2969 | IF_BC(static BC_STATUS zbc_lex_token(void);) |
| 2970 | IF_DC(static BC_STATUS zdc_lex_token(void);) |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 2971 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
| 2972 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2973 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2974 | static BC_STATUS zbc_lex_next(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2975 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2976 | BcLex *l = &G.prs.l; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2977 | BcStatus s; |
| 2978 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 2979 | l->lex_last = l->lex; |
| 2980 | if (l->lex_last == XC_LEX_EOF) RETURN_STATUS(bc_error("end of file")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2981 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2982 | l->lex_line += l->lex_newline; |
| 2983 | G.err_line = l->lex_line; |
| 2984 | l->lex_newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2985 | |
| 2986 | // Loop until failure or we don't have whitespace. This |
| 2987 | // is so the parser doesn't get inundated with whitespace. |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 2988 | // Comments are also XC_LEX_WHITESPACE tokens and eaten here. |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2989 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2990 | do { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2991 | if (l->lex_i == l->lex_len) { |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 2992 | l->lex = XC_LEX_EOF; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 2993 | if (!G.prs.input_fp) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2994 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2995 | if (!bc_lex_more_input()) { |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 2996 | G.prs.input_fp = NULL; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2997 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2998 | } |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 2999 | // here it's guaranteed that l->lex_i is below l->lex_len |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 3000 | } |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3001 | l->lex_next_at = l->lex_inbuf + l->lex_i; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3002 | dbg_lex("next string to parse:'%.*s'", |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 3003 | (int)(strchrnul(l->lex_next_at, '\n') - l->lex_next_at), |
| 3004 | l->lex_next_at |
| 3005 | ); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3006 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3007 | IF_BC(s = zbc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3008 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3009 | IF_DC(s = zdc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3010 | } |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3011 | } while (!s && l->lex == XC_LEX_WHITESPACE); |
| 3012 | dbg_lex("l->lex from string:%d", l->lex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3013 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3014 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3015 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3016 | #define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3017 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3018 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3019 | static BC_STATUS zbc_lex_skip_if_at_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3020 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3021 | if (G.prs.l.lex == XC_LEX_NLINE) |
| 3022 | RETURN_STATUS(zbc_lex_next()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3023 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 3024 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3025 | #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] | 3026 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3027 | static BC_STATUS zbc_lex_next_and_skip_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3028 | { |
| 3029 | BcStatus s; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3030 | s = zbc_lex_next(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3031 | if (s) RETURN_STATUS(s); |
| 3032 | // if(cond)<newline>stmt is accepted too (but not 2+ newlines) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3033 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3034 | RETURN_STATUS(s); |
| 3035 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3036 | #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] | 3037 | #endif |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3038 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3039 | static BC_STATUS zbc_lex_text_init(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3040 | { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3041 | G.prs.l.lex_inbuf = text; |
| 3042 | G.prs.l.lex_i = 0; |
| 3043 | G.prs.l.lex_len = strlen(text); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3044 | G.prs.l.lex = G.prs.l.lex_last = XC_LEX_INVALID; |
| 3045 | RETURN_STATUS(zbc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3046 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3047 | #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] | 3048 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3049 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3050 | static BC_STATUS zbc_lex_identifier(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3051 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3052 | BcLex *l = &G.prs.l; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3053 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3054 | unsigned i; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3055 | const char *buf = l->lex_inbuf + l->lex_i - 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3056 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3057 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 3058 | const char *keyword8 = bc_lex_kws[i].name8; |
| 3059 | unsigned j = 0; |
| 3060 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 3061 | j++; |
| 3062 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3063 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3064 | if (keyword8[j] != '\0') |
| 3065 | continue; |
| 3066 | match: |
| 3067 | // buf starts with keyword bc_lex_kws[i] |
Denys Vlasenko | 5acd14b | 2018-12-20 16:48:50 +0100 | [diff] [blame] | 3068 | if (isalnum(buf[j]) || buf[j]=='_') |
| 3069 | continue; // "ifz" does not match "if" keyword, "if." does |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3070 | l->lex = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3071 | if (!keyword_is_POSIX(i)) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3072 | s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8); |
| 3073 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | // We minus 1 because the index has already been incremented. |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3077 | l->lex_i += j - 1; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3078 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3079 | } |
| 3080 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3081 | bc_lex_name(); |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 3082 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3083 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3084 | if (l->lex_strnumbuf.len > 2) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3085 | // Prevent this: |
| 3086 | // >>> qwe=1 |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3087 | // bc: POSIX only allows one character names; this is bad: 'qwe=1 |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3088 | // ' |
| 3089 | unsigned len = strchrnul(buf, '\n') - buf; |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3090 | s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf); |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3091 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3092 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3093 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3094 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3095 | #define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3096 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3097 | static BC_STATUS zbc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3098 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3099 | BcLex *l = &G.prs.l; |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3100 | size_t len, nls, i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3101 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3102 | l->lex = XC_LEX_STR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3103 | |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3104 | nls = 0; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3105 | i = l->lex_i; |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3106 | for (;;) { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3107 | char c = l->lex_inbuf[i]; |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3108 | if (c == '\0') { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3109 | l->lex_i = i; |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3110 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3111 | } |
| 3112 | if (c == '"') |
| 3113 | break; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3114 | nls += (c == '\n'); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3115 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3116 | } |
| 3117 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3118 | len = i - l->lex_i; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3119 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3120 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3121 | if (len > BC_MAX_STRING) |
Denys Vlasenko | 9811ad0 | 2018-12-12 23:25:13 +0100 | [diff] [blame] | 3122 | 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] | 3123 | } |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3124 | bc_vec_string(&l->lex_strnumbuf, len, l->lex_inbuf + l->lex_i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3125 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3126 | l->lex_i = i + 1; |
| 3127 | l->lex_line += nls; |
| 3128 | G.err_line = l->lex_line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3129 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3130 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3131 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3132 | #define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3133 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3134 | static void bc_lex_assign(unsigned with_and_without) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3135 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3136 | BcLex *l = &G.prs.l; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3137 | if (l->lex_inbuf[l->lex_i] == '=') { |
| 3138 | l->lex_i++; |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3139 | with_and_without >>= 8; // store "with" value |
| 3140 | } // else store "without" value |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3141 | l->lex = (with_and_without & 0xff); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3142 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3143 | #define bc_lex_assign(with, without) \ |
| 3144 | bc_lex_assign(((with)<<8)|(without)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3145 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3146 | static BC_STATUS zbc_lex_comment(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3147 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3148 | BcLex *l = &G.prs.l; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3149 | size_t i, nls = 0; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3150 | const char *buf = l->lex_inbuf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3151 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3152 | l->lex = XC_LEX_WHITESPACE; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3153 | i = l->lex_i; /* here buf[l->lex_i] is the '*' of opening comment delimiter */ |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3154 | for (;;) { |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 3155 | char c = buf[++i]; |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3156 | check_star: |
| 3157 | if (c == '*') { |
| 3158 | c = buf[++i]; |
| 3159 | if (c == '/') |
| 3160 | break; |
| 3161 | goto check_star; |
| 3162 | } |
| 3163 | if (c == '\0') { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3164 | l->lex_i = i; |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3165 | RETURN_STATUS(bc_error("unterminated comment")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3166 | } |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3167 | nls += (c == '\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3168 | } |
| 3169 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3170 | l->lex_i = i + 1; |
| 3171 | l->lex_line += nls; |
| 3172 | G.err_line = l->lex_line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3173 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3174 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3175 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3176 | #define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3177 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3178 | #undef zbc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3179 | static BC_STATUS zbc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3180 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3181 | BcLex *l = &G.prs.l; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3182 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3183 | char c = l->lex_inbuf[l->lex_i++], c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3184 | |
| 3185 | // This is the workhorse of the lexer. |
| 3186 | switch (c) { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3187 | // case '\0': // probably never reached |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3188 | // l->lex_i--; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3189 | // l->lex = XC_LEX_EOF; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3190 | // l->lex_newline = true; |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3191 | // break; |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 3192 | case '\n': |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3193 | l->lex = XC_LEX_NLINE; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3194 | l->lex_newline = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3195 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3196 | case '\t': |
| 3197 | case '\v': |
| 3198 | case '\f': |
| 3199 | case '\r': |
| 3200 | case ' ': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3201 | bc_lex_whitespace(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3202 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3203 | case '!': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3204 | bc_lex_assign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3205 | if (l->lex == BC_LEX_OP_BOOL_NOT) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3206 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
| 3207 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3208 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3209 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3210 | case '"': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3211 | s = zbc_lex_string(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3212 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3213 | case '#': |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3214 | s = zbc_POSIX_does_not_allow("'#' script comments"); |
| 3215 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3216 | bc_lex_lineComment(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3217 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3218 | case '%': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3219 | bc_lex_assign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3220 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3221 | case '&': |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3222 | c2 = l->lex_inbuf[l->lex_i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3223 | if (c2 == '&') { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3224 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
| 3225 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3226 | l->lex_i++; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3227 | l->lex = BC_LEX_OP_BOOL_AND; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3228 | } else { |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3229 | l->lex = XC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3230 | s = bc_error_bad_character('&'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3231 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3232 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3233 | case '(': |
| 3234 | case ')': |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3235 | l->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3236 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3237 | case '*': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3238 | bc_lex_assign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3239 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3240 | case '+': |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3241 | c2 = l->lex_inbuf[l->lex_i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3242 | if (c2 == '+') { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3243 | l->lex_i++; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3244 | l->lex = BC_LEX_OP_INC; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3245 | } else |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3246 | bc_lex_assign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3247 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3248 | case ',': |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3249 | l->lex = BC_LEX_COMMA; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3250 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3251 | case '-': |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3252 | c2 = l->lex_inbuf[l->lex_i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3253 | if (c2 == '-') { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3254 | l->lex_i++; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3255 | l->lex = BC_LEX_OP_DEC; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3256 | } else |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3257 | bc_lex_assign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3258 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3259 | case '.': |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3260 | if (isdigit(l->lex_inbuf[l->lex_i])) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3261 | s = zbc_lex_number(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3262 | else { |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3263 | l->lex = BC_LEX_KEY_LAST; |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3264 | s = zbc_POSIX_does_not_allow("'.' as 'last'"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3265 | } |
| 3266 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3267 | case '/': |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3268 | c2 = l->lex_inbuf[l->lex_i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3269 | if (c2 == '*') |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3270 | s = zbc_lex_comment(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3271 | else |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3272 | bc_lex_assign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3273 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3274 | case '0': |
| 3275 | case '1': |
| 3276 | case '2': |
| 3277 | case '3': |
| 3278 | case '4': |
| 3279 | case '5': |
| 3280 | case '6': |
| 3281 | case '7': |
| 3282 | case '8': |
| 3283 | case '9': |
| 3284 | case 'A': |
| 3285 | case 'B': |
| 3286 | case 'C': |
| 3287 | case 'D': |
| 3288 | case 'E': |
| 3289 | case 'F': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3290 | s = zbc_lex_number(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3291 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3292 | case ';': |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3293 | l->lex = BC_LEX_SCOLON; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3294 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3295 | case '<': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3296 | bc_lex_assign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3297 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3298 | case '=': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3299 | bc_lex_assign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3300 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3301 | case '>': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3302 | bc_lex_assign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3303 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3304 | case '[': |
| 3305 | case ']': |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3306 | l->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3307 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3308 | case '\\': |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3309 | if (l->lex_inbuf[l->lex_i] == '\n') { |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3310 | l->lex = XC_LEX_WHITESPACE; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3311 | l->lex_i++; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3312 | } else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3313 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3314 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3315 | case '^': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3316 | bc_lex_assign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3317 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3318 | case 'a': |
| 3319 | case 'b': |
| 3320 | case 'c': |
| 3321 | case 'd': |
| 3322 | case 'e': |
| 3323 | case 'f': |
| 3324 | case 'g': |
| 3325 | case 'h': |
| 3326 | case 'i': |
| 3327 | case 'j': |
| 3328 | case 'k': |
| 3329 | case 'l': |
| 3330 | case 'm': |
| 3331 | case 'n': |
| 3332 | case 'o': |
| 3333 | case 'p': |
| 3334 | case 'q': |
| 3335 | case 'r': |
| 3336 | case 's': |
| 3337 | case 't': |
| 3338 | case 'u': |
| 3339 | case 'v': |
| 3340 | case 'w': |
| 3341 | case 'x': |
| 3342 | case 'y': |
| 3343 | case 'z': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3344 | s = zbc_lex_identifier(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3345 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3346 | case '{': |
| 3347 | case '}': |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3348 | l->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3349 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3350 | case '|': |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3351 | c2 = l->lex_inbuf[l->lex_i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3352 | if (c2 == '|') { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3353 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
| 3354 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3355 | l->lex_i++; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3356 | l->lex = BC_LEX_OP_BOOL_OR; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3357 | } else { |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3358 | l->lex = XC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3359 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3360 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3361 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3362 | default: |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3363 | l->lex = XC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3364 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3365 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3366 | } |
| 3367 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3368 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3369 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3370 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3371 | #endif // ENABLE_BC |
| 3372 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3373 | #if ENABLE_DC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3374 | static BC_STATUS zdc_lex_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3375 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3376 | BcLex *l = &G.prs.l; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3377 | if (G_exreg && isspace(l->lex_inbuf[l->lex_i])) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3378 | bc_lex_whitespace(); // eats whitespace (but not newline) |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3379 | l->lex_i++; // bc_lex_name() expects this |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3380 | bc_lex_name(); |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3381 | } else { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3382 | bc_vec_pop_all(&l->lex_strnumbuf); |
| 3383 | bc_vec_push(&l->lex_strnumbuf, &l->lex_inbuf[l->lex_i++]); |
| 3384 | bc_vec_pushZeroByte(&l->lex_strnumbuf); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3385 | l->lex = XC_LEX_NAME; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3386 | } |
| 3387 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3388 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3389 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3390 | #define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3391 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3392 | static BC_STATUS zdc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3393 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3394 | BcLex *l = &G.prs.l; |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3395 | size_t depth, nls, i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3396 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3397 | l->lex = XC_LEX_STR; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3398 | bc_vec_pop_all(&l->lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3399 | |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3400 | nls = 0; |
| 3401 | depth = 1; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3402 | i = l->lex_i; |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3403 | for (;;) { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3404 | char c = l->lex_inbuf[i]; |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3405 | if (c == '\0') { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3406 | l->lex_i = i; |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3407 | RETURN_STATUS(bc_error("string end could not be found")); |
| 3408 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3409 | nls += (c == '\n'); |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3410 | if (i == l->lex_i || l->lex_inbuf[i - 1] != '\\') { |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3411 | if (c == '[') depth++; |
| 3412 | if (c == ']') |
| 3413 | if (--depth == 0) |
| 3414 | break; |
| 3415 | } |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3416 | bc_vec_push(&l->lex_strnumbuf, &l->lex_inbuf[i]); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3417 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3418 | } |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3419 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3420 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3421 | bc_vec_pushZeroByte(&l->lex_strnumbuf); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3422 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3423 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3424 | if (i - l->lex_i > BC_MAX_STRING) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3425 | 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] | 3426 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3427 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3428 | l->lex_i = i; |
| 3429 | l->lex_line += nls; |
| 3430 | G.err_line = l->lex_line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3431 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3432 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3433 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3434 | #define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3435 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3436 | #undef zdc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3437 | static BC_STATUS zdc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3438 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3439 | BcLex *l = &G.prs.l; |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3440 | static const //BcLexType - should be this type, but narrower type saves size: |
| 3441 | uint8_t |
| 3442 | dc_lex_regs[] = { |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3443 | XC_LEX_OP_REL_EQ, XC_LEX_OP_REL_LE, XC_LEX_OP_REL_GE, XC_LEX_OP_REL_NE, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 3444 | XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON, |
| 3445 | DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN, |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 3446 | DC_LEX_STORE_PUSH, |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3447 | }; |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3448 | |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3449 | BcStatus s; |
| 3450 | char c, c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3451 | size_t i; |
| 3452 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3453 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3454 | if (l->lex_last == dc_lex_regs[i]) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3455 | RETURN_STATUS(zdc_lex_register()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3456 | } |
| 3457 | |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3458 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3459 | c = l->lex_inbuf[l->lex_i++]; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3460 | if (c >= '%' && c <= '~' |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3461 | && (l->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3462 | ) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3463 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | // This is the workhorse of the lexer. |
| 3467 | switch (c) { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3468 | // case '\0': // probably never reached |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3469 | // l->lex = XC_LEX_EOF; |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3470 | // break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3471 | case '\n': |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 3472 | // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 3473 | // (and "case '\n':" is not just empty here) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 3474 | // only to allow interactive dc have a way to exit |
| 3475 | // "parse" stage of "parse,execute" loop |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 3476 | // on <enter>, not on _next_ token (which would mean |
| 3477 | // commands are not executed on pressing <enter>). |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 3478 | // IOW: typing "1p<enter>" should print "1" _at once_, |
| 3479 | // not after some more input. |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3480 | l->lex = XC_LEX_NLINE; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3481 | l->lex_newline = true; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 3482 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3483 | case '\t': |
| 3484 | case '\v': |
| 3485 | case '\f': |
| 3486 | case '\r': |
| 3487 | case ' ': |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3488 | l->lex_newline = 0; // was (c == '\n') |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3489 | bc_lex_whitespace(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3490 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3491 | case '!': |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3492 | c2 = l->lex_inbuf[l->lex_i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3493 | if (c2 == '=') |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3494 | l->lex = XC_LEX_OP_REL_NE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3495 | else if (c2 == '<') |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3496 | l->lex = XC_LEX_OP_REL_LE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3497 | else if (c2 == '>') |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3498 | l->lex = XC_LEX_OP_REL_GE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3499 | else |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3500 | RETURN_STATUS(bc_error_bad_character(c)); |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3501 | l->lex_i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3502 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3503 | case '#': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3504 | bc_lex_lineComment(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3505 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3506 | case '.': |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3507 | if (isdigit(l->lex_inbuf[l->lex_i])) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3508 | s = zbc_lex_number(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3509 | else |
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 | case '0': |
| 3513 | case '1': |
| 3514 | case '2': |
| 3515 | case '3': |
| 3516 | case '4': |
| 3517 | case '5': |
| 3518 | case '6': |
| 3519 | case '7': |
| 3520 | case '8': |
| 3521 | case '9': |
| 3522 | case 'A': |
| 3523 | case 'B': |
| 3524 | case 'C': |
| 3525 | case 'D': |
| 3526 | case 'E': |
| 3527 | case 'F': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3528 | s = zbc_lex_number(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3529 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3530 | case '[': |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3531 | s = zdc_lex_string(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3532 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3533 | default: |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3534 | l->lex = XC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3535 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3536 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3537 | } |
| 3538 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3539 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3540 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3541 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3542 | #endif // ENABLE_DC |
| 3543 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3544 | static void bc_parse_push(char i) |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3545 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3546 | BcParse *p = &G.prs; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3547 | 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] | 3548 | bc_vec_pushByte(&p->func->code, i); |
| 3549 | } |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3550 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3551 | static void bc_parse_pushName(char *name) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3552 | { |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3553 | while (*name) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3554 | bc_parse_push(*name++); |
| 3555 | bc_parse_push(BC_PARSE_STREND); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3556 | } |
| 3557 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3558 | static void bc_parse_pushIndex(size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3559 | { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3560 | size_t mask; |
| 3561 | unsigned amt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3562 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 3563 | dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3564 | mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8); |
| 3565 | amt = sizeof(idx); |
| 3566 | do { |
| 3567 | if (idx & mask) break; |
| 3568 | mask >>= 8; |
| 3569 | amt--; |
| 3570 | } while (amt != 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3571 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3572 | bc_parse_push(amt); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3573 | |
| 3574 | while (idx != 0) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3575 | bc_parse_push((unsigned char)idx); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3576 | idx >>= 8; |
| 3577 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3578 | } |
| 3579 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3580 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3581 | static void bc_parse_pushJUMP(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3582 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3583 | bc_parse_push(BC_INST_JUMP); |
| 3584 | bc_parse_pushIndex(idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3585 | } |
| 3586 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3587 | static void bc_parse_pushJUMP_ZERO(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3588 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3589 | bc_parse_push(BC_INST_JUMP_ZERO); |
| 3590 | bc_parse_pushIndex(idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3591 | } |
| 3592 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3593 | static BC_STATUS zbc_parse_pushSTR(void) |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3594 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3595 | BcParse *p = &G.prs; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3596 | char *str = xstrdup(p->l.lex_strnumbuf.v); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3597 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3598 | bc_parse_push(XC_INST_STR); |
| 3599 | bc_parse_pushIndex(p->func->strs.len); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3600 | bc_vec_push(&p->func->strs, &str); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3601 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3602 | RETURN_STATUS(zbc_lex_next()); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3603 | } |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 3604 | #define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3605 | #endif |
| 3606 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3607 | static void bc_parse_pushNUM(void) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3608 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3609 | BcParse *p = &G.prs; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3610 | char *num = xstrdup(p->l.lex_strnumbuf.v); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3611 | #if ENABLE_BC && ENABLE_DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3612 | 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] | 3613 | #elif ENABLE_BC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3614 | size_t idx = bc_vec_push(&p->func->consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3615 | #else // DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3616 | size_t idx = bc_vec_push(&G.prog.consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3617 | #endif |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3618 | bc_parse_push(XC_INST_NUM); |
| 3619 | bc_parse_pushIndex(idx); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3620 | } |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3621 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3622 | static BC_STATUS zbc_parse_text_init(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3623 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3624 | BcParse *p = &G.prs; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3625 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3626 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3627 | RETURN_STATUS(zbc_lex_text_init(text)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3628 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3629 | #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] | 3630 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3631 | // Called when parsing or execution detects a failure, |
| 3632 | // resets execution structures. |
| 3633 | static void bc_program_reset(void) |
| 3634 | { |
| 3635 | BcFunc *f; |
| 3636 | BcInstPtr *ip; |
| 3637 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3638 | bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3639 | bc_vec_pop_all(&G.prog.results); |
| 3640 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3641 | f = bc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3642 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 3643 | ip->inst_idx = f->code.len; |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3644 | } |
| 3645 | |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3646 | // Called when parsing code detects a failure, |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3647 | // resets parsing structures. |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3648 | static void bc_parse_reset(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3649 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3650 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3651 | if (p->fidx != BC_PROG_MAIN) { |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3652 | bc_func_free(p->func); |
| 3653 | bc_func_init(p->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3654 | |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 3655 | p->fidx = BC_PROG_MAIN; |
| 3656 | p->func = bc_program_func_BC_PROG_MAIN(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3657 | } |
| 3658 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3659 | p->l.lex_i = p->l.lex_len; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3660 | p->l.lex = XC_LEX_EOF; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3661 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3662 | IF_BC(bc_vec_pop_all(&p->exits);) |
| 3663 | IF_BC(bc_vec_pop_all(&p->conds);) |
| 3664 | IF_BC(bc_vec_pop_all(&p->ops);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3665 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3666 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3667 | } |
| 3668 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3669 | static void bc_parse_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3670 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3671 | BcParse *p = &G.prs; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3672 | IF_BC(bc_vec_free(&p->exits);) |
| 3673 | IF_BC(bc_vec_free(&p->conds);) |
| 3674 | IF_BC(bc_vec_free(&p->ops);) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3675 | bc_lex_free(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3676 | } |
| 3677 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3678 | static void bc_parse_create(size_t fidx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3679 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3680 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3681 | memset(p, 0, sizeof(BcParse)); |
| 3682 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3683 | bc_lex_init(); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3684 | IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);) |
| 3685 | IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);) |
| 3686 | IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3687 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 3688 | p->fidx = fidx; |
| 3689 | p->func = bc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3690 | } |
| 3691 | |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3692 | static void bc_program_add_fn(void) |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3693 | { |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3694 | //size_t idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3695 | BcFunc f; |
| 3696 | bc_func_init(&f); |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3697 | //idx = |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3698 | bc_vec_push(&G.prog.fns, &f); |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3699 | //return idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | #if ENABLE_BC |
| 3703 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3704 | // Note: takes ownership of 'name' (must be malloced) |
| 3705 | static size_t bc_program_addFunc(char *name) |
| 3706 | { |
| 3707 | size_t idx; |
| 3708 | BcId entry, *entry_ptr; |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3709 | int inserted; |
| 3710 | |
| 3711 | entry.name = name; |
| 3712 | entry.idx = G.prog.fns.len; |
| 3713 | |
| 3714 | inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx); |
| 3715 | if (!inserted) free(name); |
| 3716 | |
| 3717 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
| 3718 | idx = entry_ptr->idx; |
| 3719 | |
| 3720 | if (!inserted) { |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3721 | // There is already a function with this name. |
| 3722 | // It'll be redefined now, clear old definition. |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3723 | BcFunc *func = bc_program_func(entry_ptr->idx); |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3724 | bc_func_free(func); |
| 3725 | bc_func_init(func); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3726 | } else { |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3727 | bc_program_add_fn(); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | return idx; |
| 3731 | } |
| 3732 | |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 3733 | #define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops)) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3734 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3735 | // position of the first operator in the lex enum and adding the position of the |
| 3736 | // first in the expr enum. Note: This only works for binary operators. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3737 | #define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER)) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3738 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3739 | static BcStatus bc_parse_expr_empty_ok(uint8_t flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3740 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3741 | static BC_STATUS zbc_parse_expr(uint8_t flags) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3742 | { |
| 3743 | BcStatus s; |
| 3744 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3745 | s = bc_parse_expr_empty_ok(flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3746 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
| 3747 | RETURN_STATUS(bc_error("empty expression")); |
| 3748 | RETURN_STATUS(s); |
| 3749 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3750 | #define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3751 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3752 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3753 | #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] | 3754 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3755 | static BC_STATUS zbc_parse_stmt(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3756 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3757 | RETURN_STATUS(zbc_parse_stmt_possibly_auto(false)); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3758 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3759 | #define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3760 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3761 | static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X) |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3762 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3763 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3764 | // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines. |
| 3765 | // Same for "else", "while()", "for()". |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3766 | BcStatus s = zbc_lex_next_and_skip_NLINE(); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3767 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3768 | if (p->l.lex == XC_LEX_NLINE) |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3769 | RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X)); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3770 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3771 | RETURN_STATUS(zbc_parse_stmt()); |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3772 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3773 | #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] | 3774 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3775 | static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3776 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3777 | BcParse *p = &G.prs; |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3778 | char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op); |
| 3779 | bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3780 | |
| 3781 | while (p->ops.len > start) { |
Denys Vlasenko | 7b1df3d | 2018-12-14 23:12:48 +0100 | [diff] [blame] | 3782 | BcLexType t = BC_PARSE_TOP_OP(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3783 | if (t == BC_LEX_LPAREN) break; |
| 3784 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3785 | l = bc_parse_op_PREC(t - XC_LEX_1st_op); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3786 | if (l >= r && (l != r || !left)) break; |
| 3787 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3788 | bc_parse_push(BC_TOKEN_2_INST(t)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3789 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3790 | *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | bc_vec_push(&p->ops, &type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3794 | } |
| 3795 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3796 | static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3797 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3798 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3799 | BcLexType top; |
| 3800 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3801 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3802 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3803 | top = BC_PARSE_TOP_OP(p); |
| 3804 | |
| 3805 | while (top != BC_LEX_LPAREN) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3806 | bc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3807 | |
| 3808 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3809 | *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3810 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3811 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3812 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3813 | top = BC_PARSE_TOP_OP(p); |
| 3814 | } |
| 3815 | |
| 3816 | bc_vec_pop(&p->ops); |
| 3817 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 3818 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3819 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3820 | #define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3821 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3822 | static BC_STATUS zbc_parse_params(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3823 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3824 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3825 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3826 | size_t nparams; |
| 3827 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3828 | dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3829 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3830 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3831 | s = zbc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3832 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3833 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3834 | nparams = 0; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3835 | if (p->l.lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3836 | for (;;) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3837 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3838 | if (s) RETURN_STATUS(s); |
| 3839 | nparams++; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3840 | if (p->l.lex != BC_LEX_COMMA) { |
| 3841 | if (p->l.lex == BC_LEX_RPAREN) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3842 | break; |
| 3843 | RETURN_STATUS(bc_error_bad_token()); |
| 3844 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3845 | s = zbc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3846 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3847 | } |
| 3848 | } |
| 3849 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3850 | bc_parse_push(BC_INST_CALL); |
| 3851 | bc_parse_pushIndex(nparams); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3852 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3853 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3854 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3855 | #define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3856 | |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 3857 | // Note: takes ownership of 'name' (must be malloced) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3858 | static BC_STATUS zbc_parse_call(char *name, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3859 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3860 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3861 | BcStatus s; |
| 3862 | BcId entry, *entry_ptr; |
| 3863 | size_t idx; |
| 3864 | |
| 3865 | entry.name = name; |
| 3866 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3867 | s = zbc_parse_params(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3868 | if (s) goto err; |
| 3869 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3870 | if (p->l.lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3871 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3872 | goto err; |
| 3873 | } |
| 3874 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3875 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3876 | |
| 3877 | if (idx == BC_VEC_INVALID_IDX) { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3878 | // No such function exists, create an empty one |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 3879 | bc_program_addFunc(name); |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3880 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
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 | free(name); |
| 3883 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3884 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3885 | bc_parse_pushIndex(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3886 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3887 | RETURN_STATUS(zbc_lex_next()); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3888 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3889 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3890 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3891 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3892 | #define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3893 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3894 | static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3895 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3896 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3897 | BcStatus s; |
| 3898 | char *name; |
| 3899 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 3900 | name = xstrdup(p->l.lex_strnumbuf.v); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3901 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3902 | if (s) goto err; |
| 3903 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3904 | if (p->l.lex == BC_LEX_LBRACKET) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3905 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3906 | if (s) goto err; |
| 3907 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3908 | if (p->l.lex == BC_LEX_RBRACKET) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3909 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3910 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3911 | goto err; |
| 3912 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3913 | *type = XC_INST_ARRAY; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3914 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3915 | *type = XC_INST_ARRAY_ELEM; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3916 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3917 | s = zbc_parse_expr(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3918 | if (s) goto err; |
| 3919 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3920 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3921 | if (s) goto err; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3922 | bc_parse_push(*type); |
| 3923 | bc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3924 | free(name); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3925 | } else if (p->l.lex == BC_LEX_LPAREN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3926 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3927 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3928 | goto err; |
| 3929 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3930 | *type = BC_INST_CALL; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3931 | s = zbc_parse_call(name, flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3932 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3933 | *type = XC_INST_VAR; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3934 | bc_parse_push(XC_INST_VAR); |
| 3935 | bc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3936 | free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3937 | } |
| 3938 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3939 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3940 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3941 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3942 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3943 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3944 | #define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3945 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3946 | static BC_STATUS zbc_parse_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3947 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3948 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3949 | BcStatus s; |
| 3950 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3951 | s = zbc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3952 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3953 | if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3954 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3955 | s = zbc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3956 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3957 | if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3958 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3959 | bc_parse_push(XC_INST_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3960 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 3961 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3962 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3963 | #define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3964 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3965 | static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3966 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3967 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3968 | BcStatus s; |
| 3969 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3970 | s = zbc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3971 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3972 | if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3973 | |
| 3974 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3975 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3976 | s = zbc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3977 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3978 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3979 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3980 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3981 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3982 | if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3983 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3984 | *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3985 | bc_parse_push(*prev); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3986 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 3987 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3988 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3989 | #define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3990 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3991 | static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3992 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3993 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3994 | BcStatus s; |
| 3995 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3996 | s = zbc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3997 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3998 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 3999 | if (p->l.lex != BC_LEX_LPAREN) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4000 | *type = XC_INST_SCALE; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4001 | bc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4002 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4003 | } |
| 4004 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4005 | *type = XC_INST_SCALE_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4006 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 4007 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4008 | s = zbc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4009 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4010 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4011 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4012 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4013 | if (p->l.lex != BC_LEX_RPAREN) |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4014 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4015 | bc_parse_push(XC_INST_SCALE_FUNC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4016 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4017 | RETURN_STATUS(zbc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4018 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4019 | #define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4020 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4021 | static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr, |
| 4022 | size_t *nexprs, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4023 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4024 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4025 | BcStatus s; |
| 4026 | BcLexType type; |
| 4027 | char inst; |
| 4028 | BcInst etype = *prev; |
| 4029 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4030 | if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM |
| 4031 | || etype == XC_INST_SCALE || etype == BC_INST_LAST |
| 4032 | || etype == XC_INST_IBASE || etype == XC_INST_OBASE |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4033 | ) { |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4034 | *prev = inst = BC_INST_INC_POST + (p->l.lex != BC_LEX_OP_INC); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4035 | bc_parse_push(inst); |
| 4036 | s = zbc_lex_next(); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4037 | } else { |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4038 | *prev = inst = BC_INST_INC_PRE + (p->l.lex != BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4039 | *paren_expr = true; |
| 4040 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4041 | s = zbc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4042 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4043 | type = p->l.lex; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4044 | |
| 4045 | // Because we parse the next part of the expression |
| 4046 | // right here, we need to increment this. |
| 4047 | *nexprs = *nexprs + 1; |
| 4048 | |
| 4049 | switch (type) { |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4050 | case XC_LEX_NAME: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4051 | s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4052 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4053 | case BC_LEX_KEY_IBASE: |
| 4054 | case BC_LEX_KEY_LAST: |
| 4055 | case BC_LEX_KEY_OBASE: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4056 | bc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
| 4057 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4058 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4059 | case BC_LEX_KEY_SCALE: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4060 | s = zbc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4061 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4062 | if (p->l.lex == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4063 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4064 | else |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4065 | bc_parse_push(XC_INST_SCALE); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4066 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4067 | default: |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4068 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4069 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4070 | } |
| 4071 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4072 | if (!s) bc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4073 | } |
| 4074 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4075 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4076 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4077 | #define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4078 | |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4079 | #if 0 |
| 4080 | #define BC_PARSE_LEAF(p, rparen) \ |
| 4081 | ((rparen) \ |
| 4082 | || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \ |
| 4083 | || (p) == BC_INST_INC_POST \ |
| 4084 | || (p) == BC_INST_DEC_POST \ |
| 4085 | ) |
| 4086 | #else |
| 4087 | static int ok_in_expr(BcInst p) |
| 4088 | { |
| 4089 | return (p >= XC_INST_NUM && p <= XC_INST_SQRT) |
| 4090 | || p == BC_INST_INC_POST |
| 4091 | || p == BC_INST_DEC_POST |
| 4092 | ; |
| 4093 | } |
| 4094 | #define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p)) |
| 4095 | #endif |
| 4096 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4097 | static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn, |
| 4098 | bool rparen, size_t *nexprs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4099 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4100 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4101 | BcStatus s; |
| 4102 | BcLexType type; |
| 4103 | BcInst etype = *prev; |
| 4104 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4105 | s = zbc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4106 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4107 | |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4108 | type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG; |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4109 | *prev = BC_TOKEN_2_INST(type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4110 | |
| 4111 | // We can just push onto the op stack because this is the largest |
| 4112 | // precedence operator that gets pushed. Inc/dec does not. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4113 | if (type != XC_LEX_OP_MINUS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4114 | bc_vec_push(&p->ops, &type); |
| 4115 | else |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4116 | bc_parse_operator(type, ops_bgn, nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4117 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4118 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4119 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4120 | #define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4121 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4122 | static BC_STATUS zbc_parse_print(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4123 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4124 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4125 | BcStatus s; |
| 4126 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4127 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4128 | for (;;) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4129 | s = zbc_lex_next(); |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4130 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4131 | type = p->l.lex; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4132 | if (type == XC_LEX_STR) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4133 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4134 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4135 | s = zbc_parse_expr(0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4136 | } |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4137 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4138 | bc_parse_push(XC_INST_PRINT_POP); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4139 | if (p->l.lex != BC_LEX_COMMA) |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4140 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4141 | } |
| 4142 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4143 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4144 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4145 | #define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4146 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4147 | static BC_STATUS zbc_parse_return(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4148 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4149 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4150 | BcStatus s; |
| 4151 | BcLexType t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4152 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4153 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4154 | s = zbc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4155 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4156 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4157 | t = p->l.lex; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4158 | if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4159 | bc_parse_push(BC_INST_RET0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4160 | else { |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4161 | bool paren = (t == BC_LEX_LPAREN); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4162 | s = bc_parse_expr_empty_ok(0); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4163 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4164 | bc_parse_push(BC_INST_RET0); |
| 4165 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4166 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4167 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4168 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4169 | if (!paren || p->l.lex_last != BC_LEX_RPAREN) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4170 | s = zbc_POSIX_requires("parentheses around return expressions"); |
| 4171 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4172 | } |
| 4173 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4174 | bc_parse_push(XC_INST_RET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4175 | } |
| 4176 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4177 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4178 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4179 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4180 | #define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4181 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4182 | static void rewrite_label_to_current(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4183 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4184 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4185 | size_t *label = bc_vec_item(&p->func->labels, idx); |
| 4186 | *label = p->func->code.len; |
| 4187 | } |
| 4188 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4189 | static BC_STATUS zbc_parse_if(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4190 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4191 | BcParse *p = &G.prs; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4192 | BcStatus s; |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4193 | size_t ip_idx; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4194 | |
| 4195 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4196 | s = zbc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4197 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4198 | if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4199 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4200 | s = zbc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4201 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4202 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4203 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4204 | if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4205 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4206 | // Encode "if zero, jump to ..." |
| 4207 | // Pushed value (destination of the jump) is uninitialized, |
| 4208 | // will be rewritten to be address of "end of if()" or of "else". |
| 4209 | ip_idx = bc_vec_push(&p->func->labels, &ip_idx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4210 | bc_parse_pushJUMP_ZERO(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4211 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4212 | s = zbc_parse_stmt_allow_NLINE_before(STRING_if); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4213 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4214 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4215 | dbg_lex("%s:%d in if after stmt: p->l.lex:%d", __func__, __LINE__, p->l.lex); |
| 4216 | if (p->l.lex == BC_LEX_KEY_ELSE) { |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4217 | size_t ip2_idx; |
Denys Vlasenko | 7415633 | 2018-12-16 21:21:27 +0100 | [diff] [blame] | 4218 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4219 | // Encode "after then_stmt, jump to end of if()" |
| 4220 | ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx); |
| 4221 | dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4222 | bc_parse_pushJUMP(ip2_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4223 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4224 | dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4225 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4226 | |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4227 | ip_idx = ip2_idx; |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4228 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4229 | s = zbc_parse_stmt_allow_NLINE_before(STRING_else); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4230 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4231 | } |
| 4232 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4233 | dbg_lex("%s:%d rewriting label to jump after 'if' body-> %zd", __func__, __LINE__, p->func->code.len); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4234 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4235 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4236 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
| 4237 | RETURN_STATUS(s); |
| 4238 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4239 | #define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4240 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4241 | static BC_STATUS zbc_parse_while(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4242 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4243 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4244 | BcStatus s; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4245 | size_t cond_idx; |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4246 | size_t ip_idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4247 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4248 | s = zbc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4249 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4250 | if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4251 | s = zbc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4252 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4253 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4254 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4255 | ip_idx = cond_idx + 1; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4256 | bc_vec_push(&p->conds, &cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4257 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4258 | bc_vec_push(&p->exits, &ip_idx); |
| 4259 | bc_vec_push(&p->func->labels, &ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4260 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4261 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4262 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4263 | if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4264 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4265 | bc_parse_pushJUMP_ZERO(ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4266 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4267 | s = zbc_parse_stmt_allow_NLINE_before(STRING_while); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4268 | if (s) RETURN_STATUS(s); |
| 4269 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4270 | dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4271 | bc_parse_pushJUMP(cond_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4272 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4273 | dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4274 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4275 | |
| 4276 | bc_vec_pop(&p->exits); |
| 4277 | bc_vec_pop(&p->conds); |
| 4278 | |
| 4279 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4280 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4281 | #define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4282 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4283 | static BC_STATUS zbc_parse_for(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4284 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4285 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4286 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4287 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4288 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4289 | dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4290 | s = zbc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4291 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4292 | if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4293 | s = zbc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4294 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4295 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4296 | if (p->l.lex != BC_LEX_SCOLON) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4297 | s = zbc_parse_expr(0); |
| 4298 | bc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4299 | if (s) RETURN_STATUS(s); |
| 4300 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4301 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
| 4302 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4303 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4304 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4305 | if (p->l.lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4306 | s = zbc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4307 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4308 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4309 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4310 | update_idx = cond_idx + 1; |
| 4311 | body_idx = update_idx + 1; |
| 4312 | exit_idx = body_idx + 1; |
| 4313 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4314 | if (p->l.lex != BC_LEX_SCOLON) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4315 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4316 | else { |
Denys Vlasenko | 447dc02 | 2018-12-21 00:39:02 +0100 | [diff] [blame] | 4317 | // Set this for the next call to bc_parse_pushNUM(). |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4318 | // This is safe to set because the current token is a semicolon, |
| 4319 | // which has no string requirement. |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 4320 | bc_vec_string(&p->l.lex_strnumbuf, 1, "1"); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4321 | bc_parse_pushNUM(); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4322 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4323 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4324 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4325 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4326 | if (p->l.lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4327 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4328 | s = zbc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4329 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4330 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4331 | bc_parse_pushJUMP_ZERO(exit_idx); |
| 4332 | bc_parse_pushJUMP(body_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4333 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4334 | bc_vec_push(&p->conds, &update_idx); |
| 4335 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4336 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4337 | if (p->l.lex != BC_LEX_RPAREN) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4338 | s = zbc_parse_expr(0); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4339 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4340 | if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4341 | bc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4342 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4343 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
| 4344 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4345 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4346 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4347 | bc_parse_pushJUMP(cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4348 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4349 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4350 | bc_vec_push(&p->exits, &exit_idx); |
| 4351 | bc_vec_push(&p->func->labels, &exit_idx); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4352 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4353 | s = zbc_parse_stmt_allow_NLINE_before(STRING_for); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4354 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4355 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4356 | dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4357 | bc_parse_pushJUMP(update_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4358 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4359 | dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4360 | rewrite_label_to_current(exit_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4361 | |
| 4362 | bc_vec_pop(&p->exits); |
| 4363 | bc_vec_pop(&p->conds); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4364 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4365 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4366 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4367 | #define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4368 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4369 | static BC_STATUS zbc_parse_break_or_continue(BcLexType type) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4370 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4371 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4372 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4373 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4374 | if (type == BC_LEX_KEY_BREAK) { |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4375 | if (p->exits.len == 0) // none of the enclosing blocks is a loop |
| 4376 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4377 | i = *(size_t*)bc_vec_top(&p->exits); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4378 | } else { |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4379 | i = *(size_t*)bc_vec_top(&p->conds); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4380 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4381 | bc_parse_pushJUMP(i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4382 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4383 | RETURN_STATUS(zbc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4384 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4385 | #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] | 4386 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4387 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var) |
| 4388 | { |
| 4389 | BcId *autoid; |
| 4390 | BcId a; |
| 4391 | size_t i; |
| 4392 | |
| 4393 | autoid = (void*)f->autos.v; |
| 4394 | for (i = 0; i < f->autos.len; i++, autoid++) { |
| 4395 | if (strcmp(name, autoid->name) == 0) |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 4396 | RETURN_STATUS(bc_error("duplicate function parameter or auto name")); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | a.idx = var; |
| 4400 | a.name = name; |
| 4401 | |
| 4402 | bc_vec_push(&f->autos, &a); |
| 4403 | |
| 4404 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 4405 | } |
| 4406 | #define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS) |
| 4407 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4408 | static BC_STATUS zbc_parse_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4409 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4410 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4411 | BcStatus s; |
| 4412 | bool var, comma = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4413 | char *name; |
| 4414 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4415 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4416 | s = zbc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4417 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4418 | if (p->l.lex != XC_LEX_NAME) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4419 | RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4420 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 4421 | name = xstrdup(p->l.lex_strnumbuf.v); |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4422 | p->fidx = bc_program_addFunc(name); |
| 4423 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4424 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4425 | s = zbc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4426 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4427 | if (p->l.lex != BC_LEX_LPAREN) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4428 | RETURN_STATUS(bc_error("bad function definition")); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4429 | s = zbc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4430 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4431 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4432 | while (p->l.lex != BC_LEX_RPAREN) { |
| 4433 | if (p->l.lex != XC_LEX_NAME) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4434 | RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4435 | |
| 4436 | ++p->func->nparams; |
| 4437 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 4438 | name = xstrdup(p->l.lex_strnumbuf.v); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4439 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4440 | if (s) goto err; |
| 4441 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4442 | var = p->l.lex != BC_LEX_LBRACKET; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4443 | |
| 4444 | if (!var) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4445 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4446 | if (s) goto err; |
| 4447 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4448 | if (p->l.lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4449 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4450 | goto err; |
| 4451 | } |
| 4452 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4453 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4454 | if (s) goto err; |
| 4455 | } |
| 4456 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4457 | comma = p->l.lex == BC_LEX_COMMA; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4458 | if (comma) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4459 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4460 | if (s) goto err; |
| 4461 | } |
| 4462 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4463 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4464 | if (s) goto err; |
| 4465 | } |
| 4466 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4467 | if (comma) RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4468 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4469 | s = zbc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4470 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4471 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4472 | if (p->l.lex != BC_LEX_LBRACE) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4473 | s = zbc_POSIX_requires("the left brace be on the same line as the function header"); |
Denys Vlasenko | d279d80 | 2018-12-24 18:28:56 +0100 | [diff] [blame] | 4474 | if (s) RETURN_STATUS(s); |
| 4475 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4476 | |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4477 | // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4478 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4479 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4480 | //GNU bc requires a {} block even if function body has single stmt, enforce this? |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4481 | if (p->l.lex != BC_LEX_LBRACE) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4482 | RETURN_STATUS(bc_error("function { body } expected")); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4483 | |
| 4484 | p->in_funcdef++; // to determine whether "return" stmt is allowed, and such |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4485 | s = zbc_parse_stmt_possibly_auto(true); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4486 | p->in_funcdef--; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4487 | if (s) RETURN_STATUS(s); |
| 4488 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4489 | bc_parse_push(BC_INST_RET0); |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 4490 | |
| 4491 | // Subsequent code generation is into main program |
| 4492 | p->fidx = BC_PROG_MAIN; |
| 4493 | p->func = bc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4494 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4495 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4496 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4497 | err: |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4498 | dbg_lex_done("%s:%d done (error)", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4499 | free(name); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4500 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4501 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4502 | #define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4503 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4504 | static BC_STATUS zbc_parse_auto(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4505 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4506 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4507 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4508 | char *name; |
| 4509 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4510 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4511 | s = zbc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4512 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4513 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4514 | for (;;) { |
| 4515 | bool var; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4516 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4517 | if (p->l.lex != XC_LEX_NAME) |
| 4518 | RETURN_STATUS(bc_error("bad 'auto' syntax")); |
| 4519 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 4520 | name = xstrdup(p->l.lex_strnumbuf.v); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4521 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4522 | if (s) goto err; |
| 4523 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4524 | var = (p->l.lex != BC_LEX_LBRACKET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4525 | if (!var) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4526 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4527 | if (s) goto err; |
| 4528 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4529 | if (p->l.lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4530 | s = bc_error("bad 'auto' syntax"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4531 | goto err; |
| 4532 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4533 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4534 | if (s) goto err; |
| 4535 | } |
| 4536 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4537 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4538 | if (s) goto err; |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4539 | |
| 4540 | if (p->l.lex == XC_LEX_NLINE |
| 4541 | || p->l.lex == BC_LEX_SCOLON |
| 4542 | //|| p->l.lex == BC_LEX_RBRACE // allow "define f() {auto a}" |
| 4543 | ) { |
| 4544 | break; |
| 4545 | } |
| 4546 | if (p->l.lex != BC_LEX_COMMA) |
| 4547 | RETURN_STATUS(bc_error("bad 'auto' syntax")); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4548 | s = zbc_lex_next(); // skip comma |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4549 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4550 | } |
| 4551 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4552 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4553 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4554 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4555 | free(name); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4556 | dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4557 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4558 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4559 | #define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4560 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4561 | #undef zbc_parse_stmt_possibly_auto |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4562 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4563 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4564 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4565 | BcStatus s = BC_STATUS_SUCCESS; |
| 4566 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4567 | dbg_lex_enter("%s:%d entered, p->l.lex:%d", __func__, __LINE__, p->l.lex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4568 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4569 | if (p->l.lex == XC_LEX_NLINE) { |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4570 | dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4571 | RETURN_STATUS(zbc_lex_next()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4572 | } |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4573 | if (p->l.lex == BC_LEX_SCOLON) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4574 | dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4575 | RETURN_STATUS(zbc_lex_next()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4576 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4577 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4578 | if (p->l.lex == BC_LEX_LBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4579 | dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed); |
| 4580 | do { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4581 | s = zbc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4582 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4583 | } while (p->l.lex == XC_LEX_NLINE); |
| 4584 | if (auto_allowed && p->l.lex == BC_LEX_KEY_AUTO) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4585 | dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4586 | s = zbc_parse_auto(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4587 | if (s) RETURN_STATUS(s); |
| 4588 | } |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4589 | while (p->l.lex != BC_LEX_RBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4590 | dbg_lex("%s:%d block parsing loop", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4591 | s = zbc_parse_stmt(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4592 | if (s) RETURN_STATUS(s); |
| 4593 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4594 | s = zbc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4595 | dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__); |
| 4596 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4597 | } |
| 4598 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4599 | dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex); |
| 4600 | switch (p->l.lex) { |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4601 | case XC_LEX_OP_MINUS: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4602 | case BC_LEX_OP_INC: |
| 4603 | case BC_LEX_OP_DEC: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4604 | case BC_LEX_OP_BOOL_NOT: |
| 4605 | case BC_LEX_LPAREN: |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4606 | case XC_LEX_NAME: |
| 4607 | case XC_LEX_NUMBER: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4608 | case BC_LEX_KEY_IBASE: |
| 4609 | case BC_LEX_KEY_LAST: |
| 4610 | case BC_LEX_KEY_LENGTH: |
| 4611 | case BC_LEX_KEY_OBASE: |
| 4612 | case BC_LEX_KEY_READ: |
| 4613 | case BC_LEX_KEY_SCALE: |
| 4614 | case BC_LEX_KEY_SQRT: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4615 | s = zbc_parse_expr(BC_PARSE_PRINT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4616 | break; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4617 | case XC_LEX_STR: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4618 | s = zbc_parse_pushSTR(); |
| 4619 | bc_parse_push(XC_INST_PRINT_STR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4620 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4621 | case BC_LEX_KEY_BREAK: |
| 4622 | case BC_LEX_KEY_CONTINUE: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4623 | s = zbc_parse_break_or_continue(p->l.lex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4624 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4625 | case BC_LEX_KEY_FOR: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4626 | s = zbc_parse_for(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4627 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4628 | case BC_LEX_KEY_HALT: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4629 | bc_parse_push(BC_INST_HALT); |
| 4630 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4631 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4632 | case BC_LEX_KEY_IF: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4633 | s = zbc_parse_if(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4634 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4635 | case BC_LEX_KEY_LIMITS: |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4636 | // "limits" is a compile-time command, |
| 4637 | // the output is produced at _parse time_. |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 4638 | printf( |
| 4639 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4640 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4641 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4642 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4643 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4644 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4645 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4646 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4647 | ); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4648 | s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4649 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4650 | case BC_LEX_KEY_PRINT: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4651 | s = zbc_parse_print(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4652 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4653 | case BC_LEX_KEY_QUIT: |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4654 | // "quit" is a compile-time command. For example, |
| 4655 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4656 | // not when it is executed |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 4657 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4658 | case BC_LEX_KEY_RETURN: |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4659 | if (!p->in_funcdef) |
| 4660 | RETURN_STATUS(bc_error("'return' not in a function")); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4661 | s = zbc_parse_return(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4662 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4663 | case BC_LEX_KEY_WHILE: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4664 | s = zbc_parse_while(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4665 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4666 | default: |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4667 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4668 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4669 | } |
| 4670 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4671 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4672 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4673 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4674 | #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] | 4675 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4676 | static BC_STATUS zbc_parse_stmt_or_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4677 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4678 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4679 | BcStatus s; |
| 4680 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4681 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4682 | if (p->l.lex == XC_LEX_EOF) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4683 | s = bc_error("end of file"); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4684 | else if (p->l.lex == BC_LEX_KEY_DEFINE) { |
| 4685 | dbg_lex("%s:%d p->l.lex:BC_LEX_KEY_DEFINE", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4686 | s = zbc_parse_funcdef(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4687 | } else { |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4688 | dbg_lex("%s:%d p->l.lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.lex); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4689 | s = zbc_parse_stmt(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4690 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4691 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4692 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4693 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4694 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4695 | #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] | 4696 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 4697 | // This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4698 | static BcStatus bc_parse_expr_empty_ok(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4699 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4700 | BcParse *p = &G.prs; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4701 | BcInst prev = XC_INST_PRINT; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4702 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4703 | unsigned nparens, nrelops; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4704 | bool paren_first, paren_expr, rprn, assign, bin_last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4705 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4706 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4707 | paren_first = (p->l.lex == BC_LEX_LPAREN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4708 | nparens = nrelops = 0; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4709 | paren_expr = rprn = assign = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4710 | bin_last = true; |
| 4711 | |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4712 | for (;;) { |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4713 | bool get_token; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4714 | BcStatus s; |
| 4715 | BcLexType t = p->l.lex; |
| 4716 | |
| 4717 | if (!lex_allowed_in_bc_expr(t)) |
| 4718 | break; |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4719 | |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4720 | dbg_lex("%s:%d t:%d", __func__, __LINE__, t); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4721 | get_token = false; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4722 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4723 | switch (t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4724 | case BC_LEX_OP_INC: |
| 4725 | case BC_LEX_OP_DEC: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4726 | s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4727 | rprn = bin_last = false; |
| 4728 | //get_token = false; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4729 | break; |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4730 | case XC_LEX_OP_MINUS: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4731 | s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4732 | rprn = false; |
| 4733 | //get_token = false; - already is |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 4734 | bin_last = (prev == XC_INST_MINUS); |
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_OP_ASSIGN_POWER: |
| 4737 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4738 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4739 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4740 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4741 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4742 | case BC_LEX_OP_ASSIGN: |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4743 | if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM |
| 4744 | && prev != XC_INST_SCALE && prev != XC_INST_IBASE |
| 4745 | && prev != XC_INST_OBASE && prev != BC_INST_LAST |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4746 | ) { |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4747 | return bc_error("bad assignment:" |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4748 | " left side must be variable" |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4749 | " or array element" |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4750 | ); // note: shared string |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4751 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4752 | // Fallthrough. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4753 | case XC_LEX_OP_POWER: |
| 4754 | case XC_LEX_OP_MULTIPLY: |
| 4755 | case XC_LEX_OP_DIVIDE: |
| 4756 | case XC_LEX_OP_MODULUS: |
| 4757 | case XC_LEX_OP_PLUS: |
| 4758 | case XC_LEX_OP_REL_EQ: |
| 4759 | case XC_LEX_OP_REL_LE: |
| 4760 | case XC_LEX_OP_REL_GE: |
| 4761 | case XC_LEX_OP_REL_NE: |
| 4762 | case XC_LEX_OP_REL_LT: |
| 4763 | case XC_LEX_OP_REL_GT: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4764 | case BC_LEX_OP_BOOL_NOT: |
| 4765 | case BC_LEX_OP_BOOL_OR: |
| 4766 | case BC_LEX_OP_BOOL_AND: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4767 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4768 | || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4769 | ) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4770 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4771 | } |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4772 | nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT); |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4773 | prev = BC_TOKEN_2_INST(t); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4774 | bc_parse_operator(t, ops_bgn, &nexprs); |
| 4775 | s = zbc_lex_next(); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4776 | rprn = false; |
| 4777 | //get_token = false; - already is |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4778 | bin_last = (t != BC_LEX_OP_BOOL_NOT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4779 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4780 | case BC_LEX_LPAREN: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4781 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4782 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4783 | bc_vec_push(&p->ops, &t); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4784 | nparens++; |
| 4785 | get_token = true; |
| 4786 | paren_expr = false; |
| 4787 | rprn = bin_last = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4788 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4789 | case BC_LEX_RPAREN: |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4790 | if (bin_last || prev == XC_INST_BOOL_NOT) |
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 | if (nparens == 0) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4793 | goto exit_loop; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4794 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4795 | if (!paren_expr) { |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4796 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4797 | return BC_STATUS_PARSE_EMPTY_EXP; |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4798 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4799 | s = zbc_parse_rightParen(ops_bgn, &nexprs); |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4800 | nparens--; |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4801 | get_token = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4802 | paren_expr = rprn = true; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4803 | bin_last = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4804 | break; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4805 | case XC_LEX_NAME: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4806 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4807 | return bc_error_bad_expression(); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4808 | s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4809 | paren_expr = true; |
| 4810 | rprn = bin_last = false; |
| 4811 | //get_token = false; - already is |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4812 | nexprs++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4813 | break; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4814 | case XC_LEX_NUMBER: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4815 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4816 | return bc_error_bad_expression(); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4817 | bc_parse_pushNUM(); |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4818 | prev = XC_INST_NUM; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4819 | get_token = true; |
| 4820 | paren_expr = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4821 | rprn = bin_last = false; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4822 | nexprs++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4823 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4824 | case BC_LEX_KEY_IBASE: |
| 4825 | case BC_LEX_KEY_LAST: |
| 4826 | case BC_LEX_KEY_OBASE: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4827 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4828 | return bc_error_bad_expression(); |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4829 | prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4830 | bc_parse_push((char) prev); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4831 | get_token = true; |
| 4832 | paren_expr = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4833 | rprn = bin_last = false; |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4834 | nexprs++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4835 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4836 | case BC_LEX_KEY_LENGTH: |
| 4837 | case BC_LEX_KEY_SQRT: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4838 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4839 | return bc_error_bad_expression(); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4840 | s = zbc_parse_builtin(t, flags, &prev); |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 4841 | get_token = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4842 | paren_expr = true; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4843 | rprn = bin_last = false; |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4844 | nexprs++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4845 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4846 | case BC_LEX_KEY_READ: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4847 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4848 | return bc_error_bad_expression(); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4849 | s = zbc_parse_read(); |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4850 | prev = XC_INST_READ; |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 4851 | get_token = true; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4852 | paren_expr = true; |
| 4853 | rprn = bin_last = false; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4854 | nexprs++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4855 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4856 | case BC_LEX_KEY_SCALE: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4857 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4858 | return bc_error_bad_expression(); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4859 | s = zbc_parse_scale(&prev, flags); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4860 | prev = XC_INST_SCALE; |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 4861 | //get_token = false; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4862 | paren_expr = true; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4863 | rprn = bin_last = false; |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4864 | nexprs++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4865 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4866 | default: |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4867 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4868 | } |
| 4869 | |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4870 | if (s || G_interrupt) // error, or ^C: stop parsing |
| 4871 | return BC_STATUS_FAILURE; |
| 4872 | if (get_token) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4873 | s = zbc_lex_next(); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4874 | if (s) return s; |
| 4875 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4876 | } |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4877 | exit_loop: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4878 | |
| 4879 | while (p->ops.len > ops_bgn) { |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4880 | BcLexType top = BC_PARSE_TOP_OP(p); |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 4881 | assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4882 | |
| 4883 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4884 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4885 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4886 | bc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4887 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4888 | nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4889 | bc_vec_pop(&p->ops); |
| 4890 | } |
| 4891 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4892 | if (prev == XC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4893 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4894 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4895 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4896 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4897 | s = zbc_POSIX_does_not_allow("comparison operators outside if or loops"); |
| 4898 | if (s) return s; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4899 | } else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4900 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4901 | s = zbc_POSIX_requires("exactly one comparison operator per condition"); |
| 4902 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4903 | } |
| 4904 | |
| 4905 | if (flags & BC_PARSE_PRINT) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4906 | if (paren_first || !assign) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4907 | bc_parse_push(XC_INST_PRINT); |
| 4908 | bc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4909 | } |
| 4910 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4911 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4912 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4913 | } |
| 4914 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4915 | #endif // ENABLE_BC |
| 4916 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 4917 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 4918 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4919 | static BC_STATUS zdc_parse_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4920 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4921 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4922 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4923 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4924 | s = zbc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4925 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4926 | if (p->l.lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4927 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 4928 | bc_parse_pushName(p->l.lex_strnumbuf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4929 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4930 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4931 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4932 | #define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4933 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4934 | static void dc_parse_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4935 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4936 | BcParse *p = &G.prs; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4937 | char *str; |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4938 | size_t len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4939 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4940 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4941 | |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame^] | 4942 | str = xstrdup(p->l.lex_strnumbuf.v); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4943 | bc_parse_push(XC_INST_STR); |
| 4944 | bc_parse_pushIndex(len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4945 | bc_vec_push(&G.prog.strs, &str); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4946 | |
| 4947 | // Explanation needed here |
| 4948 | bc_program_add_fn(); |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4949 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4950 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4951 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4952 | } |
| 4953 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4954 | static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4955 | { |
| 4956 | BcStatus s; |
| 4957 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4958 | bc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4959 | if (name) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4960 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4961 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | if (store) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4965 | bc_parse_push(DC_INST_SWAP); |
| 4966 | bc_parse_push(XC_INST_ASSIGN); |
| 4967 | bc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4968 | } |
| 4969 | |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4970 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4971 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4972 | #define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4973 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4974 | static BC_STATUS zdc_parse_cond(uint8_t inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4975 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4976 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4977 | BcStatus s; |
| 4978 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4979 | bc_parse_push(inst); |
| 4980 | bc_parse_push(DC_INST_EXEC_COND); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4981 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4982 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4983 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4984 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4985 | s = zbc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4986 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4987 | |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4988 | // Note that 'else' part can not be on the next line: |
| 4989 | // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2" |
| 4990 | // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 4991 | if (p->l.lex == DC_LEX_ELSE) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4992 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4993 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4994 | s = zbc_lex_next(); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4995 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4996 | bc_parse_push(BC_PARSE_STREND); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4997 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4998 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4999 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5000 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5001 | #define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5002 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5003 | static BC_STATUS zdc_parse_token(BcLexType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5004 | { |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5005 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5006 | uint8_t inst; |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5007 | bool assign, get_token; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5008 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5009 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5010 | s = BC_STATUS_SUCCESS; |
| 5011 | get_token = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5012 | switch (t) { |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 5013 | case XC_LEX_OP_REL_EQ: |
| 5014 | case XC_LEX_OP_REL_LE: |
| 5015 | case XC_LEX_OP_REL_GE: |
| 5016 | case XC_LEX_OP_REL_NE: |
| 5017 | case XC_LEX_OP_REL_LT: |
| 5018 | case XC_LEX_OP_REL_GT: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5019 | dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5020 | s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ); |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5021 | get_token = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5022 | break; |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 5023 | case DC_LEX_SCOLON: |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 5024 | case DC_LEX_COLON: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5025 | dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5026 | s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5027 | break; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 5028 | case XC_LEX_STR: |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5029 | dbg_lex("%s:%d LEX_STR", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5030 | dc_parse_string(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5031 | break; |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 5032 | case XC_LEX_NEG: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5033 | dbg_lex("%s:%d LEX_NEG", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5034 | s = zbc_lex_next(); |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5035 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5036 | if (G.prs.l.lex != XC_LEX_NUMBER) |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5037 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5038 | bc_parse_pushNUM(); |
| 5039 | bc_parse_push(XC_INST_NEG); |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5040 | break; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 5041 | case XC_LEX_NUMBER: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5042 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5043 | bc_parse_pushNUM(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5044 | break; |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 5045 | case DC_LEX_READ: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5046 | dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5047 | bc_parse_push(XC_INST_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5048 | break; |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 5049 | case DC_LEX_OP_ASSIGN: |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 5050 | case DC_LEX_STORE_PUSH: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5051 | dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__); |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 5052 | assign = (t == DC_LEX_OP_ASSIGN); |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5053 | inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5054 | s = zdc_parse_mem(inst, true, assign); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5055 | break; |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 5056 | case DC_LEX_LOAD: |
| 5057 | case DC_LEX_LOAD_POP: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5058 | dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__); |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 5059 | inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5060 | s = zdc_parse_mem(inst, true, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5061 | break; |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 5062 | case DC_LEX_STORE_IBASE: |
| 5063 | case DC_LEX_STORE_SCALE: |
| 5064 | case DC_LEX_STORE_OBASE: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5065 | dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__); |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 5066 | inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5067 | s = zdc_parse_mem(inst, false, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5068 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5069 | default: |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5070 | dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__); |
| 5071 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5072 | } |
| 5073 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5074 | if (!s && get_token) s = zbc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5075 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5076 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5077 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5078 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5079 | #define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5080 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5081 | static BC_STATUS zdc_parse_expr(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5082 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5083 | BcParse *p = &G.prs; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5084 | int i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5085 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 5086 | i = (int)p->l.lex - (int)XC_LEX_OP_POWER; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5087 | if (i >= 0) { |
| 5088 | BcInst inst = dc_LEX_to_INST[i]; |
| 5089 | if (inst != DC_INST_INVALID) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5090 | bc_parse_push(inst); |
| 5091 | RETURN_STATUS(zbc_lex_next()); |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5092 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5093 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5094 | RETURN_STATUS(zdc_parse_token(p->l.lex)); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5095 | } |
| 5096 | #define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
| 5097 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5098 | static BC_STATUS zdc_parse_exprs_until_eof(void) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5099 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5100 | BcParse *p = &G.prs; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 5101 | dbg_lex_enter("%s:%d entered, p->l.lex:%d", __func__, __LINE__, p->l.lex); |
| 5102 | while (p->l.lex != XC_LEX_EOF) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5103 | BcStatus s = zdc_parse_expr(); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5104 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5105 | } |
| 5106 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5107 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5108 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5109 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5110 | #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] | 5111 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5112 | #endif // ENABLE_DC |
| 5113 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5114 | // |
| 5115 | // Execution engine |
| 5116 | // |
| 5117 | |
| 5118 | #define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n))) |
| 5119 | #define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n))) |
| 5120 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5121 | static BcVec* bc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5122 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5123 | BcId e, *ptr; |
| 5124 | BcVec *v, *map; |
| 5125 | size_t i; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5126 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5127 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5128 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5129 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5130 | |
| 5131 | e.name = id; |
| 5132 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5133 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5134 | |
| 5135 | if (new) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5136 | BcVec v2; |
| 5137 | bc_array_init(&v2, var); |
| 5138 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5139 | } |
| 5140 | |
| 5141 | ptr = bc_vec_item(map, i); |
| 5142 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5143 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5144 | } |
| 5145 | |
Denys Vlasenko | 8287b1c | 2018-12-21 22:43:53 +0100 | [diff] [blame] | 5146 | // 'num' need not be initialized on entry |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5147 | static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5148 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5149 | switch (r->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5150 | case XC_RESULT_STR: |
| 5151 | case XC_RESULT_TEMP: |
| 5152 | case XC_RESULT_IBASE: |
| 5153 | case XC_RESULT_SCALE: |
| 5154 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5155 | *num = &r->d.n; |
| 5156 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5157 | case XC_RESULT_CONSTANT: { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5158 | BcStatus s; |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 5159 | char *str; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5160 | unsigned base_t; |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 5161 | size_t len; |
| 5162 | |
| 5163 | str = *bc_program_const(r->d.id.idx); |
| 5164 | len = strlen(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5165 | |
| 5166 | bc_num_init(&r->d.n, len); |
| 5167 | |
| 5168 | hex = hex && len == 1; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5169 | base_t = hex ? 16 : G.prog.ib_t; |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 5170 | s = zbc_num_parse(&r->d.n, str, base_t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5171 | if (s) { |
| 5172 | bc_num_free(&r->d.n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5173 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5174 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5175 | *num = &r->d.n; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5176 | r->t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5177 | break; |
| 5178 | } |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5179 | case XC_RESULT_VAR: |
| 5180 | case XC_RESULT_ARRAY: |
| 5181 | case XC_RESULT_ARRAY_ELEM: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5182 | BcVec *v; |
| 5183 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5184 | v = bc_program_search(r->d.id.name, r->t == XC_RESULT_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5185 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5186 | if (r->t == XC_RESULT_ARRAY_ELEM) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5187 | v = bc_vec_top(v); |
| 5188 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5189 | *num = bc_vec_item(v, r->d.id.idx); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5190 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5191 | *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5192 | break; |
| 5193 | } |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5194 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5195 | case BC_RESULT_LAST: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5196 | *num = &G.prog.last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5197 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5198 | case BC_RESULT_ONE: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5199 | *num = &G.prog.one; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5200 | break; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5201 | #endif |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5202 | #if SANITY_CHECKS |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5203 | default: |
| 5204 | // Testing the theory that dc does not reach LAST/ONE |
| 5205 | bb_error_msg_and_die("BUG:%d", r->t); |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5206 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5207 | } |
| 5208 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5209 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5210 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5211 | #define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5212 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5213 | static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5214 | BcResult **r, BcNum **rn, bool assign) |
| 5215 | { |
| 5216 | BcStatus s; |
| 5217 | bool hex; |
| 5218 | BcResultType lt, rt; |
| 5219 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5220 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5221 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5222 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5223 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5224 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5225 | |
| 5226 | lt = (*l)->t; |
| 5227 | rt = (*r)->t; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5228 | hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5229 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5230 | s = zbc_program_num(*l, ln, false); |
| 5231 | if (s) RETURN_STATUS(s); |
| 5232 | s = zbc_program_num(*r, rn, hex); |
| 5233 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5234 | |
| 5235 | // We run this again under these conditions in case any vector has been |
| 5236 | // reallocated out from under the BcNums or arrays we had. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5237 | if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5238 | s = zbc_program_num(*l, ln, false); |
| 5239 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5240 | } |
| 5241 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5242 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5243 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5244 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5245 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5246 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5247 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5248 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5249 | #define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5250 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5251 | static void bc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5252 | { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5253 | r->t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5254 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5255 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5256 | } |
| 5257 | |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5258 | // Note: *r and *n need not be initialized by caller |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5259 | static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5260 | { |
| 5261 | BcStatus s; |
| 5262 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5263 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5264 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5265 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5266 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5267 | s = zbc_program_num(*r, n, false); |
| 5268 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5269 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5270 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5271 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5272 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5273 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5274 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5275 | #define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5276 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5277 | static void bc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5278 | { |
| 5279 | r->t = t; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5280 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5281 | } |
| 5282 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5283 | static BC_STATUS zbc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5284 | { |
| 5285 | BcStatus s; |
| 5286 | BcResult *opd1, *opd2, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5287 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5288 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5289 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5290 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5291 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5292 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5293 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5294 | IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - XC_INST_POWER](n1, n2, &res.d.n, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5295 | if (s) goto err; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5296 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5297 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5298 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5299 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5300 | bc_num_free(&res.d.n); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5301 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5302 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5303 | #define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5304 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5305 | static BC_STATUS zbc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5306 | { |
| 5307 | BcStatus s; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5308 | BcParse sv_parse; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5309 | BcVec buf; |
| 5310 | BcInstPtr ip; |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5311 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5312 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 5313 | bc_char_vec_init(&buf); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 5314 | bc_read_line(&buf, stdin); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5315 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5316 | f = bc_program_func(BC_PROG_READ); |
| 5317 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5318 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5319 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5320 | bc_parse_create(BC_PROG_READ); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5321 | //bc_lex_file(&G.prs.l); - not needed, error line info is not printed for read() |
| 5322 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5323 | s = zbc_parse_text_init(buf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5324 | if (s) goto exec_err; |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5325 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5326 | IF_BC(s = zbc_parse_expr(0)); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5327 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5328 | IF_DC(s = zdc_parse_exprs_until_eof()); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5329 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5330 | if (s) goto exec_err; |
| 5331 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5332 | if (G.prs.l.lex != XC_LEX_NLINE && G.prs.l.lex != XC_LEX_EOF) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5333 | s = bc_error("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5334 | goto exec_err; |
| 5335 | } |
| 5336 | |
| 5337 | ip.func = BC_PROG_READ; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5338 | ip.inst_idx = 0; |
| 5339 | IF_BC(ip.results_len_before_call = G.prog.results.len;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5340 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5341 | bc_parse_push(XC_INST_RET); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5342 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5343 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5344 | exec_err: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5345 | bc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5346 | G.prs = sv_parse; // struct copy |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5347 | bc_vec_free(&buf); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5348 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5349 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5350 | #define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5351 | |
| 5352 | static size_t bc_program_index(char *code, size_t *bgn) |
| 5353 | { |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5354 | unsigned char *bytes = (void*)(code + *bgn); |
| 5355 | unsigned amt; |
| 5356 | unsigned i; |
| 5357 | size_t res; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5358 | |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5359 | amt = *bytes++; |
| 5360 | *bgn += amt + 1; |
| 5361 | |
| 5362 | amt *= 8; |
| 5363 | res = 0; |
| 5364 | for (i = 0; i < amt; i += 8) |
| 5365 | res |= (size_t)(*bytes++) << i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5366 | |
| 5367 | return res; |
| 5368 | } |
| 5369 | |
| 5370 | static char *bc_program_name(char *code, size_t *bgn) |
| 5371 | { |
| 5372 | size_t i; |
Denys Vlasenko | 694d298 | 2018-12-18 19:17:11 +0100 | [diff] [blame] | 5373 | char *s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5374 | |
Denys Vlasenko | 694d298 | 2018-12-18 19:17:11 +0100 | [diff] [blame] | 5375 | code += *bgn; |
| 5376 | s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1); |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 5377 | i = 0; |
| 5378 | for (;;) { |
Denys Vlasenko | 694d298 | 2018-12-18 19:17:11 +0100 | [diff] [blame] | 5379 | char c = *code++; |
| 5380 | if (c == BC_PARSE_STREND) |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 5381 | break; |
| 5382 | s[i++] = c; |
| 5383 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5384 | s[i] = '\0'; |
Denys Vlasenko | 694d298 | 2018-12-18 19:17:11 +0100 | [diff] [blame] | 5385 | *bgn += i + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5386 | |
| 5387 | return s; |
| 5388 | } |
| 5389 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5390 | static void bc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5391 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5392 | #if ENABLE_DC |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5393 | if (!str[0]) { |
Denys Vlasenko | 2c6f563 | 2018-12-11 21:21:14 +0100 | [diff] [blame] | 5394 | // Example: echo '[]ap' | dc |
| 5395 | // should print two bytes: 0x00, 0x0A |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5396 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5397 | return; |
| 5398 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5399 | #endif |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5400 | while (*str) { |
| 5401 | int c = *str++; |
| 5402 | if (c != '\\' || !*str) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5403 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5404 | else { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5405 | c = *str++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5406 | switch (c) { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5407 | case 'a': |
| 5408 | bb_putchar('\a'); |
| 5409 | break; |
| 5410 | case 'b': |
| 5411 | bb_putchar('\b'); |
| 5412 | break; |
| 5413 | case '\\': |
| 5414 | case 'e': |
| 5415 | bb_putchar('\\'); |
| 5416 | break; |
| 5417 | case 'f': |
| 5418 | bb_putchar('\f'); |
| 5419 | break; |
| 5420 | case 'n': |
| 5421 | bb_putchar('\n'); |
| 5422 | G.prog.nchars = SIZE_MAX; |
| 5423 | break; |
| 5424 | case 'r': |
| 5425 | bb_putchar('\r'); |
| 5426 | break; |
| 5427 | case 'q': |
| 5428 | bb_putchar('"'); |
| 5429 | break; |
| 5430 | case 't': |
| 5431 | bb_putchar('\t'); |
| 5432 | break; |
| 5433 | default: |
| 5434 | // Just print the backslash and following character. |
| 5435 | bb_putchar('\\'); |
| 5436 | ++G.prog.nchars; |
| 5437 | bb_putchar(c); |
| 5438 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5439 | } |
| 5440 | } |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5441 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5442 | } |
| 5443 | } |
| 5444 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5445 | static void bc_num_printNewline(void) |
| 5446 | { |
| 5447 | if (G.prog.nchars == G.prog.len - 1) { |
| 5448 | bb_putchar('\\'); |
| 5449 | bb_putchar('\n'); |
| 5450 | G.prog.nchars = 0; |
| 5451 | } |
| 5452 | } |
| 5453 | |
| 5454 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5455 | 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] | 5456 | { |
| 5457 | (void) radix; |
| 5458 | bb_putchar((char) num); |
| 5459 | G.prog.nchars += width; |
| 5460 | } |
| 5461 | #endif |
| 5462 | |
| 5463 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
| 5464 | { |
| 5465 | size_t exp, pow; |
| 5466 | |
| 5467 | bc_num_printNewline(); |
| 5468 | bb_putchar(radix ? '.' : ' '); |
| 5469 | ++G.prog.nchars; |
| 5470 | |
| 5471 | bc_num_printNewline(); |
| 5472 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 5473 | continue; |
| 5474 | |
| 5475 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
| 5476 | size_t dig; |
| 5477 | bc_num_printNewline(); |
| 5478 | dig = num / pow; |
| 5479 | num -= dig * pow; |
| 5480 | bb_putchar(((char) dig) + '0'); |
| 5481 | } |
| 5482 | } |
| 5483 | |
| 5484 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
| 5485 | { |
| 5486 | if (radix) { |
| 5487 | bc_num_printNewline(); |
| 5488 | bb_putchar('.'); |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5489 | G.prog.nchars++; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5490 | } |
| 5491 | |
| 5492 | bc_num_printNewline(); |
| 5493 | bb_putchar(bb_hexdigits_upcase[num]); |
| 5494 | G.prog.nchars += width; |
| 5495 | } |
| 5496 | |
| 5497 | static void bc_num_printDecimal(BcNum *n) |
| 5498 | { |
| 5499 | size_t i, rdx = n->rdx - 1; |
| 5500 | |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5501 | if (n->neg) { |
| 5502 | bb_putchar('-'); |
| 5503 | G.prog.nchars++; |
| 5504 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5505 | |
| 5506 | for (i = n->len - 1; i < n->len; --i) |
| 5507 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
| 5508 | } |
| 5509 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5510 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
| 5511 | |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5512 | 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] | 5513 | { |
| 5514 | BcStatus s; |
| 5515 | BcVec stack; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5516 | BcNum base; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5517 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5518 | BcNum intp, fracp, digit, frac_len; |
| 5519 | unsigned long dig, *ptr; |
| 5520 | size_t i; |
| 5521 | bool radix; |
| 5522 | |
| 5523 | if (n->len == 0) { |
| 5524 | print(0, width, false); |
| 5525 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5526 | } |
| 5527 | |
| 5528 | bc_vec_init(&stack, sizeof(long), NULL); |
| 5529 | bc_num_init(&intp, n->len); |
| 5530 | bc_num_init(&fracp, n->rdx); |
| 5531 | bc_num_init(&digit, width); |
| 5532 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 5533 | bc_num_copy(&intp, n); |
| 5534 | bc_num_one(&frac_len); |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5535 | base.cap = ARRAY_SIZE(base_digs); |
| 5536 | base.num = base_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5537 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5538 | |
| 5539 | bc_num_truncate(&intp, intp.rdx); |
| 5540 | s = zbc_num_sub(n, &intp, &fracp, 0); |
| 5541 | if (s) goto err; |
| 5542 | |
| 5543 | while (intp.len != 0) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5544 | s = zbc_num_divmod(&intp, &base, &intp, &digit, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5545 | if (s) goto err; |
| 5546 | s = zbc_num_ulong(&digit, &dig); |
| 5547 | if (s) goto err; |
| 5548 | bc_vec_push(&stack, &dig); |
| 5549 | } |
| 5550 | |
| 5551 | for (i = 0; i < stack.len; ++i) { |
| 5552 | ptr = bc_vec_item_rev(&stack, i); |
| 5553 | print(*ptr, width, false); |
| 5554 | } |
| 5555 | |
| 5556 | if (!n->rdx) goto err; |
| 5557 | |
| 5558 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5559 | s = zbc_num_mul(&fracp, &base, &fracp, n->rdx); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5560 | if (s) goto err; |
| 5561 | s = zbc_num_ulong(&fracp, &dig); |
| 5562 | if (s) goto err; |
| 5563 | bc_num_ulong2num(&intp, dig); |
| 5564 | s = zbc_num_sub(&fracp, &intp, &fracp, 0); |
| 5565 | if (s) goto err; |
| 5566 | print(dig, width, radix); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5567 | s = zbc_num_mul(&frac_len, &base, &frac_len, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5568 | if (s) goto err; |
| 5569 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5570 | err: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5571 | bc_num_free(&frac_len); |
| 5572 | bc_num_free(&digit); |
| 5573 | bc_num_free(&fracp); |
| 5574 | bc_num_free(&intp); |
| 5575 | bc_vec_free(&stack); |
| 5576 | RETURN_STATUS(s); |
| 5577 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5578 | #define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5579 | |
| 5580 | static BC_STATUS zbc_num_printBase(BcNum *n) |
| 5581 | { |
| 5582 | BcStatus s; |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5583 | size_t width; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5584 | BcNumDigitOp print; |
| 5585 | bool neg = n->neg; |
| 5586 | |
| 5587 | if (neg) { |
| 5588 | bb_putchar('-'); |
| 5589 | G.prog.nchars++; |
| 5590 | } |
| 5591 | |
| 5592 | n->neg = false; |
| 5593 | |
| 5594 | if (G.prog.ob_t <= BC_NUM_MAX_IBASE) { |
| 5595 | width = 1; |
| 5596 | print = bc_num_printHex; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5597 | } else { |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5598 | unsigned i = G.prog.ob_t - 1; |
| 5599 | width = 0; |
| 5600 | for (;;) { |
| 5601 | width++; |
| 5602 | i /= 10; |
| 5603 | if (i == 0) |
| 5604 | break; |
| 5605 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5606 | print = bc_num_printDigits; |
| 5607 | } |
| 5608 | |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5609 | s = zbc_num_printNum(n, G.prog.ob_t, width, print); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5610 | n->neg = neg; |
| 5611 | |
| 5612 | RETURN_STATUS(s); |
| 5613 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5614 | #define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5615 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5616 | static BC_STATUS zbc_num_print(BcNum *n, bool newline) |
| 5617 | { |
| 5618 | BcStatus s = BC_STATUS_SUCCESS; |
| 5619 | |
| 5620 | bc_num_printNewline(); |
| 5621 | |
| 5622 | if (n->len == 0) { |
| 5623 | bb_putchar('0'); |
| 5624 | ++G.prog.nchars; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5625 | } else if (G.prog.ob_t == 10) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5626 | bc_num_printDecimal(n); |
| 5627 | else |
| 5628 | s = zbc_num_printBase(n); |
| 5629 | |
| 5630 | if (newline) { |
| 5631 | bb_putchar('\n'); |
| 5632 | G.prog.nchars = 0; |
| 5633 | } |
| 5634 | |
| 5635 | RETURN_STATUS(s); |
| 5636 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5637 | #define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5638 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5639 | static BC_STATUS zbc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5640 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5641 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5642 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5643 | BcNum *num; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5644 | bool pop = (inst != XC_INST_PRINT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5645 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5646 | if (!STACK_HAS_MORE_THAN(&G.prog.results, idx)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5647 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5648 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5649 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5650 | s = zbc_program_num(r, &num, false); |
| 5651 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5652 | |
| 5653 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5654 | s = zbc_num_print(num, !pop); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5655 | #if ENABLE_BC |
| 5656 | if (!s && IS_BC) bc_num_copy(&G.prog.last, num); |
| 5657 | #endif |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5658 | } else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5659 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5660 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5661 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5662 | str = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5663 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5664 | if (inst == XC_INST_PRINT_STR) { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5665 | for (;;) { |
| 5666 | char c = *str++; |
| 5667 | if (c == '\0') break; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5668 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5669 | ++G.prog.nchars; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5670 | if (c == '\n') G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5671 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5672 | } else { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5673 | bc_program_printString(str); |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5674 | if (inst == XC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5675 | } |
| 5676 | } |
| 5677 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5678 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5679 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5680 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5681 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5682 | #define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5683 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5684 | static BC_STATUS zbc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5685 | { |
| 5686 | BcStatus s; |
| 5687 | BcResult res, *ptr; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5688 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5689 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5690 | s = zbc_program_prep(&ptr, &num); |
| 5691 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5692 | |
| 5693 | bc_num_init(&res.d.n, num->len); |
| 5694 | bc_num_copy(&res.d.n, num); |
| 5695 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5696 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5697 | bc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5698 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5699 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5700 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5701 | #define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5702 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5703 | static BC_STATUS zbc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5704 | { |
| 5705 | BcStatus s; |
| 5706 | BcResult *opd1, *opd2, res; |
| 5707 | BcNum *n1, *n2; |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5708 | ssize_t cond; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5709 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5710 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5711 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5712 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5713 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5714 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5715 | if (inst == XC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5716 | cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero); |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5717 | else if (inst == XC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5718 | 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] | 5719 | else { |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5720 | cond = bc_num_cmp(n1, n2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5721 | switch (inst) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5722 | case XC_INST_REL_EQ: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5723 | cond = (cond == 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5724 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5725 | case XC_INST_REL_LE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5726 | cond = (cond <= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5727 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5728 | case XC_INST_REL_GE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5729 | cond = (cond >= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5730 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5731 | case XC_INST_REL_LT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5732 | cond = (cond < 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5733 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5734 | case XC_INST_REL_GT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5735 | cond = (cond > 0); |
| 5736 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5737 | default: // = case XC_INST_REL_NE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5738 | //cond = (cond != 0); - not needed |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5739 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5740 | } |
| 5741 | } |
| 5742 | |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5743 | if (cond) bc_num_one(&res.d.n); |
| 5744 | //else bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5745 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5746 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5747 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5748 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5749 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5750 | #define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5751 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5752 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5753 | static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5754 | { |
| 5755 | BcNum n2; |
| 5756 | BcResult res; |
| 5757 | |
| 5758 | memset(&n2, 0, sizeof(BcNum)); |
| 5759 | n2.rdx = res.d.id.idx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5760 | res.t = XC_RESULT_STR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5761 | |
| 5762 | if (!push) { |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5763 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5764 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5765 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5766 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5767 | } |
| 5768 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5769 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5770 | bc_vec_push(v, &n2); |
| 5771 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5772 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5773 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5774 | #define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5775 | #endif // ENABLE_DC |
| 5776 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5777 | static BC_STATUS zbc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5778 | { |
| 5779 | BcStatus s; |
| 5780 | BcResult *ptr, r; |
| 5781 | BcVec *v; |
| 5782 | BcNum *n; |
| 5783 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5784 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5785 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5786 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5787 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5788 | if ((ptr->t == XC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5789 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5790 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5791 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5792 | #if ENABLE_DC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5793 | if (ptr->t == XC_RESULT_STR && !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5794 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5795 | if (ptr->t == XC_RESULT_STR) |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5796 | RETURN_STATUS(zdc_program_assignStr(ptr, v, true)); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5797 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5798 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5799 | s = zbc_program_num(ptr, &n, false); |
| 5800 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5801 | |
| 5802 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5803 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5804 | |
| 5805 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5806 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5807 | bc_num_copy(&r.d.n, n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5808 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5809 | bc_array_init(&r.d.v, true); |
| 5810 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 5811 | } |
| 5812 | |
| 5813 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5814 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5815 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5816 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5817 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5818 | #define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5819 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5820 | static BC_STATUS zbc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5821 | { |
| 5822 | BcStatus s; |
| 5823 | BcResult *left, *right, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5824 | BcNum *l, *r; |
| 5825 | bool assign = (inst == XC_INST_ASSIGN); |
| 5826 | bool ib, sc; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5827 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5828 | s = zbc_program_binOpPrep(&left, &l, &right, &r, assign); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5829 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5830 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5831 | ib = left->t == XC_RESULT_IBASE; |
| 5832 | sc = left->t == XC_RESULT_SCALE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5833 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5834 | #if ENABLE_DC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5835 | if (right->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5836 | BcVec *v; |
| 5837 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5838 | if (left->t != XC_RESULT_VAR) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5839 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5840 | v = bc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5841 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5842 | RETURN_STATUS(zdc_program_assignStr(right, v, false)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5843 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5844 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5845 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5846 | if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP) |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5847 | RETURN_STATUS(bc_error("bad assignment:" |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 5848 | " left side must be variable" |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5849 | " or array element" |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 5850 | )); // note: shared string |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5851 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5852 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5853 | 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] | 5854 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5855 | |
| 5856 | if (assign) |
| 5857 | bc_num_copy(l, r); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5858 | else { |
| 5859 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5860 | 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] | 5861 | } |
| 5862 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5863 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5864 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5865 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5866 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5867 | if (ib || sc || left->t == XC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5868 | static const char *const msg[] = { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5869 | "bad ibase; must be [2,16]", //XC_RESULT_IBASE |
| 5870 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE |
| 5871 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5872 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5873 | size_t *ptr; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5874 | size_t max; |
| 5875 | unsigned long val; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5876 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5877 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5878 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5879 | s = left->t - XC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5880 | if (sc) { |
| 5881 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5882 | ptr = &G.prog.scale; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5883 | } else { |
| 5884 | if (val < 2) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5885 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5886 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5887 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5888 | } |
| 5889 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5890 | if (val > max) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5891 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5892 | |
| 5893 | *ptr = (size_t) val; |
| 5894 | s = BC_STATUS_SUCCESS; |
| 5895 | } |
| 5896 | |
| 5897 | bc_num_init(&res.d.n, l->len); |
| 5898 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5899 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5900 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5901 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5902 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5903 | #define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5904 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5905 | #if !ENABLE_DC |
| 5906 | #define bc_program_pushVar(code, bgn, pop, copy) \ |
| 5907 | bc_program_pushVar(code, bgn) |
| 5908 | // for bc, 'pop' and 'copy' are always false |
| 5909 | #endif |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 5910 | static BC_STATUS bc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5911 | bool pop, bool copy) |
| 5912 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5913 | BcResult r; |
| 5914 | char *name = bc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5915 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5916 | r.t = XC_RESULT_VAR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5917 | r.d.id.name = name; |
| 5918 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5919 | #if ENABLE_DC |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5920 | if (pop || copy) { |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5921 | BcVec *v = bc_program_search(name, true); |
| 5922 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5923 | |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5924 | free(name); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5925 | if (!STACK_HAS_MORE_THAN(v, 1 - copy)) { |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5926 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5927 | } |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5928 | |
| 5929 | if (!BC_PROG_STR(num)) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5930 | r.t = XC_RESULT_TEMP; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5931 | bc_num_init_DEF_SIZE(&r.d.n); |
| 5932 | bc_num_copy(&r.d.n, num); |
| 5933 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5934 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5935 | r.d.id.idx = num->rdx; |
| 5936 | } |
| 5937 | |
| 5938 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5939 | } |
| 5940 | #endif // ENABLE_DC |
| 5941 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5942 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5943 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 5944 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5945 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5946 | #define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5947 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5948 | 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] | 5949 | { |
| 5950 | BcStatus s = BC_STATUS_SUCCESS; |
| 5951 | BcResult r; |
| 5952 | BcNum *num; |
| 5953 | |
| 5954 | r.d.id.name = bc_program_name(code, bgn); |
| 5955 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5956 | if (inst == XC_INST_ARRAY) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5957 | r.t = XC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5958 | bc_vec_push(&G.prog.results, &r); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5959 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5960 | BcResult *operand; |
| 5961 | unsigned long temp; |
| 5962 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5963 | s = zbc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5964 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5965 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5966 | if (s) goto err; |
| 5967 | |
| 5968 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 5969 | 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] | 5970 | goto err; |
| 5971 | } |
| 5972 | |
| 5973 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5974 | bc_program_retire(&r, XC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5975 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5976 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5977 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5978 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5979 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5980 | #define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5981 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5982 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5983 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5984 | { |
| 5985 | BcStatus s; |
| 5986 | BcResult *ptr, res, copy; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5987 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5988 | char inst2 = inst; |
| 5989 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5990 | s = zbc_program_prep(&ptr, &num); |
| 5991 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5992 | |
| 5993 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5994 | copy.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5995 | bc_num_init(©.d.n, num->len); |
| 5996 | bc_num_copy(©.d.n, num); |
| 5997 | } |
| 5998 | |
| 5999 | res.t = BC_RESULT_ONE; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6000 | inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST) |
| 6001 | ? BC_INST_ASSIGN_PLUS |
| 6002 | : BC_INST_ASSIGN_MINUS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6003 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6004 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6005 | s = zbc_program_assign(inst); |
| 6006 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6007 | |
| 6008 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 6009 | bc_result_pop_and_push(©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6010 | } |
| 6011 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6012 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6013 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6014 | #define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6015 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6016 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6017 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6018 | BcInstPtr ip; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6019 | size_t i, nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6020 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6021 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6022 | BcResult *arg; |
| 6023 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6024 | nparams = bc_program_index(code, idx); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6025 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6026 | ip.func = bc_program_index(code, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6027 | func = bc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6028 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6029 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6030 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6031 | } |
| 6032 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6033 | 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] | 6034 | } |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6035 | ip.results_len_before_call = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6036 | |
| 6037 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6038 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6039 | |
| 6040 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6041 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6042 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6043 | if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6044 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6045 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6046 | s = zbc_program_copyToVar(a->name, a->idx); |
| 6047 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6048 | } |
| 6049 | |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6050 | a = bc_vec_item(&func->autos, i); |
| 6051 | for (; i < func->autos.len; i++, a++) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6052 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6053 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6054 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6055 | if (a->idx) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 6056 | BcNum n2; |
| 6057 | bc_num_init_DEF_SIZE(&n2); |
| 6058 | bc_vec_push(v, &n2); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6059 | } else { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 6060 | BcVec v2; |
| 6061 | bc_array_init(&v2, true); |
| 6062 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6063 | } |
| 6064 | } |
| 6065 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6066 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6067 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6068 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6069 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6070 | #define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6071 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6072 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6073 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6074 | BcResult res; |
| 6075 | BcFunc *f; |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6076 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6077 | size_t i; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6078 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6079 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6080 | if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->results_len_before_call + (inst == XC_INST_RET))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6081 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6082 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6083 | f = bc_program_func(ip->func); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6084 | res.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6085 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6086 | if (inst == XC_INST_RET) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6087 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6088 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6089 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6090 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6091 | s = zbc_program_num(operand, &num, false); |
| 6092 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6093 | bc_num_init(&res.d.n, num->len); |
| 6094 | bc_num_copy(&res.d.n, num); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6095 | } else { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6096 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6097 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6098 | } |
| 6099 | |
| 6100 | // 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] | 6101 | a = (void*)f->autos.v; |
| 6102 | for (i = 0; i < f->autos.len; i++, a++) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6103 | BcVec *v; |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6104 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6105 | bc_vec_pop(v); |
| 6106 | } |
| 6107 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6108 | 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] | 6109 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6110 | bc_vec_pop(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6111 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6112 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6113 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6114 | #define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6115 | #endif // ENABLE_BC |
| 6116 | |
| 6117 | static unsigned long bc_program_scale(BcNum *n) |
| 6118 | { |
| 6119 | return (unsigned long) n->rdx; |
| 6120 | } |
| 6121 | |
| 6122 | static unsigned long bc_program_len(BcNum *n) |
| 6123 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6124 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6125 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6126 | if (n->rdx != len) return len; |
| 6127 | for (;;) { |
| 6128 | if (len == 0) break; |
| 6129 | len--; |
| 6130 | if (n->num[len] != 0) break; |
| 6131 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6132 | return len; |
| 6133 | } |
| 6134 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6135 | static BC_STATUS zbc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6136 | { |
| 6137 | BcStatus s; |
| 6138 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6139 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6140 | BcResult res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6141 | bool len = (inst == XC_INST_LENGTH); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6142 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6143 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6144 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6145 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6146 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6147 | s = zbc_program_num(opnd, &num, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6148 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6149 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6150 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6151 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6152 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6153 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6154 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6155 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6156 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6157 | if (inst == XC_INST_SQRT) |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6158 | s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6159 | #if ENABLE_BC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6160 | else if (len != 0 && opnd->t == XC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6161 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6162 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6163 | #endif |
| 6164 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6165 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6166 | char **str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6167 | size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6168 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6169 | str = bc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6170 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6171 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6172 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6173 | else { |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 6174 | 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] | 6175 | } |
| 6176 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6177 | bc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6178 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6179 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6180 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6181 | #define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6182 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6183 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6184 | static BC_STATUS zdc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6185 | { |
| 6186 | BcStatus s; |
| 6187 | BcResult *opd1, *opd2, res, res2; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6188 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6189 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6190 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6191 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6192 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6193 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6194 | bc_num_init(&res2.d.n, n2->len); |
| 6195 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6196 | 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] | 6197 | if (s) goto err; |
| 6198 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6199 | bc_program_binOpRetire(&res2); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6200 | res.t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6201 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6202 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6203 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6204 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6205 | bc_num_free(&res2.d.n); |
| 6206 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6207 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6208 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6209 | #define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6210 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6211 | static BC_STATUS zdc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6212 | { |
| 6213 | BcStatus s; |
| 6214 | BcResult *r1, *r2, *r3, res; |
| 6215 | BcNum *n1, *n2, *n3; |
| 6216 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6217 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 2)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6218 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6219 | s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6220 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6221 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6222 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6223 | s = zbc_program_num(r1, &n1, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6224 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6225 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6226 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6227 | |
| 6228 | // Make sure that the values have their pointers updated, if necessary. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6229 | if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6230 | if (r1->t == r2->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6231 | s = zbc_program_num(r2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6232 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6233 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6234 | if (r1->t == r3->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6235 | s = zbc_program_num(r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6236 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6237 | } |
| 6238 | } |
| 6239 | |
| 6240 | bc_num_init(&res.d.n, n3->len); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6241 | s = zdc_num_modexp(n1, n2, n3, &res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6242 | if (s) goto err; |
| 6243 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6244 | bc_vec_pop(&G.prog.results); |
| 6245 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6246 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6247 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6248 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6249 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6250 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6251 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6252 | #define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6253 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6254 | static void dc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6255 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6256 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6257 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6258 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6259 | res.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6260 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6261 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6262 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6263 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6264 | } |
| 6265 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6266 | static BC_STATUS zdc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6267 | { |
| 6268 | BcStatus s; |
| 6269 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6270 | BcNum *num, n; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6271 | char **strs; |
| 6272 | char *str; |
| 6273 | char c; |
| 6274 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6275 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6276 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6277 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6278 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6279 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6280 | s = zbc_program_num(r, &num, false); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6281 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6282 | |
| 6283 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 6284 | unsigned long val; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6285 | BcNum strmb; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6286 | BcDig strmb_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6287 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6288 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6289 | bc_num_copy(&n, num); |
| 6290 | bc_num_truncate(&n, n.rdx); |
| 6291 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6292 | strmb.cap = ARRAY_SIZE(strmb_digs); |
| 6293 | strmb.num = strmb_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6294 | bc_num_ulong2num(&strmb, 0x100); |
| 6295 | s = zbc_num_mod(&n, &strmb, &n, 0); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6296 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6297 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6298 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6299 | if (s) goto num_err; |
| 6300 | |
| 6301 | c = (char) val; |
| 6302 | |
| 6303 | bc_num_free(&n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6304 | } else { |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6305 | char *sp; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6306 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6307 | sp = *bc_program_str(idx); |
| 6308 | c = sp[0]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6309 | } |
| 6310 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6311 | strs = (void*)G.prog.strs.v; |
| 6312 | for (idx = 0; idx < G.prog.strs.len; idx++) { |
| 6313 | if (strs[idx][0] == c && strs[idx][1] == '\0') { |
| 6314 | goto dup; |
| 6315 | } |
| 6316 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6317 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6318 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6319 | //str[1] = '\0'; - already is |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6320 | bc_vec_push(&G.prog.strs, &str); |
| 6321 | dup: |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6322 | res.t = XC_RESULT_STR; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6323 | res.d.id.idx = idx; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 6324 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6325 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6326 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6327 | num_err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6328 | bc_num_free(&n); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6329 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6330 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6331 | #define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6332 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6333 | static BC_STATUS zdc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6334 | { |
| 6335 | BcStatus s; |
| 6336 | BcResult *r; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6337 | BcNum *n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6338 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6339 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6340 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6341 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6342 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6343 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6344 | s = zbc_program_num(r, &n, false); |
| 6345 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6346 | |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6347 | if (BC_PROG_NUM(r, n)) { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6348 | s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar); |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6349 | } else { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6350 | char *str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6351 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6352 | str = *bc_program_str(idx); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6353 | fputs(str, stdout); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6354 | } |
| 6355 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6356 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6357 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6358 | #define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6359 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6360 | static BC_STATUS zdc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6361 | { |
| 6362 | BcStatus s; |
| 6363 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6364 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6365 | unsigned long val; |
| 6366 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6367 | s = zbc_program_prep(&opnd, &num); |
| 6368 | if (s) RETURN_STATUS(s); |
| 6369 | s = zbc_num_ulong(num, &val); |
| 6370 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6371 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6372 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6373 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6374 | if (G.prog.exestack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6375 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6376 | if (G.prog.exestack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6377 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6378 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6379 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6380 | bc_vec_npop(&G.prog.exestack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6381 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6382 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6383 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6384 | #define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6385 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6386 | 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] | 6387 | { |
| 6388 | BcStatus s = BC_STATUS_SUCCESS; |
| 6389 | BcResult *r; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6390 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6391 | BcInstPtr ip; |
| 6392 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6393 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6394 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6395 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6396 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6397 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6398 | |
| 6399 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6400 | BcNum *n = n; // for compiler |
| 6401 | bool exec; |
| 6402 | char *name; |
| 6403 | char *then_name = bc_program_name(code, bgn); |
| 6404 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6405 | |
| 6406 | if (code[*bgn] == BC_PARSE_STREND) |
| 6407 | (*bgn) += 1; |
| 6408 | else |
| 6409 | else_name = bc_program_name(code, bgn); |
| 6410 | |
| 6411 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6412 | name = then_name; |
| 6413 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6414 | exec = true; |
| 6415 | name = else_name; |
| 6416 | } |
| 6417 | |
| 6418 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6419 | BcVec *v; |
| 6420 | v = bc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6421 | n = bc_vec_top(v); |
| 6422 | } |
| 6423 | |
| 6424 | free(then_name); |
| 6425 | free(else_name); |
| 6426 | |
| 6427 | if (!exec) goto exit; |
| 6428 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6429 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6430 | goto exit; |
| 6431 | } |
| 6432 | |
| 6433 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6434 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6435 | if (r->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6436 | sidx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6437 | } else if (r->t == XC_RESULT_VAR) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6438 | BcNum *n; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6439 | s = zbc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6440 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6441 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6442 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6443 | goto exit; |
| 6444 | } |
| 6445 | |
| 6446 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6447 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6448 | f = bc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6449 | |
| 6450 | if (f->code.len == 0) { |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6451 | BcParse sv_parse; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6452 | char *str; |
| 6453 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6454 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6455 | bc_parse_create(fidx); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6456 | str = *bc_program_str(sidx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6457 | s = zbc_parse_text_init(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6458 | if (s) goto err; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6459 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6460 | s = zdc_parse_exprs_until_eof(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6461 | if (s) goto err; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6462 | bc_parse_push(DC_INST_POP_EXEC); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6463 | if (G.prs.l.lex != XC_LEX_EOF) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6464 | s = bc_error_bad_expression(); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6465 | bc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6466 | G.prs = sv_parse; // struct copy |
| 6467 | if (s) { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6468 | err: |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6469 | bc_vec_pop_all(&f->code); |
| 6470 | goto exit; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6471 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6472 | } |
| 6473 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6474 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6475 | ip.func = fidx; |
| 6476 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6477 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6478 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6479 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6480 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6481 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6482 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6483 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6484 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6485 | #define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6486 | #endif // ENABLE_DC |
| 6487 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6488 | static void bc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6489 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6490 | BcResult res; |
| 6491 | unsigned long val; |
| 6492 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6493 | res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6494 | if (inst == XC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6495 | val = (unsigned long) G.prog.ib_t; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6496 | else if (inst == XC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6497 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6498 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6499 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6500 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6501 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6502 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6503 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6504 | } |
| 6505 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6506 | static BC_STATUS zbc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6507 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6508 | BcResult r, *ptr; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6509 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6510 | BcFunc *func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6511 | char *code = func->code.v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6512 | |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6513 | dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d", |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6514 | ip->func, func->code.len, ip->inst_idx, G.prog.results.len); |
| 6515 | while (ip->inst_idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6516 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6517 | char inst = code[ip->inst_idx++]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6518 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6519 | 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] | 6520 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6521 | #if ENABLE_BC |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6522 | case BC_INST_JUMP_ZERO: { |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6523 | BcNum *num; |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6524 | bool zero; |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6525 | dbg_exec("BC_INST_JUMP_ZERO:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6526 | s = zbc_program_prep(&ptr, &num); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6527 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6528 | zero = (bc_num_cmp(num, &G.prog.zero) == 0); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6529 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6530 | if (!zero) { |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6531 | bc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6532 | break; |
| 6533 | } |
| 6534 | // else: fall through |
| 6535 | } |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6536 | case BC_INST_JUMP: { |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6537 | size_t idx = bc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6538 | size_t *addr = bc_vec_item(&func->labels, idx); |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6539 | dbg_exec("BC_INST_JUMP: to %ld", (long)*addr); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6540 | ip->inst_idx = *addr; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6541 | break; |
| 6542 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6543 | case BC_INST_CALL: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6544 | dbg_exec("BC_INST_CALL:"); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6545 | s = zbc_program_call(code, &ip->inst_idx); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6546 | goto read_updated_ip; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6547 | case BC_INST_INC_PRE: |
| 6548 | case BC_INST_DEC_PRE: |
| 6549 | case BC_INST_INC_POST: |
| 6550 | case BC_INST_DEC_POST: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6551 | dbg_exec("BC_INST_INCDEC:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6552 | s = zbc_program_incdec(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6553 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6554 | case BC_INST_HALT: |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6555 | dbg_exec("BC_INST_HALT:"); |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6556 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6557 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6558 | case XC_INST_RET: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6559 | case BC_INST_RET0: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6560 | dbg_exec("BC_INST_RET[0]:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6561 | s = zbc_program_return(inst); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6562 | goto read_updated_ip; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6563 | case XC_INST_BOOL_OR: |
| 6564 | case XC_INST_BOOL_AND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6565 | #endif // ENABLE_BC |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6566 | case XC_INST_REL_EQ: |
| 6567 | case XC_INST_REL_LE: |
| 6568 | case XC_INST_REL_GE: |
| 6569 | case XC_INST_REL_NE: |
| 6570 | case XC_INST_REL_LT: |
| 6571 | case XC_INST_REL_GT: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6572 | dbg_exec("BC_INST_BOOL:"); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 6573 | s = zbc_program_logical(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6574 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6575 | case XC_INST_READ: |
| 6576 | dbg_exec("XC_INST_READ:"); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 6577 | s = zbc_program_read(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6578 | goto read_updated_ip; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6579 | case XC_INST_VAR: |
| 6580 | dbg_exec("XC_INST_VAR:"); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6581 | s = zbc_program_pushVar(code, &ip->inst_idx, false, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6582 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6583 | case XC_INST_ARRAY_ELEM: |
| 6584 | case XC_INST_ARRAY: |
| 6585 | dbg_exec("XC_INST_ARRAY[_ELEM]:"); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6586 | s = zbc_program_pushArray(code, &ip->inst_idx, inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6587 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6588 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6589 | case BC_INST_LAST: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6590 | dbg_exec("BC_INST_LAST:"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6591 | r.t = BC_RESULT_LAST; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6592 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6593 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6594 | #endif |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6595 | case XC_INST_IBASE: |
| 6596 | case XC_INST_OBASE: |
| 6597 | case XC_INST_SCALE: |
| 6598 | dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6599 | bc_program_pushGlobal(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6600 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6601 | case XC_INST_SCALE_FUNC: |
| 6602 | case XC_INST_LENGTH: |
| 6603 | case XC_INST_SQRT: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6604 | dbg_exec("BC_INST_builtin:"); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6605 | s = zbc_program_builtin(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6606 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6607 | case XC_INST_NUM: |
| 6608 | dbg_exec("XC_INST_NUM:"); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6609 | r.t = XC_RESULT_CONSTANT; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6610 | r.d.id.idx = bc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6611 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6612 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6613 | case XC_INST_POP: |
| 6614 | dbg_exec("XC_INST_POP:"); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6615 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6616 | s = bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6617 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6618 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6619 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6620 | case XC_INST_PRINT: |
| 6621 | case XC_INST_PRINT_POP: |
| 6622 | case XC_INST_PRINT_STR: |
| 6623 | dbg_exec("XC_INST_PRINTxyz:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6624 | s = zbc_program_print(inst, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6625 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6626 | case XC_INST_STR: |
| 6627 | dbg_exec("XC_INST_STR:"); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6628 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6629 | r.d.id.idx = bc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6630 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6631 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6632 | case XC_INST_POWER: |
| 6633 | case XC_INST_MULTIPLY: |
| 6634 | case XC_INST_DIVIDE: |
| 6635 | case XC_INST_MODULUS: |
| 6636 | case XC_INST_PLUS: |
| 6637 | case XC_INST_MINUS: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6638 | dbg_exec("BC_INST_binaryop:"); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 6639 | s = zbc_program_op(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6640 | break; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6641 | case XC_INST_BOOL_NOT: { |
| 6642 | BcNum *num; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6643 | dbg_exec("XC_INST_BOOL_NOT:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6644 | s = zbc_program_prep(&ptr, &num); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6645 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6646 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6647 | if (bc_num_cmp(num, &G.prog.zero) == 0) |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6648 | bc_num_one(&r.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6649 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6650 | bc_program_retire(&r, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6651 | break; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6652 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6653 | case XC_INST_NEG: |
| 6654 | dbg_exec("XC_INST_NEG:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6655 | s = zbc_program_negate(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6656 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6657 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6658 | case BC_INST_ASSIGN_POWER: |
| 6659 | case BC_INST_ASSIGN_MULTIPLY: |
| 6660 | case BC_INST_ASSIGN_DIVIDE: |
| 6661 | case BC_INST_ASSIGN_MODULUS: |
| 6662 | case BC_INST_ASSIGN_PLUS: |
| 6663 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6664 | #endif |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6665 | case XC_INST_ASSIGN: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6666 | dbg_exec("BC_INST_ASSIGNxyz:"); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6667 | s = zbc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6668 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6669 | #if ENABLE_DC |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6670 | case DC_INST_POP_EXEC: |
| 6671 | dbg_exec("DC_INST_POP_EXEC:"); |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 6672 | bc_vec_pop(&G.prog.exestack); |
| 6673 | goto read_updated_ip; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6674 | case DC_INST_MODEXP: |
| 6675 | dbg_exec("DC_INST_MODEXP:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6676 | s = zdc_program_modexp(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6677 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6678 | case DC_INST_DIVMOD: |
| 6679 | dbg_exec("DC_INST_DIVMOD:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6680 | s = zdc_program_divmod(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6681 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6682 | case DC_INST_EXECUTE: |
| 6683 | case DC_INST_EXEC_COND: |
| 6684 | dbg_exec("DC_INST_EXEC[_COND]:"); |
| 6685 | s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6686 | goto read_updated_ip; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6687 | case DC_INST_PRINT_STACK: { |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6688 | size_t idx; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6689 | dbg_exec("DC_INST_PRINT_STACK:"); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6690 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6691 | s = zbc_program_print(XC_INST_PRINT, idx); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6692 | if (s) break; |
| 6693 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6694 | break; |
| 6695 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6696 | case DC_INST_CLEAR_STACK: |
| 6697 | dbg_exec("DC_INST_CLEAR_STACK:"); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6698 | bc_vec_pop_all(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6699 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6700 | case DC_INST_STACK_LEN: |
| 6701 | dbg_exec("DC_INST_STACK_LEN:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6702 | dc_program_stackLen(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6703 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6704 | case DC_INST_DUPLICATE: |
| 6705 | dbg_exec("DC_INST_DUPLICATE:"); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6706 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6707 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6708 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6709 | dc_result_copy(&r, ptr); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6710 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6711 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6712 | case DC_INST_SWAP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6713 | BcResult *ptr2; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6714 | dbg_exec("DC_INST_SWAP:"); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6715 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6716 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6717 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6718 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6719 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6720 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6721 | memcpy(ptr2, &r, sizeof(BcResult)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6722 | break; |
| 6723 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6724 | case DC_INST_ASCIIFY: |
| 6725 | dbg_exec("DC_INST_ASCIIFY:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6726 | s = zdc_program_asciify(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6727 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6728 | case DC_INST_PRINT_STREAM: |
| 6729 | dbg_exec("DC_INST_PRINT_STREAM:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6730 | s = zdc_program_printStream(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6731 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6732 | case DC_INST_LOAD: |
| 6733 | case DC_INST_PUSH_VAR: { |
| 6734 | bool copy = inst == DC_INST_LOAD; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6735 | s = zbc_program_pushVar(code, &ip->inst_idx, true, copy); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6736 | break; |
| 6737 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6738 | case DC_INST_PUSH_TO_VAR: { |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6739 | char *name = bc_program_name(code, &ip->inst_idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6740 | s = zbc_program_copyToVar(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6741 | free(name); |
| 6742 | break; |
| 6743 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6744 | case DC_INST_QUIT: |
| 6745 | dbg_exec("DC_INST_QUIT:"); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6746 | if (G.prog.exestack.len <= 2) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6747 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6748 | bc_vec_npop(&G.prog.exestack, 2); |
Denys Vlasenko | 085b420 | 2018-12-19 14:02:59 +0100 | [diff] [blame] | 6749 | goto read_updated_ip; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6750 | case DC_INST_NQUIT: |
| 6751 | dbg_exec("DC_INST_NQUIT:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6752 | s = zdc_program_nquit(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6753 | //goto read_updated_ip; - just fall through to it |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6754 | #endif // ENABLE_DC |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6755 | read_updated_ip: |
| 6756 | // Instruction stack has changed, read new pointers |
| 6757 | ip = bc_vec_top(&G.prog.exestack); |
| 6758 | func = bc_program_func(ip->func); |
| 6759 | code = func->code.v; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6760 | 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] | 6761 | } |
| 6762 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6763 | if (s || G_interrupt) { |
| 6764 | bc_program_reset(); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6765 | RETURN_STATUS(s); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6766 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6767 | |
Denys Vlasenko | c5774a3 | 2018-12-17 01:22:53 +0100 | [diff] [blame] | 6768 | fflush_and_check(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6769 | } |
| 6770 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6771 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6772 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6773 | #define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6774 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6775 | static unsigned bc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6776 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6777 | char *lenv; |
| 6778 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6779 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6780 | lenv = getenv(var); |
| 6781 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6782 | if (!lenv) return len; |
| 6783 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6784 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 6785 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6786 | len = BC_NUM_PRINT_WIDTH; |
| 6787 | |
| 6788 | return len; |
| 6789 | } |
| 6790 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6791 | static BC_STATUS zbc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6792 | { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6793 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6794 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6795 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6796 | s = zbc_parse_text_init(text); // does the first zbc_lex_next() |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6797 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6798 | |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6799 | IF_BC(check_eof:) |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 6800 | while (G.prs.l.lex != XC_LEX_EOF) { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6801 | BcInstPtr *ip; |
| 6802 | BcFunc *f; |
| 6803 | |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 6804 | dbg_lex("%s:%d G.prs.l.lex:%d, parsing...", __func__, __LINE__, G.prs.l.lex); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6805 | if (IS_BC) { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6806 | #if ENABLE_BC |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6807 | if (G.prs.l.lex == BC_LEX_SCOLON |
| 6808 | || G.prs.l.lex == XC_LEX_NLINE |
| 6809 | ) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6810 | s = zbc_lex_next(); |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6811 | if (s) goto err; |
| 6812 | goto check_eof; |
| 6813 | } |
| 6814 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6815 | s = zbc_parse_stmt_or_funcdef(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6816 | if (s) goto err; |
| 6817 | |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6818 | // Check that next token is a correct stmt delimiter - |
| 6819 | // disallows "print 1 print 2" and such. |
| 6820 | if (G.prs.l.lex != BC_LEX_SCOLON |
| 6821 | && G.prs.l.lex != XC_LEX_NLINE |
| 6822 | && G.prs.l.lex != XC_LEX_EOF |
| 6823 | ) { |
| 6824 | const char *err_at; |
| 6825 | //TODO: commonalize for other parse errors: |
| 6826 | err_at = G.prs.l.lex_next_at ? G.prs.l.lex_next_at : "UNKNOWN"; |
| 6827 | bc_error_fmt("bad statement terminator at '%.*s'", |
| 6828 | (int)(strchrnul(err_at, '\n') - err_at), |
| 6829 | err_at |
| 6830 | ); |
| 6831 | goto err; |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6832 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6833 | // The above logic is fragile. Check these examples: |
| 6834 | // - interative read() still works |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6835 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6836 | } else { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6837 | #if ENABLE_DC |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6838 | // Most of dc parsing assumes all whitespace, |
| 6839 | // including '\n', is eaten. |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 6840 | while (G.prs.l.lex == XC_LEX_NLINE) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6841 | s = zbc_lex_next(); |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6842 | if (s) goto err; |
Denys Vlasenko | a17d95d | 2018-12-24 22:55:54 +0100 | [diff] [blame] | 6843 | if (G.prs.l.lex == XC_LEX_EOF) |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6844 | goto done; |
| 6845 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6846 | s = zdc_parse_expr(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6847 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6848 | } |
| 6849 | if (s || G_interrupt) { |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6850 | err: |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6851 | bc_parse_reset(); // includes bc_program_reset() |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6852 | RETURN_STATUS(BC_STATUS_FAILURE); |
| 6853 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6854 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 6855 | dbg_lex("%s:%d executing...", __func__, __LINE__); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6856 | s = zbc_program_exec(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6857 | if (s) { |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6858 | bc_program_reset(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6859 | break; |
| 6860 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6861 | |
| 6862 | ip = (void*)G.prog.exestack.v; |
| 6863 | #if SANITY_CHECKS |
| 6864 | if (G.prog.exestack.len != 1) // should have only main's IP |
| 6865 | bb_error_msg_and_die("BUG:call stack"); |
| 6866 | if (ip->func != BC_PROG_MAIN) |
| 6867 | bb_error_msg_and_die("BUG:not MAIN"); |
| 6868 | #endif |
| 6869 | f = bc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6870 | // bc discards strings, constants and code after each |
| 6871 | // top-level statement in the "main program". |
| 6872 | // This prevents "yes 1 | bc" from growing its memory |
| 6873 | // without bound. This can be done because data stack |
| 6874 | // is empty and thus can't hold any references to |
| 6875 | // strings or constants, there is no generated code |
| 6876 | // which can hold references (after we discard one |
| 6877 | // we just executed). Code of functions can have references, |
| 6878 | // but bc stores function strings/constants in per-function |
| 6879 | // storage. |
| 6880 | if (IS_BC) { |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6881 | #if SANITY_CHECKS |
Denys Vlasenko | 7c1c9dc | 2018-12-22 14:18:47 +0100 | [diff] [blame] | 6882 | if (G.prog.results.len != 0) // should be empty |
| 6883 | bb_error_msg_and_die("BUG:data stack"); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6884 | #endif |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6885 | IF_BC(bc_vec_pop_all(&f->strs);) |
| 6886 | IF_BC(bc_vec_pop_all(&f->consts);) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6887 | } else { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6888 | if (G.prog.results.len == 0 |
| 6889 | && G.prog.vars.len == 0 |
| 6890 | ) { |
| 6891 | // If stack is empty and no registers exist (TODO: or they are all empty), |
| 6892 | // we can get rid of accumulated strings and constants. |
| 6893 | // In this example dc process should not grow |
| 6894 | // its memory consumption with time: |
| 6895 | // yes 1pc | dc |
| 6896 | IF_DC(bc_vec_pop_all(&G.prog.strs);) |
| 6897 | IF_DC(bc_vec_pop_all(&G.prog.consts);) |
| 6898 | } |
| 6899 | // The code is discarded always (below), thus this example |
| 6900 | // should also not grow its memory consumption with time, |
| 6901 | // even though its data stack is not empty: |
| 6902 | // { echo 1; yes dk; } | dc |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6903 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6904 | // We drop generated and executed code for both bc and dc: |
| 6905 | bc_vec_pop_all(&f->code); |
| 6906 | ip->inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6907 | } |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6908 | done: |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6909 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6910 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6911 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6912 | #define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6913 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6914 | static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6915 | { |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6916 | // So far bc/dc have no way to include a file from another file, |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6917 | // therefore we know G.prs.filename == NULL on entry |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6918 | //const char *sv_file; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6919 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6920 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6921 | G.prs.filename = filename; |
| 6922 | G.prs.input_fp = fp; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6923 | bc_lex_file(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6924 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6925 | do { |
| 6926 | s = zbc_vm_process(""); |
| 6927 | // We do not stop looping on errors here if reading stdin. |
| 6928 | // Example: start interactive bc and enter "return". |
| 6929 | // It should say "'return' not in a function" |
| 6930 | // but should not exit. |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6931 | } while (G.prs.input_fp == stdin); |
| 6932 | G.prs.filename = NULL; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6933 | RETURN_STATUS(s); |
| 6934 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6935 | #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] | 6936 | |
| 6937 | static BC_STATUS zbc_vm_file(const char *file) |
| 6938 | { |
| 6939 | BcStatus s; |
| 6940 | FILE *fp; |
| 6941 | |
| 6942 | fp = xfopen_for_read(file); |
| 6943 | s = zbc_vm_execute_FILE(fp, file); |
| 6944 | fclose(fp); |
| 6945 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6946 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6947 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6948 | #define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6949 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6950 | #if ENABLE_BC |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 6951 | static void bc_vm_info(void) |
| 6952 | { |
| 6953 | printf("%s "BB_VER"\n" |
Gavin Howard | fa495ce | 2018-12-18 10:03:14 -0700 | [diff] [blame] | 6954 | "Adapted from https://github.com/gavinhoward/bc\n" |
| 6955 | "Original code (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 6956 | , applet_name); |
| 6957 | } |
| 6958 | |
| 6959 | static void bc_args(char **argv) |
| 6960 | { |
| 6961 | unsigned opts; |
| 6962 | int i; |
| 6963 | |
| 6964 | GETOPT_RESET(); |
| 6965 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
| 6966 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
| 6967 | "warn\0" No_argument "w" |
| 6968 | "version\0" No_argument "v" |
| 6969 | "standard\0" No_argument "s" |
| 6970 | "quiet\0" No_argument "q" |
| 6971 | "mathlib\0" No_argument "l" |
| 6972 | "interactive\0" No_argument "i" |
| 6973 | ); |
| 6974 | #else |
| 6975 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
| 6976 | #endif |
| 6977 | if (getenv("POSIXLY_CORRECT")) |
| 6978 | option_mask32 |= BC_FLAG_S; |
| 6979 | |
| 6980 | if (opts & BC_FLAG_V) { |
| 6981 | bc_vm_info(); |
| 6982 | exit(0); |
| 6983 | } |
| 6984 | |
| 6985 | for (i = optind; argv[i]; ++i) |
| 6986 | bc_vec_push(&G.files, argv + i); |
| 6987 | } |
| 6988 | |
| 6989 | static void bc_vm_envArgs(void) |
| 6990 | { |
| 6991 | BcVec v; |
| 6992 | char *buf; |
| 6993 | char *env_args = getenv("BC_ENV_ARGS"); |
| 6994 | |
| 6995 | if (!env_args) return; |
| 6996 | |
| 6997 | G.env_args = xstrdup(env_args); |
| 6998 | buf = G.env_args; |
| 6999 | |
| 7000 | bc_vec_init(&v, sizeof(char *), NULL); |
| 7001 | |
| 7002 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 7003 | bc_vec_push(&v, &buf); |
| 7004 | buf = skip_non_whitespace(buf); |
| 7005 | if (!*buf) |
| 7006 | break; |
| 7007 | *buf++ = '\0'; |
| 7008 | } |
| 7009 | |
| 7010 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 7011 | if (sizeof(int) == sizeof(char*)) { |
| 7012 | bc_vec_push(&v, &const_int_0); |
| 7013 | } else { |
| 7014 | static char *const nullptr = NULL; |
| 7015 | bc_vec_push(&v, &nullptr); |
| 7016 | } |
| 7017 | bc_args(((char **)v.v) - 1); |
| 7018 | |
| 7019 | bc_vec_free(&v); |
| 7020 | } |
| 7021 | |
| 7022 | static const char bc_lib[] ALIGN1 = { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7023 | "scale=20" |
| 7024 | "\n" "define e(x){" |
| 7025 | "\n" "auto b,s,n,r,d,i,p,f,v" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7026 | ////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below |
| 7027 | //^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc: |
| 7028 | //e(-.998896): GNU:.36828580434569428695 |
| 7029 | // above code:.36828580434569428696 |
| 7030 | // actual value:.3682858043456942869594... |
| 7031 | // but for now let's be "GNU compatible" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7032 | "\n" "b=ibase" |
| 7033 | "\n" "ibase=A" |
| 7034 | "\n" "if(x<0){" |
| 7035 | "\n" "n=1" |
| 7036 | "\n" "x=-x" |
| 7037 | "\n" "}" |
| 7038 | "\n" "s=scale" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 7039 | "\n" "r=6+s+.44*x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7040 | "\n" "scale=scale(x)+1" |
| 7041 | "\n" "while(x>1){" |
| 7042 | "\n" "d+=1" |
| 7043 | "\n" "x/=2" |
| 7044 | "\n" "scale+=1" |
| 7045 | "\n" "}" |
| 7046 | "\n" "scale=r" |
| 7047 | "\n" "r=x+1" |
| 7048 | "\n" "p=x" |
| 7049 | "\n" "f=v=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7050 | "\n" "for(i=2;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7051 | "\n" "p*=x" |
| 7052 | "\n" "f*=i" |
| 7053 | "\n" "v=p/f" |
| 7054 | "\n" "r+=v" |
| 7055 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7056 | "\n" "while(d--)r*=r" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7057 | "\n" "scale=s" |
| 7058 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7059 | "\n" "if(n)return(1/r)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7060 | "\n" "return(r/1)" |
| 7061 | "\n" "}" |
| 7062 | "\n" "define l(x){" |
| 7063 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7064 | "\n" "b=ibase" |
| 7065 | "\n" "ibase=A" |
| 7066 | "\n" "if(x<=0){" |
| 7067 | "\n" "r=(1-10^scale)/1" |
| 7068 | "\n" "ibase=b" |
| 7069 | "\n" "return(r)" |
| 7070 | "\n" "}" |
| 7071 | "\n" "s=scale" |
| 7072 | "\n" "scale+=6" |
| 7073 | "\n" "p=2" |
| 7074 | "\n" "while(x>=2){" |
| 7075 | "\n" "p*=2" |
| 7076 | "\n" "x=sqrt(x)" |
| 7077 | "\n" "}" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 7078 | "\n" "while(x<=.5){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7079 | "\n" "p*=2" |
| 7080 | "\n" "x=sqrt(x)" |
| 7081 | "\n" "}" |
| 7082 | "\n" "r=a=(x-1)/(x+1)" |
| 7083 | "\n" "q=a*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7084 | "\n" "v=1" |
| 7085 | "\n" "for(i=3;v;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7086 | "\n" "a*=q" |
| 7087 | "\n" "v=a/i" |
| 7088 | "\n" "r+=v" |
| 7089 | "\n" "}" |
| 7090 | "\n" "r*=p" |
| 7091 | "\n" "scale=s" |
| 7092 | "\n" "ibase=b" |
| 7093 | "\n" "return(r/1)" |
| 7094 | "\n" "}" |
| 7095 | "\n" "define s(x){" |
Denys Vlasenko | 240d7ee | 2018-12-14 11:27:09 +0100 | [diff] [blame] | 7096 | "\n" "auto b,s,r,a,q,i" |
| 7097 | "\n" "if(x<0)return(-s(-x))" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7098 | "\n" "b=ibase" |
| 7099 | "\n" "ibase=A" |
| 7100 | "\n" "s=scale" |
| 7101 | "\n" "scale=1.1*s+2" |
| 7102 | "\n" "a=a(1)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7103 | "\n" "scale=0" |
| 7104 | "\n" "q=(x/a+2)/4" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7105 | "\n" "x-=4*q*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7106 | "\n" "if(q%2)x=-x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7107 | "\n" "scale=s+2" |
| 7108 | "\n" "r=a=x" |
| 7109 | "\n" "q=-x*x" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7110 | "\n" "for(i=3;a;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7111 | "\n" "a*=q/(i*(i-1))" |
| 7112 | "\n" "r+=a" |
| 7113 | "\n" "}" |
| 7114 | "\n" "scale=s" |
| 7115 | "\n" "ibase=b" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7116 | "\n" "return(r/1)" |
| 7117 | "\n" "}" |
| 7118 | "\n" "define c(x){" |
| 7119 | "\n" "auto b,s" |
| 7120 | "\n" "b=ibase" |
| 7121 | "\n" "ibase=A" |
| 7122 | "\n" "s=scale" |
| 7123 | "\n" "scale*=1.2" |
| 7124 | "\n" "x=s(2*a(1)+x)" |
| 7125 | "\n" "scale=s" |
| 7126 | "\n" "ibase=b" |
| 7127 | "\n" "return(x/1)" |
| 7128 | "\n" "}" |
| 7129 | "\n" "define a(x){" |
| 7130 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7131 | "\n" "b=ibase" |
| 7132 | "\n" "ibase=A" |
| 7133 | "\n" "n=1" |
| 7134 | "\n" "if(x<0){" |
| 7135 | "\n" "n=-1" |
| 7136 | "\n" "x=-x" |
| 7137 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7138 | "\n" "if(scale<65){" |
| 7139 | "\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7140 | "\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7141 | "\n" "}" |
| 7142 | "\n" "s=scale" |
| 7143 | "\n" "if(x>.2){" |
| 7144 | "\n" "scale+=5" |
| 7145 | "\n" "a=a(.2)" |
| 7146 | "\n" "}" |
| 7147 | "\n" "scale=s+3" |
| 7148 | "\n" "while(x>.2){" |
| 7149 | "\n" "m+=1" |
| 7150 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7151 | "\n" "}" |
| 7152 | "\n" "r=u=x" |
| 7153 | "\n" "f=-x*x" |
| 7154 | "\n" "t=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7155 | "\n" "for(i=3;t;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7156 | "\n" "u*=f" |
| 7157 | "\n" "t=u/i" |
| 7158 | "\n" "r+=t" |
| 7159 | "\n" "}" |
| 7160 | "\n" "scale=s" |
| 7161 | "\n" "ibase=b" |
| 7162 | "\n" "return((m*a+r)/n)" |
| 7163 | "\n" "}" |
| 7164 | "\n" "define j(n,x){" |
| 7165 | "\n" "auto b,s,o,a,i,v,f" |
| 7166 | "\n" "b=ibase" |
| 7167 | "\n" "ibase=A" |
| 7168 | "\n" "s=scale" |
| 7169 | "\n" "scale=0" |
| 7170 | "\n" "n/=1" |
| 7171 | "\n" "if(n<0){" |
| 7172 | "\n" "n=-n" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7173 | "\n" "o=n%2" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7174 | "\n" "}" |
| 7175 | "\n" "a=1" |
| 7176 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7177 | "\n" "scale=1.5*s" |
| 7178 | "\n" "a=(x^n)/2^n/a" |
| 7179 | "\n" "r=v=1" |
| 7180 | "\n" "f=-x*x/4" |
Denys Vlasenko | c06537d | 2018-12-14 10:10:37 +0100 | [diff] [blame] | 7181 | "\n" "scale+=length(a)-scale(a)" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7182 | "\n" "for(i=1;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7183 | "\n" "v=v*f/i/(n+i)" |
| 7184 | "\n" "r+=v" |
| 7185 | "\n" "}" |
| 7186 | "\n" "scale=s" |
| 7187 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7188 | "\n" "if(o)a=-a" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7189 | "\n" "return(a*r/1)" |
| 7190 | "\n" "}" |
| 7191 | }; |
| 7192 | #endif // ENABLE_BC |
| 7193 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7194 | static BC_STATUS zbc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7195 | { |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7196 | char **fname; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7197 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7198 | size_t i; |
| 7199 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7200 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7201 | if (option_mask32 & BC_FLAG_L) { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7202 | // We know that internal library is not buggy, |
| 7203 | // thus error checking is normally disabled. |
| 7204 | # define DEBUG_LIB 0 |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 7205 | bc_lex_file(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 7206 | s = zbc_vm_process(bc_lib); |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7207 | if (DEBUG_LIB && s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7208 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7209 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7210 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7211 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7212 | fname = (void*)G.files.v; |
| 7213 | for (i = 0; i < G.files.len; i++) { |
| 7214 | s = zbc_vm_file(*fname++); |
| 7215 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) { |
| 7216 | // Debug config, non-interactive mode: |
| 7217 | // return all the way back to main. |
| 7218 | // Non-debug builds do not come here |
| 7219 | // in non-interactive mode, they exit. |
| 7220 | RETURN_STATUS(s); |
| 7221 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7222 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7223 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7224 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7225 | s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7226 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7227 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7228 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 7229 | #define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7230 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7231 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7232 | static void bc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7233 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7234 | bc_vec_free(&G.prog.fns); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7235 | IF_BC(bc_vec_free(&G.prog.fn_map);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7236 | bc_vec_free(&G.prog.vars); |
| 7237 | bc_vec_free(&G.prog.var_map); |
| 7238 | bc_vec_free(&G.prog.arrs); |
| 7239 | bc_vec_free(&G.prog.arr_map); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7240 | IF_DC(bc_vec_free(&G.prog.strs);) |
| 7241 | IF_DC(bc_vec_free(&G.prog.consts);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7242 | bc_vec_free(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7243 | bc_vec_free(&G.prog.exestack); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7244 | IF_BC(bc_num_free(&G.prog.last);) |
| 7245 | IF_BC(bc_num_free(&G.prog.zero);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7246 | IF_BC(bc_num_free(&G.prog.one);) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7247 | bc_vec_free(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7248 | } |
| 7249 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7250 | static void bc_vm_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7251 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7252 | bc_vec_free(&G.files); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7253 | bc_program_free(); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 7254 | bc_parse_free(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7255 | free(G.env_args); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7256 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7257 | #endif |
| 7258 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7259 | static void bc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7260 | { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7261 | BcInstPtr ip; |
| 7262 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7263 | // memset(&G.prog, 0, sizeof(G.prog)); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7264 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7265 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7266 | // G.prog.nchars = G.prog.scale = 0; - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7267 | G.prog.ib_t = 10; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7268 | G.prog.ob_t = 10; |
| 7269 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7270 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);) |
| 7271 | //IF_BC(bc_num_zero(&G.prog.last);) - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7272 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7273 | bc_num_init_DEF_SIZE(&G.prog.zero); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7274 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7275 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7276 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);) |
| 7277 | IF_BC(bc_num_one(&G.prog.one);) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7278 | |
| 7279 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7280 | 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] | 7281 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7282 | if (IS_BC) { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7283 | // Names are chosen simply to be distinct and never match |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 7284 | // a valid function name (and be short) |
| 7285 | IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7286 | IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read() |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7287 | } else { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7288 | // in dc, functions have no names |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7289 | bc_program_add_fn(); |
| 7290 | bc_program_add_fn(); |
| 7291 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7292 | |
| 7293 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7294 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7295 | |
| 7296 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7297 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7298 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7299 | IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);) |
| 7300 | 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] | 7301 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7302 | bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL); |
| 7303 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7304 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7305 | bc_char_vec_init(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7306 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7307 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7308 | static int bc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7309 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7310 | #if ENABLE_FEATURE_EDITING |
| 7311 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7312 | #endif |
| 7313 | G.prog.len = bc_vm_envLen(env_len); |
| 7314 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7315 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 5f263f4 | 2018-12-13 22:49:59 +0100 | [diff] [blame] | 7316 | IF_BC(if (IS_BC) bc_vm_envArgs();) |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7317 | bc_program_init(); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 7318 | bc_parse_create(BC_PROG_MAIN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7319 | |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7320 | //TODO: in GNU bc, the check is (isatty(0) && isatty(1)), |
| 7321 | //-i option unconditionally enables this regardless of isatty(): |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7322 | if (isatty(0)) { |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7323 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7324 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7325 | // With SA_RESTART, most system calls will restart |
| 7326 | // (IOW: they won't fail with EINTR). |
| 7327 | // In particular, this means ^C won't cause |
| 7328 | // stdout to get into "error state" if SIGINT hits |
| 7329 | // within write() syscall. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7330 | // |
| 7331 | // The downside is that ^C while tty input is taken |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7332 | // will only be handled after [Enter] since read() |
| 7333 | // from stdin is not interrupted by ^C either, |
| 7334 | // it restarts, thus fgetc() does not return on ^C. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7335 | // (This problem manifests only if line editing is disabled) |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7336 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7337 | |
| 7338 | // Without SA_RESTART, this exhibits a bug: |
| 7339 | // "while (1) print 1" and try ^C-ing it. |
| 7340 | // Intermittently, instead of returning to input line, |
| 7341 | // you'll get "output error: Interrupted system call" |
| 7342 | // and exit. |
| 7343 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7344 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7345 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7346 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7347 | return 0; // "not a tty" |
| 7348 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7349 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7350 | static BcStatus bc_vm_run(void) |
| 7351 | { |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7352 | BcStatus st = zbc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7353 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7354 | if (G_exiting) // it was actually "halt" or "quit" |
| 7355 | st = EXIT_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7356 | bc_vm_free(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7357 | # if ENABLE_FEATURE_EDITING |
| 7358 | free_line_input_t(G.line_input_state); |
| 7359 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7360 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7361 | #endif |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7362 | dbg_exec("exiting with exitcode %d", st); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7363 | return st; |
| 7364 | } |
| 7365 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7366 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7367 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7368 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7369 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7370 | int is_tty; |
| 7371 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7372 | INIT_G(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7373 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7374 | is_tty = bc_vm_init("BC_LINE_LENGTH"); |
| 7375 | |
| 7376 | bc_args(argv); |
| 7377 | |
| 7378 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7379 | bc_vm_info(); |
| 7380 | |
| 7381 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7382 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7383 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7384 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7385 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7386 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7387 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7388 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7389 | int noscript; |
| 7390 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7391 | INIT_G(); |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7392 | |
| 7393 | // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7394 | // 1 char wider than bc from the same package. |
| 7395 | // Both default width, and xC_LINE_LENGTH=N are wider: |
| 7396 | // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7397 | // |1234\ | |
| 7398 | // |56 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7399 | // "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7400 | // |123\ | |
| 7401 | // |456 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7402 | // Do the same, or it's a bug? |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7403 | bc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7404 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7405 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7406 | noscript = BC_FLAG_I; |
| 7407 | for (;;) { |
| 7408 | int n = getopt(argc, argv, "e:f:x"); |
| 7409 | if (n <= 0) |
| 7410 | break; |
| 7411 | switch (n) { |
| 7412 | case 'e': |
| 7413 | noscript = 0; |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7414 | n = zbc_vm_process(optarg); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7415 | if (n) return n; |
| 7416 | break; |
| 7417 | case 'f': |
| 7418 | noscript = 0; |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7419 | n = zbc_vm_file(optarg); |
| 7420 | if (n) return n; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7421 | break; |
| 7422 | case 'x': |
| 7423 | option_mask32 |= DC_FLAG_X; |
| 7424 | break; |
| 7425 | default: |
| 7426 | bb_show_usage(); |
| 7427 | } |
| 7428 | } |
| 7429 | argv += optind; |
| 7430 | |
| 7431 | while (*argv) { |
| 7432 | noscript = 0; |
| 7433 | bc_vec_push(&G.files, argv++); |
| 7434 | } |
| 7435 | |
| 7436 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7437 | |
| 7438 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7439 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7440 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7441 | |
| 7442 | #endif // not DC_SMALL |