Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Gavin Howard | fa495ce | 2018-12-18 10:03:14 -0700 | [diff] [blame] | 4 | * Adapted from https://github.com/gavinhoward/bc |
| 5 | * Original code copyright (c) 2018 Gavin D. Howard and contributors. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6 | */ |
| 7 | //config:config BC |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 8 | //config: bool "bc (45 kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 9 | //config: default y |
Denys Vlasenko | cdadad5 | 2018-12-28 15:13:23 +0100 | [diff] [blame] | 10 | //config: select FEATURE_DC_BIG |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 11 | //config: help |
| 12 | //config: bc is a command-line, arbitrary-precision calculator with a |
| 13 | //config: Turing-complete language. See the GNU bc manual |
| 14 | //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] | 15 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 16 | //config: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 17 | //config: This bc has five differences to the GNU bc: |
| 18 | //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] | 19 | //config: 2) Arrays are copied before being passed as arguments to |
| 20 | //config: functions. This behavior is required by the bc spec. |
| 21 | //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] | 22 | //config: the number of elements in the array. This prints "1": |
| 23 | //config: a[0] = 0; length(a[]) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 24 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 25 | //config: that of the unary minus (-) negation operator. This still |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 26 | //config: allows POSIX-compliant scripts to work while somewhat |
| 27 | //config: preserving expected behavior (versus C) and making parsing |
| 28 | //config: easier. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 29 | //config: 5) "read()" accepts expressions, not only numeric literals. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 30 | //config: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 31 | //config:config DC |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 32 | //config: bool "dc (36 kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 33 | //config: default y |
| 34 | //config: help |
| 35 | //config: dc is a reverse-polish notation command-line calculator which |
| 36 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 37 | //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] | 38 | //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] | 39 | //config: |
| 40 | //config: This dc has a few differences from the two above: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 41 | //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] | 42 | //config: the FreeBSD dc does. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 43 | //config: 2) Implements the GNU extensions for divmod ("~") and |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 44 | //config: modular exponentiation ("|"). |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 45 | //config: 3) Implements all FreeBSD extensions, except for "J" and "M". |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 46 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 47 | //config: However, they are implemented differently. When it encounters |
| 48 | //config: whitespace where a register should be, it skips the whitespace. |
| 49 | //config: If the character following is not a lowercase letter, an error |
| 50 | //config: is issued. Otherwise, the register name is parsed by the |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 51 | //config: following regex: [a-z][a-z0-9_]* |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 52 | //config: This generally means that register names will be surrounded by |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 53 | //config: whitespace. Examples: |
| 54 | //config: l idx s temp L index S temp2 < do_thing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 55 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 56 | //config: allowed unless the "-x" option is given. |
| 57 | //config: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 58 | //config:if BC || DC # for menuconfig indenting |
| 59 | //config: |
| 60 | //config:config FEATURE_DC_BIG |
| 61 | //config: bool "Use bc code base for dc (larger, more features)" |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 62 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 63 | //config: |
| 64 | //config:config FEATURE_DC_LIBM |
| 65 | //config: bool "Enable power and exp functions (requires libm)" |
| 66 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 67 | //config: depends on DC && !BC && !FEATURE_DC_BIG |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 68 | //config: help |
| 69 | //config: Enable power and exp functions. |
| 70 | //config: NOTE: This will require libm to be present for linking. |
| 71 | //config: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 72 | //config:config FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 73 | //config: bool "Interactive mode (+4kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 74 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 75 | //config: depends on BC || (DC && FEATURE_DC_BIG) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 76 | //config: help |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 77 | //config: Enable interactive mode: when started on a tty, |
| 78 | //config: ^C interrupts execution and returns to command line, |
| 79 | //config: errors also return to command line instead of exiting, |
| 80 | //config: line editing with history is available. |
| 81 | //config: |
| 82 | //config: With this option off, input can still be taken from tty, |
| 83 | //config: but all errors are fatal, ^C is fatal, |
| 84 | //config: tty is treated exactly the same as any other |
| 85 | //config: standard input (IOW: no line editing). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 86 | //config: |
| 87 | //config:config FEATURE_BC_LONG_OPTIONS |
| 88 | //config: bool "Enable bc/dc long options" |
| 89 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 90 | //config: depends on BC || (DC && FEATURE_DC_BIG) |
| 91 | //config: |
| 92 | //config:endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 93 | |
| 94 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 95 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 96 | |
| 97 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 98 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 99 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 100 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 101 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 09fe0aa | 2018-12-18 16:32:25 +0100 | [diff] [blame] | 102 | //usage: "[-sqlw] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 103 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 104 | //usage:#define bc_full_usage "\n" |
| 105 | //usage: "\nArbitrary precision calculator" |
| 106 | //usage: "\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 107 | ///////: "\n -i Interactive" - has no effect for now |
| 108 | //usage: "\n -q Quiet" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 109 | //usage: "\n -l Load standard math library" |
| 110 | //usage: "\n -s Be POSIX compatible" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 111 | //usage: "\n -w Warn if extensions are used" |
| 112 | ///////: "\n -v Version" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 113 | //usage: "\n" |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 114 | //usage: "\n$BC_LINE_LENGTH changes output width" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 115 | //usage: |
| 116 | //usage:#define bc_example_usage |
| 117 | //usage: "3 + 4.129\n" |
| 118 | //usage: "1903 - 2893\n" |
| 119 | //usage: "-129 * 213.28935\n" |
| 120 | //usage: "12 / -1932\n" |
| 121 | //usage: "12 % 12\n" |
| 122 | //usage: "34 ^ 189\n" |
| 123 | //usage: "scale = 13\n" |
| 124 | //usage: "ibase = 2\n" |
| 125 | //usage: "obase = A\n" |
| 126 | //usage: |
| 127 | //usage:#define dc_trivial_usage |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 128 | //usage: IF_FEATURE_DC_BIG("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 129 | //usage: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 130 | //usage:#define dc_full_usage "\n" |
| 131 | //usage: "\nTiny RPN calculator. Operations:" |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 132 | //usage: "\n+, -, *, /, %, ~, ^," IF_FEATURE_DC_BIG(" |,") |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 133 | //usage: "\np - print top of the stack without popping" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 134 | //usage: "\nf - print entire stack" |
| 135 | //usage: "\nk - pop the value and set the precision" |
| 136 | //usage: "\ni - pop the value and set input radix" |
| 137 | //usage: "\no - pop the value and set output radix" |
| 138 | //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] | 139 | //usage: |
| 140 | //usage:#define dc_example_usage |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 141 | //usage: "$ dc -e'2 2 + p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 142 | //usage: "4\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 143 | //usage: "$ dc -e'8 8 \\* 2 2 + / p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 144 | //usage: "16\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 145 | //usage: "$ dc -e'0 1 & p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 146 | //usage: "0\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 147 | //usage: "$ dc -e'0 1 | p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 148 | //usage: "1\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 149 | //usage: "$ echo '72 9 / 8 * p' | dc\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 150 | //usage: "64\n" |
| 151 | |
| 152 | #include "libbb.h" |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 153 | #include "common_bufsiz.h" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 154 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 155 | #if !ENABLE_BC && !ENABLE_FEATURE_DC_BIG |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 156 | # include "dc.c" |
| 157 | #else |
| 158 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 159 | #define DEBUG_LEXER 0 |
| 160 | #define DEBUG_COMPILE 0 |
| 161 | #define DEBUG_EXEC 0 |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 162 | // This can be left enabled for production as well: |
| 163 | #define SANITY_CHECKS 1 |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 164 | |
| 165 | #if DEBUG_LEXER |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 166 | static uint8_t lex_indent; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 167 | #define dbg_lex(...) \ |
| 168 | do { \ |
| 169 | fprintf(stderr, "%*s", lex_indent, ""); \ |
| 170 | bb_error_msg(__VA_ARGS__); \ |
| 171 | } while (0) |
| 172 | #define dbg_lex_enter(...) \ |
| 173 | do { \ |
| 174 | dbg_lex(__VA_ARGS__); \ |
| 175 | lex_indent++; \ |
| 176 | } while (0) |
| 177 | #define dbg_lex_done(...) \ |
| 178 | do { \ |
| 179 | lex_indent--; \ |
| 180 | dbg_lex(__VA_ARGS__); \ |
| 181 | } while (0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 182 | #else |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 183 | # define dbg_lex(...) ((void)0) |
| 184 | # define dbg_lex_enter(...) ((void)0) |
| 185 | # define dbg_lex_done(...) ((void)0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 186 | #endif |
| 187 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 188 | #if DEBUG_COMPILE |
| 189 | # define dbg_compile(...) bb_error_msg(__VA_ARGS__) |
| 190 | #else |
| 191 | # define dbg_compile(...) ((void)0) |
| 192 | #endif |
| 193 | |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 194 | #if DEBUG_EXEC |
| 195 | # define dbg_exec(...) bb_error_msg(__VA_ARGS__) |
| 196 | #else |
| 197 | # define dbg_exec(...) ((void)0) |
| 198 | #endif |
| 199 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 200 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 201 | BC_STATUS_SUCCESS = 0, |
| 202 | BC_STATUS_FAILURE = 1, |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 203 | BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 204 | } BcStatus; |
| 205 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 206 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 207 | #define BC_VEC_START_CAP (1 << 5) |
| 208 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 209 | typedef void (*BcVecFree)(void *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 210 | |
| 211 | typedef struct BcVec { |
| 212 | char *v; |
| 213 | size_t len; |
| 214 | size_t cap; |
| 215 | size_t size; |
| 216 | BcVecFree dtor; |
| 217 | } BcVec; |
| 218 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 219 | typedef signed char BcDig; |
| 220 | |
| 221 | typedef struct BcNum { |
| 222 | BcDig *restrict num; |
| 223 | size_t rdx; |
| 224 | size_t len; |
| 225 | size_t cap; |
| 226 | bool neg; |
| 227 | } BcNum; |
| 228 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 229 | #define BC_NUM_MAX_IBASE ((unsigned long) 16) |
| 230 | // larger value might speed up BIGNUM calculations a bit: |
| 231 | #define BC_NUM_DEF_SIZE (16) |
| 232 | #define BC_NUM_PRINT_WIDTH (69) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 233 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 234 | #define BC_NUM_KARATSUBA_LEN (32) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 235 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 236 | typedef enum BcInst { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 237 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 238 | BC_INST_INC_PRE, |
| 239 | BC_INST_DEC_PRE, |
| 240 | BC_INST_INC_POST, |
| 241 | BC_INST_DEC_POST, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 242 | #endif |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 243 | XC_INST_NEG, // order |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 244 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 245 | XC_INST_REL_EQ, // should |
| 246 | XC_INST_REL_LE, // match |
| 247 | XC_INST_REL_GE, // LEX |
| 248 | XC_INST_REL_NE, // constants |
| 249 | XC_INST_REL_LT, // for |
| 250 | XC_INST_REL_GT, // these |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 251 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 252 | XC_INST_POWER, // operations |
| 253 | XC_INST_MULTIPLY, // | |
| 254 | XC_INST_DIVIDE, // | |
| 255 | XC_INST_MODULUS, // | |
| 256 | XC_INST_PLUS, // | |
| 257 | XC_INST_MINUS, // | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 258 | |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 259 | XC_INST_BOOL_NOT, // | |
| 260 | XC_INST_BOOL_OR, // | |
| 261 | XC_INST_BOOL_AND, // | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 262 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 263 | BC_INST_ASSIGN_POWER, // | |
| 264 | BC_INST_ASSIGN_MULTIPLY,// | |
| 265 | BC_INST_ASSIGN_DIVIDE, // | |
| 266 | BC_INST_ASSIGN_MODULUS, // | |
| 267 | BC_INST_ASSIGN_PLUS, // | |
| 268 | BC_INST_ASSIGN_MINUS, // | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 269 | #endif |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 270 | XC_INST_ASSIGN, // V |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 271 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 272 | XC_INST_NUM, |
| 273 | XC_INST_VAR, |
| 274 | XC_INST_ARRAY_ELEM, |
| 275 | XC_INST_ARRAY, |
| 276 | XC_INST_SCALE_FUNC, |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 277 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 278 | XC_INST_IBASE, // order of these constans should match other enums |
| 279 | XC_INST_OBASE, // order of these constans should match other enums |
| 280 | XC_INST_SCALE, // order of these constans should match other enums |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 281 | 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] | 282 | XC_INST_LENGTH, |
| 283 | XC_INST_READ, |
| 284 | XC_INST_SQRT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 285 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 286 | XC_INST_PRINT, |
| 287 | XC_INST_PRINT_POP, |
| 288 | XC_INST_STR, |
| 289 | XC_INST_PRINT_STR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 290 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 291 | #if ENABLE_BC |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 292 | BC_INST_HALT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 293 | BC_INST_JUMP, |
| 294 | BC_INST_JUMP_ZERO, |
| 295 | |
| 296 | BC_INST_CALL, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 297 | BC_INST_RET0, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 298 | #endif |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 299 | XC_INST_RET, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 300 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 301 | XC_INST_POP, |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 302 | #if ENABLE_DC |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 303 | DC_INST_POP_EXEC, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 304 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 305 | DC_INST_MODEXP, |
| 306 | DC_INST_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 307 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 308 | DC_INST_EXECUTE, |
| 309 | DC_INST_EXEC_COND, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 310 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 311 | DC_INST_ASCIIFY, |
| 312 | DC_INST_PRINT_STREAM, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 313 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 314 | DC_INST_PRINT_STACK, |
| 315 | DC_INST_CLEAR_STACK, |
| 316 | DC_INST_STACK_LEN, |
| 317 | DC_INST_DUPLICATE, |
| 318 | DC_INST_SWAP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 319 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 320 | DC_INST_LOAD, |
| 321 | DC_INST_PUSH_VAR, |
| 322 | DC_INST_PUSH_TO_VAR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 323 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 324 | DC_INST_QUIT, |
| 325 | DC_INST_NQUIT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 326 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 327 | DC_INST_INVALID = -1, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 328 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 329 | } BcInst; |
| 330 | |
| 331 | typedef struct BcId { |
| 332 | char *name; |
| 333 | size_t idx; |
| 334 | } BcId; |
| 335 | |
| 336 | typedef struct BcFunc { |
| 337 | BcVec code; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 338 | IF_BC(BcVec labels;) |
| 339 | IF_BC(BcVec autos;) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 340 | IF_BC(BcVec strs;) |
| 341 | IF_BC(BcVec consts;) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 342 | IF_BC(size_t nparams;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 343 | } BcFunc; |
| 344 | |
| 345 | typedef enum BcResultType { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 346 | XC_RESULT_TEMP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 347 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 348 | XC_RESULT_VAR, |
| 349 | XC_RESULT_ARRAY_ELEM, |
| 350 | XC_RESULT_ARRAY, |
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_STR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 353 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 354 | //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct, |
| 355 | XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE |
| 356 | XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE |
| 357 | XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 358 | 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] | 359 | XC_RESULT_CONSTANT, |
| 360 | IF_BC(BC_RESULT_ONE,) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 361 | } BcResultType; |
| 362 | |
| 363 | typedef union BcResultData { |
| 364 | BcNum n; |
| 365 | BcVec v; |
| 366 | BcId id; |
| 367 | } BcResultData; |
| 368 | |
| 369 | typedef struct BcResult { |
| 370 | BcResultType t; |
| 371 | BcResultData d; |
| 372 | } BcResult; |
| 373 | |
| 374 | typedef struct BcInstPtr { |
| 375 | size_t func; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 376 | size_t inst_idx; |
| 377 | IF_BC(size_t results_len_before_call;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 378 | } BcInstPtr; |
| 379 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 380 | typedef enum BcLexType { |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 381 | XC_LEX_EOF, |
| 382 | XC_LEX_INVALID, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 383 | |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 384 | XC_LEX_NLINE, |
| 385 | XC_LEX_WHITESPACE, |
| 386 | XC_LEX_STR, |
| 387 | XC_LEX_NAME, |
| 388 | XC_LEX_NUMBER, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 389 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 390 | XC_LEX_1st_op, |
| 391 | XC_LEX_NEG = XC_LEX_1st_op, // order |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 392 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 393 | XC_LEX_OP_REL_EQ, // should |
| 394 | XC_LEX_OP_REL_LE, // match |
| 395 | XC_LEX_OP_REL_GE, // INST |
| 396 | XC_LEX_OP_REL_NE, // constants |
| 397 | XC_LEX_OP_REL_LT, // for |
| 398 | XC_LEX_OP_REL_GT, // these |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 399 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 400 | XC_LEX_OP_POWER, // operations |
| 401 | XC_LEX_OP_MULTIPLY, // | |
| 402 | XC_LEX_OP_DIVIDE, // | |
| 403 | XC_LEX_OP_MODULUS, // | |
| 404 | XC_LEX_OP_PLUS, // | |
| 405 | XC_LEX_OP_MINUS, // | |
| 406 | XC_LEX_OP_last = XC_LEX_OP_MINUS, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 407 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 408 | BC_LEX_OP_BOOL_NOT, // | |
| 409 | BC_LEX_OP_BOOL_OR, // | |
| 410 | BC_LEX_OP_BOOL_AND, // | |
| 411 | |
| 412 | BC_LEX_OP_ASSIGN_POWER, // | |
| 413 | BC_LEX_OP_ASSIGN_MULTIPLY, // | |
| 414 | BC_LEX_OP_ASSIGN_DIVIDE, // | |
| 415 | BC_LEX_OP_ASSIGN_MODULUS, // | |
| 416 | BC_LEX_OP_ASSIGN_PLUS, // | |
| 417 | BC_LEX_OP_ASSIGN_MINUS, // | |
| 418 | |
| 419 | BC_LEX_OP_ASSIGN, // V |
| 420 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 421 | BC_LEX_OP_INC, |
| 422 | BC_LEX_OP_DEC, |
| 423 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 424 | BC_LEX_LPAREN, // () are 0x28 and 0x29 |
| 425 | 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] | 426 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 427 | BC_LEX_LBRACKET, // [] are 0x5B and 0x5D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 428 | BC_LEX_COMMA, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 429 | 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] | 430 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 431 | BC_LEX_LBRACE, // {} are 0x7B and 0x7D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 432 | BC_LEX_SCOLON, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 433 | 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] | 434 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 435 | BC_LEX_KEY_1st_keyword, |
| 436 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 437 | BC_LEX_KEY_BREAK, |
| 438 | BC_LEX_KEY_CONTINUE, |
| 439 | BC_LEX_KEY_DEFINE, |
| 440 | BC_LEX_KEY_ELSE, |
| 441 | BC_LEX_KEY_FOR, |
| 442 | BC_LEX_KEY_HALT, |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 443 | // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct, |
| 444 | BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE |
| 445 | BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 446 | BC_LEX_KEY_IF, |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 447 | IF_BC(BC_LEX_KEY_LAST,) // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 448 | BC_LEX_KEY_LENGTH, |
| 449 | BC_LEX_KEY_LIMITS, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 450 | BC_LEX_KEY_PRINT, |
| 451 | BC_LEX_KEY_QUIT, |
| 452 | BC_LEX_KEY_READ, |
| 453 | BC_LEX_KEY_RETURN, |
| 454 | BC_LEX_KEY_SCALE, |
| 455 | BC_LEX_KEY_SQRT, |
| 456 | BC_LEX_KEY_WHILE, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 457 | #endif // ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 458 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 459 | #if ENABLE_DC |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 460 | DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1, |
| 461 | DC_LEX_OP_ASSIGN, |
| 462 | |
| 463 | DC_LEX_LPAREN, |
| 464 | DC_LEX_SCOLON, |
| 465 | DC_LEX_READ, |
| 466 | DC_LEX_IBASE, |
| 467 | DC_LEX_SCALE, |
| 468 | DC_LEX_OBASE, |
| 469 | DC_LEX_LENGTH, |
| 470 | DC_LEX_PRINT, |
| 471 | DC_LEX_QUIT, |
| 472 | DC_LEX_SQRT, |
| 473 | DC_LEX_LBRACE, |
| 474 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 475 | DC_LEX_EQ_NO_REG, |
| 476 | DC_LEX_OP_MODEXP, |
| 477 | DC_LEX_OP_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 478 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 479 | DC_LEX_COLON, |
| 480 | DC_LEX_ELSE, |
| 481 | DC_LEX_EXECUTE, |
| 482 | DC_LEX_PRINT_STACK, |
| 483 | DC_LEX_CLEAR_STACK, |
| 484 | DC_LEX_STACK_LEVEL, |
| 485 | DC_LEX_DUPLICATE, |
| 486 | DC_LEX_SWAP, |
| 487 | DC_LEX_POP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 488 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 489 | DC_LEX_ASCIIFY, |
| 490 | DC_LEX_PRINT_STREAM, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 491 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 492 | // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct, |
| 493 | DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE |
| 494 | DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE |
| 495 | DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE |
| 496 | DC_LEX_LOAD, |
| 497 | DC_LEX_LOAD_POP, |
| 498 | DC_LEX_STORE_PUSH, |
| 499 | DC_LEX_PRINT_POP, |
| 500 | DC_LEX_NQUIT, |
| 501 | DC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 502 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 503 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 504 | // must match order of BC_LEX_KEY_foo etc above |
| 505 | #if ENABLE_BC |
| 506 | struct BcLexKeyword { |
| 507 | char name8[8]; |
| 508 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 509 | #define LEX_KW_ENTRY(a, b) \ |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 510 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 511 | static const struct BcLexKeyword bc_lex_kws[20] = { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 512 | LEX_KW_ENTRY("auto" , 1), // 0 |
| 513 | LEX_KW_ENTRY("break" , 1), // 1 |
| 514 | LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 515 | LEX_KW_ENTRY("define" , 1), // 3 |
| 516 | LEX_KW_ENTRY("else" , 0), // 4 |
| 517 | LEX_KW_ENTRY("for" , 1), // 5 |
| 518 | LEX_KW_ENTRY("halt" , 0), // 6 |
| 519 | LEX_KW_ENTRY("ibase" , 1), // 7 |
| 520 | LEX_KW_ENTRY("obase" , 1), // 8 |
| 521 | LEX_KW_ENTRY("if" , 1), // 9 |
| 522 | LEX_KW_ENTRY("last" , 0), // 10 |
| 523 | LEX_KW_ENTRY("length" , 1), // 11 |
| 524 | LEX_KW_ENTRY("limits" , 0), // 12 |
| 525 | LEX_KW_ENTRY("print" , 0), // 13 |
| 526 | LEX_KW_ENTRY("quit" , 1), // 14 |
| 527 | LEX_KW_ENTRY("read" , 0), // 15 |
| 528 | LEX_KW_ENTRY("return" , 1), // 16 |
| 529 | LEX_KW_ENTRY("scale" , 1), // 17 |
| 530 | LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 531 | LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 532 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 533 | #undef LEX_KW_ENTRY |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 534 | #define STRING_else (bc_lex_kws[4].name8) |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 535 | #define STRING_for (bc_lex_kws[5].name8) |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 536 | #define STRING_if (bc_lex_kws[9].name8) |
| 537 | #define STRING_while (bc_lex_kws[19].name8) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 538 | enum { |
| 539 | POSIX_KWORD_MASK = 0 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 540 | | (1 << 0) // 0 |
| 541 | | (1 << 1) // 1 |
| 542 | | (0 << 2) // 2 |
| 543 | | (1 << 3) // 3 |
| 544 | | (0 << 4) // 4 |
| 545 | | (1 << 5) // 5 |
| 546 | | (0 << 6) // 6 |
| 547 | | (1 << 7) // 7 |
| 548 | | (1 << 8) // 8 |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 549 | | (1 << 9) // 9 |
| 550 | | (0 << 10) // 10 |
| 551 | | (1 << 11) // 11 |
| 552 | | (0 << 12) // 12 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 553 | | (0 << 13) // 13 |
| 554 | | (1 << 14) // 14 |
| 555 | | (0 << 15) // 15 |
| 556 | | (1 << 16) // 16 |
| 557 | | (1 << 17) // 17 |
| 558 | | (1 << 18) // 18 |
| 559 | | (1 << 19) // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 560 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 561 | #define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 562 | |
| 563 | // This is a bit array that corresponds to token types. An entry is |
| 564 | // true if the token is valid in an expression, false otherwise. |
| 565 | // Used to figure out when expr parsing should stop *without error message* |
| 566 | // - 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] | 567 | // 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] | 568 | // by later processing. |
| 569 | enum { |
| 570 | #define EXBITS(a,b,c,d,e,f,g,h) \ |
| 571 | ((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] | 572 | BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz: |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 573 | + (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] | 574 | + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ * |
| 575 | + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^= |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 576 | + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ -- |
| 577 | + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; } |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 578 | + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase |
| 579 | + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read |
| 580 | + (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] | 581 | #undef EXBITS |
| 582 | }; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 583 | static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 584 | { |
| 585 | #if ULONG_MAX > 0xffffffff |
| 586 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 587 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 588 | #else |
| 589 | // 32-bit version |
| 590 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 591 | if (i >= 32) { |
| 592 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 593 | i &= 31; |
| 594 | } |
| 595 | return m & (1UL << i); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 596 | #endif |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // This is an array of data for operators that correspond to |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 600 | // [XC_LEX_1st_op...] token types. |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 601 | static const uint8_t bc_parse_ops[] ALIGN1 = { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 602 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 603 | OP(1, false), // neg |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 604 | 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] | 605 | OP(2, false), // pow |
| 606 | OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod |
| 607 | OP(4, true ), OP( 4, true ), // + - |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 608 | OP(1, false), // not |
| 609 | OP(7, true ), OP( 7, true ), // or and |
| 610 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 611 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 612 | OP(0, false), OP( 0, false ), // inc dec |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 613 | #undef OP |
| 614 | }; |
| 615 | #define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f) |
| 616 | #define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10) |
| 617 | #endif // ENABLE_BC |
| 618 | |
| 619 | #if ENABLE_DC |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 620 | static const //BcLexType - should be this type |
| 621 | uint8_t |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 622 | dc_char_to_LEX[] ALIGN1 = { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 623 | // %&'( |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 624 | 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] | 625 | // )*+, |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 626 | 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] | 627 | // -./ |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 628 | XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 629 | // 0123456789 |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 630 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 631 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 632 | XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 633 | // :;<=>?@ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 634 | DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ, |
| 635 | XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 636 | // ABCDEFGH |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 637 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 638 | 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] | 639 | // IJKLMNOP |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 640 | DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP, |
| 641 | XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 642 | // QRSTUVWXY |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 643 | DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID, |
| 644 | XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 645 | // Z[\] |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 646 | DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 647 | // ^_` |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 648 | XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 649 | // abcdefgh |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 650 | DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE, |
| 651 | 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] | 652 | // ijklmnop |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 653 | 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] | 654 | 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] | 655 | // qrstuvwx |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 656 | DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID, |
| 657 | XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 658 | // yz |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 659 | XC_LEX_INVALID, DC_LEX_STACK_LEVEL, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 660 | // {|}~ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 661 | 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] | 662 | }; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 663 | 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] | 664 | int8_t |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 665 | dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz: |
| 666 | XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY |
| 667 | XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS |
| 668 | 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] | 669 | XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT |
| 670 | DC_INST_INVALID, // DC_LEX_OP_ASSIGN |
| 671 | XC_INST_REL_GT, // DC_LEX_LPAREN |
| 672 | DC_INST_INVALID, // DC_LEX_SCOLON |
| 673 | DC_INST_INVALID, // DC_LEX_READ |
| 674 | XC_INST_IBASE, // DC_LEX_IBASE |
| 675 | XC_INST_SCALE, // DC_LEX_SCALE |
| 676 | XC_INST_OBASE, // DC_LEX_OBASE |
| 677 | XC_INST_LENGTH, // DC_LEX_LENGTH |
| 678 | XC_INST_PRINT, // DC_LEX_PRINT |
| 679 | DC_INST_QUIT, // DC_LEX_QUIT |
| 680 | XC_INST_SQRT, // DC_LEX_SQRT |
| 681 | XC_INST_REL_GE, // DC_LEX_LBRACE |
| 682 | XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 683 | DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD |
| 684 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE |
| 685 | DC_INST_EXECUTE, // DC_LEX_EXECUTE |
| 686 | DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK |
| 687 | DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE |
| 688 | DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP |
| 689 | DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM |
| 690 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE |
| 691 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD |
| 692 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH |
| 693 | XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT |
| 694 | XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 695 | // DC_INST_INVALID in this table either means that corresponding LEX |
| 696 | // is not possible for dc, or that it does not compile one-to-one |
| 697 | // to a single INST. |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 698 | }; |
| 699 | #endif // ENABLE_DC |
| 700 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 701 | typedef struct BcParse { |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 702 | smallint lex; // was BcLexType // first member is most used |
| 703 | smallint lex_last; // was BcLexType |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 704 | size_t lex_line; |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 705 | const char *lex_inbuf; |
| 706 | 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] | 707 | const char *lex_filename; |
| 708 | FILE *lex_input_fp; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 709 | BcVec lex_strnumbuf; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 710 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 711 | BcFunc *func; |
| 712 | size_t fidx; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 713 | IF_BC(size_t in_funcdef;) |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 714 | IF_BC(BcVec exits;) |
| 715 | IF_BC(BcVec conds;) |
| 716 | IF_BC(BcVec ops;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 717 | } BcParse; |
| 718 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 719 | typedef struct BcProgram { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 720 | size_t len; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 721 | size_t nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 722 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 723 | size_t scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 724 | size_t ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 725 | size_t ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 726 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 727 | BcVec results; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 728 | BcVec exestack; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 729 | |
| 730 | BcVec fns; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 731 | IF_BC(BcVec fn_map;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 732 | |
| 733 | BcVec vars; |
| 734 | BcVec var_map; |
| 735 | |
| 736 | BcVec arrs; |
| 737 | BcVec arr_map; |
| 738 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 739 | IF_DC(BcVec strs;) |
| 740 | IF_DC(BcVec consts;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 741 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 742 | BcNum zero; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 743 | IF_BC(BcNum one;) |
| 744 | IF_BC(BcNum last;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 745 | } BcProgram; |
| 746 | |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 747 | struct globals { |
| 748 | BcParse prs; // first member is most used |
| 749 | |
| 750 | // For error messages. Can be set to current parsed line, |
| 751 | // or [TODO] to current executing line (can be before last parsed one) |
| 752 | unsigned err_line; |
| 753 | |
| 754 | BcVec input_buffer; |
| 755 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 756 | IF_FEATURE_BC_INTERACTIVE(smallint ttyin;) |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 757 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
| 758 | |
| 759 | BcProgram prog; |
| 760 | |
| 761 | BcVec files; |
| 762 | |
| 763 | char *env_args; |
| 764 | |
| 765 | #if ENABLE_FEATURE_EDITING |
| 766 | line_input_t *line_input_state; |
| 767 | #endif |
| 768 | } FIX_ALIASING; |
| 769 | #define G (*ptr_to_globals) |
| 770 | #define INIT_G() do { \ |
| 771 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 772 | } while (0) |
| 773 | #define FREE_G() do { \ |
| 774 | FREE_PTR_TO_GLOBALS(); \ |
| 775 | } while (0) |
| 776 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 777 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
| 778 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 779 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 780 | # define G_interrupt bb_got_signal |
| 781 | # define G_ttyin G.ttyin |
| 782 | #else |
| 783 | # define G_interrupt 0 |
| 784 | # define G_ttyin 0 |
| 785 | #endif |
| 786 | #if ENABLE_FEATURE_CLEAN_UP |
| 787 | # define G_exiting G.exiting |
| 788 | #else |
| 789 | # define G_exiting 0 |
| 790 | #endif |
| 791 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
| 792 | #define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b')) |
| 793 | |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 794 | #if ENABLE_BC |
| 795 | # define BC_PARSE_REL (1 << 0) |
| 796 | # define BC_PARSE_PRINT (1 << 1) |
| 797 | # define BC_PARSE_ARRAY (1 << 2) |
| 798 | # define BC_PARSE_NOCALL (1 << 3) |
| 799 | #endif |
| 800 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 801 | #define BC_PROG_MAIN (0) |
| 802 | #define BC_PROG_READ (1) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 803 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 804 | #define BC_PROG_REQ_FUNCS (2) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 805 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 806 | |
| 807 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 808 | #define BC_PROG_NUM(r, n) \ |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 809 | ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 810 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 811 | #define BC_FLAG_W (1 << 0) |
| 812 | #define BC_FLAG_V (1 << 1) |
| 813 | #define BC_FLAG_S (1 << 2) |
| 814 | #define BC_FLAG_Q (1 << 3) |
| 815 | #define BC_FLAG_L (1 << 4) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 816 | #define BC_FLAG_I ((1 << 5) * ENABLE_DC) |
| 817 | #define DC_FLAG_X ((1 << 6) * ENABLE_DC) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 818 | |
| 819 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 820 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 821 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 822 | #define BC_MAX_OBASE ((unsigned) 999) |
| 823 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 824 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 825 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 826 | #define BC_MAX_NUM BC_MAX_STRING |
| 827 | // Unused apart from "limits" message. Just show a "biggish number" there. |
| 828 | //#define BC_MAX_NAME BC_MAX_STRING |
| 829 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 830 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 831 | #define BC_MAX_NAME_STR "999999999" |
| 832 | #define BC_MAX_EXP_STR "999999999" |
| 833 | #define BC_MAX_VARS_STR "999999999" |
| 834 | |
| 835 | #define BC_MAX_OBASE_STR "999" |
| 836 | |
| 837 | #if INT_MAX == 2147483647 |
| 838 | # define BC_MAX_DIM_STR "2147483647" |
| 839 | #elif INT_MAX == 9223372036854775807 |
| 840 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 841 | #else |
| 842 | # error Strange INT_MAX |
| 843 | #endif |
| 844 | |
| 845 | #if UINT_MAX == 4294967295 |
| 846 | # define BC_MAX_SCALE_STR "4294967295" |
| 847 | # define BC_MAX_STRING_STR "4294967294" |
| 848 | #elif UINT_MAX == 18446744073709551615 |
| 849 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 850 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 851 | #else |
| 852 | # error Strange UINT_MAX |
| 853 | #endif |
| 854 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 855 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 856 | // In configurations where errors abort instead of propagating error |
| 857 | // return code up the call chain, functions returning BC_STATUS |
| 858 | // actually don't return anything, they always succeed and return "void". |
| 859 | // A macro wrapper is provided, which makes this statement work: |
| 860 | // s = zbc_func(...) |
| 861 | // and makes it visible to the compiler that s is always zero, |
| 862 | // allowing compiler to optimize dead code after the statement. |
| 863 | // |
| 864 | // To make code more readable, each such function has a "z" |
| 865 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. |
| 866 | // |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 867 | #if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 868 | # define ERRORS_ARE_FATAL 0 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 869 | # define ERRORFUNC /*nothing*/ |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 870 | # define IF_ERROR_RETURN_POSSIBLE(a) a |
| 871 | # define BC_STATUS BcStatus |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 872 | # define RETURN_STATUS(v) return (v) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 873 | # define COMMA_SUCCESS /*nothing*/ |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 874 | #else |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 875 | # define ERRORS_ARE_FATAL 1 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 876 | # define ERRORFUNC NORETURN |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 877 | # define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/ |
| 878 | # define BC_STATUS void |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 879 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 880 | # define COMMA_SUCCESS ,BC_STATUS_SUCCESS |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 881 | #endif |
| 882 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 883 | // |
| 884 | // Utility routines |
| 885 | // |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 886 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 887 | static void fflush_and_check(void) |
| 888 | { |
| 889 | fflush_all(); |
| 890 | if (ferror(stdout) || ferror(stderr)) |
| 891 | bb_perror_msg_and_die("output error"); |
| 892 | } |
| 893 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 894 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 895 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 896 | do { \ |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 897 | 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] | 898 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 899 | return BC_STATUS_FAILURE; \ |
| 900 | } while (0) |
| 901 | #else |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 902 | static void quit(void) NORETURN; |
| 903 | static void quit(void) |
| 904 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 905 | if (ferror(stdin)) |
| 906 | bb_perror_msg_and_die("input error"); |
| 907 | fflush_and_check(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 908 | dbg_exec("quit(): exiting with exitcode SUCCESS"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 909 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 910 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 911 | #define QUIT_OR_RETURN_TO_MAIN quit() |
| 912 | #endif |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 913 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 914 | static void bc_verror_msg(const char *fmt, va_list p) |
| 915 | { |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 916 | const char *sv = sv; // for compiler |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 917 | if (G.prs.lex_filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 918 | sv = applet_name; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 919 | applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.lex_filename, G.err_line); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 920 | } |
| 921 | bb_verror_msg(fmt, p, NULL); |
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 | free((char*)applet_name); |
| 924 | applet_name = sv; |
| 925 | } |
| 926 | } |
| 927 | |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 928 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 929 | { |
| 930 | va_list p; |
| 931 | |
| 932 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 933 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 934 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 935 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 936 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
| 937 | IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE); |
| 938 | exit(1); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 941 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 942 | static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 943 | { |
| 944 | va_list p; |
| 945 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 946 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 947 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 948 | RETURN_STATUS(BC_STATUS_SUCCESS); // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 949 | |
| 950 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 951 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 952 | va_end(p); |
| 953 | |
| 954 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 955 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 956 | RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning |
| 957 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 958 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 959 | RETURN_STATUS(BC_STATUS_FAILURE); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 960 | exit(1); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 961 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 962 | #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] | 963 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 964 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 965 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 966 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 967 | // function must not have caller-cleaned parameters on stack. |
| 968 | // Unfortunately, vararg function API does exactly that on most arches. |
| 969 | // 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] | 970 | static ERRORFUNC int bc_error(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 971 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 972 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 973 | } |
| 974 | static ERRORFUNC int bc_error_bad_character(char c) |
| 975 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 976 | if (!c) |
| 977 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 978 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 979 | } |
| 980 | static ERRORFUNC int bc_error_bad_expression(void) |
| 981 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 982 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 983 | } |
| 984 | static ERRORFUNC int bc_error_bad_token(void) |
| 985 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 986 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 987 | } |
| 988 | static ERRORFUNC int bc_error_stack_has_too_few_elements(void) |
| 989 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 990 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 991 | } |
| 992 | static ERRORFUNC int bc_error_variable_is_wrong_type(void) |
| 993 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 994 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 995 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 996 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 997 | static BC_STATUS zbc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 998 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 999 | RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg)); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1000 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1001 | #define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS) |
| 1002 | static BC_STATUS zbc_POSIX_does_not_allow(const char *msg) |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1003 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1004 | 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] | 1005 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1006 | #define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS) |
| 1007 | 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] | 1008 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1009 | 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] | 1010 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1011 | #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) |
| 1012 | 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] | 1013 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1014 | 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] | 1015 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1016 | #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] | 1017 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1018 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1019 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1020 | { |
| 1021 | size_t cap = v->cap * 2; |
| 1022 | while (cap < v->len + n) cap *= 2; |
| 1023 | v->v = xrealloc(v->v, v->size * cap); |
| 1024 | v->cap = cap; |
| 1025 | } |
| 1026 | |
| 1027 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1028 | { |
| 1029 | v->size = esize; |
| 1030 | v->cap = BC_VEC_START_CAP; |
| 1031 | v->len = 0; |
| 1032 | v->dtor = dtor; |
| 1033 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1034 | } |
| 1035 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1036 | static void bc_char_vec_init(BcVec *v) |
| 1037 | { |
| 1038 | bc_vec_init(v, sizeof(char), NULL); |
| 1039 | } |
| 1040 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1041 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1042 | { |
| 1043 | if (v->cap < req) { |
| 1044 | v->v = xrealloc(v->v, v->size * req); |
| 1045 | v->cap = req; |
| 1046 | } |
| 1047 | } |
| 1048 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1049 | static void bc_vec_pop(BcVec *v) |
| 1050 | { |
| 1051 | v->len--; |
| 1052 | if (v->dtor) |
| 1053 | v->dtor(v->v + (v->size * v->len)); |
| 1054 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1055 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1056 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1057 | { |
| 1058 | if (!v->dtor) |
| 1059 | v->len -= n; |
| 1060 | else { |
| 1061 | size_t len = v->len - n; |
| 1062 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1063 | } |
| 1064 | } |
| 1065 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1066 | static void bc_vec_pop_all(BcVec *v) |
| 1067 | { |
| 1068 | bc_vec_npop(v, v->len); |
| 1069 | } |
| 1070 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1071 | static size_t bc_vec_push(BcVec *v, const void *data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1072 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1073 | size_t len = v->len; |
| 1074 | if (len >= v->cap) bc_vec_grow(v, 1); |
| 1075 | memmove(v->v + (v->size * len), data, v->size); |
| 1076 | v->len++; |
| 1077 | return len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1078 | } |
| 1079 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 1080 | // G.prog.results often needs "pop old operand, push result" idiom. |
| 1081 | // Can do this without a few extra ops |
| 1082 | static size_t bc_result_pop_and_push(const void *data) |
| 1083 | { |
| 1084 | BcVec *v = &G.prog.results; |
| 1085 | char *last; |
| 1086 | size_t len = v->len - 1; |
| 1087 | |
| 1088 | last = v->v + (v->size * len); |
| 1089 | if (v->dtor) |
| 1090 | v->dtor(last); |
| 1091 | memmove(last, data, v->size); |
| 1092 | return len; |
| 1093 | } |
| 1094 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1095 | static size_t bc_vec_pushByte(BcVec *v, char data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1096 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1097 | return bc_vec_push(v, &data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1098 | } |
| 1099 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1100 | static size_t bc_vec_pushZeroByte(BcVec *v) |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1101 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1102 | //return bc_vec_pushByte(v, '\0'); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1103 | // better: |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1104 | return bc_vec_push(v, &const_int_0); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1107 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1108 | { |
| 1109 | if (idx == v->len) |
| 1110 | bc_vec_push(v, data); |
| 1111 | else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1112 | char *ptr; |
| 1113 | |
| 1114 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1115 | |
| 1116 | ptr = v->v + v->size * idx; |
| 1117 | |
| 1118 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1119 | memmove(ptr, data, v->size); |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1124 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1125 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1126 | bc_vec_expand(v, len + 1); |
| 1127 | memcpy(v->v, str, len); |
| 1128 | v->len = len; |
| 1129 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1130 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1131 | } |
| 1132 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1133 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1134 | { |
| 1135 | return v->v + v->size * idx; |
| 1136 | } |
| 1137 | |
| 1138 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1139 | { |
| 1140 | return v->v + v->size * (v->len - idx - 1); |
| 1141 | } |
| 1142 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1143 | static void *bc_vec_top(const BcVec *v) |
| 1144 | { |
| 1145 | return v->v + v->size * (v->len - 1); |
| 1146 | } |
| 1147 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1148 | static FAST_FUNC void bc_vec_free(void *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1149 | { |
| 1150 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1151 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1152 | free(v->v); |
| 1153 | } |
| 1154 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1155 | static BcFunc* xc_program_func(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1156 | { |
| 1157 | return bc_vec_item(&G.prog.fns, idx); |
| 1158 | } |
| 1159 | // BC_PROG_MAIN is zeroth element, so: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1160 | #define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v)) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1161 | |
| 1162 | #if ENABLE_BC |
| 1163 | static BcFunc* bc_program_current_func(void) |
| 1164 | { |
| 1165 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1166 | BcFunc *func = xc_program_func(ip->func); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1167 | return func; |
| 1168 | } |
| 1169 | #endif |
| 1170 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1171 | static char** xc_program_str(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1172 | { |
| 1173 | #if ENABLE_BC |
| 1174 | if (IS_BC) { |
| 1175 | BcFunc *func = bc_program_current_func(); |
| 1176 | return bc_vec_item(&func->strs, idx); |
| 1177 | } |
| 1178 | #endif |
| 1179 | IF_DC(return bc_vec_item(&G.prog.strs, idx);) |
| 1180 | } |
| 1181 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1182 | static char** xc_program_const(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1183 | { |
| 1184 | #if ENABLE_BC |
| 1185 | if (IS_BC) { |
| 1186 | BcFunc *func = bc_program_current_func(); |
| 1187 | return bc_vec_item(&func->consts, idx); |
| 1188 | } |
| 1189 | #endif |
| 1190 | IF_DC(return bc_vec_item(&G.prog.consts, idx);) |
| 1191 | } |
| 1192 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1193 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1194 | { |
| 1195 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1196 | } |
| 1197 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1198 | static FAST_FUNC void bc_id_free(void *id) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1199 | { |
| 1200 | free(((BcId *) id)->name); |
| 1201 | } |
| 1202 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1203 | 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] | 1204 | { |
| 1205 | size_t low = 0, high = v->len; |
| 1206 | |
| 1207 | while (low < high) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1208 | size_t mid = (low + high) / 2; |
| 1209 | BcId *id = bc_vec_item(v, mid); |
| 1210 | int result = bc_id_cmp(ptr, id); |
| 1211 | |
| 1212 | if (result == 0) |
| 1213 | return mid; |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 1214 | if (result < 0) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1215 | high = mid; |
| 1216 | else |
| 1217 | low = mid + 1; |
| 1218 | } |
| 1219 | |
| 1220 | return low; |
| 1221 | } |
| 1222 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1223 | 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] | 1224 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1225 | size_t n = *i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1226 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1227 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1228 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1229 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1230 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1231 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1232 | bc_vec_pushAt(v, ptr, n); |
| 1233 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1234 | } |
| 1235 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1236 | #if ENABLE_BC |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1237 | 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] | 1238 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1239 | size_t i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1240 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1241 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1242 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1243 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1244 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1245 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1246 | { |
| 1247 | n->len = 0; |
| 1248 | n->neg = false; |
| 1249 | n->rdx = scale; |
| 1250 | } |
| 1251 | |
| 1252 | static void bc_num_zero(BcNum *n) |
| 1253 | { |
| 1254 | bc_num_setToZero(n, 0); |
| 1255 | } |
| 1256 | |
| 1257 | static void bc_num_one(BcNum *n) |
| 1258 | { |
| 1259 | bc_num_setToZero(n, 0); |
| 1260 | n->len = 1; |
| 1261 | n->num[0] = 1; |
| 1262 | } |
| 1263 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1264 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1265 | static void bc_num_init(BcNum *n, size_t req) |
| 1266 | { |
| 1267 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1268 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1269 | n->num = xmalloc(req); |
| 1270 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1271 | n->rdx = 0; |
| 1272 | n->len = 0; |
| 1273 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1274 | } |
| 1275 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1276 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1277 | { |
| 1278 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1279 | } |
| 1280 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1281 | static void bc_num_expand(BcNum *n, size_t req) |
| 1282 | { |
| 1283 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1284 | if (req > n->cap) { |
| 1285 | n->num = xrealloc(n->num, req); |
| 1286 | n->cap = req; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1290 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1291 | { |
| 1292 | free(((BcNum *) num)->num); |
| 1293 | } |
| 1294 | |
| 1295 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1296 | { |
| 1297 | if (d != s) { |
| 1298 | bc_num_expand(d, s->cap); |
| 1299 | d->len = s->len; |
| 1300 | d->neg = s->neg; |
| 1301 | d->rdx = s->rdx; |
| 1302 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1303 | } |
| 1304 | } |
| 1305 | |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1306 | 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] | 1307 | { |
| 1308 | size_t i; |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1309 | unsigned long result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1310 | |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1311 | result = 0; |
| 1312 | i = n->len; |
| 1313 | while (i > n->rdx) { |
| 1314 | unsigned long prev = result; |
| 1315 | result = result * 10 + n->num[--i]; |
| 1316 | // Even overflowed N*10 can still satisfy N*10>=N. For example, |
| 1317 | // 0x1ff00000 * 10 is 0x13f600000, |
| 1318 | // or 0x3f600000 truncated to 32 bits. Which is larger. |
| 1319 | // However, (N*10)/8 < N check is always correct. |
| 1320 | if ((result / 8) < prev) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1321 | RETURN_STATUS(bc_error("overflow")); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1322 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1323 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1324 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1325 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1326 | } |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1327 | #define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS) |
| 1328 | |
| 1329 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
| 1330 | { |
| 1331 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
| 1332 | |
| 1333 | RETURN_STATUS(zbc_num_ulong_abs(n, result_p)); |
| 1334 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1335 | #define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1336 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1337 | #if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295 |
| 1338 | # define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE) |
| 1339 | #elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615 |
| 1340 | # define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE) |
| 1341 | #endif |
| 1342 | // minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num() |
| 1343 | // would not hit realloc() code path - not good if num[] is not malloced |
| 1344 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1345 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1346 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1347 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1348 | |
| 1349 | bc_num_zero(n); |
| 1350 | |
| 1351 | if (val == 0) return; |
| 1352 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1353 | bc_num_expand(n, ULONG_NUM_BUFSIZE); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1354 | |
| 1355 | ptr = n->num; |
| 1356 | for (;;) { |
| 1357 | n->len++; |
| 1358 | *ptr++ = val % 10; |
| 1359 | val /= 10; |
| 1360 | if (val == 0) break; |
| 1361 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1362 | } |
| 1363 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1364 | 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] | 1365 | { |
| 1366 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1367 | for (i = 0; i < len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1368 | a[i] -= b[i]; |
| 1369 | for (j = i; a[j] < 0;) { |
| 1370 | a[j++] += 10; |
| 1371 | a[j] -= 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1372 | } |
| 1373 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1377 | { |
Denys Vlasenko | 4113e1f | 2018-12-18 00:39:24 +0100 | [diff] [blame] | 1378 | size_t i = len; |
| 1379 | for (;;) { |
| 1380 | int c; |
| 1381 | if (i == 0) |
| 1382 | return 0; |
| 1383 | i--; |
| 1384 | c = a[i] - b[i]; |
| 1385 | if (c != 0) { |
| 1386 | i++; |
| 1387 | if (c < 0) |
| 1388 | return -i; |
| 1389 | return i; |
| 1390 | } |
| 1391 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1392 | } |
| 1393 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1394 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 1395 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 1396 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 1397 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 1398 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 1399 | { |
| 1400 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 1401 | } |
| 1402 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 1403 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 1404 | { |
| 1405 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 1406 | } |
| 1407 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1408 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1409 | { |
| 1410 | size_t i, min, a_int, b_int, diff; |
| 1411 | BcDig *max_num, *min_num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1412 | bool a_max, neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1413 | ssize_t cmp; |
| 1414 | |
| 1415 | if (a == b) return 0; |
| 1416 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1417 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1418 | |
| 1419 | if (a->neg != b->neg) // signs of a and b differ |
| 1420 | // +a,-b = a>b = 1 or -a,+b = a<b = -1 |
| 1421 | return (int)b->neg - (int)a->neg; |
| 1422 | neg = a->neg; // 1 if both negative, 0 if both positive |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1423 | |
| 1424 | a_int = BC_NUM_INT(a); |
| 1425 | b_int = BC_NUM_INT(b); |
| 1426 | a_int -= b_int; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1427 | |
| 1428 | if (a_int != 0) return (ssize_t) a_int; |
| 1429 | |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1430 | a_max = (a->rdx > b->rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1431 | if (a_max) { |
| 1432 | min = b->rdx; |
| 1433 | diff = a->rdx - b->rdx; |
| 1434 | max_num = a->num + diff; |
| 1435 | min_num = b->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1436 | // neg = (a_max == neg); - NOP (maps 1->1 and 0->0) |
| 1437 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1438 | min = a->rdx; |
| 1439 | diff = b->rdx - a->rdx; |
| 1440 | max_num = b->num + diff; |
| 1441 | min_num = a->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1442 | neg = !neg; // same as "neg = (a_max == neg)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1446 | if (cmp != 0) return BC_NUM_NEG(cmp, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1447 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1448 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1449 | if (max_num[i]) return BC_NUM_NEG(1, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
| 1455 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1456 | { |
| 1457 | if (places == 0) return; |
| 1458 | |
| 1459 | n->rdx -= places; |
| 1460 | |
| 1461 | if (n->len != 0) { |
| 1462 | n->len -= places; |
| 1463 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | static void bc_num_extend(BcNum *n, size_t places) |
| 1468 | { |
| 1469 | size_t len = n->len + places; |
| 1470 | |
| 1471 | if (places != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1472 | if (n->cap < len) bc_num_expand(n, len); |
| 1473 | |
| 1474 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1475 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1476 | |
| 1477 | n->len += places; |
| 1478 | n->rdx += places; |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | static void bc_num_clean(BcNum *n) |
| 1483 | { |
| 1484 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1485 | if (n->len == 0) |
| 1486 | n->neg = false; |
| 1487 | else if (n->len < n->rdx) |
| 1488 | n->len = n->rdx; |
| 1489 | } |
| 1490 | |
| 1491 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1492 | { |
| 1493 | if (n->rdx < scale) |
| 1494 | bc_num_extend(n, scale - n->rdx); |
| 1495 | else |
| 1496 | bc_num_truncate(n, n->rdx - scale); |
| 1497 | |
| 1498 | bc_num_clean(n); |
| 1499 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1500 | } |
| 1501 | |
| 1502 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1503 | BcNum *restrict b) |
| 1504 | { |
| 1505 | if (idx < n->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1506 | b->len = n->len - idx; |
| 1507 | a->len = idx; |
| 1508 | a->rdx = b->rdx = 0; |
| 1509 | |
| 1510 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1511 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1512 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1513 | bc_num_zero(b); |
| 1514 | bc_num_copy(a, n); |
| 1515 | } |
| 1516 | |
| 1517 | bc_num_clean(a); |
| 1518 | bc_num_clean(b); |
| 1519 | } |
| 1520 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1521 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1522 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1523 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1524 | |
| 1525 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1526 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1527 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1528 | 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] | 1529 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1530 | |
| 1531 | if (n->rdx >= places) |
| 1532 | n->rdx -= places; |
| 1533 | else { |
| 1534 | bc_num_extend(n, places - n->rdx); |
| 1535 | n->rdx = 0; |
| 1536 | } |
| 1537 | |
| 1538 | bc_num_clean(n); |
| 1539 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1540 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1541 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1542 | #define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1543 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1544 | typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
| 1545 | |
| 1546 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 1547 | BcNumBinaryOp op, size_t req) |
| 1548 | { |
| 1549 | BcStatus s; |
| 1550 | BcNum num2, *ptr_a, *ptr_b; |
| 1551 | bool init = false; |
| 1552 | |
| 1553 | if (c == a) { |
| 1554 | ptr_a = &num2; |
| 1555 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 1556 | init = true; |
| 1557 | } else |
| 1558 | ptr_a = a; |
| 1559 | |
| 1560 | if (c == b) { |
| 1561 | ptr_b = &num2; |
| 1562 | if (c != a) { |
| 1563 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 1564 | init = true; |
| 1565 | } |
| 1566 | } else |
| 1567 | ptr_b = b; |
| 1568 | |
| 1569 | if (init) |
| 1570 | bc_num_init(c, req); |
| 1571 | else |
| 1572 | bc_num_expand(c, req); |
| 1573 | |
| 1574 | s = BC_STATUS_SUCCESS; |
| 1575 | IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale); |
| 1576 | |
| 1577 | if (init) bc_num_free(&num2); |
| 1578 | |
| 1579 | RETURN_STATUS(s); |
| 1580 | } |
| 1581 | #define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS) |
| 1582 | |
| 1583 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1584 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1585 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1586 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1587 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1588 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1589 | |
| 1590 | static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1591 | { |
| 1592 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s; |
| 1593 | (void) scale; |
| 1594 | RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b))); |
| 1595 | } |
| 1596 | |
| 1597 | static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1598 | { |
| 1599 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a; |
| 1600 | (void) scale; |
| 1601 | RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b))); |
| 1602 | } |
| 1603 | |
| 1604 | static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1605 | { |
| 1606 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1607 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req)); |
| 1608 | } |
| 1609 | |
| 1610 | static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1611 | { |
| 1612 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1613 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req)); |
| 1614 | } |
| 1615 | |
| 1616 | static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1617 | { |
| 1618 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1619 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req)); |
| 1620 | } |
| 1621 | |
| 1622 | static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1623 | { |
| 1624 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); |
| 1625 | } |
| 1626 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1627 | static const BcNumBinaryOp zxc_program_ops[] = { |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1628 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, |
| 1629 | }; |
| 1630 | #define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS) |
| 1631 | #define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS) |
| 1632 | #define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS) |
| 1633 | #define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS) |
| 1634 | #define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS) |
| 1635 | #define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS) |
| 1636 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1637 | 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] | 1638 | { |
| 1639 | BcNum one; |
| 1640 | BcDig num[2]; |
| 1641 | |
| 1642 | one.cap = 2; |
| 1643 | one.num = num; |
| 1644 | bc_num_one(&one); |
| 1645 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1646 | RETURN_STATUS(zbc_num_div(&one, a, b, scale)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1647 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1648 | #define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1649 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1650 | 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] | 1651 | { |
| 1652 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1653 | 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] | 1654 | unsigned carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1655 | |
| 1656 | // Because this function doesn't need to use scale (per the bc spec), |
| 1657 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1658 | |
| 1659 | if (a->len == 0) { |
| 1660 | bc_num_copy(c, b); |
| 1661 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1662 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1663 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1664 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1665 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1666 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | c->neg = a->neg; |
| 1670 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1671 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1672 | c->len = 0; |
| 1673 | |
| 1674 | if (a->rdx > b->rdx) { |
| 1675 | diff = a->rdx - b->rdx; |
| 1676 | ptr = a->num; |
| 1677 | ptr_a = a->num + diff; |
| 1678 | ptr_b = b->num; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1679 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1680 | diff = b->rdx - a->rdx; |
| 1681 | ptr = b->num; |
| 1682 | ptr_a = a->num; |
| 1683 | ptr_b = b->num + diff; |
| 1684 | } |
| 1685 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1686 | ptr_c = c->num; |
| 1687 | for (i = 0; i < diff; ++i, ++c->len) |
| 1688 | ptr_c[i] = ptr[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1689 | |
| 1690 | ptr_c += diff; |
| 1691 | a_int = BC_NUM_INT(a); |
| 1692 | b_int = BC_NUM_INT(b); |
| 1693 | |
| 1694 | if (a_int > b_int) { |
| 1695 | min_int = b_int; |
| 1696 | max = a_int; |
| 1697 | ptr = ptr_a; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1698 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1699 | min_int = a_int; |
| 1700 | max = b_int; |
| 1701 | ptr = ptr_b; |
| 1702 | } |
| 1703 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1704 | carry = 0; |
| 1705 | for (i = 0; i < min_rdx + min_int; ++i) { |
| 1706 | unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1707 | carry = in / 10; |
| 1708 | ptr_c[i] = (BcDig)(in % 10); |
| 1709 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1710 | for (; i < max + min_rdx; ++i) { |
| 1711 | unsigned in = (unsigned)ptr[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1712 | carry = in / 10; |
| 1713 | ptr_c[i] = (BcDig)(in % 10); |
| 1714 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1715 | c->len += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1716 | |
| 1717 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1718 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1719 | 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] | 1720 | } |
| 1721 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1722 | 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] | 1723 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1724 | ssize_t cmp; |
| 1725 | BcNum *minuend, *subtrahend; |
| 1726 | size_t start; |
| 1727 | bool aneg, bneg, neg; |
| 1728 | |
| 1729 | // Because this function doesn't need to use scale (per the bc spec), |
| 1730 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1731 | |
| 1732 | if (a->len == 0) { |
| 1733 | bc_num_copy(c, b); |
| 1734 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1735 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1736 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1737 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1738 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1739 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | aneg = a->neg; |
| 1743 | bneg = b->neg; |
| 1744 | a->neg = b->neg = false; |
| 1745 | |
| 1746 | cmp = bc_num_cmp(a, b); |
| 1747 | |
| 1748 | a->neg = aneg; |
| 1749 | b->neg = bneg; |
| 1750 | |
| 1751 | if (cmp == 0) { |
| 1752 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1753 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1754 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1755 | if (cmp > 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1756 | neg = a->neg; |
| 1757 | minuend = a; |
| 1758 | subtrahend = b; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1759 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1760 | neg = b->neg; |
| 1761 | if (sub) neg = !neg; |
| 1762 | minuend = b; |
| 1763 | subtrahend = a; |
| 1764 | } |
| 1765 | |
| 1766 | bc_num_copy(c, minuend); |
| 1767 | c->neg = neg; |
| 1768 | |
| 1769 | if (c->rdx < subtrahend->rdx) { |
| 1770 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1771 | start = 0; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1772 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1773 | start = c->rdx - subtrahend->rdx; |
| 1774 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1775 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1776 | |
| 1777 | bc_num_clean(c); |
| 1778 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1779 | 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] | 1780 | } |
| 1781 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1782 | 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] | 1783 | BcNum *restrict c) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1784 | #define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1785 | { |
| 1786 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1787 | size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1788 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1789 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1790 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1791 | if (a->len == 0 || b->len == 0) { |
| 1792 | bc_num_zero(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1793 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1794 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1795 | aone = BC_NUM_ONE(a); |
| 1796 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1797 | bc_num_copy(c, aone ? b : a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1798 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1799 | } |
| 1800 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1801 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN |
| 1802 | || a->len < BC_NUM_KARATSUBA_LEN |
| 1803 | || b->len < BC_NUM_KARATSUBA_LEN |
| 1804 | ) { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1805 | size_t i, j, len; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1806 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1807 | bc_num_expand(c, a->len + b->len + 1); |
| 1808 | |
| 1809 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1810 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1811 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1812 | for (i = 0; i < b->len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1813 | unsigned carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1814 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1815 | unsigned in = c->num[i + j]; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1816 | in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1817 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1818 | carry = in / 10; |
| 1819 | c->num[i + j] = (BcDig)(in % 10); |
| 1820 | } |
| 1821 | |
| 1822 | c->num[i + j] += (BcDig) carry; |
| 1823 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1824 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 1825 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1826 | // a=2^1000000 |
| 1827 | // a*a <- without check below, this will not be interruptible |
| 1828 | if (G_interrupt) return BC_STATUS_FAILURE; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1829 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | c->len = len; |
| 1833 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1834 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | bc_num_init(&l1, max); |
| 1838 | bc_num_init(&h1, max); |
| 1839 | bc_num_init(&l2, max); |
| 1840 | bc_num_init(&h2, max); |
| 1841 | bc_num_init(&m1, max); |
| 1842 | bc_num_init(&m2, max); |
| 1843 | bc_num_init(&z0, max); |
| 1844 | bc_num_init(&z1, max); |
| 1845 | bc_num_init(&z2, max); |
| 1846 | bc_num_init(&temp, max + max); |
| 1847 | |
| 1848 | bc_num_split(a, max2, &l1, &h1); |
| 1849 | bc_num_split(b, max2, &l2, &h2); |
| 1850 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1851 | s = zbc_num_add(&h1, &l1, &m1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1852 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1853 | s = zbc_num_add(&h2, &l2, &m2, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1854 | if (s) goto err; |
| 1855 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1856 | s = zbc_num_k(&h1, &h2, &z0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1857 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1858 | s = zbc_num_k(&m1, &m2, &z1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1859 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1860 | s = zbc_num_k(&l1, &l2, &z2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1861 | if (s) goto err; |
| 1862 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1863 | s = zbc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1864 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1865 | s = zbc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1866 | if (s) goto err; |
| 1867 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1868 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1869 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1870 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1871 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1872 | s = zbc_num_add(&z0, &z1, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1873 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1874 | s = zbc_num_add(&temp, &z2, c, 0); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1875 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1876 | bc_num_free(&temp); |
| 1877 | bc_num_free(&z2); |
| 1878 | bc_num_free(&z1); |
| 1879 | bc_num_free(&z0); |
| 1880 | bc_num_free(&m2); |
| 1881 | bc_num_free(&m1); |
| 1882 | bc_num_free(&h2); |
| 1883 | bc_num_free(&l2); |
| 1884 | bc_num_free(&h1); |
| 1885 | bc_num_free(&l1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1886 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1887 | } |
| 1888 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1889 | 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] | 1890 | { |
| 1891 | BcStatus s; |
| 1892 | BcNum cpa, cpb; |
| 1893 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1894 | |
| 1895 | scale = BC_MAX(scale, a->rdx); |
| 1896 | scale = BC_MAX(scale, b->rdx); |
| 1897 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1898 | maxrdx = BC_MAX(maxrdx, scale); |
| 1899 | |
| 1900 | bc_num_init(&cpa, a->len); |
| 1901 | bc_num_init(&cpb, b->len); |
| 1902 | |
| 1903 | bc_num_copy(&cpa, a); |
| 1904 | bc_num_copy(&cpb, b); |
| 1905 | cpa.neg = cpb.neg = false; |
| 1906 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1907 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1908 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1909 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1910 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1911 | s = zbc_num_k(&cpa, &cpb, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1912 | if (s) goto err; |
| 1913 | |
| 1914 | maxrdx += scale; |
| 1915 | bc_num_expand(c, c->len + maxrdx); |
| 1916 | |
| 1917 | if (c->len < maxrdx) { |
| 1918 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 1919 | c->len += maxrdx; |
| 1920 | } |
| 1921 | |
| 1922 | c->rdx = maxrdx; |
| 1923 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1924 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1925 | bc_num_free(&cpb); |
| 1926 | bc_num_free(&cpa); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1927 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1928 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1929 | #define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1930 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1931 | 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] | 1932 | { |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 1933 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1934 | size_t len, end, i; |
| 1935 | BcNum cp; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1936 | |
| 1937 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1938 | RETURN_STATUS(bc_error("divide by zero")); |
| 1939 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1940 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1941 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1942 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1943 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1944 | bc_num_copy(c, a); |
| 1945 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1946 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 1950 | bc_num_copy(&cp, a); |
| 1951 | len = b->len; |
| 1952 | |
| 1953 | if (len > cp.len) { |
| 1954 | bc_num_expand(&cp, len + 2); |
| 1955 | bc_num_extend(&cp, len - cp.len); |
| 1956 | } |
| 1957 | |
| 1958 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 1959 | cp.rdx -= b->rdx; |
| 1960 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 1961 | |
| 1962 | if (b->rdx == b->len) { |
Denys Vlasenko | 71c82d1 | 2018-12-18 12:43:21 +0100 | [diff] [blame] | 1963 | for (;;) { |
| 1964 | if (len == 0) break; |
| 1965 | len--; |
| 1966 | if (b->num[len] != 0) |
| 1967 | break; |
| 1968 | } |
| 1969 | len++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 1973 | |
| 1974 | // We want an extra zero in front to make things simpler. |
| 1975 | cp.num[cp.len++] = 0; |
| 1976 | end = cp.len - len; |
| 1977 | |
| 1978 | bc_num_expand(c, cp.len); |
| 1979 | |
| 1980 | bc_num_zero(c); |
| 1981 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 1982 | c->rdx = cp.rdx; |
| 1983 | c->len = cp.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1984 | |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 1985 | s = BC_STATUS_SUCCESS; |
| 1986 | for (i = end - 1; i < end; --i) { |
| 1987 | BcDig *n, q; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1988 | n = cp.num + i; |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 1989 | for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q) |
| 1990 | bc_num_subArrays(n, b->num, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1991 | c->num[i] = q; |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 1992 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 1993 | // a=2^100000 |
| 1994 | // scale=40000 |
| 1995 | // 1/a <- without check below, this will not be interruptible |
| 1996 | if (G_interrupt) { |
| 1997 | s = BC_STATUS_FAILURE; |
| 1998 | break; |
| 1999 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2000 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2001 | } |
| 2002 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2003 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2004 | bc_num_free(&cp); |
| 2005 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2006 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2007 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2008 | #define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2009 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2010 | 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] | 2011 | BcNum *restrict d, size_t scale, size_t ts) |
| 2012 | { |
| 2013 | BcStatus s; |
| 2014 | BcNum temp; |
| 2015 | bool neg; |
| 2016 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2017 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2018 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2019 | |
| 2020 | if (a->len == 0) { |
| 2021 | bc_num_setToZero(d, ts); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2022 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2026 | s = zbc_num_d(a, b, c, scale); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2027 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2028 | |
| 2029 | if (scale != 0) scale = ts; |
| 2030 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2031 | s = zbc_num_m(c, b, &temp, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2032 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2033 | s = zbc_num_sub(a, &temp, d, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2034 | if (s) goto err; |
| 2035 | |
| 2036 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2037 | |
| 2038 | neg = d->neg; |
| 2039 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2040 | d->neg = neg; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2041 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2042 | bc_num_free(&temp); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2043 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2044 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2045 | #define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2046 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2047 | 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] | 2048 | { |
| 2049 | BcStatus s; |
| 2050 | BcNum c1; |
| 2051 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2052 | |
| 2053 | bc_num_init(&c1, len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2054 | s = zbc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2055 | bc_num_free(&c1); |
| 2056 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2057 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2058 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2059 | #define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2060 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2061 | 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] | 2062 | { |
| 2063 | BcStatus s = BC_STATUS_SUCCESS; |
| 2064 | BcNum copy; |
| 2065 | unsigned long pow; |
| 2066 | size_t i, powrdx, resrdx; |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2067 | bool neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2068 | |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2069 | // GNU bc does not allow 2^2.0 - we do |
| 2070 | for (i = 0; i < b->rdx; i++) |
| 2071 | if (b->num[i] != 0) |
| 2072 | RETURN_STATUS(bc_error("not an integer")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2073 | |
| 2074 | if (b->len == 0) { |
| 2075 | bc_num_one(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2076 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2077 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2078 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2079 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2080 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2081 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2082 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2083 | if (!b->neg) |
| 2084 | bc_num_copy(c, a); |
| 2085 | else |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2086 | s = zbc_num_inv(a, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2087 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | neg = b->neg; |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 2091 | s = zbc_num_ulong_abs(b, &pow); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2092 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 2ea8ddf | 2018-12-22 21:45:18 +0100 | [diff] [blame] | 2093 | // b is not used beyond this point |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2094 | |
| 2095 | bc_num_init(©, a->len); |
| 2096 | bc_num_copy(©, a); |
| 2097 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2098 | if (!neg) { |
| 2099 | if (a->rdx > scale) |
| 2100 | scale = a->rdx; |
| 2101 | if (a->rdx * pow < scale) |
| 2102 | scale = a->rdx * pow; |
| 2103 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2104 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2105 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2106 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2107 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2108 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2109 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2110 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2111 | //if (G_interrupt) { |
| 2112 | // s = BC_STATUS_FAILURE; |
| 2113 | // goto err; |
| 2114 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2115 | } |
| 2116 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2117 | bc_num_copy(c, ©); |
| 2118 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2119 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2120 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2121 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2122 | if (s) goto err; |
| 2123 | |
| 2124 | if (pow & 1) { |
| 2125 | resrdx += powrdx; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2126 | s = zbc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2127 | if (s) goto err; |
| 2128 | } |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2129 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2130 | //if (G_interrupt) { |
| 2131 | // s = BC_STATUS_FAILURE; |
| 2132 | // goto err; |
| 2133 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | if (neg) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2137 | s = zbc_num_inv(c, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2138 | if (s) goto err; |
| 2139 | } |
| 2140 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2141 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2142 | |
| 2143 | // We can't use bc_num_clean() here. |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2144 | for (i = 0; i < c->len; ++i) |
| 2145 | if (c->num[i] != 0) |
| 2146 | goto skip; |
| 2147 | bc_num_setToZero(c, scale); |
| 2148 | skip: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2149 | |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2150 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2151 | bc_num_free(©); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2152 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2153 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2154 | #define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2155 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2156 | 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] | 2157 | { |
| 2158 | BcStatus s; |
| 2159 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
| 2160 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2161 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2162 | |
| 2163 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2164 | bc_num_expand(b, req); |
| 2165 | |
| 2166 | if (a->len == 0) { |
| 2167 | bc_num_setToZero(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2168 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2169 | } |
| 2170 | if (a->neg) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2171 | RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2172 | } |
| 2173 | if (BC_NUM_ONE(a)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2174 | bc_num_one(b); |
| 2175 | bc_num_extend(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2176 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2180 | len = a->len + scale; |
| 2181 | |
| 2182 | bc_num_init(&num1, len); |
| 2183 | bc_num_init(&num2, len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2184 | bc_num_init_DEF_SIZE(&half); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2185 | |
| 2186 | bc_num_one(&half); |
| 2187 | half.num[0] = 5; |
| 2188 | half.rdx = 1; |
| 2189 | |
| 2190 | bc_num_init(&f, len); |
| 2191 | bc_num_init(&fprime, len); |
| 2192 | |
| 2193 | x0 = &num1; |
| 2194 | x1 = &num2; |
| 2195 | |
| 2196 | bc_num_one(x0); |
| 2197 | pow = BC_NUM_INT(a); |
| 2198 | |
| 2199 | if (pow) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2200 | if (pow & 1) |
| 2201 | x0->num[0] = 2; |
| 2202 | else |
| 2203 | x0->num[0] = 6; |
| 2204 | |
| 2205 | pow -= 2 - (pow & 1); |
| 2206 | |
| 2207 | bc_num_extend(x0, pow); |
| 2208 | |
| 2209 | // Make sure to move the radix back. |
| 2210 | x0->rdx -= pow; |
| 2211 | } |
| 2212 | |
| 2213 | x0->rdx = digs = digs1 = 0; |
| 2214 | resrdx = scale + 2; |
| 2215 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2216 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2217 | while (cmp != 0 || digs < len) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2218 | s = zbc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2219 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2220 | s = zbc_num_add(x0, &f, &fprime, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2221 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2222 | s = zbc_num_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2223 | if (s) goto err; |
| 2224 | |
| 2225 | cmp = bc_num_cmp(x1, x0); |
| 2226 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2227 | |
| 2228 | if (cmp == cmp2 && digs == digs1) |
| 2229 | times += 1; |
| 2230 | else |
| 2231 | times = 0; |
| 2232 | |
| 2233 | resrdx += times > 4; |
| 2234 | |
| 2235 | cmp2 = cmp1; |
| 2236 | cmp1 = cmp; |
| 2237 | digs1 = digs; |
| 2238 | |
| 2239 | temp = x0; |
| 2240 | x0 = x1; |
| 2241 | x1 = temp; |
| 2242 | } |
| 2243 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2244 | bc_num_copy(b, x0); |
| 2245 | scale -= 1; |
| 2246 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2247 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2248 | bc_num_free(&fprime); |
| 2249 | bc_num_free(&f); |
| 2250 | bc_num_free(&half); |
| 2251 | bc_num_free(&num2); |
| 2252 | bc_num_free(&num1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2253 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2254 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2255 | #define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2256 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2257 | 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] | 2258 | size_t scale) |
| 2259 | { |
| 2260 | BcStatus s; |
| 2261 | BcNum num2, *ptr_a; |
| 2262 | bool init = false; |
| 2263 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2264 | |
| 2265 | if (c == a) { |
| 2266 | memcpy(&num2, c, sizeof(BcNum)); |
| 2267 | ptr_a = &num2; |
| 2268 | bc_num_init(c, len); |
| 2269 | init = true; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2270 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2271 | ptr_a = a; |
| 2272 | bc_num_expand(c, len); |
| 2273 | } |
| 2274 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2275 | s = zbc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2276 | |
| 2277 | if (init) bc_num_free(&num2); |
| 2278 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2279 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2280 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2281 | #define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2282 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2283 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2284 | 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] | 2285 | { |
| 2286 | BcStatus s; |
| 2287 | BcNum base, exp, two, temp; |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2288 | BcDig two_digs[2]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2289 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2290 | if (c->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2291 | RETURN_STATUS(bc_error("divide by zero")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2292 | if (a->rdx || b->rdx || c->rdx) |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2293 | RETURN_STATUS(bc_error("not an integer")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2294 | if (b->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2295 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2296 | |
| 2297 | bc_num_expand(d, c->len); |
| 2298 | bc_num_init(&base, c->len); |
| 2299 | bc_num_init(&exp, b->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2300 | bc_num_init(&temp, b->len); |
| 2301 | |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2302 | two.cap = ARRAY_SIZE(two_digs); |
| 2303 | two.num = two_digs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2304 | bc_num_one(&two); |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2305 | two_digs[0] = 2; |
| 2306 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2307 | bc_num_one(d); |
| 2308 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2309 | s = zbc_num_rem(a, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2310 | if (s) goto err; |
| 2311 | bc_num_copy(&exp, b); |
| 2312 | |
| 2313 | while (exp.len != 0) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2314 | s = zbc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2315 | if (s) goto err; |
| 2316 | |
| 2317 | if (BC_NUM_ONE(&temp)) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2318 | s = zbc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2319 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2320 | s = zbc_num_rem(&temp, c, d, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2321 | if (s) goto err; |
| 2322 | } |
| 2323 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2324 | s = zbc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2325 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2326 | s = zbc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2327 | if (s) goto err; |
| 2328 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2329 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2330 | bc_num_free(&temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2331 | bc_num_free(&exp); |
| 2332 | bc_num_free(&base); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2333 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2334 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2335 | #define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2336 | #endif // ENABLE_DC |
| 2337 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2338 | static FAST_FUNC void bc_string_free(void *string) |
| 2339 | { |
| 2340 | free(*(char**)string); |
| 2341 | } |
| 2342 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2343 | static void bc_func_init(BcFunc *f) |
| 2344 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2345 | bc_char_vec_init(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2346 | IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);) |
| 2347 | IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2348 | IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);) |
| 2349 | IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2350 | IF_BC(f->nparams = 0;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2351 | } |
| 2352 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2353 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2354 | { |
| 2355 | BcFunc *f = (BcFunc *) func; |
| 2356 | bc_vec_free(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2357 | IF_BC(bc_vec_free(&f->labels);) |
| 2358 | IF_BC(bc_vec_free(&f->autos);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2359 | IF_BC(bc_vec_free(&f->strs);) |
| 2360 | IF_BC(bc_vec_free(&f->consts);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2361 | } |
| 2362 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2363 | static void bc_array_expand(BcVec *a, size_t len); |
| 2364 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2365 | static void bc_array_init(BcVec *a, bool nums) |
| 2366 | { |
| 2367 | if (nums) |
| 2368 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2369 | else |
| 2370 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2371 | bc_array_expand(a, 1); |
| 2372 | } |
| 2373 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2374 | static void bc_array_expand(BcVec *a, size_t len) |
| 2375 | { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2376 | if (a->dtor == bc_num_free |
| 2377 | // && a->size == sizeof(BcNum) - always true |
| 2378 | ) { |
| 2379 | BcNum n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2380 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2381 | bc_num_init_DEF_SIZE(&n); |
| 2382 | bc_vec_push(a, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2383 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2384 | } else { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2385 | BcVec v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2386 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2387 | bc_array_init(&v, true); |
| 2388 | bc_vec_push(a, &v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2389 | } |
| 2390 | } |
| 2391 | } |
| 2392 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2393 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2394 | { |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2395 | BcNum *dnum, *snum; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2396 | size_t i; |
| 2397 | |
| 2398 | bc_vec_pop_all(d); |
| 2399 | bc_vec_expand(d, s->cap); |
| 2400 | d->len = s->len; |
| 2401 | |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2402 | dnum = (void*)d->v; |
| 2403 | snum = (void*)s->v; |
| 2404 | for (i = 0; i < s->len; i++, dnum++, snum++) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2405 | bc_num_init(dnum, snum->len); |
| 2406 | bc_num_copy(dnum, snum); |
| 2407 | } |
| 2408 | } |
| 2409 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2410 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2411 | static void dc_result_copy(BcResult *d, BcResult *src) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2412 | { |
| 2413 | d->t = src->t; |
| 2414 | |
| 2415 | switch (d->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2416 | case XC_RESULT_TEMP: |
| 2417 | case XC_RESULT_IBASE: |
| 2418 | case XC_RESULT_SCALE: |
| 2419 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2420 | bc_num_init(&d->d.n, src->d.n.len); |
| 2421 | bc_num_copy(&d->d.n, &src->d.n); |
| 2422 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2423 | case XC_RESULT_VAR: |
| 2424 | case XC_RESULT_ARRAY: |
| 2425 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2426 | d->d.id.name = xstrdup(src->d.id.name); |
| 2427 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2428 | case XC_RESULT_CONSTANT: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 2429 | IF_BC(case BC_RESULT_LAST:) |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2430 | IF_BC(case BC_RESULT_ONE:) |
| 2431 | case XC_RESULT_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2432 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2433 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | #endif // ENABLE_DC |
| 2437 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2438 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2439 | { |
| 2440 | BcResult *r = (BcResult *) result; |
| 2441 | |
| 2442 | switch (r->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2443 | case XC_RESULT_TEMP: |
| 2444 | case XC_RESULT_IBASE: |
| 2445 | case XC_RESULT_SCALE: |
| 2446 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2447 | bc_num_free(&r->d.n); |
| 2448 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2449 | case XC_RESULT_VAR: |
| 2450 | case XC_RESULT_ARRAY: |
| 2451 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2452 | free(r->d.id.name); |
| 2453 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2454 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2455 | // Do nothing. |
| 2456 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2457 | } |
| 2458 | } |
| 2459 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2460 | static int bad_input_byte(char c) |
| 2461 | { |
| 2462 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 2463 | || c > 0x7e |
| 2464 | ) { |
| 2465 | bc_error_fmt("illegal character 0x%02x", c); |
| 2466 | return 1; |
| 2467 | } |
| 2468 | return 0; |
| 2469 | } |
| 2470 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2471 | static void xc_read_line(BcVec *vec, FILE *fp) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2472 | { |
| 2473 | again: |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2474 | bc_vec_pop_all(vec); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2475 | fflush_and_check(); |
| 2476 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2477 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2478 | if (G_interrupt) { // ^C was pressed |
| 2479 | intr: |
| 2480 | if (fp != stdin) { |
| 2481 | // ^C while running a script (bc SCRIPT): die. |
| 2482 | // We do not return to interactive prompt: |
| 2483 | // user might be running us from a shell, |
| 2484 | // and SCRIPT might be intended to terminate |
| 2485 | // (e.g. contain a "halt" stmt). |
| 2486 | // ^C dropping user into a bc prompt instead of |
| 2487 | // the shell would be unexpected. |
| 2488 | xfunc_die(); |
| 2489 | } |
| 2490 | // ^C while interactive input |
| 2491 | G_interrupt = 0; |
| 2492 | // GNU bc says "interrupted execution." |
| 2493 | // GNU dc says "Interrupt!" |
| 2494 | fputs("\ninterrupted execution\n", stderr); |
| 2495 | } |
| 2496 | |
| 2497 | # if ENABLE_FEATURE_EDITING |
| 2498 | if (G_ttyin && fp == stdin) { |
| 2499 | int n, i; |
| 2500 | # define line_buf bb_common_bufsiz1 |
| 2501 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
| 2502 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 2503 | if (n == 0) // ^C |
| 2504 | goto intr; |
| 2505 | bc_vec_pushZeroByte(vec); // ^D or EOF (or error) |
| 2506 | return; |
| 2507 | } |
| 2508 | i = 0; |
| 2509 | for (;;) { |
| 2510 | char c = line_buf[i++]; |
| 2511 | if (!c) break; |
| 2512 | if (bad_input_byte(c)) goto again; |
| 2513 | } |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2514 | bc_vec_string(vec, n, line_buf); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2515 | # undef line_buf |
| 2516 | } else |
| 2517 | # endif |
| 2518 | #endif |
| 2519 | { |
| 2520 | int c; |
| 2521 | bool bad_chars = 0; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2522 | |
| 2523 | do { |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2524 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2525 | if (G_interrupt) { |
| 2526 | // ^C was pressed: ignore entire line, get another one |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2527 | bc_vec_pop_all(vec); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2528 | goto intr; |
| 2529 | } |
| 2530 | #endif |
| 2531 | do c = fgetc(fp); while (c == '\0'); |
| 2532 | if (c == EOF) { |
| 2533 | if (ferror(fp)) |
| 2534 | bb_perror_msg_and_die("input error"); |
| 2535 | // Note: EOF does not append '\n' |
| 2536 | break; |
| 2537 | } |
| 2538 | bad_chars |= bad_input_byte(c); |
| 2539 | bc_vec_pushByte(vec, (char)c); |
| 2540 | } while (c != '\n'); |
| 2541 | |
| 2542 | if (bad_chars) { |
| 2543 | // Bad chars on this line |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2544 | if (!G.prs.lex_filename) { // stdin |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2545 | // ignore entire line, get another one |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2546 | goto again; |
| 2547 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2548 | 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] | 2549 | } |
| 2550 | bc_vec_pushZeroByte(vec); |
| 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | // |
| 2555 | // Parsing routines |
| 2556 | // |
| 2557 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2558 | // "Input numbers may contain the characters 0-9 and A-Z. |
| 2559 | // (Note: They must be capitals. Lower case letters are variable names.) |
| 2560 | // Single digit numbers always have the value of the digit regardless of |
| 2561 | // the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes |
| 2562 | // all input digits greater or equal to ibase to the value of ibase-1. |
| 2563 | // This makes the number ZZZ always be the largest 3 digit number of the |
| 2564 | // input base." |
| 2565 | static bool xc_num_strValid(const char *val) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2566 | { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2567 | bool radix = false; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2568 | for (;;) { |
| 2569 | BcDig c = *val++; |
| 2570 | if (c == '\0') |
| 2571 | break; |
| 2572 | if (c == '.') { |
| 2573 | if (radix) return false; |
| 2574 | radix = true; |
| 2575 | continue; |
| 2576 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2577 | if ((c < '0' || c > '9') && (c < 'A' || c > 'Z')) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2578 | return false; |
| 2579 | } |
| 2580 | return true; |
| 2581 | } |
| 2582 | |
| 2583 | // Note: n is already "bc_num_zero()"ed, |
| 2584 | // leading zeroes in "val" are removed |
| 2585 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2586 | { |
| 2587 | size_t len, i; |
| 2588 | const char *ptr; |
| 2589 | |
| 2590 | len = strlen(val); |
| 2591 | if (len == 0) |
| 2592 | return; |
| 2593 | |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2594 | 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] | 2595 | |
| 2596 | ptr = strchr(val, '.'); |
| 2597 | |
| 2598 | n->rdx = 0; |
| 2599 | if (ptr != NULL) |
| 2600 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2601 | |
| 2602 | for (i = 0; val[i]; ++i) { |
| 2603 | if (val[i] != '0' && val[i] != '.') { |
| 2604 | // Not entirely zero value - convert it, and exit |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2605 | if (len == 1) { |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2606 | unsigned c = val[0] - '0'; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2607 | n->len = 1; |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2608 | if (c > 9) { // A-Z => 10-36 |
| 2609 | n->len = 2; |
| 2610 | c -= ('A' - '9' - 1); |
| 2611 | n->num[1] = c/10; |
| 2612 | c = c%10; |
| 2613 | } |
| 2614 | n->num[0] = c; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2615 | break; |
| 2616 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2617 | i = len - 1; |
| 2618 | for (;;) { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2619 | char c = val[i] - '0'; |
| 2620 | if (c > 9) // A-Z => 9 |
| 2621 | c = 9; |
| 2622 | n->num[n->len] = c; |
| 2623 | n->len++; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2624 | skip_dot: |
| 2625 | if (i == 0) break; |
| 2626 | if (val[--i] == '.') goto skip_dot; |
| 2627 | } |
| 2628 | break; |
| 2629 | } |
| 2630 | } |
| 2631 | // if for() exits without hitting if(), the value is entirely zero |
| 2632 | } |
| 2633 | |
| 2634 | // Note: n is already "bc_num_zero()"ed, |
| 2635 | // leading zeroes in "val" are removed |
| 2636 | static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t) |
| 2637 | { |
| 2638 | BcStatus s; |
| 2639 | BcNum temp, mult, result; |
| 2640 | BcNum base; |
| 2641 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
| 2642 | BcDig c = '\0'; |
| 2643 | unsigned long v; |
| 2644 | size_t i, digits; |
| 2645 | |
| 2646 | for (i = 0; ; ++i) { |
| 2647 | if (val[i] == '\0') |
| 2648 | return; |
| 2649 | if (val[i] != '.' && val[i] != '0') |
| 2650 | break; |
| 2651 | } |
| 2652 | |
| 2653 | bc_num_init_DEF_SIZE(&temp); |
| 2654 | bc_num_init_DEF_SIZE(&mult); |
| 2655 | base.cap = ARRAY_SIZE(base_digs); |
| 2656 | base.num = base_digs; |
| 2657 | bc_num_ulong2num(&base, base_t); |
| 2658 | |
| 2659 | for (;;) { |
| 2660 | c = *val++; |
| 2661 | if (c == '\0') goto int_err; |
| 2662 | if (c == '.') break; |
| 2663 | |
| 2664 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2665 | |
| 2666 | s = zbc_num_mul(n, &base, &mult, 0); |
| 2667 | if (s) goto int_err; |
| 2668 | bc_num_ulong2num(&temp, v); |
| 2669 | s = zbc_num_add(&mult, &temp, n, 0); |
| 2670 | if (s) goto int_err; |
| 2671 | } |
| 2672 | |
| 2673 | bc_num_init(&result, base.len); |
| 2674 | //bc_num_zero(&result); - already is |
| 2675 | bc_num_one(&mult); |
| 2676 | |
| 2677 | digits = 0; |
| 2678 | for (;;) { |
| 2679 | c = *val++; |
| 2680 | if (c == '\0') break; |
| 2681 | digits++; |
| 2682 | |
| 2683 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2684 | |
| 2685 | s = zbc_num_mul(&result, &base, &result, 0); |
| 2686 | if (s) goto err; |
| 2687 | bc_num_ulong2num(&temp, v); |
| 2688 | s = zbc_num_add(&result, &temp, &result, 0); |
| 2689 | if (s) goto err; |
| 2690 | s = zbc_num_mul(&mult, &base, &mult, 0); |
| 2691 | if (s) goto err; |
| 2692 | } |
| 2693 | |
| 2694 | s = zbc_num_div(&result, &mult, &result, digits); |
| 2695 | if (s) goto err; |
| 2696 | s = zbc_num_add(n, &result, n, digits); |
| 2697 | if (s) goto err; |
| 2698 | |
| 2699 | if (n->len != 0) { |
| 2700 | if (n->rdx < digits) |
| 2701 | bc_num_extend(n, digits - n->rdx); |
| 2702 | } else |
| 2703 | bc_num_zero(n); |
| 2704 | err: |
| 2705 | bc_num_free(&result); |
| 2706 | int_err: |
| 2707 | bc_num_free(&mult); |
| 2708 | bc_num_free(&temp); |
| 2709 | } |
| 2710 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2711 | 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] | 2712 | { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2713 | if (!xc_num_strValid(val)) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2714 | RETURN_STATUS(bc_error("bad number string")); |
| 2715 | |
| 2716 | bc_num_zero(n); |
| 2717 | while (*val == '0') val++; |
| 2718 | |
| 2719 | if (base_t == 10) |
| 2720 | bc_num_parseDecimal(n, val); |
| 2721 | else |
| 2722 | bc_num_parseBase(n, val, base_t); |
| 2723 | |
| 2724 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2725 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2726 | #define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2727 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2728 | static bool xc_lex_more_input(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2729 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2730 | BcParse *p = &G.prs; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2731 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2732 | bc_vec_pop_all(&G.input_buffer); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2733 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2734 | xc_read_line(&G.input_buffer, G.prs.lex_input_fp); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2735 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2736 | p->lex_inbuf = G.input_buffer.v; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2737 | // bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2738 | |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 2739 | return G.input_buffer.len > 1; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2740 | } |
| 2741 | |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2742 | // p->lex_inbuf points to the current string to be parsed. |
| 2743 | // if p->lex_inbuf points to '\0', it's either EOF or it points after |
| 2744 | // last processed line's terminating '\n' (and more reading needs to be done |
| 2745 | // to get next character). |
| 2746 | // |
| 2747 | // If you are in a situation where that is a possibility, call peek_inbuf(). |
| 2748 | // If necessary, it performs more reading and changes p->lex_inbuf, |
| 2749 | // then it returns *p->lex_inbuf (which will be '\0' only if it's EOF). |
| 2750 | // After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0', |
| 2751 | // it's ok to do p->lex_inbuf++ once without end-of-buffer checking. |
| 2752 | // |
| 2753 | // eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++": |
| 2754 | // it returns current char and advances the pointer (if not EOF). |
| 2755 | // After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid. |
| 2756 | // |
| 2757 | // In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf(): |
| 2758 | // unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY |
| 2759 | // on real EOF, not end-of-buffer. |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2760 | // |
| 2761 | // bc cases to test interactively: |
| 2762 | // 1 #comment\ - prints "1<newline>" at once (comment is not continued) |
| 2763 | // 1 #comment/* - prints "1<newline>" at once |
| 2764 | // 1 #comment" - prints "1<newline>" at once |
| 2765 | // 1\#comment - error at once (\ is not a line continuation) |
| 2766 | // 1 + /*"*/2 - prints "3<newline>" at once |
| 2767 | // 1 + /*#*/2 - prints "3<newline>" at once |
| 2768 | // "str\" - prints "str\" at once |
| 2769 | // "str#" - prints "str#" at once |
| 2770 | // "str/*" - prints "str/*" at once |
| 2771 | // "str#\ - waits for second line |
| 2772 | // end" - ...prints "str#\<newline>end" |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2773 | static char peek_inbuf(void) |
| 2774 | { |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2775 | if (*G.prs.lex_inbuf == '\0') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2776 | if (G.prs.lex_input_fp) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2777 | if (!xc_lex_more_input()) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2778 | G.prs.lex_input_fp = NULL; |
| 2779 | } |
| 2780 | return *G.prs.lex_inbuf; |
| 2781 | } |
| 2782 | static char eat_inbuf(void) |
| 2783 | { |
| 2784 | char c = peek_inbuf(); |
| 2785 | if (c) G.prs.lex_inbuf++; |
| 2786 | return c; |
| 2787 | } |
| 2788 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2789 | static void xc_lex_lineComment(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2790 | { |
| 2791 | BcParse *p = &G.prs; |
| 2792 | char c; |
| 2793 | |
| 2794 | // Try: echo -n '#foo' | bc |
| 2795 | p->lex = XC_LEX_WHITESPACE; |
| 2796 | |
| 2797 | // We depend here on input being done in whole lines: |
| 2798 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2799 | while ((c = *p->lex_inbuf) != '\n' && c != '\0') |
| 2800 | p->lex_inbuf++; |
| 2801 | } |
| 2802 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2803 | static void xc_lex_whitespace(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2804 | { |
| 2805 | BcParse *p = &G.prs; |
| 2806 | |
| 2807 | p->lex = XC_LEX_WHITESPACE; |
| 2808 | for (;;) { |
| 2809 | // We depend here on input being done in whole lines: |
| 2810 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2811 | char c = *p->lex_inbuf; |
| 2812 | if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
| 2813 | break; |
| 2814 | if (!isspace(c)) |
| 2815 | break; |
| 2816 | p->lex_inbuf++; |
| 2817 | } |
| 2818 | } |
| 2819 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2820 | static BC_STATUS zxc_lex_number(char last) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2821 | { |
| 2822 | BcParse *p = &G.prs; |
| 2823 | bool pt; |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2824 | char last_valid_ch; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2825 | |
| 2826 | bc_vec_pop_all(&p->lex_strnumbuf); |
| 2827 | bc_vec_pushByte(&p->lex_strnumbuf, last); |
| 2828 | |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2829 | // bc: "Input numbers may contain the characters 0-9 and A-Z. |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2830 | // (Note: They must be capitals. Lower case letters are variable names.) |
| 2831 | // Single digit numbers always have the value of the digit regardless of |
| 2832 | // the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes |
| 2833 | // all input digits greater or equal to ibase to the value of ibase-1. |
| 2834 | // This makes the number ZZZ always be the largest 3 digit number of the |
| 2835 | // input base." |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2836 | // dc only allows A-F, the rules about single-char and multi-char are the same. |
| 2837 | last_valid_ch = (IS_BC ? 'Z' : 'F'); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2838 | pt = (last == '.'); |
| 2839 | p->lex = XC_LEX_NUMBER; |
| 2840 | for (;;) { |
| 2841 | // We depend here on input being done in whole lines: |
| 2842 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2843 | char c = *p->lex_inbuf; |
| 2844 | check_c: |
| 2845 | if (c == '\0') |
| 2846 | break; |
| 2847 | if (c == '\\' && p->lex_inbuf[1] == '\n') { |
| 2848 | p->lex_inbuf += 2; |
| 2849 | p->lex_line++; |
| 2850 | c = peek_inbuf(); // force next line to be read |
| 2851 | goto check_c; |
| 2852 | } |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2853 | if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2854 | if (c != '.') break; |
| 2855 | // if '.' was already seen, stop on second one: |
| 2856 | if (pt) break; |
| 2857 | pt = true; |
| 2858 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2859 | // c is one of "0-9A-Z." |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2860 | last = c; |
| 2861 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 2862 | p->lex_inbuf++; |
| 2863 | } |
| 2864 | if (last == '.') // remove trailing '.' if any |
| 2865 | bc_vec_pop(&p->lex_strnumbuf); |
| 2866 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 2867 | |
| 2868 | G.err_line = G.prs.lex_line; |
| 2869 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2870 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2871 | #define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2872 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2873 | static void xc_lex_name(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2874 | { |
| 2875 | BcParse *p = &G.prs; |
| 2876 | size_t i; |
| 2877 | const char *buf; |
| 2878 | |
| 2879 | p->lex = XC_LEX_NAME; |
| 2880 | |
| 2881 | // Since names can't cross lines with \<newline>, |
| 2882 | // we depend on the fact that whole line is in the buffer |
| 2883 | i = 0; |
| 2884 | buf = p->lex_inbuf - 1; |
| 2885 | for (;;) { |
| 2886 | char c = buf[i]; |
| 2887 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 2888 | i++; |
| 2889 | } |
| 2890 | |
| 2891 | #if 0 // We do not protect against people with gigabyte-long names |
| 2892 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2893 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2894 | if (i > BC_MAX_STRING) |
| 2895 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2896 | } |
| 2897 | #endif |
| 2898 | bc_vec_string(&p->lex_strnumbuf, i, buf); |
| 2899 | |
| 2900 | // Increment the index. We minus 1 because it has already been incremented. |
| 2901 | p->lex_inbuf += i - 1; |
| 2902 | |
| 2903 | //return BC_STATUS_SUCCESS; |
| 2904 | } |
| 2905 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2906 | IF_BC(static BC_STATUS zbc_lex_token(void);) |
| 2907 | IF_DC(static BC_STATUS zdc_lex_token(void);) |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 2908 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
| 2909 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2910 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2911 | static BC_STATUS zxc_lex_next(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2912 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2913 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2914 | BcStatus s; |
| 2915 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2916 | p->lex_last = p->lex; |
Denys Vlasenko | 2f7352b | 2018-12-26 18:59:42 +0100 | [diff] [blame] | 2917 | //why? |
| 2918 | // if (p->lex_last == XC_LEX_EOF) |
| 2919 | // RETURN_STATUS(bc_error("end of file")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2920 | |
| 2921 | // Loop until failure or we don't have whitespace. This |
| 2922 | // is so the parser doesn't get inundated with whitespace. |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 2923 | // Comments are also XC_LEX_WHITESPACE tokens and eaten here. |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2924 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2925 | do { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2926 | if (*p->lex_inbuf == '\0') { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2927 | p->lex = XC_LEX_EOF; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2928 | if (peek_inbuf() == '\0') |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2929 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2930 | } |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 2931 | p->lex_next_at = p->lex_inbuf; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 2932 | dbg_lex("next string to parse:'%.*s'", |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2933 | (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at), |
| 2934 | p->lex_next_at |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 2935 | ); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2936 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2937 | IF_BC(s = zbc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2938 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2939 | IF_DC(s = zdc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2940 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2941 | } while (!s && p->lex == XC_LEX_WHITESPACE); |
| 2942 | dbg_lex("p->lex from string:%d", p->lex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2943 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2944 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2945 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2946 | #define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2947 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 2948 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2949 | static BC_STATUS zbc_lex_skip_if_at_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2950 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2951 | if (G.prs.lex == XC_LEX_NLINE) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2952 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2953 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2954 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2955 | #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] | 2956 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2957 | static BC_STATUS zbc_lex_next_and_skip_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2958 | { |
| 2959 | BcStatus s; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2960 | s = zxc_lex_next(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2961 | if (s) RETURN_STATUS(s); |
| 2962 | // if(cond)<newline>stmt is accepted too (but not 2+ newlines) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2963 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2964 | RETURN_STATUS(s); |
| 2965 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2966 | #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] | 2967 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2968 | static BC_STATUS zbc_lex_identifier(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2969 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2970 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2971 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2972 | unsigned i; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 2973 | const char *buf = p->lex_inbuf - 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2974 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2975 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 2976 | const char *keyword8 = bc_lex_kws[i].name8; |
| 2977 | unsigned j = 0; |
| 2978 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 2979 | j++; |
| 2980 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2981 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2982 | if (keyword8[j] != '\0') |
| 2983 | continue; |
| 2984 | match: |
| 2985 | // buf starts with keyword bc_lex_kws[i] |
Denys Vlasenko | 5acd14b | 2018-12-20 16:48:50 +0100 | [diff] [blame] | 2986 | if (isalnum(buf[j]) || buf[j]=='_') |
| 2987 | continue; // "ifz" does not match "if" keyword, "if." does |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2988 | p->lex = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 2989 | if (!keyword_is_POSIX(i)) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 2990 | s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8); |
| 2991 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | // We minus 1 because the index has already been incremented. |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 2995 | p->lex_inbuf += j - 1; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2996 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2997 | } |
| 2998 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2999 | xc_lex_name(); |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 3000 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3001 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3002 | if (p->lex_strnumbuf.len > 2) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3003 | // Prevent this: |
| 3004 | // >>> qwe=1 |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3005 | // bc: POSIX only allows one character names; this is bad: 'qwe=1 |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3006 | // ' |
| 3007 | unsigned len = strchrnul(buf, '\n') - buf; |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3008 | 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] | 3009 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3010 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3011 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3012 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3013 | #define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3014 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3015 | static BC_STATUS zbc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3016 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3017 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3018 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3019 | p->lex = XC_LEX_STR; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3020 | bc_vec_pop_all(&p->lex_strnumbuf); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3021 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3022 | char c = peek_inbuf(); // strings can cross lines |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3023 | if (c == '\0') { |
Denys Vlasenko | 8af1108 | 2018-12-26 21:01:41 +0100 | [diff] [blame] | 3024 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3025 | } |
| 3026 | if (c == '"') |
| 3027 | break; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3028 | if (c == '\n') |
| 3029 | p->lex_line++; |
| 3030 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 3031 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3032 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3033 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 3034 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3035 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3036 | G.err_line = p->lex_line; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3037 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3038 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3039 | #define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3040 | |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3041 | static void parse_lex_by_checking_eq_sign(unsigned with_and_without) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3042 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3043 | BcParse *p = &G.prs; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3044 | if (*p->lex_inbuf == '=') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3045 | // ^^^ not using peek_inbuf() since '==' etc can't be split across lines |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3046 | p->lex_inbuf++; |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3047 | with_and_without >>= 8; // store "with" value |
| 3048 | } // else store "without" value |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3049 | p->lex = (with_and_without & 0xff); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3050 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3051 | #define parse_lex_by_checking_eq_sign(with, without) \ |
| 3052 | parse_lex_by_checking_eq_sign(((with)<<8)|(without)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3053 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3054 | static BC_STATUS zbc_lex_comment(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3055 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3056 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3057 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3058 | p->lex = XC_LEX_WHITESPACE; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3059 | // here lex_inbuf is at '*' of opening comment delimiter |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3060 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3061 | char c; |
| 3062 | |
| 3063 | p->lex_inbuf++; |
| 3064 | c = peek_inbuf(); |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3065 | check_star: |
| 3066 | if (c == '*') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3067 | p->lex_inbuf++; |
Denys Vlasenko | 8af1108 | 2018-12-26 21:01:41 +0100 | [diff] [blame] | 3068 | c = *p->lex_inbuf; // no need to peek_inbuf() |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3069 | if (c == '/') |
| 3070 | break; |
| 3071 | goto check_star; |
| 3072 | } |
| 3073 | if (c == '\0') { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3074 | RETURN_STATUS(bc_error("unterminated comment")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3075 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3076 | if (c == '\n') |
| 3077 | p->lex_line++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3078 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3079 | p->lex_inbuf++; // skip trailing '/' |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3080 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3081 | G.err_line = p->lex_line; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3082 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3083 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3084 | #define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3085 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3086 | #undef zbc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3087 | static BC_STATUS zbc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3088 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3089 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3090 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3091 | char c = eat_inbuf(); |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3092 | char c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3093 | |
| 3094 | // This is the workhorse of the lexer. |
| 3095 | switch (c) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3096 | // case '\0': // probably never reached |
| 3097 | // p->lex_inbuf--; |
| 3098 | // p->lex = XC_LEX_EOF; |
| 3099 | // break; |
| 3100 | case '\n': |
| 3101 | p->lex_line++; |
| 3102 | p->lex = XC_LEX_NLINE; |
| 3103 | break; |
| 3104 | case '\t': |
| 3105 | case '\v': |
| 3106 | case '\f': |
| 3107 | case '\r': |
| 3108 | case ' ': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3109 | xc_lex_whitespace(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3110 | break; |
| 3111 | case '!': |
| 3112 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3113 | if (p->lex == BC_LEX_OP_BOOL_NOT) { |
| 3114 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3115 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3116 | } |
| 3117 | break; |
| 3118 | case '"': |
| 3119 | s = zbc_lex_string(); |
| 3120 | break; |
| 3121 | case '#': |
| 3122 | s = zbc_POSIX_does_not_allow("'#' script comments"); |
| 3123 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3124 | xc_lex_lineComment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3125 | break; |
| 3126 | case '%': |
| 3127 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS); |
| 3128 | break; |
| 3129 | case '&': |
| 3130 | c2 = *p->lex_inbuf; |
| 3131 | if (c2 == '&') { |
| 3132 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
| 3133 | if (s) RETURN_STATUS(s); |
| 3134 | p->lex_inbuf++; |
| 3135 | p->lex = BC_LEX_OP_BOOL_AND; |
| 3136 | } else { |
| 3137 | p->lex = XC_LEX_INVALID; |
| 3138 | s = bc_error_bad_character('&'); |
| 3139 | } |
| 3140 | break; |
| 3141 | case '(': |
| 3142 | case ')': |
| 3143 | p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3144 | break; |
| 3145 | case '*': |
| 3146 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY); |
| 3147 | break; |
| 3148 | case '+': |
| 3149 | c2 = *p->lex_inbuf; |
| 3150 | if (c2 == '+') { |
| 3151 | p->lex_inbuf++; |
| 3152 | p->lex = BC_LEX_OP_INC; |
| 3153 | } else |
| 3154 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS); |
| 3155 | break; |
| 3156 | case ',': |
| 3157 | p->lex = BC_LEX_COMMA; |
| 3158 | break; |
| 3159 | case '-': |
| 3160 | c2 = *p->lex_inbuf; |
| 3161 | if (c2 == '-') { |
| 3162 | p->lex_inbuf++; |
| 3163 | p->lex = BC_LEX_OP_DEC; |
| 3164 | } else |
| 3165 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS); |
| 3166 | break; |
| 3167 | case '.': |
| 3168 | if (isdigit(*p->lex_inbuf)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3169 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3170 | else { |
| 3171 | p->lex = BC_LEX_KEY_LAST; |
| 3172 | s = zbc_POSIX_does_not_allow("'.' as 'last'"); |
| 3173 | } |
| 3174 | break; |
| 3175 | case '/': |
| 3176 | c2 = *p->lex_inbuf; |
| 3177 | if (c2 == '*') |
| 3178 | s = zbc_lex_comment(); |
| 3179 | else |
| 3180 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE); |
| 3181 | break; |
| 3182 | case '0': |
| 3183 | case '1': |
| 3184 | case '2': |
| 3185 | case '3': |
| 3186 | case '4': |
| 3187 | case '5': |
| 3188 | case '6': |
| 3189 | case '7': |
| 3190 | case '8': |
| 3191 | case '9': |
| 3192 | case 'A': |
| 3193 | case 'B': |
| 3194 | case 'C': |
| 3195 | case 'D': |
| 3196 | case 'E': |
| 3197 | case 'F': |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3198 | case 'G': |
| 3199 | case 'H': |
| 3200 | case 'I': |
| 3201 | case 'J': |
| 3202 | case 'K': |
| 3203 | case 'L': |
| 3204 | case 'M': |
| 3205 | case 'N': |
| 3206 | case 'O': |
| 3207 | case 'P': |
| 3208 | case 'Q': |
| 3209 | case 'R': |
| 3210 | case 'S': |
| 3211 | case 'T': |
| 3212 | case 'U': |
| 3213 | case 'V': |
| 3214 | case 'W': |
| 3215 | case 'X': |
| 3216 | case 'Y': |
| 3217 | case 'Z': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3218 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3219 | break; |
| 3220 | case ';': |
| 3221 | p->lex = BC_LEX_SCOLON; |
| 3222 | break; |
| 3223 | case '<': |
| 3224 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT); |
| 3225 | break; |
| 3226 | case '=': |
| 3227 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3228 | break; |
| 3229 | case '>': |
| 3230 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT); |
| 3231 | break; |
| 3232 | case '[': |
| 3233 | case ']': |
| 3234 | p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3235 | break; |
| 3236 | case '\\': |
| 3237 | if (*p->lex_inbuf == '\n') { |
| 3238 | p->lex = XC_LEX_WHITESPACE; |
| 3239 | p->lex_inbuf++; |
| 3240 | } else |
| 3241 | s = bc_error_bad_character(c); |
| 3242 | break; |
| 3243 | case '^': |
| 3244 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER); |
| 3245 | break; |
| 3246 | case 'a': |
| 3247 | case 'b': |
| 3248 | case 'c': |
| 3249 | case 'd': |
| 3250 | case 'e': |
| 3251 | case 'f': |
| 3252 | case 'g': |
| 3253 | case 'h': |
| 3254 | case 'i': |
| 3255 | case 'j': |
| 3256 | case 'k': |
| 3257 | case 'l': |
| 3258 | case 'm': |
| 3259 | case 'n': |
| 3260 | case 'o': |
| 3261 | case 'p': |
| 3262 | case 'q': |
| 3263 | case 'r': |
| 3264 | case 's': |
| 3265 | case 't': |
| 3266 | case 'u': |
| 3267 | case 'v': |
| 3268 | case 'w': |
| 3269 | case 'x': |
| 3270 | case 'y': |
| 3271 | case 'z': |
| 3272 | s = zbc_lex_identifier(); |
| 3273 | break; |
| 3274 | case '{': |
| 3275 | case '}': |
| 3276 | p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3277 | break; |
| 3278 | case '|': |
| 3279 | c2 = *p->lex_inbuf; |
| 3280 | if (c2 == '|') { |
| 3281 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
| 3282 | if (s) RETURN_STATUS(s); |
| 3283 | p->lex_inbuf++; |
| 3284 | p->lex = BC_LEX_OP_BOOL_OR; |
| 3285 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3286 | p->lex = XC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3287 | s = bc_error_bad_character(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3288 | } |
| 3289 | break; |
| 3290 | default: |
| 3291 | p->lex = XC_LEX_INVALID; |
| 3292 | s = bc_error_bad_character(c); |
| 3293 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3294 | } |
| 3295 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3296 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3297 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3298 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3299 | #endif // ENABLE_BC |
| 3300 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3301 | #if ENABLE_DC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3302 | static BC_STATUS zdc_lex_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3303 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3304 | BcParse *p = &G.prs; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3305 | if (G_exreg && isspace(*p->lex_inbuf)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3306 | xc_lex_whitespace(); // eats whitespace (but not newline) |
| 3307 | p->lex_inbuf++; // xc_lex_name() expects this |
| 3308 | xc_lex_name(); |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3309 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3310 | bc_vec_pop_all(&p->lex_strnumbuf); |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3311 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3312 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 3313 | p->lex = XC_LEX_NAME; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3314 | } |
| 3315 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3316 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3317 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3318 | #define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3319 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3320 | static BC_STATUS zdc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3321 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3322 | BcParse *p = &G.prs; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3323 | size_t depth; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3324 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3325 | p->lex = XC_LEX_STR; |
| 3326 | bc_vec_pop_all(&p->lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3327 | |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3328 | depth = 1; |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3329 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3330 | char c = peek_inbuf(); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3331 | if (c == '\0') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3332 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3333 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3334 | if (c == '[') depth++; |
| 3335 | if (c == ']') |
| 3336 | if (--depth == 0) |
| 3337 | break; |
| 3338 | if (c == '\n') |
| 3339 | p->lex_line++; |
| 3340 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 3341 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3342 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3343 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3344 | p->lex_inbuf++; // skip trailing ']' |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3345 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3346 | G.err_line = p->lex_line; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3347 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3348 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3349 | #define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3350 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3351 | #undef zdc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3352 | static BC_STATUS zdc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3353 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3354 | static const //BcLexType - should be this type, but narrower type saves size: |
| 3355 | uint8_t |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 3356 | dc_lex_regs[] ALIGN1 = { |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3357 | 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] | 3358 | XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON, |
| 3359 | 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] | 3360 | DC_LEX_STORE_PUSH, |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3361 | }; |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3362 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3363 | BcParse *p = &G.prs; |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3364 | BcStatus s; |
| 3365 | char c, c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3366 | size_t i; |
| 3367 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3368 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3369 | if (p->lex_last == dc_lex_regs[i]) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3370 | RETURN_STATUS(zdc_lex_register()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3371 | } |
| 3372 | |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3373 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3374 | c = eat_inbuf(); |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3375 | if (c >= '%' && c <= '~' |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3376 | && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3377 | ) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3378 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | // This is the workhorse of the lexer. |
| 3382 | switch (c) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3383 | // case '\0': // probably never reached |
| 3384 | // p->lex = XC_LEX_EOF; |
| 3385 | // break; |
| 3386 | case '\n': |
| 3387 | // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
| 3388 | // (and "case '\n':" is not just empty here) |
| 3389 | // only to allow interactive dc have a way to exit |
| 3390 | // "parse" stage of "parse,execute" loop |
| 3391 | // on <enter>, not on _next_ token (which would mean |
| 3392 | // commands are not executed on pressing <enter>). |
| 3393 | // IOW: typing "1p<enter>" should print "1" _at once_, |
| 3394 | // not after some more input. |
| 3395 | p->lex_line++; |
| 3396 | p->lex = XC_LEX_NLINE; |
| 3397 | break; |
| 3398 | case '\t': |
| 3399 | case '\v': |
| 3400 | case '\f': |
| 3401 | case '\r': |
| 3402 | case ' ': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3403 | xc_lex_whitespace(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3404 | break; |
| 3405 | case '!': |
| 3406 | c2 = *p->lex_inbuf; |
| 3407 | if (c2 == '=') |
| 3408 | p->lex = XC_LEX_OP_REL_NE; |
| 3409 | else if (c2 == '<') |
| 3410 | p->lex = XC_LEX_OP_REL_LE; |
| 3411 | else if (c2 == '>') |
| 3412 | p->lex = XC_LEX_OP_REL_GE; |
| 3413 | else |
| 3414 | RETURN_STATUS(bc_error_bad_character(c)); |
| 3415 | p->lex_inbuf++; |
| 3416 | break; |
| 3417 | case '#': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3418 | xc_lex_lineComment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3419 | break; |
| 3420 | case '.': |
| 3421 | if (isdigit(*p->lex_inbuf)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3422 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3423 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3424 | s = bc_error_bad_character(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3425 | break; |
| 3426 | case '0': |
| 3427 | case '1': |
| 3428 | case '2': |
| 3429 | case '3': |
| 3430 | case '4': |
| 3431 | case '5': |
| 3432 | case '6': |
| 3433 | case '7': |
| 3434 | case '8': |
| 3435 | case '9': |
| 3436 | case 'A': |
| 3437 | case 'B': |
| 3438 | case 'C': |
| 3439 | case 'D': |
| 3440 | case 'E': |
| 3441 | case 'F': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3442 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3443 | break; |
| 3444 | case '[': |
| 3445 | s = zdc_lex_string(); |
| 3446 | break; |
| 3447 | default: |
| 3448 | p->lex = XC_LEX_INVALID; |
| 3449 | s = bc_error_bad_character(c); |
| 3450 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3451 | } |
| 3452 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3453 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3454 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3455 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3456 | #endif // ENABLE_DC |
| 3457 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3458 | static void xc_parse_push(char i) |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3459 | { |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3460 | BcVec *code = &G.prs.func->code; |
| 3461 | dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i); |
| 3462 | bc_vec_pushByte(code, i); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3463 | } |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3464 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3465 | static void xc_parse_pushName(char *name) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3466 | { |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3467 | #if 1 |
| 3468 | BcVec *code = &G.prs.func->code; |
| 3469 | size_t pos = code->len; |
| 3470 | size_t len = strlen(name) + 1; |
| 3471 | |
| 3472 | bc_vec_expand(code, pos + len); |
| 3473 | strcpy(code->v + pos, name); |
| 3474 | code->len = pos + len; |
| 3475 | #else |
| 3476 | // Smaller code, but way slow: |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3477 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3478 | xc_parse_push(*name); |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3479 | } while (*name++); |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3480 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3481 | } |
| 3482 | |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3483 | // Indexes < 0xfc are encoded verbatim, else first byte is |
| 3484 | // 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes", |
| 3485 | // followed by that many bytes, lsb first. |
| 3486 | // (The above describes 32-bit case). |
| 3487 | #define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t)) |
| 3488 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3489 | static void xc_parse_pushIndex(size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3490 | { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3491 | size_t mask; |
| 3492 | unsigned amt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3493 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 3494 | dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx); |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3495 | if (idx < SMALL_INDEX_LIMIT) { |
| 3496 | goto push_idx; |
| 3497 | } |
| 3498 | |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3499 | mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8); |
| 3500 | amt = sizeof(idx); |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3501 | for (;;) { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3502 | if (idx & mask) break; |
| 3503 | mask >>= 8; |
| 3504 | amt--; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3505 | } |
| 3506 | // amt is at least 1 here - "one byte of length data follows" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3507 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3508 | xc_parse_push((SMALL_INDEX_LIMIT - 1) + amt); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3509 | |
| 3510 | while (idx != 0) { |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3511 | push_idx: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3512 | xc_parse_push((unsigned char)idx); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3513 | idx >>= 8; |
| 3514 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3515 | } |
| 3516 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3517 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3518 | static void bc_parse_pushJUMP(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3519 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3520 | xc_parse_push(BC_INST_JUMP); |
| 3521 | xc_parse_pushIndex(idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3522 | } |
| 3523 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3524 | static void bc_parse_pushJUMP_ZERO(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3525 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3526 | xc_parse_push(BC_INST_JUMP_ZERO); |
| 3527 | xc_parse_pushIndex(idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3528 | } |
| 3529 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3530 | static BC_STATUS zbc_parse_pushSTR(void) |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3531 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3532 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3533 | char *str = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3534 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3535 | xc_parse_push(XC_INST_STR); |
| 3536 | xc_parse_pushIndex(p->func->strs.len); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3537 | bc_vec_push(&p->func->strs, &str); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3538 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3539 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3540 | } |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 3541 | #define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3542 | #endif |
| 3543 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3544 | static void xc_parse_pushNUM(void) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3545 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3546 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3547 | char *num = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3548 | #if ENABLE_BC && ENABLE_DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3549 | 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] | 3550 | #elif ENABLE_BC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3551 | size_t idx = bc_vec_push(&p->func->consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3552 | #else // DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3553 | size_t idx = bc_vec_push(&G.prog.consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3554 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3555 | xc_parse_push(XC_INST_NUM); |
| 3556 | xc_parse_pushIndex(idx); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3557 | } |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3558 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3559 | static BC_STATUS zxc_parse_text_init(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3560 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3561 | G.prs.func = xc_program_func(G.prs.fidx); |
| 3562 | G.prs.lex_inbuf = text; |
| 3563 | G.prs.lex = G.prs.lex_last = XC_LEX_INVALID; |
| 3564 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3565 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3566 | #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] | 3567 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3568 | // Called when parsing or execution detects a failure, |
| 3569 | // resets execution structures. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3570 | static void xc_program_reset(void) |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3571 | { |
| 3572 | BcFunc *f; |
| 3573 | BcInstPtr *ip; |
| 3574 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3575 | bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3576 | bc_vec_pop_all(&G.prog.results); |
| 3577 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3578 | f = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3579 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 3580 | ip->inst_idx = f->code.len; |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3581 | } |
| 3582 | |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3583 | // Called when parsing code detects a failure, |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3584 | // resets parsing structures. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3585 | static void xc_parse_reset(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3586 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3587 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3588 | if (p->fidx != BC_PROG_MAIN) { |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3589 | bc_func_free(p->func); |
| 3590 | bc_func_init(p->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3591 | |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 3592 | p->fidx = BC_PROG_MAIN; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3593 | p->func = xc_program_func_BC_PROG_MAIN(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3594 | } |
| 3595 | |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 3596 | p->lex_inbuf += strlen(p->lex_inbuf); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3597 | p->lex = XC_LEX_EOF; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3598 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3599 | IF_BC(bc_vec_pop_all(&p->exits);) |
| 3600 | IF_BC(bc_vec_pop_all(&p->conds);) |
| 3601 | IF_BC(bc_vec_pop_all(&p->ops);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3602 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3603 | xc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3604 | } |
| 3605 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3606 | static void xc_parse_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3607 | { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3608 | IF_BC(bc_vec_free(&G.prs.exits);) |
| 3609 | IF_BC(bc_vec_free(&G.prs.conds);) |
| 3610 | IF_BC(bc_vec_free(&G.prs.ops);) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3611 | bc_vec_free(&G.prs.lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3612 | } |
| 3613 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3614 | static void xc_parse_create(size_t fidx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3615 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3616 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3617 | memset(p, 0, sizeof(BcParse)); |
| 3618 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3619 | bc_char_vec_init(&p->lex_strnumbuf); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3620 | IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);) |
| 3621 | IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);) |
| 3622 | IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3623 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 3624 | p->fidx = fidx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3625 | p->func = xc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3626 | } |
| 3627 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3628 | static void xc_program_add_fn(void) |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3629 | { |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3630 | //size_t idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3631 | BcFunc f; |
| 3632 | bc_func_init(&f); |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3633 | //idx = |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3634 | bc_vec_push(&G.prog.fns, &f); |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3635 | //return idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | #if ENABLE_BC |
| 3639 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3640 | // Note: takes ownership of 'name' (must be malloced) |
| 3641 | static size_t bc_program_addFunc(char *name) |
| 3642 | { |
| 3643 | size_t idx; |
| 3644 | BcId entry, *entry_ptr; |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3645 | int inserted; |
| 3646 | |
| 3647 | entry.name = name; |
| 3648 | entry.idx = G.prog.fns.len; |
| 3649 | |
| 3650 | inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx); |
| 3651 | if (!inserted) free(name); |
| 3652 | |
| 3653 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
| 3654 | idx = entry_ptr->idx; |
| 3655 | |
| 3656 | if (!inserted) { |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3657 | // There is already a function with this name. |
| 3658 | // It'll be redefined now, clear old definition. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3659 | BcFunc *func = xc_program_func(entry_ptr->idx); |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3660 | bc_func_free(func); |
| 3661 | bc_func_init(func); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3662 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3663 | xc_program_add_fn(); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | return idx; |
| 3667 | } |
| 3668 | |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 3669 | #define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops)) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3670 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3671 | // position of the first operator in the lex enum and adding the position of the |
| 3672 | // first in the expr enum. Note: This only works for binary operators. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3673 | #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] | 3674 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3675 | static BcStatus bc_parse_expr_empty_ok(uint8_t flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3676 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3677 | static BC_STATUS zbc_parse_expr(uint8_t flags) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3678 | { |
| 3679 | BcStatus s; |
| 3680 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3681 | s = bc_parse_expr_empty_ok(flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3682 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
| 3683 | RETURN_STATUS(bc_error("empty expression")); |
| 3684 | RETURN_STATUS(s); |
| 3685 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3686 | #define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3687 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3688 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3689 | #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] | 3690 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3691 | static BC_STATUS zbc_parse_stmt(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3692 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3693 | RETURN_STATUS(zbc_parse_stmt_possibly_auto(false)); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3694 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3695 | #define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3696 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3697 | 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] | 3698 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3699 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3700 | // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines. |
| 3701 | // Same for "else", "while()", "for()". |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3702 | BcStatus s = zbc_lex_next_and_skip_NLINE(); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3703 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3704 | if (p->lex == XC_LEX_NLINE) |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3705 | RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X)); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3706 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3707 | RETURN_STATUS(zbc_parse_stmt()); |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3708 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3709 | #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] | 3710 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3711 | 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] | 3712 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3713 | BcParse *p = &G.prs; |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3714 | char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op); |
| 3715 | bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3716 | |
| 3717 | while (p->ops.len > start) { |
Denys Vlasenko | 7b1df3d | 2018-12-14 23:12:48 +0100 | [diff] [blame] | 3718 | BcLexType t = BC_PARSE_TOP_OP(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3719 | if (t == BC_LEX_LPAREN) break; |
| 3720 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3721 | l = bc_parse_op_PREC(t - XC_LEX_1st_op); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3722 | if (l >= r && (l != r || !left)) break; |
| 3723 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3724 | xc_parse_push(BC_TOKEN_2_INST(t)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3725 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3726 | *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | bc_vec_push(&p->ops, &type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3730 | } |
| 3731 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3732 | 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] | 3733 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3734 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3735 | BcLexType top; |
| 3736 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3737 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3738 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3739 | top = BC_PARSE_TOP_OP(p); |
| 3740 | |
| 3741 | while (top != BC_LEX_LPAREN) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3742 | xc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3743 | |
| 3744 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3745 | *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3746 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3747 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3748 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3749 | top = BC_PARSE_TOP_OP(p); |
| 3750 | } |
| 3751 | |
| 3752 | bc_vec_pop(&p->ops); |
| 3753 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 3754 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3755 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3756 | #define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3757 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3758 | static BC_STATUS zbc_parse_params(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3759 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3760 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3761 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3762 | size_t nparams; |
| 3763 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3764 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3765 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3766 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3767 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3768 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3769 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3770 | nparams = 0; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3771 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3772 | for (;;) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3773 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3774 | if (s) RETURN_STATUS(s); |
| 3775 | nparams++; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3776 | if (p->lex != BC_LEX_COMMA) { |
| 3777 | if (p->lex == BC_LEX_RPAREN) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3778 | break; |
| 3779 | RETURN_STATUS(bc_error_bad_token()); |
| 3780 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3781 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3782 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3783 | } |
| 3784 | } |
| 3785 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3786 | xc_parse_push(BC_INST_CALL); |
| 3787 | xc_parse_pushIndex(nparams); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3788 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3789 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3790 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3791 | #define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3792 | |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 3793 | // Note: takes ownership of 'name' (must be malloced) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3794 | static BC_STATUS zbc_parse_call(char *name, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3795 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3796 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3797 | BcStatus s; |
| 3798 | BcId entry, *entry_ptr; |
| 3799 | size_t idx; |
| 3800 | |
| 3801 | entry.name = name; |
| 3802 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3803 | s = zbc_parse_params(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3804 | if (s) goto err; |
| 3805 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3806 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3807 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3808 | goto err; |
| 3809 | } |
| 3810 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3811 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3812 | |
| 3813 | if (idx == BC_VEC_INVALID_IDX) { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3814 | // No such function exists, create an empty one |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 3815 | bc_program_addFunc(name); |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3816 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3817 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3818 | free(name); |
| 3819 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3820 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3821 | xc_parse_pushIndex(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3822 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3823 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3824 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3825 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3826 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3827 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3828 | #define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3829 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3830 | static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3831 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3832 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3833 | BcStatus s; |
| 3834 | char *name; |
| 3835 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3836 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3837 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3838 | if (s) goto err; |
| 3839 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3840 | if (p->lex == BC_LEX_LBRACKET) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3841 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3842 | if (s) goto err; |
| 3843 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3844 | if (p->lex == BC_LEX_RBRACKET) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3845 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3846 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3847 | goto err; |
| 3848 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3849 | *type = XC_INST_ARRAY; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3850 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3851 | *type = XC_INST_ARRAY_ELEM; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3852 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3853 | s = zbc_parse_expr(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3854 | if (s) goto err; |
| 3855 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3856 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3857 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3858 | xc_parse_push(*type); |
| 3859 | xc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3860 | free(name); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3861 | } else if (p->lex == BC_LEX_LPAREN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3862 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3863 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3864 | goto err; |
| 3865 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3866 | *type = BC_INST_CALL; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3867 | s = zbc_parse_call(name, flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3868 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3869 | *type = XC_INST_VAR; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3870 | xc_parse_push(XC_INST_VAR); |
| 3871 | xc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3872 | free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3873 | } |
| 3874 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3875 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3876 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3877 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3878 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3879 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3880 | #define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3881 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3882 | static BC_STATUS zbc_parse_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3883 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3884 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3885 | BcStatus s; |
| 3886 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3887 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3888 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3889 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3890 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3891 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3892 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3893 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3894 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3895 | xc_parse_push(XC_INST_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3896 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 3897 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3898 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3899 | #define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3900 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3901 | 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] | 3902 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3903 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3904 | BcStatus s; |
| 3905 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3906 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3907 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3908 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3909 | |
| 3910 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3911 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3912 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3913 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3914 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3915 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3916 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3917 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3918 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3919 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3920 | *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3921 | xc_parse_push(*prev); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3922 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 3923 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3924 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3925 | #define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3926 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3927 | static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3928 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3929 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3930 | BcStatus s; |
| 3931 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3932 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3933 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3934 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3935 | if (p->lex != BC_LEX_LPAREN) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3936 | *type = XC_INST_SCALE; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3937 | xc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3938 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3939 | } |
| 3940 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3941 | *type = XC_INST_SCALE_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3942 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3943 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3944 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3945 | if (s) RETURN_STATUS(s); |
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 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3948 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3949 | if (p->lex != BC_LEX_RPAREN) |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3950 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3951 | xc_parse_push(XC_INST_SCALE_FUNC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3952 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3953 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3954 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3955 | #define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3956 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3957 | static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr, |
| 3958 | size_t *nexprs, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3959 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3960 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3961 | BcStatus s; |
| 3962 | BcLexType type; |
| 3963 | char inst; |
| 3964 | BcInst etype = *prev; |
| 3965 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3966 | if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM |
| 3967 | || etype == XC_INST_SCALE || etype == BC_INST_LAST |
| 3968 | || etype == XC_INST_IBASE || etype == XC_INST_OBASE |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3969 | ) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3970 | *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3971 | xc_parse_push(inst); |
| 3972 | s = zxc_lex_next(); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3973 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3974 | *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3975 | *paren_expr = true; |
| 3976 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3977 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3978 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3979 | type = p->lex; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3980 | |
| 3981 | // Because we parse the next part of the expression |
| 3982 | // right here, we need to increment this. |
| 3983 | *nexprs = *nexprs + 1; |
| 3984 | |
| 3985 | switch (type) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3986 | case XC_LEX_NAME: |
| 3987 | s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL); |
| 3988 | break; |
| 3989 | case BC_LEX_KEY_IBASE: |
| 3990 | case BC_LEX_KEY_LAST: |
| 3991 | case BC_LEX_KEY_OBASE: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3992 | xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
| 3993 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3994 | break; |
| 3995 | case BC_LEX_KEY_SCALE: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3996 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3997 | if (s) RETURN_STATUS(s); |
| 3998 | if (p->lex == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3999 | s = bc_error_bad_token(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4000 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4001 | xc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4002 | break; |
| 4003 | default: |
| 4004 | s = bc_error_bad_token(); |
| 4005 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4006 | } |
| 4007 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4008 | if (!s) xc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4009 | } |
| 4010 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4011 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4012 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4013 | #define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4014 | |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4015 | #if 0 |
| 4016 | #define BC_PARSE_LEAF(p, rparen) \ |
| 4017 | ((rparen) \ |
| 4018 | || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \ |
| 4019 | || (p) == BC_INST_INC_POST \ |
| 4020 | || (p) == BC_INST_DEC_POST \ |
| 4021 | ) |
| 4022 | #else |
| 4023 | static int ok_in_expr(BcInst p) |
| 4024 | { |
| 4025 | return (p >= XC_INST_NUM && p <= XC_INST_SQRT) |
| 4026 | || p == BC_INST_INC_POST |
| 4027 | || p == BC_INST_DEC_POST |
| 4028 | ; |
| 4029 | } |
| 4030 | #define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p)) |
| 4031 | #endif |
| 4032 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4033 | static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn, |
| 4034 | bool rparen, size_t *nexprs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4035 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4036 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4037 | BcStatus s; |
| 4038 | BcLexType type; |
| 4039 | BcInst etype = *prev; |
| 4040 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4041 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4042 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4043 | |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4044 | type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG; |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4045 | *prev = BC_TOKEN_2_INST(type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4046 | |
| 4047 | // We can just push onto the op stack because this is the largest |
| 4048 | // precedence operator that gets pushed. Inc/dec does not. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4049 | if (type != XC_LEX_OP_MINUS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4050 | bc_vec_push(&p->ops, &type); |
| 4051 | else |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4052 | bc_parse_operator(type, ops_bgn, nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4053 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4054 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4055 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4056 | #define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4057 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4058 | static BC_STATUS zbc_parse_print(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4059 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4060 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4061 | BcStatus s; |
| 4062 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4063 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4064 | for (;;) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4065 | s = zxc_lex_next(); |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4066 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4067 | type = p->lex; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4068 | if (type == XC_LEX_STR) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4069 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4070 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4071 | s = zbc_parse_expr(0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4072 | } |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4073 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4074 | xc_parse_push(XC_INST_PRINT_POP); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4075 | if (p->lex != BC_LEX_COMMA) |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4076 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4077 | } |
| 4078 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4079 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4080 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4081 | #define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4082 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4083 | static BC_STATUS zbc_parse_return(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4084 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4085 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4086 | BcStatus s; |
| 4087 | BcLexType t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4088 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4089 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4090 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4091 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4092 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4093 | t = p->lex; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4094 | if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4095 | xc_parse_push(BC_INST_RET0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4096 | else { |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4097 | bool paren = (t == BC_LEX_LPAREN); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4098 | s = bc_parse_expr_empty_ok(0); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4099 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4100 | xc_parse_push(BC_INST_RET0); |
| 4101 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4102 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4103 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4104 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4105 | if (!paren || p->lex_last != BC_LEX_RPAREN) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4106 | s = zbc_POSIX_requires("parentheses around return expressions"); |
| 4107 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4108 | } |
| 4109 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4110 | xc_parse_push(XC_INST_RET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4111 | } |
| 4112 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4113 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4114 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4115 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4116 | #define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4117 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4118 | static void rewrite_label_to_current(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4119 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4120 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4121 | size_t *label = bc_vec_item(&p->func->labels, idx); |
| 4122 | *label = p->func->code.len; |
| 4123 | } |
| 4124 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4125 | static BC_STATUS zbc_parse_if(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4126 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4127 | BcParse *p = &G.prs; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4128 | BcStatus s; |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4129 | size_t ip_idx; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4130 | |
| 4131 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4132 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4133 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4134 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4135 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4136 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4137 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4138 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4139 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4140 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4141 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4142 | // Encode "if zero, jump to ..." |
| 4143 | // Pushed value (destination of the jump) is uninitialized, |
| 4144 | // will be rewritten to be address of "end of if()" or of "else". |
| 4145 | ip_idx = bc_vec_push(&p->func->labels, &ip_idx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4146 | bc_parse_pushJUMP_ZERO(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4147 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4148 | s = zbc_parse_stmt_allow_NLINE_before(STRING_if); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4149 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4150 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4151 | dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex); |
| 4152 | if (p->lex == BC_LEX_KEY_ELSE) { |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4153 | size_t ip2_idx; |
Denys Vlasenko | 7415633 | 2018-12-16 21:21:27 +0100 | [diff] [blame] | 4154 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4155 | // Encode "after then_stmt, jump to end of if()" |
| 4156 | ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx); |
| 4157 | 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] | 4158 | bc_parse_pushJUMP(ip2_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4159 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4160 | 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] | 4161 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4162 | |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4163 | ip_idx = ip2_idx; |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4164 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4165 | s = zbc_parse_stmt_allow_NLINE_before(STRING_else); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4166 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4167 | } |
| 4168 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4169 | 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] | 4170 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4171 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4172 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
| 4173 | RETURN_STATUS(s); |
| 4174 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4175 | #define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS) |
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 | static BC_STATUS zbc_parse_while(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4178 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4179 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4180 | BcStatus s; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4181 | size_t cond_idx; |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4182 | size_t ip_idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4183 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4184 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4185 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4186 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4187 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4188 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4189 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4190 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4191 | ip_idx = cond_idx + 1; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4192 | bc_vec_push(&p->conds, &cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4193 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4194 | bc_vec_push(&p->exits, &ip_idx); |
| 4195 | bc_vec_push(&p->func->labels, &ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4196 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4197 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4198 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4199 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4200 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4201 | bc_parse_pushJUMP_ZERO(ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4202 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4203 | s = zbc_parse_stmt_allow_NLINE_before(STRING_while); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4204 | if (s) RETURN_STATUS(s); |
| 4205 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4206 | 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] | 4207 | bc_parse_pushJUMP(cond_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4208 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4209 | 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] | 4210 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4211 | |
| 4212 | bc_vec_pop(&p->exits); |
| 4213 | bc_vec_pop(&p->conds); |
| 4214 | |
| 4215 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4216 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4217 | #define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4218 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4219 | static BC_STATUS zbc_parse_for(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4220 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4221 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4222 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4223 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4224 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4225 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4226 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4227 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4228 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4229 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4230 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4231 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4232 | if (p->lex != BC_LEX_SCOLON) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4233 | s = zbc_parse_expr(0); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4234 | xc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4235 | if (s) RETURN_STATUS(s); |
| 4236 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4237 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
| 4238 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4239 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4240 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4241 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4242 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4243 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4244 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4245 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4246 | update_idx = cond_idx + 1; |
| 4247 | body_idx = update_idx + 1; |
| 4248 | exit_idx = body_idx + 1; |
| 4249 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4250 | if (p->lex != BC_LEX_SCOLON) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4251 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4252 | else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4253 | // Set this for the next call to xc_parse_pushNUM(). |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4254 | // This is safe to set because the current token is a semicolon, |
| 4255 | // which has no string requirement. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4256 | bc_vec_string(&p->lex_strnumbuf, 1, "1"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4257 | xc_parse_pushNUM(); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4258 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4259 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4260 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4261 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4262 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4263 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4264 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4265 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4266 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4267 | bc_parse_pushJUMP_ZERO(exit_idx); |
| 4268 | bc_parse_pushJUMP(body_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4269 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4270 | bc_vec_push(&p->conds, &update_idx); |
| 4271 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4272 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4273 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4274 | s = zbc_parse_expr(0); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4275 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4276 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4277 | xc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4278 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4279 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
| 4280 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4281 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4282 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4283 | bc_parse_pushJUMP(cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4284 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4285 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4286 | bc_vec_push(&p->exits, &exit_idx); |
| 4287 | bc_vec_push(&p->func->labels, &exit_idx); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4288 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4289 | s = zbc_parse_stmt_allow_NLINE_before(STRING_for); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4290 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4291 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4292 | 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] | 4293 | bc_parse_pushJUMP(update_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4294 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4295 | 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] | 4296 | rewrite_label_to_current(exit_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4297 | |
| 4298 | bc_vec_pop(&p->exits); |
| 4299 | bc_vec_pop(&p->conds); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4300 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4301 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4302 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4303 | #define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4304 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4305 | static BC_STATUS zbc_parse_break_or_continue(BcLexType type) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4306 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4307 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4308 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4309 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4310 | if (type == BC_LEX_KEY_BREAK) { |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4311 | if (p->exits.len == 0) // none of the enclosing blocks is a loop |
| 4312 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4313 | i = *(size_t*)bc_vec_top(&p->exits); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4314 | } else { |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4315 | i = *(size_t*)bc_vec_top(&p->conds); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4316 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4317 | bc_parse_pushJUMP(i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4318 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4319 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4320 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4321 | #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] | 4322 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4323 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var) |
| 4324 | { |
| 4325 | BcId *autoid; |
| 4326 | BcId a; |
| 4327 | size_t i; |
| 4328 | |
| 4329 | autoid = (void*)f->autos.v; |
| 4330 | for (i = 0; i < f->autos.len; i++, autoid++) { |
| 4331 | if (strcmp(name, autoid->name) == 0) |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 4332 | RETURN_STATUS(bc_error("duplicate function parameter or auto name")); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | a.idx = var; |
| 4336 | a.name = name; |
| 4337 | |
| 4338 | bc_vec_push(&f->autos, &a); |
| 4339 | |
| 4340 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 4341 | } |
| 4342 | #define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS) |
| 4343 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4344 | static BC_STATUS zbc_parse_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4345 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4346 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4347 | BcStatus s; |
| 4348 | bool var, comma = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4349 | char *name; |
| 4350 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4351 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4352 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4353 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4354 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4355 | RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4356 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4357 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4358 | p->fidx = bc_program_addFunc(name); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4359 | p->func = xc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4360 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4361 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4362 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4363 | if (p->lex != BC_LEX_LPAREN) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4364 | RETURN_STATUS(bc_error("bad function definition")); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4365 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4366 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4367 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4368 | while (p->lex != BC_LEX_RPAREN) { |
| 4369 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4370 | RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4371 | |
| 4372 | ++p->func->nparams; |
| 4373 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4374 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4375 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4376 | if (s) goto err; |
| 4377 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4378 | var = p->lex != BC_LEX_LBRACKET; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4379 | |
| 4380 | if (!var) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4381 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4382 | if (s) goto err; |
| 4383 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4384 | if (p->lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4385 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4386 | goto err; |
| 4387 | } |
| 4388 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4389 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4390 | if (s) goto err; |
| 4391 | } |
| 4392 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4393 | comma = p->lex == BC_LEX_COMMA; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4394 | if (comma) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4395 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4396 | if (s) goto err; |
| 4397 | } |
| 4398 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4399 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4400 | if (s) goto err; |
| 4401 | } |
| 4402 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4403 | if (comma) RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4404 | |
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); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4407 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4408 | if (p->lex != BC_LEX_LBRACE) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4409 | 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] | 4410 | if (s) RETURN_STATUS(s); |
| 4411 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4412 | |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4413 | // 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] | 4414 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4415 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4416 | //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] | 4417 | if (p->lex != BC_LEX_LBRACE) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4418 | RETURN_STATUS(bc_error("function { body } expected")); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4419 | |
| 4420 | p->in_funcdef++; // to determine whether "return" stmt is allowed, and such |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4421 | s = zbc_parse_stmt_possibly_auto(true); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4422 | p->in_funcdef--; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4423 | if (s) RETURN_STATUS(s); |
| 4424 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4425 | xc_parse_push(BC_INST_RET0); |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 4426 | |
| 4427 | // Subsequent code generation is into main program |
| 4428 | p->fidx = BC_PROG_MAIN; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4429 | p->func = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4430 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4431 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4432 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4433 | err: |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4434 | dbg_lex_done("%s:%d done (error)", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4435 | free(name); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4436 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4437 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4438 | #define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4439 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4440 | static BC_STATUS zbc_parse_auto(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4441 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4442 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4443 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4444 | char *name; |
| 4445 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4446 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4447 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4448 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4449 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4450 | for (;;) { |
| 4451 | bool var; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4452 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4453 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4454 | RETURN_STATUS(bc_error("bad 'auto' syntax")); |
| 4455 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4456 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4457 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4458 | if (s) goto err; |
| 4459 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4460 | var = (p->lex != BC_LEX_LBRACKET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4461 | if (!var) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4462 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4463 | if (s) goto err; |
| 4464 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4465 | if (p->lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4466 | s = bc_error("bad 'auto' syntax"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4467 | goto err; |
| 4468 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4469 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4470 | if (s) goto err; |
| 4471 | } |
| 4472 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4473 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4474 | if (s) goto err; |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4475 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4476 | if (p->lex == XC_LEX_NLINE |
| 4477 | || p->lex == BC_LEX_SCOLON |
| 4478 | //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}" |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4479 | ) { |
| 4480 | break; |
| 4481 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4482 | if (p->lex != BC_LEX_COMMA) |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4483 | RETURN_STATUS(bc_error("bad 'auto' syntax")); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4484 | s = zxc_lex_next(); // skip comma |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4485 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4486 | } |
| 4487 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4488 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4489 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4490 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4491 | free(name); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4492 | dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4493 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4494 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4495 | #define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4496 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4497 | #undef zbc_parse_stmt_possibly_auto |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4498 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4499 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4500 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4501 | BcStatus s = BC_STATUS_SUCCESS; |
| 4502 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4503 | 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] | 4504 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4505 | if (p->lex == XC_LEX_NLINE) { |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4506 | dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4507 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4508 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4509 | if (p->lex == BC_LEX_SCOLON) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4510 | dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4511 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4512 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4513 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4514 | if (p->lex == BC_LEX_LBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4515 | dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed); |
| 4516 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4517 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4518 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4519 | } while (p->lex == XC_LEX_NLINE); |
| 4520 | if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4521 | dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4522 | s = zbc_parse_auto(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4523 | if (s) RETURN_STATUS(s); |
| 4524 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4525 | while (p->lex != BC_LEX_RBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4526 | dbg_lex("%s:%d block parsing loop", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4527 | s = zbc_parse_stmt(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4528 | if (s) RETURN_STATUS(s); |
| 4529 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4530 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4531 | dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__); |
| 4532 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4533 | } |
| 4534 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4535 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
| 4536 | switch (p->lex) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4537 | case XC_LEX_OP_MINUS: |
| 4538 | case BC_LEX_OP_INC: |
| 4539 | case BC_LEX_OP_DEC: |
| 4540 | case BC_LEX_OP_BOOL_NOT: |
| 4541 | case BC_LEX_LPAREN: |
| 4542 | case XC_LEX_NAME: |
| 4543 | case XC_LEX_NUMBER: |
| 4544 | case BC_LEX_KEY_IBASE: |
| 4545 | case BC_LEX_KEY_LAST: |
| 4546 | case BC_LEX_KEY_LENGTH: |
| 4547 | case BC_LEX_KEY_OBASE: |
| 4548 | case BC_LEX_KEY_READ: |
| 4549 | case BC_LEX_KEY_SCALE: |
| 4550 | case BC_LEX_KEY_SQRT: |
| 4551 | s = zbc_parse_expr(BC_PARSE_PRINT); |
| 4552 | break; |
| 4553 | case XC_LEX_STR: |
| 4554 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4555 | xc_parse_push(XC_INST_PRINT_STR); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4556 | break; |
| 4557 | case BC_LEX_KEY_BREAK: |
| 4558 | case BC_LEX_KEY_CONTINUE: |
| 4559 | s = zbc_parse_break_or_continue(p->lex); |
| 4560 | break; |
| 4561 | case BC_LEX_KEY_FOR: |
| 4562 | s = zbc_parse_for(); |
| 4563 | break; |
| 4564 | case BC_LEX_KEY_HALT: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4565 | xc_parse_push(BC_INST_HALT); |
| 4566 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4567 | break; |
| 4568 | case BC_LEX_KEY_IF: |
| 4569 | s = zbc_parse_if(); |
| 4570 | break; |
| 4571 | case BC_LEX_KEY_LIMITS: |
| 4572 | // "limits" is a compile-time command, |
| 4573 | // the output is produced at _parse time_. |
| 4574 | printf( |
| 4575 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4576 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4577 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4578 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4579 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4580 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4581 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4582 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4583 | ); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4584 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4585 | break; |
| 4586 | case BC_LEX_KEY_PRINT: |
| 4587 | s = zbc_parse_print(); |
| 4588 | break; |
| 4589 | case BC_LEX_KEY_QUIT: |
| 4590 | // "quit" is a compile-time command. For example, |
| 4591 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4592 | // not when it is executed |
| 4593 | QUIT_OR_RETURN_TO_MAIN; |
| 4594 | case BC_LEX_KEY_RETURN: |
| 4595 | if (!p->in_funcdef) |
| 4596 | RETURN_STATUS(bc_error("'return' not in a function")); |
| 4597 | s = zbc_parse_return(); |
| 4598 | break; |
| 4599 | case BC_LEX_KEY_WHILE: |
| 4600 | s = zbc_parse_while(); |
| 4601 | break; |
| 4602 | default: |
| 4603 | s = bc_error_bad_token(); |
| 4604 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4605 | } |
| 4606 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4607 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4608 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4609 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4610 | #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] | 4611 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4612 | static BC_STATUS zbc_parse_stmt_or_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4613 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4614 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4615 | BcStatus s; |
| 4616 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4617 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4618 | if (p->lex == XC_LEX_EOF) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4619 | s = bc_error("end of file"); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4620 | else if (p->lex == BC_LEX_KEY_DEFINE) { |
| 4621 | dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4622 | s = zbc_parse_funcdef(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4623 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4624 | 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] | 4625 | s = zbc_parse_stmt(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4626 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4627 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4628 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4629 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4630 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4631 | #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] | 4632 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 4633 | // This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4634 | static BcStatus bc_parse_expr_empty_ok(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4635 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4636 | BcParse *p = &G.prs; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4637 | BcInst prev = XC_INST_PRINT; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4638 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4639 | unsigned nparens, nrelops; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4640 | bool paren_first, paren_expr, rprn, assign, bin_last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4641 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4642 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4643 | paren_first = (p->lex == BC_LEX_LPAREN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4644 | nparens = nrelops = 0; |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4645 | paren_expr = rprn = assign = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4646 | bin_last = true; |
| 4647 | |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4648 | for (;;) { |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4649 | bool get_token; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4650 | BcStatus s; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4651 | BcLexType t = p->lex; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4652 | |
| 4653 | if (!lex_allowed_in_bc_expr(t)) |
| 4654 | break; |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4655 | |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4656 | dbg_lex("%s:%d t:%d", __func__, __LINE__, t); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4657 | get_token = false; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4658 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4659 | switch (t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4660 | case BC_LEX_OP_INC: |
| 4661 | case BC_LEX_OP_DEC: |
| 4662 | dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__); |
| 4663 | s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags); |
| 4664 | rprn = bin_last = false; |
| 4665 | //get_token = false; - already is |
| 4666 | break; |
| 4667 | case XC_LEX_OP_MINUS: |
| 4668 | dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__); |
| 4669 | s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs); |
| 4670 | rprn = false; |
| 4671 | //get_token = false; - already is |
| 4672 | bin_last = (prev == XC_INST_MINUS); |
| 4673 | break; |
| 4674 | case BC_LEX_OP_ASSIGN_POWER: |
| 4675 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4676 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4677 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4678 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4679 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4680 | case BC_LEX_OP_ASSIGN: |
| 4681 | dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__); |
| 4682 | if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM |
| 4683 | && prev != XC_INST_SCALE && prev != XC_INST_IBASE |
| 4684 | && prev != XC_INST_OBASE && prev != BC_INST_LAST |
| 4685 | ) { |
| 4686 | return bc_error("bad assignment:" |
| 4687 | " left side must be variable" |
| 4688 | " or array element" |
| 4689 | ); // note: shared string |
| 4690 | } |
| 4691 | // Fallthrough. |
| 4692 | case XC_LEX_OP_POWER: |
| 4693 | case XC_LEX_OP_MULTIPLY: |
| 4694 | case XC_LEX_OP_DIVIDE: |
| 4695 | case XC_LEX_OP_MODULUS: |
| 4696 | case XC_LEX_OP_PLUS: |
| 4697 | case XC_LEX_OP_REL_EQ: |
| 4698 | case XC_LEX_OP_REL_LE: |
| 4699 | case XC_LEX_OP_REL_GE: |
| 4700 | case XC_LEX_OP_REL_NE: |
| 4701 | case XC_LEX_OP_REL_LT: |
| 4702 | case XC_LEX_OP_REL_GT: |
| 4703 | case BC_LEX_OP_BOOL_NOT: |
| 4704 | case BC_LEX_OP_BOOL_OR: |
| 4705 | case BC_LEX_OP_BOOL_AND: |
| 4706 | dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__); |
| 4707 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
| 4708 | || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT) |
| 4709 | ) { |
| 4710 | return bc_error_bad_expression(); |
| 4711 | } |
| 4712 | nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT); |
| 4713 | prev = BC_TOKEN_2_INST(t); |
| 4714 | bc_parse_operator(t, ops_bgn, &nexprs); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4715 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4716 | rprn = false; |
| 4717 | //get_token = false; - already is |
| 4718 | bin_last = (t != BC_LEX_OP_BOOL_NOT); |
| 4719 | break; |
| 4720 | case BC_LEX_LPAREN: |
| 4721 | dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__); |
| 4722 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4723 | return bc_error_bad_expression(); |
| 4724 | bc_vec_push(&p->ops, &t); |
| 4725 | nparens++; |
| 4726 | get_token = true; |
| 4727 | paren_expr = false; |
| 4728 | rprn = bin_last = false; |
| 4729 | break; |
| 4730 | case BC_LEX_RPAREN: |
| 4731 | dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__); |
| 4732 | if (bin_last || prev == XC_INST_BOOL_NOT) |
| 4733 | return bc_error_bad_expression(); |
| 4734 | if (nparens == 0) { |
| 4735 | goto exit_loop; |
| 4736 | } |
| 4737 | if (!paren_expr) { |
| 4738 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); |
| 4739 | return BC_STATUS_PARSE_EMPTY_EXP; |
| 4740 | } |
| 4741 | s = zbc_parse_rightParen(ops_bgn, &nexprs); |
| 4742 | nparens--; |
| 4743 | get_token = true; |
| 4744 | paren_expr = rprn = true; |
| 4745 | bin_last = false; |
| 4746 | break; |
| 4747 | case XC_LEX_NAME: |
| 4748 | dbg_lex("%s:%d LEX_NAME", __func__, __LINE__); |
| 4749 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4750 | return bc_error_bad_expression(); |
| 4751 | s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL); |
| 4752 | paren_expr = true; |
| 4753 | rprn = bin_last = false; |
| 4754 | //get_token = false; - already is |
| 4755 | nexprs++; |
| 4756 | break; |
| 4757 | case XC_LEX_NUMBER: |
| 4758 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
| 4759 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4760 | return bc_error_bad_expression(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4761 | xc_parse_pushNUM(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4762 | prev = XC_INST_NUM; |
| 4763 | get_token = true; |
| 4764 | paren_expr = true; |
| 4765 | rprn = bin_last = false; |
| 4766 | nexprs++; |
| 4767 | break; |
| 4768 | case BC_LEX_KEY_IBASE: |
| 4769 | case BC_LEX_KEY_LAST: |
| 4770 | case BC_LEX_KEY_OBASE: |
| 4771 | dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__); |
| 4772 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4773 | return bc_error_bad_expression(); |
| 4774 | prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4775 | xc_parse_push((char) prev); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4776 | get_token = true; |
| 4777 | paren_expr = true; |
| 4778 | rprn = bin_last = false; |
| 4779 | nexprs++; |
| 4780 | break; |
| 4781 | case BC_LEX_KEY_LENGTH: |
| 4782 | case BC_LEX_KEY_SQRT: |
| 4783 | dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__); |
| 4784 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4785 | return bc_error_bad_expression(); |
| 4786 | s = zbc_parse_builtin(t, flags, &prev); |
| 4787 | get_token = true; |
| 4788 | paren_expr = true; |
| 4789 | rprn = bin_last = false; |
| 4790 | nexprs++; |
| 4791 | break; |
| 4792 | case BC_LEX_KEY_READ: |
| 4793 | dbg_lex("%s:%d LEX_READ", __func__, __LINE__); |
| 4794 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4795 | return bc_error_bad_expression(); |
| 4796 | s = zbc_parse_read(); |
| 4797 | prev = XC_INST_READ; |
| 4798 | get_token = true; |
| 4799 | paren_expr = true; |
| 4800 | rprn = bin_last = false; |
| 4801 | nexprs++; |
| 4802 | break; |
| 4803 | case BC_LEX_KEY_SCALE: |
| 4804 | dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__); |
| 4805 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4806 | return bc_error_bad_expression(); |
| 4807 | s = zbc_parse_scale(&prev, flags); |
| 4808 | prev = XC_INST_SCALE; |
| 4809 | //get_token = false; - already is |
| 4810 | paren_expr = true; |
| 4811 | rprn = bin_last = false; |
| 4812 | nexprs++; |
| 4813 | break; |
| 4814 | default: |
| 4815 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4816 | } |
| 4817 | |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4818 | if (s || G_interrupt) // error, or ^C: stop parsing |
| 4819 | return BC_STATUS_FAILURE; |
| 4820 | if (get_token) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4821 | s = zxc_lex_next(); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4822 | if (s) return s; |
| 4823 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4824 | } |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4825 | exit_loop: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4826 | |
| 4827 | while (p->ops.len > ops_bgn) { |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4828 | BcLexType top = BC_PARSE_TOP_OP(p); |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 4829 | assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4830 | |
| 4831 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4832 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4833 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4834 | xc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4835 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4836 | nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4837 | bc_vec_pop(&p->ops); |
| 4838 | } |
| 4839 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4840 | if (prev == XC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4841 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4842 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4843 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4844 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4845 | s = zbc_POSIX_does_not_allow("comparison operators outside if or loops"); |
| 4846 | if (s) return s; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4847 | } else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4848 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4849 | s = zbc_POSIX_requires("exactly one comparison operator per condition"); |
| 4850 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | if (flags & BC_PARSE_PRINT) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4854 | if (paren_first || !assign) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4855 | xc_parse_push(XC_INST_PRINT); |
| 4856 | xc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4857 | } |
| 4858 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4859 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4860 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4861 | } |
| 4862 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4863 | #endif // ENABLE_BC |
| 4864 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 4865 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 4866 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4867 | static BC_STATUS zdc_parse_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4868 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4869 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4870 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4871 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4872 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4873 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4874 | if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4875 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4876 | xc_parse_pushName(p->lex_strnumbuf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4877 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4878 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4879 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4880 | #define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4881 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4882 | static void dc_parse_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4883 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4884 | BcParse *p = &G.prs; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4885 | char *str; |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4886 | size_t len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4887 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4888 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4889 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4890 | str = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4891 | xc_parse_push(XC_INST_STR); |
| 4892 | xc_parse_pushIndex(len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4893 | bc_vec_push(&G.prog.strs, &str); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4894 | |
| 4895 | // Explanation needed here |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4896 | xc_program_add_fn(); |
| 4897 | p->func = xc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4898 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4899 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4900 | } |
| 4901 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4902 | 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] | 4903 | { |
| 4904 | BcStatus s; |
| 4905 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4906 | xc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4907 | if (name) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4908 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4909 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | if (store) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4913 | xc_parse_push(DC_INST_SWAP); |
| 4914 | xc_parse_push(XC_INST_ASSIGN); |
| 4915 | xc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4916 | } |
| 4917 | |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4918 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4919 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4920 | #define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4921 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4922 | static BC_STATUS zdc_parse_cond(uint8_t inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4923 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4924 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4925 | BcStatus s; |
| 4926 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4927 | xc_parse_push(inst); |
| 4928 | xc_parse_push(DC_INST_EXEC_COND); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4929 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4930 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4931 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4932 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4933 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4934 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4935 | |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4936 | // Note that 'else' part can not be on the next line: |
| 4937 | // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2" |
| 4938 | // 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] | 4939 | if (p->lex == DC_LEX_ELSE) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4940 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4941 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4942 | s = zxc_lex_next(); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4943 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4944 | xc_parse_push('\0'); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4945 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4946 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4947 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4948 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4949 | #define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4950 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4951 | static BC_STATUS zdc_parse_token(BcLexType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4952 | { |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4953 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4954 | uint8_t inst; |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4955 | bool assign, get_token; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4956 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4957 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4958 | s = BC_STATUS_SUCCESS; |
| 4959 | get_token = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4960 | switch (t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4961 | case XC_LEX_OP_REL_EQ: |
| 4962 | case XC_LEX_OP_REL_LE: |
| 4963 | case XC_LEX_OP_REL_GE: |
| 4964 | case XC_LEX_OP_REL_NE: |
| 4965 | case XC_LEX_OP_REL_LT: |
| 4966 | case XC_LEX_OP_REL_GT: |
| 4967 | dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__); |
| 4968 | s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ); |
| 4969 | get_token = false; |
| 4970 | break; |
| 4971 | case DC_LEX_SCOLON: |
| 4972 | case DC_LEX_COLON: |
| 4973 | dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__); |
| 4974 | s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON); |
| 4975 | break; |
| 4976 | case XC_LEX_STR: |
| 4977 | dbg_lex("%s:%d LEX_STR", __func__, __LINE__); |
| 4978 | dc_parse_string(); |
| 4979 | break; |
| 4980 | case XC_LEX_NEG: |
| 4981 | dbg_lex("%s:%d LEX_NEG", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4982 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4983 | if (s) RETURN_STATUS(s); |
| 4984 | if (G.prs.lex != XC_LEX_NUMBER) |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4985 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4986 | xc_parse_pushNUM(); |
| 4987 | xc_parse_push(XC_INST_NEG); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4988 | break; |
| 4989 | case XC_LEX_NUMBER: |
| 4990 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4991 | xc_parse_pushNUM(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4992 | break; |
| 4993 | case DC_LEX_READ: |
| 4994 | dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4995 | xc_parse_push(XC_INST_READ); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4996 | break; |
| 4997 | case DC_LEX_OP_ASSIGN: |
| 4998 | case DC_LEX_STORE_PUSH: |
| 4999 | dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__); |
| 5000 | assign = (t == DC_LEX_OP_ASSIGN); |
| 5001 | inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR; |
| 5002 | s = zdc_parse_mem(inst, true, assign); |
| 5003 | break; |
| 5004 | case DC_LEX_LOAD: |
| 5005 | case DC_LEX_LOAD_POP: |
| 5006 | dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__); |
| 5007 | inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD; |
| 5008 | s = zdc_parse_mem(inst, true, false); |
| 5009 | break; |
| 5010 | case DC_LEX_STORE_IBASE: |
| 5011 | case DC_LEX_STORE_SCALE: |
| 5012 | case DC_LEX_STORE_OBASE: |
| 5013 | dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__); |
| 5014 | inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE; |
| 5015 | s = zdc_parse_mem(inst, false, true); |
| 5016 | break; |
| 5017 | default: |
| 5018 | dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__); |
| 5019 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5020 | } |
| 5021 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5022 | if (!s && get_token) s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5023 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5024 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5025 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5026 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5027 | #define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5028 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5029 | static BC_STATUS zdc_parse_expr(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5030 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5031 | BcParse *p = &G.prs; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5032 | int i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5033 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5034 | i = (int)p->lex - (int)XC_LEX_OP_POWER; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5035 | if (i >= 0) { |
| 5036 | BcInst inst = dc_LEX_to_INST[i]; |
| 5037 | if (inst != DC_INST_INVALID) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5038 | xc_parse_push(inst); |
| 5039 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5040 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5041 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5042 | RETURN_STATUS(zdc_parse_token(p->lex)); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5043 | } |
| 5044 | #define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
| 5045 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5046 | static BC_STATUS zdc_parse_exprs_until_eof(void) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5047 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5048 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5049 | dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex); |
| 5050 | while (p->lex != XC_LEX_EOF) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5051 | BcStatus s = zdc_parse_expr(); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5052 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5053 | } |
| 5054 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5055 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5056 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5057 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5058 | #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] | 5059 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5060 | #endif // ENABLE_DC |
| 5061 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5062 | // |
| 5063 | // Execution engine |
| 5064 | // |
| 5065 | |
| 5066 | #define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n))) |
| 5067 | #define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n))) |
| 5068 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5069 | static BcVec* xc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5070 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5071 | BcId e, *ptr; |
| 5072 | BcVec *v, *map; |
| 5073 | size_t i; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5074 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5075 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5076 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5077 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5078 | |
| 5079 | e.name = id; |
| 5080 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5081 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5082 | |
| 5083 | if (new) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5084 | BcVec v2; |
| 5085 | bc_array_init(&v2, var); |
| 5086 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5087 | } |
| 5088 | |
| 5089 | ptr = bc_vec_item(map, i); |
| 5090 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5091 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5092 | } |
| 5093 | |
Denys Vlasenko | 8287b1c | 2018-12-21 22:43:53 +0100 | [diff] [blame] | 5094 | // 'num' need not be initialized on entry |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 5095 | static BC_STATUS zxc_program_num(BcResult *r, BcNum **num) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5096 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5097 | switch (r->t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5098 | case XC_RESULT_STR: |
| 5099 | case XC_RESULT_TEMP: |
| 5100 | case XC_RESULT_IBASE: |
| 5101 | case XC_RESULT_SCALE: |
| 5102 | case XC_RESULT_OBASE: |
| 5103 | *num = &r->d.n; |
| 5104 | break; |
| 5105 | case XC_RESULT_CONSTANT: { |
| 5106 | BcStatus s; |
| 5107 | char *str; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5108 | size_t len; |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 5109 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5110 | str = *xc_program_const(r->d.id.idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5111 | len = strlen(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5112 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5113 | bc_num_init(&r->d.n, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5114 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 5115 | s = zxc_num_parse(&r->d.n, str, G.prog.ib_t); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5116 | if (s) { |
| 5117 | bc_num_free(&r->d.n); |
| 5118 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5119 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5120 | *num = &r->d.n; |
| 5121 | r->t = XC_RESULT_TEMP; |
| 5122 | break; |
| 5123 | } |
| 5124 | case XC_RESULT_VAR: |
| 5125 | case XC_RESULT_ARRAY: |
| 5126 | case XC_RESULT_ARRAY_ELEM: { |
| 5127 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5128 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5129 | v = xc_program_search(r->d.id.name, r->t == XC_RESULT_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5130 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5131 | if (r->t == XC_RESULT_ARRAY_ELEM) { |
| 5132 | v = bc_vec_top(v); |
| 5133 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5134 | *num = bc_vec_item(v, r->d.id.idx); |
| 5135 | } else |
| 5136 | *num = bc_vec_top(v); |
| 5137 | break; |
| 5138 | } |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5139 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5140 | case BC_RESULT_LAST: |
| 5141 | *num = &G.prog.last; |
| 5142 | break; |
| 5143 | case BC_RESULT_ONE: |
| 5144 | *num = &G.prog.one; |
| 5145 | break; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5146 | #endif |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5147 | #if SANITY_CHECKS |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5148 | default: |
| 5149 | // Testing the theory that dc does not reach LAST/ONE |
| 5150 | bb_error_msg_and_die("BUG:%d", r->t); |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5151 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5152 | } |
| 5153 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5154 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5155 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5156 | #define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5157 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5158 | static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5159 | BcResult **r, BcNum **rn, bool assign) |
| 5160 | { |
| 5161 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5162 | BcResultType lt, rt; |
| 5163 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5164 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5165 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5166 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5167 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5168 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5169 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 5170 | s = zxc_program_num(*l, ln); |
| 5171 | if (s) RETURN_STATUS(s); |
| 5172 | s = zxc_program_num(*r, rn); |
| 5173 | if (s) RETURN_STATUS(s); |
| 5174 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5175 | lt = (*l)->t; |
| 5176 | rt = (*r)->t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5177 | |
| 5178 | // We run this again under these conditions in case any vector has been |
| 5179 | // reallocated out from under the BcNums or arrays we had. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5180 | if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 5181 | s = zxc_program_num(*l, ln); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5182 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5183 | } |
| 5184 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5185 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5186 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5187 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5188 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5189 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5190 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5191 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5192 | #define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5193 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5194 | static void xc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5195 | { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5196 | r->t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5197 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5198 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5199 | } |
| 5200 | |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5201 | // Note: *r and *n need not be initialized by caller |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5202 | static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5203 | { |
| 5204 | BcStatus s; |
| 5205 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5206 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5207 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5208 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5209 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 5210 | s = zxc_program_num(*r, n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5211 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5212 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5213 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5214 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5215 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5216 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5217 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5218 | #define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5219 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5220 | static void xc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5221 | { |
| 5222 | r->t = t; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5223 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5224 | } |
| 5225 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5226 | static BC_STATUS zxc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5227 | { |
| 5228 | BcStatus s; |
| 5229 | BcResult *opd1, *opd2, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5230 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5231 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5232 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5233 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5234 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5235 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5236 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5237 | 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] | 5238 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5239 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5240 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5241 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5242 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5243 | bc_num_free(&res.d.n); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5244 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5245 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5246 | #define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5247 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5248 | static BC_STATUS zxc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5249 | { |
| 5250 | BcStatus s; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5251 | BcParse sv_parse; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5252 | BcVec buf; |
| 5253 | BcInstPtr ip; |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5254 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5255 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 5256 | bc_char_vec_init(&buf); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5257 | xc_read_line(&buf, stdin); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5258 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5259 | f = xc_program_func(BC_PROG_READ); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5260 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5261 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5262 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5263 | xc_parse_create(BC_PROG_READ); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 5264 | //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] | 5265 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5266 | s = zxc_parse_text_init(buf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5267 | if (s) goto exec_err; |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5268 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5269 | IF_BC(s = zbc_parse_expr(0)); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5270 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5271 | IF_DC(s = zdc_parse_exprs_until_eof()); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5272 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5273 | if (s) goto exec_err; |
| 5274 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5275 | if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5276 | s = bc_error("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5277 | goto exec_err; |
| 5278 | } |
| 5279 | |
| 5280 | ip.func = BC_PROG_READ; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5281 | ip.inst_idx = 0; |
| 5282 | IF_BC(ip.results_len_before_call = G.prog.results.len;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5283 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5284 | xc_parse_push(XC_INST_RET); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5285 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5286 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5287 | exec_err: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5288 | xc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5289 | G.prs = sv_parse; // struct copy |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5290 | bc_vec_free(&buf); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5291 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5292 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5293 | #define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5294 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5295 | static size_t xc_program_index(char *code, size_t *bgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5296 | { |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5297 | unsigned char *bytes = (void*)(code + *bgn); |
| 5298 | unsigned amt; |
| 5299 | unsigned i; |
| 5300 | size_t res; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5301 | |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5302 | amt = *bytes++; |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 5303 | if (amt < SMALL_INDEX_LIMIT) { |
| 5304 | *bgn += 1; |
| 5305 | return amt; |
| 5306 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 5307 | amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5308 | *bgn += amt + 1; |
| 5309 | |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5310 | res = 0; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 5311 | i = 0; |
| 5312 | do { |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5313 | res |= (size_t)(*bytes++) << i; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 5314 | i += 8; |
| 5315 | } while (--amt != 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5316 | |
| 5317 | return res; |
| 5318 | } |
| 5319 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5320 | static char *xc_program_name(char *code, size_t *bgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5321 | { |
Denys Vlasenko | 694d298 | 2018-12-18 19:17:11 +0100 | [diff] [blame] | 5322 | code += *bgn; |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 5323 | *bgn += strlen(code) + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5324 | |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 5325 | return xstrdup(code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5326 | } |
| 5327 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5328 | static void xc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5329 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5330 | #if ENABLE_DC |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5331 | if (!str[0]) { |
Denys Vlasenko | 2c6f563 | 2018-12-11 21:21:14 +0100 | [diff] [blame] | 5332 | // Example: echo '[]ap' | dc |
| 5333 | // should print two bytes: 0x00, 0x0A |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5334 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5335 | return; |
| 5336 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5337 | #endif |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5338 | while (*str) { |
| 5339 | int c = *str++; |
| 5340 | if (c != '\\' || !*str) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5341 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5342 | else { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5343 | c = *str++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5344 | switch (c) { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5345 | case 'a': |
| 5346 | bb_putchar('\a'); |
| 5347 | break; |
| 5348 | case 'b': |
| 5349 | bb_putchar('\b'); |
| 5350 | break; |
| 5351 | case '\\': |
| 5352 | case 'e': |
| 5353 | bb_putchar('\\'); |
| 5354 | break; |
| 5355 | case 'f': |
| 5356 | bb_putchar('\f'); |
| 5357 | break; |
| 5358 | case 'n': |
| 5359 | bb_putchar('\n'); |
| 5360 | G.prog.nchars = SIZE_MAX; |
| 5361 | break; |
| 5362 | case 'r': |
| 5363 | bb_putchar('\r'); |
| 5364 | break; |
| 5365 | case 'q': |
| 5366 | bb_putchar('"'); |
| 5367 | break; |
| 5368 | case 't': |
| 5369 | bb_putchar('\t'); |
| 5370 | break; |
| 5371 | default: |
| 5372 | // Just print the backslash and following character. |
| 5373 | bb_putchar('\\'); |
| 5374 | ++G.prog.nchars; |
| 5375 | bb_putchar(c); |
| 5376 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5377 | } |
| 5378 | } |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5379 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5380 | } |
| 5381 | } |
| 5382 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5383 | static void bc_num_printNewline(void) |
| 5384 | { |
| 5385 | if (G.prog.nchars == G.prog.len - 1) { |
| 5386 | bb_putchar('\\'); |
| 5387 | bb_putchar('\n'); |
| 5388 | G.prog.nchars = 0; |
| 5389 | } |
| 5390 | } |
| 5391 | |
| 5392 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5393 | 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] | 5394 | { |
| 5395 | (void) radix; |
| 5396 | bb_putchar((char) num); |
| 5397 | G.prog.nchars += width; |
| 5398 | } |
| 5399 | #endif |
| 5400 | |
| 5401 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
| 5402 | { |
| 5403 | size_t exp, pow; |
| 5404 | |
| 5405 | bc_num_printNewline(); |
| 5406 | bb_putchar(radix ? '.' : ' '); |
| 5407 | ++G.prog.nchars; |
| 5408 | |
| 5409 | bc_num_printNewline(); |
| 5410 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 5411 | continue; |
| 5412 | |
| 5413 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
| 5414 | size_t dig; |
| 5415 | bc_num_printNewline(); |
| 5416 | dig = num / pow; |
| 5417 | num -= dig * pow; |
| 5418 | bb_putchar(((char) dig) + '0'); |
| 5419 | } |
| 5420 | } |
| 5421 | |
| 5422 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
| 5423 | { |
| 5424 | if (radix) { |
| 5425 | bc_num_printNewline(); |
| 5426 | bb_putchar('.'); |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5427 | G.prog.nchars++; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | bc_num_printNewline(); |
| 5431 | bb_putchar(bb_hexdigits_upcase[num]); |
| 5432 | G.prog.nchars += width; |
| 5433 | } |
| 5434 | |
| 5435 | static void bc_num_printDecimal(BcNum *n) |
| 5436 | { |
| 5437 | size_t i, rdx = n->rdx - 1; |
| 5438 | |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5439 | if (n->neg) { |
| 5440 | bb_putchar('-'); |
| 5441 | G.prog.nchars++; |
| 5442 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5443 | |
| 5444 | for (i = n->len - 1; i < n->len; --i) |
| 5445 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
| 5446 | } |
| 5447 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5448 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
| 5449 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5450 | 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] | 5451 | { |
| 5452 | BcStatus s; |
| 5453 | BcVec stack; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5454 | BcNum base; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5455 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5456 | BcNum intp, fracp, digit, frac_len; |
| 5457 | unsigned long dig, *ptr; |
| 5458 | size_t i; |
| 5459 | bool radix; |
| 5460 | |
| 5461 | if (n->len == 0) { |
| 5462 | print(0, width, false); |
| 5463 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5464 | } |
| 5465 | |
| 5466 | bc_vec_init(&stack, sizeof(long), NULL); |
| 5467 | bc_num_init(&intp, n->len); |
| 5468 | bc_num_init(&fracp, n->rdx); |
| 5469 | bc_num_init(&digit, width); |
| 5470 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 5471 | bc_num_copy(&intp, n); |
| 5472 | bc_num_one(&frac_len); |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5473 | base.cap = ARRAY_SIZE(base_digs); |
| 5474 | base.num = base_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5475 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5476 | |
| 5477 | bc_num_truncate(&intp, intp.rdx); |
| 5478 | s = zbc_num_sub(n, &intp, &fracp, 0); |
| 5479 | if (s) goto err; |
| 5480 | |
| 5481 | while (intp.len != 0) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5482 | s = zbc_num_divmod(&intp, &base, &intp, &digit, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5483 | if (s) goto err; |
| 5484 | s = zbc_num_ulong(&digit, &dig); |
| 5485 | if (s) goto err; |
| 5486 | bc_vec_push(&stack, &dig); |
| 5487 | } |
| 5488 | |
| 5489 | for (i = 0; i < stack.len; ++i) { |
| 5490 | ptr = bc_vec_item_rev(&stack, i); |
| 5491 | print(*ptr, width, false); |
| 5492 | } |
| 5493 | |
| 5494 | if (!n->rdx) goto err; |
| 5495 | |
| 5496 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5497 | s = zbc_num_mul(&fracp, &base, &fracp, n->rdx); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5498 | if (s) goto err; |
| 5499 | s = zbc_num_ulong(&fracp, &dig); |
| 5500 | if (s) goto err; |
| 5501 | bc_num_ulong2num(&intp, dig); |
| 5502 | s = zbc_num_sub(&fracp, &intp, &fracp, 0); |
| 5503 | if (s) goto err; |
| 5504 | print(dig, width, radix); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5505 | s = zbc_num_mul(&frac_len, &base, &frac_len, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5506 | if (s) goto err; |
| 5507 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5508 | err: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5509 | bc_num_free(&frac_len); |
| 5510 | bc_num_free(&digit); |
| 5511 | bc_num_free(&fracp); |
| 5512 | bc_num_free(&intp); |
| 5513 | bc_vec_free(&stack); |
| 5514 | RETURN_STATUS(s); |
| 5515 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5516 | #define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5517 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5518 | static BC_STATUS zxc_num_printBase(BcNum *n) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5519 | { |
| 5520 | BcStatus s; |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5521 | size_t width; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5522 | BcNumDigitOp print; |
| 5523 | bool neg = n->neg; |
| 5524 | |
| 5525 | if (neg) { |
| 5526 | bb_putchar('-'); |
| 5527 | G.prog.nchars++; |
| 5528 | } |
| 5529 | |
| 5530 | n->neg = false; |
| 5531 | |
| 5532 | if (G.prog.ob_t <= BC_NUM_MAX_IBASE) { |
| 5533 | width = 1; |
| 5534 | print = bc_num_printHex; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5535 | } else { |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5536 | unsigned i = G.prog.ob_t - 1; |
| 5537 | width = 0; |
| 5538 | for (;;) { |
| 5539 | width++; |
| 5540 | i /= 10; |
| 5541 | if (i == 0) |
| 5542 | break; |
| 5543 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5544 | print = bc_num_printDigits; |
| 5545 | } |
| 5546 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5547 | s = zxc_num_printNum(n, G.prog.ob_t, width, print); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5548 | n->neg = neg; |
| 5549 | |
| 5550 | RETURN_STATUS(s); |
| 5551 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5552 | #define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5553 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5554 | static BC_STATUS zxc_num_print(BcNum *n, bool newline) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5555 | { |
| 5556 | BcStatus s = BC_STATUS_SUCCESS; |
| 5557 | |
| 5558 | bc_num_printNewline(); |
| 5559 | |
| 5560 | if (n->len == 0) { |
| 5561 | bb_putchar('0'); |
| 5562 | ++G.prog.nchars; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5563 | } else if (G.prog.ob_t == 10) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5564 | bc_num_printDecimal(n); |
| 5565 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5566 | s = zxc_num_printBase(n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5567 | |
| 5568 | if (newline) { |
| 5569 | bb_putchar('\n'); |
| 5570 | G.prog.nchars = 0; |
| 5571 | } |
| 5572 | |
| 5573 | RETURN_STATUS(s); |
| 5574 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5575 | #define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5576 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5577 | static BC_STATUS zxc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5578 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5579 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5580 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5581 | BcNum *num; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5582 | bool pop = (inst != XC_INST_PRINT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5583 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5584 | if (!STACK_HAS_MORE_THAN(&G.prog.results, idx)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5585 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5586 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5587 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 5588 | s = zxc_program_num(r, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5589 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5590 | |
| 5591 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5592 | s = zxc_num_print(num, !pop); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5593 | #if ENABLE_BC |
| 5594 | if (!s && IS_BC) bc_num_copy(&G.prog.last, num); |
| 5595 | #endif |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5596 | } else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5597 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5598 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5599 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5600 | str = *xc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5601 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5602 | if (inst == XC_INST_PRINT_STR) { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5603 | for (;;) { |
| 5604 | char c = *str++; |
| 5605 | if (c == '\0') break; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5606 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5607 | ++G.prog.nchars; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5608 | if (c == '\n') G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5609 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5610 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5611 | xc_program_printString(str); |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5612 | if (inst == XC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5613 | } |
| 5614 | } |
| 5615 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5616 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5617 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5618 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5619 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5620 | #define zxc_program_print(...) (zxc_program_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5621 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5622 | static BC_STATUS zxc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5623 | { |
| 5624 | BcStatus s; |
| 5625 | BcResult res, *ptr; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5626 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5627 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5628 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5629 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5630 | |
| 5631 | bc_num_init(&res.d.n, num->len); |
| 5632 | bc_num_copy(&res.d.n, num); |
| 5633 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5634 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5635 | xc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5636 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5637 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5638 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5639 | #define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5640 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5641 | static BC_STATUS zxc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5642 | { |
| 5643 | BcStatus s; |
| 5644 | BcResult *opd1, *opd2, res; |
| 5645 | BcNum *n1, *n2; |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5646 | ssize_t cond; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5647 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5648 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5649 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5650 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5651 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5652 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5653 | if (inst == XC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5654 | cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero); |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5655 | else if (inst == XC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5656 | cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5657 | else { |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5658 | cond = bc_num_cmp(n1, n2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5659 | switch (inst) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5660 | case XC_INST_REL_EQ: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5661 | cond = (cond == 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5662 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5663 | case XC_INST_REL_LE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5664 | cond = (cond <= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5665 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5666 | case XC_INST_REL_GE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5667 | cond = (cond >= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5668 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5669 | case XC_INST_REL_LT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5670 | cond = (cond < 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5671 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5672 | case XC_INST_REL_GT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5673 | cond = (cond > 0); |
| 5674 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5675 | default: // = case XC_INST_REL_NE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5676 | //cond = (cond != 0); - not needed |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5677 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5678 | } |
| 5679 | } |
| 5680 | |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5681 | if (cond) bc_num_one(&res.d.n); |
| 5682 | //else bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5683 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5684 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5685 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5686 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5687 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5688 | #define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5689 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5690 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5691 | static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5692 | { |
| 5693 | BcNum n2; |
| 5694 | BcResult res; |
| 5695 | |
| 5696 | memset(&n2, 0, sizeof(BcNum)); |
| 5697 | n2.rdx = res.d.id.idx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5698 | res.t = XC_RESULT_STR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5699 | |
| 5700 | if (!push) { |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5701 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5702 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5703 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5704 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5705 | } |
| 5706 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5707 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5708 | bc_vec_push(v, &n2); |
| 5709 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5710 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5711 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5712 | #define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5713 | #endif // ENABLE_DC |
| 5714 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5715 | static BC_STATUS zxc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5716 | { |
| 5717 | BcStatus s; |
| 5718 | BcResult *ptr, r; |
| 5719 | BcVec *v; |
| 5720 | BcNum *n; |
| 5721 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5722 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5723 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5724 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5725 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5726 | if ((ptr->t == XC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5727 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5728 | v = xc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5729 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5730 | #if ENABLE_DC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5731 | if (ptr->t == XC_RESULT_STR && !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5732 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5733 | if (ptr->t == XC_RESULT_STR) |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5734 | RETURN_STATUS(zdc_program_assignStr(ptr, v, true)); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5735 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5736 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 5737 | s = zxc_program_num(ptr, &n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5738 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5739 | |
| 5740 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5741 | v = xc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5742 | |
| 5743 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5744 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5745 | bc_num_copy(&r.d.n, n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5746 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5747 | bc_array_init(&r.d.v, true); |
| 5748 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 5749 | } |
| 5750 | |
| 5751 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5752 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5753 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5754 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5755 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5756 | #define zxc_program_copyToVar(...) (zxc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5757 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5758 | static BC_STATUS zxc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5759 | { |
| 5760 | BcStatus s; |
| 5761 | BcResult *left, *right, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5762 | BcNum *l, *r; |
| 5763 | bool assign = (inst == XC_INST_ASSIGN); |
| 5764 | bool ib, sc; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5765 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5766 | s = zxc_program_binOpPrep(&left, &l, &right, &r, assign); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5767 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5768 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5769 | ib = left->t == XC_RESULT_IBASE; |
| 5770 | sc = left->t == XC_RESULT_SCALE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5771 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5772 | #if ENABLE_DC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5773 | if (right->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5774 | BcVec *v; |
| 5775 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5776 | if (left->t != XC_RESULT_VAR) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5777 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5778 | v = xc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5779 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5780 | RETURN_STATUS(zdc_program_assignStr(right, v, false)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5781 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5782 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5783 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5784 | if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP) |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5785 | RETURN_STATUS(bc_error("bad assignment:" |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 5786 | " left side must be variable" |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5787 | " or array element" |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 5788 | )); // note: shared string |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5789 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5790 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5791 | if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5792 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5793 | |
| 5794 | if (assign) |
| 5795 | bc_num_copy(l, r); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5796 | else { |
| 5797 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5798 | 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] | 5799 | } |
| 5800 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5801 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5802 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5803 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5804 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5805 | if (ib || sc || left->t == XC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5806 | static const char *const msg[] = { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5807 | "bad ibase; must be [2,16]", //XC_RESULT_IBASE |
| 5808 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE |
| 5809 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5810 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5811 | size_t *ptr; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5812 | size_t max; |
| 5813 | unsigned long val; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5814 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5815 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5816 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5817 | s = left->t - XC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5818 | if (sc) { |
| 5819 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5820 | ptr = &G.prog.scale; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5821 | } else { |
| 5822 | if (val < 2) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5823 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5824 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5825 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5826 | } |
| 5827 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5828 | if (val > max) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5829 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5830 | |
| 5831 | *ptr = (size_t) val; |
| 5832 | s = BC_STATUS_SUCCESS; |
| 5833 | } |
| 5834 | |
| 5835 | bc_num_init(&res.d.n, l->len); |
| 5836 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5837 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5838 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5839 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5840 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5841 | #define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5842 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5843 | #if !ENABLE_DC |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5844 | #define xc_program_pushVar(code, bgn, pop, copy) \ |
| 5845 | xc_program_pushVar(code, bgn) |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5846 | // for bc, 'pop' and 'copy' are always false |
| 5847 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5848 | static BC_STATUS xc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5849 | bool pop, bool copy) |
| 5850 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5851 | BcResult r; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5852 | char *name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5853 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5854 | r.t = XC_RESULT_VAR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5855 | r.d.id.name = name; |
| 5856 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5857 | #if ENABLE_DC |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5858 | if (pop || copy) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5859 | BcVec *v = xc_program_search(name, true); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5860 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5861 | |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5862 | free(name); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5863 | if (!STACK_HAS_MORE_THAN(v, 1 - copy)) { |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5864 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5865 | } |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5866 | |
| 5867 | if (!BC_PROG_STR(num)) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5868 | r.t = XC_RESULT_TEMP; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5869 | bc_num_init_DEF_SIZE(&r.d.n); |
| 5870 | bc_num_copy(&r.d.n, num); |
| 5871 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5872 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5873 | r.d.id.idx = num->rdx; |
| 5874 | } |
| 5875 | |
| 5876 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5877 | } |
| 5878 | #endif // ENABLE_DC |
| 5879 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5880 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5881 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 5882 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5883 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5884 | #define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5885 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5886 | 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] | 5887 | { |
| 5888 | BcStatus s = BC_STATUS_SUCCESS; |
| 5889 | BcResult r; |
| 5890 | BcNum *num; |
| 5891 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5892 | r.d.id.name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5893 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5894 | if (inst == XC_INST_ARRAY) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5895 | r.t = XC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5896 | bc_vec_push(&G.prog.results, &r); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5897 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5898 | BcResult *operand; |
| 5899 | unsigned long temp; |
| 5900 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5901 | s = zxc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5902 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5903 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5904 | if (s) goto err; |
| 5905 | |
| 5906 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 5907 | 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] | 5908 | goto err; |
| 5909 | } |
| 5910 | |
| 5911 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5912 | xc_program_retire(&r, XC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5913 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5914 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5915 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5916 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5917 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5918 | #define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5919 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5920 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5921 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5922 | { |
| 5923 | BcStatus s; |
| 5924 | BcResult *ptr, res, copy; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5925 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5926 | char inst2 = inst; |
| 5927 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5928 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5929 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5930 | |
| 5931 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5932 | copy.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5933 | bc_num_init(©.d.n, num->len); |
| 5934 | bc_num_copy(©.d.n, num); |
| 5935 | } |
| 5936 | |
| 5937 | res.t = BC_RESULT_ONE; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5938 | inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST) |
| 5939 | ? BC_INST_ASSIGN_PLUS |
| 5940 | : BC_INST_ASSIGN_MINUS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5941 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5942 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5943 | s = zxc_program_assign(inst); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5944 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5945 | |
| 5946 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5947 | bc_result_pop_and_push(©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5948 | } |
| 5949 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5950 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5951 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5952 | #define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5953 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5954 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5955 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5956 | BcInstPtr ip; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5957 | size_t i, nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5958 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5959 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5960 | BcResult *arg; |
| 5961 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5962 | nparams = xc_program_index(code, idx); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5963 | ip.inst_idx = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5964 | ip.func = xc_program_index(code, idx); |
| 5965 | func = xc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5966 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 5967 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5968 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 5969 | } |
| 5970 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5971 | 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] | 5972 | } |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5973 | ip.results_len_before_call = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5974 | |
| 5975 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5976 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5977 | |
| 5978 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5979 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5980 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5981 | if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5982 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5983 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5984 | s = zxc_program_copyToVar(a->name, a->idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5985 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5986 | } |
| 5987 | |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 5988 | a = bc_vec_item(&func->autos, i); |
| 5989 | for (; i < func->autos.len; i++, a++) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5990 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5991 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5992 | v = xc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5993 | if (a->idx) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5994 | BcNum n2; |
| 5995 | bc_num_init_DEF_SIZE(&n2); |
| 5996 | bc_vec_push(v, &n2); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5997 | } else { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5998 | BcVec v2; |
| 5999 | bc_array_init(&v2, true); |
| 6000 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6001 | } |
| 6002 | } |
| 6003 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6004 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6005 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6006 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6007 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6008 | #define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6009 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6010 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6011 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6012 | BcResult res; |
| 6013 | BcFunc *f; |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6014 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6015 | size_t i; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6016 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6017 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6018 | if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->results_len_before_call + (inst == XC_INST_RET))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6019 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6020 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6021 | f = xc_program_func(ip->func); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6022 | res.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6023 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6024 | if (inst == XC_INST_RET) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6025 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6026 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6027 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6028 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 6029 | s = zxc_program_num(operand, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6030 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6031 | bc_num_init(&res.d.n, num->len); |
| 6032 | bc_num_copy(&res.d.n, num); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6033 | } else { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6034 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6035 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6036 | } |
| 6037 | |
| 6038 | // 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] | 6039 | a = (void*)f->autos.v; |
| 6040 | for (i = 0; i < f->autos.len; i++, a++) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6041 | BcVec *v; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6042 | v = xc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6043 | bc_vec_pop(v); |
| 6044 | } |
| 6045 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6046 | bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6047 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6048 | bc_vec_pop(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6049 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6050 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6051 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6052 | #define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6053 | #endif // ENABLE_BC |
| 6054 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6055 | static unsigned long xc_program_scale(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6056 | { |
| 6057 | return (unsigned long) n->rdx; |
| 6058 | } |
| 6059 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6060 | static unsigned long xc_program_len(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6061 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6062 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6063 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6064 | if (n->rdx != len) return len; |
| 6065 | for (;;) { |
| 6066 | if (len == 0) break; |
| 6067 | len--; |
| 6068 | if (n->num[len] != 0) break; |
| 6069 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6070 | return len; |
| 6071 | } |
| 6072 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6073 | static BC_STATUS zxc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6074 | { |
| 6075 | BcStatus s; |
| 6076 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6077 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6078 | BcResult res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6079 | bool len = (inst == XC_INST_LENGTH); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6080 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6081 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6082 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6083 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6084 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 6085 | s = zxc_program_num(opnd, &num); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6086 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6087 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6088 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6089 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6090 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6091 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6092 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6093 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6094 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6095 | if (inst == XC_INST_SQRT) |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6096 | s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6097 | #if ENABLE_BC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6098 | else if (len != 0 && opnd->t == XC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6099 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6100 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6101 | #endif |
| 6102 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6103 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6104 | char **str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6105 | 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] | 6106 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6107 | str = xc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6108 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6109 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6110 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6111 | else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6112 | 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] | 6113 | } |
| 6114 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6115 | xc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6116 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6117 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6118 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6119 | #define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6120 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6121 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6122 | static BC_STATUS zdc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6123 | { |
| 6124 | BcStatus s; |
| 6125 | BcResult *opd1, *opd2, res, res2; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6126 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6127 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6128 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6129 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6130 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6131 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6132 | bc_num_init(&res2.d.n, n2->len); |
| 6133 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6134 | 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] | 6135 | if (s) goto err; |
| 6136 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6137 | xc_program_binOpRetire(&res2); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6138 | res.t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6139 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6140 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6141 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6142 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6143 | bc_num_free(&res2.d.n); |
| 6144 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6145 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6146 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6147 | #define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6148 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6149 | static BC_STATUS zdc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6150 | { |
| 6151 | BcStatus s; |
| 6152 | BcResult *r1, *r2, *r3, res; |
| 6153 | BcNum *n1, *n2, *n3; |
| 6154 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6155 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 2)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6156 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6157 | s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6158 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6159 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6160 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 6161 | s = zxc_program_num(r1, &n1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6162 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6163 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6164 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6165 | |
| 6166 | // Make sure that the values have their pointers updated, if necessary. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6167 | if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6168 | if (r1->t == r2->t) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 6169 | s = zxc_program_num(r2, &n2); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6170 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6171 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6172 | if (r1->t == r3->t) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 6173 | s = zxc_program_num(r3, &n3); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6174 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6175 | } |
| 6176 | } |
| 6177 | |
| 6178 | bc_num_init(&res.d.n, n3->len); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6179 | s = zdc_num_modexp(n1, n2, n3, &res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6180 | if (s) goto err; |
| 6181 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6182 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6183 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6184 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6185 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6186 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6187 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6188 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6189 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6190 | #define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6191 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6192 | static void dc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6193 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6194 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6195 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6196 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6197 | res.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6198 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6199 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6200 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6201 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6202 | } |
| 6203 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6204 | static BC_STATUS zdc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6205 | { |
| 6206 | BcStatus s; |
| 6207 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6208 | BcNum *num, n; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6209 | char **strs; |
| 6210 | char *str; |
| 6211 | char c; |
| 6212 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6213 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6214 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6215 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6216 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6217 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 6218 | s = zxc_program_num(r, &num); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6219 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6220 | |
| 6221 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 6222 | unsigned long val; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6223 | BcNum strmb; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6224 | BcDig strmb_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6225 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6226 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6227 | bc_num_copy(&n, num); |
| 6228 | bc_num_truncate(&n, n.rdx); |
| 6229 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6230 | strmb.cap = ARRAY_SIZE(strmb_digs); |
| 6231 | strmb.num = strmb_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6232 | bc_num_ulong2num(&strmb, 0x100); |
| 6233 | s = zbc_num_mod(&n, &strmb, &n, 0); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6234 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6235 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6236 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6237 | if (s) goto num_err; |
| 6238 | |
| 6239 | c = (char) val; |
| 6240 | |
| 6241 | bc_num_free(&n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6242 | } else { |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6243 | char *sp; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6244 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6245 | sp = *xc_program_str(idx); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6246 | c = sp[0]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6247 | } |
| 6248 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6249 | strs = (void*)G.prog.strs.v; |
| 6250 | for (idx = 0; idx < G.prog.strs.len; idx++) { |
| 6251 | if (strs[idx][0] == c && strs[idx][1] == '\0') { |
| 6252 | goto dup; |
| 6253 | } |
| 6254 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6255 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6256 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6257 | //str[1] = '\0'; - already is |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6258 | bc_vec_push(&G.prog.strs, &str); |
| 6259 | dup: |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6260 | res.t = XC_RESULT_STR; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6261 | res.d.id.idx = idx; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 6262 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6263 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6264 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6265 | num_err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6266 | bc_num_free(&n); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6267 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6268 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6269 | #define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6270 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6271 | static BC_STATUS zdc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6272 | { |
| 6273 | BcStatus s; |
| 6274 | BcResult *r; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6275 | BcNum *n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6276 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6277 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6278 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6279 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6280 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6281 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 6282 | s = zxc_program_num(r, &n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6283 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6284 | |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6285 | if (BC_PROG_NUM(r, n)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6286 | s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar); |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6287 | } else { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6288 | char *str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6289 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6290 | str = *xc_program_str(idx); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6291 | fputs(str, stdout); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6292 | } |
| 6293 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6294 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6295 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6296 | #define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6297 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6298 | static BC_STATUS zdc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6299 | { |
| 6300 | BcStatus s; |
| 6301 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6302 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6303 | unsigned long val; |
| 6304 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6305 | s = zxc_program_prep(&opnd, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6306 | if (s) RETURN_STATUS(s); |
| 6307 | s = zbc_num_ulong(num, &val); |
| 6308 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6309 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6310 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6311 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6312 | if (G.prog.exestack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6313 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6314 | if (G.prog.exestack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6315 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6316 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6317 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6318 | bc_vec_npop(&G.prog.exestack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6319 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6320 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6321 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6322 | #define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6323 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6324 | 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] | 6325 | { |
| 6326 | BcStatus s = BC_STATUS_SUCCESS; |
| 6327 | BcResult *r; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6328 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6329 | BcInstPtr ip; |
| 6330 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6331 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6332 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6333 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6334 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6335 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6336 | |
| 6337 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6338 | BcNum *n = n; // for compiler |
| 6339 | bool exec; |
| 6340 | char *name; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6341 | char *then_name = xc_program_name(code, bgn); |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6342 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6343 | |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 6344 | if (code[*bgn] == '\0') |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6345 | (*bgn) += 1; |
| 6346 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6347 | else_name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6348 | |
| 6349 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6350 | name = then_name; |
| 6351 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6352 | exec = true; |
| 6353 | name = else_name; |
| 6354 | } |
| 6355 | |
| 6356 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6357 | BcVec *v; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6358 | v = xc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6359 | n = bc_vec_top(v); |
| 6360 | } |
| 6361 | |
| 6362 | free(then_name); |
| 6363 | free(else_name); |
| 6364 | |
| 6365 | if (!exec) goto exit; |
| 6366 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6367 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6368 | goto exit; |
| 6369 | } |
| 6370 | |
| 6371 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6372 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6373 | if (r->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6374 | sidx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6375 | } else if (r->t == XC_RESULT_VAR) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6376 | BcNum *n; |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame^] | 6377 | s = zxc_program_num(r, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6378 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6379 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6380 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6381 | goto exit; |
| 6382 | } |
| 6383 | |
| 6384 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6385 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6386 | f = xc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6387 | |
| 6388 | if (f->code.len == 0) { |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6389 | BcParse sv_parse; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6390 | char *str; |
| 6391 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6392 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6393 | xc_parse_create(fidx); |
| 6394 | str = *xc_program_str(sidx); |
| 6395 | s = zxc_parse_text_init(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6396 | if (s) goto err; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6397 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6398 | s = zdc_parse_exprs_until_eof(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6399 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6400 | xc_parse_push(DC_INST_POP_EXEC); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6401 | if (G.prs.lex != XC_LEX_EOF) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6402 | s = bc_error_bad_expression(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6403 | xc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6404 | G.prs = sv_parse; // struct copy |
| 6405 | if (s) { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6406 | err: |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6407 | bc_vec_pop_all(&f->code); |
| 6408 | goto exit; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6409 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6410 | } |
| 6411 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6412 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6413 | ip.func = fidx; |
| 6414 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6415 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6416 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6417 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6418 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6419 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6420 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6421 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6422 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6423 | #define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6424 | #endif // ENABLE_DC |
| 6425 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6426 | static void xc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6427 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6428 | BcResult res; |
| 6429 | unsigned long val; |
| 6430 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6431 | res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6432 | if (inst == XC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6433 | val = (unsigned long) G.prog.ib_t; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6434 | else if (inst == XC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6435 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6436 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6437 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6438 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6439 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6440 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6441 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6442 | } |
| 6443 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6444 | static BC_STATUS zxc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6445 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6446 | BcResult r, *ptr; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6447 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6448 | BcFunc *func = xc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6449 | char *code = func->code.v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6450 | |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6451 | dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d", |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6452 | ip->func, func->code.len, ip->inst_idx, G.prog.results.len); |
| 6453 | while (ip->inst_idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6454 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6455 | char inst = code[ip->inst_idx++]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6456 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6457 | 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] | 6458 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6459 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6460 | case BC_INST_JUMP_ZERO: { |
| 6461 | BcNum *num; |
| 6462 | bool zero; |
| 6463 | dbg_exec("BC_INST_JUMP_ZERO:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6464 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6465 | if (s) RETURN_STATUS(s); |
| 6466 | zero = (bc_num_cmp(num, &G.prog.zero) == 0); |
| 6467 | bc_vec_pop(&G.prog.results); |
| 6468 | if (!zero) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6469 | xc_program_index(code, &ip->inst_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6470 | break; |
| 6471 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6472 | // else: fall through |
| 6473 | } |
| 6474 | case BC_INST_JUMP: { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6475 | size_t idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6476 | size_t *addr = bc_vec_item(&func->labels, idx); |
| 6477 | dbg_exec("BC_INST_JUMP: to %ld", (long)*addr); |
| 6478 | ip->inst_idx = *addr; |
| 6479 | break; |
| 6480 | } |
| 6481 | case BC_INST_CALL: |
| 6482 | dbg_exec("BC_INST_CALL:"); |
| 6483 | s = zbc_program_call(code, &ip->inst_idx); |
| 6484 | goto read_updated_ip; |
| 6485 | case BC_INST_INC_PRE: |
| 6486 | case BC_INST_DEC_PRE: |
| 6487 | case BC_INST_INC_POST: |
| 6488 | case BC_INST_DEC_POST: |
| 6489 | dbg_exec("BC_INST_INCDEC:"); |
| 6490 | s = zbc_program_incdec(inst); |
| 6491 | break; |
| 6492 | case BC_INST_HALT: |
| 6493 | dbg_exec("BC_INST_HALT:"); |
| 6494 | QUIT_OR_RETURN_TO_MAIN; |
| 6495 | break; |
| 6496 | case XC_INST_RET: |
| 6497 | case BC_INST_RET0: |
| 6498 | dbg_exec("BC_INST_RET[0]:"); |
| 6499 | s = zbc_program_return(inst); |
| 6500 | goto read_updated_ip; |
| 6501 | case XC_INST_BOOL_OR: |
| 6502 | case XC_INST_BOOL_AND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6503 | #endif // ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6504 | case XC_INST_REL_EQ: |
| 6505 | case XC_INST_REL_LE: |
| 6506 | case XC_INST_REL_GE: |
| 6507 | case XC_INST_REL_NE: |
| 6508 | case XC_INST_REL_LT: |
| 6509 | case XC_INST_REL_GT: |
| 6510 | dbg_exec("BC_INST_BOOL:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6511 | s = zxc_program_logical(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6512 | break; |
| 6513 | case XC_INST_READ: |
| 6514 | dbg_exec("XC_INST_READ:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6515 | s = zxc_program_read(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6516 | goto read_updated_ip; |
| 6517 | case XC_INST_VAR: |
| 6518 | dbg_exec("XC_INST_VAR:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6519 | s = zxc_program_pushVar(code, &ip->inst_idx, false, false); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6520 | break; |
| 6521 | case XC_INST_ARRAY_ELEM: |
| 6522 | case XC_INST_ARRAY: |
| 6523 | dbg_exec("XC_INST_ARRAY[_ELEM]:"); |
| 6524 | s = zbc_program_pushArray(code, &ip->inst_idx, inst); |
| 6525 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6526 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6527 | case BC_INST_LAST: |
| 6528 | dbg_exec("BC_INST_LAST:"); |
| 6529 | r.t = BC_RESULT_LAST; |
| 6530 | bc_vec_push(&G.prog.results, &r); |
| 6531 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6532 | #endif |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6533 | case XC_INST_IBASE: |
| 6534 | case XC_INST_OBASE: |
| 6535 | case XC_INST_SCALE: |
| 6536 | dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6537 | xc_program_pushGlobal(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6538 | break; |
| 6539 | case XC_INST_SCALE_FUNC: |
| 6540 | case XC_INST_LENGTH: |
| 6541 | case XC_INST_SQRT: |
| 6542 | dbg_exec("BC_INST_builtin:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6543 | s = zxc_program_builtin(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6544 | break; |
| 6545 | case XC_INST_NUM: |
| 6546 | dbg_exec("XC_INST_NUM:"); |
| 6547 | r.t = XC_RESULT_CONSTANT; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6548 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6549 | bc_vec_push(&G.prog.results, &r); |
| 6550 | break; |
| 6551 | case XC_INST_POP: |
| 6552 | dbg_exec("XC_INST_POP:"); |
| 6553 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
| 6554 | s = bc_error_stack_has_too_few_elements(); |
| 6555 | else |
| 6556 | bc_vec_pop(&G.prog.results); |
| 6557 | break; |
| 6558 | case XC_INST_PRINT: |
| 6559 | case XC_INST_PRINT_POP: |
| 6560 | case XC_INST_PRINT_STR: |
| 6561 | dbg_exec("XC_INST_PRINTxyz:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6562 | s = zxc_program_print(inst, 0); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6563 | break; |
| 6564 | case XC_INST_STR: |
| 6565 | dbg_exec("XC_INST_STR:"); |
| 6566 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6567 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6568 | bc_vec_push(&G.prog.results, &r); |
| 6569 | break; |
| 6570 | case XC_INST_POWER: |
| 6571 | case XC_INST_MULTIPLY: |
| 6572 | case XC_INST_DIVIDE: |
| 6573 | case XC_INST_MODULUS: |
| 6574 | case XC_INST_PLUS: |
| 6575 | case XC_INST_MINUS: |
| 6576 | dbg_exec("BC_INST_binaryop:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6577 | s = zxc_program_op(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6578 | break; |
| 6579 | case XC_INST_BOOL_NOT: { |
| 6580 | BcNum *num; |
| 6581 | dbg_exec("XC_INST_BOOL_NOT:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6582 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6583 | if (s) RETURN_STATUS(s); |
| 6584 | bc_num_init_DEF_SIZE(&r.d.n); |
| 6585 | if (bc_num_cmp(num, &G.prog.zero) == 0) |
| 6586 | bc_num_one(&r.d.n); |
| 6587 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6588 | xc_program_retire(&r, XC_RESULT_TEMP); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6589 | break; |
| 6590 | } |
| 6591 | case XC_INST_NEG: |
| 6592 | dbg_exec("XC_INST_NEG:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6593 | s = zxc_program_negate(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6594 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6595 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6596 | case BC_INST_ASSIGN_POWER: |
| 6597 | case BC_INST_ASSIGN_MULTIPLY: |
| 6598 | case BC_INST_ASSIGN_DIVIDE: |
| 6599 | case BC_INST_ASSIGN_MODULUS: |
| 6600 | case BC_INST_ASSIGN_PLUS: |
| 6601 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6602 | #endif |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6603 | case XC_INST_ASSIGN: |
| 6604 | dbg_exec("BC_INST_ASSIGNxyz:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6605 | s = zxc_program_assign(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6606 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6607 | #if ENABLE_DC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6608 | case DC_INST_POP_EXEC: |
| 6609 | dbg_exec("DC_INST_POP_EXEC:"); |
| 6610 | bc_vec_pop(&G.prog.exestack); |
| 6611 | goto read_updated_ip; |
| 6612 | case DC_INST_MODEXP: |
| 6613 | dbg_exec("DC_INST_MODEXP:"); |
| 6614 | s = zdc_program_modexp(); |
| 6615 | break; |
| 6616 | case DC_INST_DIVMOD: |
| 6617 | dbg_exec("DC_INST_DIVMOD:"); |
| 6618 | s = zdc_program_divmod(); |
| 6619 | break; |
| 6620 | case DC_INST_EXECUTE: |
| 6621 | case DC_INST_EXEC_COND: |
| 6622 | dbg_exec("DC_INST_EXEC[_COND]:"); |
| 6623 | s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND); |
| 6624 | goto read_updated_ip; |
| 6625 | case DC_INST_PRINT_STACK: { |
| 6626 | size_t idx; |
| 6627 | dbg_exec("DC_INST_PRINT_STACK:"); |
| 6628 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6629 | s = zxc_program_print(XC_INST_PRINT, idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6630 | if (s) break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6631 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6632 | break; |
| 6633 | } |
| 6634 | case DC_INST_CLEAR_STACK: |
| 6635 | dbg_exec("DC_INST_CLEAR_STACK:"); |
| 6636 | bc_vec_pop_all(&G.prog.results); |
| 6637 | break; |
| 6638 | case DC_INST_STACK_LEN: |
| 6639 | dbg_exec("DC_INST_STACK_LEN:"); |
| 6640 | dc_program_stackLen(); |
| 6641 | break; |
| 6642 | case DC_INST_DUPLICATE: |
| 6643 | dbg_exec("DC_INST_DUPLICATE:"); |
| 6644 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
| 6645 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| 6646 | ptr = bc_vec_top(&G.prog.results); |
| 6647 | dc_result_copy(&r, ptr); |
| 6648 | bc_vec_push(&G.prog.results, &r); |
| 6649 | break; |
| 6650 | case DC_INST_SWAP: { |
| 6651 | BcResult *ptr2; |
| 6652 | dbg_exec("DC_INST_SWAP:"); |
| 6653 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
| 6654 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| 6655 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6656 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
| 6657 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6658 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6659 | memcpy(ptr2, &r, sizeof(BcResult)); |
| 6660 | break; |
| 6661 | } |
| 6662 | case DC_INST_ASCIIFY: |
| 6663 | dbg_exec("DC_INST_ASCIIFY:"); |
| 6664 | s = zdc_program_asciify(); |
| 6665 | break; |
| 6666 | case DC_INST_PRINT_STREAM: |
| 6667 | dbg_exec("DC_INST_PRINT_STREAM:"); |
| 6668 | s = zdc_program_printStream(); |
| 6669 | break; |
| 6670 | case DC_INST_LOAD: |
| 6671 | case DC_INST_PUSH_VAR: { |
| 6672 | bool copy = inst == DC_INST_LOAD; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6673 | s = zxc_program_pushVar(code, &ip->inst_idx, true, copy); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6674 | break; |
| 6675 | } |
| 6676 | case DC_INST_PUSH_TO_VAR: { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6677 | char *name = xc_program_name(code, &ip->inst_idx); |
| 6678 | s = zxc_program_copyToVar(name, true); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6679 | free(name); |
| 6680 | break; |
| 6681 | } |
| 6682 | case DC_INST_QUIT: |
| 6683 | dbg_exec("DC_INST_QUIT:"); |
| 6684 | if (G.prog.exestack.len <= 2) |
| 6685 | QUIT_OR_RETURN_TO_MAIN; |
| 6686 | bc_vec_npop(&G.prog.exestack, 2); |
| 6687 | goto read_updated_ip; |
| 6688 | case DC_INST_NQUIT: |
| 6689 | dbg_exec("DC_INST_NQUIT:"); |
| 6690 | s = zdc_program_nquit(); |
| 6691 | //goto read_updated_ip; - just fall through to it |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6692 | #endif // ENABLE_DC |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6693 | read_updated_ip: |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6694 | // Instruction stack has changed, read new pointers |
| 6695 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6696 | func = xc_program_func(ip->func); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6697 | code = func->code.v; |
| 6698 | 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] | 6699 | } |
| 6700 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6701 | if (s || G_interrupt) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6702 | xc_program_reset(); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6703 | RETURN_STATUS(s); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6704 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6705 | |
Denys Vlasenko | c5774a3 | 2018-12-17 01:22:53 +0100 | [diff] [blame] | 6706 | fflush_and_check(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6707 | } |
| 6708 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6709 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6710 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6711 | #define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6712 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6713 | static unsigned xc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6714 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6715 | char *lenv; |
| 6716 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6717 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6718 | lenv = getenv(var); |
| 6719 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6720 | if (!lenv) return len; |
| 6721 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6722 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 6723 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6724 | len = BC_NUM_PRINT_WIDTH; |
| 6725 | |
| 6726 | return len; |
| 6727 | } |
| 6728 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6729 | static BC_STATUS zxc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6730 | { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6731 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6732 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6733 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6734 | s = zxc_parse_text_init(text); // does the first zxc_lex_next() |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6735 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6736 | |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6737 | IF_BC(check_eof:) |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6738 | while (G.prs.lex != XC_LEX_EOF) { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6739 | BcInstPtr *ip; |
| 6740 | BcFunc *f; |
| 6741 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6742 | 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] | 6743 | if (IS_BC) { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6744 | #if ENABLE_BC |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6745 | if (G.prs.lex == BC_LEX_SCOLON |
| 6746 | || G.prs.lex == XC_LEX_NLINE |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6747 | ) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6748 | s = zxc_lex_next(); |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6749 | if (s) goto err; |
| 6750 | goto check_eof; |
| 6751 | } |
| 6752 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6753 | s = zbc_parse_stmt_or_funcdef(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6754 | if (s) goto err; |
| 6755 | |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6756 | // Check that next token is a correct stmt delimiter - |
| 6757 | // disallows "print 1 print 2" and such. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6758 | if (G.prs.lex != BC_LEX_SCOLON |
| 6759 | && G.prs.lex != XC_LEX_NLINE |
| 6760 | && G.prs.lex != XC_LEX_EOF |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6761 | ) { |
| 6762 | const char *err_at; |
| 6763 | //TODO: commonalize for other parse errors: |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6764 | err_at = G.prs.lex_next_at ? G.prs.lex_next_at : "UNKNOWN"; |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6765 | bc_error_fmt("bad statement terminator at '%.*s'", |
| 6766 | (int)(strchrnul(err_at, '\n') - err_at), |
| 6767 | err_at |
| 6768 | ); |
| 6769 | goto err; |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6770 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6771 | // The above logic is fragile. Check these examples: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6772 | // - interactive read() still works |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6773 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6774 | } else { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6775 | #if ENABLE_DC |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6776 | // Most of dc parsing assumes all whitespace, |
| 6777 | // including '\n', is eaten. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6778 | while (G.prs.lex == XC_LEX_NLINE) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6779 | s = zxc_lex_next(); |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6780 | if (s) goto err; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6781 | if (G.prs.lex == XC_LEX_EOF) |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6782 | goto done; |
| 6783 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6784 | s = zdc_parse_expr(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6785 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6786 | } |
| 6787 | if (s || G_interrupt) { |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6788 | err: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6789 | xc_parse_reset(); // includes xc_program_reset() |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6790 | RETURN_STATUS(BC_STATUS_FAILURE); |
| 6791 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6792 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 6793 | dbg_lex("%s:%d executing...", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6794 | s = zxc_program_exec(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6795 | if (s) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6796 | xc_program_reset(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6797 | break; |
| 6798 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6799 | |
| 6800 | ip = (void*)G.prog.exestack.v; |
| 6801 | #if SANITY_CHECKS |
| 6802 | if (G.prog.exestack.len != 1) // should have only main's IP |
| 6803 | bb_error_msg_and_die("BUG:call stack"); |
| 6804 | if (ip->func != BC_PROG_MAIN) |
| 6805 | bb_error_msg_and_die("BUG:not MAIN"); |
| 6806 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6807 | f = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6808 | // bc discards strings, constants and code after each |
| 6809 | // top-level statement in the "main program". |
| 6810 | // This prevents "yes 1 | bc" from growing its memory |
| 6811 | // without bound. This can be done because data stack |
| 6812 | // is empty and thus can't hold any references to |
| 6813 | // strings or constants, there is no generated code |
| 6814 | // which can hold references (after we discard one |
| 6815 | // we just executed). Code of functions can have references, |
| 6816 | // but bc stores function strings/constants in per-function |
| 6817 | // storage. |
| 6818 | if (IS_BC) { |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6819 | #if SANITY_CHECKS |
Denys Vlasenko | 7c1c9dc | 2018-12-22 14:18:47 +0100 | [diff] [blame] | 6820 | if (G.prog.results.len != 0) // should be empty |
| 6821 | bb_error_msg_and_die("BUG:data stack"); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6822 | #endif |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6823 | IF_BC(bc_vec_pop_all(&f->strs);) |
| 6824 | IF_BC(bc_vec_pop_all(&f->consts);) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6825 | } else { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6826 | if (G.prog.results.len == 0 |
| 6827 | && G.prog.vars.len == 0 |
| 6828 | ) { |
| 6829 | // If stack is empty and no registers exist (TODO: or they are all empty), |
| 6830 | // we can get rid of accumulated strings and constants. |
| 6831 | // In this example dc process should not grow |
| 6832 | // its memory consumption with time: |
| 6833 | // yes 1pc | dc |
| 6834 | IF_DC(bc_vec_pop_all(&G.prog.strs);) |
| 6835 | IF_DC(bc_vec_pop_all(&G.prog.consts);) |
| 6836 | } |
| 6837 | // The code is discarded always (below), thus this example |
| 6838 | // should also not grow its memory consumption with time, |
| 6839 | // even though its data stack is not empty: |
| 6840 | // { echo 1; yes dk; } | dc |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6841 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6842 | // We drop generated and executed code for both bc and dc: |
| 6843 | bc_vec_pop_all(&f->code); |
| 6844 | ip->inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6845 | } |
Denys Vlasenko | 8a56e36 | 2018-12-26 19:09:23 +0100 | [diff] [blame] | 6846 | IF_DC(done:) |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6847 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6848 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6849 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6850 | #define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6851 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6852 | static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6853 | { |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6854 | // 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] | 6855 | // therefore we know G.prs.lex_filename == NULL on entry |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6856 | //const char *sv_file; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6857 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6858 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6859 | G.prs.lex_filename = filename; |
| 6860 | G.prs.lex_input_fp = fp; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 6861 | G.err_line = G.prs.lex_line = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6862 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6863 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6864 | s = zxc_vm_process(""); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6865 | // We do not stop looping on errors here if reading stdin. |
| 6866 | // Example: start interactive bc and enter "return". |
| 6867 | // It should say "'return' not in a function" |
| 6868 | // but should not exit. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6869 | } while (G.prs.lex_input_fp == stdin); |
| 6870 | G.prs.lex_filename = NULL; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6871 | RETURN_STATUS(s); |
| 6872 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6873 | #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] | 6874 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6875 | static BC_STATUS zxc_vm_file(const char *file) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6876 | { |
| 6877 | BcStatus s; |
| 6878 | FILE *fp; |
| 6879 | |
| 6880 | fp = xfopen_for_read(file); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6881 | s = zxc_vm_execute_FILE(fp, file); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6882 | fclose(fp); |
| 6883 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6884 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6885 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6886 | #define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6887 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6888 | #if ENABLE_BC |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 6889 | static void bc_vm_info(void) |
| 6890 | { |
| 6891 | printf("%s "BB_VER"\n" |
Gavin Howard | fa495ce | 2018-12-18 10:03:14 -0700 | [diff] [blame] | 6892 | "Adapted from https://github.com/gavinhoward/bc\n" |
| 6893 | "Original code (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 6894 | , applet_name); |
| 6895 | } |
| 6896 | |
| 6897 | static void bc_args(char **argv) |
| 6898 | { |
| 6899 | unsigned opts; |
| 6900 | int i; |
| 6901 | |
| 6902 | GETOPT_RESET(); |
| 6903 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
| 6904 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
| 6905 | "warn\0" No_argument "w" |
| 6906 | "version\0" No_argument "v" |
| 6907 | "standard\0" No_argument "s" |
| 6908 | "quiet\0" No_argument "q" |
| 6909 | "mathlib\0" No_argument "l" |
| 6910 | "interactive\0" No_argument "i" |
| 6911 | ); |
| 6912 | #else |
| 6913 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
| 6914 | #endif |
| 6915 | if (getenv("POSIXLY_CORRECT")) |
| 6916 | option_mask32 |= BC_FLAG_S; |
| 6917 | |
| 6918 | if (opts & BC_FLAG_V) { |
| 6919 | bc_vm_info(); |
| 6920 | exit(0); |
| 6921 | } |
| 6922 | |
| 6923 | for (i = optind; argv[i]; ++i) |
| 6924 | bc_vec_push(&G.files, argv + i); |
| 6925 | } |
| 6926 | |
| 6927 | static void bc_vm_envArgs(void) |
| 6928 | { |
| 6929 | BcVec v; |
| 6930 | char *buf; |
| 6931 | char *env_args = getenv("BC_ENV_ARGS"); |
| 6932 | |
| 6933 | if (!env_args) return; |
| 6934 | |
| 6935 | G.env_args = xstrdup(env_args); |
| 6936 | buf = G.env_args; |
| 6937 | |
| 6938 | bc_vec_init(&v, sizeof(char *), NULL); |
| 6939 | |
| 6940 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 6941 | bc_vec_push(&v, &buf); |
| 6942 | buf = skip_non_whitespace(buf); |
| 6943 | if (!*buf) |
| 6944 | break; |
| 6945 | *buf++ = '\0'; |
| 6946 | } |
| 6947 | |
| 6948 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 6949 | if (sizeof(int) == sizeof(char*)) { |
| 6950 | bc_vec_push(&v, &const_int_0); |
| 6951 | } else { |
| 6952 | static char *const nullptr = NULL; |
| 6953 | bc_vec_push(&v, &nullptr); |
| 6954 | } |
| 6955 | bc_args(((char **)v.v) - 1); |
| 6956 | |
| 6957 | bc_vec_free(&v); |
| 6958 | } |
| 6959 | |
| 6960 | static const char bc_lib[] ALIGN1 = { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6961 | "scale=20" |
| 6962 | "\n" "define e(x){" |
| 6963 | "\n" "auto b,s,n,r,d,i,p,f,v" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 6964 | ////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below |
| 6965 | //^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc: |
| 6966 | //e(-.998896): GNU:.36828580434569428695 |
| 6967 | // above code:.36828580434569428696 |
| 6968 | // actual value:.3682858043456942869594... |
| 6969 | // but for now let's be "GNU compatible" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6970 | "\n" "b=ibase" |
| 6971 | "\n" "ibase=A" |
| 6972 | "\n" "if(x<0){" |
| 6973 | "\n" "n=1" |
| 6974 | "\n" "x=-x" |
| 6975 | "\n" "}" |
| 6976 | "\n" "s=scale" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 6977 | "\n" "r=6+s+.44*x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6978 | "\n" "scale=scale(x)+1" |
| 6979 | "\n" "while(x>1){" |
| 6980 | "\n" "d+=1" |
| 6981 | "\n" "x/=2" |
| 6982 | "\n" "scale+=1" |
| 6983 | "\n" "}" |
| 6984 | "\n" "scale=r" |
| 6985 | "\n" "r=x+1" |
| 6986 | "\n" "p=x" |
| 6987 | "\n" "f=v=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6988 | "\n" "for(i=2;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6989 | "\n" "p*=x" |
| 6990 | "\n" "f*=i" |
| 6991 | "\n" "v=p/f" |
| 6992 | "\n" "r+=v" |
| 6993 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6994 | "\n" "while(d--)r*=r" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6995 | "\n" "scale=s" |
| 6996 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6997 | "\n" "if(n)return(1/r)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6998 | "\n" "return(r/1)" |
| 6999 | "\n" "}" |
| 7000 | "\n" "define l(x){" |
| 7001 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7002 | "\n" "b=ibase" |
| 7003 | "\n" "ibase=A" |
| 7004 | "\n" "if(x<=0){" |
| 7005 | "\n" "r=(1-10^scale)/1" |
| 7006 | "\n" "ibase=b" |
| 7007 | "\n" "return(r)" |
| 7008 | "\n" "}" |
| 7009 | "\n" "s=scale" |
| 7010 | "\n" "scale+=6" |
| 7011 | "\n" "p=2" |
| 7012 | "\n" "while(x>=2){" |
| 7013 | "\n" "p*=2" |
| 7014 | "\n" "x=sqrt(x)" |
| 7015 | "\n" "}" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 7016 | "\n" "while(x<=.5){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7017 | "\n" "p*=2" |
| 7018 | "\n" "x=sqrt(x)" |
| 7019 | "\n" "}" |
| 7020 | "\n" "r=a=(x-1)/(x+1)" |
| 7021 | "\n" "q=a*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7022 | "\n" "v=1" |
| 7023 | "\n" "for(i=3;v;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7024 | "\n" "a*=q" |
| 7025 | "\n" "v=a/i" |
| 7026 | "\n" "r+=v" |
| 7027 | "\n" "}" |
| 7028 | "\n" "r*=p" |
| 7029 | "\n" "scale=s" |
| 7030 | "\n" "ibase=b" |
| 7031 | "\n" "return(r/1)" |
| 7032 | "\n" "}" |
| 7033 | "\n" "define s(x){" |
Denys Vlasenko | 240d7ee | 2018-12-14 11:27:09 +0100 | [diff] [blame] | 7034 | "\n" "auto b,s,r,a,q,i" |
| 7035 | "\n" "if(x<0)return(-s(-x))" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7036 | "\n" "b=ibase" |
| 7037 | "\n" "ibase=A" |
| 7038 | "\n" "s=scale" |
| 7039 | "\n" "scale=1.1*s+2" |
| 7040 | "\n" "a=a(1)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7041 | "\n" "scale=0" |
| 7042 | "\n" "q=(x/a+2)/4" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7043 | "\n" "x-=4*q*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7044 | "\n" "if(q%2)x=-x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7045 | "\n" "scale=s+2" |
| 7046 | "\n" "r=a=x" |
| 7047 | "\n" "q=-x*x" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7048 | "\n" "for(i=3;a;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7049 | "\n" "a*=q/(i*(i-1))" |
| 7050 | "\n" "r+=a" |
| 7051 | "\n" "}" |
| 7052 | "\n" "scale=s" |
| 7053 | "\n" "ibase=b" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7054 | "\n" "return(r/1)" |
| 7055 | "\n" "}" |
| 7056 | "\n" "define c(x){" |
| 7057 | "\n" "auto b,s" |
| 7058 | "\n" "b=ibase" |
| 7059 | "\n" "ibase=A" |
| 7060 | "\n" "s=scale" |
| 7061 | "\n" "scale*=1.2" |
| 7062 | "\n" "x=s(2*a(1)+x)" |
| 7063 | "\n" "scale=s" |
| 7064 | "\n" "ibase=b" |
| 7065 | "\n" "return(x/1)" |
| 7066 | "\n" "}" |
| 7067 | "\n" "define a(x){" |
| 7068 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7069 | "\n" "b=ibase" |
| 7070 | "\n" "ibase=A" |
| 7071 | "\n" "n=1" |
| 7072 | "\n" "if(x<0){" |
| 7073 | "\n" "n=-1" |
| 7074 | "\n" "x=-x" |
| 7075 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7076 | "\n" "if(scale<65){" |
| 7077 | "\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7078 | "\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7079 | "\n" "}" |
| 7080 | "\n" "s=scale" |
| 7081 | "\n" "if(x>.2){" |
| 7082 | "\n" "scale+=5" |
| 7083 | "\n" "a=a(.2)" |
| 7084 | "\n" "}" |
| 7085 | "\n" "scale=s+3" |
| 7086 | "\n" "while(x>.2){" |
| 7087 | "\n" "m+=1" |
| 7088 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7089 | "\n" "}" |
| 7090 | "\n" "r=u=x" |
| 7091 | "\n" "f=-x*x" |
| 7092 | "\n" "t=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7093 | "\n" "for(i=3;t;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7094 | "\n" "u*=f" |
| 7095 | "\n" "t=u/i" |
| 7096 | "\n" "r+=t" |
| 7097 | "\n" "}" |
| 7098 | "\n" "scale=s" |
| 7099 | "\n" "ibase=b" |
| 7100 | "\n" "return((m*a+r)/n)" |
| 7101 | "\n" "}" |
| 7102 | "\n" "define j(n,x){" |
| 7103 | "\n" "auto b,s,o,a,i,v,f" |
| 7104 | "\n" "b=ibase" |
| 7105 | "\n" "ibase=A" |
| 7106 | "\n" "s=scale" |
| 7107 | "\n" "scale=0" |
| 7108 | "\n" "n/=1" |
| 7109 | "\n" "if(n<0){" |
| 7110 | "\n" "n=-n" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7111 | "\n" "o=n%2" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7112 | "\n" "}" |
| 7113 | "\n" "a=1" |
| 7114 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7115 | "\n" "scale=1.5*s" |
| 7116 | "\n" "a=(x^n)/2^n/a" |
| 7117 | "\n" "r=v=1" |
| 7118 | "\n" "f=-x*x/4" |
Denys Vlasenko | c06537d | 2018-12-14 10:10:37 +0100 | [diff] [blame] | 7119 | "\n" "scale+=length(a)-scale(a)" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7120 | "\n" "for(i=1;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7121 | "\n" "v=v*f/i/(n+i)" |
| 7122 | "\n" "r+=v" |
| 7123 | "\n" "}" |
| 7124 | "\n" "scale=s" |
| 7125 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7126 | "\n" "if(o)a=-a" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7127 | "\n" "return(a*r/1)" |
| 7128 | "\n" "}" |
| 7129 | }; |
| 7130 | #endif // ENABLE_BC |
| 7131 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7132 | static BC_STATUS zxc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7133 | { |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7134 | char **fname; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7135 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7136 | size_t i; |
| 7137 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7138 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7139 | if (option_mask32 & BC_FLAG_L) { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7140 | // We know that internal library is not buggy, |
| 7141 | // thus error checking is normally disabled. |
| 7142 | # define DEBUG_LIB 0 |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7143 | s = zxc_vm_process(bc_lib); |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7144 | if (DEBUG_LIB && s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7145 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7146 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7147 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7148 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7149 | fname = (void*)G.files.v; |
| 7150 | for (i = 0; i < G.files.len; i++) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7151 | s = zxc_vm_file(*fname++); |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7152 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) { |
| 7153 | // Debug config, non-interactive mode: |
| 7154 | // return all the way back to main. |
| 7155 | // Non-debug builds do not come here |
| 7156 | // in non-interactive mode, they exit. |
| 7157 | RETURN_STATUS(s); |
| 7158 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7159 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7160 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7161 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7162 | s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7163 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7164 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7165 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7166 | #define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7167 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7168 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7169 | static void xc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7170 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7171 | bc_vec_free(&G.prog.fns); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7172 | IF_BC(bc_vec_free(&G.prog.fn_map);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7173 | bc_vec_free(&G.prog.vars); |
| 7174 | bc_vec_free(&G.prog.var_map); |
| 7175 | bc_vec_free(&G.prog.arrs); |
| 7176 | bc_vec_free(&G.prog.arr_map); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7177 | IF_DC(bc_vec_free(&G.prog.strs);) |
| 7178 | IF_DC(bc_vec_free(&G.prog.consts);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7179 | bc_vec_free(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7180 | bc_vec_free(&G.prog.exestack); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7181 | IF_BC(bc_num_free(&G.prog.last);) |
Denys Vlasenko | db8d607 | 2018-12-27 18:08:30 +0100 | [diff] [blame] | 7182 | //IF_BC(bc_num_free(&G.prog.zero);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7183 | IF_BC(bc_num_free(&G.prog.one);) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7184 | bc_vec_free(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7185 | } |
| 7186 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7187 | static void xc_vm_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7188 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7189 | bc_vec_free(&G.files); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7190 | xc_program_free(); |
| 7191 | xc_parse_free(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7192 | free(G.env_args); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7193 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7194 | #endif |
| 7195 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7196 | static void xc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7197 | { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7198 | BcInstPtr ip; |
| 7199 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7200 | // memset(&G.prog, 0, sizeof(G.prog)); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7201 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7202 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7203 | // G.prog.nchars = G.prog.scale = 0; - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7204 | G.prog.ib_t = 10; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7205 | G.prog.ob_t = 10; |
| 7206 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7207 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);) |
| 7208 | //IF_BC(bc_num_zero(&G.prog.last);) - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7209 | |
Denys Vlasenko | db8d607 | 2018-12-27 18:08:30 +0100 | [diff] [blame] | 7210 | //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7211 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7212 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7213 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);) |
| 7214 | IF_BC(bc_num_one(&G.prog.one);) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7215 | |
| 7216 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7217 | 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] | 7218 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7219 | if (IS_BC) { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7220 | // Names are chosen simply to be distinct and never match |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 7221 | // a valid function name (and be short) |
| 7222 | IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7223 | IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read() |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7224 | } else { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7225 | // in dc, functions have no names |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7226 | xc_program_add_fn(); |
| 7227 | xc_program_add_fn(); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7228 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7229 | |
| 7230 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7231 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7232 | |
| 7233 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7234 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7235 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7236 | IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);) |
| 7237 | 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] | 7238 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7239 | bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL); |
| 7240 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7241 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7242 | bc_char_vec_init(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7243 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7244 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7245 | static int xc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7246 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7247 | #if ENABLE_FEATURE_EDITING |
| 7248 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7249 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7250 | G.prog.len = xc_vm_envLen(env_len); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7251 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7252 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7253 | xc_program_init(); |
Denys Vlasenko | 5f263f4 | 2018-12-13 22:49:59 +0100 | [diff] [blame] | 7254 | IF_BC(if (IS_BC) bc_vm_envArgs();) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7255 | xc_parse_create(BC_PROG_MAIN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7256 | |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7257 | //TODO: in GNU bc, the check is (isatty(0) && isatty(1)), |
| 7258 | //-i option unconditionally enables this regardless of isatty(): |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7259 | if (isatty(0)) { |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 7260 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7261 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7262 | // With SA_RESTART, most system calls will restart |
| 7263 | // (IOW: they won't fail with EINTR). |
| 7264 | // In particular, this means ^C won't cause |
| 7265 | // stdout to get into "error state" if SIGINT hits |
| 7266 | // within write() syscall. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7267 | // |
| 7268 | // The downside is that ^C while tty input is taken |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7269 | // will only be handled after [Enter] since read() |
| 7270 | // from stdin is not interrupted by ^C either, |
| 7271 | // it restarts, thus fgetc() does not return on ^C. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7272 | // (This problem manifests only if line editing is disabled) |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7273 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7274 | |
| 7275 | // Without SA_RESTART, this exhibits a bug: |
| 7276 | // "while (1) print 1" and try ^C-ing it. |
| 7277 | // Intermittently, instead of returning to input line, |
| 7278 | // you'll get "output error: Interrupted system call" |
| 7279 | // and exit. |
| 7280 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7281 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7282 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7283 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7284 | return 0; // "not a tty" |
| 7285 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7286 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7287 | static BcStatus xc_vm_run(void) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7288 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7289 | BcStatus st = zxc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7290 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7291 | if (G_exiting) // it was actually "halt" or "quit" |
| 7292 | st = EXIT_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7293 | xc_vm_free(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7294 | # if ENABLE_FEATURE_EDITING |
| 7295 | free_line_input_t(G.line_input_state); |
| 7296 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7297 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7298 | #endif |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7299 | dbg_exec("exiting with exitcode %d", st); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7300 | return st; |
| 7301 | } |
| 7302 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7303 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7304 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7305 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7306 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7307 | int is_tty; |
| 7308 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7309 | INIT_G(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7310 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7311 | is_tty = xc_vm_init("BC_LINE_LENGTH"); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7312 | |
| 7313 | bc_args(argv); |
| 7314 | |
| 7315 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7316 | bc_vm_info(); |
| 7317 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7318 | return xc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7319 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7320 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7321 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7322 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7323 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7324 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7325 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7326 | int noscript; |
| 7327 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7328 | INIT_G(); |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7329 | |
| 7330 | // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7331 | // 1 char wider than bc from the same package. |
| 7332 | // Both default width, and xC_LINE_LENGTH=N are wider: |
| 7333 | // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7334 | // |1234\ | |
| 7335 | // |56 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7336 | // "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7337 | // |123\ | |
| 7338 | // |456 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7339 | // Do the same, or it's a bug? |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7340 | xc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7341 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7342 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7343 | noscript = BC_FLAG_I; |
| 7344 | for (;;) { |
| 7345 | int n = getopt(argc, argv, "e:f:x"); |
| 7346 | if (n <= 0) |
| 7347 | break; |
| 7348 | switch (n) { |
| 7349 | case 'e': |
| 7350 | noscript = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7351 | n = zxc_vm_process(optarg); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7352 | if (n) return n; |
| 7353 | break; |
| 7354 | case 'f': |
| 7355 | noscript = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7356 | n = zxc_vm_file(optarg); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7357 | if (n) return n; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7358 | break; |
| 7359 | case 'x': |
| 7360 | option_mask32 |= DC_FLAG_X; |
| 7361 | break; |
| 7362 | default: |
| 7363 | bb_show_usage(); |
| 7364 | } |
| 7365 | } |
| 7366 | argv += optind; |
| 7367 | |
| 7368 | while (*argv) { |
| 7369 | noscript = 0; |
| 7370 | bc_vec_push(&G.files, argv++); |
| 7371 | } |
| 7372 | |
| 7373 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7374 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7375 | return xc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7376 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7377 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7378 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 7379 | #endif // DC_BIG |
| 7380 | |