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 | */ |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 7 | //TODO: |
| 8 | // maybe implement a^b for non-integer b? |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 9 | |
| 10 | #define DEBUG_LEXER 0 |
| 11 | #define DEBUG_COMPILE 0 |
| 12 | #define DEBUG_EXEC 0 |
| 13 | // This can be left enabled for production as well: |
| 14 | #define SANITY_CHECKS 1 |
| 15 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 16 | //config:config BC |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 17 | //config: bool "bc (45 kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 18 | //config: default y |
Denys Vlasenko | cdadad5 | 2018-12-28 15:13:23 +0100 | [diff] [blame] | 19 | //config: select FEATURE_DC_BIG |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 20 | //config: help |
| 21 | //config: bc is a command-line, arbitrary-precision calculator with a |
| 22 | //config: Turing-complete language. See the GNU bc manual |
| 23 | //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] | 24 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 25 | //config: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 26 | //config: This bc has five differences to the GNU bc: |
| 27 | //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] | 28 | //config: 2) Arrays are copied before being passed as arguments to |
| 29 | //config: functions. This behavior is required by the bc spec. |
| 30 | //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] | 31 | //config: the number of elements in the array. This prints "1": |
| 32 | //config: a[0] = 0; length(a[]) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 33 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 34 | //config: that of the unary minus (-) negation operator. This still |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 35 | //config: allows POSIX-compliant scripts to work while somewhat |
| 36 | //config: preserving expected behavior (versus C) and making parsing |
| 37 | //config: easier. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 38 | //config: 5) "read()" accepts expressions, not only numeric literals. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 39 | //config: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 40 | //config:config DC |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 41 | //config: bool "dc (36 kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 42 | //config: default y |
| 43 | //config: help |
| 44 | //config: dc is a reverse-polish notation command-line calculator which |
| 45 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 46 | //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] | 47 | //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] | 48 | //config: |
| 49 | //config: This dc has a few differences from the two above: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 50 | //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] | 51 | //config: the FreeBSD dc does. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 52 | //config: 2) Implements the GNU extensions for divmod ("~") and |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 53 | //config: modular exponentiation ("|"). |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 54 | //config: 3) Implements all FreeBSD extensions, except for "J" and "M". |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 55 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 56 | //config: However, they are implemented differently. When it encounters |
| 57 | //config: whitespace where a register should be, it skips the whitespace. |
| 58 | //config: If the character following is not a lowercase letter, an error |
| 59 | //config: is issued. Otherwise, the register name is parsed by the |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 60 | //config: following regex: [a-z][a-z0-9_]* |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 61 | //config: This generally means that register names will be surrounded by |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 62 | //config: whitespace. Examples: |
| 63 | //config: l idx s temp L index S temp2 < do_thing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 64 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 65 | //config: allowed unless the "-x" option is given. |
| 66 | //config: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 67 | //config:if BC || DC # for menuconfig indenting |
| 68 | //config: |
| 69 | //config:config FEATURE_DC_BIG |
| 70 | //config: bool "Use bc code base for dc (larger, more features)" |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 71 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 72 | //config: |
| 73 | //config:config FEATURE_DC_LIBM |
| 74 | //config: bool "Enable power and exp functions (requires libm)" |
| 75 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 76 | //config: depends on DC && !BC && !FEATURE_DC_BIG |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 77 | //config: help |
| 78 | //config: Enable power and exp functions. |
| 79 | //config: NOTE: This will require libm to be present for linking. |
| 80 | //config: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 81 | //config:config FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 82 | //config: bool "Interactive mode (+4kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 83 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 84 | //config: depends on BC || (DC && FEATURE_DC_BIG) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 85 | //config: help |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 86 | //config: Enable interactive mode: when started on a tty, |
| 87 | //config: ^C interrupts execution and returns to command line, |
| 88 | //config: errors also return to command line instead of exiting, |
| 89 | //config: line editing with history is available. |
| 90 | //config: |
| 91 | //config: With this option off, input can still be taken from tty, |
| 92 | //config: but all errors are fatal, ^C is fatal, |
| 93 | //config: tty is treated exactly the same as any other |
| 94 | //config: standard input (IOW: no line editing). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 95 | //config: |
| 96 | //config:config FEATURE_BC_LONG_OPTIONS |
| 97 | //config: bool "Enable bc/dc long options" |
| 98 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 99 | //config: depends on BC || (DC && FEATURE_DC_BIG) |
| 100 | //config: |
| 101 | //config:endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 102 | |
| 103 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 104 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 105 | |
| 106 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 107 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 108 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 109 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 110 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 09fe0aa | 2018-12-18 16:32:25 +0100 | [diff] [blame] | 111 | //usage: "[-sqlw] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 112 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 113 | //usage:#define bc_full_usage "\n" |
| 114 | //usage: "\nArbitrary precision calculator" |
| 115 | //usage: "\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 116 | ///////: "\n -i Interactive" - has no effect for now |
| 117 | //usage: "\n -q Quiet" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 118 | //usage: "\n -l Load standard math library" |
| 119 | //usage: "\n -s Be POSIX compatible" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 120 | //usage: "\n -w Warn if extensions are used" |
| 121 | ///////: "\n -v Version" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 122 | //usage: "\n" |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 123 | //usage: "\n$BC_LINE_LENGTH changes output width" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 124 | //usage: |
| 125 | //usage:#define bc_example_usage |
| 126 | //usage: "3 + 4.129\n" |
| 127 | //usage: "1903 - 2893\n" |
| 128 | //usage: "-129 * 213.28935\n" |
| 129 | //usage: "12 / -1932\n" |
| 130 | //usage: "12 % 12\n" |
| 131 | //usage: "34 ^ 189\n" |
| 132 | //usage: "scale = 13\n" |
| 133 | //usage: "ibase = 2\n" |
| 134 | //usage: "obase = A\n" |
| 135 | //usage: |
| 136 | //usage:#define dc_trivial_usage |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 137 | //usage: IF_FEATURE_DC_BIG("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 138 | //usage: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 139 | //usage:#define dc_full_usage "\n" |
| 140 | //usage: "\nTiny RPN calculator. Operations:" |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 141 | //usage: "\n+, -, *, /, %, ~, ^," IF_FEATURE_DC_BIG(" |,") |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 142 | //usage: "\np - print top of the stack without popping" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 143 | //usage: "\nf - print entire stack" |
| 144 | //usage: "\nk - pop the value and set the precision" |
| 145 | //usage: "\ni - pop the value and set input radix" |
| 146 | //usage: "\no - pop the value and set output radix" |
| 147 | //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] | 148 | //usage: |
| 149 | //usage:#define dc_example_usage |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 150 | //usage: "$ dc -e'2 2 + p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 151 | //usage: "4\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 152 | //usage: "$ dc -e'8 8 \\* 2 2 + / p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 153 | //usage: "16\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 154 | //usage: "$ dc -e'0 1 & p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 155 | //usage: "0\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 156 | //usage: "$ dc -e'0 1 | p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 157 | //usage: "1\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 158 | //usage: "$ echo '72 9 / 8 * p' | dc\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 159 | //usage: "64\n" |
| 160 | |
| 161 | #include "libbb.h" |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 162 | #include "common_bufsiz.h" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 163 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 164 | #if !ENABLE_BC && !ENABLE_FEATURE_DC_BIG |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 165 | # include "dc.c" |
| 166 | #else |
| 167 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 168 | #if DEBUG_LEXER |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 169 | static uint8_t lex_indent; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 170 | #define dbg_lex(...) \ |
| 171 | do { \ |
| 172 | fprintf(stderr, "%*s", lex_indent, ""); \ |
| 173 | bb_error_msg(__VA_ARGS__); \ |
| 174 | } while (0) |
| 175 | #define dbg_lex_enter(...) \ |
| 176 | do { \ |
| 177 | dbg_lex(__VA_ARGS__); \ |
| 178 | lex_indent++; \ |
| 179 | } while (0) |
| 180 | #define dbg_lex_done(...) \ |
| 181 | do { \ |
| 182 | lex_indent--; \ |
| 183 | dbg_lex(__VA_ARGS__); \ |
| 184 | } while (0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 185 | #else |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 186 | # define dbg_lex(...) ((void)0) |
| 187 | # define dbg_lex_enter(...) ((void)0) |
| 188 | # define dbg_lex_done(...) ((void)0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 189 | #endif |
| 190 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 191 | #if DEBUG_COMPILE |
| 192 | # define dbg_compile(...) bb_error_msg(__VA_ARGS__) |
| 193 | #else |
| 194 | # define dbg_compile(...) ((void)0) |
| 195 | #endif |
| 196 | |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 197 | #if DEBUG_EXEC |
| 198 | # define dbg_exec(...) bb_error_msg(__VA_ARGS__) |
| 199 | #else |
| 200 | # define dbg_exec(...) ((void)0) |
| 201 | #endif |
| 202 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 203 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 204 | BC_STATUS_SUCCESS = 0, |
| 205 | BC_STATUS_FAILURE = 1, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 206 | } BcStatus; |
| 207 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 208 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 209 | #define BC_VEC_START_CAP (1 << 5) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 210 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 211 | typedef void (*BcVecFree)(void *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 212 | |
| 213 | typedef struct BcVec { |
| 214 | char *v; |
| 215 | size_t len; |
| 216 | size_t cap; |
| 217 | size_t size; |
| 218 | BcVecFree dtor; |
| 219 | } BcVec; |
| 220 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 221 | typedef signed char BcDig; |
| 222 | |
| 223 | typedef struct BcNum { |
| 224 | BcDig *restrict num; |
| 225 | size_t rdx; |
| 226 | size_t len; |
| 227 | size_t cap; |
| 228 | bool neg; |
| 229 | } BcNum; |
| 230 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 231 | #define BC_NUM_MAX_IBASE 36 |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 232 | // larger value might speed up BIGNUM calculations a bit: |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 233 | #define BC_NUM_DEF_SIZE 16 |
Denys Vlasenko | 29a9043 | 2020-12-29 18:50:56 +0100 | [diff] [blame] | 234 | #define BC_NUM_PRINT_WIDTH 70 |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 235 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 236 | #define BC_NUM_KARATSUBA_LEN 32 |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 237 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 238 | typedef enum BcInst { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 239 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 240 | BC_INST_INC_PRE, |
| 241 | BC_INST_DEC_PRE, |
| 242 | BC_INST_INC_POST, |
| 243 | BC_INST_DEC_POST, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 244 | #endif |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 245 | XC_INST_NEG, // order |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 246 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 247 | XC_INST_REL_EQ, // should |
| 248 | XC_INST_REL_LE, // match |
| 249 | XC_INST_REL_GE, // LEX |
| 250 | XC_INST_REL_NE, // constants |
| 251 | XC_INST_REL_LT, // for |
| 252 | XC_INST_REL_GT, // these |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 253 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 254 | XC_INST_POWER, // operations |
| 255 | XC_INST_MULTIPLY, // | |
| 256 | XC_INST_DIVIDE, // | |
| 257 | XC_INST_MODULUS, // | |
| 258 | XC_INST_PLUS, // | |
| 259 | XC_INST_MINUS, // | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 260 | |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 261 | XC_INST_BOOL_NOT, // | |
| 262 | XC_INST_BOOL_OR, // | |
| 263 | XC_INST_BOOL_AND, // | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 264 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 265 | BC_INST_ASSIGN_POWER, // | |
| 266 | BC_INST_ASSIGN_MULTIPLY,// | |
| 267 | BC_INST_ASSIGN_DIVIDE, // | |
| 268 | BC_INST_ASSIGN_MODULUS, // | |
| 269 | BC_INST_ASSIGN_PLUS, // | |
| 270 | BC_INST_ASSIGN_MINUS, // | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 271 | #endif |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 272 | XC_INST_ASSIGN, // V |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 273 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 274 | XC_INST_NUM, |
| 275 | XC_INST_VAR, |
| 276 | XC_INST_ARRAY_ELEM, |
| 277 | XC_INST_ARRAY, |
| 278 | XC_INST_SCALE_FUNC, |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 279 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 280 | XC_INST_IBASE, // order of these constans should match other enums |
| 281 | XC_INST_OBASE, // order of these constans should match other enums |
| 282 | XC_INST_SCALE, // order of these constans should match other enums |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 283 | 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] | 284 | XC_INST_LENGTH, |
| 285 | XC_INST_READ, |
| 286 | XC_INST_SQRT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 287 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 288 | XC_INST_PRINT, |
| 289 | XC_INST_PRINT_POP, |
| 290 | XC_INST_STR, |
| 291 | XC_INST_PRINT_STR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 292 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 293 | #if ENABLE_BC |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 294 | BC_INST_HALT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 295 | BC_INST_JUMP, |
| 296 | BC_INST_JUMP_ZERO, |
| 297 | |
| 298 | BC_INST_CALL, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 299 | BC_INST_RET0, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 300 | #endif |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 301 | XC_INST_RET, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 302 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 303 | XC_INST_POP, |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 304 | #if ENABLE_DC |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 305 | DC_INST_POP_EXEC, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 306 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 307 | DC_INST_MODEXP, |
| 308 | DC_INST_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 309 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 310 | DC_INST_EXECUTE, |
| 311 | DC_INST_EXEC_COND, |
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 | DC_INST_ASCIIFY, |
| 314 | DC_INST_PRINT_STREAM, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 315 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 316 | DC_INST_PRINT_STACK, |
| 317 | DC_INST_CLEAR_STACK, |
| 318 | DC_INST_STACK_LEN, |
| 319 | DC_INST_DUPLICATE, |
| 320 | DC_INST_SWAP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 321 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 322 | DC_INST_LOAD, |
| 323 | DC_INST_PUSH_VAR, |
| 324 | DC_INST_PUSH_TO_VAR, |
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_QUIT, |
| 327 | DC_INST_NQUIT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 328 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 329 | DC_INST_INVALID = -1, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 330 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 331 | } BcInst; |
| 332 | |
| 333 | typedef struct BcId { |
| 334 | char *name; |
| 335 | size_t idx; |
| 336 | } BcId; |
| 337 | |
| 338 | typedef struct BcFunc { |
| 339 | BcVec code; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 340 | IF_BC(BcVec labels;) |
| 341 | IF_BC(BcVec autos;) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 342 | IF_BC(BcVec strs;) |
| 343 | IF_BC(BcVec consts;) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 344 | IF_BC(size_t nparams;) |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 345 | IF_BC(bool voidfunc;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 346 | } BcFunc; |
| 347 | |
| 348 | typedef enum BcResultType { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 349 | XC_RESULT_TEMP, |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 350 | IF_BC(BC_RESULT_VOID,) // same as TEMP, but INST_PRINT will ignore it |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 351 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 352 | XC_RESULT_VAR, |
| 353 | XC_RESULT_ARRAY_ELEM, |
| 354 | XC_RESULT_ARRAY, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 355 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 356 | XC_RESULT_STR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 357 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 358 | //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct, |
| 359 | XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE |
| 360 | XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE |
| 361 | XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 362 | 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] | 363 | XC_RESULT_CONSTANT, |
| 364 | IF_BC(BC_RESULT_ONE,) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 365 | } BcResultType; |
| 366 | |
| 367 | typedef union BcResultData { |
| 368 | BcNum n; |
| 369 | BcVec v; |
| 370 | BcId id; |
| 371 | } BcResultData; |
| 372 | |
| 373 | typedef struct BcResult { |
| 374 | BcResultType t; |
| 375 | BcResultData d; |
| 376 | } BcResult; |
| 377 | |
| 378 | typedef struct BcInstPtr { |
| 379 | size_t func; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 380 | size_t inst_idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 381 | } BcInstPtr; |
| 382 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 383 | typedef enum BcType { |
| 384 | BC_TYPE_VAR, |
| 385 | BC_TYPE_ARRAY, |
| 386 | BC_TYPE_REF, |
| 387 | } BcType; |
| 388 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 389 | typedef enum BcLexType { |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 390 | XC_LEX_EOF, |
| 391 | XC_LEX_INVALID, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 392 | |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 393 | XC_LEX_NLINE, |
| 394 | XC_LEX_WHITESPACE, |
| 395 | XC_LEX_STR, |
| 396 | XC_LEX_NAME, |
| 397 | XC_LEX_NUMBER, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 398 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 399 | XC_LEX_1st_op, |
| 400 | XC_LEX_NEG = XC_LEX_1st_op, // order |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 401 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 402 | XC_LEX_OP_REL_EQ, // should |
| 403 | XC_LEX_OP_REL_LE, // match |
| 404 | XC_LEX_OP_REL_GE, // INST |
| 405 | XC_LEX_OP_REL_NE, // constants |
| 406 | XC_LEX_OP_REL_LT, // for |
| 407 | XC_LEX_OP_REL_GT, // these |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 408 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 409 | XC_LEX_OP_POWER, // operations |
| 410 | XC_LEX_OP_MULTIPLY, // | |
| 411 | XC_LEX_OP_DIVIDE, // | |
| 412 | XC_LEX_OP_MODULUS, // | |
| 413 | XC_LEX_OP_PLUS, // | |
| 414 | XC_LEX_OP_MINUS, // | |
| 415 | XC_LEX_OP_last = XC_LEX_OP_MINUS, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 416 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 417 | BC_LEX_OP_BOOL_NOT, // | |
| 418 | BC_LEX_OP_BOOL_OR, // | |
| 419 | BC_LEX_OP_BOOL_AND, // | |
| 420 | |
| 421 | BC_LEX_OP_ASSIGN_POWER, // | |
| 422 | BC_LEX_OP_ASSIGN_MULTIPLY, // | |
| 423 | BC_LEX_OP_ASSIGN_DIVIDE, // | |
| 424 | BC_LEX_OP_ASSIGN_MODULUS, // | |
| 425 | BC_LEX_OP_ASSIGN_PLUS, // | |
| 426 | BC_LEX_OP_ASSIGN_MINUS, // | |
| 427 | |
| 428 | BC_LEX_OP_ASSIGN, // V |
| 429 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 430 | BC_LEX_OP_INC, |
| 431 | BC_LEX_OP_DEC, |
| 432 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 433 | BC_LEX_LPAREN, // () are 0x28 and 0x29 |
| 434 | 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] | 435 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 436 | BC_LEX_LBRACKET, // [] are 0x5B and 0x5D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 437 | BC_LEX_COMMA, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 438 | 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] | 439 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 440 | BC_LEX_LBRACE, // {} are 0x7B and 0x7D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 441 | BC_LEX_SCOLON, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 442 | 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] | 443 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 444 | BC_LEX_KEY_1st_keyword, |
| 445 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 446 | BC_LEX_KEY_BREAK, |
| 447 | BC_LEX_KEY_CONTINUE, |
| 448 | BC_LEX_KEY_DEFINE, |
| 449 | BC_LEX_KEY_ELSE, |
| 450 | BC_LEX_KEY_FOR, |
| 451 | BC_LEX_KEY_HALT, |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 452 | // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct, |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 453 | BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE |
| 454 | BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 455 | BC_LEX_KEY_IF, |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 456 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 457 | BC_LEX_KEY_LENGTH, |
| 458 | BC_LEX_KEY_LIMITS, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 459 | BC_LEX_KEY_PRINT, |
| 460 | BC_LEX_KEY_QUIT, |
| 461 | BC_LEX_KEY_READ, |
| 462 | BC_LEX_KEY_RETURN, |
| 463 | BC_LEX_KEY_SCALE, |
| 464 | BC_LEX_KEY_SQRT, |
| 465 | BC_LEX_KEY_WHILE, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 466 | #endif // ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 467 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 468 | #if ENABLE_DC |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 469 | DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1, |
| 470 | DC_LEX_OP_ASSIGN, |
| 471 | |
| 472 | DC_LEX_LPAREN, |
| 473 | DC_LEX_SCOLON, |
| 474 | DC_LEX_READ, |
| 475 | DC_LEX_IBASE, |
| 476 | DC_LEX_SCALE, |
| 477 | DC_LEX_OBASE, |
| 478 | DC_LEX_LENGTH, |
| 479 | DC_LEX_PRINT, |
| 480 | DC_LEX_QUIT, |
| 481 | DC_LEX_SQRT, |
| 482 | DC_LEX_LBRACE, |
| 483 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 484 | DC_LEX_EQ_NO_REG, |
| 485 | DC_LEX_OP_MODEXP, |
| 486 | DC_LEX_OP_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 487 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 488 | DC_LEX_COLON, |
| 489 | DC_LEX_ELSE, |
| 490 | DC_LEX_EXECUTE, |
| 491 | DC_LEX_PRINT_STACK, |
| 492 | DC_LEX_CLEAR_STACK, |
| 493 | DC_LEX_STACK_LEVEL, |
| 494 | DC_LEX_DUPLICATE, |
| 495 | DC_LEX_SWAP, |
| 496 | DC_LEX_POP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 497 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 498 | DC_LEX_ASCIIFY, |
| 499 | DC_LEX_PRINT_STREAM, |
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 | // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct, |
| 502 | DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE |
| 503 | DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE |
| 504 | DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE |
| 505 | DC_LEX_LOAD, |
| 506 | DC_LEX_LOAD_POP, |
| 507 | DC_LEX_STORE_PUSH, |
| 508 | DC_LEX_PRINT_POP, |
| 509 | DC_LEX_NQUIT, |
| 510 | DC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 511 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 512 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 513 | // must match order of BC_LEX_KEY_foo etc above |
| 514 | #if ENABLE_BC |
| 515 | struct BcLexKeyword { |
| 516 | char name8[8]; |
| 517 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 518 | #define LEX_KW_ENTRY(a, b) \ |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 519 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 6cc4962 | 2020-11-30 14:58:02 +0100 | [diff] [blame] | 520 | static const struct BcLexKeyword bc_lex_kws[20] ALIGN8 = { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 521 | LEX_KW_ENTRY("auto" , 1), // 0 |
| 522 | LEX_KW_ENTRY("break" , 1), // 1 |
| 523 | LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 524 | LEX_KW_ENTRY("define" , 1), // 3 |
| 525 | LEX_KW_ENTRY("else" , 0), // 4 |
| 526 | LEX_KW_ENTRY("for" , 1), // 5 |
| 527 | LEX_KW_ENTRY("halt" , 0), // 6 |
| 528 | LEX_KW_ENTRY("ibase" , 1), // 7 |
| 529 | LEX_KW_ENTRY("obase" , 1), // 8 |
| 530 | LEX_KW_ENTRY("if" , 1), // 9 |
| 531 | LEX_KW_ENTRY("last" , 0), // 10 |
| 532 | LEX_KW_ENTRY("length" , 1), // 11 |
| 533 | LEX_KW_ENTRY("limits" , 0), // 12 |
| 534 | LEX_KW_ENTRY("print" , 0), // 13 |
| 535 | LEX_KW_ENTRY("quit" , 1), // 14 |
| 536 | LEX_KW_ENTRY("read" , 0), // 15 |
| 537 | LEX_KW_ENTRY("return" , 1), // 16 |
| 538 | LEX_KW_ENTRY("scale" , 1), // 17 |
| 539 | LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 540 | LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 541 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 542 | #undef LEX_KW_ENTRY |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 543 | #define STRING_else (bc_lex_kws[4].name8) |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 544 | #define STRING_for (bc_lex_kws[5].name8) |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 545 | #define STRING_if (bc_lex_kws[9].name8) |
| 546 | #define STRING_while (bc_lex_kws[19].name8) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 547 | enum { |
| 548 | POSIX_KWORD_MASK = 0 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 549 | | (1 << 0) // 0 |
| 550 | | (1 << 1) // 1 |
| 551 | | (0 << 2) // 2 |
| 552 | | (1 << 3) // 3 |
| 553 | | (0 << 4) // 4 |
| 554 | | (1 << 5) // 5 |
| 555 | | (0 << 6) // 6 |
| 556 | | (1 << 7) // 7 |
| 557 | | (1 << 8) // 8 |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 558 | | (1 << 9) // 9 |
| 559 | | (0 << 10) // 10 |
| 560 | | (1 << 11) // 11 |
| 561 | | (0 << 12) // 12 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 562 | | (0 << 13) // 13 |
| 563 | | (1 << 14) // 14 |
| 564 | | (0 << 15) // 15 |
| 565 | | (1 << 16) // 16 |
| 566 | | (1 << 17) // 17 |
| 567 | | (1 << 18) // 18 |
| 568 | | (1 << 19) // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 569 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 570 | #define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 571 | |
| 572 | // This is a bit array that corresponds to token types. An entry is |
| 573 | // true if the token is valid in an expression, false otherwise. |
| 574 | // Used to figure out when expr parsing should stop *without error message* |
| 575 | // - 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] | 576 | // 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] | 577 | // by later processing. |
| 578 | enum { |
| 579 | #define EXBITS(a,b,c,d,e,f,g,h) \ |
| 580 | ((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] | 581 | BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz: |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 582 | + (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] | 583 | + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ * |
| 584 | + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^= |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 585 | + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ -- |
| 586 | + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; } |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 587 | + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase |
| 588 | + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read |
| 589 | + (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] | 590 | #undef EXBITS |
| 591 | }; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 592 | static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 593 | { |
| 594 | #if ULONG_MAX > 0xffffffff |
| 595 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 596 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 597 | #else |
| 598 | // 32-bit version |
| 599 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 600 | if (i >= 32) { |
| 601 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 602 | i &= 31; |
| 603 | } |
| 604 | return m & (1UL << i); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 605 | #endif |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | // This is an array of data for operators that correspond to |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 609 | // [XC_LEX_1st_op...] token types. |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 610 | static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 611 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 612 | OP(1, false), // neg |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 613 | 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] | 614 | OP(2, false), // pow |
| 615 | OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod |
| 616 | OP(4, true ), OP( 4, true ), // + - |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 617 | OP(1, false), // not |
| 618 | OP(7, true ), OP( 7, true ), // or and |
| 619 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 620 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 621 | OP(0, false), OP( 0, false ), // inc dec |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 622 | #undef OP |
| 623 | }; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 624 | #define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f) |
| 625 | #define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 626 | #endif // ENABLE_BC |
| 627 | |
| 628 | #if ENABLE_DC |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 629 | static const //BcLexType - should be this type |
| 630 | uint8_t |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 631 | dc_char_to_LEX[] ALIGN1 = { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 632 | // %&'( |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 633 | XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 634 | // )*+, |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 635 | XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 636 | // -./ |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 637 | XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 638 | // 0123456789 |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 639 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 640 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 641 | XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 642 | // :;<=>?@ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 643 | DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ, |
| 644 | XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 645 | // ABCDEFGH |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 646 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 647 | XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 648 | // IJKLMNOP |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 649 | DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP, |
| 650 | XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM, |
Denys Vlasenko | f11b5b9 | 2019-01-04 15:54:40 +0100 | [diff] [blame] | 651 | // QRSTUVWX |
| 652 | DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, |
| 653 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, |
| 654 | // YZ |
| 655 | XC_LEX_INVALID, DC_LEX_LENGTH, |
| 656 | // [\] |
| 657 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 658 | // ^_` |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 659 | XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 660 | // abcdefgh |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 661 | DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE, |
| 662 | DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 663 | // ijklmnop |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 664 | 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] | 665 | XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 666 | // qrstuvwx |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 667 | DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID, |
| 668 | XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 669 | // yz |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 670 | XC_LEX_INVALID, DC_LEX_STACK_LEVEL, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 671 | // {|}~ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 672 | 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] | 673 | }; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 674 | 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] | 675 | int8_t |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 676 | dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz: |
| 677 | XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY |
| 678 | XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS |
| 679 | XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_OP_MINUS |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 680 | XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT |
| 681 | DC_INST_INVALID, // DC_LEX_OP_ASSIGN |
| 682 | XC_INST_REL_GT, // DC_LEX_LPAREN |
| 683 | DC_INST_INVALID, // DC_LEX_SCOLON |
| 684 | DC_INST_INVALID, // DC_LEX_READ |
| 685 | XC_INST_IBASE, // DC_LEX_IBASE |
| 686 | XC_INST_SCALE, // DC_LEX_SCALE |
| 687 | XC_INST_OBASE, // DC_LEX_OBASE |
| 688 | XC_INST_LENGTH, // DC_LEX_LENGTH |
| 689 | XC_INST_PRINT, // DC_LEX_PRINT |
| 690 | DC_INST_QUIT, // DC_LEX_QUIT |
| 691 | XC_INST_SQRT, // DC_LEX_SQRT |
| 692 | XC_INST_REL_GE, // DC_LEX_LBRACE |
| 693 | XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 694 | DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD |
| 695 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE |
| 696 | DC_INST_EXECUTE, // DC_LEX_EXECUTE |
| 697 | DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK |
| 698 | DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE |
| 699 | DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP |
| 700 | DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM |
| 701 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE |
| 702 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD |
| 703 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH |
| 704 | XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT |
| 705 | XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 706 | // DC_INST_INVALID in this table either means that corresponding LEX |
| 707 | // is not possible for dc, or that it does not compile one-to-one |
| 708 | // to a single INST. |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 709 | }; |
| 710 | #endif // ENABLE_DC |
| 711 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 712 | typedef struct BcParse { |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 713 | smallint lex; // was BcLexType // first member is most used |
| 714 | smallint lex_last; // was BcLexType |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 715 | size_t lex_line; |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 716 | const char *lex_inbuf; |
| 717 | const char *lex_next_at; // last lex_next() was called at this string |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 718 | const char *lex_filename; |
| 719 | FILE *lex_input_fp; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 720 | BcVec lex_strnumbuf; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 721 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 722 | BcFunc *func; |
| 723 | size_t fidx; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 724 | IF_BC(size_t in_funcdef;) |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 725 | IF_BC(BcVec exits;) |
| 726 | IF_BC(BcVec conds;) |
| 727 | IF_BC(BcVec ops;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 728 | } BcParse; |
| 729 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 730 | typedef struct BcProgram { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 731 | size_t len; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 732 | size_t nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 733 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 734 | size_t scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 735 | size_t ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 736 | size_t ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 737 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 738 | BcVec results; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 739 | BcVec exestack; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 740 | |
| 741 | BcVec fns; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 742 | IF_BC(BcVec fn_map;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 743 | |
| 744 | BcVec vars; |
| 745 | BcVec var_map; |
| 746 | |
| 747 | BcVec arrs; |
| 748 | BcVec arr_map; |
| 749 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 750 | IF_DC(BcVec strs;) |
| 751 | IF_DC(BcVec consts;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 752 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 753 | BcNum zero; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 754 | IF_BC(BcNum one;) |
| 755 | IF_BC(BcNum last;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 756 | } BcProgram; |
| 757 | |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 758 | struct globals { |
| 759 | BcParse prs; // first member is most used |
| 760 | |
| 761 | // For error messages. Can be set to current parsed line, |
| 762 | // or [TODO] to current executing line (can be before last parsed one) |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 763 | size_t err_line; |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 764 | |
| 765 | BcVec input_buffer; |
| 766 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 767 | IF_FEATURE_BC_INTERACTIVE(smallint ttyin;) |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 768 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
| 769 | |
| 770 | BcProgram prog; |
| 771 | |
| 772 | BcVec files; |
| 773 | |
| 774 | char *env_args; |
| 775 | |
| 776 | #if ENABLE_FEATURE_EDITING |
| 777 | line_input_t *line_input_state; |
| 778 | #endif |
| 779 | } FIX_ALIASING; |
| 780 | #define G (*ptr_to_globals) |
| 781 | #define INIT_G() do { \ |
| 782 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 783 | } while (0) |
| 784 | #define FREE_G() do { \ |
| 785 | FREE_PTR_TO_GLOBALS(); \ |
| 786 | } while (0) |
| 787 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 788 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
| 789 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 790 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 791 | # define G_interrupt bb_got_signal |
| 792 | # define G_ttyin G.ttyin |
| 793 | #else |
| 794 | # define G_interrupt 0 |
| 795 | # define G_ttyin 0 |
| 796 | #endif |
| 797 | #if ENABLE_FEATURE_CLEAN_UP |
| 798 | # define G_exiting G.exiting |
| 799 | #else |
| 800 | # define G_exiting 0 |
| 801 | #endif |
| 802 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
| 803 | #define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b')) |
| 804 | |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 805 | #if ENABLE_BC |
| 806 | # define BC_PARSE_REL (1 << 0) |
| 807 | # define BC_PARSE_PRINT (1 << 1) |
| 808 | # define BC_PARSE_ARRAY (1 << 2) |
| 809 | # define BC_PARSE_NOCALL (1 << 3) |
| 810 | #endif |
| 811 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 812 | #define BC_PROG_MAIN 0 |
| 813 | #define BC_PROG_READ 1 |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 814 | #if ENABLE_DC |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 815 | #define BC_PROG_REQ_FUNCS 2 |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 816 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 817 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 818 | #define BC_FLAG_W (1 << 0) |
| 819 | #define BC_FLAG_V (1 << 1) |
| 820 | #define BC_FLAG_S (1 << 2) |
| 821 | #define BC_FLAG_Q (1 << 3) |
| 822 | #define BC_FLAG_L (1 << 4) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 823 | #define BC_FLAG_I ((1 << 5) * ENABLE_DC) |
| 824 | #define DC_FLAG_X ((1 << 6) * ENABLE_DC) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 825 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 826 | #define BC_MAX_OBASE ((unsigned) 999) |
| 827 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 828 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 829 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 830 | #define BC_MAX_NUM BC_MAX_STRING |
| 831 | // Unused apart from "limits" message. Just show a "biggish number" there. |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 832 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 833 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 834 | #define BC_MAX_EXP_STR "999999999" |
| 835 | #define BC_MAX_VARS_STR "999999999" |
| 836 | |
| 837 | #define BC_MAX_OBASE_STR "999" |
| 838 | |
| 839 | #if INT_MAX == 2147483647 |
| 840 | # define BC_MAX_DIM_STR "2147483647" |
| 841 | #elif INT_MAX == 9223372036854775807 |
| 842 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 843 | #else |
| 844 | # error Strange INT_MAX |
| 845 | #endif |
| 846 | |
Kang-Che Sung | f159352 | 2019-09-05 23:40:38 +0800 | [diff] [blame] | 847 | #if UINT_MAX == 4294967295U |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 848 | # define BC_MAX_SCALE_STR "4294967295" |
| 849 | # define BC_MAX_STRING_STR "4294967294" |
Kang-Che Sung | f159352 | 2019-09-05 23:40:38 +0800 | [diff] [blame] | 850 | #elif UINT_MAX == 18446744073709551615U |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 851 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 852 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 853 | #else |
| 854 | # error Strange UINT_MAX |
| 855 | #endif |
| 856 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 857 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 858 | // In configurations where errors abort instead of propagating error |
| 859 | // return code up the call chain, functions returning BC_STATUS |
| 860 | // actually don't return anything, they always succeed and return "void". |
| 861 | // A macro wrapper is provided, which makes this statement work: |
| 862 | // s = zbc_func(...) |
| 863 | // and makes it visible to the compiler that s is always zero, |
| 864 | // allowing compiler to optimize dead code after the statement. |
| 865 | // |
| 866 | // To make code more readable, each such function has a "z" |
| 867 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. |
| 868 | // |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 869 | #if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 870 | # define ERRORS_ARE_FATAL 0 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 871 | # define ERRORFUNC /*nothing*/ |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 872 | # define IF_ERROR_RETURN_POSSIBLE(a) a |
| 873 | # define BC_STATUS BcStatus |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 874 | # define RETURN_STATUS(v) return (v) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 875 | # define COMMA_SUCCESS /*nothing*/ |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 876 | #else |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 877 | # define ERRORS_ARE_FATAL 1 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 878 | # define ERRORFUNC NORETURN |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 879 | # define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/ |
| 880 | # define BC_STATUS void |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 881 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 882 | # define COMMA_SUCCESS ,BC_STATUS_SUCCESS |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 883 | #endif |
| 884 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 885 | // |
| 886 | // Utility routines |
| 887 | // |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 888 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 889 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 890 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 891 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 892 | static void fflush_and_check(void) |
| 893 | { |
| 894 | fflush_all(); |
| 895 | if (ferror(stdout) || ferror(stderr)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 896 | bb_simple_perror_msg_and_die("output error"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 897 | } |
| 898 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 899 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 900 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 901 | do { \ |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 902 | IF_FEATURE_BC_INTERACTIVE(G_ttyin = 0;) /* do not loop in main loop anymore */ \ |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 903 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 904 | return BC_STATUS_FAILURE; \ |
| 905 | } while (0) |
| 906 | #else |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 907 | static void quit(void) NORETURN; |
| 908 | static void quit(void) |
| 909 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 910 | if (ferror(stdin)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 911 | bb_simple_perror_msg_and_die("input error"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 912 | fflush_and_check(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 913 | dbg_exec("quit(): exiting with exitcode SUCCESS"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 914 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 915 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 916 | #define QUIT_OR_RETURN_TO_MAIN quit() |
| 917 | #endif |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 918 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 919 | static void bc_verror_msg(const char *fmt, va_list p) |
| 920 | { |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 921 | const char *sv = sv; // for compiler |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 922 | if (G.prs.lex_filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 923 | sv = applet_name; |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 924 | applet_name = xasprintf("%s: %s:%lu", applet_name, |
| 925 | G.prs.lex_filename, (unsigned long)G.err_line |
| 926 | ); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 927 | } |
| 928 | bb_verror_msg(fmt, p, NULL); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 929 | if (G.prs.lex_filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 930 | free((char*)applet_name); |
| 931 | applet_name = sv; |
| 932 | } |
| 933 | } |
| 934 | |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 935 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 936 | { |
| 937 | va_list p; |
| 938 | |
| 939 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 940 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 941 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 942 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 943 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
| 944 | IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE); |
| 945 | exit(1); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 946 | } |
| 947 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 948 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 949 | static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 950 | { |
| 951 | va_list p; |
| 952 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 953 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 954 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 955 | RETURN_STATUS(BC_STATUS_SUCCESS); // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 956 | |
| 957 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 958 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 959 | va_end(p); |
| 960 | |
| 961 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 962 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 963 | RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning |
| 964 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 965 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 966 | RETURN_STATUS(BC_STATUS_FAILURE); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 967 | exit(1); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 968 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 969 | #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] | 970 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 971 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 972 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 973 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 974 | // function must not have caller-cleaned parameters on stack. |
| 975 | // Unfortunately, vararg function API does exactly that on most arches. |
| 976 | // 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] | 977 | static ERRORFUNC int bc_error(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 978 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 979 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 980 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 981 | static ERRORFUNC int bc_error_at(const char *msg) |
| 982 | { |
| 983 | const char *err_at = G.prs.lex_next_at; |
| 984 | if (err_at) { |
| 985 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt( |
| 986 | "%s at '%.*s'", |
| 987 | msg, |
| 988 | (int)(strchrnul(err_at, '\n') - err_at), |
| 989 | err_at |
| 990 | ); |
| 991 | } |
| 992 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
| 993 | } |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 994 | static ERRORFUNC int bc_error_bad_character(char c) |
| 995 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 996 | if (!c) |
| 997 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 998 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 999 | } |
Denys Vlasenko | 7a4e554 | 2019-06-08 12:39:30 +0200 | [diff] [blame] | 1000 | #if ENABLE_BC |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 1001 | static ERRORFUNC int bc_error_bad_function_definition(void) |
| 1002 | { |
| 1003 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad function definition"); |
| 1004 | } |
Denys Vlasenko | 7a4e554 | 2019-06-08 12:39:30 +0200 | [diff] [blame] | 1005 | #endif |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1006 | static ERRORFUNC int bc_error_bad_expression(void) |
| 1007 | { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 1008 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad expression"); |
| 1009 | } |
| 1010 | static ERRORFUNC int bc_error_bad_assignment(void) |
| 1011 | { |
| 1012 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at( |
| 1013 | "bad assignment: left side must be variable or array element" |
| 1014 | ); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1015 | } |
| 1016 | static ERRORFUNC int bc_error_bad_token(void) |
| 1017 | { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 1018 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad token"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1019 | } |
| 1020 | static ERRORFUNC int bc_error_stack_has_too_few_elements(void) |
| 1021 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1022 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1023 | } |
| 1024 | static ERRORFUNC int bc_error_variable_is_wrong_type(void) |
| 1025 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1026 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1027 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1028 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1029 | static BC_STATUS zbc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1030 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1031 | RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg)); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1032 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1033 | #define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS) |
| 1034 | static BC_STATUS zbc_POSIX_does_not_allow(const char *msg) |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1035 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1036 | 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] | 1037 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1038 | #define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS) |
| 1039 | 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] | 1040 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1041 | 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] | 1042 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1043 | #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) |
| 1044 | 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] | 1045 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1046 | 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] | 1047 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1048 | #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] | 1049 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1050 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1051 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1052 | { |
| 1053 | size_t cap = v->cap * 2; |
| 1054 | while (cap < v->len + n) cap *= 2; |
| 1055 | v->v = xrealloc(v->v, v->size * cap); |
| 1056 | v->cap = cap; |
| 1057 | } |
| 1058 | |
| 1059 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1060 | { |
| 1061 | v->size = esize; |
| 1062 | v->cap = BC_VEC_START_CAP; |
| 1063 | v->len = 0; |
| 1064 | v->dtor = dtor; |
| 1065 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1066 | } |
| 1067 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1068 | static void bc_char_vec_init(BcVec *v) |
| 1069 | { |
| 1070 | bc_vec_init(v, sizeof(char), NULL); |
| 1071 | } |
| 1072 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1073 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1074 | { |
| 1075 | if (v->cap < req) { |
| 1076 | v->v = xrealloc(v->v, v->size * req); |
| 1077 | v->cap = req; |
| 1078 | } |
| 1079 | } |
| 1080 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1081 | static void bc_vec_pop(BcVec *v) |
| 1082 | { |
| 1083 | v->len--; |
| 1084 | if (v->dtor) |
| 1085 | v->dtor(v->v + (v->size * v->len)); |
| 1086 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1087 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1088 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1089 | { |
| 1090 | if (!v->dtor) |
| 1091 | v->len -= n; |
| 1092 | else { |
| 1093 | size_t len = v->len - n; |
| 1094 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1095 | } |
| 1096 | } |
| 1097 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1098 | static void bc_vec_pop_all(BcVec *v) |
| 1099 | { |
| 1100 | bc_vec_npop(v, v->len); |
| 1101 | } |
| 1102 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 1103 | static size_t bc_vec_npush(BcVec *v, size_t n, const void *data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1104 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1105 | size_t len = v->len; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 1106 | if (len + n > v->cap) bc_vec_grow(v, n); |
| 1107 | memmove(v->v + (v->size * len), data, v->size * n); |
| 1108 | v->len = len + n; |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1109 | return len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1110 | } |
| 1111 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 1112 | static size_t bc_vec_push(BcVec *v, const void *data) |
| 1113 | { |
| 1114 | return bc_vec_npush(v, 1, data); |
| 1115 | //size_t len = v->len; |
| 1116 | //if (len >= v->cap) bc_vec_grow(v, 1); |
| 1117 | //memmove(v->v + (v->size * len), data, v->size); |
| 1118 | //v->len = len + 1; |
| 1119 | //return len; |
| 1120 | } |
| 1121 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 1122 | // G.prog.results often needs "pop old operand, push result" idiom. |
| 1123 | // Can do this without a few extra ops |
| 1124 | static size_t bc_result_pop_and_push(const void *data) |
| 1125 | { |
| 1126 | BcVec *v = &G.prog.results; |
| 1127 | char *last; |
| 1128 | size_t len = v->len - 1; |
| 1129 | |
| 1130 | last = v->v + (v->size * len); |
| 1131 | if (v->dtor) |
| 1132 | v->dtor(last); |
| 1133 | memmove(last, data, v->size); |
| 1134 | return len; |
| 1135 | } |
| 1136 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1137 | static size_t bc_vec_pushByte(BcVec *v, char data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1138 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1139 | return bc_vec_push(v, &data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1140 | } |
| 1141 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1142 | static size_t bc_vec_pushZeroByte(BcVec *v) |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1143 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1144 | //return bc_vec_pushByte(v, '\0'); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1145 | // better: |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1146 | return bc_vec_push(v, &const_int_0); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1147 | } |
| 1148 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1149 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1150 | { |
| 1151 | if (idx == v->len) |
| 1152 | bc_vec_push(v, data); |
| 1153 | else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1154 | char *ptr; |
| 1155 | |
| 1156 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1157 | |
| 1158 | ptr = v->v + v->size * idx; |
| 1159 | |
| 1160 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1161 | memmove(ptr, data, v->size); |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1166 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1167 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1168 | bc_vec_expand(v, len + 1); |
| 1169 | memcpy(v->v, str, len); |
| 1170 | v->len = len; |
| 1171 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1172 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1173 | } |
| 1174 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1175 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1176 | { |
| 1177 | return v->v + v->size * idx; |
| 1178 | } |
| 1179 | |
| 1180 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1181 | { |
| 1182 | return v->v + v->size * (v->len - idx - 1); |
| 1183 | } |
| 1184 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1185 | static void *bc_vec_top(const BcVec *v) |
| 1186 | { |
| 1187 | return v->v + v->size * (v->len - 1); |
| 1188 | } |
| 1189 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1190 | static FAST_FUNC void bc_vec_free(void *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1191 | { |
| 1192 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1193 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1194 | free(v->v); |
| 1195 | } |
| 1196 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1197 | static BcFunc* xc_program_func(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1198 | { |
| 1199 | return bc_vec_item(&G.prog.fns, idx); |
| 1200 | } |
| 1201 | // BC_PROG_MAIN is zeroth element, so: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1202 | #define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v)) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1203 | |
| 1204 | #if ENABLE_BC |
| 1205 | static BcFunc* bc_program_current_func(void) |
| 1206 | { |
| 1207 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1208 | BcFunc *func = xc_program_func(ip->func); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1209 | return func; |
| 1210 | } |
| 1211 | #endif |
| 1212 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1213 | static char** xc_program_str(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1214 | { |
| 1215 | #if ENABLE_BC |
| 1216 | if (IS_BC) { |
| 1217 | BcFunc *func = bc_program_current_func(); |
| 1218 | return bc_vec_item(&func->strs, idx); |
| 1219 | } |
| 1220 | #endif |
| 1221 | IF_DC(return bc_vec_item(&G.prog.strs, idx);) |
| 1222 | } |
| 1223 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1224 | static char** xc_program_const(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1225 | { |
| 1226 | #if ENABLE_BC |
| 1227 | if (IS_BC) { |
| 1228 | BcFunc *func = bc_program_current_func(); |
| 1229 | return bc_vec_item(&func->consts, idx); |
| 1230 | } |
| 1231 | #endif |
| 1232 | IF_DC(return bc_vec_item(&G.prog.consts, idx);) |
| 1233 | } |
| 1234 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1235 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1236 | { |
| 1237 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1238 | } |
| 1239 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1240 | static FAST_FUNC void bc_id_free(void *id) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1241 | { |
| 1242 | free(((BcId *) id)->name); |
| 1243 | } |
| 1244 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1245 | 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] | 1246 | { |
| 1247 | size_t low = 0, high = v->len; |
| 1248 | |
| 1249 | while (low < high) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1250 | size_t mid = (low + high) / 2; |
| 1251 | BcId *id = bc_vec_item(v, mid); |
| 1252 | int result = bc_id_cmp(ptr, id); |
| 1253 | |
| 1254 | if (result == 0) |
| 1255 | return mid; |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 1256 | if (result < 0) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1257 | high = mid; |
| 1258 | else |
| 1259 | low = mid + 1; |
| 1260 | } |
| 1261 | |
| 1262 | return low; |
| 1263 | } |
| 1264 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1265 | 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] | 1266 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1267 | size_t n = *i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1268 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1269 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1270 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1271 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1272 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1273 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1274 | bc_vec_pushAt(v, ptr, n); |
| 1275 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1276 | } |
| 1277 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1278 | 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] | 1279 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1280 | size_t i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1281 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1282 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1283 | } |
| 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 | |
Denys Vlasenko | 0084137 | 2019-11-23 17:25:21 +0100 | [diff] [blame] | 1468 | if (a_int != 0) { |
| 1469 | if (neg) return - (ssize_t) a_int; |
| 1470 | return (ssize_t) a_int; |
| 1471 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1472 | |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1473 | a_max = (a->rdx > b->rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1474 | if (a_max) { |
| 1475 | min = b->rdx; |
| 1476 | diff = a->rdx - b->rdx; |
| 1477 | max_num = a->num + diff; |
| 1478 | min_num = b->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1479 | // neg = (a_max == neg); - NOP (maps 1->1 and 0->0) |
| 1480 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1481 | min = a->rdx; |
| 1482 | diff = b->rdx - a->rdx; |
| 1483 | max_num = b->num + diff; |
| 1484 | min_num = a->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1485 | neg = !neg; // same as "neg = (a_max == neg)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1489 | if (cmp != 0) return BC_NUM_NEG(cmp, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1490 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1491 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1492 | if (max_num[i]) return BC_NUM_NEG(1, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
| 1498 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1499 | { |
| 1500 | if (places == 0) return; |
| 1501 | |
| 1502 | n->rdx -= places; |
| 1503 | |
| 1504 | if (n->len != 0) { |
| 1505 | n->len -= places; |
| 1506 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | static void bc_num_extend(BcNum *n, size_t places) |
| 1511 | { |
| 1512 | size_t len = n->len + places; |
| 1513 | |
| 1514 | if (places != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1515 | if (n->cap < len) bc_num_expand(n, len); |
| 1516 | |
| 1517 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1518 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1519 | |
| 1520 | n->len += places; |
| 1521 | n->rdx += places; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | static void bc_num_clean(BcNum *n) |
| 1526 | { |
| 1527 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1528 | if (n->len == 0) |
| 1529 | n->neg = false; |
| 1530 | else if (n->len < n->rdx) |
| 1531 | n->len = n->rdx; |
| 1532 | } |
| 1533 | |
| 1534 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1535 | { |
| 1536 | if (n->rdx < scale) |
| 1537 | bc_num_extend(n, scale - n->rdx); |
| 1538 | else |
| 1539 | bc_num_truncate(n, n->rdx - scale); |
| 1540 | |
| 1541 | bc_num_clean(n); |
| 1542 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1543 | } |
| 1544 | |
| 1545 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1546 | BcNum *restrict b) |
| 1547 | { |
| 1548 | if (idx < n->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1549 | b->len = n->len - idx; |
| 1550 | a->len = idx; |
| 1551 | a->rdx = b->rdx = 0; |
| 1552 | |
| 1553 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1554 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1555 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1556 | bc_num_zero(b); |
| 1557 | bc_num_copy(a, n); |
| 1558 | } |
| 1559 | |
| 1560 | bc_num_clean(a); |
| 1561 | bc_num_clean(b); |
| 1562 | } |
| 1563 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1564 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1565 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1566 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1567 | |
| 1568 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1569 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1570 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1571 | 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] | 1572 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1573 | |
| 1574 | if (n->rdx >= places) |
| 1575 | n->rdx -= places; |
| 1576 | else { |
| 1577 | bc_num_extend(n, places - n->rdx); |
| 1578 | n->rdx = 0; |
| 1579 | } |
| 1580 | |
| 1581 | bc_num_clean(n); |
| 1582 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1583 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1584 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1585 | #define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1586 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1587 | typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
| 1588 | |
| 1589 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 1590 | BcNumBinaryOp op, size_t req) |
| 1591 | { |
| 1592 | BcStatus s; |
| 1593 | BcNum num2, *ptr_a, *ptr_b; |
| 1594 | bool init = false; |
| 1595 | |
| 1596 | if (c == a) { |
| 1597 | ptr_a = &num2; |
| 1598 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 1599 | init = true; |
| 1600 | } else |
| 1601 | ptr_a = a; |
| 1602 | |
| 1603 | if (c == b) { |
| 1604 | ptr_b = &num2; |
| 1605 | if (c != a) { |
| 1606 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 1607 | init = true; |
| 1608 | } |
| 1609 | } else |
| 1610 | ptr_b = b; |
| 1611 | |
| 1612 | if (init) |
| 1613 | bc_num_init(c, req); |
| 1614 | else |
| 1615 | bc_num_expand(c, req); |
| 1616 | |
| 1617 | s = BC_STATUS_SUCCESS; |
| 1618 | IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale); |
| 1619 | |
| 1620 | if (init) bc_num_free(&num2); |
| 1621 | |
| 1622 | RETURN_STATUS(s); |
| 1623 | } |
| 1624 | #define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS) |
| 1625 | |
| 1626 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1627 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1628 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1629 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1630 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1631 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1632 | |
| 1633 | static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1634 | { |
| 1635 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s; |
| 1636 | (void) scale; |
| 1637 | RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b))); |
| 1638 | } |
| 1639 | |
| 1640 | static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1641 | { |
| 1642 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a; |
| 1643 | (void) scale; |
| 1644 | RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b))); |
| 1645 | } |
| 1646 | |
| 1647 | static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1648 | { |
| 1649 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1650 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req)); |
| 1651 | } |
| 1652 | |
| 1653 | static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1654 | { |
| 1655 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1656 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req)); |
| 1657 | } |
| 1658 | |
| 1659 | static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1660 | { |
| 1661 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1662 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req)); |
| 1663 | } |
| 1664 | |
| 1665 | static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1666 | { |
| 1667 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); |
| 1668 | } |
| 1669 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1670 | static const BcNumBinaryOp zxc_program_ops[] = { |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1671 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, |
| 1672 | }; |
| 1673 | #define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS) |
| 1674 | #define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS) |
| 1675 | #define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS) |
| 1676 | #define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS) |
| 1677 | #define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS) |
| 1678 | #define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS) |
| 1679 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1680 | 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] | 1681 | { |
| 1682 | BcNum one; |
| 1683 | BcDig num[2]; |
| 1684 | |
| 1685 | one.cap = 2; |
| 1686 | one.num = num; |
| 1687 | bc_num_one(&one); |
| 1688 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1689 | RETURN_STATUS(zbc_num_div(&one, a, b, scale)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1690 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1691 | #define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1692 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1693 | 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] | 1694 | { |
| 1695 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1696 | 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] | 1697 | unsigned carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1698 | |
| 1699 | // Because this function doesn't need to use scale (per the bc spec), |
| 1700 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1701 | |
| 1702 | if (a->len == 0) { |
| 1703 | bc_num_copy(c, b); |
| 1704 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1705 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1706 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1707 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1708 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1709 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | c->neg = a->neg; |
| 1713 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1714 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1715 | c->len = 0; |
| 1716 | |
| 1717 | if (a->rdx > b->rdx) { |
| 1718 | diff = a->rdx - b->rdx; |
| 1719 | ptr = a->num; |
| 1720 | ptr_a = a->num + diff; |
| 1721 | ptr_b = b->num; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1722 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1723 | diff = b->rdx - a->rdx; |
| 1724 | ptr = b->num; |
| 1725 | ptr_a = a->num; |
| 1726 | ptr_b = b->num + diff; |
| 1727 | } |
| 1728 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1729 | ptr_c = c->num; |
| 1730 | for (i = 0; i < diff; ++i, ++c->len) |
| 1731 | ptr_c[i] = ptr[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1732 | |
| 1733 | ptr_c += diff; |
| 1734 | a_int = BC_NUM_INT(a); |
| 1735 | b_int = BC_NUM_INT(b); |
| 1736 | |
| 1737 | if (a_int > b_int) { |
| 1738 | min_int = b_int; |
| 1739 | max = a_int; |
| 1740 | ptr = ptr_a; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1741 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1742 | min_int = a_int; |
| 1743 | max = b_int; |
| 1744 | ptr = ptr_b; |
| 1745 | } |
| 1746 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1747 | carry = 0; |
| 1748 | for (i = 0; i < min_rdx + min_int; ++i) { |
| 1749 | unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1750 | carry = in / 10; |
| 1751 | ptr_c[i] = (BcDig)(in % 10); |
| 1752 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1753 | for (; i < max + min_rdx; ++i) { |
| 1754 | unsigned in = (unsigned)ptr[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1755 | carry = in / 10; |
| 1756 | ptr_c[i] = (BcDig)(in % 10); |
| 1757 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1758 | c->len += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1759 | |
| 1760 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1761 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1762 | 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] | 1763 | } |
| 1764 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1765 | 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] | 1766 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1767 | ssize_t cmp; |
| 1768 | BcNum *minuend, *subtrahend; |
| 1769 | size_t start; |
| 1770 | bool aneg, bneg, neg; |
| 1771 | |
| 1772 | // Because this function doesn't need to use scale (per the bc spec), |
| 1773 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1774 | |
| 1775 | if (a->len == 0) { |
| 1776 | bc_num_copy(c, b); |
| 1777 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1778 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1779 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1780 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1781 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1782 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | aneg = a->neg; |
| 1786 | bneg = b->neg; |
| 1787 | a->neg = b->neg = false; |
| 1788 | |
| 1789 | cmp = bc_num_cmp(a, b); |
| 1790 | |
| 1791 | a->neg = aneg; |
| 1792 | b->neg = bneg; |
| 1793 | |
| 1794 | if (cmp == 0) { |
| 1795 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1796 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1797 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1798 | if (cmp > 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1799 | neg = a->neg; |
| 1800 | minuend = a; |
| 1801 | subtrahend = b; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1802 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1803 | neg = b->neg; |
| 1804 | if (sub) neg = !neg; |
| 1805 | minuend = b; |
| 1806 | subtrahend = a; |
| 1807 | } |
| 1808 | |
| 1809 | bc_num_copy(c, minuend); |
| 1810 | c->neg = neg; |
| 1811 | |
| 1812 | if (c->rdx < subtrahend->rdx) { |
| 1813 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1814 | start = 0; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1815 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1816 | start = c->rdx - subtrahend->rdx; |
| 1817 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1818 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1819 | |
| 1820 | bc_num_clean(c); |
| 1821 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1822 | 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] | 1823 | } |
| 1824 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1825 | 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] | 1826 | BcNum *restrict c) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1827 | #define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1828 | { |
| 1829 | BcStatus s; |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 1830 | size_t max, max2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1831 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1832 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1833 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1834 | if (a->len == 0 || b->len == 0) { |
| 1835 | bc_num_zero(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1836 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1837 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1838 | aone = BC_NUM_ONE(a); |
| 1839 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1840 | bc_num_copy(c, aone ? b : a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1841 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1842 | } |
| 1843 | |
Denys Vlasenko | cc4303f | 2021-01-06 14:00:53 +0100 | [diff] [blame] | 1844 | if (a->len < BC_NUM_KARATSUBA_LEN |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1845 | || b->len < BC_NUM_KARATSUBA_LEN |
Denys Vlasenko | cc4303f | 2021-01-06 14:00:53 +0100 | [diff] [blame] | 1846 | /* || a->len + b->len < BC_NUM_KARATSUBA_LEN - redundant check */ |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1847 | ) { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1848 | size_t i, j, len; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1849 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1850 | bc_num_expand(c, a->len + b->len + 1); |
| 1851 | |
| 1852 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1853 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1854 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1855 | for (i = 0; i < b->len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1856 | unsigned carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1857 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1858 | unsigned in = c->num[i + j]; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1859 | in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1860 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1861 | carry = in / 10; |
| 1862 | c->num[i + j] = (BcDig)(in % 10); |
| 1863 | } |
| 1864 | |
| 1865 | c->num[i + j] += (BcDig) carry; |
| 1866 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1867 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 1868 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1869 | // a=2^1000000 |
| 1870 | // a*a <- without check below, this will not be interruptible |
| 1871 | if (G_interrupt) return BC_STATUS_FAILURE; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1872 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | c->len = len; |
| 1876 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1877 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1878 | } |
| 1879 | |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 1880 | max = BC_MAX(a->len, b->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1881 | bc_num_init(&l1, max); |
| 1882 | bc_num_init(&h1, max); |
| 1883 | bc_num_init(&l2, max); |
| 1884 | bc_num_init(&h2, max); |
| 1885 | bc_num_init(&m1, max); |
| 1886 | bc_num_init(&m2, max); |
| 1887 | bc_num_init(&z0, max); |
| 1888 | bc_num_init(&z1, max); |
| 1889 | bc_num_init(&z2, max); |
| 1890 | bc_num_init(&temp, max + max); |
| 1891 | |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 1892 | max2 = (max + 1) / 2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1893 | bc_num_split(a, max2, &l1, &h1); |
| 1894 | bc_num_split(b, max2, &l2, &h2); |
| 1895 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1896 | s = zbc_num_add(&h1, &l1, &m1, 0); |
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_add(&h2, &l2, &m2, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1899 | if (s) goto err; |
| 1900 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1901 | s = zbc_num_k(&h1, &h2, &z0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1902 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1903 | s = zbc_num_k(&m1, &m2, &z1); |
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_k(&l1, &l2, &z2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1906 | if (s) goto err; |
| 1907 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1908 | s = zbc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1909 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1910 | s = zbc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1911 | if (s) goto err; |
| 1912 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1913 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1914 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1915 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1916 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1917 | s = zbc_num_add(&z0, &z1, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1918 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1919 | s = zbc_num_add(&temp, &z2, c, 0); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1920 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1921 | bc_num_free(&temp); |
| 1922 | bc_num_free(&z2); |
| 1923 | bc_num_free(&z1); |
| 1924 | bc_num_free(&z0); |
| 1925 | bc_num_free(&m2); |
| 1926 | bc_num_free(&m1); |
| 1927 | bc_num_free(&h2); |
| 1928 | bc_num_free(&l2); |
| 1929 | bc_num_free(&h1); |
| 1930 | bc_num_free(&l1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1931 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1932 | } |
| 1933 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1934 | 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] | 1935 | { |
| 1936 | BcStatus s; |
| 1937 | BcNum cpa, cpb; |
| 1938 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1939 | |
| 1940 | scale = BC_MAX(scale, a->rdx); |
| 1941 | scale = BC_MAX(scale, b->rdx); |
| 1942 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1943 | maxrdx = BC_MAX(maxrdx, scale); |
| 1944 | |
| 1945 | bc_num_init(&cpa, a->len); |
| 1946 | bc_num_init(&cpb, b->len); |
| 1947 | |
| 1948 | bc_num_copy(&cpa, a); |
| 1949 | bc_num_copy(&cpb, b); |
| 1950 | cpa.neg = cpb.neg = false; |
| 1951 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1952 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1953 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1954 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1955 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1956 | s = zbc_num_k(&cpa, &cpb, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1957 | if (s) goto err; |
| 1958 | |
| 1959 | maxrdx += scale; |
| 1960 | bc_num_expand(c, c->len + maxrdx); |
| 1961 | |
| 1962 | if (c->len < maxrdx) { |
| 1963 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 1964 | c->len += maxrdx; |
| 1965 | } |
| 1966 | |
| 1967 | c->rdx = maxrdx; |
| 1968 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1969 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1970 | bc_num_free(&cpb); |
| 1971 | bc_num_free(&cpa); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1972 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1973 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1974 | #define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1975 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1976 | 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] | 1977 | { |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 1978 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1979 | size_t len, end, i; |
| 1980 | BcNum cp; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1981 | |
| 1982 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1983 | RETURN_STATUS(bc_error("divide by zero")); |
| 1984 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1985 | bc_num_setToZero(c, scale); |
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 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1988 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1989 | bc_num_copy(c, a); |
| 1990 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1991 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 1995 | bc_num_copy(&cp, a); |
| 1996 | len = b->len; |
| 1997 | |
| 1998 | if (len > cp.len) { |
| 1999 | bc_num_expand(&cp, len + 2); |
| 2000 | bc_num_extend(&cp, len - cp.len); |
| 2001 | } |
| 2002 | |
| 2003 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 2004 | cp.rdx -= b->rdx; |
| 2005 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 2006 | |
| 2007 | if (b->rdx == b->len) { |
Denys Vlasenko | 71c82d1 | 2018-12-18 12:43:21 +0100 | [diff] [blame] | 2008 | for (;;) { |
| 2009 | if (len == 0) break; |
| 2010 | len--; |
| 2011 | if (b->num[len] != 0) |
| 2012 | break; |
| 2013 | } |
| 2014 | len++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 2018 | |
| 2019 | // We want an extra zero in front to make things simpler. |
| 2020 | cp.num[cp.len++] = 0; |
| 2021 | end = cp.len - len; |
| 2022 | |
| 2023 | bc_num_expand(c, cp.len); |
| 2024 | |
| 2025 | bc_num_zero(c); |
| 2026 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2027 | c->rdx = cp.rdx; |
| 2028 | c->len = cp.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2029 | |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2030 | s = BC_STATUS_SUCCESS; |
| 2031 | for (i = end - 1; i < end; --i) { |
| 2032 | BcDig *n, q; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2033 | n = cp.num + i; |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2034 | for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q) |
| 2035 | bc_num_subArrays(n, b->num, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2036 | c->num[i] = q; |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2037 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2038 | // a=2^100000 |
| 2039 | // scale=40000 |
| 2040 | // 1/a <- without check below, this will not be interruptible |
| 2041 | if (G_interrupt) { |
| 2042 | s = BC_STATUS_FAILURE; |
| 2043 | break; |
| 2044 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2045 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2046 | } |
| 2047 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2048 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2049 | bc_num_free(&cp); |
| 2050 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2051 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2052 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2053 | #define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2054 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2055 | 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] | 2056 | BcNum *restrict d, size_t scale, size_t ts) |
| 2057 | { |
| 2058 | BcStatus s; |
| 2059 | BcNum temp; |
| 2060 | bool neg; |
| 2061 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2062 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2063 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2064 | |
| 2065 | if (a->len == 0) { |
| 2066 | bc_num_setToZero(d, ts); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2067 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2071 | s = zbc_num_d(a, b, c, scale); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2072 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2073 | |
| 2074 | if (scale != 0) scale = ts; |
| 2075 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2076 | s = zbc_num_m(c, b, &temp, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2077 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2078 | s = zbc_num_sub(a, &temp, d, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2079 | if (s) goto err; |
| 2080 | |
| 2081 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2082 | |
| 2083 | neg = d->neg; |
| 2084 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2085 | d->neg = neg; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2086 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2087 | bc_num_free(&temp); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2088 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2089 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2090 | #define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2091 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2092 | 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] | 2093 | { |
| 2094 | BcStatus s; |
| 2095 | BcNum c1; |
| 2096 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2097 | |
| 2098 | bc_num_init(&c1, len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2099 | s = zbc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2100 | bc_num_free(&c1); |
| 2101 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2102 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2103 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2104 | #define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2105 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2106 | 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] | 2107 | { |
| 2108 | BcStatus s = BC_STATUS_SUCCESS; |
| 2109 | BcNum copy; |
| 2110 | unsigned long pow; |
| 2111 | size_t i, powrdx, resrdx; |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2112 | bool neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2113 | |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2114 | // GNU bc does not allow 2^2.0 - we do |
| 2115 | for (i = 0; i < b->rdx; i++) |
| 2116 | if (b->num[i] != 0) |
| 2117 | RETURN_STATUS(bc_error("not an integer")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2118 | |
| 2119 | if (b->len == 0) { |
| 2120 | bc_num_one(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2121 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2122 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2123 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2124 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2125 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2126 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2127 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2128 | if (!b->neg) |
| 2129 | bc_num_copy(c, a); |
| 2130 | else |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2131 | s = zbc_num_inv(a, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2132 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | neg = b->neg; |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 2136 | s = zbc_num_ulong_abs(b, &pow); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2137 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 2ea8ddf | 2018-12-22 21:45:18 +0100 | [diff] [blame] | 2138 | // b is not used beyond this point |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2139 | |
| 2140 | bc_num_init(©, a->len); |
| 2141 | bc_num_copy(©, a); |
| 2142 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2143 | if (!neg) { |
| 2144 | if (a->rdx > scale) |
| 2145 | scale = a->rdx; |
| 2146 | if (a->rdx * pow < scale) |
| 2147 | scale = a->rdx * pow; |
| 2148 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2149 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2150 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2151 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2152 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2153 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2154 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2155 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2156 | //if (G_interrupt) { |
| 2157 | // s = BC_STATUS_FAILURE; |
| 2158 | // goto err; |
| 2159 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2160 | } |
| 2161 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2162 | bc_num_copy(c, ©); |
| 2163 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2164 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2165 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2166 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2167 | if (s) goto err; |
| 2168 | |
| 2169 | if (pow & 1) { |
| 2170 | resrdx += powrdx; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2171 | s = zbc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2172 | if (s) goto err; |
| 2173 | } |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2174 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2175 | //if (G_interrupt) { |
| 2176 | // s = BC_STATUS_FAILURE; |
| 2177 | // goto err; |
| 2178 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | if (neg) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2182 | s = zbc_num_inv(c, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2183 | if (s) goto err; |
| 2184 | } |
| 2185 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2186 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2187 | |
| 2188 | // We can't use bc_num_clean() here. |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2189 | for (i = 0; i < c->len; ++i) |
| 2190 | if (c->num[i] != 0) |
| 2191 | goto skip; |
| 2192 | bc_num_setToZero(c, scale); |
| 2193 | skip: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2194 | |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2195 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2196 | bc_num_free(©); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2197 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2198 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2199 | #define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2200 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2201 | 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] | 2202 | { |
| 2203 | BcStatus s; |
| 2204 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2205 | BcDig half_digs[1]; |
Denys Vlasenko | 40f9fe2 | 2020-12-29 16:54:37 +0100 | [diff] [blame] | 2206 | size_t pow, len, digs, digs1, resrdx, req, times; |
| 2207 | ssize_t cmp, cmp1, cmp2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2208 | |
| 2209 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2210 | bc_num_expand(b, req); |
| 2211 | |
| 2212 | if (a->len == 0) { |
| 2213 | bc_num_setToZero(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2214 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2215 | } |
| 2216 | if (a->neg) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2217 | RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2218 | } |
| 2219 | if (BC_NUM_ONE(a)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2220 | bc_num_one(b); |
| 2221 | bc_num_extend(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2222 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2226 | len = a->len + scale; |
| 2227 | |
| 2228 | bc_num_init(&num1, len); |
| 2229 | bc_num_init(&num2, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2230 | |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2231 | half.cap = ARRAY_SIZE(half_digs); |
| 2232 | half.num = half_digs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2233 | bc_num_one(&half); |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2234 | half_digs[0] = 5; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2235 | half.rdx = 1; |
| 2236 | |
| 2237 | bc_num_init(&f, len); |
| 2238 | bc_num_init(&fprime, len); |
| 2239 | |
| 2240 | x0 = &num1; |
| 2241 | x1 = &num2; |
| 2242 | |
| 2243 | bc_num_one(x0); |
| 2244 | pow = BC_NUM_INT(a); |
| 2245 | |
| 2246 | if (pow) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2247 | if (pow & 1) |
| 2248 | x0->num[0] = 2; |
| 2249 | else |
| 2250 | x0->num[0] = 6; |
| 2251 | |
| 2252 | pow -= 2 - (pow & 1); |
| 2253 | |
| 2254 | bc_num_extend(x0, pow); |
| 2255 | |
| 2256 | // Make sure to move the radix back. |
| 2257 | x0->rdx -= pow; |
| 2258 | } |
| 2259 | |
Denys Vlasenko | 40f9fe2 | 2020-12-29 16:54:37 +0100 | [diff] [blame] | 2260 | x0->rdx = digs = digs1 = times = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2261 | resrdx = scale + 2; |
Denys Vlasenko | 40f9fe2 | 2020-12-29 16:54:37 +0100 | [diff] [blame] | 2262 | len = x0->len + resrdx - 1; |
| 2263 | cmp = 1; |
| 2264 | cmp1 = cmp2 = SSIZE_MAX; |
| 2265 | do { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2266 | s = zbc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2267 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2268 | s = zbc_num_add(x0, &f, &fprime, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2269 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2270 | s = zbc_num_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2271 | if (s) goto err; |
| 2272 | |
| 2273 | cmp = bc_num_cmp(x1, x0); |
| 2274 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2275 | |
| 2276 | if (cmp == cmp2 && digs == digs1) |
| 2277 | times += 1; |
| 2278 | else |
| 2279 | times = 0; |
| 2280 | |
| 2281 | resrdx += times > 4; |
| 2282 | |
| 2283 | cmp2 = cmp1; |
| 2284 | cmp1 = cmp; |
| 2285 | digs1 = digs; |
| 2286 | |
| 2287 | temp = x0; |
| 2288 | x0 = x1; |
| 2289 | x1 = temp; |
Denys Vlasenko | 40f9fe2 | 2020-12-29 16:54:37 +0100 | [diff] [blame] | 2290 | } while (cmp != 0 || digs < len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2291 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2292 | bc_num_copy(b, x0); |
| 2293 | scale -= 1; |
Denys Vlasenko | 40f9fe2 | 2020-12-29 16:54:37 +0100 | [diff] [blame] | 2294 | if (b->rdx > scale) |
| 2295 | bc_num_truncate(b, b->rdx - scale); |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2296 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2297 | bc_num_free(&fprime); |
| 2298 | bc_num_free(&f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2299 | bc_num_free(&num2); |
| 2300 | bc_num_free(&num1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2301 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2302 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2303 | #define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2304 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2305 | 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] | 2306 | size_t scale) |
| 2307 | { |
| 2308 | BcStatus s; |
| 2309 | BcNum num2, *ptr_a; |
| 2310 | bool init = false; |
| 2311 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2312 | |
| 2313 | if (c == a) { |
| 2314 | memcpy(&num2, c, sizeof(BcNum)); |
| 2315 | ptr_a = &num2; |
| 2316 | bc_num_init(c, len); |
| 2317 | init = true; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2318 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2319 | ptr_a = a; |
| 2320 | bc_num_expand(c, len); |
| 2321 | } |
| 2322 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2323 | s = zbc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2324 | |
| 2325 | if (init) bc_num_free(&num2); |
| 2326 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2327 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2328 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2329 | #define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2330 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2331 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2332 | 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] | 2333 | { |
| 2334 | BcStatus s; |
| 2335 | BcNum base, exp, two, temp; |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2336 | BcDig two_digs[1]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2337 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2338 | if (c->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2339 | RETURN_STATUS(bc_error("divide by zero")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2340 | if (a->rdx || b->rdx || c->rdx) |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2341 | RETURN_STATUS(bc_error("not an integer")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2342 | if (b->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2343 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2344 | |
| 2345 | bc_num_expand(d, c->len); |
| 2346 | bc_num_init(&base, c->len); |
| 2347 | bc_num_init(&exp, b->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2348 | bc_num_init(&temp, b->len); |
| 2349 | |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2350 | two.cap = ARRAY_SIZE(two_digs); |
| 2351 | two.num = two_digs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2352 | bc_num_one(&two); |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2353 | two_digs[0] = 2; |
| 2354 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2355 | bc_num_one(d); |
| 2356 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2357 | s = zbc_num_rem(a, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2358 | if (s) goto err; |
| 2359 | bc_num_copy(&exp, b); |
| 2360 | |
| 2361 | while (exp.len != 0) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2362 | s = zbc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2363 | if (s) goto err; |
| 2364 | |
| 2365 | if (BC_NUM_ONE(&temp)) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2366 | s = zbc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2367 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2368 | s = zbc_num_rem(&temp, c, d, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2369 | if (s) goto err; |
| 2370 | } |
| 2371 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2372 | s = zbc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2373 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2374 | s = zbc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2375 | if (s) goto err; |
| 2376 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2377 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2378 | bc_num_free(&temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2379 | bc_num_free(&exp); |
| 2380 | bc_num_free(&base); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2381 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2382 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2383 | #define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2384 | #endif // ENABLE_DC |
| 2385 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2386 | static FAST_FUNC void bc_string_free(void *string) |
| 2387 | { |
| 2388 | free(*(char**)string); |
| 2389 | } |
| 2390 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2391 | static void bc_func_init(BcFunc *f) |
| 2392 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2393 | bc_char_vec_init(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2394 | IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);) |
| 2395 | IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2396 | IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);) |
| 2397 | IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2398 | IF_BC(f->nparams = 0;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2399 | } |
| 2400 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2401 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2402 | { |
| 2403 | BcFunc *f = (BcFunc *) func; |
| 2404 | bc_vec_free(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2405 | IF_BC(bc_vec_free(&f->labels);) |
| 2406 | IF_BC(bc_vec_free(&f->autos);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2407 | IF_BC(bc_vec_free(&f->strs);) |
| 2408 | IF_BC(bc_vec_free(&f->consts);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2409 | } |
| 2410 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2411 | static void bc_array_expand(BcVec *a, size_t len); |
| 2412 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2413 | static void bc_array_init(BcVec *a, bool nums) |
| 2414 | { |
| 2415 | if (nums) |
| 2416 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2417 | else |
| 2418 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2419 | bc_array_expand(a, 1); |
| 2420 | } |
| 2421 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2422 | static void bc_array_expand(BcVec *a, size_t len) |
| 2423 | { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2424 | if (a->dtor == bc_num_free |
| 2425 | // && a->size == sizeof(BcNum) - always true |
| 2426 | ) { |
| 2427 | BcNum n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2428 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2429 | bc_num_init_DEF_SIZE(&n); |
| 2430 | bc_vec_push(a, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2431 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2432 | } else { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2433 | BcVec v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2434 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2435 | bc_array_init(&v, true); |
| 2436 | bc_vec_push(a, &v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2441 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2442 | { |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2443 | BcNum *dnum, *snum; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2444 | size_t i; |
| 2445 | |
| 2446 | bc_vec_pop_all(d); |
| 2447 | bc_vec_expand(d, s->cap); |
| 2448 | d->len = s->len; |
| 2449 | |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2450 | dnum = (void*)d->v; |
| 2451 | snum = (void*)s->v; |
| 2452 | for (i = 0; i < s->len; i++, dnum++, snum++) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2453 | bc_num_init(dnum, snum->len); |
| 2454 | bc_num_copy(dnum, snum); |
| 2455 | } |
| 2456 | } |
| 2457 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2458 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2459 | static void dc_result_copy(BcResult *d, BcResult *src) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2460 | { |
| 2461 | d->t = src->t; |
| 2462 | |
| 2463 | switch (d->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2464 | case XC_RESULT_TEMP: |
| 2465 | case XC_RESULT_IBASE: |
| 2466 | case XC_RESULT_SCALE: |
| 2467 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2468 | bc_num_init(&d->d.n, src->d.n.len); |
| 2469 | bc_num_copy(&d->d.n, &src->d.n); |
| 2470 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2471 | case XC_RESULT_VAR: |
| 2472 | case XC_RESULT_ARRAY: |
| 2473 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2474 | d->d.id.name = xstrdup(src->d.id.name); |
| 2475 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2476 | case XC_RESULT_CONSTANT: |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2477 | case XC_RESULT_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2478 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2479 | break; |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 2480 | default: // placate compiler |
| 2481 | // BC_RESULT_VOID, BC_RESULT_LAST, BC_RESULT_ONE - do not happen |
| 2482 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | #endif // ENABLE_DC |
| 2486 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2487 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2488 | { |
| 2489 | BcResult *r = (BcResult *) result; |
| 2490 | |
| 2491 | switch (r->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2492 | case XC_RESULT_TEMP: |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 2493 | IF_BC(case BC_RESULT_VOID:) |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2494 | case XC_RESULT_IBASE: |
| 2495 | case XC_RESULT_SCALE: |
| 2496 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2497 | bc_num_free(&r->d.n); |
| 2498 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2499 | case XC_RESULT_VAR: |
| 2500 | case XC_RESULT_ARRAY: |
| 2501 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2502 | free(r->d.id.name); |
| 2503 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2504 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2505 | // Do nothing. |
| 2506 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2507 | } |
| 2508 | } |
| 2509 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2510 | static int bad_input_byte(char c) |
| 2511 | { |
| 2512 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 2513 | || c > 0x7e |
| 2514 | ) { |
| 2515 | bc_error_fmt("illegal character 0x%02x", c); |
| 2516 | return 1; |
| 2517 | } |
| 2518 | return 0; |
| 2519 | } |
| 2520 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2521 | static void xc_read_line(BcVec *vec, FILE *fp) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2522 | { |
| 2523 | again: |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2524 | bc_vec_pop_all(vec); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2525 | fflush_and_check(); |
| 2526 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2527 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2528 | if (G_interrupt) { // ^C was pressed |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2529 | if (fp != stdin) { |
| 2530 | // ^C while running a script (bc SCRIPT): die. |
| 2531 | // We do not return to interactive prompt: |
| 2532 | // user might be running us from a shell, |
| 2533 | // and SCRIPT might be intended to terminate |
| 2534 | // (e.g. contain a "halt" stmt). |
| 2535 | // ^C dropping user into a bc prompt instead of |
| 2536 | // the shell would be unexpected. |
| 2537 | xfunc_die(); |
| 2538 | } |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 2539 | // There was ^C while running calculations |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2540 | G_interrupt = 0; |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 2541 | // GNU bc says "interrupted execution." (to stdout, not stderr) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2542 | // GNU dc says "Interrupt!" |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 2543 | puts("\ninterrupted execution"); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | # if ENABLE_FEATURE_EDITING |
| 2547 | if (G_ttyin && fp == stdin) { |
| 2548 | int n, i; |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 2549 | if (!G.line_input_state) |
| 2550 | G.line_input_state = new_line_input_t(DO_HISTORY); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2551 | # define line_buf bb_common_bufsiz1 |
| 2552 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
| 2553 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 2554 | //GNU bc prints this on ^C: |
| 2555 | //if (n == 0) // ^C |
| 2556 | // puts("(interrupt) Exiting bc."); |
| 2557 | bc_vec_pushZeroByte(vec); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2558 | return; |
| 2559 | } |
| 2560 | i = 0; |
| 2561 | for (;;) { |
| 2562 | char c = line_buf[i++]; |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2563 | if (c == '\0') break; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2564 | if (bad_input_byte(c)) goto again; |
| 2565 | } |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2566 | bc_vec_string(vec, n, line_buf); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2567 | # undef line_buf |
| 2568 | } else |
| 2569 | # endif |
| 2570 | #endif |
| 2571 | { |
| 2572 | int c; |
| 2573 | bool bad_chars = 0; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2574 | |
| 2575 | do { |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2576 | get_char: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2577 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2578 | if (G_interrupt) { |
| 2579 | // ^C was pressed: ignore entire line, get another one |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2580 | goto again; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2581 | } |
| 2582 | #endif |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2583 | c = fgetc(fp); |
| 2584 | if (c == '\0') |
| 2585 | goto get_char; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2586 | if (c == EOF) { |
| 2587 | if (ferror(fp)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2588 | bb_simple_perror_msg_and_die("input error"); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2589 | // Note: EOF does not append '\n' |
| 2590 | break; |
| 2591 | } |
| 2592 | bad_chars |= bad_input_byte(c); |
| 2593 | bc_vec_pushByte(vec, (char)c); |
| 2594 | } while (c != '\n'); |
| 2595 | |
| 2596 | if (bad_chars) { |
| 2597 | // Bad chars on this line |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2598 | if (!G.prs.lex_filename) { // stdin |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2599 | // ignore entire line, get another one |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2600 | goto again; |
| 2601 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2602 | bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2603 | } |
| 2604 | bc_vec_pushZeroByte(vec); |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | // |
| 2609 | // Parsing routines |
| 2610 | // |
| 2611 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2612 | // "Input numbers may contain the characters 0-9 and A-Z. |
| 2613 | // (Note: They must be capitals. Lower case letters are variable names.) |
| 2614 | // Single digit numbers always have the value of the digit regardless of |
| 2615 | // the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes |
| 2616 | // all input digits greater or equal to ibase to the value of ibase-1. |
| 2617 | // This makes the number ZZZ always be the largest 3 digit number of the |
| 2618 | // input base." |
| 2619 | static bool xc_num_strValid(const char *val) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2620 | { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2621 | bool radix = false; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2622 | for (;;) { |
| 2623 | BcDig c = *val++; |
| 2624 | if (c == '\0') |
| 2625 | break; |
| 2626 | if (c == '.') { |
| 2627 | if (radix) return false; |
| 2628 | radix = true; |
| 2629 | continue; |
| 2630 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2631 | if ((c < '0' || c > '9') && (c < 'A' || c > 'Z')) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2632 | return false; |
| 2633 | } |
| 2634 | return true; |
| 2635 | } |
| 2636 | |
| 2637 | // Note: n is already "bc_num_zero()"ed, |
| 2638 | // leading zeroes in "val" are removed |
| 2639 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2640 | { |
| 2641 | size_t len, i; |
| 2642 | const char *ptr; |
| 2643 | |
| 2644 | len = strlen(val); |
| 2645 | if (len == 0) |
| 2646 | return; |
| 2647 | |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2648 | bc_num_expand(n, len + 1); // +1 for e.g. "A" converting into 10 |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2649 | |
| 2650 | ptr = strchr(val, '.'); |
| 2651 | |
| 2652 | n->rdx = 0; |
| 2653 | if (ptr != NULL) |
| 2654 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2655 | |
| 2656 | for (i = 0; val[i]; ++i) { |
| 2657 | if (val[i] != '0' && val[i] != '.') { |
| 2658 | // Not entirely zero value - convert it, and exit |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2659 | if (len == 1) { |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2660 | unsigned c = val[0] - '0'; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2661 | n->len = 1; |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2662 | if (c > 9) { // A-Z => 10-36 |
| 2663 | n->len = 2; |
| 2664 | c -= ('A' - '9' - 1); |
| 2665 | n->num[1] = c/10; |
| 2666 | c = c%10; |
| 2667 | } |
| 2668 | n->num[0] = c; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2669 | break; |
| 2670 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2671 | i = len - 1; |
| 2672 | for (;;) { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2673 | char c = val[i] - '0'; |
| 2674 | if (c > 9) // A-Z => 9 |
| 2675 | c = 9; |
| 2676 | n->num[n->len] = c; |
| 2677 | n->len++; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2678 | skip_dot: |
| 2679 | if (i == 0) break; |
| 2680 | if (val[--i] == '.') goto skip_dot; |
| 2681 | } |
| 2682 | break; |
| 2683 | } |
| 2684 | } |
| 2685 | // if for() exits without hitting if(), the value is entirely zero |
| 2686 | } |
| 2687 | |
| 2688 | // Note: n is already "bc_num_zero()"ed, |
| 2689 | // leading zeroes in "val" are removed |
| 2690 | static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t) |
| 2691 | { |
| 2692 | BcStatus s; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2693 | BcNum mult, result; |
| 2694 | BcNum temp; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2695 | BcNum base; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2696 | BcDig temp_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2697 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2698 | size_t digits; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2699 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2700 | bc_num_init_DEF_SIZE(&mult); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2701 | |
| 2702 | temp.cap = ARRAY_SIZE(temp_digs); |
| 2703 | temp.num = temp_digs; |
| 2704 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2705 | base.cap = ARRAY_SIZE(base_digs); |
| 2706 | base.num = base_digs; |
| 2707 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2708 | base_t--; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2709 | |
| 2710 | for (;;) { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2711 | unsigned v; |
Denys Vlasenko | 8797adc | 2018-12-31 19:50:06 +0100 | [diff] [blame] | 2712 | char c; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2713 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2714 | c = *val++; |
| 2715 | if (c == '\0') goto int_err; |
| 2716 | if (c == '.') break; |
| 2717 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2718 | v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10); |
| 2719 | if (v > base_t) v = base_t; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2720 | |
| 2721 | s = zbc_num_mul(n, &base, &mult, 0); |
| 2722 | if (s) goto int_err; |
| 2723 | bc_num_ulong2num(&temp, v); |
| 2724 | s = zbc_num_add(&mult, &temp, n, 0); |
| 2725 | if (s) goto int_err; |
| 2726 | } |
| 2727 | |
| 2728 | bc_num_init(&result, base.len); |
| 2729 | //bc_num_zero(&result); - already is |
| 2730 | bc_num_one(&mult); |
| 2731 | |
| 2732 | digits = 0; |
| 2733 | for (;;) { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2734 | unsigned v; |
Denys Vlasenko | 8797adc | 2018-12-31 19:50:06 +0100 | [diff] [blame] | 2735 | char c; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2736 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2737 | c = *val++; |
| 2738 | if (c == '\0') break; |
| 2739 | digits++; |
| 2740 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2741 | v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10); |
| 2742 | if (v > base_t) v = base_t; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2743 | |
| 2744 | s = zbc_num_mul(&result, &base, &result, 0); |
| 2745 | if (s) goto err; |
| 2746 | bc_num_ulong2num(&temp, v); |
| 2747 | s = zbc_num_add(&result, &temp, &result, 0); |
| 2748 | if (s) goto err; |
| 2749 | s = zbc_num_mul(&mult, &base, &mult, 0); |
| 2750 | if (s) goto err; |
| 2751 | } |
| 2752 | |
| 2753 | s = zbc_num_div(&result, &mult, &result, digits); |
| 2754 | if (s) goto err; |
| 2755 | s = zbc_num_add(n, &result, n, digits); |
| 2756 | if (s) goto err; |
| 2757 | |
| 2758 | if (n->len != 0) { |
| 2759 | if (n->rdx < digits) |
| 2760 | bc_num_extend(n, digits - n->rdx); |
| 2761 | } else |
| 2762 | bc_num_zero(n); |
| 2763 | err: |
| 2764 | bc_num_free(&result); |
| 2765 | int_err: |
| 2766 | bc_num_free(&mult); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2767 | } |
| 2768 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2769 | static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2770 | { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2771 | size_t i; |
| 2772 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2773 | if (!xc_num_strValid(val)) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2774 | RETURN_STATUS(bc_error("bad number string")); |
| 2775 | |
| 2776 | bc_num_zero(n); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2777 | while (*val == '0') |
| 2778 | val++; |
| 2779 | for (i = 0; ; ++i) { |
| 2780 | if (val[i] == '\0') |
| 2781 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2782 | if (val[i] != '.' && val[i] != '0') |
| 2783 | break; |
| 2784 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2785 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2786 | if (base_t == 10 || val[1] == '\0') |
| 2787 | // Decimal, or single-digit number |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2788 | bc_num_parseDecimal(n, val); |
| 2789 | else |
| 2790 | bc_num_parseBase(n, val, base_t); |
| 2791 | |
| 2792 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2793 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2794 | #define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2795 | |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2796 | // p->lex_inbuf points to the current string to be parsed. |
| 2797 | // if p->lex_inbuf points to '\0', it's either EOF or it points after |
| 2798 | // last processed line's terminating '\n' (and more reading needs to be done |
| 2799 | // to get next character). |
| 2800 | // |
| 2801 | // If you are in a situation where that is a possibility, call peek_inbuf(). |
| 2802 | // If necessary, it performs more reading and changes p->lex_inbuf, |
| 2803 | // then it returns *p->lex_inbuf (which will be '\0' only if it's EOF). |
| 2804 | // After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0', |
| 2805 | // it's ok to do p->lex_inbuf++ once without end-of-buffer checking. |
| 2806 | // |
| 2807 | // eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++": |
| 2808 | // it returns current char and advances the pointer (if not EOF). |
| 2809 | // After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid. |
| 2810 | // |
| 2811 | // In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf(): |
| 2812 | // unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY |
| 2813 | // on real EOF, not end-of-buffer. |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2814 | // |
| 2815 | // bc cases to test interactively: |
| 2816 | // 1 #comment\ - prints "1<newline>" at once (comment is not continued) |
| 2817 | // 1 #comment/* - prints "1<newline>" at once |
| 2818 | // 1 #comment" - prints "1<newline>" at once |
| 2819 | // 1\#comment - error at once (\ is not a line continuation) |
| 2820 | // 1 + /*"*/2 - prints "3<newline>" at once |
| 2821 | // 1 + /*#*/2 - prints "3<newline>" at once |
| 2822 | // "str\" - prints "str\" at once |
| 2823 | // "str#" - prints "str#" at once |
| 2824 | // "str/*" - prints "str/*" at once |
| 2825 | // "str#\ - waits for second line |
| 2826 | // end" - ...prints "str#\<newline>end" |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2827 | static char peek_inbuf(void) |
| 2828 | { |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 2829 | if (*G.prs.lex_inbuf == '\0' |
| 2830 | && G.prs.lex_input_fp |
| 2831 | ) { |
| 2832 | xc_read_line(&G.input_buffer, G.prs.lex_input_fp); |
| 2833 | G.prs.lex_inbuf = G.input_buffer.v; |
| 2834 | if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte) |
| 2835 | G.prs.lex_input_fp = NULL; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2836 | } |
| 2837 | return *G.prs.lex_inbuf; |
| 2838 | } |
| 2839 | static char eat_inbuf(void) |
| 2840 | { |
| 2841 | char c = peek_inbuf(); |
| 2842 | if (c) G.prs.lex_inbuf++; |
| 2843 | return c; |
| 2844 | } |
| 2845 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2846 | static void xc_lex_lineComment(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2847 | { |
| 2848 | BcParse *p = &G.prs; |
| 2849 | char c; |
| 2850 | |
| 2851 | // Try: echo -n '#foo' | bc |
| 2852 | p->lex = XC_LEX_WHITESPACE; |
| 2853 | |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 2854 | // Not peek_inbuf(): we depend on input being done in whole lines: |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2855 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2856 | while ((c = *p->lex_inbuf) != '\n' && c != '\0') |
| 2857 | p->lex_inbuf++; |
| 2858 | } |
| 2859 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2860 | static void xc_lex_whitespace(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2861 | { |
| 2862 | BcParse *p = &G.prs; |
| 2863 | |
| 2864 | p->lex = XC_LEX_WHITESPACE; |
| 2865 | for (;;) { |
| 2866 | // We depend here on input being done in whole lines: |
| 2867 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2868 | char c = *p->lex_inbuf; |
| 2869 | if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
| 2870 | break; |
| 2871 | if (!isspace(c)) |
| 2872 | break; |
| 2873 | p->lex_inbuf++; |
| 2874 | } |
| 2875 | } |
| 2876 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2877 | static BC_STATUS zxc_lex_number(char last) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2878 | { |
| 2879 | BcParse *p = &G.prs; |
| 2880 | bool pt; |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2881 | char last_valid_ch; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2882 | |
| 2883 | bc_vec_pop_all(&p->lex_strnumbuf); |
| 2884 | bc_vec_pushByte(&p->lex_strnumbuf, last); |
| 2885 | |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2886 | // bc: "Input numbers may contain the characters 0-9 and A-Z. |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2887 | // (Note: They must be capitals. Lower case letters are variable names.) |
| 2888 | // Single digit numbers always have the value of the digit regardless of |
| 2889 | // the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes |
| 2890 | // all input digits greater or equal to ibase to the value of ibase-1. |
| 2891 | // This makes the number ZZZ always be the largest 3 digit number of the |
| 2892 | // input base." |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2893 | // dc only allows A-F, the rules about single-char and multi-char are the same. |
| 2894 | last_valid_ch = (IS_BC ? 'Z' : 'F'); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2895 | pt = (last == '.'); |
| 2896 | p->lex = XC_LEX_NUMBER; |
| 2897 | for (;;) { |
| 2898 | // We depend here on input being done in whole lines: |
| 2899 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2900 | char c = *p->lex_inbuf; |
| 2901 | check_c: |
| 2902 | if (c == '\0') |
| 2903 | break; |
| 2904 | if (c == '\\' && p->lex_inbuf[1] == '\n') { |
| 2905 | p->lex_inbuf += 2; |
| 2906 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 2907 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2908 | c = peek_inbuf(); // force next line to be read |
| 2909 | goto check_c; |
| 2910 | } |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2911 | if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2912 | if (c != '.') break; |
| 2913 | // if '.' was already seen, stop on second one: |
| 2914 | if (pt) break; |
| 2915 | pt = true; |
| 2916 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2917 | // c is one of "0-9A-Z." |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2918 | last = c; |
| 2919 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 2920 | p->lex_inbuf++; |
| 2921 | } |
| 2922 | if (last == '.') // remove trailing '.' if any |
| 2923 | bc_vec_pop(&p->lex_strnumbuf); |
| 2924 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 2925 | |
| 2926 | G.err_line = G.prs.lex_line; |
| 2927 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2928 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2929 | #define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2930 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2931 | static void xc_lex_name(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2932 | { |
| 2933 | BcParse *p = &G.prs; |
| 2934 | size_t i; |
| 2935 | const char *buf; |
| 2936 | |
| 2937 | p->lex = XC_LEX_NAME; |
| 2938 | |
| 2939 | // Since names can't cross lines with \<newline>, |
| 2940 | // we depend on the fact that whole line is in the buffer |
| 2941 | i = 0; |
| 2942 | buf = p->lex_inbuf - 1; |
| 2943 | for (;;) { |
| 2944 | char c = buf[i]; |
| 2945 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 2946 | i++; |
| 2947 | } |
| 2948 | |
| 2949 | #if 0 // We do not protect against people with gigabyte-long names |
| 2950 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2951 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2952 | if (i > BC_MAX_STRING) |
| 2953 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2954 | } |
| 2955 | #endif |
| 2956 | bc_vec_string(&p->lex_strnumbuf, i, buf); |
| 2957 | |
| 2958 | // Increment the index. We minus 1 because it has already been incremented. |
| 2959 | p->lex_inbuf += i - 1; |
| 2960 | |
| 2961 | //return BC_STATUS_SUCCESS; |
| 2962 | } |
| 2963 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2964 | IF_BC(static BC_STATUS zbc_lex_token(void);) |
| 2965 | IF_DC(static BC_STATUS zdc_lex_token(void);) |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 2966 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
| 2967 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2968 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2969 | static BC_STATUS zxc_lex_next(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2970 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2971 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2972 | BcStatus s; |
| 2973 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 2974 | G.err_line = p->lex_line; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2975 | p->lex_last = p->lex; |
Denys Vlasenko | 2f7352b | 2018-12-26 18:59:42 +0100 | [diff] [blame] | 2976 | //why? |
| 2977 | // if (p->lex_last == XC_LEX_EOF) |
| 2978 | // RETURN_STATUS(bc_error("end of file")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2979 | |
| 2980 | // Loop until failure or we don't have whitespace. This |
| 2981 | // is so the parser doesn't get inundated with whitespace. |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 2982 | // Comments are also XC_LEX_WHITESPACE tokens and eaten here. |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2983 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2984 | do { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2985 | if (*p->lex_inbuf == '\0') { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2986 | p->lex = XC_LEX_EOF; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2987 | if (peek_inbuf() == '\0') |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2988 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2989 | } |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 2990 | p->lex_next_at = p->lex_inbuf; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 2991 | dbg_lex("next string to parse:'%.*s'", |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2992 | (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at), |
| 2993 | p->lex_next_at |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 2994 | ); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2995 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2996 | IF_BC(s = zbc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2997 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2998 | IF_DC(s = zdc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2999 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3000 | } while (!s && p->lex == XC_LEX_WHITESPACE); |
| 3001 | dbg_lex("p->lex from string:%d", p->lex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3002 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3003 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3004 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3005 | #define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3006 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3007 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3008 | static BC_STATUS zbc_lex_skip_if_at_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3009 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3010 | if (G.prs.lex == XC_LEX_NLINE) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3011 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3012 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 3013 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3014 | #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] | 3015 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3016 | static BC_STATUS zbc_lex_next_and_skip_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3017 | { |
| 3018 | BcStatus s; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3019 | s = zxc_lex_next(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3020 | if (s) RETURN_STATUS(s); |
| 3021 | // if(cond)<newline>stmt is accepted too (but not 2+ newlines) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3022 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3023 | RETURN_STATUS(s); |
| 3024 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3025 | #define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_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_identifier(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3028 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3029 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3030 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3031 | unsigned i; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3032 | const char *buf = p->lex_inbuf - 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3033 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3034 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 3035 | const char *keyword8 = bc_lex_kws[i].name8; |
| 3036 | unsigned j = 0; |
| 3037 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 3038 | j++; |
| 3039 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3040 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3041 | if (keyword8[j] != '\0') |
| 3042 | continue; |
| 3043 | match: |
| 3044 | // buf starts with keyword bc_lex_kws[i] |
Denys Vlasenko | 5acd14b | 2018-12-20 16:48:50 +0100 | [diff] [blame] | 3045 | if (isalnum(buf[j]) || buf[j]=='_') |
| 3046 | continue; // "ifz" does not match "if" keyword, "if." does |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3047 | p->lex = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 3048 | if (!keyword_is_POSIX(i)) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3049 | s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8); |
| 3050 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | // We minus 1 because the index has already been incremented. |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3054 | p->lex_inbuf += j - 1; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3055 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3056 | } |
| 3057 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3058 | xc_lex_name(); |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 3059 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3060 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3061 | if (p->lex_strnumbuf.len > 2) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3062 | // Prevent this: |
| 3063 | // >>> qwe=1 |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3064 | // bc: POSIX only allows one character names; this is bad: 'qwe=1 |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3065 | // ' |
| 3066 | unsigned len = strchrnul(buf, '\n') - buf; |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3067 | 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] | 3068 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3069 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3070 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3071 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3072 | #define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3073 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3074 | static BC_STATUS zbc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3075 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3076 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3077 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3078 | p->lex = XC_LEX_STR; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3079 | bc_vec_pop_all(&p->lex_strnumbuf); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3080 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3081 | char c = peek_inbuf(); // strings can cross lines |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3082 | if (c == '\0') { |
Denys Vlasenko | 8af1108 | 2018-12-26 21:01:41 +0100 | [diff] [blame] | 3083 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3084 | } |
| 3085 | if (c == '"') |
| 3086 | break; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3087 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3088 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3089 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3090 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3091 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 3092 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3093 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3094 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 3095 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3096 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3097 | G.err_line = p->lex_line; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3098 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3099 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3100 | #define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3101 | |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3102 | static void parse_lex_by_checking_eq_sign(unsigned with_and_without) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3103 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3104 | BcParse *p = &G.prs; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3105 | if (*p->lex_inbuf == '=') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3106 | // ^^^ not using peek_inbuf() since '==' etc can't be split across lines |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3107 | p->lex_inbuf++; |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3108 | with_and_without >>= 8; // store "with" value |
| 3109 | } // else store "without" value |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3110 | p->lex = (with_and_without & 0xff); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3111 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3112 | #define parse_lex_by_checking_eq_sign(with, without) \ |
| 3113 | parse_lex_by_checking_eq_sign(((with)<<8)|(without)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3114 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3115 | static BC_STATUS zbc_lex_comment(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3116 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3117 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3118 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3119 | p->lex = XC_LEX_WHITESPACE; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3120 | // here lex_inbuf is at '*' of opening comment delimiter |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3121 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3122 | char c; |
| 3123 | |
| 3124 | p->lex_inbuf++; |
| 3125 | c = peek_inbuf(); |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3126 | check_star: |
| 3127 | if (c == '*') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3128 | p->lex_inbuf++; |
Denys Vlasenko | 8af1108 | 2018-12-26 21:01:41 +0100 | [diff] [blame] | 3129 | c = *p->lex_inbuf; // no need to peek_inbuf() |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3130 | if (c == '/') |
| 3131 | break; |
| 3132 | goto check_star; |
| 3133 | } |
| 3134 | if (c == '\0') { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3135 | RETURN_STATUS(bc_error("unterminated comment")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3136 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3137 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3138 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3139 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3140 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3141 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3142 | p->lex_inbuf++; // skip trailing '/' |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3143 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3144 | G.err_line = p->lex_line; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3145 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3146 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3147 | #define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3148 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3149 | #undef zbc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3150 | static BC_STATUS zbc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3151 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3152 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3153 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3154 | char c = eat_inbuf(); |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3155 | char c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3156 | |
| 3157 | // This is the workhorse of the lexer. |
| 3158 | switch (c) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3159 | // case '\0': // probably never reached |
| 3160 | // p->lex_inbuf--; |
| 3161 | // p->lex = XC_LEX_EOF; |
| 3162 | // break; |
| 3163 | case '\n': |
| 3164 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3165 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3166 | p->lex = XC_LEX_NLINE; |
| 3167 | break; |
| 3168 | case '\t': |
| 3169 | case '\v': |
| 3170 | case '\f': |
| 3171 | case '\r': |
| 3172 | case ' ': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3173 | xc_lex_whitespace(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3174 | break; |
| 3175 | case '!': |
| 3176 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3177 | if (p->lex == BC_LEX_OP_BOOL_NOT) { |
| 3178 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3179 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3180 | } |
| 3181 | break; |
| 3182 | case '"': |
| 3183 | s = zbc_lex_string(); |
| 3184 | break; |
| 3185 | case '#': |
| 3186 | s = zbc_POSIX_does_not_allow("'#' script comments"); |
| 3187 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3188 | xc_lex_lineComment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3189 | break; |
| 3190 | case '%': |
| 3191 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS); |
| 3192 | break; |
| 3193 | case '&': |
| 3194 | c2 = *p->lex_inbuf; |
| 3195 | if (c2 == '&') { |
| 3196 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
| 3197 | if (s) RETURN_STATUS(s); |
| 3198 | p->lex_inbuf++; |
| 3199 | p->lex = BC_LEX_OP_BOOL_AND; |
| 3200 | } else { |
| 3201 | p->lex = XC_LEX_INVALID; |
| 3202 | s = bc_error_bad_character('&'); |
| 3203 | } |
| 3204 | break; |
| 3205 | case '(': |
| 3206 | case ')': |
| 3207 | p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3208 | break; |
| 3209 | case '*': |
| 3210 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY); |
| 3211 | break; |
| 3212 | case '+': |
| 3213 | c2 = *p->lex_inbuf; |
| 3214 | if (c2 == '+') { |
| 3215 | p->lex_inbuf++; |
| 3216 | p->lex = BC_LEX_OP_INC; |
| 3217 | } else |
| 3218 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS); |
| 3219 | break; |
| 3220 | case ',': |
| 3221 | p->lex = BC_LEX_COMMA; |
| 3222 | break; |
| 3223 | case '-': |
| 3224 | c2 = *p->lex_inbuf; |
| 3225 | if (c2 == '-') { |
| 3226 | p->lex_inbuf++; |
| 3227 | p->lex = BC_LEX_OP_DEC; |
| 3228 | } else |
| 3229 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS); |
| 3230 | break; |
| 3231 | case '.': |
| 3232 | if (isdigit(*p->lex_inbuf)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3233 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3234 | else { |
| 3235 | p->lex = BC_LEX_KEY_LAST; |
| 3236 | s = zbc_POSIX_does_not_allow("'.' as 'last'"); |
| 3237 | } |
| 3238 | break; |
| 3239 | case '/': |
| 3240 | c2 = *p->lex_inbuf; |
| 3241 | if (c2 == '*') |
| 3242 | s = zbc_lex_comment(); |
| 3243 | else |
| 3244 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE); |
| 3245 | break; |
| 3246 | case '0': |
| 3247 | case '1': |
| 3248 | case '2': |
| 3249 | case '3': |
| 3250 | case '4': |
| 3251 | case '5': |
| 3252 | case '6': |
| 3253 | case '7': |
| 3254 | case '8': |
| 3255 | case '9': |
| 3256 | case 'A': |
| 3257 | case 'B': |
| 3258 | case 'C': |
| 3259 | case 'D': |
| 3260 | case 'E': |
| 3261 | case 'F': |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3262 | case 'G': |
| 3263 | case 'H': |
| 3264 | case 'I': |
| 3265 | case 'J': |
| 3266 | case 'K': |
| 3267 | case 'L': |
| 3268 | case 'M': |
| 3269 | case 'N': |
| 3270 | case 'O': |
| 3271 | case 'P': |
| 3272 | case 'Q': |
| 3273 | case 'R': |
| 3274 | case 'S': |
| 3275 | case 'T': |
| 3276 | case 'U': |
| 3277 | case 'V': |
| 3278 | case 'W': |
| 3279 | case 'X': |
| 3280 | case 'Y': |
| 3281 | case 'Z': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3282 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3283 | break; |
| 3284 | case ';': |
| 3285 | p->lex = BC_LEX_SCOLON; |
| 3286 | break; |
| 3287 | case '<': |
| 3288 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT); |
| 3289 | break; |
| 3290 | case '=': |
| 3291 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3292 | break; |
| 3293 | case '>': |
| 3294 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT); |
| 3295 | break; |
| 3296 | case '[': |
| 3297 | case ']': |
| 3298 | p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3299 | break; |
| 3300 | case '\\': |
| 3301 | if (*p->lex_inbuf == '\n') { |
| 3302 | p->lex = XC_LEX_WHITESPACE; |
| 3303 | p->lex_inbuf++; |
| 3304 | } else |
| 3305 | s = bc_error_bad_character(c); |
| 3306 | break; |
| 3307 | case '^': |
| 3308 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER); |
| 3309 | break; |
| 3310 | case 'a': |
| 3311 | case 'b': |
| 3312 | case 'c': |
| 3313 | case 'd': |
| 3314 | case 'e': |
| 3315 | case 'f': |
| 3316 | case 'g': |
| 3317 | case 'h': |
| 3318 | case 'i': |
| 3319 | case 'j': |
| 3320 | case 'k': |
| 3321 | case 'l': |
| 3322 | case 'm': |
| 3323 | case 'n': |
| 3324 | case 'o': |
| 3325 | case 'p': |
| 3326 | case 'q': |
| 3327 | case 'r': |
| 3328 | case 's': |
| 3329 | case 't': |
| 3330 | case 'u': |
| 3331 | case 'v': |
| 3332 | case 'w': |
| 3333 | case 'x': |
| 3334 | case 'y': |
| 3335 | case 'z': |
| 3336 | s = zbc_lex_identifier(); |
| 3337 | break; |
| 3338 | case '{': |
| 3339 | case '}': |
| 3340 | p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3341 | break; |
| 3342 | case '|': |
| 3343 | c2 = *p->lex_inbuf; |
| 3344 | if (c2 == '|') { |
| 3345 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
| 3346 | if (s) RETURN_STATUS(s); |
| 3347 | p->lex_inbuf++; |
| 3348 | p->lex = BC_LEX_OP_BOOL_OR; |
| 3349 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3350 | p->lex = XC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3351 | s = bc_error_bad_character(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3352 | } |
| 3353 | break; |
| 3354 | default: |
| 3355 | p->lex = XC_LEX_INVALID; |
| 3356 | s = bc_error_bad_character(c); |
| 3357 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3358 | } |
| 3359 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3360 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3361 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3362 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3363 | #endif // ENABLE_BC |
| 3364 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3365 | #if ENABLE_DC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3366 | static BC_STATUS zdc_lex_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3367 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3368 | BcParse *p = &G.prs; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3369 | if (G_exreg && isspace(*p->lex_inbuf)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3370 | xc_lex_whitespace(); // eats whitespace (but not newline) |
| 3371 | p->lex_inbuf++; // xc_lex_name() expects this |
| 3372 | xc_lex_name(); |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3373 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3374 | bc_vec_pop_all(&p->lex_strnumbuf); |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3375 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3376 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 3377 | p->lex = XC_LEX_NAME; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3378 | } |
| 3379 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3380 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3381 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3382 | #define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3383 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3384 | static BC_STATUS zdc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3385 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3386 | BcParse *p = &G.prs; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3387 | size_t depth; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3388 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3389 | p->lex = XC_LEX_STR; |
| 3390 | bc_vec_pop_all(&p->lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3391 | |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3392 | depth = 1; |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3393 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3394 | char c = peek_inbuf(); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3395 | if (c == '\0') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3396 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3397 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3398 | if (c == '[') depth++; |
| 3399 | if (c == ']') |
| 3400 | if (--depth == 0) |
| 3401 | break; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3402 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3403 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3404 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3405 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3406 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 3407 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3408 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3409 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3410 | p->lex_inbuf++; // skip trailing ']' |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3411 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3412 | G.err_line = p->lex_line; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3413 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3414 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3415 | #define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3416 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3417 | #undef zdc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3418 | static BC_STATUS zdc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3419 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3420 | static const //BcLexType - should be this type, but narrower type saves size: |
| 3421 | uint8_t |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 3422 | dc_lex_regs[] ALIGN1 = { |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3423 | 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] | 3424 | XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON, |
| 3425 | 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] | 3426 | DC_LEX_STORE_PUSH, |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3427 | }; |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3428 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3429 | BcParse *p = &G.prs; |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3430 | BcStatus s; |
| 3431 | char c, c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3432 | size_t i; |
| 3433 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3434 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3435 | if (p->lex_last == dc_lex_regs[i]) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3436 | RETURN_STATUS(zdc_lex_register()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3437 | } |
| 3438 | |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3439 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3440 | c = eat_inbuf(); |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3441 | if (c >= '%' && c <= '~' |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3442 | && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3443 | ) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3444 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | // This is the workhorse of the lexer. |
| 3448 | switch (c) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3449 | // case '\0': // probably never reached |
| 3450 | // p->lex = XC_LEX_EOF; |
| 3451 | // break; |
| 3452 | case '\n': |
| 3453 | // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
| 3454 | // (and "case '\n':" is not just empty here) |
| 3455 | // only to allow interactive dc have a way to exit |
| 3456 | // "parse" stage of "parse,execute" loop |
| 3457 | // on <enter>, not on _next_ token (which would mean |
| 3458 | // commands are not executed on pressing <enter>). |
| 3459 | // IOW: typing "1p<enter>" should print "1" _at once_, |
| 3460 | // not after some more input. |
| 3461 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3462 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3463 | p->lex = XC_LEX_NLINE; |
| 3464 | break; |
| 3465 | case '\t': |
| 3466 | case '\v': |
| 3467 | case '\f': |
| 3468 | case '\r': |
| 3469 | case ' ': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3470 | xc_lex_whitespace(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3471 | break; |
| 3472 | case '!': |
| 3473 | c2 = *p->lex_inbuf; |
| 3474 | if (c2 == '=') |
| 3475 | p->lex = XC_LEX_OP_REL_NE; |
| 3476 | else if (c2 == '<') |
| 3477 | p->lex = XC_LEX_OP_REL_LE; |
| 3478 | else if (c2 == '>') |
| 3479 | p->lex = XC_LEX_OP_REL_GE; |
| 3480 | else |
| 3481 | RETURN_STATUS(bc_error_bad_character(c)); |
| 3482 | p->lex_inbuf++; |
| 3483 | break; |
| 3484 | case '#': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3485 | xc_lex_lineComment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3486 | break; |
| 3487 | case '.': |
| 3488 | if (isdigit(*p->lex_inbuf)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3489 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3490 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3491 | s = bc_error_bad_character(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3492 | break; |
| 3493 | case '0': |
| 3494 | case '1': |
| 3495 | case '2': |
| 3496 | case '3': |
| 3497 | case '4': |
| 3498 | case '5': |
| 3499 | case '6': |
| 3500 | case '7': |
| 3501 | case '8': |
| 3502 | case '9': |
| 3503 | case 'A': |
| 3504 | case 'B': |
| 3505 | case 'C': |
| 3506 | case 'D': |
| 3507 | case 'E': |
| 3508 | case 'F': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3509 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3510 | break; |
| 3511 | case '[': |
| 3512 | s = zdc_lex_string(); |
| 3513 | break; |
| 3514 | default: |
| 3515 | p->lex = XC_LEX_INVALID; |
| 3516 | s = bc_error_bad_character(c); |
| 3517 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3518 | } |
| 3519 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3520 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3521 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3522 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3523 | #endif // ENABLE_DC |
| 3524 | |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3525 | static void xc_parse_push(unsigned i) |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3526 | { |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3527 | BcVec *code = &G.prs.func->code; |
| 3528 | dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i); |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3529 | bc_vec_pushByte(code, (uint8_t)i); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3530 | } |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3531 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3532 | static void xc_parse_pushName(char *name) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3533 | { |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3534 | #if 1 |
| 3535 | BcVec *code = &G.prs.func->code; |
| 3536 | size_t pos = code->len; |
| 3537 | size_t len = strlen(name) + 1; |
| 3538 | |
| 3539 | bc_vec_expand(code, pos + len); |
| 3540 | strcpy(code->v + pos, name); |
| 3541 | code->len = pos + len; |
| 3542 | #else |
| 3543 | // Smaller code, but way slow: |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3544 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3545 | xc_parse_push(*name); |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3546 | } while (*name++); |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3547 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3548 | } |
| 3549 | |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3550 | // Indexes < 0xfc are encoded verbatim, else first byte is |
| 3551 | // 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes", |
| 3552 | // followed by that many bytes, lsb first. |
| 3553 | // (The above describes 32-bit case). |
| 3554 | #define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t)) |
| 3555 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 3556 | static void bc_vec_pushIndex(BcVec *v, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3557 | { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3558 | size_t mask; |
| 3559 | unsigned amt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3560 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 3561 | dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx); |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3562 | if (idx < SMALL_INDEX_LIMIT) { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 3563 | bc_vec_pushByte(v, idx); |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 3564 | return; |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3565 | } |
| 3566 | |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3567 | mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8); |
| 3568 | amt = sizeof(idx); |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3569 | for (;;) { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3570 | if (idx & mask) break; |
| 3571 | mask >>= 8; |
| 3572 | amt--; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3573 | } |
| 3574 | // amt is at least 1 here - "one byte of length data follows" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3575 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 3576 | bc_vec_pushByte(v, (SMALL_INDEX_LIMIT - 1) + amt); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3577 | |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 3578 | do { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 3579 | bc_vec_pushByte(v, (unsigned char)idx); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3580 | idx >>= 8; |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 3581 | } while (idx != 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3582 | } |
| 3583 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 3584 | static void xc_parse_pushIndex(size_t idx) |
| 3585 | { |
| 3586 | bc_vec_pushIndex(&G.prs.func->code, idx); |
| 3587 | } |
| 3588 | |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3589 | static void xc_parse_pushInst_and_Index(unsigned inst, size_t idx) |
| 3590 | { |
| 3591 | xc_parse_push(inst); |
| 3592 | xc_parse_pushIndex(idx); |
| 3593 | } |
| 3594 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3595 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3596 | static void bc_parse_pushJUMP(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3597 | { |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3598 | xc_parse_pushInst_and_Index(BC_INST_JUMP, idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3599 | } |
| 3600 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3601 | static void bc_parse_pushJUMP_ZERO(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3602 | { |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3603 | xc_parse_pushInst_and_Index(BC_INST_JUMP_ZERO, idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3604 | } |
| 3605 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3606 | static BC_STATUS zbc_parse_pushSTR(void) |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3607 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3608 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3609 | char *str = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3610 | |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3611 | xc_parse_pushInst_and_Index(XC_INST_STR, p->func->strs.len); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3612 | bc_vec_push(&p->func->strs, &str); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3613 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3614 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3615 | } |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 3616 | #define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3617 | #endif |
| 3618 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3619 | static void xc_parse_pushNUM(void) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3620 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3621 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3622 | char *num = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3623 | #if ENABLE_BC && ENABLE_DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3624 | 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] | 3625 | #elif ENABLE_BC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3626 | size_t idx = bc_vec_push(&p->func->consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3627 | #else // DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3628 | size_t idx = bc_vec_push(&G.prog.consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3629 | #endif |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3630 | xc_parse_pushInst_and_Index(XC_INST_NUM, idx); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3631 | } |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3632 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3633 | static BC_STATUS zxc_parse_text_init(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3634 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3635 | G.prs.func = xc_program_func(G.prs.fidx); |
| 3636 | G.prs.lex_inbuf = text; |
| 3637 | G.prs.lex = G.prs.lex_last = XC_LEX_INVALID; |
| 3638 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3639 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3640 | #define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3641 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3642 | // Called when parsing or execution detects a failure, |
| 3643 | // resets execution structures. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3644 | static void xc_program_reset(void) |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3645 | { |
| 3646 | BcFunc *f; |
| 3647 | BcInstPtr *ip; |
| 3648 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3649 | bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3650 | bc_vec_pop_all(&G.prog.results); |
| 3651 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3652 | f = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3653 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 3654 | ip->inst_idx = f->code.len; |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3655 | } |
| 3656 | |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3657 | // Called when parsing code detects a failure, |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3658 | // resets parsing structures. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3659 | static void xc_parse_reset(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3660 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3661 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3662 | if (p->fidx != BC_PROG_MAIN) { |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3663 | bc_func_free(p->func); |
| 3664 | bc_func_init(p->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3665 | |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 3666 | p->fidx = BC_PROG_MAIN; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3667 | p->func = xc_program_func_BC_PROG_MAIN(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3668 | } |
| 3669 | |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 3670 | p->lex_inbuf += strlen(p->lex_inbuf); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3671 | p->lex = XC_LEX_EOF; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3672 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3673 | IF_BC(bc_vec_pop_all(&p->exits);) |
| 3674 | IF_BC(bc_vec_pop_all(&p->conds);) |
| 3675 | IF_BC(bc_vec_pop_all(&p->ops);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3676 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3677 | xc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3678 | } |
| 3679 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3680 | static void xc_parse_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3681 | { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3682 | IF_BC(bc_vec_free(&G.prs.exits);) |
| 3683 | IF_BC(bc_vec_free(&G.prs.conds);) |
| 3684 | IF_BC(bc_vec_free(&G.prs.ops);) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3685 | bc_vec_free(&G.prs.lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3686 | } |
| 3687 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3688 | static void xc_parse_create(size_t fidx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3689 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3690 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3691 | memset(p, 0, sizeof(BcParse)); |
| 3692 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3693 | bc_char_vec_init(&p->lex_strnumbuf); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3694 | IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);) |
| 3695 | IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);) |
| 3696 | IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3697 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 3698 | p->fidx = fidx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3699 | p->func = xc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3700 | } |
| 3701 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3702 | static void xc_program_add_fn(void) |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3703 | { |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3704 | //size_t idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3705 | BcFunc f; |
| 3706 | bc_func_init(&f); |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3707 | //idx = |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3708 | bc_vec_push(&G.prog.fns, &f); |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3709 | //return idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | #if ENABLE_BC |
| 3713 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3714 | // Note: takes ownership of 'name' (must be malloced) |
| 3715 | static size_t bc_program_addFunc(char *name) |
| 3716 | { |
| 3717 | size_t idx; |
| 3718 | BcId entry, *entry_ptr; |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3719 | int inserted; |
| 3720 | |
| 3721 | entry.name = name; |
| 3722 | entry.idx = G.prog.fns.len; |
| 3723 | |
| 3724 | inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx); |
| 3725 | if (!inserted) free(name); |
| 3726 | |
| 3727 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
| 3728 | idx = entry_ptr->idx; |
| 3729 | |
| 3730 | if (!inserted) { |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3731 | // There is already a function with this name. |
| 3732 | // It'll be redefined now, clear old definition. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3733 | BcFunc *func = xc_program_func(entry_ptr->idx); |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3734 | bc_func_free(func); |
| 3735 | bc_func_init(func); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3736 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3737 | xc_program_add_fn(); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | return idx; |
| 3741 | } |
| 3742 | |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 3743 | #define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops)) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3744 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3745 | // position of the first operator in the lex enum and adding the position of the |
| 3746 | // first in the expr enum. Note: This only works for binary operators. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3747 | #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] | 3748 | |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 3749 | static BC_STATUS zbc_parse_expr(uint8_t flags); |
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 | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3768 | if (p->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 | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 3778 | char l, r = bc_operation_PREC(type - XC_LEX_1st_op); |
| 3779 | bool left = bc_operation_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 | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 3785 | l = bc_operation_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 | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3788 | xc_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 | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3806 | xc_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 | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3828 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->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 | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3831 | s = zxc_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 | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3835 | if (p->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 | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3840 | if (p->lex != BC_LEX_COMMA) { |
| 3841 | if (p->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 | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3845 | s = zxc_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 | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3850 | xc_parse_pushInst_and_Index(BC_INST_CALL, nparams); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3851 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3852 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3853 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3854 | #define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3855 | |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 3856 | // Note: takes ownership of 'name' (must be malloced) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3857 | static BC_STATUS zbc_parse_call(char *name, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3858 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3859 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3860 | BcStatus s; |
| 3861 | BcId entry, *entry_ptr; |
| 3862 | size_t idx; |
| 3863 | |
| 3864 | entry.name = name; |
| 3865 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3866 | s = zbc_parse_params(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3867 | if (s) goto err; |
| 3868 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3869 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3870 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3871 | goto err; |
| 3872 | } |
| 3873 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3874 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3875 | |
| 3876 | if (idx == BC_VEC_INVALID_IDX) { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3877 | // No such function exists, create an empty one |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 3878 | bc_program_addFunc(name); |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3879 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3880 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3881 | free(name); |
| 3882 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3883 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3884 | xc_parse_pushIndex(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3885 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3886 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3887 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3888 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3889 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3890 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3891 | #define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3892 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3893 | static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3894 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3895 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3896 | BcStatus s; |
| 3897 | char *name; |
| 3898 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3899 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3900 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3901 | if (s) goto err; |
| 3902 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3903 | if (p->lex == BC_LEX_LBRACKET) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3904 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3905 | if (s) goto err; |
| 3906 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3907 | if (p->lex == BC_LEX_RBRACKET) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3908 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3909 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3910 | goto err; |
| 3911 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3912 | *type = XC_INST_ARRAY; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3913 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3914 | *type = XC_INST_ARRAY_ELEM; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3915 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3916 | s = zbc_parse_expr(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3917 | if (s) goto err; |
| 3918 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3919 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3920 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3921 | xc_parse_push(*type); |
| 3922 | xc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3923 | free(name); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3924 | } else if (p->lex == BC_LEX_LPAREN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3925 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3926 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3927 | goto err; |
| 3928 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3929 | *type = BC_INST_CALL; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3930 | s = zbc_parse_call(name, flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3931 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3932 | *type = XC_INST_VAR; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3933 | xc_parse_push(XC_INST_VAR); |
| 3934 | xc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3935 | free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3936 | } |
| 3937 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3938 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3939 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3940 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3941 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3942 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3943 | #define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3944 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3945 | static BC_STATUS zbc_parse_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3946 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3947 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3948 | BcStatus s; |
| 3949 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3950 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3951 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3952 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3953 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3954 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3955 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3956 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3957 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3958 | xc_parse_push(XC_INST_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3959 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 3960 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3961 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3962 | #define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3963 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3964 | 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] | 3965 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3966 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3967 | BcStatus s; |
| 3968 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3969 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3970 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3971 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3972 | |
| 3973 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3974 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3975 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3976 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3977 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3978 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3979 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3980 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3981 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3982 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3983 | *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3984 | xc_parse_push(*prev); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3985 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 3986 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3987 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3988 | #define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3989 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3990 | static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3991 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3992 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3993 | BcStatus s; |
| 3994 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3995 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3996 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3997 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3998 | if (p->lex != BC_LEX_LPAREN) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3999 | *type = XC_INST_SCALE; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4000 | xc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4001 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4002 | } |
| 4003 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4004 | *type = XC_INST_SCALE_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4005 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 4006 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4007 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4008 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4009 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4010 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4011 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4012 | if (p->lex != BC_LEX_RPAREN) |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4013 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4014 | xc_parse_push(XC_INST_SCALE_FUNC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4015 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4016 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4017 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4018 | #define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4019 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4020 | static BC_STATUS zbc_parse_incdec(BcInst *prev, size_t *nexs, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4021 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4022 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4023 | BcStatus s; |
| 4024 | BcLexType type; |
| 4025 | char inst; |
| 4026 | BcInst etype = *prev; |
| 4027 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4028 | if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM |
| 4029 | || etype == XC_INST_SCALE || etype == BC_INST_LAST |
| 4030 | || etype == XC_INST_IBASE || etype == XC_INST_OBASE |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4031 | ) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4032 | *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4033 | xc_parse_push(inst); |
| 4034 | s = zxc_lex_next(); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4035 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4036 | *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4037 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4038 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4039 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4040 | type = p->lex; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4041 | |
| 4042 | // Because we parse the next part of the expression |
| 4043 | // right here, we need to increment this. |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4044 | *nexs = *nexs + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4045 | |
| 4046 | switch (type) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4047 | case XC_LEX_NAME: |
| 4048 | s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL); |
| 4049 | break; |
| 4050 | case BC_LEX_KEY_IBASE: |
| 4051 | case BC_LEX_KEY_LAST: |
| 4052 | case BC_LEX_KEY_OBASE: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4053 | xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
| 4054 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4055 | break; |
| 4056 | case BC_LEX_KEY_SCALE: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4057 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4058 | if (s) RETURN_STATUS(s); |
| 4059 | if (p->lex == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4060 | s = bc_error_bad_token(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4061 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4062 | xc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4063 | break; |
| 4064 | default: |
| 4065 | s = bc_error_bad_token(); |
| 4066 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4067 | } |
| 4068 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4069 | if (!s) xc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4070 | } |
| 4071 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4072 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4073 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4074 | #define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4075 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4076 | static int bc_parse_inst_isLeaf(BcInst p) |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4077 | { |
| 4078 | return (p >= XC_INST_NUM && p <= XC_INST_SQRT) |
| 4079 | || p == BC_INST_INC_POST |
| 4080 | || p == BC_INST_DEC_POST |
| 4081 | ; |
| 4082 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4083 | #define BC_PARSE_LEAF(prev, bin_last, rparen) \ |
| 4084 | (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev))) |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4085 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4086 | static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn, |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4087 | bool rparen, bool bin_last, size_t *nexprs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4088 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4089 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4090 | BcStatus s; |
| 4091 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4092 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4093 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4094 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4095 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4096 | type = BC_PARSE_LEAF(*prev, bin_last, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG; |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4097 | *prev = BC_TOKEN_2_INST(type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4098 | |
| 4099 | // We can just push onto the op stack because this is the largest |
| 4100 | // precedence operator that gets pushed. Inc/dec does not. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4101 | if (type != XC_LEX_OP_MINUS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4102 | bc_vec_push(&p->ops, &type); |
| 4103 | else |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4104 | bc_parse_operator(type, ops_bgn, nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4105 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4106 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4107 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4108 | #define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4109 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4110 | static BC_STATUS zbc_parse_print(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4111 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4112 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4113 | BcStatus s; |
| 4114 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4115 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4116 | for (;;) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4117 | s = zxc_lex_next(); |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4118 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4119 | type = p->lex; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4120 | if (type == XC_LEX_STR) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4121 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4122 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4123 | s = zbc_parse_expr(0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4124 | } |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4125 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4126 | xc_parse_push(XC_INST_PRINT_POP); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4127 | if (p->lex != BC_LEX_COMMA) |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4128 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4129 | } |
| 4130 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4131 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4132 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4133 | #define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4134 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4135 | static BC_STATUS zbc_parse_return(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4136 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4137 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4138 | BcStatus s; |
| 4139 | BcLexType t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4140 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4141 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4142 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4143 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4144 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4145 | t = p->lex; |
Denys Vlasenko | 96b5ec1 | 2019-01-03 23:34:36 +0100 | [diff] [blame] | 4146 | if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON || t == BC_LEX_RBRACE) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4147 | xc_parse_push(BC_INST_RET0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4148 | else { |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4149 | //TODO: if (p->func->voidfunc) ERROR |
Denys Vlasenko | 96b5ec1 | 2019-01-03 23:34:36 +0100 | [diff] [blame] | 4150 | s = zbc_parse_expr(0); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4151 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4152 | |
Denys Vlasenko | 96b5ec1 | 2019-01-03 23:34:36 +0100 | [diff] [blame] | 4153 | if (t != BC_LEX_LPAREN // "return EXPR", no () |
| 4154 | || p->lex_last != BC_LEX_RPAREN // example: "return (a) + b" |
| 4155 | ) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4156 | s = zbc_POSIX_requires("parentheses around return expressions"); |
| 4157 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4158 | } |
| 4159 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4160 | xc_parse_push(XC_INST_RET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4161 | } |
| 4162 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4163 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4164 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4165 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4166 | #define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4167 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4168 | static void rewrite_label_to_current(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4169 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4170 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4171 | size_t *label = bc_vec_item(&p->func->labels, idx); |
| 4172 | *label = p->func->code.len; |
| 4173 | } |
| 4174 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4175 | static BC_STATUS zbc_parse_if(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4176 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4177 | BcParse *p = &G.prs; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4178 | BcStatus s; |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4179 | size_t ip_idx; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4180 | |
| 4181 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4182 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4183 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4184 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4185 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4186 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4187 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4188 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4189 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4190 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4191 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4192 | // Encode "if zero, jump to ..." |
| 4193 | // Pushed value (destination of the jump) is uninitialized, |
| 4194 | // will be rewritten to be address of "end of if()" or of "else". |
| 4195 | ip_idx = bc_vec_push(&p->func->labels, &ip_idx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4196 | bc_parse_pushJUMP_ZERO(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4197 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4198 | s = zbc_parse_stmt_allow_NLINE_before(STRING_if); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4199 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4200 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4201 | dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex); |
| 4202 | if (p->lex == BC_LEX_KEY_ELSE) { |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4203 | size_t ip2_idx; |
Denys Vlasenko | 7415633 | 2018-12-16 21:21:27 +0100 | [diff] [blame] | 4204 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4205 | // Encode "after then_stmt, jump to end of if()" |
| 4206 | ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx); |
| 4207 | 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] | 4208 | bc_parse_pushJUMP(ip2_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4209 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4210 | 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] | 4211 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4212 | |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4213 | ip_idx = ip2_idx; |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4214 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4215 | s = zbc_parse_stmt_allow_NLINE_before(STRING_else); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4216 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4217 | } |
| 4218 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4219 | 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] | 4220 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4221 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4222 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
| 4223 | RETURN_STATUS(s); |
| 4224 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4225 | #define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4226 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4227 | static BC_STATUS zbc_parse_while(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4228 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4229 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4230 | BcStatus s; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4231 | size_t cond_idx; |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4232 | size_t ip_idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4233 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4234 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4235 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4236 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4237 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4238 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4239 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4240 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4241 | ip_idx = cond_idx + 1; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4242 | bc_vec_push(&p->conds, &cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4243 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4244 | bc_vec_push(&p->exits, &ip_idx); |
| 4245 | bc_vec_push(&p->func->labels, &ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4246 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4247 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4248 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4249 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4250 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4251 | bc_parse_pushJUMP_ZERO(ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4252 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4253 | s = zbc_parse_stmt_allow_NLINE_before(STRING_while); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4254 | if (s) RETURN_STATUS(s); |
| 4255 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4256 | 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] | 4257 | bc_parse_pushJUMP(cond_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4258 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4259 | 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] | 4260 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4261 | |
| 4262 | bc_vec_pop(&p->exits); |
| 4263 | bc_vec_pop(&p->conds); |
| 4264 | |
| 4265 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4266 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4267 | #define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4268 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4269 | static BC_STATUS zbc_parse_for(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4270 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4271 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4272 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4273 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4274 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4275 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4276 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4277 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4278 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4279 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4280 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4281 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4282 | if (p->lex != BC_LEX_SCOLON) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4283 | s = zbc_parse_expr(0); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4284 | xc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4285 | if (s) RETURN_STATUS(s); |
| 4286 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4287 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
| 4288 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4289 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4290 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4291 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4292 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4293 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4294 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4295 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4296 | update_idx = cond_idx + 1; |
| 4297 | body_idx = update_idx + 1; |
| 4298 | exit_idx = body_idx + 1; |
| 4299 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4300 | if (p->lex != BC_LEX_SCOLON) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4301 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4302 | else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4303 | // Set this for the next call to xc_parse_pushNUM(). |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4304 | // This is safe to set because the current token is a semicolon, |
| 4305 | // which has no string requirement. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4306 | bc_vec_string(&p->lex_strnumbuf, 1, "1"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4307 | xc_parse_pushNUM(); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4308 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4309 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4310 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4311 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4312 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4313 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4314 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4315 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4316 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4317 | bc_parse_pushJUMP_ZERO(exit_idx); |
| 4318 | bc_parse_pushJUMP(body_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4319 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4320 | bc_vec_push(&p->conds, &update_idx); |
| 4321 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4322 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4323 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4324 | s = zbc_parse_expr(0); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4325 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4326 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4327 | xc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4328 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4329 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
| 4330 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4331 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4332 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4333 | bc_parse_pushJUMP(cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4334 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4335 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4336 | bc_vec_push(&p->exits, &exit_idx); |
| 4337 | bc_vec_push(&p->func->labels, &exit_idx); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4338 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4339 | s = zbc_parse_stmt_allow_NLINE_before(STRING_for); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4340 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4341 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4342 | 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] | 4343 | bc_parse_pushJUMP(update_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4344 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4345 | 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] | 4346 | rewrite_label_to_current(exit_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4347 | |
| 4348 | bc_vec_pop(&p->exits); |
| 4349 | bc_vec_pop(&p->conds); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4350 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4351 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4352 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4353 | #define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4354 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4355 | static BC_STATUS zbc_parse_break_or_continue(BcLexType type) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4356 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4357 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4358 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4359 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4360 | if (type == BC_LEX_KEY_BREAK) { |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4361 | if (p->exits.len == 0) // none of the enclosing blocks is a loop |
| 4362 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4363 | i = *(size_t*)bc_vec_top(&p->exits); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4364 | } else { |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4365 | i = *(size_t*)bc_vec_top(&p->conds); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4366 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4367 | bc_parse_pushJUMP(i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4368 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4369 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4370 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4371 | #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] | 4372 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4373 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, BcType type) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4374 | { |
| 4375 | BcId *autoid; |
| 4376 | BcId a; |
| 4377 | size_t i; |
| 4378 | |
| 4379 | autoid = (void*)f->autos.v; |
| 4380 | for (i = 0; i < f->autos.len; i++, autoid++) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4381 | if (strcmp(name, autoid->name) == 0 |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4382 | && type == (BcType) autoid->idx |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4383 | ) { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 4384 | RETURN_STATUS(bc_error("duplicate function parameter or auto name")); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4385 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4386 | } |
| 4387 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4388 | a.idx = type; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4389 | a.name = name; |
| 4390 | |
| 4391 | bc_vec_push(&f->autos, &a); |
| 4392 | |
| 4393 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 4394 | } |
| 4395 | #define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS) |
| 4396 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4397 | static BC_STATUS zbc_parse_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4398 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4399 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4400 | BcStatus s; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4401 | bool comma, voidfunc; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4402 | char *name; |
| 4403 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4404 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4405 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4406 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4407 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4408 | RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4409 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4410 | // To be maximally both POSIX and GNU-compatible, |
| 4411 | // "void" is not treated as a normal keyword: |
| 4412 | // you can have variable named "void", and even a function |
| 4413 | // named "void": "define void() { return 6; }" is ok. |
| 4414 | // _Only_ "define void f() ..." syntax treats "void" |
| 4415 | // specially. |
| 4416 | voidfunc = (strcmp(p->lex_strnumbuf.v, "void") == 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4417 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4418 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4419 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4420 | |
| 4421 | voidfunc = (voidfunc && p->lex == XC_LEX_NAME); |
| 4422 | if (voidfunc) { |
| 4423 | s = zxc_lex_next(); |
| 4424 | if (s) RETURN_STATUS(s); |
| 4425 | } |
| 4426 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4427 | if (p->lex != BC_LEX_LPAREN) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4428 | RETURN_STATUS(bc_error_bad_function_definition()); |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4429 | |
| 4430 | p->fidx = bc_program_addFunc(xstrdup(p->lex_strnumbuf.v)); |
| 4431 | p->func = xc_program_func(p->fidx); |
| 4432 | p->func->voidfunc = voidfunc; |
| 4433 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4434 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4435 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4436 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4437 | comma = false; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4438 | while (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4439 | BcType t = BC_TYPE_VAR; |
| 4440 | |
| 4441 | if (p->lex == XC_LEX_OP_MULTIPLY) { |
| 4442 | t = BC_TYPE_REF; |
| 4443 | s = zxc_lex_next(); |
| 4444 | if (s) RETURN_STATUS(s); |
| 4445 | s = zbc_POSIX_does_not_allow("references"); |
| 4446 | if (s) RETURN_STATUS(s); |
| 4447 | } |
| 4448 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4449 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4450 | RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4451 | |
| 4452 | ++p->func->nparams; |
| 4453 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4454 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4455 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4456 | if (s) goto err; |
| 4457 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4458 | if (p->lex == BC_LEX_LBRACKET) { |
| 4459 | if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4460 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4461 | if (s) goto err; |
| 4462 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4463 | if (p->lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4464 | s = bc_error_bad_function_definition(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4465 | goto err; |
| 4466 | } |
| 4467 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4468 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4469 | if (s) goto err; |
| 4470 | } |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4471 | else if (t == BC_TYPE_REF) { |
| 4472 | s = bc_error_at("vars can't be references"); |
| 4473 | goto err; |
| 4474 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4475 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4476 | comma = p->lex == BC_LEX_COMMA; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4477 | if (comma) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4478 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4479 | if (s) goto err; |
| 4480 | } |
| 4481 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4482 | s = zbc_func_insert(p->func, name, t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4483 | if (s) goto err; |
| 4484 | } |
| 4485 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4486 | if (comma) RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4487 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4488 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4489 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4490 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4491 | if (p->lex != BC_LEX_LBRACE) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4492 | 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] | 4493 | if (s) RETURN_STATUS(s); |
| 4494 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4495 | |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4496 | // 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] | 4497 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4498 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4499 | // GNU bc requires a {} block even if function body has single stmt, enforce this |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4500 | if (p->lex != BC_LEX_LBRACE) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4501 | RETURN_STATUS(bc_error("function { body } expected")); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4502 | |
| 4503 | p->in_funcdef++; // to determine whether "return" stmt is allowed, and such |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4504 | s = zbc_parse_stmt_possibly_auto(true); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4505 | p->in_funcdef--; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4506 | if (s) RETURN_STATUS(s); |
| 4507 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4508 | xc_parse_push(BC_INST_RET0); |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 4509 | |
| 4510 | // Subsequent code generation is into main program |
| 4511 | p->fidx = BC_PROG_MAIN; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4512 | p->func = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4513 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4514 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4515 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4516 | err: |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4517 | dbg_lex_done("%s:%d done (error)", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4518 | free(name); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4519 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4520 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4521 | #define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4522 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4523 | static BC_STATUS zbc_parse_auto(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4524 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4525 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4526 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4527 | char *name; |
| 4528 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4529 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4530 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4531 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4532 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4533 | for (;;) { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4534 | BcType t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4535 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4536 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4537 | RETURN_STATUS(bc_error_at("bad 'auto' syntax")); |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4538 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4539 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4540 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4541 | if (s) goto err; |
| 4542 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4543 | t = BC_TYPE_VAR; |
| 4544 | if (p->lex == BC_LEX_LBRACKET) { |
| 4545 | t = BC_TYPE_ARRAY; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4546 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4547 | if (s) goto err; |
| 4548 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4549 | if (p->lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4550 | s = bc_error_at("bad 'auto' syntax"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4551 | goto err; |
| 4552 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4553 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4554 | if (s) goto err; |
| 4555 | } |
| 4556 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4557 | s = zbc_func_insert(p->func, name, t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4558 | if (s) goto err; |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4559 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4560 | if (p->lex == XC_LEX_NLINE |
| 4561 | || p->lex == BC_LEX_SCOLON |
| 4562 | //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}" |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4563 | ) { |
| 4564 | break; |
| 4565 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4566 | if (p->lex != BC_LEX_COMMA) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4567 | RETURN_STATUS(bc_error_at("bad 'auto' syntax")); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4568 | s = zxc_lex_next(); // skip comma |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4569 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4570 | } |
| 4571 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4572 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4573 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4574 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4575 | free(name); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4576 | dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4577 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4578 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4579 | #define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4580 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4581 | #undef zbc_parse_stmt_possibly_auto |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4582 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4583 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4584 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4585 | BcStatus s = BC_STATUS_SUCCESS; |
| 4586 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4587 | dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4588 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4589 | if (p->lex == XC_LEX_NLINE) { |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4590 | dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__); |
Denys Vlasenko | fc7aa7a | 2019-01-08 18:08:48 +0100 | [diff] [blame] | 4591 | RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4592 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4593 | if (p->lex == BC_LEX_SCOLON) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4594 | dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__); |
Denys Vlasenko | fc7aa7a | 2019-01-08 18:08:48 +0100 | [diff] [blame] | 4595 | RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4596 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4597 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4598 | if (p->lex == BC_LEX_LBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4599 | dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed); |
| 4600 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4601 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4602 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4603 | } while (p->lex == XC_LEX_NLINE); |
| 4604 | if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4605 | dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4606 | s = zbc_parse_auto(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4607 | if (s) RETURN_STATUS(s); |
| 4608 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4609 | while (p->lex != BC_LEX_RBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4610 | dbg_lex("%s:%d block parsing loop", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4611 | s = zbc_parse_stmt(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4612 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | fc7aa7a | 2019-01-08 18:08:48 +0100 | [diff] [blame] | 4613 | // Check that next token is a correct stmt delimiter - |
| 4614 | // disallows "print 1 print 2" and such. |
| 4615 | if (p->lex == BC_LEX_RBRACE) |
| 4616 | break; |
| 4617 | if (p->lex != BC_LEX_SCOLON |
| 4618 | && p->lex != XC_LEX_NLINE |
| 4619 | ) { |
| 4620 | RETURN_STATUS(bc_error_at("bad statement terminator")); |
| 4621 | } |
| 4622 | s = zxc_lex_next(); |
| 4623 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4624 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4625 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4626 | dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__); |
| 4627 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4628 | } |
| 4629 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4630 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
| 4631 | switch (p->lex) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4632 | case XC_LEX_OP_MINUS: |
| 4633 | case BC_LEX_OP_INC: |
| 4634 | case BC_LEX_OP_DEC: |
| 4635 | case BC_LEX_OP_BOOL_NOT: |
| 4636 | case BC_LEX_LPAREN: |
| 4637 | case XC_LEX_NAME: |
| 4638 | case XC_LEX_NUMBER: |
| 4639 | case BC_LEX_KEY_IBASE: |
| 4640 | case BC_LEX_KEY_LAST: |
| 4641 | case BC_LEX_KEY_LENGTH: |
| 4642 | case BC_LEX_KEY_OBASE: |
| 4643 | case BC_LEX_KEY_READ: |
| 4644 | case BC_LEX_KEY_SCALE: |
| 4645 | case BC_LEX_KEY_SQRT: |
| 4646 | s = zbc_parse_expr(BC_PARSE_PRINT); |
| 4647 | break; |
| 4648 | case XC_LEX_STR: |
| 4649 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4650 | xc_parse_push(XC_INST_PRINT_STR); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4651 | break; |
| 4652 | case BC_LEX_KEY_BREAK: |
| 4653 | case BC_LEX_KEY_CONTINUE: |
| 4654 | s = zbc_parse_break_or_continue(p->lex); |
| 4655 | break; |
| 4656 | case BC_LEX_KEY_FOR: |
| 4657 | s = zbc_parse_for(); |
| 4658 | break; |
| 4659 | case BC_LEX_KEY_HALT: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4660 | xc_parse_push(BC_INST_HALT); |
| 4661 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4662 | break; |
| 4663 | case BC_LEX_KEY_IF: |
| 4664 | s = zbc_parse_if(); |
| 4665 | break; |
| 4666 | case BC_LEX_KEY_LIMITS: |
| 4667 | // "limits" is a compile-time command, |
| 4668 | // the output is produced at _parse time_. |
| 4669 | printf( |
| 4670 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4671 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4672 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4673 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
Denys Vlasenko | e05ec6e | 2019-01-04 16:26:19 +0100 | [diff] [blame] | 4674 | // "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" - GNU bc does not show this |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4675 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4676 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4677 | ); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4678 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4679 | break; |
| 4680 | case BC_LEX_KEY_PRINT: |
| 4681 | s = zbc_parse_print(); |
| 4682 | break; |
| 4683 | case BC_LEX_KEY_QUIT: |
| 4684 | // "quit" is a compile-time command. For example, |
| 4685 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4686 | // not when it is executed |
| 4687 | QUIT_OR_RETURN_TO_MAIN; |
| 4688 | case BC_LEX_KEY_RETURN: |
| 4689 | if (!p->in_funcdef) |
| 4690 | RETURN_STATUS(bc_error("'return' not in a function")); |
| 4691 | s = zbc_parse_return(); |
| 4692 | break; |
| 4693 | case BC_LEX_KEY_WHILE: |
| 4694 | s = zbc_parse_while(); |
| 4695 | break; |
| 4696 | default: |
| 4697 | s = bc_error_bad_token(); |
| 4698 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4699 | } |
| 4700 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4701 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4702 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4703 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4704 | #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] | 4705 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4706 | static BC_STATUS zbc_parse_stmt_or_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4707 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4708 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4709 | BcStatus s; |
| 4710 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4711 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | fc7aa7a | 2019-01-08 18:08:48 +0100 | [diff] [blame] | 4712 | //why? |
| 4713 | // if (p->lex == XC_LEX_EOF) |
| 4714 | // s = bc_error("end of file"); |
| 4715 | // else |
| 4716 | if (p->lex == BC_LEX_KEY_DEFINE) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4717 | dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4718 | s = zbc_parse_funcdef(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4719 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4720 | dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4721 | s = zbc_parse_stmt(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4722 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4723 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4724 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4725 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4726 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4727 | #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] | 4728 | |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4729 | #undef zbc_parse_expr |
| 4730 | static BC_STATUS zbc_parse_expr(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4731 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4732 | BcParse *p = &G.prs; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4733 | BcInst prev = XC_INST_PRINT; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4734 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4735 | unsigned nparens, nrelops; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4736 | bool paren_first, rprn, assign, bin_last, incdec; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4737 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4738 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4739 | paren_first = (p->lex == BC_LEX_LPAREN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4740 | nparens = nrelops = 0; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4741 | rprn = assign = incdec = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4742 | bin_last = true; |
| 4743 | |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4744 | for (;;) { |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4745 | bool get_token; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4746 | BcStatus s; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4747 | BcLexType t = p->lex; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4748 | |
| 4749 | if (!lex_allowed_in_bc_expr(t)) |
| 4750 | break; |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4751 | |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4752 | dbg_lex("%s:%d t:%d", __func__, __LINE__, t); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4753 | get_token = false; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4754 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4755 | switch (t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4756 | case BC_LEX_OP_INC: |
| 4757 | case BC_LEX_OP_DEC: |
| 4758 | dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__); |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4759 | if (incdec) RETURN_STATUS(bc_error_bad_assignment()); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4760 | s = zbc_parse_incdec(&prev, &nexprs, flags); |
| 4761 | incdec = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4762 | rprn = bin_last = false; |
| 4763 | //get_token = false; - already is |
| 4764 | break; |
| 4765 | case XC_LEX_OP_MINUS: |
| 4766 | dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4767 | s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4768 | rprn = false; |
| 4769 | //get_token = false; - already is |
| 4770 | bin_last = (prev == XC_INST_MINUS); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4771 | if (bin_last) incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4772 | break; |
| 4773 | case BC_LEX_OP_ASSIGN_POWER: |
| 4774 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4775 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4776 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4777 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4778 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4779 | case BC_LEX_OP_ASSIGN: |
| 4780 | dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__); |
| 4781 | if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM |
| 4782 | && prev != XC_INST_SCALE && prev != XC_INST_IBASE |
| 4783 | && prev != XC_INST_OBASE && prev != BC_INST_LAST |
| 4784 | ) { |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4785 | RETURN_STATUS(bc_error_bad_assignment()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4786 | } |
| 4787 | // Fallthrough. |
| 4788 | case XC_LEX_OP_POWER: |
| 4789 | case XC_LEX_OP_MULTIPLY: |
| 4790 | case XC_LEX_OP_DIVIDE: |
| 4791 | case XC_LEX_OP_MODULUS: |
| 4792 | case XC_LEX_OP_PLUS: |
| 4793 | case XC_LEX_OP_REL_EQ: |
| 4794 | case XC_LEX_OP_REL_LE: |
| 4795 | case XC_LEX_OP_REL_GE: |
| 4796 | case XC_LEX_OP_REL_NE: |
| 4797 | case XC_LEX_OP_REL_LT: |
| 4798 | case XC_LEX_OP_REL_GT: |
| 4799 | case BC_LEX_OP_BOOL_NOT: |
| 4800 | case BC_LEX_OP_BOOL_OR: |
| 4801 | case BC_LEX_OP_BOOL_AND: |
| 4802 | dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4803 | if (t == BC_LEX_OP_BOOL_NOT) { |
| 4804 | if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4805 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4806 | } else if (prev == XC_INST_BOOL_NOT) { |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4807 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4808 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4809 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4810 | nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT); |
| 4811 | prev = BC_TOKEN_2_INST(t); |
| 4812 | bc_parse_operator(t, ops_bgn, &nexprs); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4813 | rprn = incdec = false; |
| 4814 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4815 | bin_last = (t != BC_LEX_OP_BOOL_NOT); |
| 4816 | break; |
| 4817 | case BC_LEX_LPAREN: |
| 4818 | dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4819 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4820 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4821 | bc_vec_push(&p->ops, &t); |
| 4822 | nparens++; |
| 4823 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4824 | rprn = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4825 | break; |
| 4826 | case BC_LEX_RPAREN: |
| 4827 | dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__); |
Denys Vlasenko | a1698a1 | 2019-01-08 19:32:38 +0100 | [diff] [blame] | 4828 | //why? |
| 4829 | // if (p->lex_last == BC_LEX_LPAREN) { |
| 4830 | // RETURN_STATUS(bc_error_at("empty expression")); |
| 4831 | // } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4832 | if (bin_last || prev == XC_INST_BOOL_NOT) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4833 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4834 | if (nparens == 0) { |
| 4835 | goto exit_loop; |
| 4836 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4837 | s = zbc_parse_rightParen(ops_bgn, &nexprs); |
| 4838 | nparens--; |
| 4839 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4840 | rprn = true; |
| 4841 | bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4842 | break; |
| 4843 | case XC_LEX_NAME: |
| 4844 | dbg_lex("%s:%d LEX_NAME", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4845 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4846 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4847 | s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4848 | rprn = (prev == BC_INST_CALL); |
| 4849 | bin_last = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4850 | //get_token = false; - already is |
| 4851 | nexprs++; |
| 4852 | break; |
| 4853 | case XC_LEX_NUMBER: |
| 4854 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4855 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4856 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4857 | xc_parse_pushNUM(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4858 | prev = XC_INST_NUM; |
| 4859 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4860 | rprn = bin_last = false; |
| 4861 | nexprs++; |
| 4862 | break; |
| 4863 | case BC_LEX_KEY_IBASE: |
| 4864 | case BC_LEX_KEY_LAST: |
| 4865 | case BC_LEX_KEY_OBASE: |
| 4866 | dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4867 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4868 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4869 | prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4870 | xc_parse_push((char) prev); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4871 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4872 | rprn = bin_last = false; |
| 4873 | nexprs++; |
| 4874 | break; |
| 4875 | case BC_LEX_KEY_LENGTH: |
| 4876 | case BC_LEX_KEY_SQRT: |
| 4877 | dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4878 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4879 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4880 | s = zbc_parse_builtin(t, flags, &prev); |
| 4881 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4882 | rprn = bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4883 | nexprs++; |
| 4884 | break; |
| 4885 | case BC_LEX_KEY_READ: |
| 4886 | dbg_lex("%s:%d LEX_READ", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4887 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4888 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4889 | s = zbc_parse_read(); |
| 4890 | prev = XC_INST_READ; |
| 4891 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4892 | rprn = bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4893 | nexprs++; |
| 4894 | break; |
| 4895 | case BC_LEX_KEY_SCALE: |
| 4896 | dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4897 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4898 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4899 | s = zbc_parse_scale(&prev, flags); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4900 | //get_token = false; - already is |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4901 | rprn = bin_last = false; |
| 4902 | nexprs++; |
| 4903 | break; |
| 4904 | default: |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4905 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4906 | } |
| 4907 | |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4908 | if (s || G_interrupt) // error, or ^C: stop parsing |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4909 | RETURN_STATUS(BC_STATUS_FAILURE); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4910 | if (get_token) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4911 | s = zxc_lex_next(); |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4912 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4913 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4914 | } |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4915 | exit_loop: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4916 | |
| 4917 | while (p->ops.len > ops_bgn) { |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4918 | BcLexType top = BC_PARSE_TOP_OP(p); |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 4919 | assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4920 | |
| 4921 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4922 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4923 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4924 | xc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4925 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4926 | nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4927 | bc_vec_pop(&p->ops); |
| 4928 | } |
| 4929 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4930 | if (prev == XC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4931 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4932 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4933 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4934 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4935 | s = zbc_POSIX_does_not_allow("comparison operators outside if or loops"); |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4936 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4937 | } else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4938 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4939 | s = zbc_POSIX_requires("exactly one comparison operator per condition"); |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4940 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | if (flags & BC_PARSE_PRINT) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4944 | if (paren_first || !assign) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4945 | xc_parse_push(XC_INST_PRINT); |
| 4946 | xc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4947 | } |
| 4948 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4949 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4950 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4951 | } |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4952 | #define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4953 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4954 | #endif // ENABLE_BC |
| 4955 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 4956 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 4957 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4958 | static BC_STATUS zdc_parse_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4959 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4960 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4961 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4962 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4963 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4964 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4965 | if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4966 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4967 | xc_parse_pushName(p->lex_strnumbuf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4968 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4969 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4970 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4971 | #define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4972 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4973 | static void dc_parse_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4974 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4975 | BcParse *p = &G.prs; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4976 | char *str; |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4977 | size_t len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4978 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4979 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4980 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4981 | str = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 4982 | xc_parse_pushInst_and_Index(XC_INST_STR, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4983 | bc_vec_push(&G.prog.strs, &str); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4984 | |
Brian Foley | b64470b | 2019-09-05 10:50:13 +0200 | [diff] [blame] | 4985 | // Add an empty function so that if zdc_program_execStr ever needs to |
| 4986 | // parse the string into code (from the 'x' command) there's somewhere |
| 4987 | // to store the bytecode. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4988 | xc_program_add_fn(); |
| 4989 | p->func = xc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4990 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4991 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4992 | } |
| 4993 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4994 | 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] | 4995 | { |
| 4996 | BcStatus s; |
| 4997 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4998 | xc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4999 | if (name) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5000 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5001 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5002 | } |
| 5003 | |
| 5004 | if (store) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5005 | xc_parse_push(DC_INST_SWAP); |
| 5006 | xc_parse_push(XC_INST_ASSIGN); |
| 5007 | xc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5008 | } |
| 5009 | |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5010 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5011 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5012 | #define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5013 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5014 | static BC_STATUS zdc_parse_cond(uint8_t inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5015 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5016 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5017 | BcStatus s; |
| 5018 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5019 | xc_parse_push(inst); |
| 5020 | xc_parse_push(DC_INST_EXEC_COND); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5021 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5022 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5023 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5024 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5025 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5026 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5027 | |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5028 | // Note that 'else' part can not be on the next line: |
| 5029 | // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2" |
| 5030 | // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5031 | if (p->lex == DC_LEX_ELSE) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5032 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5033 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5034 | s = zxc_lex_next(); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5035 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5036 | xc_parse_push('\0'); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5037 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5038 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5039 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5040 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5041 | #define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5042 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5043 | static BC_STATUS zdc_parse_token(BcLexType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5044 | { |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5045 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5046 | uint8_t inst; |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5047 | bool assign, get_token; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5048 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5049 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5050 | s = BC_STATUS_SUCCESS; |
| 5051 | get_token = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5052 | switch (t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5053 | case XC_LEX_OP_REL_EQ: |
| 5054 | case XC_LEX_OP_REL_LE: |
| 5055 | case XC_LEX_OP_REL_GE: |
| 5056 | case XC_LEX_OP_REL_NE: |
| 5057 | case XC_LEX_OP_REL_LT: |
| 5058 | case XC_LEX_OP_REL_GT: |
| 5059 | dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__); |
| 5060 | s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ); |
| 5061 | get_token = false; |
| 5062 | break; |
| 5063 | case DC_LEX_SCOLON: |
| 5064 | case DC_LEX_COLON: |
| 5065 | dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__); |
| 5066 | s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON); |
| 5067 | break; |
| 5068 | case XC_LEX_STR: |
| 5069 | dbg_lex("%s:%d LEX_STR", __func__, __LINE__); |
| 5070 | dc_parse_string(); |
| 5071 | break; |
| 5072 | case XC_LEX_NEG: |
| 5073 | dbg_lex("%s:%d LEX_NEG", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5074 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5075 | if (s) RETURN_STATUS(s); |
| 5076 | if (G.prs.lex != XC_LEX_NUMBER) |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5077 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5078 | xc_parse_pushNUM(); |
| 5079 | xc_parse_push(XC_INST_NEG); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5080 | break; |
| 5081 | case XC_LEX_NUMBER: |
| 5082 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5083 | xc_parse_pushNUM(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5084 | break; |
| 5085 | case DC_LEX_READ: |
| 5086 | dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5087 | xc_parse_push(XC_INST_READ); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5088 | break; |
| 5089 | case DC_LEX_OP_ASSIGN: |
| 5090 | case DC_LEX_STORE_PUSH: |
| 5091 | dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__); |
| 5092 | assign = (t == DC_LEX_OP_ASSIGN); |
| 5093 | inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR; |
| 5094 | s = zdc_parse_mem(inst, true, assign); |
| 5095 | break; |
| 5096 | case DC_LEX_LOAD: |
| 5097 | case DC_LEX_LOAD_POP: |
| 5098 | dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__); |
| 5099 | inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD; |
| 5100 | s = zdc_parse_mem(inst, true, false); |
| 5101 | break; |
| 5102 | case DC_LEX_STORE_IBASE: |
| 5103 | case DC_LEX_STORE_SCALE: |
| 5104 | case DC_LEX_STORE_OBASE: |
| 5105 | dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__); |
| 5106 | inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE; |
| 5107 | s = zdc_parse_mem(inst, false, true); |
| 5108 | break; |
| 5109 | default: |
| 5110 | dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__); |
| 5111 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5112 | } |
| 5113 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5114 | if (!s && get_token) s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5115 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5116 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5117 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5118 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5119 | #define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5120 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5121 | static BC_STATUS zdc_parse_expr(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5122 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5123 | BcParse *p = &G.prs; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5124 | int i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5125 | |
Denys Vlasenko | 6842c60 | 2019-01-04 05:41:47 +0100 | [diff] [blame] | 5126 | if (p->lex == XC_LEX_NLINE) |
| 5127 | RETURN_STATUS(zxc_lex_next()); |
| 5128 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5129 | i = (int)p->lex - (int)XC_LEX_OP_POWER; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5130 | if (i >= 0) { |
| 5131 | BcInst inst = dc_LEX_to_INST[i]; |
| 5132 | if (inst != DC_INST_INVALID) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5133 | xc_parse_push(inst); |
| 5134 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5135 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5136 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5137 | RETURN_STATUS(zdc_parse_token(p->lex)); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5138 | } |
| 5139 | #define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
| 5140 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5141 | static BC_STATUS zdc_parse_exprs_until_eof(void) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5142 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5143 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5144 | dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex); |
| 5145 | while (p->lex != XC_LEX_EOF) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5146 | BcStatus s = zdc_parse_expr(); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5147 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5148 | } |
| 5149 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5150 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5151 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5152 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5153 | #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] | 5154 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5155 | #endif // ENABLE_DC |
| 5156 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5157 | // |
| 5158 | // Execution engine |
| 5159 | // |
| 5160 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 5161 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 5162 | #define BC_PROG_NUM(r, n) \ |
| 5163 | ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n)) |
| 5164 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5165 | #define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n))) |
| 5166 | #define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n))) |
| 5167 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5168 | static size_t xc_program_index(char *code, size_t *bgn) |
| 5169 | { |
| 5170 | unsigned char *bytes = (void*)(code + *bgn); |
| 5171 | unsigned amt; |
| 5172 | unsigned i; |
| 5173 | size_t res; |
| 5174 | |
| 5175 | amt = *bytes++; |
| 5176 | if (amt < SMALL_INDEX_LIMIT) { |
| 5177 | *bgn += 1; |
| 5178 | return amt; |
| 5179 | } |
| 5180 | amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here |
| 5181 | *bgn += amt + 1; |
| 5182 | |
| 5183 | res = 0; |
| 5184 | i = 0; |
| 5185 | do { |
| 5186 | res |= (size_t)(*bytes++) << i; |
| 5187 | i += 8; |
| 5188 | } while (--amt != 0); |
| 5189 | |
| 5190 | return res; |
| 5191 | } |
| 5192 | |
| 5193 | static char *xc_program_name(char *code, size_t *bgn) |
| 5194 | { |
| 5195 | code += *bgn; |
| 5196 | *bgn += strlen(code) + 1; |
| 5197 | |
| 5198 | return xstrdup(code); |
| 5199 | } |
| 5200 | |
| 5201 | static BcVec* xc_program_dereference(BcVec *vec) |
| 5202 | { |
| 5203 | BcVec *v; |
| 5204 | size_t vidx, nidx, i = 0; |
| 5205 | |
| 5206 | //assert(vec->size == sizeof(uint8_t)); |
| 5207 | |
| 5208 | vidx = xc_program_index(vec->v, &i); |
| 5209 | nidx = xc_program_index(vec->v, &i); |
| 5210 | |
| 5211 | v = bc_vec_item(&G.prog.arrs, vidx); |
| 5212 | v = bc_vec_item(v, nidx); |
| 5213 | |
| 5214 | //assert(v->size != sizeof(uint8_t)); |
| 5215 | |
| 5216 | return v; |
| 5217 | } |
| 5218 | |
| 5219 | static BcVec* xc_program_search(char *id, BcType type) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5220 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5221 | BcId e, *ptr; |
| 5222 | BcVec *v, *map; |
| 5223 | size_t i; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5224 | int new; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5225 | bool var = (type == BC_TYPE_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5226 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5227 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5228 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5229 | |
| 5230 | e.name = id; |
| 5231 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5232 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5233 | |
| 5234 | if (new) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5235 | BcVec v2; |
| 5236 | bc_array_init(&v2, var); |
| 5237 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5238 | } |
| 5239 | |
| 5240 | ptr = bc_vec_item(map, i); |
| 5241 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5242 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5243 | } |
| 5244 | |
Denys Vlasenko | 8287b1c | 2018-12-21 22:43:53 +0100 | [diff] [blame] | 5245 | // 'num' need not be initialized on entry |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5246 | static BC_STATUS zxc_program_num(BcResult *r, BcNum **num) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5247 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5248 | switch (r->t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5249 | case XC_RESULT_STR: |
| 5250 | case XC_RESULT_TEMP: |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5251 | IF_BC(case BC_RESULT_VOID:) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5252 | case XC_RESULT_IBASE: |
| 5253 | case XC_RESULT_SCALE: |
| 5254 | case XC_RESULT_OBASE: |
| 5255 | *num = &r->d.n; |
| 5256 | break; |
| 5257 | case XC_RESULT_CONSTANT: { |
| 5258 | BcStatus s; |
| 5259 | char *str; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5260 | size_t len; |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 5261 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5262 | str = *xc_program_const(r->d.id.idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5263 | len = strlen(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5264 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5265 | bc_num_init(&r->d.n, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5266 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5267 | s = zxc_num_parse(&r->d.n, str, G.prog.ib_t); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5268 | if (s) { |
| 5269 | bc_num_free(&r->d.n); |
| 5270 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5271 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5272 | *num = &r->d.n; |
| 5273 | r->t = XC_RESULT_TEMP; |
| 5274 | break; |
| 5275 | } |
| 5276 | case XC_RESULT_VAR: |
| 5277 | case XC_RESULT_ARRAY: |
| 5278 | case XC_RESULT_ARRAY_ELEM: { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5279 | BcType type = (r->t == XC_RESULT_VAR) ? BC_TYPE_VAR : BC_TYPE_ARRAY; |
| 5280 | BcVec *v = xc_program_search(r->d.id.name, type); |
| 5281 | void *p = bc_vec_top(v); |
| 5282 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5283 | if (r->t == XC_RESULT_ARRAY_ELEM) { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5284 | size_t idx = r->d.id.idx; |
| 5285 | |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5286 | v = p; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5287 | if (v->size == sizeof(uint8_t)) |
| 5288 | v = xc_program_dereference(v); |
| 5289 | //assert(v->size == sizeof(BcNum)); |
| 5290 | if (v->len <= idx) |
| 5291 | bc_array_expand(v, idx + 1); |
| 5292 | *num = bc_vec_item(v, idx); |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5293 | } else { |
| 5294 | *num = p; |
| 5295 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5296 | break; |
| 5297 | } |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5298 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5299 | case BC_RESULT_LAST: |
| 5300 | *num = &G.prog.last; |
| 5301 | break; |
| 5302 | case BC_RESULT_ONE: |
| 5303 | *num = &G.prog.one; |
| 5304 | break; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5305 | #endif |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5306 | #if SANITY_CHECKS |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5307 | default: |
| 5308 | // Testing the theory that dc does not reach LAST/ONE |
| 5309 | bb_error_msg_and_die("BUG:%d", r->t); |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5310 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5311 | } |
| 5312 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5313 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5314 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5315 | #define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5316 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5317 | static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5318 | BcResult **r, BcNum **rn, bool assign) |
| 5319 | { |
| 5320 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5321 | BcResultType lt, rt; |
| 5322 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5323 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5324 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5325 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5326 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5327 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5328 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5329 | s = zxc_program_num(*l, ln); |
| 5330 | if (s) RETURN_STATUS(s); |
| 5331 | s = zxc_program_num(*r, rn); |
| 5332 | if (s) RETURN_STATUS(s); |
| 5333 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5334 | lt = (*l)->t; |
| 5335 | rt = (*r)->t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5336 | |
| 5337 | // We run this again under these conditions in case any vector has been |
| 5338 | // reallocated out from under the BcNums or arrays we had. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5339 | if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5340 | s = zxc_program_num(*l, ln); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5341 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5342 | } |
| 5343 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5344 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5345 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5346 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5347 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5348 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5349 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5350 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5351 | #define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5352 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5353 | static void xc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5354 | { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5355 | r->t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5356 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5357 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5358 | } |
| 5359 | |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5360 | // Note: *r and *n need not be initialized by caller |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5361 | static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5362 | { |
| 5363 | BcStatus s; |
| 5364 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5365 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5366 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5367 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5368 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5369 | s = zxc_program_num(*r, n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5370 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5371 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5372 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5373 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5374 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5375 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5376 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5377 | #define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5378 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5379 | static void xc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5380 | { |
| 5381 | r->t = t; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5382 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5383 | } |
| 5384 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5385 | static BC_STATUS zxc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5386 | { |
| 5387 | BcStatus s; |
| 5388 | BcResult *opd1, *opd2, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5389 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5390 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5391 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5392 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5393 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5394 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5395 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5396 | IF_ERROR_RETURN_POSSIBLE(s =) zxc_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] | 5397 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5398 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5399 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5400 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5401 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5402 | bc_num_free(&res.d.n); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5403 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5404 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5405 | #define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5406 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5407 | static BC_STATUS zxc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5408 | { |
| 5409 | BcStatus s; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5410 | BcParse sv_parse; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5411 | BcVec buf; |
| 5412 | BcInstPtr ip; |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5413 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5414 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 5415 | bc_char_vec_init(&buf); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5416 | xc_read_line(&buf, stdin); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5417 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5418 | f = xc_program_func(BC_PROG_READ); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5419 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5420 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5421 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5422 | xc_parse_create(BC_PROG_READ); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 5423 | //G.err_line = G.prs.lex_line = 1; - not needed, error line info is not printed for read() |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5424 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5425 | s = zxc_parse_text_init(buf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5426 | if (s) goto exec_err; |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5427 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5428 | IF_BC(s = zbc_parse_expr(0)); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5429 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5430 | IF_DC(s = zdc_parse_exprs_until_eof()); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5431 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5432 | if (s) goto exec_err; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5433 | if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 5434 | s = bc_error_at("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5435 | goto exec_err; |
| 5436 | } |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 5437 | xc_parse_push(XC_INST_RET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5438 | |
| 5439 | ip.func = BC_PROG_READ; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5440 | ip.inst_idx = 0; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5441 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5442 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5443 | exec_err: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5444 | xc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5445 | G.prs = sv_parse; // struct copy |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5446 | bc_vec_free(&buf); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5447 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5448 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5449 | #define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5450 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5451 | static void xc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5452 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5453 | #if ENABLE_DC |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5454 | if (!str[0] && IS_DC) { |
Denys Vlasenko | 2c6f563 | 2018-12-11 21:21:14 +0100 | [diff] [blame] | 5455 | // Example: echo '[]ap' | dc |
| 5456 | // should print two bytes: 0x00, 0x0A |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5457 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5458 | return; |
| 5459 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5460 | #endif |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5461 | while (*str) { |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5462 | char c = *str++; |
| 5463 | if (c == '\\') { |
| 5464 | static const char esc[] ALIGN1 = "nabfrt""e\\"; |
| 5465 | char *n; |
| 5466 | |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5467 | c = *str++; |
Brian Foley | 10509a7 | 2019-09-05 10:53:21 +0200 | [diff] [blame] | 5468 | n = strchr(esc, c); // note: if c is NUL, n = \0 at end of esc |
| 5469 | if (!n || !c) { |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5470 | // Just print the backslash and following character |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5471 | bb_putchar('\\'); |
| 5472 | ++G.prog.nchars; |
Brian Foley | 10509a7 | 2019-09-05 10:53:21 +0200 | [diff] [blame] | 5473 | // But if we're at the end of the string, stop |
| 5474 | if (!c) break; |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5475 | } else { |
| 5476 | if (n - esc == 0) // "\n" ? |
| 5477 | G.prog.nchars = SIZE_MAX; |
| 5478 | c = "\n\a\b\f\r\t""\\\\""\\"[n - esc]; |
| 5479 | // n a b f r t e \ \<end of line> |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5480 | } |
| 5481 | } |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5482 | putchar(c); |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5483 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5484 | } |
| 5485 | } |
| 5486 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5487 | static void bc_num_printNewline(void) |
| 5488 | { |
| 5489 | if (G.prog.nchars == G.prog.len - 1) { |
| 5490 | bb_putchar('\\'); |
| 5491 | bb_putchar('\n'); |
| 5492 | G.prog.nchars = 0; |
| 5493 | } |
| 5494 | } |
| 5495 | |
| 5496 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5497 | 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] | 5498 | { |
| 5499 | (void) radix; |
| 5500 | bb_putchar((char) num); |
| 5501 | G.prog.nchars += width; |
| 5502 | } |
| 5503 | #endif |
| 5504 | |
| 5505 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
| 5506 | { |
| 5507 | size_t exp, pow; |
| 5508 | |
| 5509 | bc_num_printNewline(); |
| 5510 | bb_putchar(radix ? '.' : ' '); |
| 5511 | ++G.prog.nchars; |
| 5512 | |
| 5513 | bc_num_printNewline(); |
| 5514 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 5515 | continue; |
| 5516 | |
| 5517 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
| 5518 | size_t dig; |
| 5519 | bc_num_printNewline(); |
| 5520 | dig = num / pow; |
| 5521 | num -= dig * pow; |
| 5522 | bb_putchar(((char) dig) + '0'); |
| 5523 | } |
| 5524 | } |
| 5525 | |
| 5526 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
| 5527 | { |
| 5528 | if (radix) { |
| 5529 | bc_num_printNewline(); |
| 5530 | bb_putchar('.'); |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5531 | G.prog.nchars++; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5532 | } |
| 5533 | |
| 5534 | bc_num_printNewline(); |
| 5535 | bb_putchar(bb_hexdigits_upcase[num]); |
| 5536 | G.prog.nchars += width; |
| 5537 | } |
| 5538 | |
| 5539 | static void bc_num_printDecimal(BcNum *n) |
| 5540 | { |
| 5541 | size_t i, rdx = n->rdx - 1; |
| 5542 | |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5543 | if (n->neg) { |
| 5544 | bb_putchar('-'); |
| 5545 | G.prog.nchars++; |
| 5546 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5547 | |
| 5548 | for (i = n->len - 1; i < n->len; --i) |
| 5549 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
| 5550 | } |
| 5551 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5552 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
| 5553 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5554 | static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5555 | { |
| 5556 | BcStatus s; |
| 5557 | BcVec stack; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5558 | BcNum base; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5559 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5560 | BcNum intp, fracp, digit, frac_len; |
| 5561 | unsigned long dig, *ptr; |
| 5562 | size_t i; |
| 5563 | bool radix; |
| 5564 | |
| 5565 | if (n->len == 0) { |
| 5566 | print(0, width, false); |
| 5567 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5568 | } |
| 5569 | |
| 5570 | bc_vec_init(&stack, sizeof(long), NULL); |
| 5571 | bc_num_init(&intp, n->len); |
| 5572 | bc_num_init(&fracp, n->rdx); |
| 5573 | bc_num_init(&digit, width); |
| 5574 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 5575 | bc_num_copy(&intp, n); |
| 5576 | bc_num_one(&frac_len); |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5577 | base.cap = ARRAY_SIZE(base_digs); |
| 5578 | base.num = base_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5579 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5580 | |
| 5581 | bc_num_truncate(&intp, intp.rdx); |
| 5582 | s = zbc_num_sub(n, &intp, &fracp, 0); |
| 5583 | if (s) goto err; |
| 5584 | |
| 5585 | while (intp.len != 0) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5586 | s = zbc_num_divmod(&intp, &base, &intp, &digit, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5587 | if (s) goto err; |
| 5588 | s = zbc_num_ulong(&digit, &dig); |
| 5589 | if (s) goto err; |
| 5590 | bc_vec_push(&stack, &dig); |
| 5591 | } |
| 5592 | |
| 5593 | for (i = 0; i < stack.len; ++i) { |
| 5594 | ptr = bc_vec_item_rev(&stack, i); |
| 5595 | print(*ptr, width, false); |
| 5596 | } |
| 5597 | |
| 5598 | if (!n->rdx) goto err; |
| 5599 | |
| 5600 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5601 | s = zbc_num_mul(&fracp, &base, &fracp, n->rdx); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5602 | if (s) goto err; |
| 5603 | s = zbc_num_ulong(&fracp, &dig); |
| 5604 | if (s) goto err; |
| 5605 | bc_num_ulong2num(&intp, dig); |
| 5606 | s = zbc_num_sub(&fracp, &intp, &fracp, 0); |
| 5607 | if (s) goto err; |
| 5608 | print(dig, width, radix); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5609 | s = zbc_num_mul(&frac_len, &base, &frac_len, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5610 | if (s) goto err; |
| 5611 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5612 | err: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5613 | bc_num_free(&frac_len); |
| 5614 | bc_num_free(&digit); |
| 5615 | bc_num_free(&fracp); |
| 5616 | bc_num_free(&intp); |
| 5617 | bc_vec_free(&stack); |
| 5618 | RETURN_STATUS(s); |
| 5619 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5620 | #define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5621 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5622 | static BC_STATUS zxc_num_printBase(BcNum *n) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5623 | { |
| 5624 | BcStatus s; |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5625 | size_t width; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5626 | BcNumDigitOp print; |
| 5627 | bool neg = n->neg; |
| 5628 | |
| 5629 | if (neg) { |
| 5630 | bb_putchar('-'); |
| 5631 | G.prog.nchars++; |
| 5632 | } |
| 5633 | |
| 5634 | n->neg = false; |
| 5635 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 5636 | if (G.prog.ob_t <= 16) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5637 | width = 1; |
| 5638 | print = bc_num_printHex; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5639 | } else { |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5640 | unsigned i = G.prog.ob_t - 1; |
| 5641 | width = 0; |
| 5642 | for (;;) { |
| 5643 | width++; |
| 5644 | i /= 10; |
| 5645 | if (i == 0) |
| 5646 | break; |
| 5647 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5648 | print = bc_num_printDigits; |
| 5649 | } |
| 5650 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5651 | s = zxc_num_printNum(n, G.prog.ob_t, width, print); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5652 | n->neg = neg; |
| 5653 | |
| 5654 | RETURN_STATUS(s); |
| 5655 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5656 | #define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5657 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5658 | static BC_STATUS zxc_num_print(BcNum *n, bool newline) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5659 | { |
| 5660 | BcStatus s = BC_STATUS_SUCCESS; |
| 5661 | |
| 5662 | bc_num_printNewline(); |
| 5663 | |
| 5664 | if (n->len == 0) { |
| 5665 | bb_putchar('0'); |
| 5666 | ++G.prog.nchars; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5667 | } else if (G.prog.ob_t == 10) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5668 | bc_num_printDecimal(n); |
| 5669 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5670 | s = zxc_num_printBase(n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5671 | |
| 5672 | if (newline) { |
| 5673 | bb_putchar('\n'); |
| 5674 | G.prog.nchars = 0; |
| 5675 | } |
| 5676 | |
| 5677 | RETURN_STATUS(s); |
| 5678 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5679 | #define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5680 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5681 | #if !ENABLE_DC |
| 5682 | // for bc, idx is always 0 |
| 5683 | #define xc_program_print(inst, idx) \ |
| 5684 | xc_program_print(inst) |
| 5685 | #endif |
| 5686 | static BC_STATUS xc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5687 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5688 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5689 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5690 | BcNum *num; |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5691 | IF_NOT_DC(size_t idx = 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5692 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5693 | if (!STACK_HAS_MORE_THAN(&G.prog.results, idx)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5694 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5695 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5696 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5697 | #if ENABLE_BC |
| 5698 | if (inst == XC_INST_PRINT && r->t == BC_RESULT_VOID) |
| 5699 | // void function's result on stack, ignore |
| 5700 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5701 | #endif |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5702 | s = zxc_program_num(r, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5703 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5704 | |
| 5705 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5706 | s = zxc_num_print(num, /*newline:*/ inst == XC_INST_PRINT); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5707 | #if ENABLE_BC |
| 5708 | if (!s && IS_BC) bc_num_copy(&G.prog.last, num); |
| 5709 | #endif |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5710 | } else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5711 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5712 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5713 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5714 | str = *xc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5715 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5716 | if (inst == XC_INST_PRINT_STR) { |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5717 | char *nl; |
| 5718 | G.prog.nchars += printf("%s", str); |
| 5719 | nl = strrchr(str, '\n'); |
| 5720 | if (nl) |
| 5721 | G.prog.nchars = strlen(nl + 1); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5722 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5723 | xc_program_printString(str); |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5724 | if (inst == XC_INST_PRINT) |
| 5725 | bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5726 | } |
| 5727 | } |
| 5728 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5729 | if (!s && inst != XC_INST_PRINT) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5730 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5731 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5732 | } |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5733 | #define zxc_program_print(...) (xc_program_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5734 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5735 | static BC_STATUS zxc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5736 | { |
| 5737 | BcStatus s; |
| 5738 | BcResult res, *ptr; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5739 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5740 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5741 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5742 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5743 | |
| 5744 | bc_num_init(&res.d.n, num->len); |
| 5745 | bc_num_copy(&res.d.n, num); |
| 5746 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5747 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5748 | xc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5749 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5750 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5751 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5752 | #define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5753 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5754 | static BC_STATUS zxc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5755 | { |
| 5756 | BcStatus s; |
| 5757 | BcResult *opd1, *opd2, res; |
| 5758 | BcNum *n1, *n2; |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5759 | ssize_t cond; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5760 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5761 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5762 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5763 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5764 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5765 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5766 | if (inst == XC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5767 | 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] | 5768 | else if (inst == XC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5769 | 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] | 5770 | else { |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5771 | cond = bc_num_cmp(n1, n2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5772 | switch (inst) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5773 | case XC_INST_REL_EQ: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5774 | cond = (cond == 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5775 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5776 | case XC_INST_REL_LE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5777 | cond = (cond <= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5778 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5779 | case XC_INST_REL_GE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5780 | cond = (cond >= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5781 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5782 | case XC_INST_REL_LT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5783 | cond = (cond < 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5784 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5785 | case XC_INST_REL_GT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5786 | cond = (cond > 0); |
| 5787 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5788 | default: // = case XC_INST_REL_NE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5789 | //cond = (cond != 0); - not needed |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5790 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5791 | } |
| 5792 | } |
| 5793 | |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5794 | if (cond) bc_num_one(&res.d.n); |
| 5795 | //else bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5796 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5797 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5798 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5799 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5800 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5801 | #define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5802 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5803 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5804 | static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5805 | { |
| 5806 | BcNum n2; |
| 5807 | BcResult res; |
| 5808 | |
| 5809 | memset(&n2, 0, sizeof(BcNum)); |
| 5810 | n2.rdx = res.d.id.idx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5811 | res.t = XC_RESULT_STR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5812 | |
| 5813 | if (!push) { |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5814 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5815 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5816 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5817 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5818 | } |
| 5819 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5820 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5821 | bc_vec_push(v, &n2); |
| 5822 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5823 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5824 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5825 | #define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5826 | #endif // ENABLE_DC |
| 5827 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5828 | static BC_STATUS zxc_program_popResultAndCopyToVar(char *name, BcType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5829 | { |
| 5830 | BcStatus s; |
| 5831 | BcResult *ptr, r; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5832 | BcVec *vec; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5833 | BcNum *n; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5834 | bool var = (t == BC_TYPE_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5835 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5836 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5837 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5838 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5839 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5840 | if ((ptr->t == XC_RESULT_ARRAY) == var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5841 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5842 | vec = xc_program_search(name, t); |
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 | #if ENABLE_DC |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5845 | if (ptr->t == XC_RESULT_STR) { |
| 5846 | if (!var) |
| 5847 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
| 5848 | RETURN_STATUS(zdc_program_assignStr(ptr, vec, true)); |
| 5849 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5850 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5851 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5852 | s = zxc_program_num(ptr, &n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5853 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5854 | |
| 5855 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5856 | vec = xc_program_search(name, t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5857 | |
| 5858 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5859 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5860 | bc_num_copy(&r.d.n, n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5861 | } else { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5862 | BcVec *v = (BcVec*) n; |
| 5863 | bool ref, ref_size; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5864 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5865 | ref = (v->size == sizeof(BcVec) && t != BC_TYPE_ARRAY); |
| 5866 | ref_size = (v->size == sizeof(uint8_t)); |
| 5867 | |
| 5868 | if (ref || (ref_size && t == BC_TYPE_REF)) { |
| 5869 | bc_vec_init(&r.d.v, sizeof(uint8_t), NULL); |
| 5870 | if (ref) { |
| 5871 | size_t vidx, idx; |
| 5872 | BcId id; |
| 5873 | |
| 5874 | id.name = ptr->d.id.name; |
| 5875 | v = xc_program_search(ptr->d.id.name, BC_TYPE_REF); |
| 5876 | |
| 5877 | // Make sure the pointer was not invalidated. |
| 5878 | vec = xc_program_search(name, t); |
| 5879 | |
| 5880 | vidx = bc_map_find_exact(&G.prog.arr_map, &id); |
| 5881 | //assert(vidx != BC_VEC_INVALID_IDX); |
| 5882 | vidx = ((BcId*) bc_vec_item(&G.prog.arr_map, vidx))->idx; |
| 5883 | idx = v->len - 1; |
| 5884 | |
| 5885 | bc_vec_pushIndex(&r.d.v, vidx); |
| 5886 | bc_vec_pushIndex(&r.d.v, idx); |
| 5887 | } |
| 5888 | // If we get here, we are copying a ref to a ref. |
| 5889 | else bc_vec_npush(&r.d.v, v->len, v->v); |
| 5890 | |
| 5891 | // We need to return early. |
| 5892 | goto ret; |
| 5893 | } |
| 5894 | |
| 5895 | if (ref_size && t != BC_TYPE_REF) |
| 5896 | v = xc_program_dereference(v); |
| 5897 | |
| 5898 | bc_array_init(&r.d.v, true); |
| 5899 | bc_array_copy(&r.d.v, v); |
| 5900 | } |
| 5901 | ret: |
| 5902 | bc_vec_push(vec, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5903 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5904 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5905 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5906 | } |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 5907 | #define zxc_program_popResultAndCopyToVar(...) (zxc_program_popResultAndCopyToVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5908 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5909 | static BC_STATUS zxc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5910 | { |
| 5911 | BcStatus s; |
| 5912 | BcResult *left, *right, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5913 | BcNum *l, *r; |
| 5914 | bool assign = (inst == XC_INST_ASSIGN); |
| 5915 | bool ib, sc; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5916 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5917 | s = zxc_program_binOpPrep(&left, &l, &right, &r, assign); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5918 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5919 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5920 | ib = left->t == XC_RESULT_IBASE; |
| 5921 | sc = left->t == XC_RESULT_SCALE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5922 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5923 | #if ENABLE_DC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5924 | if (right->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5925 | BcVec *v; |
| 5926 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5927 | if (left->t != XC_RESULT_VAR) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5928 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5929 | v = xc_program_search(left->d.id.name, BC_TYPE_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5930 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5931 | RETURN_STATUS(zdc_program_assignStr(right, v, false)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5932 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5933 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5934 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5935 | if (left->t == XC_RESULT_CONSTANT |
| 5936 | || left->t == XC_RESULT_TEMP |
| 5937 | IF_BC(|| left->t == BC_RESULT_VOID) |
| 5938 | ) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 5939 | RETURN_STATUS(bc_error_bad_assignment()); |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5940 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5941 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5942 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5943 | if (assign) |
| 5944 | bc_num_copy(l, r); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5945 | else { |
| 5946 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5947 | IF_ERROR_RETURN_POSSIBLE(s =) zxc_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] | 5948 | } |
| 5949 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5950 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5951 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5952 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5953 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5954 | if (ib || sc || left->t == XC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5955 | static const char *const msg[] = { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5956 | "bad ibase; must be [2,16]", //XC_RESULT_IBASE |
| 5957 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE |
| 5958 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5959 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5960 | size_t *ptr; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5961 | size_t max; |
| 5962 | unsigned long val; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5963 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5964 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5965 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5966 | s = left->t - XC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5967 | if (sc) { |
| 5968 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5969 | ptr = &G.prog.scale; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5970 | } else { |
| 5971 | if (val < 2) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5972 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5973 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5974 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5975 | } |
| 5976 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5977 | if (val > max) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5978 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5979 | |
| 5980 | *ptr = (size_t) val; |
| 5981 | s = BC_STATUS_SUCCESS; |
| 5982 | } |
| 5983 | |
| 5984 | bc_num_init(&res.d.n, l->len); |
| 5985 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5986 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5987 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5988 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5989 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5990 | #define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5991 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5992 | #if !ENABLE_DC |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5993 | #define xc_program_pushVar(code, bgn, pop, copy) \ |
| 5994 | xc_program_pushVar(code, bgn) |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5995 | // for bc, 'pop' and 'copy' are always false |
| 5996 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5997 | static BC_STATUS xc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5998 | bool pop, bool copy) |
| 5999 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6000 | BcResult r; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6001 | char *name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6002 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6003 | r.t = XC_RESULT_VAR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6004 | r.d.id.name = name; |
| 6005 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6006 | #if ENABLE_DC |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6007 | if (pop || copy) { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6008 | BcVec *v = xc_program_search(name, BC_TYPE_VAR); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6009 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6010 | |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6011 | free(name); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6012 | if (!STACK_HAS_MORE_THAN(v, 1 - copy)) { |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6013 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6014 | } |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6015 | |
| 6016 | if (!BC_PROG_STR(num)) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6017 | r.t = XC_RESULT_TEMP; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6018 | bc_num_init_DEF_SIZE(&r.d.n); |
| 6019 | bc_num_copy(&r.d.n, num); |
| 6020 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6021 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6022 | r.d.id.idx = num->rdx; |
| 6023 | } |
| 6024 | |
| 6025 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6026 | } |
| 6027 | #endif // ENABLE_DC |
| 6028 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6029 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6030 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6031 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6032 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6033 | #define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6034 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6035 | 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] | 6036 | { |
| 6037 | BcStatus s = BC_STATUS_SUCCESS; |
| 6038 | BcResult r; |
| 6039 | BcNum *num; |
| 6040 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6041 | r.d.id.name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6042 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6043 | if (inst == XC_INST_ARRAY) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6044 | r.t = XC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6045 | bc_vec_push(&G.prog.results, &r); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6046 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6047 | BcResult *operand; |
| 6048 | unsigned long temp; |
| 6049 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6050 | s = zxc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6051 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6052 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6053 | if (s) goto err; |
| 6054 | |
| 6055 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6056 | 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] | 6057 | goto err; |
| 6058 | } |
| 6059 | |
| 6060 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6061 | xc_program_retire(&r, XC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6062 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6063 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6064 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6065 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6066 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6067 | #define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6068 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6069 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6070 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6071 | { |
| 6072 | BcStatus s; |
| 6073 | BcResult *ptr, res, copy; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6074 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6075 | char inst2 = inst; |
| 6076 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6077 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6078 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6079 | |
| 6080 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6081 | copy.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6082 | bc_num_init(©.d.n, num->len); |
| 6083 | bc_num_copy(©.d.n, num); |
| 6084 | } |
| 6085 | |
| 6086 | res.t = BC_RESULT_ONE; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6087 | inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST) |
| 6088 | ? BC_INST_ASSIGN_PLUS |
| 6089 | : BC_INST_ASSIGN_MINUS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6090 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6091 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6092 | s = zxc_program_assign(inst); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6093 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6094 | |
| 6095 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 6096 | bc_result_pop_and_push(©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6097 | } |
| 6098 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6099 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6100 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6101 | #define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6102 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6103 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6104 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6105 | BcInstPtr ip; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6106 | size_t i, nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6107 | BcId *a; |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 6108 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6109 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6110 | nparams = xc_program_index(code, idx); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6111 | ip.func = xc_program_index(code, idx); |
| 6112 | func = xc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6113 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6114 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6115 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6116 | } |
| 6117 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6118 | 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] | 6119 | } |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 6120 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6121 | |
| 6122 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 6123 | BcResult *arg; |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6124 | BcStatus s; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6125 | bool arr; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6126 | |
| 6127 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6128 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6129 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6130 | arr = (a->idx == BC_TYPE_ARRAY || a->idx == BC_TYPE_REF); |
| 6131 | |
| 6132 | if (arr != (arg->t == XC_RESULT_ARRAY) // array/variable mismatch |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 6133 | // || arg->t == XC_RESULT_STR - impossible, f("str") is not a legal syntax (strings are not bc expressions) |
| 6134 | ) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6135 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 6136 | } |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6137 | s = zxc_program_popResultAndCopyToVar(a->name, (BcType) a->idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6138 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6139 | } |
| 6140 | |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6141 | a = bc_vec_item(&func->autos, i); |
| 6142 | for (; i < func->autos.len; i++, a++) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6143 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6144 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6145 | v = xc_program_search(a->name, (BcType) a->idx); |
| 6146 | if (a->idx == BC_TYPE_VAR) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 6147 | BcNum n2; |
| 6148 | bc_num_init_DEF_SIZE(&n2); |
| 6149 | bc_vec_push(v, &n2); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6150 | } else { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6151 | //assert(a->idx == BC_TYPE_ARRAY); |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 6152 | BcVec v2; |
| 6153 | bc_array_init(&v2, true); |
| 6154 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6155 | } |
| 6156 | } |
| 6157 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6158 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6159 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6160 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6161 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6162 | #define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6163 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6164 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6165 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6166 | BcResult res; |
| 6167 | BcFunc *f; |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6168 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6169 | size_t i; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6170 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6171 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 6172 | f = xc_program_func(ip->func); |
| 6173 | |
| 6174 | res.t = XC_RESULT_TEMP; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6175 | if (inst == XC_INST_RET) { |
Denys Vlasenko | 1db367a | 2019-01-04 06:18:00 +0100 | [diff] [blame] | 6176 | // bc needs this for e.g. RESULT_CONSTANT ("return 5") |
| 6177 | // because bc constants are per-function. |
| 6178 | // TODO: maybe avoid if value is already RESULT_TEMP? |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6179 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6180 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6181 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6182 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6183 | s = zxc_program_num(operand, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6184 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6185 | bc_num_init(&res.d.n, num->len); |
| 6186 | bc_num_copy(&res.d.n, num); |
Denys Vlasenko | 377cc97 | 2019-01-04 00:34:52 +0100 | [diff] [blame] | 6187 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6188 | } else { |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 6189 | if (f->voidfunc) |
| 6190 | res.t = BC_RESULT_VOID; |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6191 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6192 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6193 | } |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 6194 | bc_vec_push(&G.prog.results, &res); |
| 6195 | |
| 6196 | bc_vec_pop(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6197 | |
| 6198 | // 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] | 6199 | a = (void*)f->autos.v; |
| 6200 | for (i = 0; i < f->autos.len; i++, a++) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6201 | BcVec *v; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6202 | v = xc_program_search(a->name, (BcType) a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6203 | bc_vec_pop(v); |
| 6204 | } |
| 6205 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6206 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6207 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6208 | #define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6209 | #endif // ENABLE_BC |
| 6210 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6211 | static unsigned long xc_program_scale(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6212 | { |
| 6213 | return (unsigned long) n->rdx; |
| 6214 | } |
| 6215 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6216 | static unsigned long xc_program_len(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6217 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6218 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6219 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6220 | if (n->rdx != len) return len; |
| 6221 | for (;;) { |
| 6222 | if (len == 0) break; |
| 6223 | len--; |
| 6224 | if (n->num[len] != 0) break; |
| 6225 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6226 | return len; |
| 6227 | } |
| 6228 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6229 | static BC_STATUS zxc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6230 | { |
| 6231 | BcStatus s; |
| 6232 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6233 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6234 | BcResult res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6235 | bool len = (inst == XC_INST_LENGTH); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6236 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6237 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6238 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6239 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6240 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6241 | s = zxc_program_num(opnd, &num); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6242 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6243 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6244 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6245 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6246 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6247 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6248 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6249 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6250 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6251 | if (inst == XC_INST_SQRT) |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6252 | s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6253 | #if ENABLE_BC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6254 | else if (len != 0 && opnd->t == XC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6255 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6256 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6257 | #endif |
| 6258 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6259 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6260 | char **str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6261 | 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] | 6262 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6263 | str = xc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6264 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6265 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6266 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6267 | else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6268 | bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6269 | } |
| 6270 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6271 | xc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6272 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6273 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6274 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6275 | #define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6276 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6277 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6278 | static BC_STATUS zdc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6279 | { |
| 6280 | BcStatus s; |
| 6281 | BcResult *opd1, *opd2, res, res2; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6282 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6283 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6284 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6285 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6286 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6287 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6288 | bc_num_init(&res2.d.n, n2->len); |
| 6289 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6290 | 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] | 6291 | if (s) goto err; |
| 6292 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6293 | xc_program_binOpRetire(&res2); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6294 | res.t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6295 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6296 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6297 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6298 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6299 | bc_num_free(&res2.d.n); |
| 6300 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6301 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6302 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6303 | #define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6304 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6305 | static BC_STATUS zdc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6306 | { |
| 6307 | BcStatus s; |
| 6308 | BcResult *r1, *r2, *r3, res; |
| 6309 | BcNum *n1, *n2, *n3; |
| 6310 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6311 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 2)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6312 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6313 | s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6314 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6315 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6316 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6317 | s = zxc_program_num(r1, &n1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6318 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6319 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6320 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6321 | |
| 6322 | // Make sure that the values have their pointers updated, if necessary. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6323 | if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6324 | if (r1->t == r2->t) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6325 | s = zxc_program_num(r2, &n2); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6326 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6327 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6328 | if (r1->t == r3->t) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6329 | s = zxc_program_num(r3, &n3); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6330 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6331 | } |
| 6332 | } |
| 6333 | |
| 6334 | bc_num_init(&res.d.n, n3->len); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6335 | s = zdc_num_modexp(n1, n2, n3, &res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6336 | if (s) goto err; |
| 6337 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6338 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6339 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6340 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6341 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6342 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6343 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6344 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6345 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6346 | #define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6347 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6348 | static void dc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6349 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6350 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6351 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6352 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6353 | res.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6354 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6355 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6356 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6357 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6358 | } |
| 6359 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6360 | static BC_STATUS zdc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6361 | { |
| 6362 | BcStatus s; |
| 6363 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6364 | BcNum *num, n; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6365 | char **strs; |
| 6366 | char *str; |
| 6367 | char c; |
| 6368 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6369 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6370 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6371 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6372 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 6373 | r = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6374 | s = zxc_program_num(r, &num); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6375 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6376 | |
| 6377 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 6378 | unsigned long val; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6379 | BcNum strmb; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6380 | BcDig strmb_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6381 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6382 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6383 | bc_num_copy(&n, num); |
| 6384 | bc_num_truncate(&n, n.rdx); |
| 6385 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6386 | strmb.cap = ARRAY_SIZE(strmb_digs); |
| 6387 | strmb.num = strmb_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6388 | bc_num_ulong2num(&strmb, 0x100); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6389 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 6390 | s = zbc_num_mod(&n, &strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6391 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6392 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6393 | if (s) goto num_err; |
| 6394 | |
| 6395 | c = (char) val; |
| 6396 | |
| 6397 | bc_num_free(&n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6398 | } else { |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6399 | char *sp; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6400 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6401 | sp = *xc_program_str(idx); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6402 | c = sp[0]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6403 | } |
| 6404 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6405 | strs = (void*)G.prog.strs.v; |
| 6406 | for (idx = 0; idx < G.prog.strs.len; idx++) { |
| 6407 | if (strs[idx][0] == c && strs[idx][1] == '\0') { |
| 6408 | goto dup; |
| 6409 | } |
| 6410 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6411 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6412 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6413 | //str[1] = '\0'; - already is |
Brian Foley | b64470b | 2019-09-05 10:50:13 +0200 | [diff] [blame] | 6414 | idx = bc_vec_push(&G.prog.strs, &str); |
| 6415 | // Add an empty function so that if zdc_program_execStr ever needs to |
| 6416 | // parse the string into code (from the 'x' command) there's somewhere |
| 6417 | // to store the bytecode. |
| 6418 | xc_program_add_fn(); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6419 | dup: |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6420 | res.t = XC_RESULT_STR; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6421 | res.d.id.idx = idx; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 6422 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6423 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6424 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6425 | num_err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6426 | bc_num_free(&n); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6427 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6428 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6429 | #define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6430 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6431 | static BC_STATUS zdc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6432 | { |
| 6433 | BcStatus s; |
| 6434 | BcResult *r; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6435 | BcNum *n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6436 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6437 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6438 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6439 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6440 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6441 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6442 | s = zxc_program_num(r, &n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6443 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6444 | |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6445 | if (BC_PROG_NUM(r, n)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6446 | s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar); |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6447 | } else { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6448 | char *str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6449 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6450 | str = *xc_program_str(idx); |
Ron Yorston | cad3fc7 | 2021-02-03 20:47:14 +0100 | [diff] [blame^] | 6451 | fputs_stdout(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6452 | } |
| 6453 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6454 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6455 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6456 | #define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6457 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6458 | static BC_STATUS zdc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6459 | { |
| 6460 | BcStatus s; |
| 6461 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6462 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6463 | unsigned long val; |
| 6464 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6465 | s = zxc_program_prep(&opnd, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6466 | if (s) RETURN_STATUS(s); |
| 6467 | s = zbc_num_ulong(num, &val); |
| 6468 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6469 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6470 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6471 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6472 | if (G.prog.exestack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6473 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6474 | if (G.prog.exestack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6475 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6476 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6477 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6478 | bc_vec_npop(&G.prog.exestack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6479 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6480 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6481 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6482 | #define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6483 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6484 | 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] | 6485 | { |
| 6486 | BcStatus s = BC_STATUS_SUCCESS; |
| 6487 | BcResult *r; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6488 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6489 | BcInstPtr ip; |
| 6490 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6491 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6492 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6493 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6494 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6495 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6496 | |
| 6497 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6498 | BcNum *n = n; // for compiler |
| 6499 | bool exec; |
| 6500 | char *name; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6501 | char *then_name = xc_program_name(code, bgn); |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6502 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6503 | |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 6504 | if (code[*bgn] == '\0') |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6505 | (*bgn) += 1; |
| 6506 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6507 | else_name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6508 | |
| 6509 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6510 | name = then_name; |
| 6511 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6512 | exec = true; |
| 6513 | name = else_name; |
| 6514 | } |
| 6515 | |
| 6516 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6517 | BcVec *v; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6518 | v = xc_program_search(name, BC_TYPE_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6519 | n = bc_vec_top(v); |
| 6520 | } |
| 6521 | |
| 6522 | free(then_name); |
| 6523 | free(else_name); |
| 6524 | |
| 6525 | if (!exec) goto exit; |
| 6526 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6527 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6528 | goto exit; |
| 6529 | } |
| 6530 | |
| 6531 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6532 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6533 | if (r->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6534 | sidx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6535 | } else if (r->t == XC_RESULT_VAR) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6536 | BcNum *n; |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6537 | s = zxc_program_num(r, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6538 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6539 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6540 | } else |
Brian Foley | 7454879 | 2019-09-05 10:46:22 +0200 | [diff] [blame] | 6541 | goto exit_nopop; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6542 | } |
| 6543 | |
| 6544 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6545 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6546 | f = xc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6547 | |
| 6548 | if (f->code.len == 0) { |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6549 | BcParse sv_parse; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6550 | char *str; |
| 6551 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6552 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6553 | xc_parse_create(fidx); |
| 6554 | str = *xc_program_str(sidx); |
| 6555 | s = zxc_parse_text_init(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6556 | if (s) goto err; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6557 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6558 | s = zdc_parse_exprs_until_eof(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6559 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6560 | xc_parse_push(DC_INST_POP_EXEC); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6561 | if (G.prs.lex != XC_LEX_EOF) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6562 | s = bc_error_bad_expression(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6563 | xc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6564 | G.prs = sv_parse; // struct copy |
| 6565 | if (s) { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6566 | err: |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6567 | bc_vec_pop_all(&f->code); |
| 6568 | goto exit; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6569 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6570 | } |
| 6571 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6572 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6573 | ip.func = fidx; |
| 6574 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6575 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6576 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6577 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6578 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6579 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6580 | bc_vec_pop(&G.prog.results); |
Brian Foley | 7454879 | 2019-09-05 10:46:22 +0200 | [diff] [blame] | 6581 | exit_nopop: |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6582 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6583 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6584 | #define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6585 | #endif // ENABLE_DC |
| 6586 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6587 | static void xc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6588 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6589 | BcResult res; |
| 6590 | unsigned long val; |
| 6591 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6592 | res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6593 | if (inst == XC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6594 | val = (unsigned long) G.prog.ib_t; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6595 | else if (inst == XC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6596 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6597 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6598 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6599 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6600 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6601 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6602 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6603 | } |
| 6604 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6605 | static BC_STATUS zxc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6606 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6607 | BcResult r, *ptr; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6608 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6609 | BcFunc *func = xc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6610 | char *code = func->code.v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6611 | |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6612 | dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d", |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6613 | ip->func, func->code.len, ip->inst_idx, G.prog.results.len); |
| 6614 | while (ip->inst_idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6615 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6616 | char inst = code[ip->inst_idx++]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6617 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6618 | 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] | 6619 | switch (inst) { |
Denys Vlasenko | 1db367a | 2019-01-04 06:18:00 +0100 | [diff] [blame] | 6620 | case XC_INST_RET: |
| 6621 | if (IS_DC) { // end of '?' reached |
| 6622 | bc_vec_pop(&G.prog.exestack); |
| 6623 | goto read_updated_ip; |
| 6624 | } |
| 6625 | // bc: fall through |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6626 | #if ENABLE_BC |
Denys Vlasenko | 1db367a | 2019-01-04 06:18:00 +0100 | [diff] [blame] | 6627 | case BC_INST_RET0: |
| 6628 | dbg_exec("BC_INST_RET[0]:"); |
| 6629 | s = zbc_program_return(inst); |
| 6630 | goto read_updated_ip; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6631 | case BC_INST_JUMP_ZERO: { |
| 6632 | BcNum *num; |
| 6633 | bool zero; |
| 6634 | dbg_exec("BC_INST_JUMP_ZERO:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6635 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6636 | if (s) RETURN_STATUS(s); |
| 6637 | zero = (bc_num_cmp(num, &G.prog.zero) == 0); |
| 6638 | bc_vec_pop(&G.prog.results); |
| 6639 | if (!zero) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6640 | xc_program_index(code, &ip->inst_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6641 | break; |
| 6642 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6643 | // else: fall through |
| 6644 | } |
| 6645 | case BC_INST_JUMP: { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6646 | size_t idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6647 | size_t *addr = bc_vec_item(&func->labels, idx); |
| 6648 | dbg_exec("BC_INST_JUMP: to %ld", (long)*addr); |
| 6649 | ip->inst_idx = *addr; |
| 6650 | break; |
| 6651 | } |
| 6652 | case BC_INST_CALL: |
| 6653 | dbg_exec("BC_INST_CALL:"); |
| 6654 | s = zbc_program_call(code, &ip->inst_idx); |
| 6655 | goto read_updated_ip; |
| 6656 | case BC_INST_INC_PRE: |
| 6657 | case BC_INST_DEC_PRE: |
| 6658 | case BC_INST_INC_POST: |
| 6659 | case BC_INST_DEC_POST: |
| 6660 | dbg_exec("BC_INST_INCDEC:"); |
| 6661 | s = zbc_program_incdec(inst); |
| 6662 | break; |
| 6663 | case BC_INST_HALT: |
| 6664 | dbg_exec("BC_INST_HALT:"); |
| 6665 | QUIT_OR_RETURN_TO_MAIN; |
| 6666 | break; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6667 | case XC_INST_BOOL_OR: |
| 6668 | case XC_INST_BOOL_AND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6669 | #endif // ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6670 | case XC_INST_REL_EQ: |
| 6671 | case XC_INST_REL_LE: |
| 6672 | case XC_INST_REL_GE: |
| 6673 | case XC_INST_REL_NE: |
| 6674 | case XC_INST_REL_LT: |
| 6675 | case XC_INST_REL_GT: |
| 6676 | dbg_exec("BC_INST_BOOL:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6677 | s = zxc_program_logical(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6678 | break; |
| 6679 | case XC_INST_READ: |
| 6680 | dbg_exec("XC_INST_READ:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6681 | s = zxc_program_read(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6682 | goto read_updated_ip; |
| 6683 | case XC_INST_VAR: |
| 6684 | dbg_exec("XC_INST_VAR:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6685 | s = zxc_program_pushVar(code, &ip->inst_idx, false, false); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6686 | break; |
| 6687 | case XC_INST_ARRAY_ELEM: |
| 6688 | case XC_INST_ARRAY: |
| 6689 | dbg_exec("XC_INST_ARRAY[_ELEM]:"); |
| 6690 | s = zbc_program_pushArray(code, &ip->inst_idx, inst); |
| 6691 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6692 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6693 | case BC_INST_LAST: |
| 6694 | dbg_exec("BC_INST_LAST:"); |
| 6695 | r.t = BC_RESULT_LAST; |
| 6696 | bc_vec_push(&G.prog.results, &r); |
| 6697 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6698 | #endif |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6699 | case XC_INST_IBASE: |
| 6700 | case XC_INST_OBASE: |
| 6701 | case XC_INST_SCALE: |
| 6702 | dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6703 | xc_program_pushGlobal(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6704 | break; |
| 6705 | case XC_INST_SCALE_FUNC: |
| 6706 | case XC_INST_LENGTH: |
| 6707 | case XC_INST_SQRT: |
| 6708 | dbg_exec("BC_INST_builtin:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6709 | s = zxc_program_builtin(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6710 | break; |
| 6711 | case XC_INST_NUM: |
| 6712 | dbg_exec("XC_INST_NUM:"); |
| 6713 | r.t = XC_RESULT_CONSTANT; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6714 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6715 | bc_vec_push(&G.prog.results, &r); |
| 6716 | break; |
| 6717 | case XC_INST_POP: |
| 6718 | dbg_exec("XC_INST_POP:"); |
| 6719 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
| 6720 | s = bc_error_stack_has_too_few_elements(); |
| 6721 | else |
| 6722 | bc_vec_pop(&G.prog.results); |
| 6723 | break; |
| 6724 | case XC_INST_PRINT: |
| 6725 | case XC_INST_PRINT_POP: |
| 6726 | case XC_INST_PRINT_STR: |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 6727 | dbg_exec("XC_INST_PRINTxyz(%d):", inst - XC_INST_PRINT); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6728 | s = zxc_program_print(inst, 0); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6729 | break; |
| 6730 | case XC_INST_STR: |
| 6731 | dbg_exec("XC_INST_STR:"); |
| 6732 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6733 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6734 | bc_vec_push(&G.prog.results, &r); |
| 6735 | break; |
| 6736 | case XC_INST_POWER: |
| 6737 | case XC_INST_MULTIPLY: |
| 6738 | case XC_INST_DIVIDE: |
| 6739 | case XC_INST_MODULUS: |
| 6740 | case XC_INST_PLUS: |
| 6741 | case XC_INST_MINUS: |
| 6742 | dbg_exec("BC_INST_binaryop:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6743 | s = zxc_program_op(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6744 | break; |
| 6745 | case XC_INST_BOOL_NOT: { |
| 6746 | BcNum *num; |
| 6747 | dbg_exec("XC_INST_BOOL_NOT:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6748 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6749 | if (s) RETURN_STATUS(s); |
| 6750 | bc_num_init_DEF_SIZE(&r.d.n); |
| 6751 | if (bc_num_cmp(num, &G.prog.zero) == 0) |
| 6752 | bc_num_one(&r.d.n); |
| 6753 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6754 | xc_program_retire(&r, XC_RESULT_TEMP); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6755 | break; |
| 6756 | } |
| 6757 | case XC_INST_NEG: |
| 6758 | dbg_exec("XC_INST_NEG:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6759 | s = zxc_program_negate(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6760 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6761 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6762 | case BC_INST_ASSIGN_POWER: |
| 6763 | case BC_INST_ASSIGN_MULTIPLY: |
| 6764 | case BC_INST_ASSIGN_DIVIDE: |
| 6765 | case BC_INST_ASSIGN_MODULUS: |
| 6766 | case BC_INST_ASSIGN_PLUS: |
| 6767 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6768 | #endif |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6769 | case XC_INST_ASSIGN: |
| 6770 | dbg_exec("BC_INST_ASSIGNxyz:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6771 | s = zxc_program_assign(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6772 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6773 | #if ENABLE_DC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6774 | case DC_INST_POP_EXEC: |
| 6775 | dbg_exec("DC_INST_POP_EXEC:"); |
| 6776 | bc_vec_pop(&G.prog.exestack); |
| 6777 | goto read_updated_ip; |
| 6778 | case DC_INST_MODEXP: |
| 6779 | dbg_exec("DC_INST_MODEXP:"); |
| 6780 | s = zdc_program_modexp(); |
| 6781 | break; |
| 6782 | case DC_INST_DIVMOD: |
| 6783 | dbg_exec("DC_INST_DIVMOD:"); |
| 6784 | s = zdc_program_divmod(); |
| 6785 | break; |
| 6786 | case DC_INST_EXECUTE: |
| 6787 | case DC_INST_EXEC_COND: |
| 6788 | dbg_exec("DC_INST_EXEC[_COND]:"); |
| 6789 | s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND); |
| 6790 | goto read_updated_ip; |
| 6791 | case DC_INST_PRINT_STACK: { |
| 6792 | size_t idx; |
| 6793 | dbg_exec("DC_INST_PRINT_STACK:"); |
| 6794 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6795 | s = zxc_program_print(XC_INST_PRINT, idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6796 | if (s) break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6797 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6798 | break; |
| 6799 | } |
| 6800 | case DC_INST_CLEAR_STACK: |
| 6801 | dbg_exec("DC_INST_CLEAR_STACK:"); |
| 6802 | bc_vec_pop_all(&G.prog.results); |
| 6803 | break; |
| 6804 | case DC_INST_STACK_LEN: |
| 6805 | dbg_exec("DC_INST_STACK_LEN:"); |
| 6806 | dc_program_stackLen(); |
| 6807 | break; |
| 6808 | case DC_INST_DUPLICATE: |
| 6809 | dbg_exec("DC_INST_DUPLICATE:"); |
| 6810 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
| 6811 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| 6812 | ptr = bc_vec_top(&G.prog.results); |
| 6813 | dc_result_copy(&r, ptr); |
| 6814 | bc_vec_push(&G.prog.results, &r); |
| 6815 | break; |
| 6816 | case DC_INST_SWAP: { |
| 6817 | BcResult *ptr2; |
| 6818 | dbg_exec("DC_INST_SWAP:"); |
| 6819 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
| 6820 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| 6821 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6822 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
| 6823 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6824 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6825 | memcpy(ptr2, &r, sizeof(BcResult)); |
| 6826 | break; |
| 6827 | } |
| 6828 | case DC_INST_ASCIIFY: |
| 6829 | dbg_exec("DC_INST_ASCIIFY:"); |
| 6830 | s = zdc_program_asciify(); |
| 6831 | break; |
| 6832 | case DC_INST_PRINT_STREAM: |
| 6833 | dbg_exec("DC_INST_PRINT_STREAM:"); |
| 6834 | s = zdc_program_printStream(); |
| 6835 | break; |
| 6836 | case DC_INST_LOAD: |
| 6837 | case DC_INST_PUSH_VAR: { |
| 6838 | bool copy = inst == DC_INST_LOAD; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6839 | s = zxc_program_pushVar(code, &ip->inst_idx, true, copy); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6840 | break; |
| 6841 | } |
| 6842 | case DC_INST_PUSH_TO_VAR: { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6843 | char *name = xc_program_name(code, &ip->inst_idx); |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6844 | s = zxc_program_popResultAndCopyToVar(name, BC_TYPE_VAR); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6845 | free(name); |
| 6846 | break; |
| 6847 | } |
| 6848 | case DC_INST_QUIT: |
| 6849 | dbg_exec("DC_INST_QUIT:"); |
| 6850 | if (G.prog.exestack.len <= 2) |
| 6851 | QUIT_OR_RETURN_TO_MAIN; |
| 6852 | bc_vec_npop(&G.prog.exestack, 2); |
| 6853 | goto read_updated_ip; |
| 6854 | case DC_INST_NQUIT: |
| 6855 | dbg_exec("DC_INST_NQUIT:"); |
| 6856 | s = zdc_program_nquit(); |
| 6857 | //goto read_updated_ip; - just fall through to it |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6858 | #endif // ENABLE_DC |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6859 | read_updated_ip: |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6860 | // Instruction stack has changed, read new pointers |
| 6861 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6862 | func = xc_program_func(ip->func); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6863 | code = func->code.v; |
| 6864 | 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] | 6865 | } |
| 6866 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6867 | if (s || G_interrupt) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6868 | xc_program_reset(); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6869 | RETURN_STATUS(s); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6870 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6871 | |
Denys Vlasenko | c5774a3 | 2018-12-17 01:22:53 +0100 | [diff] [blame] | 6872 | fflush_and_check(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6873 | } |
| 6874 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6875 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6876 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6877 | #define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6878 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6879 | static BC_STATUS zxc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6880 | { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6881 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6882 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6883 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6884 | s = zxc_parse_text_init(text); // does the first zxc_lex_next() |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6885 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6886 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6887 | while (G.prs.lex != XC_LEX_EOF) { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6888 | BcInstPtr *ip; |
| 6889 | BcFunc *f; |
| 6890 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6891 | dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6892 | if (IS_BC) { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6893 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6894 | s = zbc_parse_stmt_or_funcdef(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6895 | if (s) goto err; |
| 6896 | |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6897 | // Check that next token is a correct stmt delimiter - |
| 6898 | // disallows "print 1 print 2" and such. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6899 | if (G.prs.lex != BC_LEX_SCOLON |
| 6900 | && G.prs.lex != XC_LEX_NLINE |
| 6901 | && G.prs.lex != XC_LEX_EOF |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6902 | ) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 6903 | bc_error_at("bad statement terminator"); |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6904 | goto err; |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6905 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6906 | // The above logic is fragile. Check these examples: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6907 | // - interactive read() still works |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6908 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6909 | } else { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6910 | #if ENABLE_DC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6911 | s = zdc_parse_expr(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6912 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6913 | } |
| 6914 | if (s || G_interrupt) { |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6915 | err: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6916 | xc_parse_reset(); // includes xc_program_reset() |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6917 | RETURN_STATUS(BC_STATUS_FAILURE); |
| 6918 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6919 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 6920 | dbg_lex("%s:%d executing...", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6921 | s = zxc_program_exec(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6922 | if (s) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6923 | xc_program_reset(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6924 | break; |
| 6925 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6926 | |
| 6927 | ip = (void*)G.prog.exestack.v; |
| 6928 | #if SANITY_CHECKS |
| 6929 | if (G.prog.exestack.len != 1) // should have only main's IP |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 6930 | bb_simple_error_msg_and_die("BUG:call stack"); |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6931 | if (ip->func != BC_PROG_MAIN) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 6932 | bb_simple_error_msg_and_die("BUG:not MAIN"); |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6933 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6934 | f = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6935 | // bc discards strings, constants and code after each |
| 6936 | // top-level statement in the "main program". |
| 6937 | // This prevents "yes 1 | bc" from growing its memory |
| 6938 | // without bound. This can be done because data stack |
| 6939 | // is empty and thus can't hold any references to |
| 6940 | // strings or constants, there is no generated code |
| 6941 | // which can hold references (after we discard one |
| 6942 | // we just executed). Code of functions can have references, |
| 6943 | // but bc stores function strings/constants in per-function |
| 6944 | // storage. |
| 6945 | if (IS_BC) { |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6946 | #if SANITY_CHECKS |
Denys Vlasenko | 7c1c9dc | 2018-12-22 14:18:47 +0100 | [diff] [blame] | 6947 | if (G.prog.results.len != 0) // should be empty |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 6948 | bb_simple_error_msg_and_die("BUG:data stack"); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6949 | #endif |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6950 | IF_BC(bc_vec_pop_all(&f->strs);) |
| 6951 | IF_BC(bc_vec_pop_all(&f->consts);) |
Denys Vlasenko | fc7aa7a | 2019-01-08 18:08:48 +0100 | [diff] [blame] | 6952 | // We are at SCOLON/NLINE, skip it: |
| 6953 | s = zxc_lex_next(); |
| 6954 | if (s) goto err; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6955 | } else { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6956 | if (G.prog.results.len == 0 |
| 6957 | && G.prog.vars.len == 0 |
| 6958 | ) { |
| 6959 | // If stack is empty and no registers exist (TODO: or they are all empty), |
| 6960 | // we can get rid of accumulated strings and constants. |
| 6961 | // In this example dc process should not grow |
| 6962 | // its memory consumption with time: |
| 6963 | // yes 1pc | dc |
| 6964 | IF_DC(bc_vec_pop_all(&G.prog.strs);) |
| 6965 | IF_DC(bc_vec_pop_all(&G.prog.consts);) |
| 6966 | } |
| 6967 | // The code is discarded always (below), thus this example |
| 6968 | // should also not grow its memory consumption with time, |
| 6969 | // even though its data stack is not empty: |
| 6970 | // { echo 1; yes dk; } | dc |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6971 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6972 | // We drop generated and executed code for both bc and dc: |
| 6973 | bc_vec_pop_all(&f->code); |
| 6974 | ip->inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6975 | } |
Denys Vlasenko | 6842c60 | 2019-01-04 05:41:47 +0100 | [diff] [blame] | 6976 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6977 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6978 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6979 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6980 | #define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6981 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6982 | static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6983 | { |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6984 | // So far bc/dc have no way to include a file from another file, |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6985 | // therefore we know G.prs.lex_filename == NULL on entry |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6986 | //const char *sv_file; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6987 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6988 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6989 | G.prs.lex_filename = filename; |
| 6990 | G.prs.lex_input_fp = fp; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 6991 | G.err_line = G.prs.lex_line = 1; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 6992 | dbg_lex("p->lex_line reset to 1"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6993 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6994 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6995 | s = zxc_vm_process(""); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6996 | // We do not stop looping on errors here if reading stdin. |
| 6997 | // Example: start interactive bc and enter "return". |
| 6998 | // It should say "'return' not in a function" |
| 6999 | // but should not exit. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 7000 | } while (G.prs.lex_input_fp == stdin); |
| 7001 | G.prs.lex_filename = NULL; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7002 | RETURN_STATUS(s); |
| 7003 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7004 | #define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7005 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7006 | static BC_STATUS zxc_vm_file(const char *file) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7007 | { |
| 7008 | BcStatus s; |
| 7009 | FILE *fp; |
| 7010 | |
| 7011 | fp = xfopen_for_read(file); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7012 | s = zxc_vm_execute_FILE(fp, file); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7013 | fclose(fp); |
| 7014 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7015 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7016 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7017 | #define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7018 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7019 | #if ENABLE_BC |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 7020 | static void bc_vm_info(void) |
| 7021 | { |
| 7022 | printf("%s "BB_VER"\n" |
Gavin Howard | fa495ce | 2018-12-18 10:03:14 -0700 | [diff] [blame] | 7023 | "Adapted from https://github.com/gavinhoward/bc\n" |
| 7024 | "Original code (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 7025 | , applet_name); |
| 7026 | } |
| 7027 | |
| 7028 | static void bc_args(char **argv) |
| 7029 | { |
| 7030 | unsigned opts; |
| 7031 | int i; |
| 7032 | |
| 7033 | GETOPT_RESET(); |
| 7034 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
| 7035 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
| 7036 | "warn\0" No_argument "w" |
| 7037 | "version\0" No_argument "v" |
| 7038 | "standard\0" No_argument "s" |
| 7039 | "quiet\0" No_argument "q" |
| 7040 | "mathlib\0" No_argument "l" |
| 7041 | "interactive\0" No_argument "i" |
| 7042 | ); |
| 7043 | #else |
| 7044 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
| 7045 | #endif |
| 7046 | if (getenv("POSIXLY_CORRECT")) |
| 7047 | option_mask32 |= BC_FLAG_S; |
| 7048 | |
| 7049 | if (opts & BC_FLAG_V) { |
| 7050 | bc_vm_info(); |
| 7051 | exit(0); |
| 7052 | } |
| 7053 | |
| 7054 | for (i = optind; argv[i]; ++i) |
| 7055 | bc_vec_push(&G.files, argv + i); |
| 7056 | } |
| 7057 | |
| 7058 | static void bc_vm_envArgs(void) |
| 7059 | { |
| 7060 | BcVec v; |
| 7061 | char *buf; |
| 7062 | char *env_args = getenv("BC_ENV_ARGS"); |
| 7063 | |
| 7064 | if (!env_args) return; |
| 7065 | |
| 7066 | G.env_args = xstrdup(env_args); |
| 7067 | buf = G.env_args; |
| 7068 | |
| 7069 | bc_vec_init(&v, sizeof(char *), NULL); |
| 7070 | |
| 7071 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 7072 | bc_vec_push(&v, &buf); |
| 7073 | buf = skip_non_whitespace(buf); |
| 7074 | if (!*buf) |
| 7075 | break; |
| 7076 | *buf++ = '\0'; |
| 7077 | } |
| 7078 | |
| 7079 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 7080 | if (sizeof(int) == sizeof(char*)) { |
| 7081 | bc_vec_push(&v, &const_int_0); |
| 7082 | } else { |
| 7083 | static char *const nullptr = NULL; |
| 7084 | bc_vec_push(&v, &nullptr); |
| 7085 | } |
| 7086 | bc_args(((char **)v.v) - 1); |
| 7087 | |
| 7088 | bc_vec_free(&v); |
| 7089 | } |
| 7090 | |
| 7091 | static const char bc_lib[] ALIGN1 = { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7092 | "scale=20" |
| 7093 | "\n" "define e(x){" |
| 7094 | "\n" "auto b,s,n,r,d,i,p,f,v" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7095 | ////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below |
| 7096 | //^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc: |
| 7097 | //e(-.998896): GNU:.36828580434569428695 |
| 7098 | // above code:.36828580434569428696 |
| 7099 | // actual value:.3682858043456942869594... |
| 7100 | // but for now let's be "GNU compatible" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7101 | "\n" "b=ibase" |
| 7102 | "\n" "ibase=A" |
| 7103 | "\n" "if(x<0){" |
| 7104 | "\n" "n=1" |
| 7105 | "\n" "x=-x" |
| 7106 | "\n" "}" |
| 7107 | "\n" "s=scale" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 7108 | "\n" "r=6+s+.44*x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7109 | "\n" "scale=scale(x)+1" |
| 7110 | "\n" "while(x>1){" |
| 7111 | "\n" "d+=1" |
| 7112 | "\n" "x/=2" |
| 7113 | "\n" "scale+=1" |
| 7114 | "\n" "}" |
| 7115 | "\n" "scale=r" |
| 7116 | "\n" "r=x+1" |
| 7117 | "\n" "p=x" |
| 7118 | "\n" "f=v=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7119 | "\n" "for(i=2;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7120 | "\n" "p*=x" |
| 7121 | "\n" "f*=i" |
| 7122 | "\n" "v=p/f" |
| 7123 | "\n" "r+=v" |
| 7124 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7125 | "\n" "while(d--)r*=r" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7126 | "\n" "scale=s" |
| 7127 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7128 | "\n" "if(n)return(1/r)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7129 | "\n" "return(r/1)" |
| 7130 | "\n" "}" |
| 7131 | "\n" "define l(x){" |
| 7132 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7133 | "\n" "b=ibase" |
| 7134 | "\n" "ibase=A" |
| 7135 | "\n" "if(x<=0){" |
| 7136 | "\n" "r=(1-10^scale)/1" |
| 7137 | "\n" "ibase=b" |
| 7138 | "\n" "return(r)" |
| 7139 | "\n" "}" |
| 7140 | "\n" "s=scale" |
| 7141 | "\n" "scale+=6" |
| 7142 | "\n" "p=2" |
| 7143 | "\n" "while(x>=2){" |
| 7144 | "\n" "p*=2" |
| 7145 | "\n" "x=sqrt(x)" |
| 7146 | "\n" "}" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 7147 | "\n" "while(x<=.5){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7148 | "\n" "p*=2" |
| 7149 | "\n" "x=sqrt(x)" |
| 7150 | "\n" "}" |
| 7151 | "\n" "r=a=(x-1)/(x+1)" |
| 7152 | "\n" "q=a*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7153 | "\n" "v=1" |
| 7154 | "\n" "for(i=3;v;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7155 | "\n" "a*=q" |
| 7156 | "\n" "v=a/i" |
| 7157 | "\n" "r+=v" |
| 7158 | "\n" "}" |
| 7159 | "\n" "r*=p" |
| 7160 | "\n" "scale=s" |
| 7161 | "\n" "ibase=b" |
| 7162 | "\n" "return(r/1)" |
| 7163 | "\n" "}" |
| 7164 | "\n" "define s(x){" |
Denys Vlasenko | 240d7ee | 2018-12-14 11:27:09 +0100 | [diff] [blame] | 7165 | "\n" "auto b,s,r,a,q,i" |
| 7166 | "\n" "if(x<0)return(-s(-x))" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7167 | "\n" "b=ibase" |
| 7168 | "\n" "ibase=A" |
| 7169 | "\n" "s=scale" |
| 7170 | "\n" "scale=1.1*s+2" |
| 7171 | "\n" "a=a(1)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7172 | "\n" "scale=0" |
| 7173 | "\n" "q=(x/a+2)/4" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7174 | "\n" "x-=4*q*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7175 | "\n" "if(q%2)x=-x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7176 | "\n" "scale=s+2" |
| 7177 | "\n" "r=a=x" |
| 7178 | "\n" "q=-x*x" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7179 | "\n" "for(i=3;a;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7180 | "\n" "a*=q/(i*(i-1))" |
| 7181 | "\n" "r+=a" |
| 7182 | "\n" "}" |
| 7183 | "\n" "scale=s" |
| 7184 | "\n" "ibase=b" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7185 | "\n" "return(r/1)" |
| 7186 | "\n" "}" |
| 7187 | "\n" "define c(x){" |
| 7188 | "\n" "auto b,s" |
| 7189 | "\n" "b=ibase" |
| 7190 | "\n" "ibase=A" |
| 7191 | "\n" "s=scale" |
| 7192 | "\n" "scale*=1.2" |
| 7193 | "\n" "x=s(2*a(1)+x)" |
| 7194 | "\n" "scale=s" |
| 7195 | "\n" "ibase=b" |
| 7196 | "\n" "return(x/1)" |
| 7197 | "\n" "}" |
| 7198 | "\n" "define a(x){" |
| 7199 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7200 | "\n" "b=ibase" |
| 7201 | "\n" "ibase=A" |
| 7202 | "\n" "n=1" |
| 7203 | "\n" "if(x<0){" |
| 7204 | "\n" "n=-1" |
| 7205 | "\n" "x=-x" |
| 7206 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7207 | "\n" "if(scale<65){" |
| 7208 | "\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7209 | "\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7210 | "\n" "}" |
| 7211 | "\n" "s=scale" |
| 7212 | "\n" "if(x>.2){" |
| 7213 | "\n" "scale+=5" |
| 7214 | "\n" "a=a(.2)" |
| 7215 | "\n" "}" |
| 7216 | "\n" "scale=s+3" |
| 7217 | "\n" "while(x>.2){" |
| 7218 | "\n" "m+=1" |
| 7219 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7220 | "\n" "}" |
| 7221 | "\n" "r=u=x" |
| 7222 | "\n" "f=-x*x" |
| 7223 | "\n" "t=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7224 | "\n" "for(i=3;t;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7225 | "\n" "u*=f" |
| 7226 | "\n" "t=u/i" |
| 7227 | "\n" "r+=t" |
| 7228 | "\n" "}" |
| 7229 | "\n" "scale=s" |
| 7230 | "\n" "ibase=b" |
| 7231 | "\n" "return((m*a+r)/n)" |
| 7232 | "\n" "}" |
| 7233 | "\n" "define j(n,x){" |
| 7234 | "\n" "auto b,s,o,a,i,v,f" |
| 7235 | "\n" "b=ibase" |
| 7236 | "\n" "ibase=A" |
| 7237 | "\n" "s=scale" |
| 7238 | "\n" "scale=0" |
| 7239 | "\n" "n/=1" |
| 7240 | "\n" "if(n<0){" |
| 7241 | "\n" "n=-n" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7242 | "\n" "o=n%2" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7243 | "\n" "}" |
| 7244 | "\n" "a=1" |
| 7245 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7246 | "\n" "scale=1.5*s" |
| 7247 | "\n" "a=(x^n)/2^n/a" |
| 7248 | "\n" "r=v=1" |
| 7249 | "\n" "f=-x*x/4" |
Denys Vlasenko | c06537d | 2018-12-14 10:10:37 +0100 | [diff] [blame] | 7250 | "\n" "scale+=length(a)-scale(a)" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7251 | "\n" "for(i=1;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7252 | "\n" "v=v*f/i/(n+i)" |
| 7253 | "\n" "r+=v" |
| 7254 | "\n" "}" |
| 7255 | "\n" "scale=s" |
| 7256 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7257 | "\n" "if(o)a=-a" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7258 | "\n" "return(a*r/1)" |
| 7259 | "\n" "}" |
| 7260 | }; |
| 7261 | #endif // ENABLE_BC |
| 7262 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7263 | static BC_STATUS zxc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7264 | { |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7265 | char **fname; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7266 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7267 | size_t i; |
| 7268 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7269 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7270 | if (option_mask32 & BC_FLAG_L) { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7271 | // We know that internal library is not buggy, |
| 7272 | // thus error checking is normally disabled. |
| 7273 | # define DEBUG_LIB 0 |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7274 | s = zxc_vm_process(bc_lib); |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7275 | if (DEBUG_LIB && s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7276 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7277 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7278 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7279 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7280 | fname = (void*)G.files.v; |
| 7281 | for (i = 0; i < G.files.len; i++) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7282 | s = zxc_vm_file(*fname++); |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7283 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) { |
| 7284 | // Debug config, non-interactive mode: |
| 7285 | // return all the way back to main. |
| 7286 | // Non-debug builds do not come here |
| 7287 | // in non-interactive mode, they exit. |
| 7288 | RETURN_STATUS(s); |
| 7289 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7290 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7291 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7292 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7293 | s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7294 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7295 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7296 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7297 | #define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7298 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7299 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7300 | static void xc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7301 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7302 | bc_vec_free(&G.prog.fns); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7303 | IF_BC(bc_vec_free(&G.prog.fn_map);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7304 | bc_vec_free(&G.prog.vars); |
| 7305 | bc_vec_free(&G.prog.var_map); |
| 7306 | bc_vec_free(&G.prog.arrs); |
| 7307 | bc_vec_free(&G.prog.arr_map); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7308 | IF_DC(bc_vec_free(&G.prog.strs);) |
| 7309 | IF_DC(bc_vec_free(&G.prog.consts);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7310 | bc_vec_free(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7311 | bc_vec_free(&G.prog.exestack); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7312 | IF_BC(bc_num_free(&G.prog.last);) |
Denys Vlasenko | db8d607 | 2018-12-27 18:08:30 +0100 | [diff] [blame] | 7313 | //IF_BC(bc_num_free(&G.prog.zero);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7314 | IF_BC(bc_num_free(&G.prog.one);) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7315 | bc_vec_free(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7316 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7317 | #endif |
| 7318 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7319 | static void xc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7320 | { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7321 | BcInstPtr ip; |
| 7322 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7323 | // memset(&G.prog, 0, sizeof(G.prog)); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7324 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7325 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7326 | // G.prog.nchars = G.prog.scale = 0; - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7327 | G.prog.ib_t = 10; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7328 | G.prog.ob_t = 10; |
| 7329 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7330 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);) |
| 7331 | //IF_BC(bc_num_zero(&G.prog.last);) - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7332 | |
Denys Vlasenko | db8d607 | 2018-12-27 18:08:30 +0100 | [diff] [blame] | 7333 | //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7334 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7335 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7336 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);) |
| 7337 | IF_BC(bc_num_one(&G.prog.one);) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7338 | |
| 7339 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7340 | 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] | 7341 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7342 | if (IS_BC) { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7343 | // Names are chosen simply to be distinct and never match |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 7344 | // a valid function name (and be short) |
| 7345 | IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7346 | IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read() |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7347 | } else { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7348 | // in dc, functions have no names |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7349 | xc_program_add_fn(); |
| 7350 | xc_program_add_fn(); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7351 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7352 | |
| 7353 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7354 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7355 | |
| 7356 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7357 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7358 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7359 | IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);) |
| 7360 | 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] | 7361 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7362 | bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL); |
| 7363 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7364 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7365 | bc_char_vec_init(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7366 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7367 | |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 7368 | static unsigned xc_vm_envLen(const char *var) |
| 7369 | { |
| 7370 | char *lenv; |
| 7371 | unsigned len; |
| 7372 | |
| 7373 | lenv = getenv(var); |
| 7374 | len = BC_NUM_PRINT_WIDTH; |
Denys Vlasenko | 29a9043 | 2020-12-29 18:50:56 +0100 | [diff] [blame] | 7375 | if (lenv) { |
| 7376 | len = bb_strtou(lenv, NULL, 10); |
| 7377 | if (len == 0 || len > INT_MAX) |
| 7378 | len = INT_MAX; |
| 7379 | if (errno) |
| 7380 | len = BC_NUM_PRINT_WIDTH; |
| 7381 | } |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 7382 | |
Denys Vlasenko | 29a9043 | 2020-12-29 18:50:56 +0100 | [diff] [blame] | 7383 | // dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7384 | // 1 char wider than bc from the same package. |
| 7385 | // Both default width, and xC_LINE_LENGTH=N are wider: |
| 7386 | // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
| 7387 | // |1234\ | |
| 7388 | // |56 | |
| 7389 | // "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
| 7390 | // |123\ | |
| 7391 | // |456 | |
| 7392 | // Do the same, but it might be a bug in GNU package |
| 7393 | if (IS_BC) |
| 7394 | len--; |
| 7395 | |
| 7396 | if (len < 2) |
| 7397 | len = IS_BC ? BC_NUM_PRINT_WIDTH - 1 : BC_NUM_PRINT_WIDTH; |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 7398 | |
| 7399 | return len; |
| 7400 | } |
| 7401 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7402 | static int xc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7403 | { |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7404 | G.prog.len = xc_vm_envLen(env_len); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7405 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7406 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7407 | xc_program_init(); |
Denys Vlasenko | 5f263f4 | 2018-12-13 22:49:59 +0100 | [diff] [blame] | 7408 | IF_BC(if (IS_BC) bc_vm_envArgs();) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7409 | xc_parse_create(BC_PROG_MAIN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7410 | |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7411 | //TODO: in GNU bc, the check is (isatty(0) && isatty(1)), |
| 7412 | //-i option unconditionally enables this regardless of isatty(): |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7413 | if (isatty(0)) { |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 7414 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7415 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7416 | // With SA_RESTART, most system calls will restart |
| 7417 | // (IOW: they won't fail with EINTR). |
| 7418 | // In particular, this means ^C won't cause |
| 7419 | // stdout to get into "error state" if SIGINT hits |
| 7420 | // within write() syscall. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7421 | // |
| 7422 | // The downside is that ^C while tty input is taken |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7423 | // will only be handled after [Enter] since read() |
| 7424 | // from stdin is not interrupted by ^C either, |
| 7425 | // it restarts, thus fgetc() does not return on ^C. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7426 | // (This problem manifests only if line editing is disabled) |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7427 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7428 | |
| 7429 | // Without SA_RESTART, this exhibits a bug: |
| 7430 | // "while (1) print 1" and try ^C-ing it. |
| 7431 | // Intermittently, instead of returning to input line, |
| 7432 | // you'll get "output error: Interrupted system call" |
| 7433 | // and exit. |
| 7434 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7435 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7436 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7437 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7438 | return 0; // "not a tty" |
| 7439 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7440 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7441 | static BcStatus xc_vm_run(void) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7442 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7443 | BcStatus st = zxc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7444 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7445 | if (G_exiting) // it was actually "halt" or "quit" |
| 7446 | st = EXIT_SUCCESS; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7447 | |
| 7448 | bc_vec_free(&G.files); |
| 7449 | xc_program_free(); |
| 7450 | xc_parse_free(); |
| 7451 | free(G.env_args); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7452 | # if ENABLE_FEATURE_EDITING |
| 7453 | free_line_input_t(G.line_input_state); |
| 7454 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7455 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7456 | #endif |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7457 | dbg_exec("exiting with exitcode %d", st); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7458 | return st; |
| 7459 | } |
| 7460 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7461 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7462 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7463 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7464 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7465 | int is_tty; |
| 7466 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7467 | INIT_G(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7468 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7469 | is_tty = xc_vm_init("BC_LINE_LENGTH"); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7470 | |
| 7471 | bc_args(argv); |
| 7472 | |
| 7473 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7474 | bc_vm_info(); |
| 7475 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7476 | return xc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7477 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7478 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7479 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7480 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7481 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7482 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7483 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7484 | int noscript; |
| 7485 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7486 | INIT_G(); |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7487 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7488 | xc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7489 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7490 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7491 | noscript = BC_FLAG_I; |
| 7492 | for (;;) { |
| 7493 | int n = getopt(argc, argv, "e:f:x"); |
| 7494 | if (n <= 0) |
| 7495 | break; |
| 7496 | switch (n) { |
| 7497 | case 'e': |
| 7498 | noscript = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7499 | n = zxc_vm_process(optarg); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7500 | if (n) return n; |
| 7501 | break; |
| 7502 | case 'f': |
| 7503 | noscript = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7504 | n = zxc_vm_file(optarg); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7505 | if (n) return n; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7506 | break; |
| 7507 | case 'x': |
| 7508 | option_mask32 |= DC_FLAG_X; |
| 7509 | break; |
| 7510 | default: |
| 7511 | bb_show_usage(); |
| 7512 | } |
| 7513 | } |
| 7514 | argv += optind; |
| 7515 | |
| 7516 | while (*argv) { |
| 7517 | noscript = 0; |
| 7518 | bc_vec_push(&G.files, argv++); |
| 7519 | } |
| 7520 | |
| 7521 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7522 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7523 | return xc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7524 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7525 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7526 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 7527 | #endif // DC_BIG |