Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Gavin Howard | fa495ce | 2018-12-18 10:03:14 -0700 | [diff] [blame] | 4 | * Adapted from https://github.com/gavinhoward/bc |
| 5 | * Original code copyright (c) 2018 Gavin D. Howard and contributors. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6 | */ |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7 | //TODO: GNU extensions: |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 8 | // support "define void f()..." |
| 9 | // support "define f(*param[])" - "pass array by reference" syntax |
| 10 | |
| 11 | #define DEBUG_LEXER 0 |
| 12 | #define DEBUG_COMPILE 0 |
| 13 | #define DEBUG_EXEC 0 |
| 14 | // This can be left enabled for production as well: |
| 15 | #define SANITY_CHECKS 1 |
| 16 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 17 | //config:config BC |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 18 | //config: bool "bc (45 kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 19 | //config: default y |
Denys Vlasenko | cdadad5 | 2018-12-28 15:13:23 +0100 | [diff] [blame] | 20 | //config: select FEATURE_DC_BIG |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 21 | //config: help |
| 22 | //config: bc is a command-line, arbitrary-precision calculator with a |
| 23 | //config: Turing-complete language. See the GNU bc manual |
| 24 | //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] | 25 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 26 | //config: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 27 | //config: This bc has five differences to the GNU bc: |
| 28 | //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] | 29 | //config: 2) Arrays are copied before being passed as arguments to |
| 30 | //config: functions. This behavior is required by the bc spec. |
| 31 | //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] | 32 | //config: the number of elements in the array. This prints "1": |
| 33 | //config: a[0] = 0; length(a[]) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 34 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 35 | //config: that of the unary minus (-) negation operator. This still |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 36 | //config: allows POSIX-compliant scripts to work while somewhat |
| 37 | //config: preserving expected behavior (versus C) and making parsing |
| 38 | //config: easier. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 39 | //config: 5) "read()" accepts expressions, not only numeric literals. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 40 | //config: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 41 | //config:config DC |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 42 | //config: bool "dc (36 kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 43 | //config: default y |
| 44 | //config: help |
| 45 | //config: dc is a reverse-polish notation command-line calculator which |
| 46 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 47 | //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] | 48 | //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] | 49 | //config: |
| 50 | //config: This dc has a few differences from the two above: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 51 | //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] | 52 | //config: the FreeBSD dc does. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 53 | //config: 2) Implements the GNU extensions for divmod ("~") and |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 54 | //config: modular exponentiation ("|"). |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 55 | //config: 3) Implements all FreeBSD extensions, except for "J" and "M". |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 56 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 57 | //config: However, they are implemented differently. When it encounters |
| 58 | //config: whitespace where a register should be, it skips the whitespace. |
| 59 | //config: If the character following is not a lowercase letter, an error |
| 60 | //config: is issued. Otherwise, the register name is parsed by the |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 61 | //config: following regex: [a-z][a-z0-9_]* |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 62 | //config: This generally means that register names will be surrounded by |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 63 | //config: whitespace. Examples: |
| 64 | //config: l idx s temp L index S temp2 < do_thing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 65 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 66 | //config: allowed unless the "-x" option is given. |
| 67 | //config: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 68 | //config:if BC || DC # for menuconfig indenting |
| 69 | //config: |
| 70 | //config:config FEATURE_DC_BIG |
| 71 | //config: bool "Use bc code base for dc (larger, more features)" |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 72 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 73 | //config: |
| 74 | //config:config FEATURE_DC_LIBM |
| 75 | //config: bool "Enable power and exp functions (requires libm)" |
| 76 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 77 | //config: depends on DC && !BC && !FEATURE_DC_BIG |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 78 | //config: help |
| 79 | //config: Enable power and exp functions. |
| 80 | //config: NOTE: This will require libm to be present for linking. |
| 81 | //config: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 82 | //config:config FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 83 | //config: bool "Interactive mode (+4kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 84 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 85 | //config: depends on BC || (DC && FEATURE_DC_BIG) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 86 | //config: help |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 87 | //config: Enable interactive mode: when started on a tty, |
| 88 | //config: ^C interrupts execution and returns to command line, |
| 89 | //config: errors also return to command line instead of exiting, |
| 90 | //config: line editing with history is available. |
| 91 | //config: |
| 92 | //config: With this option off, input can still be taken from tty, |
| 93 | //config: but all errors are fatal, ^C is fatal, |
| 94 | //config: tty is treated exactly the same as any other |
| 95 | //config: standard input (IOW: no line editing). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 96 | //config: |
| 97 | //config:config FEATURE_BC_LONG_OPTIONS |
| 98 | //config: bool "Enable bc/dc long options" |
| 99 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 100 | //config: depends on BC || (DC && FEATURE_DC_BIG) |
| 101 | //config: |
| 102 | //config:endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 103 | |
| 104 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 105 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 106 | |
| 107 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 108 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 109 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 110 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 111 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 09fe0aa | 2018-12-18 16:32:25 +0100 | [diff] [blame] | 112 | //usage: "[-sqlw] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 113 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 114 | //usage:#define bc_full_usage "\n" |
| 115 | //usage: "\nArbitrary precision calculator" |
| 116 | //usage: "\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 117 | ///////: "\n -i Interactive" - has no effect for now |
| 118 | //usage: "\n -q Quiet" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 119 | //usage: "\n -l Load standard math library" |
| 120 | //usage: "\n -s Be POSIX compatible" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 121 | //usage: "\n -w Warn if extensions are used" |
| 122 | ///////: "\n -v Version" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 123 | //usage: "\n" |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 124 | //usage: "\n$BC_LINE_LENGTH changes output width" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 125 | //usage: |
| 126 | //usage:#define bc_example_usage |
| 127 | //usage: "3 + 4.129\n" |
| 128 | //usage: "1903 - 2893\n" |
| 129 | //usage: "-129 * 213.28935\n" |
| 130 | //usage: "12 / -1932\n" |
| 131 | //usage: "12 % 12\n" |
| 132 | //usage: "34 ^ 189\n" |
| 133 | //usage: "scale = 13\n" |
| 134 | //usage: "ibase = 2\n" |
| 135 | //usage: "obase = A\n" |
| 136 | //usage: |
| 137 | //usage:#define dc_trivial_usage |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 138 | //usage: IF_FEATURE_DC_BIG("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 139 | //usage: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 140 | //usage:#define dc_full_usage "\n" |
| 141 | //usage: "\nTiny RPN calculator. Operations:" |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 142 | //usage: "\n+, -, *, /, %, ~, ^," IF_FEATURE_DC_BIG(" |,") |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 143 | //usage: "\np - print top of the stack without popping" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 144 | //usage: "\nf - print entire stack" |
| 145 | //usage: "\nk - pop the value and set the precision" |
| 146 | //usage: "\ni - pop the value and set input radix" |
| 147 | //usage: "\no - pop the value and set output radix" |
| 148 | //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] | 149 | //usage: |
| 150 | //usage:#define dc_example_usage |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 151 | //usage: "$ dc -e'2 2 + p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 152 | //usage: "4\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 153 | //usage: "$ dc -e'8 8 \\* 2 2 + / p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 154 | //usage: "16\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 155 | //usage: "$ dc -e'0 1 & p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 156 | //usage: "0\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 157 | //usage: "$ dc -e'0 1 | p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 158 | //usage: "1\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 159 | //usage: "$ echo '72 9 / 8 * p' | dc\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 160 | //usage: "64\n" |
| 161 | |
| 162 | #include "libbb.h" |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 163 | #include "common_bufsiz.h" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 164 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 165 | #if !ENABLE_BC && !ENABLE_FEATURE_DC_BIG |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 166 | # include "dc.c" |
| 167 | #else |
| 168 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 169 | #if DEBUG_LEXER |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 170 | static uint8_t lex_indent; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 171 | #define dbg_lex(...) \ |
| 172 | do { \ |
| 173 | fprintf(stderr, "%*s", lex_indent, ""); \ |
| 174 | bb_error_msg(__VA_ARGS__); \ |
| 175 | } while (0) |
| 176 | #define dbg_lex_enter(...) \ |
| 177 | do { \ |
| 178 | dbg_lex(__VA_ARGS__); \ |
| 179 | lex_indent++; \ |
| 180 | } while (0) |
| 181 | #define dbg_lex_done(...) \ |
| 182 | do { \ |
| 183 | lex_indent--; \ |
| 184 | dbg_lex(__VA_ARGS__); \ |
| 185 | } while (0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 186 | #else |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 187 | # define dbg_lex(...) ((void)0) |
| 188 | # define dbg_lex_enter(...) ((void)0) |
| 189 | # define dbg_lex_done(...) ((void)0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 190 | #endif |
| 191 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 192 | #if DEBUG_COMPILE |
| 193 | # define dbg_compile(...) bb_error_msg(__VA_ARGS__) |
| 194 | #else |
| 195 | # define dbg_compile(...) ((void)0) |
| 196 | #endif |
| 197 | |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 198 | #if DEBUG_EXEC |
| 199 | # define dbg_exec(...) bb_error_msg(__VA_ARGS__) |
| 200 | #else |
| 201 | # define dbg_exec(...) ((void)0) |
| 202 | #endif |
| 203 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 204 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 205 | BC_STATUS_SUCCESS = 0, |
| 206 | BC_STATUS_FAILURE = 1, |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 207 | 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] | 208 | } BcStatus; |
| 209 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 210 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 211 | #define BC_VEC_START_CAP (1 << 5) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 212 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 213 | typedef void (*BcVecFree)(void *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 214 | |
| 215 | typedef struct BcVec { |
| 216 | char *v; |
| 217 | size_t len; |
| 218 | size_t cap; |
| 219 | size_t size; |
| 220 | BcVecFree dtor; |
| 221 | } BcVec; |
| 222 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 223 | typedef signed char BcDig; |
| 224 | |
| 225 | typedef struct BcNum { |
| 226 | BcDig *restrict num; |
| 227 | size_t rdx; |
| 228 | size_t len; |
| 229 | size_t cap; |
| 230 | bool neg; |
| 231 | } BcNum; |
| 232 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 233 | #define BC_NUM_MAX_IBASE 36 |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 234 | // larger value might speed up BIGNUM calculations a bit: |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 235 | #define BC_NUM_DEF_SIZE 16 |
| 236 | #define BC_NUM_PRINT_WIDTH 69 |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 237 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 238 | #define BC_NUM_KARATSUBA_LEN 32 |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 239 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 240 | typedef enum BcInst { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 241 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 242 | BC_INST_INC_PRE, |
| 243 | BC_INST_DEC_PRE, |
| 244 | BC_INST_INC_POST, |
| 245 | BC_INST_DEC_POST, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 246 | #endif |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 247 | XC_INST_NEG, // order |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 248 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 249 | XC_INST_REL_EQ, // should |
| 250 | XC_INST_REL_LE, // match |
| 251 | XC_INST_REL_GE, // LEX |
| 252 | XC_INST_REL_NE, // constants |
| 253 | XC_INST_REL_LT, // for |
| 254 | XC_INST_REL_GT, // these |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 255 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 256 | XC_INST_POWER, // operations |
| 257 | XC_INST_MULTIPLY, // | |
| 258 | XC_INST_DIVIDE, // | |
| 259 | XC_INST_MODULUS, // | |
| 260 | XC_INST_PLUS, // | |
| 261 | XC_INST_MINUS, // | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 262 | |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 263 | XC_INST_BOOL_NOT, // | |
| 264 | XC_INST_BOOL_OR, // | |
| 265 | XC_INST_BOOL_AND, // | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 266 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 267 | BC_INST_ASSIGN_POWER, // | |
| 268 | BC_INST_ASSIGN_MULTIPLY,// | |
| 269 | BC_INST_ASSIGN_DIVIDE, // | |
| 270 | BC_INST_ASSIGN_MODULUS, // | |
| 271 | BC_INST_ASSIGN_PLUS, // | |
| 272 | BC_INST_ASSIGN_MINUS, // | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 273 | #endif |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 274 | XC_INST_ASSIGN, // V |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 275 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 276 | XC_INST_NUM, |
| 277 | XC_INST_VAR, |
| 278 | XC_INST_ARRAY_ELEM, |
| 279 | XC_INST_ARRAY, |
| 280 | XC_INST_SCALE_FUNC, |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 281 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 282 | XC_INST_IBASE, // order of these constans should match other enums |
| 283 | XC_INST_OBASE, // order of these constans should match other enums |
| 284 | XC_INST_SCALE, // order of these constans should match other enums |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 285 | 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] | 286 | XC_INST_LENGTH, |
| 287 | XC_INST_READ, |
| 288 | XC_INST_SQRT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 289 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 290 | XC_INST_PRINT, |
| 291 | XC_INST_PRINT_POP, |
| 292 | XC_INST_STR, |
| 293 | XC_INST_PRINT_STR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 294 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 295 | #if ENABLE_BC |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 296 | BC_INST_HALT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 297 | BC_INST_JUMP, |
| 298 | BC_INST_JUMP_ZERO, |
| 299 | |
| 300 | BC_INST_CALL, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 301 | BC_INST_RET0, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 302 | #endif |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 303 | XC_INST_RET, |
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 | XC_INST_POP, |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 306 | #if ENABLE_DC |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 307 | DC_INST_POP_EXEC, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 308 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 309 | DC_INST_MODEXP, |
| 310 | DC_INST_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 311 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 312 | DC_INST_EXECUTE, |
| 313 | DC_INST_EXEC_COND, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 314 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 315 | DC_INST_ASCIIFY, |
| 316 | DC_INST_PRINT_STREAM, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 317 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 318 | DC_INST_PRINT_STACK, |
| 319 | DC_INST_CLEAR_STACK, |
| 320 | DC_INST_STACK_LEN, |
| 321 | DC_INST_DUPLICATE, |
| 322 | DC_INST_SWAP, |
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_LOAD, |
| 325 | DC_INST_PUSH_VAR, |
| 326 | DC_INST_PUSH_TO_VAR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 327 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 328 | DC_INST_QUIT, |
| 329 | DC_INST_NQUIT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 330 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 331 | DC_INST_INVALID = -1, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 332 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 333 | } BcInst; |
| 334 | |
| 335 | typedef struct BcId { |
| 336 | char *name; |
| 337 | size_t idx; |
| 338 | } BcId; |
| 339 | |
| 340 | typedef struct BcFunc { |
| 341 | BcVec code; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 342 | IF_BC(BcVec labels;) |
| 343 | IF_BC(BcVec autos;) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 344 | IF_BC(BcVec strs;) |
| 345 | IF_BC(BcVec consts;) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 346 | IF_BC(size_t nparams;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 347 | } BcFunc; |
| 348 | |
| 349 | typedef enum BcResultType { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 350 | XC_RESULT_TEMP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 351 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 352 | XC_RESULT_VAR, |
| 353 | XC_RESULT_ARRAY_ELEM, |
| 354 | XC_RESULT_ARRAY, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 355 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 356 | XC_RESULT_STR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 357 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 358 | //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct, |
| 359 | XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE |
| 360 | XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE |
| 361 | XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 362 | IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 363 | XC_RESULT_CONSTANT, |
| 364 | IF_BC(BC_RESULT_ONE,) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 365 | } BcResultType; |
| 366 | |
| 367 | typedef union BcResultData { |
| 368 | BcNum n; |
| 369 | BcVec v; |
| 370 | BcId id; |
| 371 | } BcResultData; |
| 372 | |
| 373 | typedef struct BcResult { |
| 374 | BcResultType t; |
| 375 | BcResultData d; |
| 376 | } BcResult; |
| 377 | |
| 378 | typedef struct BcInstPtr { |
| 379 | size_t func; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 380 | size_t inst_idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 381 | } BcInstPtr; |
| 382 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 383 | typedef enum BcLexType { |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 384 | XC_LEX_EOF, |
| 385 | XC_LEX_INVALID, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 386 | |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 387 | XC_LEX_NLINE, |
| 388 | XC_LEX_WHITESPACE, |
| 389 | XC_LEX_STR, |
| 390 | XC_LEX_NAME, |
| 391 | XC_LEX_NUMBER, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 392 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 393 | XC_LEX_1st_op, |
| 394 | XC_LEX_NEG = XC_LEX_1st_op, // order |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 395 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 396 | XC_LEX_OP_REL_EQ, // should |
| 397 | XC_LEX_OP_REL_LE, // match |
| 398 | XC_LEX_OP_REL_GE, // INST |
| 399 | XC_LEX_OP_REL_NE, // constants |
| 400 | XC_LEX_OP_REL_LT, // for |
| 401 | XC_LEX_OP_REL_GT, // these |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 402 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 403 | XC_LEX_OP_POWER, // operations |
| 404 | XC_LEX_OP_MULTIPLY, // | |
| 405 | XC_LEX_OP_DIVIDE, // | |
| 406 | XC_LEX_OP_MODULUS, // | |
| 407 | XC_LEX_OP_PLUS, // | |
| 408 | XC_LEX_OP_MINUS, // | |
| 409 | XC_LEX_OP_last = XC_LEX_OP_MINUS, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 410 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 411 | BC_LEX_OP_BOOL_NOT, // | |
| 412 | BC_LEX_OP_BOOL_OR, // | |
| 413 | BC_LEX_OP_BOOL_AND, // | |
| 414 | |
| 415 | BC_LEX_OP_ASSIGN_POWER, // | |
| 416 | BC_LEX_OP_ASSIGN_MULTIPLY, // | |
| 417 | BC_LEX_OP_ASSIGN_DIVIDE, // | |
| 418 | BC_LEX_OP_ASSIGN_MODULUS, // | |
| 419 | BC_LEX_OP_ASSIGN_PLUS, // | |
| 420 | BC_LEX_OP_ASSIGN_MINUS, // | |
| 421 | |
| 422 | BC_LEX_OP_ASSIGN, // V |
| 423 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 424 | BC_LEX_OP_INC, |
| 425 | BC_LEX_OP_DEC, |
| 426 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 427 | BC_LEX_LPAREN, // () are 0x28 and 0x29 |
| 428 | 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] | 429 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 430 | BC_LEX_LBRACKET, // [] are 0x5B and 0x5D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 431 | BC_LEX_COMMA, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 432 | 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] | 433 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 434 | BC_LEX_LBRACE, // {} are 0x7B and 0x7D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 435 | BC_LEX_SCOLON, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 436 | 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] | 437 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 438 | BC_LEX_KEY_1st_keyword, |
| 439 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 440 | BC_LEX_KEY_BREAK, |
| 441 | BC_LEX_KEY_CONTINUE, |
| 442 | BC_LEX_KEY_DEFINE, |
| 443 | BC_LEX_KEY_ELSE, |
| 444 | BC_LEX_KEY_FOR, |
| 445 | BC_LEX_KEY_HALT, |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 446 | // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct, |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 447 | BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE |
| 448 | BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 449 | BC_LEX_KEY_IF, |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 450 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 451 | BC_LEX_KEY_LENGTH, |
| 452 | BC_LEX_KEY_LIMITS, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 453 | BC_LEX_KEY_PRINT, |
| 454 | BC_LEX_KEY_QUIT, |
| 455 | BC_LEX_KEY_READ, |
| 456 | BC_LEX_KEY_RETURN, |
| 457 | BC_LEX_KEY_SCALE, |
| 458 | BC_LEX_KEY_SQRT, |
| 459 | BC_LEX_KEY_WHILE, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 460 | #endif // ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 461 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 462 | #if ENABLE_DC |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 463 | DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1, |
| 464 | DC_LEX_OP_ASSIGN, |
| 465 | |
| 466 | DC_LEX_LPAREN, |
| 467 | DC_LEX_SCOLON, |
| 468 | DC_LEX_READ, |
| 469 | DC_LEX_IBASE, |
| 470 | DC_LEX_SCALE, |
| 471 | DC_LEX_OBASE, |
| 472 | DC_LEX_LENGTH, |
| 473 | DC_LEX_PRINT, |
| 474 | DC_LEX_QUIT, |
| 475 | DC_LEX_SQRT, |
| 476 | DC_LEX_LBRACE, |
| 477 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 478 | DC_LEX_EQ_NO_REG, |
| 479 | DC_LEX_OP_MODEXP, |
| 480 | DC_LEX_OP_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 481 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 482 | DC_LEX_COLON, |
| 483 | DC_LEX_ELSE, |
| 484 | DC_LEX_EXECUTE, |
| 485 | DC_LEX_PRINT_STACK, |
| 486 | DC_LEX_CLEAR_STACK, |
| 487 | DC_LEX_STACK_LEVEL, |
| 488 | DC_LEX_DUPLICATE, |
| 489 | DC_LEX_SWAP, |
| 490 | DC_LEX_POP, |
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 | DC_LEX_ASCIIFY, |
| 493 | DC_LEX_PRINT_STREAM, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 494 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 495 | // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct, |
| 496 | DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE |
| 497 | DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE |
| 498 | DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE |
| 499 | DC_LEX_LOAD, |
| 500 | DC_LEX_LOAD_POP, |
| 501 | DC_LEX_STORE_PUSH, |
| 502 | DC_LEX_PRINT_POP, |
| 503 | DC_LEX_NQUIT, |
| 504 | DC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 505 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 506 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 507 | // must match order of BC_LEX_KEY_foo etc above |
| 508 | #if ENABLE_BC |
| 509 | struct BcLexKeyword { |
| 510 | char name8[8]; |
| 511 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 512 | #define LEX_KW_ENTRY(a, b) \ |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 513 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 514 | static const struct BcLexKeyword bc_lex_kws[20] = { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 515 | LEX_KW_ENTRY("auto" , 1), // 0 |
| 516 | LEX_KW_ENTRY("break" , 1), // 1 |
| 517 | LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 518 | LEX_KW_ENTRY("define" , 1), // 3 |
| 519 | LEX_KW_ENTRY("else" , 0), // 4 |
| 520 | LEX_KW_ENTRY("for" , 1), // 5 |
| 521 | LEX_KW_ENTRY("halt" , 0), // 6 |
| 522 | LEX_KW_ENTRY("ibase" , 1), // 7 |
| 523 | LEX_KW_ENTRY("obase" , 1), // 8 |
| 524 | LEX_KW_ENTRY("if" , 1), // 9 |
| 525 | LEX_KW_ENTRY("last" , 0), // 10 |
| 526 | LEX_KW_ENTRY("length" , 1), // 11 |
| 527 | LEX_KW_ENTRY("limits" , 0), // 12 |
| 528 | LEX_KW_ENTRY("print" , 0), // 13 |
| 529 | LEX_KW_ENTRY("quit" , 1), // 14 |
| 530 | LEX_KW_ENTRY("read" , 0), // 15 |
| 531 | LEX_KW_ENTRY("return" , 1), // 16 |
| 532 | LEX_KW_ENTRY("scale" , 1), // 17 |
| 533 | LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 534 | LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 535 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 536 | #undef LEX_KW_ENTRY |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 537 | #define STRING_else (bc_lex_kws[4].name8) |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 538 | #define STRING_for (bc_lex_kws[5].name8) |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 539 | #define STRING_if (bc_lex_kws[9].name8) |
| 540 | #define STRING_while (bc_lex_kws[19].name8) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 541 | enum { |
| 542 | POSIX_KWORD_MASK = 0 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 543 | | (1 << 0) // 0 |
| 544 | | (1 << 1) // 1 |
| 545 | | (0 << 2) // 2 |
| 546 | | (1 << 3) // 3 |
| 547 | | (0 << 4) // 4 |
| 548 | | (1 << 5) // 5 |
| 549 | | (0 << 6) // 6 |
| 550 | | (1 << 7) // 7 |
| 551 | | (1 << 8) // 8 |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 552 | | (1 << 9) // 9 |
| 553 | | (0 << 10) // 10 |
| 554 | | (1 << 11) // 11 |
| 555 | | (0 << 12) // 12 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 556 | | (0 << 13) // 13 |
| 557 | | (1 << 14) // 14 |
| 558 | | (0 << 15) // 15 |
| 559 | | (1 << 16) // 16 |
| 560 | | (1 << 17) // 17 |
| 561 | | (1 << 18) // 18 |
| 562 | | (1 << 19) // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 563 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 564 | #define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 565 | |
| 566 | // This is a bit array that corresponds to token types. An entry is |
| 567 | // true if the token is valid in an expression, false otherwise. |
| 568 | // Used to figure out when expr parsing should stop *without error message* |
| 569 | // - 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] | 570 | // 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] | 571 | // by later processing. |
| 572 | enum { |
| 573 | #define EXBITS(a,b,c,d,e,f,g,h) \ |
| 574 | ((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] | 575 | BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz: |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 576 | + (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] | 577 | + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ * |
| 578 | + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^= |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 579 | + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ -- |
| 580 | + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; } |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 581 | + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase |
| 582 | + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read |
| 583 | + (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] | 584 | #undef EXBITS |
| 585 | }; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 586 | static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 587 | { |
| 588 | #if ULONG_MAX > 0xffffffff |
| 589 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 590 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 591 | #else |
| 592 | // 32-bit version |
| 593 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 594 | if (i >= 32) { |
| 595 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 596 | i &= 31; |
| 597 | } |
| 598 | return m & (1UL << i); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 599 | #endif |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // This is an array of data for operators that correspond to |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 603 | // [XC_LEX_1st_op...] token types. |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 604 | static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 605 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 606 | OP(1, false), // neg |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 607 | 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] | 608 | OP(2, false), // pow |
| 609 | OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod |
| 610 | OP(4, true ), OP( 4, true ), // + - |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 611 | OP(1, false), // not |
| 612 | OP(7, true ), OP( 7, true ), // or and |
| 613 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 614 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 615 | OP(0, false), OP( 0, false ), // inc dec |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 616 | #undef OP |
| 617 | }; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 618 | #define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f) |
| 619 | #define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 620 | #endif // ENABLE_BC |
| 621 | |
| 622 | #if ENABLE_DC |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 623 | static const //BcLexType - should be this type |
| 624 | uint8_t |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 625 | dc_char_to_LEX[] ALIGN1 = { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 626 | // %&'( |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 627 | 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] | 628 | // )*+, |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 629 | 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] | 630 | // -./ |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 631 | XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 632 | // 0123456789 |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 633 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 634 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 635 | XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 636 | // :;<=>?@ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 637 | DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ, |
| 638 | XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 639 | // ABCDEFGH |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 640 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 641 | 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] | 642 | // IJKLMNOP |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 643 | DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP, |
| 644 | 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] | 645 | // QRSTUVWXY |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 646 | DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID, |
| 647 | 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] | 648 | // Z[\] |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 649 | DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 650 | // ^_` |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 651 | XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 652 | // abcdefgh |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 653 | DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE, |
| 654 | 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] | 655 | // ijklmnop |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 656 | 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] | 657 | 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] | 658 | // qrstuvwx |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 659 | DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID, |
| 660 | XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 661 | // yz |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 662 | XC_LEX_INVALID, DC_LEX_STACK_LEVEL, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 663 | // {|}~ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 664 | 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] | 665 | }; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 666 | 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] | 667 | int8_t |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 668 | dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz: |
| 669 | XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY |
| 670 | XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS |
| 671 | 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] | 672 | XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT |
| 673 | DC_INST_INVALID, // DC_LEX_OP_ASSIGN |
| 674 | XC_INST_REL_GT, // DC_LEX_LPAREN |
| 675 | DC_INST_INVALID, // DC_LEX_SCOLON |
| 676 | DC_INST_INVALID, // DC_LEX_READ |
| 677 | XC_INST_IBASE, // DC_LEX_IBASE |
| 678 | XC_INST_SCALE, // DC_LEX_SCALE |
| 679 | XC_INST_OBASE, // DC_LEX_OBASE |
| 680 | XC_INST_LENGTH, // DC_LEX_LENGTH |
| 681 | XC_INST_PRINT, // DC_LEX_PRINT |
| 682 | DC_INST_QUIT, // DC_LEX_QUIT |
| 683 | XC_INST_SQRT, // DC_LEX_SQRT |
| 684 | XC_INST_REL_GE, // DC_LEX_LBRACE |
| 685 | XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 686 | DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD |
| 687 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE |
| 688 | DC_INST_EXECUTE, // DC_LEX_EXECUTE |
| 689 | DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK |
| 690 | DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE |
| 691 | DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP |
| 692 | DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM |
| 693 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE |
| 694 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD |
| 695 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH |
| 696 | XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT |
| 697 | XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 698 | // DC_INST_INVALID in this table either means that corresponding LEX |
| 699 | // is not possible for dc, or that it does not compile one-to-one |
| 700 | // to a single INST. |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 701 | }; |
| 702 | #endif // ENABLE_DC |
| 703 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 704 | typedef struct BcParse { |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 705 | smallint lex; // was BcLexType // first member is most used |
| 706 | smallint lex_last; // was BcLexType |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 707 | size_t lex_line; |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 708 | const char *lex_inbuf; |
| 709 | 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] | 710 | const char *lex_filename; |
| 711 | FILE *lex_input_fp; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 712 | BcVec lex_strnumbuf; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 713 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 714 | BcFunc *func; |
| 715 | size_t fidx; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 716 | IF_BC(size_t in_funcdef;) |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 717 | IF_BC(BcVec exits;) |
| 718 | IF_BC(BcVec conds;) |
| 719 | IF_BC(BcVec ops;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 720 | } BcParse; |
| 721 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 722 | typedef struct BcProgram { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 723 | size_t len; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 724 | size_t nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 725 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 726 | size_t scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 727 | size_t ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 728 | size_t ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 729 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 730 | BcVec results; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 731 | BcVec exestack; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 732 | |
| 733 | BcVec fns; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 734 | IF_BC(BcVec fn_map;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 735 | |
| 736 | BcVec vars; |
| 737 | BcVec var_map; |
| 738 | |
| 739 | BcVec arrs; |
| 740 | BcVec arr_map; |
| 741 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 742 | IF_DC(BcVec strs;) |
| 743 | IF_DC(BcVec consts;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 744 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 745 | BcNum zero; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 746 | IF_BC(BcNum one;) |
| 747 | IF_BC(BcNum last;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 748 | } BcProgram; |
| 749 | |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 750 | struct globals { |
| 751 | BcParse prs; // first member is most used |
| 752 | |
| 753 | // For error messages. Can be set to current parsed line, |
| 754 | // or [TODO] to current executing line (can be before last parsed one) |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 755 | size_t err_line; |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 756 | |
| 757 | BcVec input_buffer; |
| 758 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 759 | IF_FEATURE_BC_INTERACTIVE(smallint ttyin;) |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 760 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
| 761 | |
| 762 | BcProgram prog; |
| 763 | |
| 764 | BcVec files; |
| 765 | |
| 766 | char *env_args; |
| 767 | |
| 768 | #if ENABLE_FEATURE_EDITING |
| 769 | line_input_t *line_input_state; |
| 770 | #endif |
| 771 | } FIX_ALIASING; |
| 772 | #define G (*ptr_to_globals) |
| 773 | #define INIT_G() do { \ |
| 774 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 775 | } while (0) |
| 776 | #define FREE_G() do { \ |
| 777 | FREE_PTR_TO_GLOBALS(); \ |
| 778 | } while (0) |
| 779 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 780 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
| 781 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 782 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 783 | # define G_interrupt bb_got_signal |
| 784 | # define G_ttyin G.ttyin |
| 785 | #else |
| 786 | # define G_interrupt 0 |
| 787 | # define G_ttyin 0 |
| 788 | #endif |
| 789 | #if ENABLE_FEATURE_CLEAN_UP |
| 790 | # define G_exiting G.exiting |
| 791 | #else |
| 792 | # define G_exiting 0 |
| 793 | #endif |
| 794 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
| 795 | #define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b')) |
| 796 | |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 797 | #if ENABLE_BC |
| 798 | # define BC_PARSE_REL (1 << 0) |
| 799 | # define BC_PARSE_PRINT (1 << 1) |
| 800 | # define BC_PARSE_ARRAY (1 << 2) |
| 801 | # define BC_PARSE_NOCALL (1 << 3) |
| 802 | #endif |
| 803 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 804 | #define BC_PROG_MAIN 0 |
| 805 | #define BC_PROG_READ 1 |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 806 | #if ENABLE_DC |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 807 | #define BC_PROG_REQ_FUNCS 2 |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 808 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 809 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 810 | #define BC_FLAG_W (1 << 0) |
| 811 | #define BC_FLAG_V (1 << 1) |
| 812 | #define BC_FLAG_S (1 << 2) |
| 813 | #define BC_FLAG_Q (1 << 3) |
| 814 | #define BC_FLAG_L (1 << 4) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 815 | #define BC_FLAG_I ((1 << 5) * ENABLE_DC) |
| 816 | #define DC_FLAG_X ((1 << 6) * ENABLE_DC) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 817 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 818 | #define BC_MAX_OBASE ((unsigned) 999) |
| 819 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 820 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 821 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 822 | #define BC_MAX_NUM BC_MAX_STRING |
| 823 | // Unused apart from "limits" message. Just show a "biggish number" there. |
| 824 | //#define BC_MAX_NAME BC_MAX_STRING |
| 825 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 826 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 827 | #define BC_MAX_NAME_STR "999999999" |
| 828 | #define BC_MAX_EXP_STR "999999999" |
| 829 | #define BC_MAX_VARS_STR "999999999" |
| 830 | |
| 831 | #define BC_MAX_OBASE_STR "999" |
| 832 | |
| 833 | #if INT_MAX == 2147483647 |
| 834 | # define BC_MAX_DIM_STR "2147483647" |
| 835 | #elif INT_MAX == 9223372036854775807 |
| 836 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 837 | #else |
| 838 | # error Strange INT_MAX |
| 839 | #endif |
| 840 | |
| 841 | #if UINT_MAX == 4294967295 |
| 842 | # define BC_MAX_SCALE_STR "4294967295" |
| 843 | # define BC_MAX_STRING_STR "4294967294" |
| 844 | #elif UINT_MAX == 18446744073709551615 |
| 845 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 846 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 847 | #else |
| 848 | # error Strange UINT_MAX |
| 849 | #endif |
| 850 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 851 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 852 | // In configurations where errors abort instead of propagating error |
| 853 | // return code up the call chain, functions returning BC_STATUS |
| 854 | // actually don't return anything, they always succeed and return "void". |
| 855 | // A macro wrapper is provided, which makes this statement work: |
| 856 | // s = zbc_func(...) |
| 857 | // and makes it visible to the compiler that s is always zero, |
| 858 | // allowing compiler to optimize dead code after the statement. |
| 859 | // |
| 860 | // To make code more readable, each such function has a "z" |
| 861 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. |
| 862 | // |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 863 | #if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 864 | # define ERRORS_ARE_FATAL 0 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 865 | # define ERRORFUNC /*nothing*/ |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 866 | # define IF_ERROR_RETURN_POSSIBLE(a) a |
| 867 | # define BC_STATUS BcStatus |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 868 | # define RETURN_STATUS(v) return (v) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 869 | # define COMMA_SUCCESS /*nothing*/ |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 870 | #else |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 871 | # define ERRORS_ARE_FATAL 1 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 872 | # define ERRORFUNC NORETURN |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 873 | # define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/ |
| 874 | # define BC_STATUS void |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 875 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 876 | # define COMMA_SUCCESS ,BC_STATUS_SUCCESS |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 877 | #endif |
| 878 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 879 | // |
| 880 | // Utility routines |
| 881 | // |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 882 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 883 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 884 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 885 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 886 | static void fflush_and_check(void) |
| 887 | { |
| 888 | fflush_all(); |
| 889 | if (ferror(stdout) || ferror(stderr)) |
| 890 | bb_perror_msg_and_die("output error"); |
| 891 | } |
| 892 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 893 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 894 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 895 | do { \ |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 896 | 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] | 897 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 898 | return BC_STATUS_FAILURE; \ |
| 899 | } while (0) |
| 900 | #else |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 901 | static void quit(void) NORETURN; |
| 902 | static void quit(void) |
| 903 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 904 | if (ferror(stdin)) |
| 905 | bb_perror_msg_and_die("input error"); |
| 906 | fflush_and_check(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 907 | dbg_exec("quit(): exiting with exitcode SUCCESS"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 908 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 909 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 910 | #define QUIT_OR_RETURN_TO_MAIN quit() |
| 911 | #endif |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 912 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 913 | static void bc_verror_msg(const char *fmt, va_list p) |
| 914 | { |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 915 | const char *sv = sv; // for compiler |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 916 | if (G.prs.lex_filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 917 | sv = applet_name; |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 918 | applet_name = xasprintf("%s: %s:%lu", applet_name, |
| 919 | G.prs.lex_filename, (unsigned long)G.err_line |
| 920 | ); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 921 | } |
| 922 | bb_verror_msg(fmt, p, NULL); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 923 | if (G.prs.lex_filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 924 | free((char*)applet_name); |
| 925 | applet_name = sv; |
| 926 | } |
| 927 | } |
| 928 | |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 929 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 930 | { |
| 931 | va_list p; |
| 932 | |
| 933 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 934 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 935 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 936 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 937 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
| 938 | IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE); |
| 939 | exit(1); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 940 | } |
| 941 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 942 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 943 | static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 944 | { |
| 945 | va_list p; |
| 946 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 947 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 948 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 949 | RETURN_STATUS(BC_STATUS_SUCCESS); // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 950 | |
| 951 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 952 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 953 | va_end(p); |
| 954 | |
| 955 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 956 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 957 | RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning |
| 958 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 959 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 960 | RETURN_STATUS(BC_STATUS_FAILURE); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 961 | exit(1); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 962 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 963 | #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] | 964 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 965 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 966 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 967 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 968 | // function must not have caller-cleaned parameters on stack. |
| 969 | // Unfortunately, vararg function API does exactly that on most arches. |
| 970 | // 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] | 971 | static ERRORFUNC int bc_error(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 972 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 973 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 974 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 975 | static ERRORFUNC int bc_error_at(const char *msg) |
| 976 | { |
| 977 | const char *err_at = G.prs.lex_next_at; |
| 978 | if (err_at) { |
| 979 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt( |
| 980 | "%s at '%.*s'", |
| 981 | msg, |
| 982 | (int)(strchrnul(err_at, '\n') - err_at), |
| 983 | err_at |
| 984 | ); |
| 985 | } |
| 986 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
| 987 | } |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 988 | static ERRORFUNC int bc_error_bad_character(char c) |
| 989 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 990 | if (!c) |
| 991 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 992 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 993 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 994 | static ERRORFUNC int bc_error_bad_function_definition(void) |
| 995 | { |
| 996 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad function definition"); |
| 997 | } |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 998 | static ERRORFUNC int bc_error_bad_expression(void) |
| 999 | { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 1000 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad expression"); |
| 1001 | } |
| 1002 | static ERRORFUNC int bc_error_bad_assignment(void) |
| 1003 | { |
| 1004 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at( |
| 1005 | "bad assignment: left side must be variable or array element" |
| 1006 | ); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1007 | } |
| 1008 | static ERRORFUNC int bc_error_bad_token(void) |
| 1009 | { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 1010 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad token"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1011 | } |
| 1012 | static ERRORFUNC int bc_error_stack_has_too_few_elements(void) |
| 1013 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1014 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1015 | } |
| 1016 | static ERRORFUNC int bc_error_variable_is_wrong_type(void) |
| 1017 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1018 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1019 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1020 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1021 | static BC_STATUS zbc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1022 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1023 | RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg)); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1024 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1025 | #define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS) |
| 1026 | static BC_STATUS zbc_POSIX_does_not_allow(const char *msg) |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1027 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1028 | 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] | 1029 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1030 | #define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS) |
| 1031 | 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] | 1032 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1033 | 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] | 1034 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1035 | #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) |
| 1036 | 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] | 1037 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1038 | 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] | 1039 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1040 | #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] | 1041 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1042 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1043 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1044 | { |
| 1045 | size_t cap = v->cap * 2; |
| 1046 | while (cap < v->len + n) cap *= 2; |
| 1047 | v->v = xrealloc(v->v, v->size * cap); |
| 1048 | v->cap = cap; |
| 1049 | } |
| 1050 | |
| 1051 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1052 | { |
| 1053 | v->size = esize; |
| 1054 | v->cap = BC_VEC_START_CAP; |
| 1055 | v->len = 0; |
| 1056 | v->dtor = dtor; |
| 1057 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1058 | } |
| 1059 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1060 | static void bc_char_vec_init(BcVec *v) |
| 1061 | { |
| 1062 | bc_vec_init(v, sizeof(char), NULL); |
| 1063 | } |
| 1064 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1065 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1066 | { |
| 1067 | if (v->cap < req) { |
| 1068 | v->v = xrealloc(v->v, v->size * req); |
| 1069 | v->cap = req; |
| 1070 | } |
| 1071 | } |
| 1072 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1073 | static void bc_vec_pop(BcVec *v) |
| 1074 | { |
| 1075 | v->len--; |
| 1076 | if (v->dtor) |
| 1077 | v->dtor(v->v + (v->size * v->len)); |
| 1078 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1079 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1080 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1081 | { |
| 1082 | if (!v->dtor) |
| 1083 | v->len -= n; |
| 1084 | else { |
| 1085 | size_t len = v->len - n; |
| 1086 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1087 | } |
| 1088 | } |
| 1089 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1090 | static void bc_vec_pop_all(BcVec *v) |
| 1091 | { |
| 1092 | bc_vec_npop(v, v->len); |
| 1093 | } |
| 1094 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1095 | static size_t bc_vec_push(BcVec *v, const void *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 | size_t len = v->len; |
| 1098 | if (len >= v->cap) bc_vec_grow(v, 1); |
| 1099 | memmove(v->v + (v->size * len), data, v->size); |
| 1100 | v->len++; |
| 1101 | return len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1102 | } |
| 1103 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 1104 | // G.prog.results often needs "pop old operand, push result" idiom. |
| 1105 | // Can do this without a few extra ops |
| 1106 | static size_t bc_result_pop_and_push(const void *data) |
| 1107 | { |
| 1108 | BcVec *v = &G.prog.results; |
| 1109 | char *last; |
| 1110 | size_t len = v->len - 1; |
| 1111 | |
| 1112 | last = v->v + (v->size * len); |
| 1113 | if (v->dtor) |
| 1114 | v->dtor(last); |
| 1115 | memmove(last, data, v->size); |
| 1116 | return len; |
| 1117 | } |
| 1118 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1119 | static size_t bc_vec_pushByte(BcVec *v, char data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1120 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1121 | return bc_vec_push(v, &data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1122 | } |
| 1123 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1124 | static size_t bc_vec_pushZeroByte(BcVec *v) |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1125 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1126 | //return bc_vec_pushByte(v, '\0'); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1127 | // better: |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1128 | return bc_vec_push(v, &const_int_0); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1131 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1132 | { |
| 1133 | if (idx == v->len) |
| 1134 | bc_vec_push(v, data); |
| 1135 | else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1136 | char *ptr; |
| 1137 | |
| 1138 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1139 | |
| 1140 | ptr = v->v + v->size * idx; |
| 1141 | |
| 1142 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1143 | memmove(ptr, data, v->size); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1148 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1149 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1150 | bc_vec_expand(v, len + 1); |
| 1151 | memcpy(v->v, str, len); |
| 1152 | v->len = len; |
| 1153 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1154 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1155 | } |
| 1156 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1157 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1158 | { |
| 1159 | return v->v + v->size * idx; |
| 1160 | } |
| 1161 | |
| 1162 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1163 | { |
| 1164 | return v->v + v->size * (v->len - idx - 1); |
| 1165 | } |
| 1166 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1167 | static void *bc_vec_top(const BcVec *v) |
| 1168 | { |
| 1169 | return v->v + v->size * (v->len - 1); |
| 1170 | } |
| 1171 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1172 | static FAST_FUNC void bc_vec_free(void *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1173 | { |
| 1174 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1175 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1176 | free(v->v); |
| 1177 | } |
| 1178 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1179 | static BcFunc* xc_program_func(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1180 | { |
| 1181 | return bc_vec_item(&G.prog.fns, idx); |
| 1182 | } |
| 1183 | // BC_PROG_MAIN is zeroth element, so: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1184 | #define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v)) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1185 | |
| 1186 | #if ENABLE_BC |
| 1187 | static BcFunc* bc_program_current_func(void) |
| 1188 | { |
| 1189 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1190 | BcFunc *func = xc_program_func(ip->func); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1191 | return func; |
| 1192 | } |
| 1193 | #endif |
| 1194 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1195 | static char** xc_program_str(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1196 | { |
| 1197 | #if ENABLE_BC |
| 1198 | if (IS_BC) { |
| 1199 | BcFunc *func = bc_program_current_func(); |
| 1200 | return bc_vec_item(&func->strs, idx); |
| 1201 | } |
| 1202 | #endif |
| 1203 | IF_DC(return bc_vec_item(&G.prog.strs, idx);) |
| 1204 | } |
| 1205 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1206 | static char** xc_program_const(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1207 | { |
| 1208 | #if ENABLE_BC |
| 1209 | if (IS_BC) { |
| 1210 | BcFunc *func = bc_program_current_func(); |
| 1211 | return bc_vec_item(&func->consts, idx); |
| 1212 | } |
| 1213 | #endif |
| 1214 | IF_DC(return bc_vec_item(&G.prog.consts, idx);) |
| 1215 | } |
| 1216 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1217 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1218 | { |
| 1219 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1220 | } |
| 1221 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1222 | static FAST_FUNC void bc_id_free(void *id) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1223 | { |
| 1224 | free(((BcId *) id)->name); |
| 1225 | } |
| 1226 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1227 | 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] | 1228 | { |
| 1229 | size_t low = 0, high = v->len; |
| 1230 | |
| 1231 | while (low < high) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1232 | size_t mid = (low + high) / 2; |
| 1233 | BcId *id = bc_vec_item(v, mid); |
| 1234 | int result = bc_id_cmp(ptr, id); |
| 1235 | |
| 1236 | if (result == 0) |
| 1237 | return mid; |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 1238 | if (result < 0) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1239 | high = mid; |
| 1240 | else |
| 1241 | low = mid + 1; |
| 1242 | } |
| 1243 | |
| 1244 | return low; |
| 1245 | } |
| 1246 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1247 | 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] | 1248 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1249 | size_t n = *i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1250 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1251 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1252 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1253 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1254 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1255 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1256 | bc_vec_pushAt(v, ptr, n); |
| 1257 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1258 | } |
| 1259 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1260 | #if ENABLE_BC |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1261 | 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] | 1262 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1263 | size_t i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1264 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1265 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1266 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1267 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1268 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1269 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1270 | { |
| 1271 | n->len = 0; |
| 1272 | n->neg = false; |
| 1273 | n->rdx = scale; |
| 1274 | } |
| 1275 | |
| 1276 | static void bc_num_zero(BcNum *n) |
| 1277 | { |
| 1278 | bc_num_setToZero(n, 0); |
| 1279 | } |
| 1280 | |
| 1281 | static void bc_num_one(BcNum *n) |
| 1282 | { |
| 1283 | bc_num_setToZero(n, 0); |
| 1284 | n->len = 1; |
| 1285 | n->num[0] = 1; |
| 1286 | } |
| 1287 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1288 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1289 | static void bc_num_init(BcNum *n, size_t req) |
| 1290 | { |
| 1291 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1292 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1293 | n->num = xmalloc(req); |
| 1294 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1295 | n->rdx = 0; |
| 1296 | n->len = 0; |
| 1297 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1298 | } |
| 1299 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1300 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1301 | { |
| 1302 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1303 | } |
| 1304 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1305 | static void bc_num_expand(BcNum *n, size_t req) |
| 1306 | { |
| 1307 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1308 | if (req > n->cap) { |
| 1309 | n->num = xrealloc(n->num, req); |
| 1310 | n->cap = req; |
| 1311 | } |
| 1312 | } |
| 1313 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1314 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1315 | { |
| 1316 | free(((BcNum *) num)->num); |
| 1317 | } |
| 1318 | |
| 1319 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1320 | { |
| 1321 | if (d != s) { |
| 1322 | bc_num_expand(d, s->cap); |
| 1323 | d->len = s->len; |
| 1324 | d->neg = s->neg; |
| 1325 | d->rdx = s->rdx; |
| 1326 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1327 | } |
| 1328 | } |
| 1329 | |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1330 | 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] | 1331 | { |
| 1332 | size_t i; |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1333 | unsigned long result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1334 | |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1335 | result = 0; |
| 1336 | i = n->len; |
| 1337 | while (i > n->rdx) { |
| 1338 | unsigned long prev = result; |
| 1339 | result = result * 10 + n->num[--i]; |
| 1340 | // Even overflowed N*10 can still satisfy N*10>=N. For example, |
| 1341 | // 0x1ff00000 * 10 is 0x13f600000, |
| 1342 | // or 0x3f600000 truncated to 32 bits. Which is larger. |
| 1343 | // However, (N*10)/8 < N check is always correct. |
| 1344 | if ((result / 8) < prev) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1345 | RETURN_STATUS(bc_error("overflow")); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1346 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1347 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1348 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1349 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1350 | } |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1351 | #define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS) |
| 1352 | |
| 1353 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
| 1354 | { |
| 1355 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
| 1356 | |
| 1357 | RETURN_STATUS(zbc_num_ulong_abs(n, result_p)); |
| 1358 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1359 | #define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1360 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1361 | #if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295 |
| 1362 | # define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE) |
| 1363 | #elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615 |
| 1364 | # define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE) |
| 1365 | #endif |
| 1366 | // minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num() |
| 1367 | // would not hit realloc() code path - not good if num[] is not malloced |
| 1368 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1369 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1370 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1371 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1372 | |
| 1373 | bc_num_zero(n); |
| 1374 | |
| 1375 | if (val == 0) return; |
| 1376 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1377 | bc_num_expand(n, ULONG_NUM_BUFSIZE); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1378 | |
| 1379 | ptr = n->num; |
| 1380 | for (;;) { |
| 1381 | n->len++; |
| 1382 | *ptr++ = val % 10; |
| 1383 | val /= 10; |
| 1384 | if (val == 0) break; |
| 1385 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1388 | 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] | 1389 | { |
| 1390 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1391 | for (i = 0; i < len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1392 | a[i] -= b[i]; |
| 1393 | for (j = i; a[j] < 0;) { |
| 1394 | a[j++] += 10; |
| 1395 | a[j] -= 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1396 | } |
| 1397 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1401 | { |
Denys Vlasenko | 4113e1f | 2018-12-18 00:39:24 +0100 | [diff] [blame] | 1402 | size_t i = len; |
| 1403 | for (;;) { |
| 1404 | int c; |
| 1405 | if (i == 0) |
| 1406 | return 0; |
| 1407 | i--; |
| 1408 | c = a[i] - b[i]; |
| 1409 | if (c != 0) { |
| 1410 | i++; |
| 1411 | if (c < 0) |
| 1412 | return -i; |
| 1413 | return i; |
| 1414 | } |
| 1415 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1416 | } |
| 1417 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1418 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 1419 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 1420 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 1421 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 1422 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 1423 | { |
| 1424 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 1425 | } |
| 1426 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 1427 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 1428 | { |
| 1429 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 1430 | } |
| 1431 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1432 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1433 | { |
| 1434 | size_t i, min, a_int, b_int, diff; |
| 1435 | BcDig *max_num, *min_num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1436 | bool a_max, neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1437 | ssize_t cmp; |
| 1438 | |
| 1439 | if (a == b) return 0; |
| 1440 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1441 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1442 | |
| 1443 | if (a->neg != b->neg) // signs of a and b differ |
| 1444 | // +a,-b = a>b = 1 or -a,+b = a<b = -1 |
| 1445 | return (int)b->neg - (int)a->neg; |
| 1446 | neg = a->neg; // 1 if both negative, 0 if both positive |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1447 | |
| 1448 | a_int = BC_NUM_INT(a); |
| 1449 | b_int = BC_NUM_INT(b); |
| 1450 | a_int -= b_int; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1451 | |
| 1452 | if (a_int != 0) return (ssize_t) a_int; |
| 1453 | |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1454 | a_max = (a->rdx > b->rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1455 | if (a_max) { |
| 1456 | min = b->rdx; |
| 1457 | diff = a->rdx - b->rdx; |
| 1458 | max_num = a->num + diff; |
| 1459 | min_num = b->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1460 | // neg = (a_max == neg); - NOP (maps 1->1 and 0->0) |
| 1461 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1462 | min = a->rdx; |
| 1463 | diff = b->rdx - a->rdx; |
| 1464 | max_num = b->num + diff; |
| 1465 | min_num = a->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1466 | neg = !neg; // same as "neg = (a_max == neg)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1470 | if (cmp != 0) return BC_NUM_NEG(cmp, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1471 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1472 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1473 | if (max_num[i]) return BC_NUM_NEG(1, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1480 | { |
| 1481 | if (places == 0) return; |
| 1482 | |
| 1483 | n->rdx -= places; |
| 1484 | |
| 1485 | if (n->len != 0) { |
| 1486 | n->len -= places; |
| 1487 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | static void bc_num_extend(BcNum *n, size_t places) |
| 1492 | { |
| 1493 | size_t len = n->len + places; |
| 1494 | |
| 1495 | if (places != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1496 | if (n->cap < len) bc_num_expand(n, len); |
| 1497 | |
| 1498 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1499 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1500 | |
| 1501 | n->len += places; |
| 1502 | n->rdx += places; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | static void bc_num_clean(BcNum *n) |
| 1507 | { |
| 1508 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1509 | if (n->len == 0) |
| 1510 | n->neg = false; |
| 1511 | else if (n->len < n->rdx) |
| 1512 | n->len = n->rdx; |
| 1513 | } |
| 1514 | |
| 1515 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1516 | { |
| 1517 | if (n->rdx < scale) |
| 1518 | bc_num_extend(n, scale - n->rdx); |
| 1519 | else |
| 1520 | bc_num_truncate(n, n->rdx - scale); |
| 1521 | |
| 1522 | bc_num_clean(n); |
| 1523 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1524 | } |
| 1525 | |
| 1526 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1527 | BcNum *restrict b) |
| 1528 | { |
| 1529 | if (idx < n->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1530 | b->len = n->len - idx; |
| 1531 | a->len = idx; |
| 1532 | a->rdx = b->rdx = 0; |
| 1533 | |
| 1534 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1535 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1536 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1537 | bc_num_zero(b); |
| 1538 | bc_num_copy(a, n); |
| 1539 | } |
| 1540 | |
| 1541 | bc_num_clean(a); |
| 1542 | bc_num_clean(b); |
| 1543 | } |
| 1544 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1545 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1546 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1547 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1548 | |
| 1549 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1550 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1551 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1552 | 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] | 1553 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1554 | |
| 1555 | if (n->rdx >= places) |
| 1556 | n->rdx -= places; |
| 1557 | else { |
| 1558 | bc_num_extend(n, places - n->rdx); |
| 1559 | n->rdx = 0; |
| 1560 | } |
| 1561 | |
| 1562 | bc_num_clean(n); |
| 1563 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1564 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1565 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1566 | #define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1567 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1568 | typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
| 1569 | |
| 1570 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 1571 | BcNumBinaryOp op, size_t req) |
| 1572 | { |
| 1573 | BcStatus s; |
| 1574 | BcNum num2, *ptr_a, *ptr_b; |
| 1575 | bool init = false; |
| 1576 | |
| 1577 | if (c == a) { |
| 1578 | ptr_a = &num2; |
| 1579 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 1580 | init = true; |
| 1581 | } else |
| 1582 | ptr_a = a; |
| 1583 | |
| 1584 | if (c == b) { |
| 1585 | ptr_b = &num2; |
| 1586 | if (c != a) { |
| 1587 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 1588 | init = true; |
| 1589 | } |
| 1590 | } else |
| 1591 | ptr_b = b; |
| 1592 | |
| 1593 | if (init) |
| 1594 | bc_num_init(c, req); |
| 1595 | else |
| 1596 | bc_num_expand(c, req); |
| 1597 | |
| 1598 | s = BC_STATUS_SUCCESS; |
| 1599 | IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale); |
| 1600 | |
| 1601 | if (init) bc_num_free(&num2); |
| 1602 | |
| 1603 | RETURN_STATUS(s); |
| 1604 | } |
| 1605 | #define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS) |
| 1606 | |
| 1607 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1608 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1609 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1610 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1611 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1612 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1613 | |
| 1614 | static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1615 | { |
| 1616 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s; |
| 1617 | (void) scale; |
| 1618 | RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b))); |
| 1619 | } |
| 1620 | |
| 1621 | static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1622 | { |
| 1623 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a; |
| 1624 | (void) scale; |
| 1625 | RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b))); |
| 1626 | } |
| 1627 | |
| 1628 | static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1629 | { |
| 1630 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1631 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req)); |
| 1632 | } |
| 1633 | |
| 1634 | static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1635 | { |
| 1636 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1637 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req)); |
| 1638 | } |
| 1639 | |
| 1640 | static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1641 | { |
| 1642 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1643 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req)); |
| 1644 | } |
| 1645 | |
| 1646 | static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1647 | { |
| 1648 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); |
| 1649 | } |
| 1650 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1651 | static const BcNumBinaryOp zxc_program_ops[] = { |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1652 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, |
| 1653 | }; |
| 1654 | #define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS) |
| 1655 | #define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS) |
| 1656 | #define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS) |
| 1657 | #define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS) |
| 1658 | #define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS) |
| 1659 | #define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS) |
| 1660 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1661 | 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] | 1662 | { |
| 1663 | BcNum one; |
| 1664 | BcDig num[2]; |
| 1665 | |
| 1666 | one.cap = 2; |
| 1667 | one.num = num; |
| 1668 | bc_num_one(&one); |
| 1669 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1670 | RETURN_STATUS(zbc_num_div(&one, a, b, scale)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1671 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1672 | #define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1673 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1674 | 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] | 1675 | { |
| 1676 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1677 | 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] | 1678 | unsigned carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1679 | |
| 1680 | // Because this function doesn't need to use scale (per the bc spec), |
| 1681 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1682 | |
| 1683 | if (a->len == 0) { |
| 1684 | bc_num_copy(c, b); |
| 1685 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1686 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1687 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1688 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1689 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1690 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | c->neg = a->neg; |
| 1694 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1695 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1696 | c->len = 0; |
| 1697 | |
| 1698 | if (a->rdx > b->rdx) { |
| 1699 | diff = a->rdx - b->rdx; |
| 1700 | ptr = a->num; |
| 1701 | ptr_a = a->num + diff; |
| 1702 | ptr_b = b->num; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1703 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1704 | diff = b->rdx - a->rdx; |
| 1705 | ptr = b->num; |
| 1706 | ptr_a = a->num; |
| 1707 | ptr_b = b->num + diff; |
| 1708 | } |
| 1709 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1710 | ptr_c = c->num; |
| 1711 | for (i = 0; i < diff; ++i, ++c->len) |
| 1712 | ptr_c[i] = ptr[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1713 | |
| 1714 | ptr_c += diff; |
| 1715 | a_int = BC_NUM_INT(a); |
| 1716 | b_int = BC_NUM_INT(b); |
| 1717 | |
| 1718 | if (a_int > b_int) { |
| 1719 | min_int = b_int; |
| 1720 | max = a_int; |
| 1721 | ptr = ptr_a; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1722 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1723 | min_int = a_int; |
| 1724 | max = b_int; |
| 1725 | ptr = ptr_b; |
| 1726 | } |
| 1727 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1728 | carry = 0; |
| 1729 | for (i = 0; i < min_rdx + min_int; ++i) { |
| 1730 | unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1731 | carry = in / 10; |
| 1732 | ptr_c[i] = (BcDig)(in % 10); |
| 1733 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1734 | for (; i < max + min_rdx; ++i) { |
| 1735 | unsigned in = (unsigned)ptr[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1736 | carry = in / 10; |
| 1737 | ptr_c[i] = (BcDig)(in % 10); |
| 1738 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1739 | c->len += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1740 | |
| 1741 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1742 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1743 | 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] | 1744 | } |
| 1745 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1746 | 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] | 1747 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1748 | ssize_t cmp; |
| 1749 | BcNum *minuend, *subtrahend; |
| 1750 | size_t start; |
| 1751 | bool aneg, bneg, neg; |
| 1752 | |
| 1753 | // Because this function doesn't need to use scale (per the bc spec), |
| 1754 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1755 | |
| 1756 | if (a->len == 0) { |
| 1757 | bc_num_copy(c, b); |
| 1758 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1759 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1760 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1761 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1762 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1763 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | aneg = a->neg; |
| 1767 | bneg = b->neg; |
| 1768 | a->neg = b->neg = false; |
| 1769 | |
| 1770 | cmp = bc_num_cmp(a, b); |
| 1771 | |
| 1772 | a->neg = aneg; |
| 1773 | b->neg = bneg; |
| 1774 | |
| 1775 | if (cmp == 0) { |
| 1776 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1777 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1778 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1779 | if (cmp > 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1780 | neg = a->neg; |
| 1781 | minuend = a; |
| 1782 | subtrahend = b; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1783 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1784 | neg = b->neg; |
| 1785 | if (sub) neg = !neg; |
| 1786 | minuend = b; |
| 1787 | subtrahend = a; |
| 1788 | } |
| 1789 | |
| 1790 | bc_num_copy(c, minuend); |
| 1791 | c->neg = neg; |
| 1792 | |
| 1793 | if (c->rdx < subtrahend->rdx) { |
| 1794 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1795 | start = 0; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1796 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1797 | start = c->rdx - subtrahend->rdx; |
| 1798 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1799 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1800 | |
| 1801 | bc_num_clean(c); |
| 1802 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1803 | 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] | 1804 | } |
| 1805 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1806 | 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] | 1807 | BcNum *restrict c) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1808 | #define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1809 | { |
| 1810 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1811 | 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] | 1812 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1813 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1814 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1815 | if (a->len == 0 || b->len == 0) { |
| 1816 | bc_num_zero(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1817 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1818 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1819 | aone = BC_NUM_ONE(a); |
| 1820 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1821 | bc_num_copy(c, aone ? b : a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1822 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1823 | } |
| 1824 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1825 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN |
| 1826 | || a->len < BC_NUM_KARATSUBA_LEN |
| 1827 | || b->len < BC_NUM_KARATSUBA_LEN |
| 1828 | ) { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1829 | size_t i, j, len; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1830 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1831 | bc_num_expand(c, a->len + b->len + 1); |
| 1832 | |
| 1833 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1834 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1835 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1836 | for (i = 0; i < b->len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1837 | unsigned carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1838 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1839 | unsigned in = c->num[i + j]; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1840 | in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1841 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1842 | carry = in / 10; |
| 1843 | c->num[i + j] = (BcDig)(in % 10); |
| 1844 | } |
| 1845 | |
| 1846 | c->num[i + j] += (BcDig) carry; |
| 1847 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1848 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 1849 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1850 | // a=2^1000000 |
| 1851 | // a*a <- without check below, this will not be interruptible |
| 1852 | if (G_interrupt) return BC_STATUS_FAILURE; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1853 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | c->len = len; |
| 1857 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1858 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | bc_num_init(&l1, max); |
| 1862 | bc_num_init(&h1, max); |
| 1863 | bc_num_init(&l2, max); |
| 1864 | bc_num_init(&h2, max); |
| 1865 | bc_num_init(&m1, max); |
| 1866 | bc_num_init(&m2, max); |
| 1867 | bc_num_init(&z0, max); |
| 1868 | bc_num_init(&z1, max); |
| 1869 | bc_num_init(&z2, max); |
| 1870 | bc_num_init(&temp, max + max); |
| 1871 | |
| 1872 | bc_num_split(a, max2, &l1, &h1); |
| 1873 | bc_num_split(b, max2, &l2, &h2); |
| 1874 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1875 | s = zbc_num_add(&h1, &l1, &m1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1876 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1877 | s = zbc_num_add(&h2, &l2, &m2, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1878 | if (s) goto err; |
| 1879 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1880 | s = zbc_num_k(&h1, &h2, &z0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1881 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1882 | s = zbc_num_k(&m1, &m2, &z1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1883 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1884 | s = zbc_num_k(&l1, &l2, &z2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1885 | if (s) goto err; |
| 1886 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1887 | s = zbc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1888 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1889 | s = zbc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1890 | if (s) goto err; |
| 1891 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1892 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1893 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1894 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1895 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1896 | s = zbc_num_add(&z0, &z1, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1897 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1898 | s = zbc_num_add(&temp, &z2, c, 0); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1899 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1900 | bc_num_free(&temp); |
| 1901 | bc_num_free(&z2); |
| 1902 | bc_num_free(&z1); |
| 1903 | bc_num_free(&z0); |
| 1904 | bc_num_free(&m2); |
| 1905 | bc_num_free(&m1); |
| 1906 | bc_num_free(&h2); |
| 1907 | bc_num_free(&l2); |
| 1908 | bc_num_free(&h1); |
| 1909 | bc_num_free(&l1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1910 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1911 | } |
| 1912 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1913 | 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] | 1914 | { |
| 1915 | BcStatus s; |
| 1916 | BcNum cpa, cpb; |
| 1917 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1918 | |
| 1919 | scale = BC_MAX(scale, a->rdx); |
| 1920 | scale = BC_MAX(scale, b->rdx); |
| 1921 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1922 | maxrdx = BC_MAX(maxrdx, scale); |
| 1923 | |
| 1924 | bc_num_init(&cpa, a->len); |
| 1925 | bc_num_init(&cpb, b->len); |
| 1926 | |
| 1927 | bc_num_copy(&cpa, a); |
| 1928 | bc_num_copy(&cpb, b); |
| 1929 | cpa.neg = cpb.neg = false; |
| 1930 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1931 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1932 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1933 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1934 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1935 | s = zbc_num_k(&cpa, &cpb, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1936 | if (s) goto err; |
| 1937 | |
| 1938 | maxrdx += scale; |
| 1939 | bc_num_expand(c, c->len + maxrdx); |
| 1940 | |
| 1941 | if (c->len < maxrdx) { |
| 1942 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 1943 | c->len += maxrdx; |
| 1944 | } |
| 1945 | |
| 1946 | c->rdx = maxrdx; |
| 1947 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1948 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1949 | bc_num_free(&cpb); |
| 1950 | bc_num_free(&cpa); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1951 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1952 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1953 | #define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1954 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1955 | 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] | 1956 | { |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 1957 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1958 | size_t len, end, i; |
| 1959 | BcNum cp; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1960 | |
| 1961 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1962 | RETURN_STATUS(bc_error("divide by zero")); |
| 1963 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1964 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1965 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1966 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1967 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1968 | bc_num_copy(c, a); |
| 1969 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1970 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 1974 | bc_num_copy(&cp, a); |
| 1975 | len = b->len; |
| 1976 | |
| 1977 | if (len > cp.len) { |
| 1978 | bc_num_expand(&cp, len + 2); |
| 1979 | bc_num_extend(&cp, len - cp.len); |
| 1980 | } |
| 1981 | |
| 1982 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 1983 | cp.rdx -= b->rdx; |
| 1984 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 1985 | |
| 1986 | if (b->rdx == b->len) { |
Denys Vlasenko | 71c82d1 | 2018-12-18 12:43:21 +0100 | [diff] [blame] | 1987 | for (;;) { |
| 1988 | if (len == 0) break; |
| 1989 | len--; |
| 1990 | if (b->num[len] != 0) |
| 1991 | break; |
| 1992 | } |
| 1993 | len++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 1997 | |
| 1998 | // We want an extra zero in front to make things simpler. |
| 1999 | cp.num[cp.len++] = 0; |
| 2000 | end = cp.len - len; |
| 2001 | |
| 2002 | bc_num_expand(c, cp.len); |
| 2003 | |
| 2004 | bc_num_zero(c); |
| 2005 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2006 | c->rdx = cp.rdx; |
| 2007 | c->len = cp.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2008 | |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2009 | s = BC_STATUS_SUCCESS; |
| 2010 | for (i = end - 1; i < end; --i) { |
| 2011 | BcDig *n, q; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2012 | n = cp.num + i; |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2013 | for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q) |
| 2014 | bc_num_subArrays(n, b->num, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2015 | c->num[i] = q; |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2016 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2017 | // a=2^100000 |
| 2018 | // scale=40000 |
| 2019 | // 1/a <- without check below, this will not be interruptible |
| 2020 | if (G_interrupt) { |
| 2021 | s = BC_STATUS_FAILURE; |
| 2022 | break; |
| 2023 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2024 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2025 | } |
| 2026 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2027 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2028 | bc_num_free(&cp); |
| 2029 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2030 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2031 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2032 | #define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2033 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2034 | 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] | 2035 | BcNum *restrict d, size_t scale, size_t ts) |
| 2036 | { |
| 2037 | BcStatus s; |
| 2038 | BcNum temp; |
| 2039 | bool neg; |
| 2040 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2041 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2042 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2043 | |
| 2044 | if (a->len == 0) { |
| 2045 | bc_num_setToZero(d, ts); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2046 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2050 | s = zbc_num_d(a, b, c, scale); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2051 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2052 | |
| 2053 | if (scale != 0) scale = ts; |
| 2054 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2055 | s = zbc_num_m(c, b, &temp, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2056 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2057 | s = zbc_num_sub(a, &temp, d, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2058 | if (s) goto err; |
| 2059 | |
| 2060 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2061 | |
| 2062 | neg = d->neg; |
| 2063 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2064 | d->neg = neg; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2065 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2066 | bc_num_free(&temp); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2067 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2068 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2069 | #define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2070 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2071 | 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] | 2072 | { |
| 2073 | BcStatus s; |
| 2074 | BcNum c1; |
| 2075 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2076 | |
| 2077 | bc_num_init(&c1, len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2078 | s = zbc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2079 | bc_num_free(&c1); |
| 2080 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2081 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2082 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2083 | #define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2084 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2085 | 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] | 2086 | { |
| 2087 | BcStatus s = BC_STATUS_SUCCESS; |
| 2088 | BcNum copy; |
| 2089 | unsigned long pow; |
| 2090 | size_t i, powrdx, resrdx; |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2091 | bool neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2092 | |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2093 | // GNU bc does not allow 2^2.0 - we do |
| 2094 | for (i = 0; i < b->rdx; i++) |
| 2095 | if (b->num[i] != 0) |
| 2096 | RETURN_STATUS(bc_error("not an integer")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2097 | |
| 2098 | if (b->len == 0) { |
| 2099 | bc_num_one(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2100 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2101 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2102 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2103 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2104 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2105 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2106 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2107 | if (!b->neg) |
| 2108 | bc_num_copy(c, a); |
| 2109 | else |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2110 | s = zbc_num_inv(a, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2111 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | neg = b->neg; |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 2115 | s = zbc_num_ulong_abs(b, &pow); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2116 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 2ea8ddf | 2018-12-22 21:45:18 +0100 | [diff] [blame] | 2117 | // b is not used beyond this point |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2118 | |
| 2119 | bc_num_init(©, a->len); |
| 2120 | bc_num_copy(©, a); |
| 2121 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2122 | if (!neg) { |
| 2123 | if (a->rdx > scale) |
| 2124 | scale = a->rdx; |
| 2125 | if (a->rdx * pow < scale) |
| 2126 | scale = a->rdx * pow; |
| 2127 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2128 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2129 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2130 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2131 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2132 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2133 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2134 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2135 | //if (G_interrupt) { |
| 2136 | // s = BC_STATUS_FAILURE; |
| 2137 | // goto err; |
| 2138 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2139 | } |
| 2140 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2141 | bc_num_copy(c, ©); |
| 2142 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2143 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2144 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2145 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2146 | if (s) goto err; |
| 2147 | |
| 2148 | if (pow & 1) { |
| 2149 | resrdx += powrdx; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2150 | s = zbc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2151 | if (s) goto err; |
| 2152 | } |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2153 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2154 | //if (G_interrupt) { |
| 2155 | // s = BC_STATUS_FAILURE; |
| 2156 | // goto err; |
| 2157 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | if (neg) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2161 | s = zbc_num_inv(c, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2162 | if (s) goto err; |
| 2163 | } |
| 2164 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2165 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2166 | |
| 2167 | // We can't use bc_num_clean() here. |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2168 | for (i = 0; i < c->len; ++i) |
| 2169 | if (c->num[i] != 0) |
| 2170 | goto skip; |
| 2171 | bc_num_setToZero(c, scale); |
| 2172 | skip: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2173 | |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2174 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2175 | bc_num_free(©); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2176 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2177 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2178 | #define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2179 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2180 | 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] | 2181 | { |
| 2182 | BcStatus s; |
| 2183 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2184 | BcDig half_digs[1]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2185 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2186 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2187 | |
| 2188 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2189 | bc_num_expand(b, req); |
| 2190 | |
| 2191 | if (a->len == 0) { |
| 2192 | bc_num_setToZero(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2193 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2194 | } |
| 2195 | if (a->neg) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2196 | RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2197 | } |
| 2198 | if (BC_NUM_ONE(a)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2199 | bc_num_one(b); |
| 2200 | bc_num_extend(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2201 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2205 | len = a->len + scale; |
| 2206 | |
| 2207 | bc_num_init(&num1, len); |
| 2208 | bc_num_init(&num2, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2209 | |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2210 | half.cap = ARRAY_SIZE(half_digs); |
| 2211 | half.num = half_digs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2212 | bc_num_one(&half); |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2213 | half_digs[0] = 5; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2214 | half.rdx = 1; |
| 2215 | |
| 2216 | bc_num_init(&f, len); |
| 2217 | bc_num_init(&fprime, len); |
| 2218 | |
| 2219 | x0 = &num1; |
| 2220 | x1 = &num2; |
| 2221 | |
| 2222 | bc_num_one(x0); |
| 2223 | pow = BC_NUM_INT(a); |
| 2224 | |
| 2225 | if (pow) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2226 | if (pow & 1) |
| 2227 | x0->num[0] = 2; |
| 2228 | else |
| 2229 | x0->num[0] = 6; |
| 2230 | |
| 2231 | pow -= 2 - (pow & 1); |
| 2232 | |
| 2233 | bc_num_extend(x0, pow); |
| 2234 | |
| 2235 | // Make sure to move the radix back. |
| 2236 | x0->rdx -= pow; |
| 2237 | } |
| 2238 | |
| 2239 | x0->rdx = digs = digs1 = 0; |
| 2240 | resrdx = scale + 2; |
| 2241 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2242 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2243 | while (cmp != 0 || digs < len) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2244 | s = zbc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2245 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2246 | s = zbc_num_add(x0, &f, &fprime, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2247 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2248 | s = zbc_num_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2249 | if (s) goto err; |
| 2250 | |
| 2251 | cmp = bc_num_cmp(x1, x0); |
| 2252 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2253 | |
| 2254 | if (cmp == cmp2 && digs == digs1) |
| 2255 | times += 1; |
| 2256 | else |
| 2257 | times = 0; |
| 2258 | |
| 2259 | resrdx += times > 4; |
| 2260 | |
| 2261 | cmp2 = cmp1; |
| 2262 | cmp1 = cmp; |
| 2263 | digs1 = digs; |
| 2264 | |
| 2265 | temp = x0; |
| 2266 | x0 = x1; |
| 2267 | x1 = temp; |
| 2268 | } |
| 2269 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2270 | bc_num_copy(b, x0); |
| 2271 | scale -= 1; |
| 2272 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2273 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2274 | bc_num_free(&fprime); |
| 2275 | bc_num_free(&f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2276 | bc_num_free(&num2); |
| 2277 | bc_num_free(&num1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2278 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2279 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2280 | #define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2281 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2282 | 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] | 2283 | size_t scale) |
| 2284 | { |
| 2285 | BcStatus s; |
| 2286 | BcNum num2, *ptr_a; |
| 2287 | bool init = false; |
| 2288 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2289 | |
| 2290 | if (c == a) { |
| 2291 | memcpy(&num2, c, sizeof(BcNum)); |
| 2292 | ptr_a = &num2; |
| 2293 | bc_num_init(c, len); |
| 2294 | init = true; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2295 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2296 | ptr_a = a; |
| 2297 | bc_num_expand(c, len); |
| 2298 | } |
| 2299 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2300 | s = zbc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2301 | |
| 2302 | if (init) bc_num_free(&num2); |
| 2303 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2304 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2305 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2306 | #define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2307 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2308 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2309 | 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] | 2310 | { |
| 2311 | BcStatus s; |
| 2312 | BcNum base, exp, two, temp; |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2313 | BcDig two_digs[1]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2314 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2315 | if (c->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2316 | RETURN_STATUS(bc_error("divide by zero")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2317 | if (a->rdx || b->rdx || c->rdx) |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2318 | RETURN_STATUS(bc_error("not an integer")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2319 | if (b->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2320 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2321 | |
| 2322 | bc_num_expand(d, c->len); |
| 2323 | bc_num_init(&base, c->len); |
| 2324 | bc_num_init(&exp, b->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2325 | bc_num_init(&temp, b->len); |
| 2326 | |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2327 | two.cap = ARRAY_SIZE(two_digs); |
| 2328 | two.num = two_digs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2329 | bc_num_one(&two); |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2330 | two_digs[0] = 2; |
| 2331 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2332 | bc_num_one(d); |
| 2333 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2334 | s = zbc_num_rem(a, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2335 | if (s) goto err; |
| 2336 | bc_num_copy(&exp, b); |
| 2337 | |
| 2338 | while (exp.len != 0) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2339 | s = zbc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2340 | if (s) goto err; |
| 2341 | |
| 2342 | if (BC_NUM_ONE(&temp)) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2343 | s = zbc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2344 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2345 | s = zbc_num_rem(&temp, c, d, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2346 | if (s) goto err; |
| 2347 | } |
| 2348 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2349 | s = zbc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2350 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2351 | s = zbc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2352 | if (s) goto err; |
| 2353 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2354 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2355 | bc_num_free(&temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2356 | bc_num_free(&exp); |
| 2357 | bc_num_free(&base); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2358 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2359 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2360 | #define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2361 | #endif // ENABLE_DC |
| 2362 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2363 | static FAST_FUNC void bc_string_free(void *string) |
| 2364 | { |
| 2365 | free(*(char**)string); |
| 2366 | } |
| 2367 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2368 | static void bc_func_init(BcFunc *f) |
| 2369 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2370 | bc_char_vec_init(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2371 | IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);) |
| 2372 | IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2373 | IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);) |
| 2374 | IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2375 | IF_BC(f->nparams = 0;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2376 | } |
| 2377 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2378 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2379 | { |
| 2380 | BcFunc *f = (BcFunc *) func; |
| 2381 | bc_vec_free(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2382 | IF_BC(bc_vec_free(&f->labels);) |
| 2383 | IF_BC(bc_vec_free(&f->autos);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2384 | IF_BC(bc_vec_free(&f->strs);) |
| 2385 | IF_BC(bc_vec_free(&f->consts);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2386 | } |
| 2387 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2388 | static void bc_array_expand(BcVec *a, size_t len); |
| 2389 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2390 | static void bc_array_init(BcVec *a, bool nums) |
| 2391 | { |
| 2392 | if (nums) |
| 2393 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2394 | else |
| 2395 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2396 | bc_array_expand(a, 1); |
| 2397 | } |
| 2398 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2399 | static void bc_array_expand(BcVec *a, size_t len) |
| 2400 | { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2401 | if (a->dtor == bc_num_free |
| 2402 | // && a->size == sizeof(BcNum) - always true |
| 2403 | ) { |
| 2404 | BcNum n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2405 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2406 | bc_num_init_DEF_SIZE(&n); |
| 2407 | bc_vec_push(a, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2408 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2409 | } else { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2410 | BcVec v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2411 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2412 | bc_array_init(&v, true); |
| 2413 | bc_vec_push(a, &v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | } |
| 2417 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2418 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2419 | { |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2420 | BcNum *dnum, *snum; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2421 | size_t i; |
| 2422 | |
| 2423 | bc_vec_pop_all(d); |
| 2424 | bc_vec_expand(d, s->cap); |
| 2425 | d->len = s->len; |
| 2426 | |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2427 | dnum = (void*)d->v; |
| 2428 | snum = (void*)s->v; |
| 2429 | for (i = 0; i < s->len; i++, dnum++, snum++) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2430 | bc_num_init(dnum, snum->len); |
| 2431 | bc_num_copy(dnum, snum); |
| 2432 | } |
| 2433 | } |
| 2434 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2435 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2436 | static void dc_result_copy(BcResult *d, BcResult *src) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2437 | { |
| 2438 | d->t = src->t; |
| 2439 | |
| 2440 | switch (d->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2441 | case XC_RESULT_TEMP: |
| 2442 | case XC_RESULT_IBASE: |
| 2443 | case XC_RESULT_SCALE: |
| 2444 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2445 | bc_num_init(&d->d.n, src->d.n.len); |
| 2446 | bc_num_copy(&d->d.n, &src->d.n); |
| 2447 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2448 | case XC_RESULT_VAR: |
| 2449 | case XC_RESULT_ARRAY: |
| 2450 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2451 | d->d.id.name = xstrdup(src->d.id.name); |
| 2452 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2453 | case XC_RESULT_CONSTANT: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 2454 | IF_BC(case BC_RESULT_LAST:) |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2455 | IF_BC(case BC_RESULT_ONE:) |
| 2456 | case XC_RESULT_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2457 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2458 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | #endif // ENABLE_DC |
| 2462 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2463 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2464 | { |
| 2465 | BcResult *r = (BcResult *) result; |
| 2466 | |
| 2467 | switch (r->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2468 | case XC_RESULT_TEMP: |
| 2469 | case XC_RESULT_IBASE: |
| 2470 | case XC_RESULT_SCALE: |
| 2471 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2472 | bc_num_free(&r->d.n); |
| 2473 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2474 | case XC_RESULT_VAR: |
| 2475 | case XC_RESULT_ARRAY: |
| 2476 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2477 | free(r->d.id.name); |
| 2478 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2479 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2480 | // Do nothing. |
| 2481 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2482 | } |
| 2483 | } |
| 2484 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2485 | static int bad_input_byte(char c) |
| 2486 | { |
| 2487 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 2488 | || c > 0x7e |
| 2489 | ) { |
| 2490 | bc_error_fmt("illegal character 0x%02x", c); |
| 2491 | return 1; |
| 2492 | } |
| 2493 | return 0; |
| 2494 | } |
| 2495 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2496 | static void xc_read_line(BcVec *vec, FILE *fp) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2497 | { |
| 2498 | again: |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2499 | bc_vec_pop_all(vec); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2500 | fflush_and_check(); |
| 2501 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2502 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2503 | if (G_interrupt) { // ^C was pressed |
| 2504 | intr: |
| 2505 | if (fp != stdin) { |
| 2506 | // ^C while running a script (bc SCRIPT): die. |
| 2507 | // We do not return to interactive prompt: |
| 2508 | // user might be running us from a shell, |
| 2509 | // and SCRIPT might be intended to terminate |
| 2510 | // (e.g. contain a "halt" stmt). |
| 2511 | // ^C dropping user into a bc prompt instead of |
| 2512 | // the shell would be unexpected. |
| 2513 | xfunc_die(); |
| 2514 | } |
| 2515 | // ^C while interactive input |
| 2516 | G_interrupt = 0; |
| 2517 | // GNU bc says "interrupted execution." |
| 2518 | // GNU dc says "Interrupt!" |
| 2519 | fputs("\ninterrupted execution\n", stderr); |
| 2520 | } |
| 2521 | |
| 2522 | # if ENABLE_FEATURE_EDITING |
| 2523 | if (G_ttyin && fp == stdin) { |
| 2524 | int n, i; |
| 2525 | # define line_buf bb_common_bufsiz1 |
| 2526 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
| 2527 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 2528 | if (n == 0) // ^C |
| 2529 | goto intr; |
| 2530 | bc_vec_pushZeroByte(vec); // ^D or EOF (or error) |
| 2531 | return; |
| 2532 | } |
| 2533 | i = 0; |
| 2534 | for (;;) { |
| 2535 | char c = line_buf[i++]; |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2536 | if (c == '\0') break; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2537 | if (bad_input_byte(c)) goto again; |
| 2538 | } |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2539 | bc_vec_string(vec, n, line_buf); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2540 | # undef line_buf |
| 2541 | } else |
| 2542 | # endif |
| 2543 | #endif |
| 2544 | { |
| 2545 | int c; |
| 2546 | bool bad_chars = 0; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2547 | |
| 2548 | do { |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2549 | get_char: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2550 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2551 | if (G_interrupt) { |
| 2552 | // ^C was pressed: ignore entire line, get another one |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2553 | goto again; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2554 | } |
| 2555 | #endif |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2556 | c = fgetc(fp); |
| 2557 | if (c == '\0') |
| 2558 | goto get_char; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2559 | if (c == EOF) { |
| 2560 | if (ferror(fp)) |
| 2561 | bb_perror_msg_and_die("input error"); |
| 2562 | // Note: EOF does not append '\n' |
| 2563 | break; |
| 2564 | } |
| 2565 | bad_chars |= bad_input_byte(c); |
| 2566 | bc_vec_pushByte(vec, (char)c); |
| 2567 | } while (c != '\n'); |
| 2568 | |
| 2569 | if (bad_chars) { |
| 2570 | // Bad chars on this line |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2571 | if (!G.prs.lex_filename) { // stdin |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2572 | // ignore entire line, get another one |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2573 | goto again; |
| 2574 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2575 | 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] | 2576 | } |
| 2577 | bc_vec_pushZeroByte(vec); |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | // |
| 2582 | // Parsing routines |
| 2583 | // |
| 2584 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2585 | // "Input numbers may contain the characters 0-9 and A-Z. |
| 2586 | // (Note: They must be capitals. Lower case letters are variable names.) |
| 2587 | // Single digit numbers always have the value of the digit regardless of |
| 2588 | // the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes |
| 2589 | // all input digits greater or equal to ibase to the value of ibase-1. |
| 2590 | // This makes the number ZZZ always be the largest 3 digit number of the |
| 2591 | // input base." |
| 2592 | static bool xc_num_strValid(const char *val) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2593 | { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2594 | bool radix = false; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2595 | for (;;) { |
| 2596 | BcDig c = *val++; |
| 2597 | if (c == '\0') |
| 2598 | break; |
| 2599 | if (c == '.') { |
| 2600 | if (radix) return false; |
| 2601 | radix = true; |
| 2602 | continue; |
| 2603 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2604 | if ((c < '0' || c > '9') && (c < 'A' || c > 'Z')) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2605 | return false; |
| 2606 | } |
| 2607 | return true; |
| 2608 | } |
| 2609 | |
| 2610 | // Note: n is already "bc_num_zero()"ed, |
| 2611 | // leading zeroes in "val" are removed |
| 2612 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2613 | { |
| 2614 | size_t len, i; |
| 2615 | const char *ptr; |
| 2616 | |
| 2617 | len = strlen(val); |
| 2618 | if (len == 0) |
| 2619 | return; |
| 2620 | |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2621 | 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] | 2622 | |
| 2623 | ptr = strchr(val, '.'); |
| 2624 | |
| 2625 | n->rdx = 0; |
| 2626 | if (ptr != NULL) |
| 2627 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2628 | |
| 2629 | for (i = 0; val[i]; ++i) { |
| 2630 | if (val[i] != '0' && val[i] != '.') { |
| 2631 | // Not entirely zero value - convert it, and exit |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2632 | if (len == 1) { |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2633 | unsigned c = val[0] - '0'; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2634 | n->len = 1; |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2635 | if (c > 9) { // A-Z => 10-36 |
| 2636 | n->len = 2; |
| 2637 | c -= ('A' - '9' - 1); |
| 2638 | n->num[1] = c/10; |
| 2639 | c = c%10; |
| 2640 | } |
| 2641 | n->num[0] = c; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2642 | break; |
| 2643 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2644 | i = len - 1; |
| 2645 | for (;;) { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2646 | char c = val[i] - '0'; |
| 2647 | if (c > 9) // A-Z => 9 |
| 2648 | c = 9; |
| 2649 | n->num[n->len] = c; |
| 2650 | n->len++; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2651 | skip_dot: |
| 2652 | if (i == 0) break; |
| 2653 | if (val[--i] == '.') goto skip_dot; |
| 2654 | } |
| 2655 | break; |
| 2656 | } |
| 2657 | } |
| 2658 | // if for() exits without hitting if(), the value is entirely zero |
| 2659 | } |
| 2660 | |
| 2661 | // Note: n is already "bc_num_zero()"ed, |
| 2662 | // leading zeroes in "val" are removed |
| 2663 | static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t) |
| 2664 | { |
| 2665 | BcStatus s; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2666 | BcNum mult, result; |
| 2667 | BcNum temp; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2668 | BcNum base; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2669 | BcDig temp_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2670 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2671 | size_t digits; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2672 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2673 | bc_num_init_DEF_SIZE(&mult); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2674 | |
| 2675 | temp.cap = ARRAY_SIZE(temp_digs); |
| 2676 | temp.num = temp_digs; |
| 2677 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2678 | base.cap = ARRAY_SIZE(base_digs); |
| 2679 | base.num = base_digs; |
| 2680 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2681 | base_t--; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2682 | |
| 2683 | for (;;) { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2684 | unsigned v; |
Denys Vlasenko | 8797adc | 2018-12-31 19:50:06 +0100 | [diff] [blame] | 2685 | char c; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2686 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2687 | c = *val++; |
| 2688 | if (c == '\0') goto int_err; |
| 2689 | if (c == '.') break; |
| 2690 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2691 | v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10); |
| 2692 | if (v > base_t) v = base_t; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2693 | |
| 2694 | s = zbc_num_mul(n, &base, &mult, 0); |
| 2695 | if (s) goto int_err; |
| 2696 | bc_num_ulong2num(&temp, v); |
| 2697 | s = zbc_num_add(&mult, &temp, n, 0); |
| 2698 | if (s) goto int_err; |
| 2699 | } |
| 2700 | |
| 2701 | bc_num_init(&result, base.len); |
| 2702 | //bc_num_zero(&result); - already is |
| 2703 | bc_num_one(&mult); |
| 2704 | |
| 2705 | digits = 0; |
| 2706 | for (;;) { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2707 | unsigned v; |
Denys Vlasenko | 8797adc | 2018-12-31 19:50:06 +0100 | [diff] [blame] | 2708 | char c; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2709 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2710 | c = *val++; |
| 2711 | if (c == '\0') break; |
| 2712 | digits++; |
| 2713 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2714 | v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10); |
| 2715 | if (v > base_t) v = base_t; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2716 | |
| 2717 | s = zbc_num_mul(&result, &base, &result, 0); |
| 2718 | if (s) goto err; |
| 2719 | bc_num_ulong2num(&temp, v); |
| 2720 | s = zbc_num_add(&result, &temp, &result, 0); |
| 2721 | if (s) goto err; |
| 2722 | s = zbc_num_mul(&mult, &base, &mult, 0); |
| 2723 | if (s) goto err; |
| 2724 | } |
| 2725 | |
| 2726 | s = zbc_num_div(&result, &mult, &result, digits); |
| 2727 | if (s) goto err; |
| 2728 | s = zbc_num_add(n, &result, n, digits); |
| 2729 | if (s) goto err; |
| 2730 | |
| 2731 | if (n->len != 0) { |
| 2732 | if (n->rdx < digits) |
| 2733 | bc_num_extend(n, digits - n->rdx); |
| 2734 | } else |
| 2735 | bc_num_zero(n); |
| 2736 | err: |
| 2737 | bc_num_free(&result); |
| 2738 | int_err: |
| 2739 | bc_num_free(&mult); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2740 | } |
| 2741 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2742 | 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] | 2743 | { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2744 | size_t i; |
| 2745 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2746 | if (!xc_num_strValid(val)) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2747 | RETURN_STATUS(bc_error("bad number string")); |
| 2748 | |
| 2749 | bc_num_zero(n); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2750 | while (*val == '0') |
| 2751 | val++; |
| 2752 | for (i = 0; ; ++i) { |
| 2753 | if (val[i] == '\0') |
| 2754 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2755 | if (val[i] != '.' && val[i] != '0') |
| 2756 | break; |
| 2757 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2758 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2759 | if (base_t == 10 || val[1] == '\0') |
| 2760 | // Decimal, or single-digit number |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2761 | bc_num_parseDecimal(n, val); |
| 2762 | else |
| 2763 | bc_num_parseBase(n, val, base_t); |
| 2764 | |
| 2765 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2766 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2767 | #define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2768 | |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2769 | // p->lex_inbuf points to the current string to be parsed. |
| 2770 | // if p->lex_inbuf points to '\0', it's either EOF or it points after |
| 2771 | // last processed line's terminating '\n' (and more reading needs to be done |
| 2772 | // to get next character). |
| 2773 | // |
| 2774 | // If you are in a situation where that is a possibility, call peek_inbuf(). |
| 2775 | // If necessary, it performs more reading and changes p->lex_inbuf, |
| 2776 | // then it returns *p->lex_inbuf (which will be '\0' only if it's EOF). |
| 2777 | // After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0', |
| 2778 | // it's ok to do p->lex_inbuf++ once without end-of-buffer checking. |
| 2779 | // |
| 2780 | // eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++": |
| 2781 | // it returns current char and advances the pointer (if not EOF). |
| 2782 | // After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid. |
| 2783 | // |
| 2784 | // In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf(): |
| 2785 | // unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY |
| 2786 | // on real EOF, not end-of-buffer. |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2787 | // |
| 2788 | // bc cases to test interactively: |
| 2789 | // 1 #comment\ - prints "1<newline>" at once (comment is not continued) |
| 2790 | // 1 #comment/* - prints "1<newline>" at once |
| 2791 | // 1 #comment" - prints "1<newline>" at once |
| 2792 | // 1\#comment - error at once (\ is not a line continuation) |
| 2793 | // 1 + /*"*/2 - prints "3<newline>" at once |
| 2794 | // 1 + /*#*/2 - prints "3<newline>" at once |
| 2795 | // "str\" - prints "str\" at once |
| 2796 | // "str#" - prints "str#" at once |
| 2797 | // "str/*" - prints "str/*" at once |
| 2798 | // "str#\ - waits for second line |
| 2799 | // end" - ...prints "str#\<newline>end" |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2800 | static char peek_inbuf(void) |
| 2801 | { |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 2802 | if (*G.prs.lex_inbuf == '\0' |
| 2803 | && G.prs.lex_input_fp |
| 2804 | ) { |
| 2805 | xc_read_line(&G.input_buffer, G.prs.lex_input_fp); |
| 2806 | G.prs.lex_inbuf = G.input_buffer.v; |
| 2807 | if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte) |
| 2808 | G.prs.lex_input_fp = NULL; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2809 | } |
| 2810 | return *G.prs.lex_inbuf; |
| 2811 | } |
| 2812 | static char eat_inbuf(void) |
| 2813 | { |
| 2814 | char c = peek_inbuf(); |
| 2815 | if (c) G.prs.lex_inbuf++; |
| 2816 | return c; |
| 2817 | } |
| 2818 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2819 | static void xc_lex_lineComment(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2820 | { |
| 2821 | BcParse *p = &G.prs; |
| 2822 | char c; |
| 2823 | |
| 2824 | // Try: echo -n '#foo' | bc |
| 2825 | p->lex = XC_LEX_WHITESPACE; |
| 2826 | |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 2827 | // Not peek_inbuf(): we depend on input being done in whole lines: |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2828 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2829 | while ((c = *p->lex_inbuf) != '\n' && c != '\0') |
| 2830 | p->lex_inbuf++; |
| 2831 | } |
| 2832 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2833 | static void xc_lex_whitespace(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2834 | { |
| 2835 | BcParse *p = &G.prs; |
| 2836 | |
| 2837 | p->lex = XC_LEX_WHITESPACE; |
| 2838 | for (;;) { |
| 2839 | // We depend here on input being done in whole lines: |
| 2840 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2841 | char c = *p->lex_inbuf; |
| 2842 | if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
| 2843 | break; |
| 2844 | if (!isspace(c)) |
| 2845 | break; |
| 2846 | p->lex_inbuf++; |
| 2847 | } |
| 2848 | } |
| 2849 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2850 | static BC_STATUS zxc_lex_number(char last) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2851 | { |
| 2852 | BcParse *p = &G.prs; |
| 2853 | bool pt; |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2854 | char last_valid_ch; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2855 | |
| 2856 | bc_vec_pop_all(&p->lex_strnumbuf); |
| 2857 | bc_vec_pushByte(&p->lex_strnumbuf, last); |
| 2858 | |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2859 | // bc: "Input numbers may contain the characters 0-9 and A-Z. |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2860 | // (Note: They must be capitals. Lower case letters are variable names.) |
| 2861 | // Single digit numbers always have the value of the digit regardless of |
| 2862 | // the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes |
| 2863 | // all input digits greater or equal to ibase to the value of ibase-1. |
| 2864 | // This makes the number ZZZ always be the largest 3 digit number of the |
| 2865 | // input base." |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2866 | // dc only allows A-F, the rules about single-char and multi-char are the same. |
| 2867 | last_valid_ch = (IS_BC ? 'Z' : 'F'); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2868 | pt = (last == '.'); |
| 2869 | p->lex = XC_LEX_NUMBER; |
| 2870 | for (;;) { |
| 2871 | // We depend here on input being done in whole lines: |
| 2872 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2873 | char c = *p->lex_inbuf; |
| 2874 | check_c: |
| 2875 | if (c == '\0') |
| 2876 | break; |
| 2877 | if (c == '\\' && p->lex_inbuf[1] == '\n') { |
| 2878 | p->lex_inbuf += 2; |
| 2879 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 2880 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2881 | c = peek_inbuf(); // force next line to be read |
| 2882 | goto check_c; |
| 2883 | } |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2884 | if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2885 | if (c != '.') break; |
| 2886 | // if '.' was already seen, stop on second one: |
| 2887 | if (pt) break; |
| 2888 | pt = true; |
| 2889 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2890 | // c is one of "0-9A-Z." |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2891 | last = c; |
| 2892 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 2893 | p->lex_inbuf++; |
| 2894 | } |
| 2895 | if (last == '.') // remove trailing '.' if any |
| 2896 | bc_vec_pop(&p->lex_strnumbuf); |
| 2897 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 2898 | |
| 2899 | G.err_line = G.prs.lex_line; |
| 2900 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2901 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2902 | #define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2903 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2904 | static void xc_lex_name(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2905 | { |
| 2906 | BcParse *p = &G.prs; |
| 2907 | size_t i; |
| 2908 | const char *buf; |
| 2909 | |
| 2910 | p->lex = XC_LEX_NAME; |
| 2911 | |
| 2912 | // Since names can't cross lines with \<newline>, |
| 2913 | // we depend on the fact that whole line is in the buffer |
| 2914 | i = 0; |
| 2915 | buf = p->lex_inbuf - 1; |
| 2916 | for (;;) { |
| 2917 | char c = buf[i]; |
| 2918 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 2919 | i++; |
| 2920 | } |
| 2921 | |
| 2922 | #if 0 // We do not protect against people with gigabyte-long names |
| 2923 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2924 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2925 | if (i > BC_MAX_STRING) |
| 2926 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2927 | } |
| 2928 | #endif |
| 2929 | bc_vec_string(&p->lex_strnumbuf, i, buf); |
| 2930 | |
| 2931 | // Increment the index. We minus 1 because it has already been incremented. |
| 2932 | p->lex_inbuf += i - 1; |
| 2933 | |
| 2934 | //return BC_STATUS_SUCCESS; |
| 2935 | } |
| 2936 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2937 | IF_BC(static BC_STATUS zbc_lex_token(void);) |
| 2938 | IF_DC(static BC_STATUS zdc_lex_token(void);) |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 2939 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
| 2940 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2941 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2942 | static BC_STATUS zxc_lex_next(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2943 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2944 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2945 | BcStatus s; |
| 2946 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 2947 | G.err_line = p->lex_line; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2948 | p->lex_last = p->lex; |
Denys Vlasenko | 2f7352b | 2018-12-26 18:59:42 +0100 | [diff] [blame] | 2949 | //why? |
| 2950 | // if (p->lex_last == XC_LEX_EOF) |
| 2951 | // RETURN_STATUS(bc_error("end of file")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2952 | |
| 2953 | // Loop until failure or we don't have whitespace. This |
| 2954 | // is so the parser doesn't get inundated with whitespace. |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 2955 | // Comments are also XC_LEX_WHITESPACE tokens and eaten here. |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2956 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2957 | do { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2958 | if (*p->lex_inbuf == '\0') { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2959 | p->lex = XC_LEX_EOF; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2960 | if (peek_inbuf() == '\0') |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2961 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2962 | } |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 2963 | p->lex_next_at = p->lex_inbuf; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 2964 | dbg_lex("next string to parse:'%.*s'", |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2965 | (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at), |
| 2966 | p->lex_next_at |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 2967 | ); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2968 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2969 | IF_BC(s = zbc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2970 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2971 | IF_DC(s = zdc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2972 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2973 | } while (!s && p->lex == XC_LEX_WHITESPACE); |
| 2974 | dbg_lex("p->lex from string:%d", p->lex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2975 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2976 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2977 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2978 | #define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2979 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 2980 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2981 | static BC_STATUS zbc_lex_skip_if_at_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2982 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2983 | if (G.prs.lex == XC_LEX_NLINE) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2984 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2985 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2986 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2987 | #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] | 2988 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2989 | static BC_STATUS zbc_lex_next_and_skip_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2990 | { |
| 2991 | BcStatus s; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2992 | s = zxc_lex_next(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2993 | if (s) RETURN_STATUS(s); |
| 2994 | // if(cond)<newline>stmt is accepted too (but not 2+ newlines) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2995 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2996 | RETURN_STATUS(s); |
| 2997 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2998 | #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] | 2999 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3000 | static BC_STATUS zbc_lex_identifier(void) |
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 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3003 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3004 | unsigned i; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3005 | const char *buf = p->lex_inbuf - 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3006 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3007 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 3008 | const char *keyword8 = bc_lex_kws[i].name8; |
| 3009 | unsigned j = 0; |
| 3010 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 3011 | j++; |
| 3012 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3013 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3014 | if (keyword8[j] != '\0') |
| 3015 | continue; |
| 3016 | match: |
| 3017 | // buf starts with keyword bc_lex_kws[i] |
Denys Vlasenko | 5acd14b | 2018-12-20 16:48:50 +0100 | [diff] [blame] | 3018 | if (isalnum(buf[j]) || buf[j]=='_') |
| 3019 | continue; // "ifz" does not match "if" keyword, "if." does |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3020 | p->lex = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 3021 | if (!keyword_is_POSIX(i)) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3022 | s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8); |
| 3023 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | // We minus 1 because the index has already been incremented. |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3027 | p->lex_inbuf += j - 1; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3028 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3029 | } |
| 3030 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3031 | xc_lex_name(); |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 3032 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3033 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3034 | if (p->lex_strnumbuf.len > 2) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3035 | // Prevent this: |
| 3036 | // >>> qwe=1 |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3037 | // bc: POSIX only allows one character names; this is bad: 'qwe=1 |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3038 | // ' |
| 3039 | unsigned len = strchrnul(buf, '\n') - buf; |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3040 | 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] | 3041 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3042 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3043 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3044 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3045 | #define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3046 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3047 | static BC_STATUS zbc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3048 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3049 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3050 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3051 | p->lex = XC_LEX_STR; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3052 | bc_vec_pop_all(&p->lex_strnumbuf); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3053 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3054 | char c = peek_inbuf(); // strings can cross lines |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3055 | if (c == '\0') { |
Denys Vlasenko | 8af1108 | 2018-12-26 21:01:41 +0100 | [diff] [blame] | 3056 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3057 | } |
| 3058 | if (c == '"') |
| 3059 | break; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3060 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3061 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3062 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3063 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3064 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 3065 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3066 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3067 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 3068 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3069 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3070 | G.err_line = p->lex_line; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3071 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3072 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3073 | #define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3074 | |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3075 | static void parse_lex_by_checking_eq_sign(unsigned with_and_without) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3076 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3077 | BcParse *p = &G.prs; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3078 | if (*p->lex_inbuf == '=') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3079 | // ^^^ not using peek_inbuf() since '==' etc can't be split across lines |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3080 | p->lex_inbuf++; |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3081 | with_and_without >>= 8; // store "with" value |
| 3082 | } // else store "without" value |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3083 | p->lex = (with_and_without & 0xff); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3084 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3085 | #define parse_lex_by_checking_eq_sign(with, without) \ |
| 3086 | parse_lex_by_checking_eq_sign(((with)<<8)|(without)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3087 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3088 | static BC_STATUS zbc_lex_comment(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3089 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3090 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3091 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3092 | p->lex = XC_LEX_WHITESPACE; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3093 | // here lex_inbuf is at '*' of opening comment delimiter |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3094 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3095 | char c; |
| 3096 | |
| 3097 | p->lex_inbuf++; |
| 3098 | c = peek_inbuf(); |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3099 | check_star: |
| 3100 | if (c == '*') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3101 | p->lex_inbuf++; |
Denys Vlasenko | 8af1108 | 2018-12-26 21:01:41 +0100 | [diff] [blame] | 3102 | c = *p->lex_inbuf; // no need to peek_inbuf() |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3103 | if (c == '/') |
| 3104 | break; |
| 3105 | goto check_star; |
| 3106 | } |
| 3107 | if (c == '\0') { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3108 | RETURN_STATUS(bc_error("unterminated comment")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3109 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3110 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3111 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3112 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3113 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3114 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3115 | p->lex_inbuf++; // skip trailing '/' |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3116 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3117 | G.err_line = p->lex_line; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3118 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3119 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3120 | #define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3121 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3122 | #undef zbc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3123 | static BC_STATUS zbc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3124 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3125 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3126 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3127 | char c = eat_inbuf(); |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3128 | char c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3129 | |
| 3130 | // This is the workhorse of the lexer. |
| 3131 | switch (c) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3132 | // case '\0': // probably never reached |
| 3133 | // p->lex_inbuf--; |
| 3134 | // p->lex = XC_LEX_EOF; |
| 3135 | // break; |
| 3136 | case '\n': |
| 3137 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3138 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3139 | p->lex = XC_LEX_NLINE; |
| 3140 | break; |
| 3141 | case '\t': |
| 3142 | case '\v': |
| 3143 | case '\f': |
| 3144 | case '\r': |
| 3145 | case ' ': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3146 | xc_lex_whitespace(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3147 | break; |
| 3148 | case '!': |
| 3149 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3150 | if (p->lex == BC_LEX_OP_BOOL_NOT) { |
| 3151 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3152 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3153 | } |
| 3154 | break; |
| 3155 | case '"': |
| 3156 | s = zbc_lex_string(); |
| 3157 | break; |
| 3158 | case '#': |
| 3159 | s = zbc_POSIX_does_not_allow("'#' script comments"); |
| 3160 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3161 | xc_lex_lineComment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3162 | break; |
| 3163 | case '%': |
| 3164 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS); |
| 3165 | break; |
| 3166 | case '&': |
| 3167 | c2 = *p->lex_inbuf; |
| 3168 | if (c2 == '&') { |
| 3169 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
| 3170 | if (s) RETURN_STATUS(s); |
| 3171 | p->lex_inbuf++; |
| 3172 | p->lex = BC_LEX_OP_BOOL_AND; |
| 3173 | } else { |
| 3174 | p->lex = XC_LEX_INVALID; |
| 3175 | s = bc_error_bad_character('&'); |
| 3176 | } |
| 3177 | break; |
| 3178 | case '(': |
| 3179 | case ')': |
| 3180 | p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3181 | break; |
| 3182 | case '*': |
| 3183 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY); |
| 3184 | break; |
| 3185 | case '+': |
| 3186 | c2 = *p->lex_inbuf; |
| 3187 | if (c2 == '+') { |
| 3188 | p->lex_inbuf++; |
| 3189 | p->lex = BC_LEX_OP_INC; |
| 3190 | } else |
| 3191 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS); |
| 3192 | break; |
| 3193 | case ',': |
| 3194 | p->lex = BC_LEX_COMMA; |
| 3195 | break; |
| 3196 | case '-': |
| 3197 | c2 = *p->lex_inbuf; |
| 3198 | if (c2 == '-') { |
| 3199 | p->lex_inbuf++; |
| 3200 | p->lex = BC_LEX_OP_DEC; |
| 3201 | } else |
| 3202 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS); |
| 3203 | break; |
| 3204 | case '.': |
| 3205 | if (isdigit(*p->lex_inbuf)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3206 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3207 | else { |
| 3208 | p->lex = BC_LEX_KEY_LAST; |
| 3209 | s = zbc_POSIX_does_not_allow("'.' as 'last'"); |
| 3210 | } |
| 3211 | break; |
| 3212 | case '/': |
| 3213 | c2 = *p->lex_inbuf; |
| 3214 | if (c2 == '*') |
| 3215 | s = zbc_lex_comment(); |
| 3216 | else |
| 3217 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE); |
| 3218 | break; |
| 3219 | case '0': |
| 3220 | case '1': |
| 3221 | case '2': |
| 3222 | case '3': |
| 3223 | case '4': |
| 3224 | case '5': |
| 3225 | case '6': |
| 3226 | case '7': |
| 3227 | case '8': |
| 3228 | case '9': |
| 3229 | case 'A': |
| 3230 | case 'B': |
| 3231 | case 'C': |
| 3232 | case 'D': |
| 3233 | case 'E': |
| 3234 | case 'F': |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3235 | case 'G': |
| 3236 | case 'H': |
| 3237 | case 'I': |
| 3238 | case 'J': |
| 3239 | case 'K': |
| 3240 | case 'L': |
| 3241 | case 'M': |
| 3242 | case 'N': |
| 3243 | case 'O': |
| 3244 | case 'P': |
| 3245 | case 'Q': |
| 3246 | case 'R': |
| 3247 | case 'S': |
| 3248 | case 'T': |
| 3249 | case 'U': |
| 3250 | case 'V': |
| 3251 | case 'W': |
| 3252 | case 'X': |
| 3253 | case 'Y': |
| 3254 | case 'Z': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3255 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3256 | break; |
| 3257 | case ';': |
| 3258 | p->lex = BC_LEX_SCOLON; |
| 3259 | break; |
| 3260 | case '<': |
| 3261 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT); |
| 3262 | break; |
| 3263 | case '=': |
| 3264 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3265 | break; |
| 3266 | case '>': |
| 3267 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT); |
| 3268 | break; |
| 3269 | case '[': |
| 3270 | case ']': |
| 3271 | p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3272 | break; |
| 3273 | case '\\': |
| 3274 | if (*p->lex_inbuf == '\n') { |
| 3275 | p->lex = XC_LEX_WHITESPACE; |
| 3276 | p->lex_inbuf++; |
| 3277 | } else |
| 3278 | s = bc_error_bad_character(c); |
| 3279 | break; |
| 3280 | case '^': |
| 3281 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER); |
| 3282 | break; |
| 3283 | case 'a': |
| 3284 | case 'b': |
| 3285 | case 'c': |
| 3286 | case 'd': |
| 3287 | case 'e': |
| 3288 | case 'f': |
| 3289 | case 'g': |
| 3290 | case 'h': |
| 3291 | case 'i': |
| 3292 | case 'j': |
| 3293 | case 'k': |
| 3294 | case 'l': |
| 3295 | case 'm': |
| 3296 | case 'n': |
| 3297 | case 'o': |
| 3298 | case 'p': |
| 3299 | case 'q': |
| 3300 | case 'r': |
| 3301 | case 's': |
| 3302 | case 't': |
| 3303 | case 'u': |
| 3304 | case 'v': |
| 3305 | case 'w': |
| 3306 | case 'x': |
| 3307 | case 'y': |
| 3308 | case 'z': |
| 3309 | s = zbc_lex_identifier(); |
| 3310 | break; |
| 3311 | case '{': |
| 3312 | case '}': |
| 3313 | p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3314 | break; |
| 3315 | case '|': |
| 3316 | c2 = *p->lex_inbuf; |
| 3317 | if (c2 == '|') { |
| 3318 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
| 3319 | if (s) RETURN_STATUS(s); |
| 3320 | p->lex_inbuf++; |
| 3321 | p->lex = BC_LEX_OP_BOOL_OR; |
| 3322 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3323 | p->lex = XC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3324 | s = bc_error_bad_character(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3325 | } |
| 3326 | break; |
| 3327 | default: |
| 3328 | p->lex = XC_LEX_INVALID; |
| 3329 | s = bc_error_bad_character(c); |
| 3330 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3331 | } |
| 3332 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3333 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3334 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3335 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3336 | #endif // ENABLE_BC |
| 3337 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3338 | #if ENABLE_DC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3339 | static BC_STATUS zdc_lex_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3340 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3341 | BcParse *p = &G.prs; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3342 | if (G_exreg && isspace(*p->lex_inbuf)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3343 | xc_lex_whitespace(); // eats whitespace (but not newline) |
| 3344 | p->lex_inbuf++; // xc_lex_name() expects this |
| 3345 | xc_lex_name(); |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3346 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3347 | bc_vec_pop_all(&p->lex_strnumbuf); |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3348 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3349 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 3350 | p->lex = XC_LEX_NAME; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3351 | } |
| 3352 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3353 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3354 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3355 | #define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3356 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3357 | static BC_STATUS zdc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3358 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3359 | BcParse *p = &G.prs; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3360 | size_t depth; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3361 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3362 | p->lex = XC_LEX_STR; |
| 3363 | bc_vec_pop_all(&p->lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3364 | |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3365 | depth = 1; |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3366 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3367 | char c = peek_inbuf(); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3368 | if (c == '\0') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3369 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3370 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3371 | if (c == '[') depth++; |
| 3372 | if (c == ']') |
| 3373 | if (--depth == 0) |
| 3374 | break; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3375 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3376 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3377 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3378 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3379 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 3380 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3381 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3382 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3383 | p->lex_inbuf++; // skip trailing ']' |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3384 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3385 | G.err_line = p->lex_line; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3386 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3387 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3388 | #define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3389 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3390 | #undef zdc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3391 | static BC_STATUS zdc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3392 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3393 | static const //BcLexType - should be this type, but narrower type saves size: |
| 3394 | uint8_t |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 3395 | dc_lex_regs[] ALIGN1 = { |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3396 | 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] | 3397 | XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON, |
| 3398 | 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] | 3399 | DC_LEX_STORE_PUSH, |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3400 | }; |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3401 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3402 | BcParse *p = &G.prs; |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3403 | BcStatus s; |
| 3404 | char c, c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3405 | size_t i; |
| 3406 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3407 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3408 | if (p->lex_last == dc_lex_regs[i]) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3409 | RETURN_STATUS(zdc_lex_register()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3410 | } |
| 3411 | |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3412 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3413 | c = eat_inbuf(); |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3414 | if (c >= '%' && c <= '~' |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3415 | && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3416 | ) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3417 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | // This is the workhorse of the lexer. |
| 3421 | switch (c) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3422 | // case '\0': // probably never reached |
| 3423 | // p->lex = XC_LEX_EOF; |
| 3424 | // break; |
| 3425 | case '\n': |
| 3426 | // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
| 3427 | // (and "case '\n':" is not just empty here) |
| 3428 | // only to allow interactive dc have a way to exit |
| 3429 | // "parse" stage of "parse,execute" loop |
| 3430 | // on <enter>, not on _next_ token (which would mean |
| 3431 | // commands are not executed on pressing <enter>). |
| 3432 | // IOW: typing "1p<enter>" should print "1" _at once_, |
| 3433 | // not after some more input. |
| 3434 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3435 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3436 | p->lex = XC_LEX_NLINE; |
| 3437 | break; |
| 3438 | case '\t': |
| 3439 | case '\v': |
| 3440 | case '\f': |
| 3441 | case '\r': |
| 3442 | case ' ': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3443 | xc_lex_whitespace(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3444 | break; |
| 3445 | case '!': |
| 3446 | c2 = *p->lex_inbuf; |
| 3447 | if (c2 == '=') |
| 3448 | p->lex = XC_LEX_OP_REL_NE; |
| 3449 | else if (c2 == '<') |
| 3450 | p->lex = XC_LEX_OP_REL_LE; |
| 3451 | else if (c2 == '>') |
| 3452 | p->lex = XC_LEX_OP_REL_GE; |
| 3453 | else |
| 3454 | RETURN_STATUS(bc_error_bad_character(c)); |
| 3455 | p->lex_inbuf++; |
| 3456 | break; |
| 3457 | case '#': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3458 | xc_lex_lineComment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3459 | break; |
| 3460 | case '.': |
| 3461 | if (isdigit(*p->lex_inbuf)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3462 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3463 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3464 | s = bc_error_bad_character(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3465 | break; |
| 3466 | case '0': |
| 3467 | case '1': |
| 3468 | case '2': |
| 3469 | case '3': |
| 3470 | case '4': |
| 3471 | case '5': |
| 3472 | case '6': |
| 3473 | case '7': |
| 3474 | case '8': |
| 3475 | case '9': |
| 3476 | case 'A': |
| 3477 | case 'B': |
| 3478 | case 'C': |
| 3479 | case 'D': |
| 3480 | case 'E': |
| 3481 | case 'F': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3482 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3483 | break; |
| 3484 | case '[': |
| 3485 | s = zdc_lex_string(); |
| 3486 | break; |
| 3487 | default: |
| 3488 | p->lex = XC_LEX_INVALID; |
| 3489 | s = bc_error_bad_character(c); |
| 3490 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3491 | } |
| 3492 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3493 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3494 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3495 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3496 | #endif // ENABLE_DC |
| 3497 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3498 | static void xc_parse_push(char i) |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3499 | { |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3500 | BcVec *code = &G.prs.func->code; |
| 3501 | dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i); |
| 3502 | bc_vec_pushByte(code, i); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3503 | } |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3504 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3505 | static void xc_parse_pushName(char *name) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3506 | { |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3507 | #if 1 |
| 3508 | BcVec *code = &G.prs.func->code; |
| 3509 | size_t pos = code->len; |
| 3510 | size_t len = strlen(name) + 1; |
| 3511 | |
| 3512 | bc_vec_expand(code, pos + len); |
| 3513 | strcpy(code->v + pos, name); |
| 3514 | code->len = pos + len; |
| 3515 | #else |
| 3516 | // Smaller code, but way slow: |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3517 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3518 | xc_parse_push(*name); |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3519 | } while (*name++); |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3520 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3521 | } |
| 3522 | |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3523 | // Indexes < 0xfc are encoded verbatim, else first byte is |
| 3524 | // 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes", |
| 3525 | // followed by that many bytes, lsb first. |
| 3526 | // (The above describes 32-bit case). |
| 3527 | #define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t)) |
| 3528 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3529 | static void xc_parse_pushIndex(size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3530 | { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3531 | size_t mask; |
| 3532 | unsigned amt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3533 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 3534 | dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx); |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3535 | if (idx < SMALL_INDEX_LIMIT) { |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 3536 | xc_parse_push(idx); |
| 3537 | return; |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3538 | } |
| 3539 | |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3540 | mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8); |
| 3541 | amt = sizeof(idx); |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3542 | for (;;) { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3543 | if (idx & mask) break; |
| 3544 | mask >>= 8; |
| 3545 | amt--; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3546 | } |
| 3547 | // amt is at least 1 here - "one byte of length data follows" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3548 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3549 | xc_parse_push((SMALL_INDEX_LIMIT - 1) + amt); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3550 | |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 3551 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3552 | xc_parse_push((unsigned char)idx); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3553 | idx >>= 8; |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 3554 | } while (idx != 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3555 | } |
| 3556 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3557 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3558 | static void bc_parse_pushJUMP(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3559 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3560 | xc_parse_push(BC_INST_JUMP); |
| 3561 | xc_parse_pushIndex(idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3562 | } |
| 3563 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3564 | static void bc_parse_pushJUMP_ZERO(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3565 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3566 | xc_parse_push(BC_INST_JUMP_ZERO); |
| 3567 | xc_parse_pushIndex(idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3568 | } |
| 3569 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3570 | static BC_STATUS zbc_parse_pushSTR(void) |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3571 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3572 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3573 | char *str = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3574 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3575 | xc_parse_push(XC_INST_STR); |
| 3576 | xc_parse_pushIndex(p->func->strs.len); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3577 | bc_vec_push(&p->func->strs, &str); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3578 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3579 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3580 | } |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 3581 | #define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3582 | #endif |
| 3583 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3584 | static void xc_parse_pushNUM(void) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3585 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3586 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3587 | char *num = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3588 | #if ENABLE_BC && ENABLE_DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3589 | 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] | 3590 | #elif ENABLE_BC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3591 | size_t idx = bc_vec_push(&p->func->consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3592 | #else // DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3593 | size_t idx = bc_vec_push(&G.prog.consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3594 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3595 | xc_parse_push(XC_INST_NUM); |
| 3596 | xc_parse_pushIndex(idx); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3597 | } |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3598 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3599 | static BC_STATUS zxc_parse_text_init(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3600 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3601 | G.prs.func = xc_program_func(G.prs.fidx); |
| 3602 | G.prs.lex_inbuf = text; |
| 3603 | G.prs.lex = G.prs.lex_last = XC_LEX_INVALID; |
| 3604 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3605 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3606 | #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] | 3607 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3608 | // Called when parsing or execution detects a failure, |
| 3609 | // resets execution structures. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3610 | static void xc_program_reset(void) |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3611 | { |
| 3612 | BcFunc *f; |
| 3613 | BcInstPtr *ip; |
| 3614 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3615 | bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3616 | bc_vec_pop_all(&G.prog.results); |
| 3617 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3618 | f = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3619 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 3620 | ip->inst_idx = f->code.len; |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3621 | } |
| 3622 | |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3623 | // Called when parsing code detects a failure, |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3624 | // resets parsing structures. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3625 | static void xc_parse_reset(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3626 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3627 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3628 | if (p->fidx != BC_PROG_MAIN) { |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3629 | bc_func_free(p->func); |
| 3630 | bc_func_init(p->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3631 | |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 3632 | p->fidx = BC_PROG_MAIN; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3633 | p->func = xc_program_func_BC_PROG_MAIN(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3634 | } |
| 3635 | |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 3636 | p->lex_inbuf += strlen(p->lex_inbuf); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3637 | p->lex = XC_LEX_EOF; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3638 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3639 | IF_BC(bc_vec_pop_all(&p->exits);) |
| 3640 | IF_BC(bc_vec_pop_all(&p->conds);) |
| 3641 | IF_BC(bc_vec_pop_all(&p->ops);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3642 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3643 | xc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3644 | } |
| 3645 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3646 | static void xc_parse_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3647 | { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3648 | IF_BC(bc_vec_free(&G.prs.exits);) |
| 3649 | IF_BC(bc_vec_free(&G.prs.conds);) |
| 3650 | IF_BC(bc_vec_free(&G.prs.ops);) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3651 | bc_vec_free(&G.prs.lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3652 | } |
| 3653 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3654 | static void xc_parse_create(size_t fidx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3655 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3656 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3657 | memset(p, 0, sizeof(BcParse)); |
| 3658 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3659 | bc_char_vec_init(&p->lex_strnumbuf); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3660 | IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);) |
| 3661 | IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);) |
| 3662 | IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3663 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 3664 | p->fidx = fidx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3665 | p->func = xc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3666 | } |
| 3667 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3668 | static void xc_program_add_fn(void) |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3669 | { |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3670 | //size_t idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3671 | BcFunc f; |
| 3672 | bc_func_init(&f); |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3673 | //idx = |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3674 | bc_vec_push(&G.prog.fns, &f); |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3675 | //return idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | #if ENABLE_BC |
| 3679 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3680 | // Note: takes ownership of 'name' (must be malloced) |
| 3681 | static size_t bc_program_addFunc(char *name) |
| 3682 | { |
| 3683 | size_t idx; |
| 3684 | BcId entry, *entry_ptr; |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3685 | int inserted; |
| 3686 | |
| 3687 | entry.name = name; |
| 3688 | entry.idx = G.prog.fns.len; |
| 3689 | |
| 3690 | inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx); |
| 3691 | if (!inserted) free(name); |
| 3692 | |
| 3693 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
| 3694 | idx = entry_ptr->idx; |
| 3695 | |
| 3696 | if (!inserted) { |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3697 | // There is already a function with this name. |
| 3698 | // It'll be redefined now, clear old definition. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3699 | BcFunc *func = xc_program_func(entry_ptr->idx); |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3700 | bc_func_free(func); |
| 3701 | bc_func_init(func); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3702 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3703 | xc_program_add_fn(); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | return idx; |
| 3707 | } |
| 3708 | |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 3709 | #define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops)) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3710 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3711 | // position of the first operator in the lex enum and adding the position of the |
| 3712 | // first in the expr enum. Note: This only works for binary operators. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3713 | #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] | 3714 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3715 | static BcStatus bc_parse_expr_empty_ok(uint8_t flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3716 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3717 | static BC_STATUS zbc_parse_expr(uint8_t flags) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3718 | { |
| 3719 | BcStatus s; |
| 3720 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3721 | s = bc_parse_expr_empty_ok(flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3722 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
| 3723 | RETURN_STATUS(bc_error("empty expression")); |
| 3724 | RETURN_STATUS(s); |
| 3725 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3726 | #define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3727 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3728 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3729 | #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] | 3730 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3731 | static BC_STATUS zbc_parse_stmt(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3732 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3733 | RETURN_STATUS(zbc_parse_stmt_possibly_auto(false)); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3734 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3735 | #define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3736 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3737 | 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] | 3738 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3739 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3740 | // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines. |
| 3741 | // Same for "else", "while()", "for()". |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3742 | BcStatus s = zbc_lex_next_and_skip_NLINE(); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3743 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3744 | if (p->lex == XC_LEX_NLINE) |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3745 | RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X)); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3746 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3747 | RETURN_STATUS(zbc_parse_stmt()); |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3748 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3749 | #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] | 3750 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3751 | 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] | 3752 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3753 | BcParse *p = &G.prs; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 3754 | char l, r = bc_operation_PREC(type - XC_LEX_1st_op); |
| 3755 | bool left = bc_operation_LEFT(type - XC_LEX_1st_op); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3756 | |
| 3757 | while (p->ops.len > start) { |
Denys Vlasenko | 7b1df3d | 2018-12-14 23:12:48 +0100 | [diff] [blame] | 3758 | BcLexType t = BC_PARSE_TOP_OP(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3759 | if (t == BC_LEX_LPAREN) break; |
| 3760 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 3761 | l = bc_operation_PREC(t - XC_LEX_1st_op); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3762 | if (l >= r && (l != r || !left)) break; |
| 3763 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3764 | xc_parse_push(BC_TOKEN_2_INST(t)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3765 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3766 | *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | bc_vec_push(&p->ops, &type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3770 | } |
| 3771 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3772 | 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] | 3773 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3774 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3775 | BcLexType top; |
| 3776 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3777 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3778 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3779 | top = BC_PARSE_TOP_OP(p); |
| 3780 | |
| 3781 | while (top != BC_LEX_LPAREN) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3782 | xc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3783 | |
| 3784 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3785 | *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3786 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3787 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3788 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3789 | top = BC_PARSE_TOP_OP(p); |
| 3790 | } |
| 3791 | |
| 3792 | bc_vec_pop(&p->ops); |
| 3793 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 3794 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3795 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3796 | #define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3797 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3798 | static BC_STATUS zbc_parse_params(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3799 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3800 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3801 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3802 | size_t nparams; |
| 3803 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3804 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3805 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3806 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3807 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3808 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3809 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3810 | nparams = 0; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3811 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3812 | for (;;) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3813 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3814 | if (s) RETURN_STATUS(s); |
| 3815 | nparams++; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3816 | if (p->lex != BC_LEX_COMMA) { |
| 3817 | if (p->lex == BC_LEX_RPAREN) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3818 | break; |
| 3819 | RETURN_STATUS(bc_error_bad_token()); |
| 3820 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3821 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3822 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3823 | } |
| 3824 | } |
| 3825 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3826 | xc_parse_push(BC_INST_CALL); |
| 3827 | xc_parse_pushIndex(nparams); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3828 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3829 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3830 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3831 | #define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3832 | |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 3833 | // Note: takes ownership of 'name' (must be malloced) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3834 | static BC_STATUS zbc_parse_call(char *name, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3835 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3836 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3837 | BcStatus s; |
| 3838 | BcId entry, *entry_ptr; |
| 3839 | size_t idx; |
| 3840 | |
| 3841 | entry.name = name; |
| 3842 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3843 | s = zbc_parse_params(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3844 | if (s) goto err; |
| 3845 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3846 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3847 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3848 | goto err; |
| 3849 | } |
| 3850 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3851 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3852 | |
| 3853 | if (idx == BC_VEC_INVALID_IDX) { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3854 | // No such function exists, create an empty one |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 3855 | bc_program_addFunc(name); |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3856 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3857 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3858 | free(name); |
| 3859 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3860 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3861 | xc_parse_pushIndex(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3862 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3863 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3864 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3865 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3866 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3867 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3868 | #define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3869 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3870 | static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3871 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3872 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3873 | BcStatus s; |
| 3874 | char *name; |
| 3875 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3876 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3877 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3878 | if (s) goto err; |
| 3879 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3880 | if (p->lex == BC_LEX_LBRACKET) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3881 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3882 | if (s) goto err; |
| 3883 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3884 | if (p->lex == BC_LEX_RBRACKET) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3885 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3886 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3887 | goto err; |
| 3888 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3889 | *type = XC_INST_ARRAY; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3890 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3891 | *type = XC_INST_ARRAY_ELEM; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3892 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3893 | s = zbc_parse_expr(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3894 | if (s) goto err; |
| 3895 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3896 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3897 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3898 | xc_parse_push(*type); |
| 3899 | xc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3900 | free(name); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3901 | } else if (p->lex == BC_LEX_LPAREN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3902 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3903 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3904 | goto err; |
| 3905 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3906 | *type = BC_INST_CALL; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3907 | s = zbc_parse_call(name, flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3908 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3909 | *type = XC_INST_VAR; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3910 | xc_parse_push(XC_INST_VAR); |
| 3911 | xc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3912 | free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3913 | } |
| 3914 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3915 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3916 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3917 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3918 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3919 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3920 | #define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3921 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3922 | static BC_STATUS zbc_parse_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3923 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3924 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3925 | BcStatus s; |
| 3926 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3927 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3928 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3929 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3930 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3931 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3932 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3933 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3934 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3935 | xc_parse_push(XC_INST_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3936 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 3937 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3938 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3939 | #define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3940 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3941 | 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] | 3942 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3943 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3944 | BcStatus s; |
| 3945 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3946 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3947 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3948 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3949 | |
| 3950 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3951 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3952 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3953 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3954 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3955 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3956 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3957 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3958 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3959 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3960 | *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3961 | xc_parse_push(*prev); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3962 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 3963 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3964 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3965 | #define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3966 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3967 | static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3968 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3969 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3970 | BcStatus s; |
| 3971 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3972 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3973 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3974 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3975 | if (p->lex != BC_LEX_LPAREN) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3976 | *type = XC_INST_SCALE; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3977 | xc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3978 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3979 | } |
| 3980 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3981 | *type = XC_INST_SCALE_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3982 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3983 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3984 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3985 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3986 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3987 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3988 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3989 | if (p->lex != BC_LEX_RPAREN) |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3990 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3991 | xc_parse_push(XC_INST_SCALE_FUNC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3992 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3993 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3994 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3995 | #define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3996 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3997 | static BC_STATUS zbc_parse_incdec(BcInst *prev, size_t *nexs, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3998 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3999 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4000 | BcStatus s; |
| 4001 | BcLexType type; |
| 4002 | char inst; |
| 4003 | BcInst etype = *prev; |
| 4004 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4005 | if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM |
| 4006 | || etype == XC_INST_SCALE || etype == BC_INST_LAST |
| 4007 | || etype == XC_INST_IBASE || etype == XC_INST_OBASE |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4008 | ) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4009 | *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4010 | xc_parse_push(inst); |
| 4011 | s = zxc_lex_next(); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4012 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4013 | *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4014 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4015 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4016 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4017 | type = p->lex; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4018 | |
| 4019 | // Because we parse the next part of the expression |
| 4020 | // right here, we need to increment this. |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4021 | *nexs = *nexs + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4022 | |
| 4023 | switch (type) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4024 | case XC_LEX_NAME: |
| 4025 | s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL); |
| 4026 | break; |
| 4027 | case BC_LEX_KEY_IBASE: |
| 4028 | case BC_LEX_KEY_LAST: |
| 4029 | case BC_LEX_KEY_OBASE: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4030 | xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
| 4031 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4032 | break; |
| 4033 | case BC_LEX_KEY_SCALE: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4034 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4035 | if (s) RETURN_STATUS(s); |
| 4036 | if (p->lex == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4037 | s = bc_error_bad_token(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4038 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4039 | xc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4040 | break; |
| 4041 | default: |
| 4042 | s = bc_error_bad_token(); |
| 4043 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4044 | } |
| 4045 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4046 | if (!s) xc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4047 | } |
| 4048 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4049 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4050 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4051 | #define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4052 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4053 | static int bc_parse_inst_isLeaf(BcInst p) |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4054 | { |
| 4055 | return (p >= XC_INST_NUM && p <= XC_INST_SQRT) |
| 4056 | || p == BC_INST_INC_POST |
| 4057 | || p == BC_INST_DEC_POST |
| 4058 | ; |
| 4059 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4060 | #define BC_PARSE_LEAF(prev, bin_last, rparen) \ |
| 4061 | (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev))) |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4062 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4063 | static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn, |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4064 | bool rparen, bool bin_last, size_t *nexprs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4065 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4066 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4067 | BcStatus s; |
| 4068 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4069 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4070 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4071 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4072 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4073 | type = BC_PARSE_LEAF(*prev, bin_last, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG; |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4074 | *prev = BC_TOKEN_2_INST(type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4075 | |
| 4076 | // We can just push onto the op stack because this is the largest |
| 4077 | // precedence operator that gets pushed. Inc/dec does not. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4078 | if (type != XC_LEX_OP_MINUS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4079 | bc_vec_push(&p->ops, &type); |
| 4080 | else |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4081 | bc_parse_operator(type, ops_bgn, nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4082 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4083 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4084 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4085 | #define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4086 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4087 | static BC_STATUS zbc_parse_print(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4088 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4089 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4090 | BcStatus s; |
| 4091 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4092 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4093 | for (;;) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4094 | s = zxc_lex_next(); |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4095 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4096 | type = p->lex; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4097 | if (type == XC_LEX_STR) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4098 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4099 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4100 | s = zbc_parse_expr(0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4101 | } |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4102 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4103 | xc_parse_push(XC_INST_PRINT_POP); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4104 | if (p->lex != BC_LEX_COMMA) |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4105 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4106 | } |
| 4107 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4108 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4109 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4110 | #define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4111 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4112 | static BC_STATUS zbc_parse_return(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4113 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4114 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4115 | BcStatus s; |
| 4116 | BcLexType t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4117 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4118 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4119 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4120 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4121 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4122 | t = p->lex; |
Denys Vlasenko | 96b5ec1 | 2019-01-03 23:34:36 +0100 | [diff] [blame] | 4123 | if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON || t == BC_LEX_RBRACE) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4124 | xc_parse_push(BC_INST_RET0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4125 | else { |
Denys Vlasenko | 96b5ec1 | 2019-01-03 23:34:36 +0100 | [diff] [blame] | 4126 | s = zbc_parse_expr(0); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4127 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4128 | |
Denys Vlasenko | 96b5ec1 | 2019-01-03 23:34:36 +0100 | [diff] [blame] | 4129 | if (t != BC_LEX_LPAREN // "return EXPR", no () |
| 4130 | || p->lex_last != BC_LEX_RPAREN // example: "return (a) + b" |
| 4131 | ) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4132 | s = zbc_POSIX_requires("parentheses around return expressions"); |
| 4133 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4134 | } |
| 4135 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4136 | xc_parse_push(XC_INST_RET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4137 | } |
| 4138 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4139 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4140 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4141 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4142 | #define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4143 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4144 | static void rewrite_label_to_current(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4145 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4146 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4147 | size_t *label = bc_vec_item(&p->func->labels, idx); |
| 4148 | *label = p->func->code.len; |
| 4149 | } |
| 4150 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4151 | static BC_STATUS zbc_parse_if(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4152 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4153 | BcParse *p = &G.prs; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4154 | BcStatus s; |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4155 | size_t ip_idx; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4156 | |
| 4157 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4158 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4159 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4160 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4161 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4162 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4163 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4164 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4165 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4166 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4167 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4168 | // Encode "if zero, jump to ..." |
| 4169 | // Pushed value (destination of the jump) is uninitialized, |
| 4170 | // will be rewritten to be address of "end of if()" or of "else". |
| 4171 | ip_idx = bc_vec_push(&p->func->labels, &ip_idx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4172 | bc_parse_pushJUMP_ZERO(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4173 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4174 | s = zbc_parse_stmt_allow_NLINE_before(STRING_if); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4175 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4176 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4177 | dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex); |
| 4178 | if (p->lex == BC_LEX_KEY_ELSE) { |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4179 | size_t ip2_idx; |
Denys Vlasenko | 7415633 | 2018-12-16 21:21:27 +0100 | [diff] [blame] | 4180 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4181 | // Encode "after then_stmt, jump to end of if()" |
| 4182 | ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx); |
| 4183 | 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] | 4184 | bc_parse_pushJUMP(ip2_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4185 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4186 | 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] | 4187 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4188 | |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4189 | ip_idx = ip2_idx; |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4190 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4191 | s = zbc_parse_stmt_allow_NLINE_before(STRING_else); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4192 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4193 | } |
| 4194 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4195 | 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] | 4196 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4197 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4198 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
| 4199 | RETURN_STATUS(s); |
| 4200 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4201 | #define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4202 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4203 | static BC_STATUS zbc_parse_while(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4204 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4205 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4206 | BcStatus s; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4207 | size_t cond_idx; |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4208 | size_t ip_idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4209 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4210 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4211 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4212 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4213 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4214 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4215 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4216 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4217 | ip_idx = cond_idx + 1; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4218 | bc_vec_push(&p->conds, &cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4219 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4220 | bc_vec_push(&p->exits, &ip_idx); |
| 4221 | bc_vec_push(&p->func->labels, &ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4222 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4223 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4224 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4225 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4226 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4227 | bc_parse_pushJUMP_ZERO(ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4228 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4229 | s = zbc_parse_stmt_allow_NLINE_before(STRING_while); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4230 | if (s) RETURN_STATUS(s); |
| 4231 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4232 | 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] | 4233 | bc_parse_pushJUMP(cond_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4234 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4235 | 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] | 4236 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4237 | |
| 4238 | bc_vec_pop(&p->exits); |
| 4239 | bc_vec_pop(&p->conds); |
| 4240 | |
| 4241 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4242 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4243 | #define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4244 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4245 | static BC_STATUS zbc_parse_for(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4246 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4247 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4248 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4249 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4250 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4251 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4252 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4253 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4254 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4255 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4256 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4257 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4258 | if (p->lex != BC_LEX_SCOLON) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4259 | s = zbc_parse_expr(0); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4260 | xc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4261 | if (s) RETURN_STATUS(s); |
| 4262 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4263 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
| 4264 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4265 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4266 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4267 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4268 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4269 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4270 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4271 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4272 | update_idx = cond_idx + 1; |
| 4273 | body_idx = update_idx + 1; |
| 4274 | exit_idx = body_idx + 1; |
| 4275 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4276 | if (p->lex != BC_LEX_SCOLON) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4277 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4278 | else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4279 | // Set this for the next call to xc_parse_pushNUM(). |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4280 | // This is safe to set because the current token is a semicolon, |
| 4281 | // which has no string requirement. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4282 | bc_vec_string(&p->lex_strnumbuf, 1, "1"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4283 | xc_parse_pushNUM(); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4284 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4285 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4286 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4287 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4288 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4289 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4290 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4291 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4292 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4293 | bc_parse_pushJUMP_ZERO(exit_idx); |
| 4294 | bc_parse_pushJUMP(body_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4295 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4296 | bc_vec_push(&p->conds, &update_idx); |
| 4297 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4298 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4299 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4300 | s = zbc_parse_expr(0); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4301 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4302 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4303 | xc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4304 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4305 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
| 4306 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4307 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4308 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4309 | bc_parse_pushJUMP(cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4310 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4311 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4312 | bc_vec_push(&p->exits, &exit_idx); |
| 4313 | bc_vec_push(&p->func->labels, &exit_idx); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4314 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4315 | s = zbc_parse_stmt_allow_NLINE_before(STRING_for); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4316 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4317 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4318 | 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] | 4319 | bc_parse_pushJUMP(update_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4320 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4321 | 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] | 4322 | rewrite_label_to_current(exit_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4323 | |
| 4324 | bc_vec_pop(&p->exits); |
| 4325 | bc_vec_pop(&p->conds); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4326 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4327 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4328 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4329 | #define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4330 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4331 | static BC_STATUS zbc_parse_break_or_continue(BcLexType type) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4332 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4333 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4334 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4335 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4336 | if (type == BC_LEX_KEY_BREAK) { |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4337 | if (p->exits.len == 0) // none of the enclosing blocks is a loop |
| 4338 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4339 | i = *(size_t*)bc_vec_top(&p->exits); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4340 | } else { |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4341 | i = *(size_t*)bc_vec_top(&p->conds); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4342 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4343 | bc_parse_pushJUMP(i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4344 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4345 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4346 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4347 | #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] | 4348 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4349 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var) |
| 4350 | { |
| 4351 | BcId *autoid; |
| 4352 | BcId a; |
| 4353 | size_t i; |
| 4354 | |
| 4355 | autoid = (void*)f->autos.v; |
| 4356 | for (i = 0; i < f->autos.len; i++, autoid++) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4357 | if (strcmp(name, autoid->name) == 0 |
| 4358 | && var == autoid->idx |
| 4359 | ) { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 4360 | RETURN_STATUS(bc_error("duplicate function parameter or auto name")); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4361 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | a.idx = var; |
| 4365 | a.name = name; |
| 4366 | |
| 4367 | bc_vec_push(&f->autos, &a); |
| 4368 | |
| 4369 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 4370 | } |
| 4371 | #define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS) |
| 4372 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4373 | static BC_STATUS zbc_parse_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4374 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4375 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4376 | BcStatus s; |
| 4377 | bool var, comma = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4378 | char *name; |
| 4379 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4380 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4381 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4382 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4383 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4384 | RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4385 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4386 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4387 | p->fidx = bc_program_addFunc(name); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4388 | p->func = xc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4389 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4390 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4391 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4392 | if (p->lex != BC_LEX_LPAREN) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4393 | RETURN_STATUS(bc_error_bad_function_definition()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4394 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4395 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4396 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4397 | while (p->lex != BC_LEX_RPAREN) { |
| 4398 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4399 | RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4400 | |
| 4401 | ++p->func->nparams; |
| 4402 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4403 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4404 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4405 | if (s) goto err; |
| 4406 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4407 | var = p->lex != BC_LEX_LBRACKET; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4408 | |
| 4409 | if (!var) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4410 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4411 | if (s) goto err; |
| 4412 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4413 | if (p->lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4414 | s = bc_error_bad_function_definition(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4415 | goto err; |
| 4416 | } |
| 4417 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4418 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4419 | if (s) goto err; |
| 4420 | } |
| 4421 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4422 | comma = p->lex == BC_LEX_COMMA; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4423 | if (comma) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4424 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4425 | if (s) goto err; |
| 4426 | } |
| 4427 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4428 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4429 | if (s) goto err; |
| 4430 | } |
| 4431 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4432 | if (comma) RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4433 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4434 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4435 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4436 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4437 | if (p->lex != BC_LEX_LBRACE) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4438 | 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] | 4439 | if (s) RETURN_STATUS(s); |
| 4440 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4441 | |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4442 | // 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] | 4443 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4444 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4445 | //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] | 4446 | if (p->lex != BC_LEX_LBRACE) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4447 | RETURN_STATUS(bc_error("function { body } expected")); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4448 | |
| 4449 | p->in_funcdef++; // to determine whether "return" stmt is allowed, and such |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4450 | s = zbc_parse_stmt_possibly_auto(true); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4451 | p->in_funcdef--; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4452 | if (s) RETURN_STATUS(s); |
| 4453 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4454 | xc_parse_push(BC_INST_RET0); |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 4455 | |
| 4456 | // Subsequent code generation is into main program |
| 4457 | p->fidx = BC_PROG_MAIN; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4458 | p->func = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4459 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4460 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4461 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4462 | err: |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4463 | dbg_lex_done("%s:%d done (error)", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4464 | free(name); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4465 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4466 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4467 | #define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4468 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4469 | static BC_STATUS zbc_parse_auto(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4470 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4471 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4472 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4473 | char *name; |
| 4474 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4475 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4476 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4477 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4478 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4479 | for (;;) { |
| 4480 | bool var; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4481 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4482 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4483 | RETURN_STATUS(bc_error_at("bad 'auto' syntax")); |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4484 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4485 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4486 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4487 | if (s) goto err; |
| 4488 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4489 | var = (p->lex != BC_LEX_LBRACKET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4490 | if (!var) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4491 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4492 | if (s) goto err; |
| 4493 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4494 | if (p->lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4495 | s = bc_error_at("bad 'auto' syntax"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4496 | goto err; |
| 4497 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4498 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4499 | if (s) goto err; |
| 4500 | } |
| 4501 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4502 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4503 | if (s) goto err; |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4504 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4505 | if (p->lex == XC_LEX_NLINE |
| 4506 | || p->lex == BC_LEX_SCOLON |
| 4507 | //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}" |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4508 | ) { |
| 4509 | break; |
| 4510 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4511 | if (p->lex != BC_LEX_COMMA) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4512 | RETURN_STATUS(bc_error_at("bad 'auto' syntax")); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4513 | s = zxc_lex_next(); // skip comma |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4514 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4515 | } |
| 4516 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4517 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4518 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4519 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4520 | free(name); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4521 | dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4522 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4523 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4524 | #define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4525 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4526 | #undef zbc_parse_stmt_possibly_auto |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4527 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4528 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4529 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4530 | BcStatus s = BC_STATUS_SUCCESS; |
| 4531 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4532 | 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] | 4533 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4534 | if (p->lex == XC_LEX_NLINE) { |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4535 | dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4536 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4537 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4538 | if (p->lex == BC_LEX_SCOLON) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4539 | dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4540 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4541 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4542 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4543 | if (p->lex == BC_LEX_LBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4544 | dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed); |
| 4545 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4546 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4547 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4548 | } while (p->lex == XC_LEX_NLINE); |
| 4549 | if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4550 | dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4551 | s = zbc_parse_auto(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4552 | if (s) RETURN_STATUS(s); |
| 4553 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4554 | while (p->lex != BC_LEX_RBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4555 | dbg_lex("%s:%d block parsing loop", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4556 | s = zbc_parse_stmt(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4557 | if (s) RETURN_STATUS(s); |
| 4558 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4559 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4560 | dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__); |
| 4561 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4562 | } |
| 4563 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4564 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
| 4565 | switch (p->lex) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4566 | case XC_LEX_OP_MINUS: |
| 4567 | case BC_LEX_OP_INC: |
| 4568 | case BC_LEX_OP_DEC: |
| 4569 | case BC_LEX_OP_BOOL_NOT: |
| 4570 | case BC_LEX_LPAREN: |
| 4571 | case XC_LEX_NAME: |
| 4572 | case XC_LEX_NUMBER: |
| 4573 | case BC_LEX_KEY_IBASE: |
| 4574 | case BC_LEX_KEY_LAST: |
| 4575 | case BC_LEX_KEY_LENGTH: |
| 4576 | case BC_LEX_KEY_OBASE: |
| 4577 | case BC_LEX_KEY_READ: |
| 4578 | case BC_LEX_KEY_SCALE: |
| 4579 | case BC_LEX_KEY_SQRT: |
| 4580 | s = zbc_parse_expr(BC_PARSE_PRINT); |
| 4581 | break; |
| 4582 | case XC_LEX_STR: |
| 4583 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4584 | xc_parse_push(XC_INST_PRINT_STR); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4585 | break; |
| 4586 | case BC_LEX_KEY_BREAK: |
| 4587 | case BC_LEX_KEY_CONTINUE: |
| 4588 | s = zbc_parse_break_or_continue(p->lex); |
| 4589 | break; |
| 4590 | case BC_LEX_KEY_FOR: |
| 4591 | s = zbc_parse_for(); |
| 4592 | break; |
| 4593 | case BC_LEX_KEY_HALT: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4594 | xc_parse_push(BC_INST_HALT); |
| 4595 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4596 | break; |
| 4597 | case BC_LEX_KEY_IF: |
| 4598 | s = zbc_parse_if(); |
| 4599 | break; |
| 4600 | case BC_LEX_KEY_LIMITS: |
| 4601 | // "limits" is a compile-time command, |
| 4602 | // the output is produced at _parse time_. |
| 4603 | printf( |
| 4604 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4605 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4606 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4607 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4608 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4609 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4610 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4611 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4612 | ); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4613 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4614 | break; |
| 4615 | case BC_LEX_KEY_PRINT: |
| 4616 | s = zbc_parse_print(); |
| 4617 | break; |
| 4618 | case BC_LEX_KEY_QUIT: |
| 4619 | // "quit" is a compile-time command. For example, |
| 4620 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4621 | // not when it is executed |
| 4622 | QUIT_OR_RETURN_TO_MAIN; |
| 4623 | case BC_LEX_KEY_RETURN: |
| 4624 | if (!p->in_funcdef) |
| 4625 | RETURN_STATUS(bc_error("'return' not in a function")); |
| 4626 | s = zbc_parse_return(); |
| 4627 | break; |
| 4628 | case BC_LEX_KEY_WHILE: |
| 4629 | s = zbc_parse_while(); |
| 4630 | break; |
| 4631 | default: |
| 4632 | s = bc_error_bad_token(); |
| 4633 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4634 | } |
| 4635 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4636 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4637 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4638 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4639 | #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] | 4640 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4641 | static BC_STATUS zbc_parse_stmt_or_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4642 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4643 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4644 | BcStatus s; |
| 4645 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4646 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4647 | if (p->lex == XC_LEX_EOF) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4648 | s = bc_error("end of file"); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4649 | else if (p->lex == BC_LEX_KEY_DEFINE) { |
| 4650 | dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4651 | s = zbc_parse_funcdef(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4652 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4653 | 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] | 4654 | s = zbc_parse_stmt(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4655 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4656 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4657 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4658 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4659 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4660 | #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] | 4661 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 4662 | // 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] | 4663 | static BcStatus bc_parse_expr_empty_ok(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4664 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4665 | BcParse *p = &G.prs; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4666 | BcInst prev = XC_INST_PRINT; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4667 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4668 | unsigned nparens, nrelops; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4669 | bool paren_first, rprn, assign, bin_last, incdec; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4670 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4671 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4672 | paren_first = (p->lex == BC_LEX_LPAREN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4673 | nparens = nrelops = 0; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4674 | rprn = assign = incdec = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4675 | bin_last = true; |
| 4676 | |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4677 | for (;;) { |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4678 | bool get_token; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4679 | BcStatus s; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4680 | BcLexType t = p->lex; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4681 | |
| 4682 | if (!lex_allowed_in_bc_expr(t)) |
| 4683 | break; |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4684 | |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4685 | dbg_lex("%s:%d t:%d", __func__, __LINE__, t); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4686 | get_token = false; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4687 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4688 | switch (t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4689 | case BC_LEX_OP_INC: |
| 4690 | case BC_LEX_OP_DEC: |
| 4691 | dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4692 | if (incdec) return bc_error_bad_assignment(); |
| 4693 | s = zbc_parse_incdec(&prev, &nexprs, flags); |
| 4694 | incdec = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4695 | rprn = bin_last = false; |
| 4696 | //get_token = false; - already is |
| 4697 | break; |
| 4698 | case XC_LEX_OP_MINUS: |
| 4699 | dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4700 | s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4701 | rprn = false; |
| 4702 | //get_token = false; - already is |
| 4703 | bin_last = (prev == XC_INST_MINUS); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4704 | if (bin_last) incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4705 | break; |
| 4706 | case BC_LEX_OP_ASSIGN_POWER: |
| 4707 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4708 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4709 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4710 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4711 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4712 | case BC_LEX_OP_ASSIGN: |
| 4713 | dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__); |
| 4714 | if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM |
| 4715 | && prev != XC_INST_SCALE && prev != XC_INST_IBASE |
| 4716 | && prev != XC_INST_OBASE && prev != BC_INST_LAST |
| 4717 | ) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4718 | return bc_error_bad_assignment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4719 | } |
| 4720 | // Fallthrough. |
| 4721 | case XC_LEX_OP_POWER: |
| 4722 | case XC_LEX_OP_MULTIPLY: |
| 4723 | case XC_LEX_OP_DIVIDE: |
| 4724 | case XC_LEX_OP_MODULUS: |
| 4725 | case XC_LEX_OP_PLUS: |
| 4726 | case XC_LEX_OP_REL_EQ: |
| 4727 | case XC_LEX_OP_REL_LE: |
| 4728 | case XC_LEX_OP_REL_GE: |
| 4729 | case XC_LEX_OP_REL_NE: |
| 4730 | case XC_LEX_OP_REL_LT: |
| 4731 | case XC_LEX_OP_REL_GT: |
| 4732 | case BC_LEX_OP_BOOL_NOT: |
| 4733 | case BC_LEX_OP_BOOL_OR: |
| 4734 | case BC_LEX_OP_BOOL_AND: |
| 4735 | dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4736 | if (t == BC_LEX_OP_BOOL_NOT) { |
| 4737 | if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT) |
| 4738 | return bc_error_bad_expression(); |
| 4739 | } else if (prev == XC_INST_BOOL_NOT) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4740 | return bc_error_bad_expression(); |
| 4741 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4742 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4743 | nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT); |
| 4744 | prev = BC_TOKEN_2_INST(t); |
| 4745 | bc_parse_operator(t, ops_bgn, &nexprs); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4746 | rprn = incdec = false; |
| 4747 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4748 | bin_last = (t != BC_LEX_OP_BOOL_NOT); |
| 4749 | break; |
| 4750 | case BC_LEX_LPAREN: |
| 4751 | dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4752 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4753 | return bc_error_bad_expression(); |
| 4754 | bc_vec_push(&p->ops, &t); |
| 4755 | nparens++; |
| 4756 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4757 | rprn = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4758 | break; |
| 4759 | case BC_LEX_RPAREN: |
| 4760 | dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4761 | if (p->lex_last == BC_LEX_LPAREN) { |
| 4762 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); |
| 4763 | return BC_STATUS_PARSE_EMPTY_EXP; |
| 4764 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4765 | if (bin_last || prev == XC_INST_BOOL_NOT) |
| 4766 | return bc_error_bad_expression(); |
| 4767 | if (nparens == 0) { |
| 4768 | goto exit_loop; |
| 4769 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4770 | s = zbc_parse_rightParen(ops_bgn, &nexprs); |
| 4771 | nparens--; |
| 4772 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4773 | rprn = true; |
| 4774 | bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4775 | break; |
| 4776 | case XC_LEX_NAME: |
| 4777 | dbg_lex("%s:%d LEX_NAME", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4778 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4779 | return bc_error_bad_expression(); |
| 4780 | s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4781 | rprn = (prev == BC_INST_CALL); |
| 4782 | bin_last = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4783 | //get_token = false; - already is |
| 4784 | nexprs++; |
| 4785 | break; |
| 4786 | case XC_LEX_NUMBER: |
| 4787 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4788 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4789 | return bc_error_bad_expression(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4790 | xc_parse_pushNUM(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4791 | prev = XC_INST_NUM; |
| 4792 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4793 | rprn = bin_last = false; |
| 4794 | nexprs++; |
| 4795 | break; |
| 4796 | case BC_LEX_KEY_IBASE: |
| 4797 | case BC_LEX_KEY_LAST: |
| 4798 | case BC_LEX_KEY_OBASE: |
| 4799 | dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4800 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4801 | return bc_error_bad_expression(); |
| 4802 | prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4803 | xc_parse_push((char) prev); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4804 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4805 | rprn = bin_last = false; |
| 4806 | nexprs++; |
| 4807 | break; |
| 4808 | case BC_LEX_KEY_LENGTH: |
| 4809 | case BC_LEX_KEY_SQRT: |
| 4810 | dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4811 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4812 | return bc_error_bad_expression(); |
| 4813 | s = zbc_parse_builtin(t, flags, &prev); |
| 4814 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4815 | rprn = bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4816 | nexprs++; |
| 4817 | break; |
| 4818 | case BC_LEX_KEY_READ: |
| 4819 | dbg_lex("%s:%d LEX_READ", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4820 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4821 | return bc_error_bad_expression(); |
| 4822 | s = zbc_parse_read(); |
| 4823 | prev = XC_INST_READ; |
| 4824 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4825 | rprn = bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4826 | nexprs++; |
| 4827 | break; |
| 4828 | case BC_LEX_KEY_SCALE: |
| 4829 | dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4830 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4831 | return bc_error_bad_expression(); |
| 4832 | s = zbc_parse_scale(&prev, flags); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4833 | //get_token = false; - already is |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4834 | rprn = bin_last = false; |
| 4835 | nexprs++; |
| 4836 | break; |
| 4837 | default: |
| 4838 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4839 | } |
| 4840 | |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4841 | if (s || G_interrupt) // error, or ^C: stop parsing |
| 4842 | return BC_STATUS_FAILURE; |
| 4843 | if (get_token) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4844 | s = zxc_lex_next(); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4845 | if (s) return s; |
| 4846 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4847 | } |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4848 | exit_loop: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4849 | |
| 4850 | while (p->ops.len > ops_bgn) { |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4851 | BcLexType top = BC_PARSE_TOP_OP(p); |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 4852 | assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4853 | |
| 4854 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4855 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4856 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4857 | xc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4858 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4859 | nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4860 | bc_vec_pop(&p->ops); |
| 4861 | } |
| 4862 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4863 | if (prev == XC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4864 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4865 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4866 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4867 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4868 | s = zbc_POSIX_does_not_allow("comparison operators outside if or loops"); |
| 4869 | if (s) return s; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4870 | } else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4871 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4872 | s = zbc_POSIX_requires("exactly one comparison operator per condition"); |
| 4873 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | if (flags & BC_PARSE_PRINT) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4877 | if (paren_first || !assign) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4878 | xc_parse_push(XC_INST_PRINT); |
| 4879 | xc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4880 | } |
| 4881 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4882 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4883 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4884 | } |
| 4885 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4886 | #endif // ENABLE_BC |
| 4887 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 4888 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 4889 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4890 | static BC_STATUS zdc_parse_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4891 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4892 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4893 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4894 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4895 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4896 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4897 | if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4898 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4899 | xc_parse_pushName(p->lex_strnumbuf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4900 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4901 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4902 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4903 | #define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4904 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4905 | static void dc_parse_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4906 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4907 | BcParse *p = &G.prs; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4908 | char *str; |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4909 | size_t len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4910 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4911 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4912 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4913 | str = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4914 | xc_parse_push(XC_INST_STR); |
| 4915 | xc_parse_pushIndex(len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4916 | bc_vec_push(&G.prog.strs, &str); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4917 | |
| 4918 | // Explanation needed here |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4919 | xc_program_add_fn(); |
| 4920 | p->func = xc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4921 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4922 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4923 | } |
| 4924 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4925 | 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] | 4926 | { |
| 4927 | BcStatus s; |
| 4928 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4929 | xc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4930 | if (name) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4931 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4932 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | if (store) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4936 | xc_parse_push(DC_INST_SWAP); |
| 4937 | xc_parse_push(XC_INST_ASSIGN); |
| 4938 | xc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4939 | } |
| 4940 | |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4941 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4942 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4943 | #define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4944 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4945 | static BC_STATUS zdc_parse_cond(uint8_t inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4946 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4947 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4948 | BcStatus s; |
| 4949 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4950 | xc_parse_push(inst); |
| 4951 | xc_parse_push(DC_INST_EXEC_COND); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4952 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4953 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4954 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4955 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4956 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4957 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4958 | |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4959 | // Note that 'else' part can not be on the next line: |
| 4960 | // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2" |
| 4961 | // 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] | 4962 | if (p->lex == DC_LEX_ELSE) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4963 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4964 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4965 | s = zxc_lex_next(); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4966 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4967 | xc_parse_push('\0'); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4968 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4969 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4970 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4971 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4972 | #define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4973 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4974 | static BC_STATUS zdc_parse_token(BcLexType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4975 | { |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4976 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4977 | uint8_t inst; |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4978 | bool assign, get_token; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4979 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4980 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4981 | s = BC_STATUS_SUCCESS; |
| 4982 | get_token = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4983 | switch (t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4984 | case XC_LEX_OP_REL_EQ: |
| 4985 | case XC_LEX_OP_REL_LE: |
| 4986 | case XC_LEX_OP_REL_GE: |
| 4987 | case XC_LEX_OP_REL_NE: |
| 4988 | case XC_LEX_OP_REL_LT: |
| 4989 | case XC_LEX_OP_REL_GT: |
| 4990 | dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__); |
| 4991 | s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ); |
| 4992 | get_token = false; |
| 4993 | break; |
| 4994 | case DC_LEX_SCOLON: |
| 4995 | case DC_LEX_COLON: |
| 4996 | dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__); |
| 4997 | s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON); |
| 4998 | break; |
| 4999 | case XC_LEX_STR: |
| 5000 | dbg_lex("%s:%d LEX_STR", __func__, __LINE__); |
| 5001 | dc_parse_string(); |
| 5002 | break; |
| 5003 | case XC_LEX_NEG: |
| 5004 | dbg_lex("%s:%d LEX_NEG", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5005 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5006 | if (s) RETURN_STATUS(s); |
| 5007 | if (G.prs.lex != XC_LEX_NUMBER) |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5008 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5009 | xc_parse_pushNUM(); |
| 5010 | xc_parse_push(XC_INST_NEG); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5011 | break; |
| 5012 | case XC_LEX_NUMBER: |
| 5013 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5014 | xc_parse_pushNUM(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5015 | break; |
| 5016 | case DC_LEX_READ: |
| 5017 | dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5018 | xc_parse_push(XC_INST_READ); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5019 | break; |
| 5020 | case DC_LEX_OP_ASSIGN: |
| 5021 | case DC_LEX_STORE_PUSH: |
| 5022 | dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__); |
| 5023 | assign = (t == DC_LEX_OP_ASSIGN); |
| 5024 | inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR; |
| 5025 | s = zdc_parse_mem(inst, true, assign); |
| 5026 | break; |
| 5027 | case DC_LEX_LOAD: |
| 5028 | case DC_LEX_LOAD_POP: |
| 5029 | dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__); |
| 5030 | inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD; |
| 5031 | s = zdc_parse_mem(inst, true, false); |
| 5032 | break; |
| 5033 | case DC_LEX_STORE_IBASE: |
| 5034 | case DC_LEX_STORE_SCALE: |
| 5035 | case DC_LEX_STORE_OBASE: |
| 5036 | dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__); |
| 5037 | inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE; |
| 5038 | s = zdc_parse_mem(inst, false, true); |
| 5039 | break; |
| 5040 | default: |
| 5041 | dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__); |
| 5042 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5043 | } |
| 5044 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5045 | if (!s && get_token) s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5046 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5047 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5048 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5049 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5050 | #define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5051 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5052 | static BC_STATUS zdc_parse_expr(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5053 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5054 | BcParse *p = &G.prs; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5055 | int i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5056 | |
Denys Vlasenko | 6842c60 | 2019-01-04 05:41:47 +0100 | [diff] [blame^] | 5057 | if (p->lex == XC_LEX_NLINE) |
| 5058 | RETURN_STATUS(zxc_lex_next()); |
| 5059 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5060 | i = (int)p->lex - (int)XC_LEX_OP_POWER; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5061 | if (i >= 0) { |
| 5062 | BcInst inst = dc_LEX_to_INST[i]; |
| 5063 | if (inst != DC_INST_INVALID) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5064 | xc_parse_push(inst); |
| 5065 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5066 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5067 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5068 | RETURN_STATUS(zdc_parse_token(p->lex)); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5069 | } |
| 5070 | #define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
| 5071 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5072 | static BC_STATUS zdc_parse_exprs_until_eof(void) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5073 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5074 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5075 | dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex); |
| 5076 | while (p->lex != XC_LEX_EOF) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5077 | BcStatus s = zdc_parse_expr(); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5078 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5079 | } |
| 5080 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5081 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5082 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5083 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5084 | #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] | 5085 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5086 | #endif // ENABLE_DC |
| 5087 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5088 | // |
| 5089 | // Execution engine |
| 5090 | // |
| 5091 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 5092 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 5093 | #define BC_PROG_NUM(r, n) \ |
| 5094 | ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n)) |
| 5095 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5096 | #define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n))) |
| 5097 | #define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n))) |
| 5098 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5099 | static BcVec* xc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5100 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5101 | BcId e, *ptr; |
| 5102 | BcVec *v, *map; |
| 5103 | size_t i; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5104 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5105 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5106 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5107 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5108 | |
| 5109 | e.name = id; |
| 5110 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5111 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5112 | |
| 5113 | if (new) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5114 | BcVec v2; |
| 5115 | bc_array_init(&v2, var); |
| 5116 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | ptr = bc_vec_item(map, i); |
| 5120 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5121 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5122 | } |
| 5123 | |
Denys Vlasenko | 8287b1c | 2018-12-21 22:43:53 +0100 | [diff] [blame] | 5124 | // 'num' need not be initialized on entry |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5125 | static BC_STATUS zxc_program_num(BcResult *r, BcNum **num) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5126 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5127 | switch (r->t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5128 | case XC_RESULT_STR: |
| 5129 | case XC_RESULT_TEMP: |
| 5130 | case XC_RESULT_IBASE: |
| 5131 | case XC_RESULT_SCALE: |
| 5132 | case XC_RESULT_OBASE: |
| 5133 | *num = &r->d.n; |
| 5134 | break; |
| 5135 | case XC_RESULT_CONSTANT: { |
| 5136 | BcStatus s; |
| 5137 | char *str; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5138 | size_t len; |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 5139 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5140 | str = *xc_program_const(r->d.id.idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5141 | len = strlen(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5142 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5143 | bc_num_init(&r->d.n, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5144 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5145 | s = zxc_num_parse(&r->d.n, str, G.prog.ib_t); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5146 | if (s) { |
| 5147 | bc_num_free(&r->d.n); |
| 5148 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5149 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5150 | *num = &r->d.n; |
| 5151 | r->t = XC_RESULT_TEMP; |
| 5152 | break; |
| 5153 | } |
| 5154 | case XC_RESULT_VAR: |
| 5155 | case XC_RESULT_ARRAY: |
| 5156 | case XC_RESULT_ARRAY_ELEM: { |
| 5157 | BcVec *v; |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5158 | void *p; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5159 | v = xc_program_search(r->d.id.name, r->t == XC_RESULT_VAR); |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5160 | // dc variables are all stacks, so here we have this: |
| 5161 | p = bc_vec_top(v); |
| 5162 | // TODO: eliminate these stacks for bc-only config? |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5163 | if (r->t == XC_RESULT_ARRAY_ELEM) { |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5164 | v = p; |
| 5165 | if (v->len <= r->d.id.idx) |
| 5166 | bc_array_expand(v, r->d.id.idx + 1); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5167 | *num = bc_vec_item(v, r->d.id.idx); |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5168 | } else { |
| 5169 | *num = p; |
| 5170 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5171 | break; |
| 5172 | } |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5173 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5174 | case BC_RESULT_LAST: |
| 5175 | *num = &G.prog.last; |
| 5176 | break; |
| 5177 | case BC_RESULT_ONE: |
| 5178 | *num = &G.prog.one; |
| 5179 | break; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5180 | #endif |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5181 | #if SANITY_CHECKS |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5182 | default: |
| 5183 | // Testing the theory that dc does not reach LAST/ONE |
| 5184 | bb_error_msg_and_die("BUG:%d", r->t); |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5185 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5186 | } |
| 5187 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5188 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5189 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5190 | #define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS) |
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 | static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5193 | BcResult **r, BcNum **rn, bool assign) |
| 5194 | { |
| 5195 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5196 | BcResultType lt, rt; |
| 5197 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5198 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5199 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5200 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5201 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5202 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5203 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5204 | s = zxc_program_num(*l, ln); |
| 5205 | if (s) RETURN_STATUS(s); |
| 5206 | s = zxc_program_num(*r, rn); |
| 5207 | if (s) RETURN_STATUS(s); |
| 5208 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5209 | lt = (*l)->t; |
| 5210 | rt = (*r)->t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5211 | |
| 5212 | // We run this again under these conditions in case any vector has been |
| 5213 | // reallocated out from under the BcNums or arrays we had. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5214 | if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5215 | s = zxc_program_num(*l, ln); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5216 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5217 | } |
| 5218 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5219 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5220 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5221 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5222 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5223 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5224 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5225 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5226 | #define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5227 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5228 | static void xc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5229 | { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5230 | r->t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5231 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5232 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5233 | } |
| 5234 | |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5235 | // Note: *r and *n need not be initialized by caller |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5236 | static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5237 | { |
| 5238 | BcStatus s; |
| 5239 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5240 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5241 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5242 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5243 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5244 | s = zxc_program_num(*r, n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5245 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5246 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5247 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5248 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5249 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5250 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5251 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5252 | #define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5253 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5254 | static void xc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5255 | { |
| 5256 | r->t = t; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5257 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5258 | } |
| 5259 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5260 | static BC_STATUS zxc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5261 | { |
| 5262 | BcStatus s; |
| 5263 | BcResult *opd1, *opd2, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5264 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5265 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5266 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5267 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5268 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5269 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5270 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5271 | 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] | 5272 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5273 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5274 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5275 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5276 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5277 | bc_num_free(&res.d.n); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5278 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5279 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5280 | #define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5281 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5282 | static BC_STATUS zxc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5283 | { |
| 5284 | BcStatus s; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5285 | BcParse sv_parse; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5286 | BcVec buf; |
| 5287 | BcInstPtr ip; |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5288 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5289 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 5290 | bc_char_vec_init(&buf); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5291 | xc_read_line(&buf, stdin); |
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 | f = xc_program_func(BC_PROG_READ); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5294 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5295 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5296 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5297 | xc_parse_create(BC_PROG_READ); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 5298 | //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] | 5299 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5300 | s = zxc_parse_text_init(buf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5301 | if (s) goto exec_err; |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5302 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5303 | IF_BC(s = zbc_parse_expr(0)); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5304 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5305 | IF_DC(s = zdc_parse_exprs_until_eof()); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5306 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5307 | if (s) goto exec_err; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5308 | if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 5309 | s = bc_error_at("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5310 | goto exec_err; |
| 5311 | } |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 5312 | xc_parse_push(XC_INST_RET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5313 | |
| 5314 | ip.func = BC_PROG_READ; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5315 | ip.inst_idx = 0; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5316 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5317 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5318 | exec_err: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5319 | xc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5320 | G.prs = sv_parse; // struct copy |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5321 | bc_vec_free(&buf); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5322 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5323 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5324 | #define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5325 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5326 | static size_t xc_program_index(char *code, size_t *bgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5327 | { |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5328 | unsigned char *bytes = (void*)(code + *bgn); |
| 5329 | unsigned amt; |
| 5330 | unsigned i; |
| 5331 | size_t res; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5332 | |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5333 | amt = *bytes++; |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 5334 | if (amt < SMALL_INDEX_LIMIT) { |
| 5335 | *bgn += 1; |
| 5336 | return amt; |
| 5337 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 5338 | amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5339 | *bgn += amt + 1; |
| 5340 | |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5341 | res = 0; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 5342 | i = 0; |
| 5343 | do { |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5344 | res |= (size_t)(*bytes++) << i; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 5345 | i += 8; |
| 5346 | } while (--amt != 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5347 | |
| 5348 | return res; |
| 5349 | } |
| 5350 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5351 | static char *xc_program_name(char *code, size_t *bgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5352 | { |
Denys Vlasenko | 694d298 | 2018-12-18 19:17:11 +0100 | [diff] [blame] | 5353 | code += *bgn; |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 5354 | *bgn += strlen(code) + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5355 | |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 5356 | return xstrdup(code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5357 | } |
| 5358 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5359 | static void xc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5360 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5361 | #if ENABLE_DC |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5362 | if (!str[0] && IS_DC) { |
Denys Vlasenko | 2c6f563 | 2018-12-11 21:21:14 +0100 | [diff] [blame] | 5363 | // Example: echo '[]ap' | dc |
| 5364 | // should print two bytes: 0x00, 0x0A |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5365 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5366 | return; |
| 5367 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5368 | #endif |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5369 | while (*str) { |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5370 | char c = *str++; |
| 5371 | if (c == '\\') { |
| 5372 | static const char esc[] ALIGN1 = "nabfrt""e\\"; |
| 5373 | char *n; |
| 5374 | |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5375 | c = *str++; |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5376 | n = strchr(esc, c); // note: c can be NUL |
| 5377 | if (!n) { |
| 5378 | // Just print the backslash and following character |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5379 | bb_putchar('\\'); |
| 5380 | ++G.prog.nchars; |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5381 | } else { |
| 5382 | if (n - esc == 0) // "\n" ? |
| 5383 | G.prog.nchars = SIZE_MAX; |
| 5384 | c = "\n\a\b\f\r\t""\\\\""\\"[n - esc]; |
| 5385 | // n a b f r t e \ \<end of line> |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5386 | } |
| 5387 | } |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5388 | putchar(c); |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5389 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5390 | } |
| 5391 | } |
| 5392 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5393 | static void bc_num_printNewline(void) |
| 5394 | { |
| 5395 | if (G.prog.nchars == G.prog.len - 1) { |
| 5396 | bb_putchar('\\'); |
| 5397 | bb_putchar('\n'); |
| 5398 | G.prog.nchars = 0; |
| 5399 | } |
| 5400 | } |
| 5401 | |
| 5402 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5403 | 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] | 5404 | { |
| 5405 | (void) radix; |
| 5406 | bb_putchar((char) num); |
| 5407 | G.prog.nchars += width; |
| 5408 | } |
| 5409 | #endif |
| 5410 | |
| 5411 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
| 5412 | { |
| 5413 | size_t exp, pow; |
| 5414 | |
| 5415 | bc_num_printNewline(); |
| 5416 | bb_putchar(radix ? '.' : ' '); |
| 5417 | ++G.prog.nchars; |
| 5418 | |
| 5419 | bc_num_printNewline(); |
| 5420 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 5421 | continue; |
| 5422 | |
| 5423 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
| 5424 | size_t dig; |
| 5425 | bc_num_printNewline(); |
| 5426 | dig = num / pow; |
| 5427 | num -= dig * pow; |
| 5428 | bb_putchar(((char) dig) + '0'); |
| 5429 | } |
| 5430 | } |
| 5431 | |
| 5432 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
| 5433 | { |
| 5434 | if (radix) { |
| 5435 | bc_num_printNewline(); |
| 5436 | bb_putchar('.'); |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5437 | G.prog.nchars++; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | bc_num_printNewline(); |
| 5441 | bb_putchar(bb_hexdigits_upcase[num]); |
| 5442 | G.prog.nchars += width; |
| 5443 | } |
| 5444 | |
| 5445 | static void bc_num_printDecimal(BcNum *n) |
| 5446 | { |
| 5447 | size_t i, rdx = n->rdx - 1; |
| 5448 | |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5449 | if (n->neg) { |
| 5450 | bb_putchar('-'); |
| 5451 | G.prog.nchars++; |
| 5452 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5453 | |
| 5454 | for (i = n->len - 1; i < n->len; --i) |
| 5455 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
| 5456 | } |
| 5457 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5458 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
| 5459 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5460 | 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] | 5461 | { |
| 5462 | BcStatus s; |
| 5463 | BcVec stack; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5464 | BcNum base; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5465 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5466 | BcNum intp, fracp, digit, frac_len; |
| 5467 | unsigned long dig, *ptr; |
| 5468 | size_t i; |
| 5469 | bool radix; |
| 5470 | |
| 5471 | if (n->len == 0) { |
| 5472 | print(0, width, false); |
| 5473 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5474 | } |
| 5475 | |
| 5476 | bc_vec_init(&stack, sizeof(long), NULL); |
| 5477 | bc_num_init(&intp, n->len); |
| 5478 | bc_num_init(&fracp, n->rdx); |
| 5479 | bc_num_init(&digit, width); |
| 5480 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 5481 | bc_num_copy(&intp, n); |
| 5482 | bc_num_one(&frac_len); |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5483 | base.cap = ARRAY_SIZE(base_digs); |
| 5484 | base.num = base_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5485 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5486 | |
| 5487 | bc_num_truncate(&intp, intp.rdx); |
| 5488 | s = zbc_num_sub(n, &intp, &fracp, 0); |
| 5489 | if (s) goto err; |
| 5490 | |
| 5491 | while (intp.len != 0) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5492 | s = zbc_num_divmod(&intp, &base, &intp, &digit, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5493 | if (s) goto err; |
| 5494 | s = zbc_num_ulong(&digit, &dig); |
| 5495 | if (s) goto err; |
| 5496 | bc_vec_push(&stack, &dig); |
| 5497 | } |
| 5498 | |
| 5499 | for (i = 0; i < stack.len; ++i) { |
| 5500 | ptr = bc_vec_item_rev(&stack, i); |
| 5501 | print(*ptr, width, false); |
| 5502 | } |
| 5503 | |
| 5504 | if (!n->rdx) goto err; |
| 5505 | |
| 5506 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5507 | s = zbc_num_mul(&fracp, &base, &fracp, n->rdx); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5508 | if (s) goto err; |
| 5509 | s = zbc_num_ulong(&fracp, &dig); |
| 5510 | if (s) goto err; |
| 5511 | bc_num_ulong2num(&intp, dig); |
| 5512 | s = zbc_num_sub(&fracp, &intp, &fracp, 0); |
| 5513 | if (s) goto err; |
| 5514 | print(dig, width, radix); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5515 | s = zbc_num_mul(&frac_len, &base, &frac_len, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5516 | if (s) goto err; |
| 5517 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5518 | err: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5519 | bc_num_free(&frac_len); |
| 5520 | bc_num_free(&digit); |
| 5521 | bc_num_free(&fracp); |
| 5522 | bc_num_free(&intp); |
| 5523 | bc_vec_free(&stack); |
| 5524 | RETURN_STATUS(s); |
| 5525 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5526 | #define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5527 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5528 | static BC_STATUS zxc_num_printBase(BcNum *n) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5529 | { |
| 5530 | BcStatus s; |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5531 | size_t width; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5532 | BcNumDigitOp print; |
| 5533 | bool neg = n->neg; |
| 5534 | |
| 5535 | if (neg) { |
| 5536 | bb_putchar('-'); |
| 5537 | G.prog.nchars++; |
| 5538 | } |
| 5539 | |
| 5540 | n->neg = false; |
| 5541 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 5542 | if (G.prog.ob_t <= 16) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5543 | width = 1; |
| 5544 | print = bc_num_printHex; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5545 | } else { |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5546 | unsigned i = G.prog.ob_t - 1; |
| 5547 | width = 0; |
| 5548 | for (;;) { |
| 5549 | width++; |
| 5550 | i /= 10; |
| 5551 | if (i == 0) |
| 5552 | break; |
| 5553 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5554 | print = bc_num_printDigits; |
| 5555 | } |
| 5556 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5557 | s = zxc_num_printNum(n, G.prog.ob_t, width, print); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5558 | n->neg = neg; |
| 5559 | |
| 5560 | RETURN_STATUS(s); |
| 5561 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5562 | #define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5563 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5564 | static BC_STATUS zxc_num_print(BcNum *n, bool newline) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5565 | { |
| 5566 | BcStatus s = BC_STATUS_SUCCESS; |
| 5567 | |
| 5568 | bc_num_printNewline(); |
| 5569 | |
| 5570 | if (n->len == 0) { |
| 5571 | bb_putchar('0'); |
| 5572 | ++G.prog.nchars; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5573 | } else if (G.prog.ob_t == 10) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5574 | bc_num_printDecimal(n); |
| 5575 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5576 | s = zxc_num_printBase(n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5577 | |
| 5578 | if (newline) { |
| 5579 | bb_putchar('\n'); |
| 5580 | G.prog.nchars = 0; |
| 5581 | } |
| 5582 | |
| 5583 | RETURN_STATUS(s); |
| 5584 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5585 | #define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5586 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5587 | static BC_STATUS zxc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5588 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5589 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5590 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5591 | BcNum *num; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5592 | bool pop = (inst != XC_INST_PRINT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5593 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5594 | if (!STACK_HAS_MORE_THAN(&G.prog.results, idx)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5595 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5596 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5597 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5598 | s = zxc_program_num(r, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5599 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5600 | |
| 5601 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5602 | s = zxc_num_print(num, !pop); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5603 | #if ENABLE_BC |
| 5604 | if (!s && IS_BC) bc_num_copy(&G.prog.last, num); |
| 5605 | #endif |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5606 | } else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5607 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5608 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5609 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5610 | str = *xc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5611 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5612 | if (inst == XC_INST_PRINT_STR) { |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5613 | char *nl; |
| 5614 | G.prog.nchars += printf("%s", str); |
| 5615 | nl = strrchr(str, '\n'); |
| 5616 | if (nl) |
| 5617 | G.prog.nchars = strlen(nl + 1); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5618 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5619 | xc_program_printString(str); |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5620 | if (inst == XC_INST_PRINT) |
| 5621 | bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5622 | } |
| 5623 | } |
| 5624 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5625 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5626 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5627 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5628 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5629 | #define zxc_program_print(...) (zxc_program_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5630 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5631 | static BC_STATUS zxc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5632 | { |
| 5633 | BcStatus s; |
| 5634 | BcResult res, *ptr; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5635 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5636 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5637 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5638 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5639 | |
| 5640 | bc_num_init(&res.d.n, num->len); |
| 5641 | bc_num_copy(&res.d.n, num); |
| 5642 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5643 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5644 | xc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5645 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5646 | RETURN_STATUS(s); |
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 | #define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5649 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5650 | static BC_STATUS zxc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5651 | { |
| 5652 | BcStatus s; |
| 5653 | BcResult *opd1, *opd2, res; |
| 5654 | BcNum *n1, *n2; |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5655 | ssize_t cond; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5656 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5657 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5658 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5659 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5660 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5661 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5662 | if (inst == XC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5663 | 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] | 5664 | else if (inst == XC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5665 | 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] | 5666 | else { |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5667 | cond = bc_num_cmp(n1, n2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5668 | switch (inst) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5669 | case XC_INST_REL_EQ: |
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_LE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5673 | cond = (cond <= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5674 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5675 | case XC_INST_REL_GE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5676 | cond = (cond >= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5677 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5678 | case XC_INST_REL_LT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5679 | cond = (cond < 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5680 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5681 | case XC_INST_REL_GT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5682 | cond = (cond > 0); |
| 5683 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5684 | default: // = case XC_INST_REL_NE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5685 | //cond = (cond != 0); - not needed |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5686 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5687 | } |
| 5688 | } |
| 5689 | |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5690 | if (cond) bc_num_one(&res.d.n); |
| 5691 | //else bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5692 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5693 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5694 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5695 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5696 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5697 | #define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5698 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5699 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5700 | static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5701 | { |
| 5702 | BcNum n2; |
| 5703 | BcResult res; |
| 5704 | |
| 5705 | memset(&n2, 0, sizeof(BcNum)); |
| 5706 | n2.rdx = res.d.id.idx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5707 | res.t = XC_RESULT_STR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5708 | |
| 5709 | if (!push) { |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5710 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5711 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5712 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5713 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5714 | } |
| 5715 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5716 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5717 | bc_vec_push(v, &n2); |
| 5718 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5719 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5720 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5721 | #define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5722 | #endif // ENABLE_DC |
| 5723 | |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 5724 | static BC_STATUS zxc_program_popResultAndCopyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5725 | { |
| 5726 | BcStatus s; |
| 5727 | BcResult *ptr, r; |
| 5728 | BcVec *v; |
| 5729 | BcNum *n; |
| 5730 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5731 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5732 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5733 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5734 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5735 | if ((ptr->t == XC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5736 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5737 | v = xc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5738 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5739 | #if ENABLE_DC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5740 | if (ptr->t == XC_RESULT_STR && !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5741 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5742 | if (ptr->t == XC_RESULT_STR) |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5743 | RETURN_STATUS(zdc_program_assignStr(ptr, v, true)); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5744 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5745 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5746 | s = zxc_program_num(ptr, &n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5747 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5748 | |
| 5749 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5750 | v = xc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5751 | |
| 5752 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5753 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5754 | bc_num_copy(&r.d.n, n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5755 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5756 | bc_array_init(&r.d.v, true); |
| 5757 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 5758 | } |
| 5759 | |
| 5760 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5761 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5762 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5763 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5764 | } |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 5765 | #define zxc_program_popResultAndCopyToVar(...) (zxc_program_popResultAndCopyToVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5766 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5767 | static BC_STATUS zxc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5768 | { |
| 5769 | BcStatus s; |
| 5770 | BcResult *left, *right, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5771 | BcNum *l, *r; |
| 5772 | bool assign = (inst == XC_INST_ASSIGN); |
| 5773 | bool ib, sc; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5774 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5775 | s = zxc_program_binOpPrep(&left, &l, &right, &r, assign); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5776 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5777 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5778 | ib = left->t == XC_RESULT_IBASE; |
| 5779 | sc = left->t == XC_RESULT_SCALE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5780 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5781 | #if ENABLE_DC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5782 | if (right->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5783 | BcVec *v; |
| 5784 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5785 | if (left->t != XC_RESULT_VAR) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5786 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5787 | v = xc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5788 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5789 | RETURN_STATUS(zdc_program_assignStr(right, v, false)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5790 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5791 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5792 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5793 | if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 5794 | RETURN_STATUS(bc_error_bad_assignment()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5795 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5796 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5797 | if (assign) |
| 5798 | bc_num_copy(l, r); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5799 | else { |
| 5800 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5801 | 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] | 5802 | } |
| 5803 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5804 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5805 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5806 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5807 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5808 | if (ib || sc || left->t == XC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5809 | static const char *const msg[] = { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5810 | "bad ibase; must be [2,16]", //XC_RESULT_IBASE |
| 5811 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE |
| 5812 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5813 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5814 | size_t *ptr; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5815 | size_t max; |
| 5816 | unsigned long val; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5817 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5818 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5819 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5820 | s = left->t - XC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5821 | if (sc) { |
| 5822 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5823 | ptr = &G.prog.scale; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5824 | } else { |
| 5825 | if (val < 2) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5826 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5827 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5828 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5829 | } |
| 5830 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5831 | if (val > max) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5832 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5833 | |
| 5834 | *ptr = (size_t) val; |
| 5835 | s = BC_STATUS_SUCCESS; |
| 5836 | } |
| 5837 | |
| 5838 | bc_num_init(&res.d.n, l->len); |
| 5839 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5840 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5841 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5842 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5843 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5844 | #define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5845 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5846 | #if !ENABLE_DC |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5847 | #define xc_program_pushVar(code, bgn, pop, copy) \ |
| 5848 | xc_program_pushVar(code, bgn) |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5849 | // for bc, 'pop' and 'copy' are always false |
| 5850 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5851 | static BC_STATUS xc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5852 | bool pop, bool copy) |
| 5853 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5854 | BcResult r; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5855 | char *name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5856 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5857 | r.t = XC_RESULT_VAR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5858 | r.d.id.name = name; |
| 5859 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5860 | #if ENABLE_DC |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5861 | if (pop || copy) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5862 | BcVec *v = xc_program_search(name, true); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5863 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5864 | |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5865 | free(name); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5866 | if (!STACK_HAS_MORE_THAN(v, 1 - copy)) { |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5867 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5868 | } |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5869 | |
| 5870 | if (!BC_PROG_STR(num)) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5871 | r.t = XC_RESULT_TEMP; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5872 | bc_num_init_DEF_SIZE(&r.d.n); |
| 5873 | bc_num_copy(&r.d.n, num); |
| 5874 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5875 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5876 | r.d.id.idx = num->rdx; |
| 5877 | } |
| 5878 | |
| 5879 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5880 | } |
| 5881 | #endif // ENABLE_DC |
| 5882 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5883 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5884 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 5885 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5886 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5887 | #define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5888 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5889 | 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] | 5890 | { |
| 5891 | BcStatus s = BC_STATUS_SUCCESS; |
| 5892 | BcResult r; |
| 5893 | BcNum *num; |
| 5894 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5895 | r.d.id.name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5896 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5897 | if (inst == XC_INST_ARRAY) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5898 | r.t = XC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5899 | bc_vec_push(&G.prog.results, &r); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5900 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5901 | BcResult *operand; |
| 5902 | unsigned long temp; |
| 5903 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5904 | s = zxc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5905 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5906 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5907 | if (s) goto err; |
| 5908 | |
| 5909 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 5910 | 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] | 5911 | goto err; |
| 5912 | } |
| 5913 | |
| 5914 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5915 | xc_program_retire(&r, XC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5916 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5917 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5918 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5919 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5920 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5921 | #define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5922 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5923 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5924 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5925 | { |
| 5926 | BcStatus s; |
| 5927 | BcResult *ptr, res, copy; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5928 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5929 | char inst2 = inst; |
| 5930 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5931 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5932 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5933 | |
| 5934 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5935 | copy.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5936 | bc_num_init(©.d.n, num->len); |
| 5937 | bc_num_copy(©.d.n, num); |
| 5938 | } |
| 5939 | |
| 5940 | res.t = BC_RESULT_ONE; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5941 | inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST) |
| 5942 | ? BC_INST_ASSIGN_PLUS |
| 5943 | : BC_INST_ASSIGN_MINUS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5944 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5945 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5946 | s = zxc_program_assign(inst); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5947 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5948 | |
| 5949 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5950 | bc_result_pop_and_push(©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5951 | } |
| 5952 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5953 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5954 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5955 | #define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5956 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5957 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5958 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5959 | BcInstPtr ip; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5960 | size_t i, nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5961 | BcId *a; |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 5962 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5963 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5964 | nparams = xc_program_index(code, idx); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5965 | ip.func = xc_program_index(code, idx); |
| 5966 | func = xc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5967 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 5968 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5969 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 5970 | } |
| 5971 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5972 | 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] | 5973 | } |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 5974 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5975 | |
| 5976 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 5977 | BcResult *arg; |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5978 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5979 | |
| 5980 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5981 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5982 | |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 5983 | if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) // array/variable mismatch |
| 5984 | // || arg->t == XC_RESULT_STR - impossible, f("str") is not a legal syntax (strings are not bc expressions) |
| 5985 | ) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5986 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 5987 | } |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 5988 | s = zxc_program_popResultAndCopyToVar(a->name, a->idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5989 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5990 | } |
| 5991 | |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 5992 | a = bc_vec_item(&func->autos, i); |
| 5993 | for (; i < func->autos.len; i++, a++) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5994 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5995 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5996 | v = xc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5997 | if (a->idx) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5998 | BcNum n2; |
| 5999 | bc_num_init_DEF_SIZE(&n2); |
| 6000 | bc_vec_push(v, &n2); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6001 | } else { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 6002 | BcVec v2; |
| 6003 | bc_array_init(&v2, true); |
| 6004 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6005 | } |
| 6006 | } |
| 6007 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6008 | bc_vec_push(&G.prog.exestack, &ip); |
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 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6011 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6012 | #define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6013 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6014 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6015 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6016 | BcResult res; |
| 6017 | BcFunc *f; |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6018 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6019 | size_t i; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6020 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6021 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6022 | if (inst == XC_INST_RET) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6023 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6024 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6025 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6026 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6027 | s = zxc_program_num(operand, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6028 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6029 | bc_num_init(&res.d.n, num->len); |
| 6030 | bc_num_copy(&res.d.n, num); |
Denys Vlasenko | 377cc97 | 2019-01-04 00:34:52 +0100 | [diff] [blame] | 6031 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 6032 | //} else if (f->void_func) { |
| 6033 | //prepare "void" result in res |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6034 | } else { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6035 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6036 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6037 | } |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 6038 | res.t = XC_RESULT_TEMP; |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 6039 | bc_vec_push(&G.prog.results, &res); |
| 6040 | |
| 6041 | bc_vec_pop(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6042 | |
| 6043 | // We need to pop arguments as well, so this takes that into account. |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 6044 | f = xc_program_func(ip->func); |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6045 | a = (void*)f->autos.v; |
| 6046 | for (i = 0; i < f->autos.len; i++, a++) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6047 | BcVec *v; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6048 | v = xc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6049 | bc_vec_pop(v); |
| 6050 | } |
| 6051 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6052 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6053 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6054 | #define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6055 | #endif // ENABLE_BC |
| 6056 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6057 | static unsigned long xc_program_scale(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6058 | { |
| 6059 | return (unsigned long) n->rdx; |
| 6060 | } |
| 6061 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6062 | static unsigned long xc_program_len(BcNum *n) |
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 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6065 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6066 | if (n->rdx != len) return len; |
| 6067 | for (;;) { |
| 6068 | if (len == 0) break; |
| 6069 | len--; |
| 6070 | if (n->num[len] != 0) break; |
| 6071 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6072 | return len; |
| 6073 | } |
| 6074 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6075 | static BC_STATUS zxc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6076 | { |
| 6077 | BcStatus s; |
| 6078 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6079 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6080 | BcResult res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6081 | bool len = (inst == XC_INST_LENGTH); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6082 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6083 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6084 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6085 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6086 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6087 | s = zxc_program_num(opnd, &num); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6088 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6089 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6090 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6091 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6092 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6093 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6094 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6095 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6096 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6097 | if (inst == XC_INST_SQRT) |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6098 | s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6099 | #if ENABLE_BC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6100 | else if (len != 0 && opnd->t == XC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6101 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6102 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6103 | #endif |
| 6104 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6105 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6106 | char **str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6107 | 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] | 6108 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6109 | str = xc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6110 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6111 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6112 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6113 | else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6114 | 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] | 6115 | } |
| 6116 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6117 | xc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6118 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6119 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6120 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6121 | #define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6122 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6123 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6124 | static BC_STATUS zdc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6125 | { |
| 6126 | BcStatus s; |
| 6127 | BcResult *opd1, *opd2, res, res2; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6128 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6129 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6130 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6131 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6132 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6133 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6134 | bc_num_init(&res2.d.n, n2->len); |
| 6135 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6136 | 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] | 6137 | if (s) goto err; |
| 6138 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6139 | xc_program_binOpRetire(&res2); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6140 | res.t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6141 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6142 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6143 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6144 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6145 | bc_num_free(&res2.d.n); |
| 6146 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6147 | RETURN_STATUS(s); |
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 | #define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6150 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6151 | static BC_STATUS zdc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6152 | { |
| 6153 | BcStatus s; |
| 6154 | BcResult *r1, *r2, *r3, res; |
| 6155 | BcNum *n1, *n2, *n3; |
| 6156 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6157 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 2)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6158 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6159 | s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6160 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6161 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6162 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6163 | s = zxc_program_num(r1, &n1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6164 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6165 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6166 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6167 | |
| 6168 | // Make sure that the values have their pointers updated, if necessary. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6169 | if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6170 | if (r1->t == r2->t) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6171 | s = zxc_program_num(r2, &n2); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6172 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6173 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6174 | if (r1->t == r3->t) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6175 | s = zxc_program_num(r3, &n3); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6176 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6177 | } |
| 6178 | } |
| 6179 | |
| 6180 | bc_num_init(&res.d.n, n3->len); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6181 | s = zdc_num_modexp(n1, n2, n3, &res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6182 | if (s) goto err; |
| 6183 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6184 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6185 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6186 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6187 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6188 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6189 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6190 | RETURN_STATUS(s); |
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 | #define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6193 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6194 | static void dc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6195 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6196 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6197 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6198 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6199 | res.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6200 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6201 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6202 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6203 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6204 | } |
| 6205 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6206 | static BC_STATUS zdc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6207 | { |
| 6208 | BcStatus s; |
| 6209 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6210 | BcNum *num, n; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6211 | char **strs; |
| 6212 | char *str; |
| 6213 | char c; |
| 6214 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6215 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6216 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6217 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6218 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 6219 | r = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6220 | s = zxc_program_num(r, &num); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6221 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6222 | |
| 6223 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 6224 | unsigned long val; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6225 | BcNum strmb; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6226 | BcDig strmb_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6227 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6228 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6229 | bc_num_copy(&n, num); |
| 6230 | bc_num_truncate(&n, n.rdx); |
| 6231 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6232 | strmb.cap = ARRAY_SIZE(strmb_digs); |
| 6233 | strmb.num = strmb_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6234 | bc_num_ulong2num(&strmb, 0x100); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6235 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 6236 | s = zbc_num_mod(&n, &strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6237 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6238 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6239 | if (s) goto num_err; |
| 6240 | |
| 6241 | c = (char) val; |
| 6242 | |
| 6243 | bc_num_free(&n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6244 | } else { |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6245 | char *sp; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6246 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6247 | sp = *xc_program_str(idx); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6248 | c = sp[0]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6249 | } |
| 6250 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6251 | strs = (void*)G.prog.strs.v; |
| 6252 | for (idx = 0; idx < G.prog.strs.len; idx++) { |
| 6253 | if (strs[idx][0] == c && strs[idx][1] == '\0') { |
| 6254 | goto dup; |
| 6255 | } |
| 6256 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6257 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6258 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6259 | //str[1] = '\0'; - already is |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6260 | bc_vec_push(&G.prog.strs, &str); |
| 6261 | dup: |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6262 | res.t = XC_RESULT_STR; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6263 | res.d.id.idx = idx; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 6264 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6265 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6266 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6267 | num_err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6268 | bc_num_free(&n); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6269 | RETURN_STATUS(s); |
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 | #define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6272 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6273 | static BC_STATUS zdc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6274 | { |
| 6275 | BcStatus s; |
| 6276 | BcResult *r; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6277 | BcNum *n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6278 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6279 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6280 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6281 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6282 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6283 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6284 | s = zxc_program_num(r, &n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6285 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6286 | |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6287 | if (BC_PROG_NUM(r, n)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6288 | s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar); |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6289 | } else { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6290 | char *str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6291 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6292 | str = *xc_program_str(idx); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6293 | fputs(str, stdout); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6294 | } |
| 6295 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6296 | RETURN_STATUS(s); |
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 | #define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6299 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6300 | static BC_STATUS zdc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6301 | { |
| 6302 | BcStatus s; |
| 6303 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6304 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6305 | unsigned long val; |
| 6306 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6307 | s = zxc_program_prep(&opnd, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6308 | if (s) RETURN_STATUS(s); |
| 6309 | s = zbc_num_ulong(num, &val); |
| 6310 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6311 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6312 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6313 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6314 | if (G.prog.exestack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6315 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6316 | if (G.prog.exestack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6317 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6318 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6319 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6320 | bc_vec_npop(&G.prog.exestack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6321 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6322 | RETURN_STATUS(s); |
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 | #define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6325 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6326 | 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] | 6327 | { |
| 6328 | BcStatus s = BC_STATUS_SUCCESS; |
| 6329 | BcResult *r; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6330 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6331 | BcInstPtr ip; |
| 6332 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6333 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6334 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6335 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6336 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6337 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6338 | |
| 6339 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6340 | BcNum *n = n; // for compiler |
| 6341 | bool exec; |
| 6342 | char *name; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6343 | char *then_name = xc_program_name(code, bgn); |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6344 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6345 | |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 6346 | if (code[*bgn] == '\0') |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6347 | (*bgn) += 1; |
| 6348 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6349 | else_name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6350 | |
| 6351 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6352 | name = then_name; |
| 6353 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6354 | exec = true; |
| 6355 | name = else_name; |
| 6356 | } |
| 6357 | |
| 6358 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6359 | BcVec *v; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6360 | v = xc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6361 | n = bc_vec_top(v); |
| 6362 | } |
| 6363 | |
| 6364 | free(then_name); |
| 6365 | free(else_name); |
| 6366 | |
| 6367 | if (!exec) goto exit; |
| 6368 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6369 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6370 | goto exit; |
| 6371 | } |
| 6372 | |
| 6373 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6374 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6375 | if (r->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6376 | sidx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6377 | } else if (r->t == XC_RESULT_VAR) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6378 | BcNum *n; |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6379 | s = zxc_program_num(r, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6380 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6381 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6382 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6383 | goto exit; |
| 6384 | } |
| 6385 | |
| 6386 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6387 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6388 | f = xc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6389 | |
| 6390 | if (f->code.len == 0) { |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6391 | BcParse sv_parse; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6392 | char *str; |
| 6393 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6394 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6395 | xc_parse_create(fidx); |
| 6396 | str = *xc_program_str(sidx); |
| 6397 | s = zxc_parse_text_init(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6398 | if (s) goto err; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6399 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6400 | s = zdc_parse_exprs_until_eof(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6401 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6402 | xc_parse_push(DC_INST_POP_EXEC); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6403 | if (G.prs.lex != XC_LEX_EOF) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6404 | s = bc_error_bad_expression(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6405 | xc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6406 | G.prs = sv_parse; // struct copy |
| 6407 | if (s) { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6408 | err: |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6409 | bc_vec_pop_all(&f->code); |
| 6410 | goto exit; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6411 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6412 | } |
| 6413 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6414 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6415 | ip.func = fidx; |
| 6416 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6417 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6418 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6419 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6420 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6421 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6422 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6423 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6424 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6425 | #define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6426 | #endif // ENABLE_DC |
| 6427 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6428 | static void xc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6429 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6430 | BcResult res; |
| 6431 | unsigned long val; |
| 6432 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6433 | res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6434 | if (inst == XC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6435 | val = (unsigned long) G.prog.ib_t; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6436 | else if (inst == XC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6437 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6438 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6439 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6440 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6441 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6442 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6443 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6444 | } |
| 6445 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6446 | static BC_STATUS zxc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6447 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6448 | BcResult r, *ptr; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6449 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6450 | BcFunc *func = xc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6451 | char *code = func->code.v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6452 | |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6453 | dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d", |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6454 | ip->func, func->code.len, ip->inst_idx, G.prog.results.len); |
| 6455 | while (ip->inst_idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6456 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6457 | char inst = code[ip->inst_idx++]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6458 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6459 | 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] | 6460 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6461 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6462 | case BC_INST_JUMP_ZERO: { |
| 6463 | BcNum *num; |
| 6464 | bool zero; |
| 6465 | dbg_exec("BC_INST_JUMP_ZERO:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6466 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6467 | if (s) RETURN_STATUS(s); |
| 6468 | zero = (bc_num_cmp(num, &G.prog.zero) == 0); |
| 6469 | bc_vec_pop(&G.prog.results); |
| 6470 | if (!zero) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6471 | xc_program_index(code, &ip->inst_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6472 | break; |
| 6473 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6474 | // else: fall through |
| 6475 | } |
| 6476 | case BC_INST_JUMP: { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6477 | size_t idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6478 | size_t *addr = bc_vec_item(&func->labels, idx); |
| 6479 | dbg_exec("BC_INST_JUMP: to %ld", (long)*addr); |
| 6480 | ip->inst_idx = *addr; |
| 6481 | break; |
| 6482 | } |
| 6483 | case BC_INST_CALL: |
| 6484 | dbg_exec("BC_INST_CALL:"); |
| 6485 | s = zbc_program_call(code, &ip->inst_idx); |
| 6486 | goto read_updated_ip; |
| 6487 | case BC_INST_INC_PRE: |
| 6488 | case BC_INST_DEC_PRE: |
| 6489 | case BC_INST_INC_POST: |
| 6490 | case BC_INST_DEC_POST: |
| 6491 | dbg_exec("BC_INST_INCDEC:"); |
| 6492 | s = zbc_program_incdec(inst); |
| 6493 | break; |
| 6494 | case BC_INST_HALT: |
| 6495 | dbg_exec("BC_INST_HALT:"); |
| 6496 | QUIT_OR_RETURN_TO_MAIN; |
| 6497 | break; |
| 6498 | case XC_INST_RET: |
| 6499 | case BC_INST_RET0: |
| 6500 | dbg_exec("BC_INST_RET[0]:"); |
| 6501 | s = zbc_program_return(inst); |
| 6502 | goto read_updated_ip; |
| 6503 | case XC_INST_BOOL_OR: |
| 6504 | case XC_INST_BOOL_AND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6505 | #endif // ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6506 | case XC_INST_REL_EQ: |
| 6507 | case XC_INST_REL_LE: |
| 6508 | case XC_INST_REL_GE: |
| 6509 | case XC_INST_REL_NE: |
| 6510 | case XC_INST_REL_LT: |
| 6511 | case XC_INST_REL_GT: |
| 6512 | dbg_exec("BC_INST_BOOL:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6513 | s = zxc_program_logical(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6514 | break; |
| 6515 | case XC_INST_READ: |
| 6516 | dbg_exec("XC_INST_READ:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6517 | s = zxc_program_read(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6518 | goto read_updated_ip; |
| 6519 | case XC_INST_VAR: |
| 6520 | dbg_exec("XC_INST_VAR:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6521 | s = zxc_program_pushVar(code, &ip->inst_idx, false, false); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6522 | break; |
| 6523 | case XC_INST_ARRAY_ELEM: |
| 6524 | case XC_INST_ARRAY: |
| 6525 | dbg_exec("XC_INST_ARRAY[_ELEM]:"); |
| 6526 | s = zbc_program_pushArray(code, &ip->inst_idx, inst); |
| 6527 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6528 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6529 | case BC_INST_LAST: |
| 6530 | dbg_exec("BC_INST_LAST:"); |
| 6531 | r.t = BC_RESULT_LAST; |
| 6532 | bc_vec_push(&G.prog.results, &r); |
| 6533 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6534 | #endif |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6535 | case XC_INST_IBASE: |
| 6536 | case XC_INST_OBASE: |
| 6537 | case XC_INST_SCALE: |
| 6538 | dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6539 | xc_program_pushGlobal(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6540 | break; |
| 6541 | case XC_INST_SCALE_FUNC: |
| 6542 | case XC_INST_LENGTH: |
| 6543 | case XC_INST_SQRT: |
| 6544 | dbg_exec("BC_INST_builtin:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6545 | s = zxc_program_builtin(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6546 | break; |
| 6547 | case XC_INST_NUM: |
| 6548 | dbg_exec("XC_INST_NUM:"); |
| 6549 | r.t = XC_RESULT_CONSTANT; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6550 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6551 | bc_vec_push(&G.prog.results, &r); |
| 6552 | break; |
| 6553 | case XC_INST_POP: |
| 6554 | dbg_exec("XC_INST_POP:"); |
| 6555 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
| 6556 | s = bc_error_stack_has_too_few_elements(); |
| 6557 | else |
| 6558 | bc_vec_pop(&G.prog.results); |
| 6559 | break; |
| 6560 | case XC_INST_PRINT: |
| 6561 | case XC_INST_PRINT_POP: |
| 6562 | case XC_INST_PRINT_STR: |
| 6563 | dbg_exec("XC_INST_PRINTxyz:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6564 | s = zxc_program_print(inst, 0); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6565 | break; |
| 6566 | case XC_INST_STR: |
| 6567 | dbg_exec("XC_INST_STR:"); |
| 6568 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6569 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6570 | bc_vec_push(&G.prog.results, &r); |
| 6571 | break; |
| 6572 | case XC_INST_POWER: |
| 6573 | case XC_INST_MULTIPLY: |
| 6574 | case XC_INST_DIVIDE: |
| 6575 | case XC_INST_MODULUS: |
| 6576 | case XC_INST_PLUS: |
| 6577 | case XC_INST_MINUS: |
| 6578 | dbg_exec("BC_INST_binaryop:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6579 | s = zxc_program_op(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6580 | break; |
| 6581 | case XC_INST_BOOL_NOT: { |
| 6582 | BcNum *num; |
| 6583 | dbg_exec("XC_INST_BOOL_NOT:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6584 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6585 | if (s) RETURN_STATUS(s); |
| 6586 | bc_num_init_DEF_SIZE(&r.d.n); |
| 6587 | if (bc_num_cmp(num, &G.prog.zero) == 0) |
| 6588 | bc_num_one(&r.d.n); |
| 6589 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6590 | xc_program_retire(&r, XC_RESULT_TEMP); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6591 | break; |
| 6592 | } |
| 6593 | case XC_INST_NEG: |
| 6594 | dbg_exec("XC_INST_NEG:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6595 | s = zxc_program_negate(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6596 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6597 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6598 | case BC_INST_ASSIGN_POWER: |
| 6599 | case BC_INST_ASSIGN_MULTIPLY: |
| 6600 | case BC_INST_ASSIGN_DIVIDE: |
| 6601 | case BC_INST_ASSIGN_MODULUS: |
| 6602 | case BC_INST_ASSIGN_PLUS: |
| 6603 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6604 | #endif |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6605 | case XC_INST_ASSIGN: |
| 6606 | dbg_exec("BC_INST_ASSIGNxyz:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6607 | s = zxc_program_assign(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6608 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6609 | #if ENABLE_DC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6610 | case DC_INST_POP_EXEC: |
| 6611 | dbg_exec("DC_INST_POP_EXEC:"); |
| 6612 | bc_vec_pop(&G.prog.exestack); |
| 6613 | goto read_updated_ip; |
| 6614 | case DC_INST_MODEXP: |
| 6615 | dbg_exec("DC_INST_MODEXP:"); |
| 6616 | s = zdc_program_modexp(); |
| 6617 | break; |
| 6618 | case DC_INST_DIVMOD: |
| 6619 | dbg_exec("DC_INST_DIVMOD:"); |
| 6620 | s = zdc_program_divmod(); |
| 6621 | break; |
| 6622 | case DC_INST_EXECUTE: |
| 6623 | case DC_INST_EXEC_COND: |
| 6624 | dbg_exec("DC_INST_EXEC[_COND]:"); |
| 6625 | s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND); |
| 6626 | goto read_updated_ip; |
| 6627 | case DC_INST_PRINT_STACK: { |
| 6628 | size_t idx; |
| 6629 | dbg_exec("DC_INST_PRINT_STACK:"); |
| 6630 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6631 | s = zxc_program_print(XC_INST_PRINT, idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6632 | if (s) break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6633 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6634 | break; |
| 6635 | } |
| 6636 | case DC_INST_CLEAR_STACK: |
| 6637 | dbg_exec("DC_INST_CLEAR_STACK:"); |
| 6638 | bc_vec_pop_all(&G.prog.results); |
| 6639 | break; |
| 6640 | case DC_INST_STACK_LEN: |
| 6641 | dbg_exec("DC_INST_STACK_LEN:"); |
| 6642 | dc_program_stackLen(); |
| 6643 | break; |
| 6644 | case DC_INST_DUPLICATE: |
| 6645 | dbg_exec("DC_INST_DUPLICATE:"); |
| 6646 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
| 6647 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| 6648 | ptr = bc_vec_top(&G.prog.results); |
| 6649 | dc_result_copy(&r, ptr); |
| 6650 | bc_vec_push(&G.prog.results, &r); |
| 6651 | break; |
| 6652 | case DC_INST_SWAP: { |
| 6653 | BcResult *ptr2; |
| 6654 | dbg_exec("DC_INST_SWAP:"); |
| 6655 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
| 6656 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| 6657 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6658 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
| 6659 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6660 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6661 | memcpy(ptr2, &r, sizeof(BcResult)); |
| 6662 | break; |
| 6663 | } |
| 6664 | case DC_INST_ASCIIFY: |
| 6665 | dbg_exec("DC_INST_ASCIIFY:"); |
| 6666 | s = zdc_program_asciify(); |
| 6667 | break; |
| 6668 | case DC_INST_PRINT_STREAM: |
| 6669 | dbg_exec("DC_INST_PRINT_STREAM:"); |
| 6670 | s = zdc_program_printStream(); |
| 6671 | break; |
| 6672 | case DC_INST_LOAD: |
| 6673 | case DC_INST_PUSH_VAR: { |
| 6674 | bool copy = inst == DC_INST_LOAD; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6675 | s = zxc_program_pushVar(code, &ip->inst_idx, true, copy); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6676 | break; |
| 6677 | } |
| 6678 | case DC_INST_PUSH_TO_VAR: { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6679 | char *name = xc_program_name(code, &ip->inst_idx); |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 6680 | s = zxc_program_popResultAndCopyToVar(name, true); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6681 | free(name); |
| 6682 | break; |
| 6683 | } |
| 6684 | case DC_INST_QUIT: |
| 6685 | dbg_exec("DC_INST_QUIT:"); |
| 6686 | if (G.prog.exestack.len <= 2) |
| 6687 | QUIT_OR_RETURN_TO_MAIN; |
| 6688 | bc_vec_npop(&G.prog.exestack, 2); |
| 6689 | goto read_updated_ip; |
| 6690 | case DC_INST_NQUIT: |
| 6691 | dbg_exec("DC_INST_NQUIT:"); |
| 6692 | s = zdc_program_nquit(); |
| 6693 | //goto read_updated_ip; - just fall through to it |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6694 | #endif // ENABLE_DC |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6695 | read_updated_ip: |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6696 | // Instruction stack has changed, read new pointers |
| 6697 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6698 | func = xc_program_func(ip->func); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6699 | code = func->code.v; |
| 6700 | 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] | 6701 | } |
| 6702 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6703 | if (s || G_interrupt) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6704 | xc_program_reset(); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6705 | RETURN_STATUS(s); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6706 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6707 | |
Denys Vlasenko | c5774a3 | 2018-12-17 01:22:53 +0100 | [diff] [blame] | 6708 | fflush_and_check(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6709 | } |
| 6710 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6711 | RETURN_STATUS(BC_STATUS_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 | #define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6714 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6715 | static unsigned xc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6716 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6717 | char *lenv; |
| 6718 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6719 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6720 | lenv = getenv(var); |
| 6721 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6722 | if (!lenv) return len; |
| 6723 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6724 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 6725 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6726 | len = BC_NUM_PRINT_WIDTH; |
| 6727 | |
| 6728 | return len; |
| 6729 | } |
| 6730 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6731 | static BC_STATUS zxc_vm_process(const char *text) |
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 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6734 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6735 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6736 | s = zxc_parse_text_init(text); // does the first zxc_lex_next() |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6737 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6738 | |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6739 | IF_BC(check_eof:) |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6740 | while (G.prs.lex != XC_LEX_EOF) { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6741 | BcInstPtr *ip; |
| 6742 | BcFunc *f; |
| 6743 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6744 | 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] | 6745 | if (IS_BC) { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6746 | #if ENABLE_BC |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6747 | if (G.prs.lex == BC_LEX_SCOLON |
| 6748 | || G.prs.lex == XC_LEX_NLINE |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6749 | ) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6750 | s = zxc_lex_next(); |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6751 | if (s) goto err; |
| 6752 | goto check_eof; |
| 6753 | } |
| 6754 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6755 | s = zbc_parse_stmt_or_funcdef(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6756 | if (s) goto err; |
| 6757 | |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6758 | // Check that next token is a correct stmt delimiter - |
| 6759 | // disallows "print 1 print 2" and such. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6760 | if (G.prs.lex != BC_LEX_SCOLON |
| 6761 | && G.prs.lex != XC_LEX_NLINE |
| 6762 | && G.prs.lex != XC_LEX_EOF |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6763 | ) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 6764 | bc_error_at("bad statement terminator"); |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6765 | goto err; |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6766 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6767 | // The above logic is fragile. Check these examples: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6768 | // - interactive read() still works |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6769 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6770 | } else { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6771 | #if ENABLE_DC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6772 | s = zdc_parse_expr(); |
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 | } |
| 6775 | if (s || G_interrupt) { |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6776 | err: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6777 | xc_parse_reset(); // includes xc_program_reset() |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6778 | RETURN_STATUS(BC_STATUS_FAILURE); |
| 6779 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6780 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 6781 | dbg_lex("%s:%d executing...", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6782 | s = zxc_program_exec(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6783 | if (s) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6784 | xc_program_reset(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6785 | break; |
| 6786 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6787 | |
| 6788 | ip = (void*)G.prog.exestack.v; |
| 6789 | #if SANITY_CHECKS |
| 6790 | if (G.prog.exestack.len != 1) // should have only main's IP |
| 6791 | bb_error_msg_and_die("BUG:call stack"); |
| 6792 | if (ip->func != BC_PROG_MAIN) |
| 6793 | bb_error_msg_and_die("BUG:not MAIN"); |
| 6794 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6795 | f = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6796 | // bc discards strings, constants and code after each |
| 6797 | // top-level statement in the "main program". |
| 6798 | // This prevents "yes 1 | bc" from growing its memory |
| 6799 | // without bound. This can be done because data stack |
| 6800 | // is empty and thus can't hold any references to |
| 6801 | // strings or constants, there is no generated code |
| 6802 | // which can hold references (after we discard one |
| 6803 | // we just executed). Code of functions can have references, |
| 6804 | // but bc stores function strings/constants in per-function |
| 6805 | // storage. |
| 6806 | if (IS_BC) { |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6807 | #if SANITY_CHECKS |
Denys Vlasenko | 7c1c9dc | 2018-12-22 14:18:47 +0100 | [diff] [blame] | 6808 | if (G.prog.results.len != 0) // should be empty |
| 6809 | bb_error_msg_and_die("BUG:data stack"); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6810 | #endif |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6811 | IF_BC(bc_vec_pop_all(&f->strs);) |
| 6812 | IF_BC(bc_vec_pop_all(&f->consts);) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6813 | } else { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6814 | if (G.prog.results.len == 0 |
| 6815 | && G.prog.vars.len == 0 |
| 6816 | ) { |
| 6817 | // If stack is empty and no registers exist (TODO: or they are all empty), |
| 6818 | // we can get rid of accumulated strings and constants. |
| 6819 | // In this example dc process should not grow |
| 6820 | // its memory consumption with time: |
| 6821 | // yes 1pc | dc |
| 6822 | IF_DC(bc_vec_pop_all(&G.prog.strs);) |
| 6823 | IF_DC(bc_vec_pop_all(&G.prog.consts);) |
| 6824 | } |
| 6825 | // The code is discarded always (below), thus this example |
| 6826 | // should also not grow its memory consumption with time, |
| 6827 | // even though its data stack is not empty: |
| 6828 | // { echo 1; yes dk; } | dc |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6829 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6830 | // We drop generated and executed code for both bc and dc: |
| 6831 | bc_vec_pop_all(&f->code); |
| 6832 | ip->inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6833 | } |
Denys Vlasenko | 6842c60 | 2019-01-04 05:41:47 +0100 | [diff] [blame^] | 6834 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6835 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6836 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6837 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6838 | #define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6839 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6840 | static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6841 | { |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6842 | // 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] | 6843 | // therefore we know G.prs.lex_filename == NULL on entry |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6844 | //const char *sv_file; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6845 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6846 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6847 | G.prs.lex_filename = filename; |
| 6848 | G.prs.lex_input_fp = fp; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 6849 | G.err_line = G.prs.lex_line = 1; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 6850 | dbg_lex("p->lex_line reset to 1"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6851 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6852 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6853 | s = zxc_vm_process(""); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6854 | // We do not stop looping on errors here if reading stdin. |
| 6855 | // Example: start interactive bc and enter "return". |
| 6856 | // It should say "'return' not in a function" |
| 6857 | // but should not exit. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6858 | } while (G.prs.lex_input_fp == stdin); |
| 6859 | G.prs.lex_filename = NULL; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6860 | RETURN_STATUS(s); |
| 6861 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6862 | #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] | 6863 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6864 | static BC_STATUS zxc_vm_file(const char *file) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6865 | { |
| 6866 | BcStatus s; |
| 6867 | FILE *fp; |
| 6868 | |
| 6869 | fp = xfopen_for_read(file); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6870 | s = zxc_vm_execute_FILE(fp, file); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6871 | fclose(fp); |
| 6872 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6873 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6874 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6875 | #define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6876 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6877 | #if ENABLE_BC |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 6878 | static void bc_vm_info(void) |
| 6879 | { |
| 6880 | printf("%s "BB_VER"\n" |
Gavin Howard | fa495ce | 2018-12-18 10:03:14 -0700 | [diff] [blame] | 6881 | "Adapted from https://github.com/gavinhoward/bc\n" |
| 6882 | "Original code (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 6883 | , applet_name); |
| 6884 | } |
| 6885 | |
| 6886 | static void bc_args(char **argv) |
| 6887 | { |
| 6888 | unsigned opts; |
| 6889 | int i; |
| 6890 | |
| 6891 | GETOPT_RESET(); |
| 6892 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
| 6893 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
| 6894 | "warn\0" No_argument "w" |
| 6895 | "version\0" No_argument "v" |
| 6896 | "standard\0" No_argument "s" |
| 6897 | "quiet\0" No_argument "q" |
| 6898 | "mathlib\0" No_argument "l" |
| 6899 | "interactive\0" No_argument "i" |
| 6900 | ); |
| 6901 | #else |
| 6902 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
| 6903 | #endif |
| 6904 | if (getenv("POSIXLY_CORRECT")) |
| 6905 | option_mask32 |= BC_FLAG_S; |
| 6906 | |
| 6907 | if (opts & BC_FLAG_V) { |
| 6908 | bc_vm_info(); |
| 6909 | exit(0); |
| 6910 | } |
| 6911 | |
| 6912 | for (i = optind; argv[i]; ++i) |
| 6913 | bc_vec_push(&G.files, argv + i); |
| 6914 | } |
| 6915 | |
| 6916 | static void bc_vm_envArgs(void) |
| 6917 | { |
| 6918 | BcVec v; |
| 6919 | char *buf; |
| 6920 | char *env_args = getenv("BC_ENV_ARGS"); |
| 6921 | |
| 6922 | if (!env_args) return; |
| 6923 | |
| 6924 | G.env_args = xstrdup(env_args); |
| 6925 | buf = G.env_args; |
| 6926 | |
| 6927 | bc_vec_init(&v, sizeof(char *), NULL); |
| 6928 | |
| 6929 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 6930 | bc_vec_push(&v, &buf); |
| 6931 | buf = skip_non_whitespace(buf); |
| 6932 | if (!*buf) |
| 6933 | break; |
| 6934 | *buf++ = '\0'; |
| 6935 | } |
| 6936 | |
| 6937 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 6938 | if (sizeof(int) == sizeof(char*)) { |
| 6939 | bc_vec_push(&v, &const_int_0); |
| 6940 | } else { |
| 6941 | static char *const nullptr = NULL; |
| 6942 | bc_vec_push(&v, &nullptr); |
| 6943 | } |
| 6944 | bc_args(((char **)v.v) - 1); |
| 6945 | |
| 6946 | bc_vec_free(&v); |
| 6947 | } |
| 6948 | |
| 6949 | static const char bc_lib[] ALIGN1 = { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6950 | "scale=20" |
| 6951 | "\n" "define e(x){" |
| 6952 | "\n" "auto b,s,n,r,d,i,p,f,v" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 6953 | ////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below |
| 6954 | //^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc: |
| 6955 | //e(-.998896): GNU:.36828580434569428695 |
| 6956 | // above code:.36828580434569428696 |
| 6957 | // actual value:.3682858043456942869594... |
| 6958 | // but for now let's be "GNU compatible" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6959 | "\n" "b=ibase" |
| 6960 | "\n" "ibase=A" |
| 6961 | "\n" "if(x<0){" |
| 6962 | "\n" "n=1" |
| 6963 | "\n" "x=-x" |
| 6964 | "\n" "}" |
| 6965 | "\n" "s=scale" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 6966 | "\n" "r=6+s+.44*x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6967 | "\n" "scale=scale(x)+1" |
| 6968 | "\n" "while(x>1){" |
| 6969 | "\n" "d+=1" |
| 6970 | "\n" "x/=2" |
| 6971 | "\n" "scale+=1" |
| 6972 | "\n" "}" |
| 6973 | "\n" "scale=r" |
| 6974 | "\n" "r=x+1" |
| 6975 | "\n" "p=x" |
| 6976 | "\n" "f=v=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6977 | "\n" "for(i=2;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6978 | "\n" "p*=x" |
| 6979 | "\n" "f*=i" |
| 6980 | "\n" "v=p/f" |
| 6981 | "\n" "r+=v" |
| 6982 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6983 | "\n" "while(d--)r*=r" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6984 | "\n" "scale=s" |
| 6985 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6986 | "\n" "if(n)return(1/r)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6987 | "\n" "return(r/1)" |
| 6988 | "\n" "}" |
| 6989 | "\n" "define l(x){" |
| 6990 | "\n" "auto b,s,r,p,a,q,i,v" |
| 6991 | "\n" "b=ibase" |
| 6992 | "\n" "ibase=A" |
| 6993 | "\n" "if(x<=0){" |
| 6994 | "\n" "r=(1-10^scale)/1" |
| 6995 | "\n" "ibase=b" |
| 6996 | "\n" "return(r)" |
| 6997 | "\n" "}" |
| 6998 | "\n" "s=scale" |
| 6999 | "\n" "scale+=6" |
| 7000 | "\n" "p=2" |
| 7001 | "\n" "while(x>=2){" |
| 7002 | "\n" "p*=2" |
| 7003 | "\n" "x=sqrt(x)" |
| 7004 | "\n" "}" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 7005 | "\n" "while(x<=.5){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7006 | "\n" "p*=2" |
| 7007 | "\n" "x=sqrt(x)" |
| 7008 | "\n" "}" |
| 7009 | "\n" "r=a=(x-1)/(x+1)" |
| 7010 | "\n" "q=a*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7011 | "\n" "v=1" |
| 7012 | "\n" "for(i=3;v;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7013 | "\n" "a*=q" |
| 7014 | "\n" "v=a/i" |
| 7015 | "\n" "r+=v" |
| 7016 | "\n" "}" |
| 7017 | "\n" "r*=p" |
| 7018 | "\n" "scale=s" |
| 7019 | "\n" "ibase=b" |
| 7020 | "\n" "return(r/1)" |
| 7021 | "\n" "}" |
| 7022 | "\n" "define s(x){" |
Denys Vlasenko | 240d7ee | 2018-12-14 11:27:09 +0100 | [diff] [blame] | 7023 | "\n" "auto b,s,r,a,q,i" |
| 7024 | "\n" "if(x<0)return(-s(-x))" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7025 | "\n" "b=ibase" |
| 7026 | "\n" "ibase=A" |
| 7027 | "\n" "s=scale" |
| 7028 | "\n" "scale=1.1*s+2" |
| 7029 | "\n" "a=a(1)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7030 | "\n" "scale=0" |
| 7031 | "\n" "q=(x/a+2)/4" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7032 | "\n" "x-=4*q*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7033 | "\n" "if(q%2)x=-x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7034 | "\n" "scale=s+2" |
| 7035 | "\n" "r=a=x" |
| 7036 | "\n" "q=-x*x" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7037 | "\n" "for(i=3;a;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7038 | "\n" "a*=q/(i*(i-1))" |
| 7039 | "\n" "r+=a" |
| 7040 | "\n" "}" |
| 7041 | "\n" "scale=s" |
| 7042 | "\n" "ibase=b" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7043 | "\n" "return(r/1)" |
| 7044 | "\n" "}" |
| 7045 | "\n" "define c(x){" |
| 7046 | "\n" "auto b,s" |
| 7047 | "\n" "b=ibase" |
| 7048 | "\n" "ibase=A" |
| 7049 | "\n" "s=scale" |
| 7050 | "\n" "scale*=1.2" |
| 7051 | "\n" "x=s(2*a(1)+x)" |
| 7052 | "\n" "scale=s" |
| 7053 | "\n" "ibase=b" |
| 7054 | "\n" "return(x/1)" |
| 7055 | "\n" "}" |
| 7056 | "\n" "define a(x){" |
| 7057 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7058 | "\n" "b=ibase" |
| 7059 | "\n" "ibase=A" |
| 7060 | "\n" "n=1" |
| 7061 | "\n" "if(x<0){" |
| 7062 | "\n" "n=-1" |
| 7063 | "\n" "x=-x" |
| 7064 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7065 | "\n" "if(scale<65){" |
| 7066 | "\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7067 | "\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7068 | "\n" "}" |
| 7069 | "\n" "s=scale" |
| 7070 | "\n" "if(x>.2){" |
| 7071 | "\n" "scale+=5" |
| 7072 | "\n" "a=a(.2)" |
| 7073 | "\n" "}" |
| 7074 | "\n" "scale=s+3" |
| 7075 | "\n" "while(x>.2){" |
| 7076 | "\n" "m+=1" |
| 7077 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7078 | "\n" "}" |
| 7079 | "\n" "r=u=x" |
| 7080 | "\n" "f=-x*x" |
| 7081 | "\n" "t=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7082 | "\n" "for(i=3;t;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7083 | "\n" "u*=f" |
| 7084 | "\n" "t=u/i" |
| 7085 | "\n" "r+=t" |
| 7086 | "\n" "}" |
| 7087 | "\n" "scale=s" |
| 7088 | "\n" "ibase=b" |
| 7089 | "\n" "return((m*a+r)/n)" |
| 7090 | "\n" "}" |
| 7091 | "\n" "define j(n,x){" |
| 7092 | "\n" "auto b,s,o,a,i,v,f" |
| 7093 | "\n" "b=ibase" |
| 7094 | "\n" "ibase=A" |
| 7095 | "\n" "s=scale" |
| 7096 | "\n" "scale=0" |
| 7097 | "\n" "n/=1" |
| 7098 | "\n" "if(n<0){" |
| 7099 | "\n" "n=-n" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7100 | "\n" "o=n%2" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7101 | "\n" "}" |
| 7102 | "\n" "a=1" |
| 7103 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7104 | "\n" "scale=1.5*s" |
| 7105 | "\n" "a=(x^n)/2^n/a" |
| 7106 | "\n" "r=v=1" |
| 7107 | "\n" "f=-x*x/4" |
Denys Vlasenko | c06537d | 2018-12-14 10:10:37 +0100 | [diff] [blame] | 7108 | "\n" "scale+=length(a)-scale(a)" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7109 | "\n" "for(i=1;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7110 | "\n" "v=v*f/i/(n+i)" |
| 7111 | "\n" "r+=v" |
| 7112 | "\n" "}" |
| 7113 | "\n" "scale=s" |
| 7114 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7115 | "\n" "if(o)a=-a" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7116 | "\n" "return(a*r/1)" |
| 7117 | "\n" "}" |
| 7118 | }; |
| 7119 | #endif // ENABLE_BC |
| 7120 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7121 | static BC_STATUS zxc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7122 | { |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7123 | char **fname; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7124 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7125 | size_t i; |
| 7126 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7127 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7128 | if (option_mask32 & BC_FLAG_L) { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7129 | // We know that internal library is not buggy, |
| 7130 | // thus error checking is normally disabled. |
| 7131 | # define DEBUG_LIB 0 |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7132 | s = zxc_vm_process(bc_lib); |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7133 | if (DEBUG_LIB && s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7134 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7135 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7136 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7137 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7138 | fname = (void*)G.files.v; |
| 7139 | for (i = 0; i < G.files.len; i++) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7140 | s = zxc_vm_file(*fname++); |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7141 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) { |
| 7142 | // Debug config, non-interactive mode: |
| 7143 | // return all the way back to main. |
| 7144 | // Non-debug builds do not come here |
| 7145 | // in non-interactive mode, they exit. |
| 7146 | RETURN_STATUS(s); |
| 7147 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7148 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7149 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7150 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7151 | s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7152 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7153 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7154 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7155 | #define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7156 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7157 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7158 | static void xc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7159 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7160 | bc_vec_free(&G.prog.fns); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7161 | IF_BC(bc_vec_free(&G.prog.fn_map);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7162 | bc_vec_free(&G.prog.vars); |
| 7163 | bc_vec_free(&G.prog.var_map); |
| 7164 | bc_vec_free(&G.prog.arrs); |
| 7165 | bc_vec_free(&G.prog.arr_map); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7166 | IF_DC(bc_vec_free(&G.prog.strs);) |
| 7167 | IF_DC(bc_vec_free(&G.prog.consts);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7168 | bc_vec_free(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7169 | bc_vec_free(&G.prog.exestack); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7170 | IF_BC(bc_num_free(&G.prog.last);) |
Denys Vlasenko | db8d607 | 2018-12-27 18:08:30 +0100 | [diff] [blame] | 7171 | //IF_BC(bc_num_free(&G.prog.zero);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7172 | IF_BC(bc_num_free(&G.prog.one);) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7173 | bc_vec_free(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7174 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7175 | #endif |
| 7176 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7177 | static void xc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7178 | { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7179 | BcInstPtr ip; |
| 7180 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7181 | // memset(&G.prog, 0, sizeof(G.prog)); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7182 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7183 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7184 | // G.prog.nchars = G.prog.scale = 0; - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7185 | G.prog.ib_t = 10; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7186 | G.prog.ob_t = 10; |
| 7187 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7188 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);) |
| 7189 | //IF_BC(bc_num_zero(&G.prog.last);) - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7190 | |
Denys Vlasenko | db8d607 | 2018-12-27 18:08:30 +0100 | [diff] [blame] | 7191 | //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7192 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7193 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7194 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);) |
| 7195 | IF_BC(bc_num_one(&G.prog.one);) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7196 | |
| 7197 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7198 | 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] | 7199 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7200 | if (IS_BC) { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7201 | // Names are chosen simply to be distinct and never match |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 7202 | // a valid function name (and be short) |
| 7203 | IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7204 | IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read() |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7205 | } else { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7206 | // in dc, functions have no names |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7207 | xc_program_add_fn(); |
| 7208 | xc_program_add_fn(); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7209 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7210 | |
| 7211 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7212 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7213 | |
| 7214 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7215 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7216 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7217 | IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);) |
| 7218 | 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] | 7219 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7220 | bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL); |
| 7221 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7222 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7223 | bc_char_vec_init(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7224 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7225 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7226 | static int xc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7227 | { |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7228 | G.prog.len = xc_vm_envLen(env_len); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7229 | #if ENABLE_FEATURE_EDITING |
| 7230 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7231 | #endif |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7232 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7233 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7234 | xc_program_init(); |
Denys Vlasenko | 5f263f4 | 2018-12-13 22:49:59 +0100 | [diff] [blame] | 7235 | IF_BC(if (IS_BC) bc_vm_envArgs();) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7236 | xc_parse_create(BC_PROG_MAIN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7237 | |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7238 | //TODO: in GNU bc, the check is (isatty(0) && isatty(1)), |
| 7239 | //-i option unconditionally enables this regardless of isatty(): |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7240 | if (isatty(0)) { |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 7241 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7242 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7243 | // With SA_RESTART, most system calls will restart |
| 7244 | // (IOW: they won't fail with EINTR). |
| 7245 | // In particular, this means ^C won't cause |
| 7246 | // stdout to get into "error state" if SIGINT hits |
| 7247 | // within write() syscall. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7248 | // |
| 7249 | // The downside is that ^C while tty input is taken |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7250 | // will only be handled after [Enter] since read() |
| 7251 | // from stdin is not interrupted by ^C either, |
| 7252 | // it restarts, thus fgetc() does not return on ^C. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7253 | // (This problem manifests only if line editing is disabled) |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7254 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7255 | |
| 7256 | // Without SA_RESTART, this exhibits a bug: |
| 7257 | // "while (1) print 1" and try ^C-ing it. |
| 7258 | // Intermittently, instead of returning to input line, |
| 7259 | // you'll get "output error: Interrupted system call" |
| 7260 | // and exit. |
| 7261 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7262 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7263 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7264 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7265 | return 0; // "not a tty" |
| 7266 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7267 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7268 | static BcStatus xc_vm_run(void) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7269 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7270 | BcStatus st = zxc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7271 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7272 | if (G_exiting) // it was actually "halt" or "quit" |
| 7273 | st = EXIT_SUCCESS; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7274 | |
| 7275 | bc_vec_free(&G.files); |
| 7276 | xc_program_free(); |
| 7277 | xc_parse_free(); |
| 7278 | free(G.env_args); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7279 | # if ENABLE_FEATURE_EDITING |
| 7280 | free_line_input_t(G.line_input_state); |
| 7281 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7282 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7283 | #endif |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7284 | dbg_exec("exiting with exitcode %d", st); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7285 | return st; |
| 7286 | } |
| 7287 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7288 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7289 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7290 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7291 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7292 | int is_tty; |
| 7293 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7294 | INIT_G(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7295 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7296 | is_tty = xc_vm_init("BC_LINE_LENGTH"); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7297 | |
| 7298 | bc_args(argv); |
| 7299 | |
| 7300 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7301 | bc_vm_info(); |
| 7302 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7303 | return xc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7304 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7305 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7306 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7307 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7308 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7309 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7310 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7311 | int noscript; |
| 7312 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7313 | INIT_G(); |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7314 | |
| 7315 | // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7316 | // 1 char wider than bc from the same package. |
| 7317 | // Both default width, and xC_LINE_LENGTH=N are wider: |
| 7318 | // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7319 | // |1234\ | |
| 7320 | // |56 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7321 | // "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7322 | // |123\ | |
| 7323 | // |456 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7324 | // Do the same, or it's a bug? |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7325 | xc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7326 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7327 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7328 | noscript = BC_FLAG_I; |
| 7329 | for (;;) { |
| 7330 | int n = getopt(argc, argv, "e:f:x"); |
| 7331 | if (n <= 0) |
| 7332 | break; |
| 7333 | switch (n) { |
| 7334 | case 'e': |
| 7335 | noscript = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7336 | n = zxc_vm_process(optarg); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7337 | if (n) return n; |
| 7338 | break; |
| 7339 | case 'f': |
| 7340 | noscript = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7341 | n = zxc_vm_file(optarg); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7342 | if (n) return n; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7343 | break; |
| 7344 | case 'x': |
| 7345 | option_mask32 |= DC_FLAG_X; |
| 7346 | break; |
| 7347 | default: |
| 7348 | bb_show_usage(); |
| 7349 | } |
| 7350 | } |
| 7351 | argv += optind; |
| 7352 | |
| 7353 | while (*argv) { |
| 7354 | noscript = 0; |
| 7355 | bc_vec_push(&G.files, argv++); |
| 7356 | } |
| 7357 | |
| 7358 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7359 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7360 | return xc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7361 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7362 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7363 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 7364 | #endif // DC_BIG |
| 7365 | |