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; |
| 381 | IF_BC(size_t results_len_before_call;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 382 | } BcInstPtr; |
| 383 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 384 | typedef enum BcLexType { |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 385 | XC_LEX_EOF, |
| 386 | XC_LEX_INVALID, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 387 | |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 388 | XC_LEX_NLINE, |
| 389 | XC_LEX_WHITESPACE, |
| 390 | XC_LEX_STR, |
| 391 | XC_LEX_NAME, |
| 392 | XC_LEX_NUMBER, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 393 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 394 | XC_LEX_1st_op, |
| 395 | XC_LEX_NEG = XC_LEX_1st_op, // order |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 396 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 397 | XC_LEX_OP_REL_EQ, // should |
| 398 | XC_LEX_OP_REL_LE, // match |
| 399 | XC_LEX_OP_REL_GE, // INST |
| 400 | XC_LEX_OP_REL_NE, // constants |
| 401 | XC_LEX_OP_REL_LT, // for |
| 402 | XC_LEX_OP_REL_GT, // these |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 403 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 404 | XC_LEX_OP_POWER, // operations |
| 405 | XC_LEX_OP_MULTIPLY, // | |
| 406 | XC_LEX_OP_DIVIDE, // | |
| 407 | XC_LEX_OP_MODULUS, // | |
| 408 | XC_LEX_OP_PLUS, // | |
| 409 | XC_LEX_OP_MINUS, // | |
| 410 | XC_LEX_OP_last = XC_LEX_OP_MINUS, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 411 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 412 | BC_LEX_OP_BOOL_NOT, // | |
| 413 | BC_LEX_OP_BOOL_OR, // | |
| 414 | BC_LEX_OP_BOOL_AND, // | |
| 415 | |
| 416 | BC_LEX_OP_ASSIGN_POWER, // | |
| 417 | BC_LEX_OP_ASSIGN_MULTIPLY, // | |
| 418 | BC_LEX_OP_ASSIGN_DIVIDE, // | |
| 419 | BC_LEX_OP_ASSIGN_MODULUS, // | |
| 420 | BC_LEX_OP_ASSIGN_PLUS, // | |
| 421 | BC_LEX_OP_ASSIGN_MINUS, // | |
| 422 | |
| 423 | BC_LEX_OP_ASSIGN, // V |
| 424 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 425 | BC_LEX_OP_INC, |
| 426 | BC_LEX_OP_DEC, |
| 427 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 428 | BC_LEX_LPAREN, // () are 0x28 and 0x29 |
| 429 | 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] | 430 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 431 | BC_LEX_LBRACKET, // [] are 0x5B and 0x5D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 432 | BC_LEX_COMMA, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 433 | 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] | 434 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 435 | BC_LEX_LBRACE, // {} are 0x7B and 0x7D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 436 | BC_LEX_SCOLON, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 437 | 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] | 438 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 439 | BC_LEX_KEY_1st_keyword, |
| 440 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 441 | BC_LEX_KEY_BREAK, |
| 442 | BC_LEX_KEY_CONTINUE, |
| 443 | BC_LEX_KEY_DEFINE, |
| 444 | BC_LEX_KEY_ELSE, |
| 445 | BC_LEX_KEY_FOR, |
| 446 | BC_LEX_KEY_HALT, |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 447 | // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct, |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 448 | BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE |
| 449 | BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 450 | BC_LEX_KEY_IF, |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 451 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 452 | BC_LEX_KEY_LENGTH, |
| 453 | BC_LEX_KEY_LIMITS, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 454 | BC_LEX_KEY_PRINT, |
| 455 | BC_LEX_KEY_QUIT, |
| 456 | BC_LEX_KEY_READ, |
| 457 | BC_LEX_KEY_RETURN, |
| 458 | BC_LEX_KEY_SCALE, |
| 459 | BC_LEX_KEY_SQRT, |
| 460 | BC_LEX_KEY_WHILE, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 461 | #endif // ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 462 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 463 | #if ENABLE_DC |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 464 | DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1, |
| 465 | DC_LEX_OP_ASSIGN, |
| 466 | |
| 467 | DC_LEX_LPAREN, |
| 468 | DC_LEX_SCOLON, |
| 469 | DC_LEX_READ, |
| 470 | DC_LEX_IBASE, |
| 471 | DC_LEX_SCALE, |
| 472 | DC_LEX_OBASE, |
| 473 | DC_LEX_LENGTH, |
| 474 | DC_LEX_PRINT, |
| 475 | DC_LEX_QUIT, |
| 476 | DC_LEX_SQRT, |
| 477 | DC_LEX_LBRACE, |
| 478 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 479 | DC_LEX_EQ_NO_REG, |
| 480 | DC_LEX_OP_MODEXP, |
| 481 | DC_LEX_OP_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 482 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 483 | DC_LEX_COLON, |
| 484 | DC_LEX_ELSE, |
| 485 | DC_LEX_EXECUTE, |
| 486 | DC_LEX_PRINT_STACK, |
| 487 | DC_LEX_CLEAR_STACK, |
| 488 | DC_LEX_STACK_LEVEL, |
| 489 | DC_LEX_DUPLICATE, |
| 490 | DC_LEX_SWAP, |
| 491 | DC_LEX_POP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 492 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 493 | DC_LEX_ASCIIFY, |
| 494 | DC_LEX_PRINT_STREAM, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 495 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 496 | // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct, |
| 497 | DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE |
| 498 | DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE |
| 499 | DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE |
| 500 | DC_LEX_LOAD, |
| 501 | DC_LEX_LOAD_POP, |
| 502 | DC_LEX_STORE_PUSH, |
| 503 | DC_LEX_PRINT_POP, |
| 504 | DC_LEX_NQUIT, |
| 505 | DC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 506 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 507 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 508 | // must match order of BC_LEX_KEY_foo etc above |
| 509 | #if ENABLE_BC |
| 510 | struct BcLexKeyword { |
| 511 | char name8[8]; |
| 512 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 513 | #define LEX_KW_ENTRY(a, b) \ |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 514 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 515 | static const struct BcLexKeyword bc_lex_kws[20] = { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 516 | LEX_KW_ENTRY("auto" , 1), // 0 |
| 517 | LEX_KW_ENTRY("break" , 1), // 1 |
| 518 | LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 519 | LEX_KW_ENTRY("define" , 1), // 3 |
| 520 | LEX_KW_ENTRY("else" , 0), // 4 |
| 521 | LEX_KW_ENTRY("for" , 1), // 5 |
| 522 | LEX_KW_ENTRY("halt" , 0), // 6 |
| 523 | LEX_KW_ENTRY("ibase" , 1), // 7 |
| 524 | LEX_KW_ENTRY("obase" , 1), // 8 |
| 525 | LEX_KW_ENTRY("if" , 1), // 9 |
| 526 | LEX_KW_ENTRY("last" , 0), // 10 |
| 527 | LEX_KW_ENTRY("length" , 1), // 11 |
| 528 | LEX_KW_ENTRY("limits" , 0), // 12 |
| 529 | LEX_KW_ENTRY("print" , 0), // 13 |
| 530 | LEX_KW_ENTRY("quit" , 1), // 14 |
| 531 | LEX_KW_ENTRY("read" , 0), // 15 |
| 532 | LEX_KW_ENTRY("return" , 1), // 16 |
| 533 | LEX_KW_ENTRY("scale" , 1), // 17 |
| 534 | LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 535 | LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 536 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 537 | #undef LEX_KW_ENTRY |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 538 | #define STRING_else (bc_lex_kws[4].name8) |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 539 | #define STRING_for (bc_lex_kws[5].name8) |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 540 | #define STRING_if (bc_lex_kws[9].name8) |
| 541 | #define STRING_while (bc_lex_kws[19].name8) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 542 | enum { |
| 543 | POSIX_KWORD_MASK = 0 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 544 | | (1 << 0) // 0 |
| 545 | | (1 << 1) // 1 |
| 546 | | (0 << 2) // 2 |
| 547 | | (1 << 3) // 3 |
| 548 | | (0 << 4) // 4 |
| 549 | | (1 << 5) // 5 |
| 550 | | (0 << 6) // 6 |
| 551 | | (1 << 7) // 7 |
| 552 | | (1 << 8) // 8 |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 553 | | (1 << 9) // 9 |
| 554 | | (0 << 10) // 10 |
| 555 | | (1 << 11) // 11 |
| 556 | | (0 << 12) // 12 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 557 | | (0 << 13) // 13 |
| 558 | | (1 << 14) // 14 |
| 559 | | (0 << 15) // 15 |
| 560 | | (1 << 16) // 16 |
| 561 | | (1 << 17) // 17 |
| 562 | | (1 << 18) // 18 |
| 563 | | (1 << 19) // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 564 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 565 | #define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 566 | |
| 567 | // This is a bit array that corresponds to token types. An entry is |
| 568 | // true if the token is valid in an expression, false otherwise. |
| 569 | // Used to figure out when expr parsing should stop *without error message* |
| 570 | // - 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] | 571 | // 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] | 572 | // by later processing. |
| 573 | enum { |
| 574 | #define EXBITS(a,b,c,d,e,f,g,h) \ |
| 575 | ((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] | 576 | BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz: |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 577 | + (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] | 578 | + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ * |
| 579 | + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^= |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 580 | + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ -- |
| 581 | + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; } |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 582 | + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase |
| 583 | + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read |
| 584 | + (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] | 585 | #undef EXBITS |
| 586 | }; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 587 | static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 588 | { |
| 589 | #if ULONG_MAX > 0xffffffff |
| 590 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 591 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 592 | #else |
| 593 | // 32-bit version |
| 594 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 595 | if (i >= 32) { |
| 596 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 597 | i &= 31; |
| 598 | } |
| 599 | return m & (1UL << i); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 600 | #endif |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | // This is an array of data for operators that correspond to |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 604 | // [XC_LEX_1st_op...] token types. |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 605 | static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 606 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 607 | OP(1, false), // neg |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 608 | 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] | 609 | OP(2, false), // pow |
| 610 | OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod |
| 611 | OP(4, true ), OP( 4, true ), // + - |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 612 | OP(1, false), // not |
| 613 | OP(7, true ), OP( 7, true ), // or and |
| 614 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 615 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 616 | OP(0, false), OP( 0, false ), // inc dec |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 617 | #undef OP |
| 618 | }; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 619 | #define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f) |
| 620 | #define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 621 | #endif // ENABLE_BC |
| 622 | |
| 623 | #if ENABLE_DC |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 624 | static const //BcLexType - should be this type |
| 625 | uint8_t |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 626 | dc_char_to_LEX[] ALIGN1 = { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 627 | // %&'( |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 628 | 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] | 629 | // )*+, |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 630 | 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] | 631 | // -./ |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 632 | XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 633 | // 0123456789 |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 634 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 635 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 636 | XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 637 | // :;<=>?@ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 638 | DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ, |
| 639 | XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 640 | // ABCDEFGH |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 641 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 642 | 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] | 643 | // IJKLMNOP |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 644 | DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP, |
| 645 | 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] | 646 | // QRSTUVWXY |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 647 | DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID, |
| 648 | 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] | 649 | // Z[\] |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 650 | DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 651 | // ^_` |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 652 | XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 653 | // abcdefgh |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 654 | DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE, |
| 655 | 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] | 656 | // ijklmnop |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 657 | 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] | 658 | 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] | 659 | // qrstuvwx |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 660 | DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID, |
| 661 | XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 662 | // yz |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 663 | XC_LEX_INVALID, DC_LEX_STACK_LEVEL, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 664 | // {|}~ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 665 | 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] | 666 | }; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 667 | 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] | 668 | int8_t |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 669 | dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz: |
| 670 | XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY |
| 671 | XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS |
| 672 | 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] | 673 | XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT |
| 674 | DC_INST_INVALID, // DC_LEX_OP_ASSIGN |
| 675 | XC_INST_REL_GT, // DC_LEX_LPAREN |
| 676 | DC_INST_INVALID, // DC_LEX_SCOLON |
| 677 | DC_INST_INVALID, // DC_LEX_READ |
| 678 | XC_INST_IBASE, // DC_LEX_IBASE |
| 679 | XC_INST_SCALE, // DC_LEX_SCALE |
| 680 | XC_INST_OBASE, // DC_LEX_OBASE |
| 681 | XC_INST_LENGTH, // DC_LEX_LENGTH |
| 682 | XC_INST_PRINT, // DC_LEX_PRINT |
| 683 | DC_INST_QUIT, // DC_LEX_QUIT |
| 684 | XC_INST_SQRT, // DC_LEX_SQRT |
| 685 | XC_INST_REL_GE, // DC_LEX_LBRACE |
| 686 | XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 687 | DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD |
| 688 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE |
| 689 | DC_INST_EXECUTE, // DC_LEX_EXECUTE |
| 690 | DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK |
| 691 | DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE |
| 692 | DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP |
| 693 | DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM |
| 694 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE |
| 695 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD |
| 696 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH |
| 697 | XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT |
| 698 | XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 699 | // DC_INST_INVALID in this table either means that corresponding LEX |
| 700 | // is not possible for dc, or that it does not compile one-to-one |
| 701 | // to a single INST. |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 702 | }; |
| 703 | #endif // ENABLE_DC |
| 704 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 705 | typedef struct BcParse { |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 706 | smallint lex; // was BcLexType // first member is most used |
| 707 | smallint lex_last; // was BcLexType |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 708 | size_t lex_line; |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 709 | const char *lex_inbuf; |
| 710 | 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] | 711 | const char *lex_filename; |
| 712 | FILE *lex_input_fp; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 713 | BcVec lex_strnumbuf; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 714 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 715 | BcFunc *func; |
| 716 | size_t fidx; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 717 | IF_BC(size_t in_funcdef;) |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 718 | IF_BC(BcVec exits;) |
| 719 | IF_BC(BcVec conds;) |
| 720 | IF_BC(BcVec ops;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 721 | } BcParse; |
| 722 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 723 | typedef struct BcProgram { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 724 | size_t len; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 725 | size_t nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 726 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 727 | size_t scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 728 | size_t ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 729 | size_t ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 730 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 731 | BcVec results; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 732 | BcVec exestack; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 733 | |
| 734 | BcVec fns; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 735 | IF_BC(BcVec fn_map;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 736 | |
| 737 | BcVec vars; |
| 738 | BcVec var_map; |
| 739 | |
| 740 | BcVec arrs; |
| 741 | BcVec arr_map; |
| 742 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 743 | IF_DC(BcVec strs;) |
| 744 | IF_DC(BcVec consts;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 745 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 746 | BcNum zero; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 747 | IF_BC(BcNum one;) |
| 748 | IF_BC(BcNum last;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 749 | } BcProgram; |
| 750 | |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 751 | struct globals { |
| 752 | BcParse prs; // first member is most used |
| 753 | |
| 754 | // For error messages. Can be set to current parsed line, |
| 755 | // or [TODO] to current executing line (can be before last parsed one) |
| 756 | unsigned err_line; |
| 757 | |
| 758 | BcVec input_buffer; |
| 759 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 760 | IF_FEATURE_BC_INTERACTIVE(smallint ttyin;) |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 761 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
| 762 | |
| 763 | BcProgram prog; |
| 764 | |
| 765 | BcVec files; |
| 766 | |
| 767 | char *env_args; |
| 768 | |
| 769 | #if ENABLE_FEATURE_EDITING |
| 770 | line_input_t *line_input_state; |
| 771 | #endif |
| 772 | } FIX_ALIASING; |
| 773 | #define G (*ptr_to_globals) |
| 774 | #define INIT_G() do { \ |
| 775 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 776 | } while (0) |
| 777 | #define FREE_G() do { \ |
| 778 | FREE_PTR_TO_GLOBALS(); \ |
| 779 | } while (0) |
| 780 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 781 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
| 782 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 783 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 784 | # define G_interrupt bb_got_signal |
| 785 | # define G_ttyin G.ttyin |
| 786 | #else |
| 787 | # define G_interrupt 0 |
| 788 | # define G_ttyin 0 |
| 789 | #endif |
| 790 | #if ENABLE_FEATURE_CLEAN_UP |
| 791 | # define G_exiting G.exiting |
| 792 | #else |
| 793 | # define G_exiting 0 |
| 794 | #endif |
| 795 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
| 796 | #define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b')) |
| 797 | |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 798 | #if ENABLE_BC |
| 799 | # define BC_PARSE_REL (1 << 0) |
| 800 | # define BC_PARSE_PRINT (1 << 1) |
| 801 | # define BC_PARSE_ARRAY (1 << 2) |
| 802 | # define BC_PARSE_NOCALL (1 << 3) |
| 803 | #endif |
| 804 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 805 | #define BC_PROG_MAIN 0 |
| 806 | #define BC_PROG_READ 1 |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 807 | #if ENABLE_DC |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 808 | #define BC_PROG_REQ_FUNCS 2 |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 809 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 810 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 811 | #define BC_FLAG_W (1 << 0) |
| 812 | #define BC_FLAG_V (1 << 1) |
| 813 | #define BC_FLAG_S (1 << 2) |
| 814 | #define BC_FLAG_Q (1 << 3) |
| 815 | #define BC_FLAG_L (1 << 4) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 816 | #define BC_FLAG_I ((1 << 5) * ENABLE_DC) |
| 817 | #define DC_FLAG_X ((1 << 6) * ENABLE_DC) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 818 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 819 | #define BC_MAX_OBASE ((unsigned) 999) |
| 820 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 821 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 822 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 823 | #define BC_MAX_NUM BC_MAX_STRING |
| 824 | // Unused apart from "limits" message. Just show a "biggish number" there. |
| 825 | //#define BC_MAX_NAME BC_MAX_STRING |
| 826 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 827 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 828 | #define BC_MAX_NAME_STR "999999999" |
| 829 | #define BC_MAX_EXP_STR "999999999" |
| 830 | #define BC_MAX_VARS_STR "999999999" |
| 831 | |
| 832 | #define BC_MAX_OBASE_STR "999" |
| 833 | |
| 834 | #if INT_MAX == 2147483647 |
| 835 | # define BC_MAX_DIM_STR "2147483647" |
| 836 | #elif INT_MAX == 9223372036854775807 |
| 837 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 838 | #else |
| 839 | # error Strange INT_MAX |
| 840 | #endif |
| 841 | |
| 842 | #if UINT_MAX == 4294967295 |
| 843 | # define BC_MAX_SCALE_STR "4294967295" |
| 844 | # define BC_MAX_STRING_STR "4294967294" |
| 845 | #elif UINT_MAX == 18446744073709551615 |
| 846 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 847 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 848 | #else |
| 849 | # error Strange UINT_MAX |
| 850 | #endif |
| 851 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 852 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 853 | // In configurations where errors abort instead of propagating error |
| 854 | // return code up the call chain, functions returning BC_STATUS |
| 855 | // actually don't return anything, they always succeed and return "void". |
| 856 | // A macro wrapper is provided, which makes this statement work: |
| 857 | // s = zbc_func(...) |
| 858 | // and makes it visible to the compiler that s is always zero, |
| 859 | // allowing compiler to optimize dead code after the statement. |
| 860 | // |
| 861 | // To make code more readable, each such function has a "z" |
| 862 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. |
| 863 | // |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 864 | #if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 865 | # define ERRORS_ARE_FATAL 0 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 866 | # define ERRORFUNC /*nothing*/ |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 867 | # define IF_ERROR_RETURN_POSSIBLE(a) a |
| 868 | # define BC_STATUS BcStatus |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 869 | # define RETURN_STATUS(v) return (v) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 870 | # define COMMA_SUCCESS /*nothing*/ |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 871 | #else |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 872 | # define ERRORS_ARE_FATAL 1 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 873 | # define ERRORFUNC NORETURN |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 874 | # define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/ |
| 875 | # define BC_STATUS void |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 876 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 877 | # define COMMA_SUCCESS ,BC_STATUS_SUCCESS |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 878 | #endif |
| 879 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 880 | // |
| 881 | // Utility routines |
| 882 | // |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 883 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 884 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 885 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 886 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 887 | static void fflush_and_check(void) |
| 888 | { |
| 889 | fflush_all(); |
| 890 | if (ferror(stdout) || ferror(stderr)) |
| 891 | bb_perror_msg_and_die("output error"); |
| 892 | } |
| 893 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 894 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 895 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 896 | do { \ |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 897 | IF_FEATURE_BC_INTERACTIVE(G_ttyin = 0;) /* do not loop in main loop anymore */ \ |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 898 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 899 | return BC_STATUS_FAILURE; \ |
| 900 | } while (0) |
| 901 | #else |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 902 | static void quit(void) NORETURN; |
| 903 | static void quit(void) |
| 904 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 905 | if (ferror(stdin)) |
| 906 | bb_perror_msg_and_die("input error"); |
| 907 | fflush_and_check(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 908 | dbg_exec("quit(): exiting with exitcode SUCCESS"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 909 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 910 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 911 | #define QUIT_OR_RETURN_TO_MAIN quit() |
| 912 | #endif |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 913 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 914 | static void bc_verror_msg(const char *fmt, va_list p) |
| 915 | { |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 916 | const char *sv = sv; // for compiler |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 917 | if (G.prs.lex_filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 918 | sv = applet_name; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 919 | applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.lex_filename, G.err_line); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 920 | } |
| 921 | bb_verror_msg(fmt, p, NULL); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 922 | if (G.prs.lex_filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 923 | free((char*)applet_name); |
| 924 | applet_name = sv; |
| 925 | } |
| 926 | } |
| 927 | |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 928 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 929 | { |
| 930 | va_list p; |
| 931 | |
| 932 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 933 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 934 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 935 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 936 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
| 937 | IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE); |
| 938 | exit(1); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 941 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 942 | static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 943 | { |
| 944 | va_list p; |
| 945 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 946 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 947 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 948 | RETURN_STATUS(BC_STATUS_SUCCESS); // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 949 | |
| 950 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 951 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 952 | va_end(p); |
| 953 | |
| 954 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 955 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 956 | RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning |
| 957 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 958 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 959 | RETURN_STATUS(BC_STATUS_FAILURE); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 960 | exit(1); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 961 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 962 | #define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 963 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 964 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 965 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 966 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 967 | // function must not have caller-cleaned parameters on stack. |
| 968 | // Unfortunately, vararg function API does exactly that on most arches. |
| 969 | // Thus, use these shims for the cases when we have no vararg PARAMS: |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 970 | static ERRORFUNC int bc_error(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 971 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 972 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 973 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 974 | static ERRORFUNC int bc_error_at(const char *msg) |
| 975 | { |
| 976 | const char *err_at = G.prs.lex_next_at; |
| 977 | if (err_at) { |
| 978 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt( |
| 979 | "%s at '%.*s'", |
| 980 | msg, |
| 981 | (int)(strchrnul(err_at, '\n') - err_at), |
| 982 | err_at |
| 983 | ); |
| 984 | } |
| 985 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
| 986 | } |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 987 | static ERRORFUNC int bc_error_bad_character(char c) |
| 988 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 989 | if (!c) |
| 990 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 991 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 992 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 993 | static ERRORFUNC int bc_error_bad_function_definition(void) |
| 994 | { |
| 995 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad function definition"); |
| 996 | } |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 997 | static ERRORFUNC int bc_error_bad_expression(void) |
| 998 | { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 999 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad expression"); |
| 1000 | } |
| 1001 | static ERRORFUNC int bc_error_bad_assignment(void) |
| 1002 | { |
| 1003 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at( |
| 1004 | "bad assignment: left side must be variable or array element" |
| 1005 | ); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1006 | } |
| 1007 | static ERRORFUNC int bc_error_bad_token(void) |
| 1008 | { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 1009 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad token"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1010 | } |
| 1011 | static ERRORFUNC int bc_error_stack_has_too_few_elements(void) |
| 1012 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1013 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1014 | } |
| 1015 | static ERRORFUNC int bc_error_variable_is_wrong_type(void) |
| 1016 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1017 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1018 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1019 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1020 | static BC_STATUS zbc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1021 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1022 | RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg)); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1023 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1024 | #define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS) |
| 1025 | static BC_STATUS zbc_POSIX_does_not_allow(const char *msg) |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1026 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1027 | RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg)); |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1028 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1029 | #define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS) |
| 1030 | static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg) |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1031 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1032 | RETURN_STATUS(zbc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; this is bad:", msg)); |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1033 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1034 | #define zbc_POSIX_does_not_allow_bool_ops_this_is_bad(...) (zbc_POSIX_does_not_allow_bool_ops_this_is_bad(__VA_ARGS__) COMMA_SUCCESS) |
| 1035 | static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg) |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1036 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1037 | RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg)); |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1038 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1039 | #define zbc_POSIX_does_not_allow_empty_X_expression_in_for(...) (zbc_POSIX_does_not_allow_empty_X_expression_in_for(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1040 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1041 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1042 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1043 | { |
| 1044 | size_t cap = v->cap * 2; |
| 1045 | while (cap < v->len + n) cap *= 2; |
| 1046 | v->v = xrealloc(v->v, v->size * cap); |
| 1047 | v->cap = cap; |
| 1048 | } |
| 1049 | |
| 1050 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1051 | { |
| 1052 | v->size = esize; |
| 1053 | v->cap = BC_VEC_START_CAP; |
| 1054 | v->len = 0; |
| 1055 | v->dtor = dtor; |
| 1056 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1057 | } |
| 1058 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1059 | static void bc_char_vec_init(BcVec *v) |
| 1060 | { |
| 1061 | bc_vec_init(v, sizeof(char), NULL); |
| 1062 | } |
| 1063 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1064 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1065 | { |
| 1066 | if (v->cap < req) { |
| 1067 | v->v = xrealloc(v->v, v->size * req); |
| 1068 | v->cap = req; |
| 1069 | } |
| 1070 | } |
| 1071 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1072 | static void bc_vec_pop(BcVec *v) |
| 1073 | { |
| 1074 | v->len--; |
| 1075 | if (v->dtor) |
| 1076 | v->dtor(v->v + (v->size * v->len)); |
| 1077 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1078 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1079 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1080 | { |
| 1081 | if (!v->dtor) |
| 1082 | v->len -= n; |
| 1083 | else { |
| 1084 | size_t len = v->len - n; |
| 1085 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1086 | } |
| 1087 | } |
| 1088 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1089 | static void bc_vec_pop_all(BcVec *v) |
| 1090 | { |
| 1091 | bc_vec_npop(v, v->len); |
| 1092 | } |
| 1093 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1094 | static size_t bc_vec_push(BcVec *v, const void *data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1095 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1096 | size_t len = v->len; |
| 1097 | if (len >= v->cap) bc_vec_grow(v, 1); |
| 1098 | memmove(v->v + (v->size * len), data, v->size); |
| 1099 | v->len++; |
| 1100 | return len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1101 | } |
| 1102 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 1103 | // G.prog.results often needs "pop old operand, push result" idiom. |
| 1104 | // Can do this without a few extra ops |
| 1105 | static size_t bc_result_pop_and_push(const void *data) |
| 1106 | { |
| 1107 | BcVec *v = &G.prog.results; |
| 1108 | char *last; |
| 1109 | size_t len = v->len - 1; |
| 1110 | |
| 1111 | last = v->v + (v->size * len); |
| 1112 | if (v->dtor) |
| 1113 | v->dtor(last); |
| 1114 | memmove(last, data, v->size); |
| 1115 | return len; |
| 1116 | } |
| 1117 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1118 | static size_t bc_vec_pushByte(BcVec *v, char data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1119 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1120 | return bc_vec_push(v, &data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1121 | } |
| 1122 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1123 | static size_t bc_vec_pushZeroByte(BcVec *v) |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1124 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1125 | //return bc_vec_pushByte(v, '\0'); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1126 | // better: |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1127 | return bc_vec_push(v, &const_int_0); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1130 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1131 | { |
| 1132 | if (idx == v->len) |
| 1133 | bc_vec_push(v, data); |
| 1134 | else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1135 | char *ptr; |
| 1136 | |
| 1137 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1138 | |
| 1139 | ptr = v->v + v->size * idx; |
| 1140 | |
| 1141 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1142 | memmove(ptr, data, v->size); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1147 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1148 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1149 | bc_vec_expand(v, len + 1); |
| 1150 | memcpy(v->v, str, len); |
| 1151 | v->len = len; |
| 1152 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1153 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1154 | } |
| 1155 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1156 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1157 | { |
| 1158 | return v->v + v->size * idx; |
| 1159 | } |
| 1160 | |
| 1161 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1162 | { |
| 1163 | return v->v + v->size * (v->len - idx - 1); |
| 1164 | } |
| 1165 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1166 | static void *bc_vec_top(const BcVec *v) |
| 1167 | { |
| 1168 | return v->v + v->size * (v->len - 1); |
| 1169 | } |
| 1170 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1171 | static FAST_FUNC void bc_vec_free(void *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1172 | { |
| 1173 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1174 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1175 | free(v->v); |
| 1176 | } |
| 1177 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1178 | static BcFunc* xc_program_func(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1179 | { |
| 1180 | return bc_vec_item(&G.prog.fns, idx); |
| 1181 | } |
| 1182 | // BC_PROG_MAIN is zeroth element, so: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1183 | #define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v)) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1184 | |
| 1185 | #if ENABLE_BC |
| 1186 | static BcFunc* bc_program_current_func(void) |
| 1187 | { |
| 1188 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1189 | BcFunc *func = xc_program_func(ip->func); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1190 | return func; |
| 1191 | } |
| 1192 | #endif |
| 1193 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1194 | static char** xc_program_str(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1195 | { |
| 1196 | #if ENABLE_BC |
| 1197 | if (IS_BC) { |
| 1198 | BcFunc *func = bc_program_current_func(); |
| 1199 | return bc_vec_item(&func->strs, idx); |
| 1200 | } |
| 1201 | #endif |
| 1202 | IF_DC(return bc_vec_item(&G.prog.strs, idx);) |
| 1203 | } |
| 1204 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1205 | static char** xc_program_const(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1206 | { |
| 1207 | #if ENABLE_BC |
| 1208 | if (IS_BC) { |
| 1209 | BcFunc *func = bc_program_current_func(); |
| 1210 | return bc_vec_item(&func->consts, idx); |
| 1211 | } |
| 1212 | #endif |
| 1213 | IF_DC(return bc_vec_item(&G.prog.consts, idx);) |
| 1214 | } |
| 1215 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1216 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1217 | { |
| 1218 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1219 | } |
| 1220 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1221 | static FAST_FUNC void bc_id_free(void *id) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1222 | { |
| 1223 | free(((BcId *) id)->name); |
| 1224 | } |
| 1225 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1226 | 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] | 1227 | { |
| 1228 | size_t low = 0, high = v->len; |
| 1229 | |
| 1230 | while (low < high) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1231 | size_t mid = (low + high) / 2; |
| 1232 | BcId *id = bc_vec_item(v, mid); |
| 1233 | int result = bc_id_cmp(ptr, id); |
| 1234 | |
| 1235 | if (result == 0) |
| 1236 | return mid; |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 1237 | if (result < 0) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1238 | high = mid; |
| 1239 | else |
| 1240 | low = mid + 1; |
| 1241 | } |
| 1242 | |
| 1243 | return low; |
| 1244 | } |
| 1245 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1246 | 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] | 1247 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1248 | size_t n = *i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1249 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1250 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1251 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1252 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1253 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1254 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1255 | bc_vec_pushAt(v, ptr, n); |
| 1256 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1257 | } |
| 1258 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1259 | #if ENABLE_BC |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1260 | 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] | 1261 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1262 | size_t i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1263 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1264 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1265 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1266 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1267 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1268 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1269 | { |
| 1270 | n->len = 0; |
| 1271 | n->neg = false; |
| 1272 | n->rdx = scale; |
| 1273 | } |
| 1274 | |
| 1275 | static void bc_num_zero(BcNum *n) |
| 1276 | { |
| 1277 | bc_num_setToZero(n, 0); |
| 1278 | } |
| 1279 | |
| 1280 | static void bc_num_one(BcNum *n) |
| 1281 | { |
| 1282 | bc_num_setToZero(n, 0); |
| 1283 | n->len = 1; |
| 1284 | n->num[0] = 1; |
| 1285 | } |
| 1286 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1287 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1288 | static void bc_num_init(BcNum *n, size_t req) |
| 1289 | { |
| 1290 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1291 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1292 | n->num = xmalloc(req); |
| 1293 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1294 | n->rdx = 0; |
| 1295 | n->len = 0; |
| 1296 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1297 | } |
| 1298 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1299 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1300 | { |
| 1301 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1302 | } |
| 1303 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1304 | static void bc_num_expand(BcNum *n, size_t req) |
| 1305 | { |
| 1306 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1307 | if (req > n->cap) { |
| 1308 | n->num = xrealloc(n->num, req); |
| 1309 | n->cap = req; |
| 1310 | } |
| 1311 | } |
| 1312 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1313 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1314 | { |
| 1315 | free(((BcNum *) num)->num); |
| 1316 | } |
| 1317 | |
| 1318 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1319 | { |
| 1320 | if (d != s) { |
| 1321 | bc_num_expand(d, s->cap); |
| 1322 | d->len = s->len; |
| 1323 | d->neg = s->neg; |
| 1324 | d->rdx = s->rdx; |
| 1325 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1326 | } |
| 1327 | } |
| 1328 | |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1329 | 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] | 1330 | { |
| 1331 | size_t i; |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1332 | unsigned long result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1333 | |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1334 | result = 0; |
| 1335 | i = n->len; |
| 1336 | while (i > n->rdx) { |
| 1337 | unsigned long prev = result; |
| 1338 | result = result * 10 + n->num[--i]; |
| 1339 | // Even overflowed N*10 can still satisfy N*10>=N. For example, |
| 1340 | // 0x1ff00000 * 10 is 0x13f600000, |
| 1341 | // or 0x3f600000 truncated to 32 bits. Which is larger. |
| 1342 | // However, (N*10)/8 < N check is always correct. |
| 1343 | if ((result / 8) < prev) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1344 | RETURN_STATUS(bc_error("overflow")); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1345 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1346 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1347 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1348 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1349 | } |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1350 | #define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS) |
| 1351 | |
| 1352 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
| 1353 | { |
| 1354 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
| 1355 | |
| 1356 | RETURN_STATUS(zbc_num_ulong_abs(n, result_p)); |
| 1357 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1358 | #define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1359 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1360 | #if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295 |
| 1361 | # define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE) |
| 1362 | #elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615 |
| 1363 | # define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE) |
| 1364 | #endif |
| 1365 | // minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num() |
| 1366 | // would not hit realloc() code path - not good if num[] is not malloced |
| 1367 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1368 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1369 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1370 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1371 | |
| 1372 | bc_num_zero(n); |
| 1373 | |
| 1374 | if (val == 0) return; |
| 1375 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1376 | bc_num_expand(n, ULONG_NUM_BUFSIZE); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1377 | |
| 1378 | ptr = n->num; |
| 1379 | for (;;) { |
| 1380 | n->len++; |
| 1381 | *ptr++ = val % 10; |
| 1382 | val /= 10; |
| 1383 | if (val == 0) break; |
| 1384 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1385 | } |
| 1386 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1387 | 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] | 1388 | { |
| 1389 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1390 | for (i = 0; i < len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1391 | a[i] -= b[i]; |
| 1392 | for (j = i; a[j] < 0;) { |
| 1393 | a[j++] += 10; |
| 1394 | a[j] -= 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1395 | } |
| 1396 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1400 | { |
Denys Vlasenko | 4113e1f | 2018-12-18 00:39:24 +0100 | [diff] [blame] | 1401 | size_t i = len; |
| 1402 | for (;;) { |
| 1403 | int c; |
| 1404 | if (i == 0) |
| 1405 | return 0; |
| 1406 | i--; |
| 1407 | c = a[i] - b[i]; |
| 1408 | if (c != 0) { |
| 1409 | i++; |
| 1410 | if (c < 0) |
| 1411 | return -i; |
| 1412 | return i; |
| 1413 | } |
| 1414 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1415 | } |
| 1416 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1417 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 1418 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 1419 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 1420 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 1421 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 1422 | { |
| 1423 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 1424 | } |
| 1425 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 1426 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 1427 | { |
| 1428 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 1429 | } |
| 1430 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1431 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1432 | { |
| 1433 | size_t i, min, a_int, b_int, diff; |
| 1434 | BcDig *max_num, *min_num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1435 | bool a_max, neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1436 | ssize_t cmp; |
| 1437 | |
| 1438 | if (a == b) return 0; |
| 1439 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1440 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1441 | |
| 1442 | if (a->neg != b->neg) // signs of a and b differ |
| 1443 | // +a,-b = a>b = 1 or -a,+b = a<b = -1 |
| 1444 | return (int)b->neg - (int)a->neg; |
| 1445 | neg = a->neg; // 1 if both negative, 0 if both positive |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1446 | |
| 1447 | a_int = BC_NUM_INT(a); |
| 1448 | b_int = BC_NUM_INT(b); |
| 1449 | a_int -= b_int; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1450 | |
| 1451 | if (a_int != 0) return (ssize_t) a_int; |
| 1452 | |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1453 | a_max = (a->rdx > b->rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1454 | if (a_max) { |
| 1455 | min = b->rdx; |
| 1456 | diff = a->rdx - b->rdx; |
| 1457 | max_num = a->num + diff; |
| 1458 | min_num = b->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1459 | // neg = (a_max == neg); - NOP (maps 1->1 and 0->0) |
| 1460 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1461 | min = a->rdx; |
| 1462 | diff = b->rdx - a->rdx; |
| 1463 | max_num = b->num + diff; |
| 1464 | min_num = a->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1465 | neg = !neg; // same as "neg = (a_max == neg)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1469 | if (cmp != 0) return BC_NUM_NEG(cmp, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1470 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1471 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1472 | if (max_num[i]) return BC_NUM_NEG(1, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1479 | { |
| 1480 | if (places == 0) return; |
| 1481 | |
| 1482 | n->rdx -= places; |
| 1483 | |
| 1484 | if (n->len != 0) { |
| 1485 | n->len -= places; |
| 1486 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | static void bc_num_extend(BcNum *n, size_t places) |
| 1491 | { |
| 1492 | size_t len = n->len + places; |
| 1493 | |
| 1494 | if (places != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1495 | if (n->cap < len) bc_num_expand(n, len); |
| 1496 | |
| 1497 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1498 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1499 | |
| 1500 | n->len += places; |
| 1501 | n->rdx += places; |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | static void bc_num_clean(BcNum *n) |
| 1506 | { |
| 1507 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1508 | if (n->len == 0) |
| 1509 | n->neg = false; |
| 1510 | else if (n->len < n->rdx) |
| 1511 | n->len = n->rdx; |
| 1512 | } |
| 1513 | |
| 1514 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1515 | { |
| 1516 | if (n->rdx < scale) |
| 1517 | bc_num_extend(n, scale - n->rdx); |
| 1518 | else |
| 1519 | bc_num_truncate(n, n->rdx - scale); |
| 1520 | |
| 1521 | bc_num_clean(n); |
| 1522 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1523 | } |
| 1524 | |
| 1525 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1526 | BcNum *restrict b) |
| 1527 | { |
| 1528 | if (idx < n->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1529 | b->len = n->len - idx; |
| 1530 | a->len = idx; |
| 1531 | a->rdx = b->rdx = 0; |
| 1532 | |
| 1533 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1534 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1535 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1536 | bc_num_zero(b); |
| 1537 | bc_num_copy(a, n); |
| 1538 | } |
| 1539 | |
| 1540 | bc_num_clean(a); |
| 1541 | bc_num_clean(b); |
| 1542 | } |
| 1543 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1544 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1545 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1546 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1547 | |
| 1548 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1549 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1550 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1551 | 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] | 1552 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1553 | |
| 1554 | if (n->rdx >= places) |
| 1555 | n->rdx -= places; |
| 1556 | else { |
| 1557 | bc_num_extend(n, places - n->rdx); |
| 1558 | n->rdx = 0; |
| 1559 | } |
| 1560 | |
| 1561 | bc_num_clean(n); |
| 1562 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1563 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1564 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1565 | #define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1566 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1567 | typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
| 1568 | |
| 1569 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 1570 | BcNumBinaryOp op, size_t req) |
| 1571 | { |
| 1572 | BcStatus s; |
| 1573 | BcNum num2, *ptr_a, *ptr_b; |
| 1574 | bool init = false; |
| 1575 | |
| 1576 | if (c == a) { |
| 1577 | ptr_a = &num2; |
| 1578 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 1579 | init = true; |
| 1580 | } else |
| 1581 | ptr_a = a; |
| 1582 | |
| 1583 | if (c == b) { |
| 1584 | ptr_b = &num2; |
| 1585 | if (c != a) { |
| 1586 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 1587 | init = true; |
| 1588 | } |
| 1589 | } else |
| 1590 | ptr_b = b; |
| 1591 | |
| 1592 | if (init) |
| 1593 | bc_num_init(c, req); |
| 1594 | else |
| 1595 | bc_num_expand(c, req); |
| 1596 | |
| 1597 | s = BC_STATUS_SUCCESS; |
| 1598 | IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale); |
| 1599 | |
| 1600 | if (init) bc_num_free(&num2); |
| 1601 | |
| 1602 | RETURN_STATUS(s); |
| 1603 | } |
| 1604 | #define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS) |
| 1605 | |
| 1606 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1607 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1608 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1609 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1610 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1611 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1612 | |
| 1613 | static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1614 | { |
| 1615 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s; |
| 1616 | (void) scale; |
| 1617 | RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b))); |
| 1618 | } |
| 1619 | |
| 1620 | static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1621 | { |
| 1622 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a; |
| 1623 | (void) scale; |
| 1624 | RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b))); |
| 1625 | } |
| 1626 | |
| 1627 | static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1628 | { |
| 1629 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1630 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req)); |
| 1631 | } |
| 1632 | |
| 1633 | static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1634 | { |
| 1635 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1636 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req)); |
| 1637 | } |
| 1638 | |
| 1639 | static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1640 | { |
| 1641 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1642 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req)); |
| 1643 | } |
| 1644 | |
| 1645 | static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1646 | { |
| 1647 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); |
| 1648 | } |
| 1649 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1650 | static const BcNumBinaryOp zxc_program_ops[] = { |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1651 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, |
| 1652 | }; |
| 1653 | #define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS) |
| 1654 | #define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS) |
| 1655 | #define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS) |
| 1656 | #define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS) |
| 1657 | #define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS) |
| 1658 | #define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS) |
| 1659 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1660 | 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] | 1661 | { |
| 1662 | BcNum one; |
| 1663 | BcDig num[2]; |
| 1664 | |
| 1665 | one.cap = 2; |
| 1666 | one.num = num; |
| 1667 | bc_num_one(&one); |
| 1668 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1669 | RETURN_STATUS(zbc_num_div(&one, a, b, scale)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1670 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1671 | #define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1672 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1673 | 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] | 1674 | { |
| 1675 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1676 | 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] | 1677 | unsigned carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1678 | |
| 1679 | // Because this function doesn't need to use scale (per the bc spec), |
| 1680 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1681 | |
| 1682 | if (a->len == 0) { |
| 1683 | bc_num_copy(c, b); |
| 1684 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1685 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1686 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1687 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1688 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1689 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | c->neg = a->neg; |
| 1693 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1694 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1695 | c->len = 0; |
| 1696 | |
| 1697 | if (a->rdx > b->rdx) { |
| 1698 | diff = a->rdx - b->rdx; |
| 1699 | ptr = a->num; |
| 1700 | ptr_a = a->num + diff; |
| 1701 | ptr_b = b->num; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1702 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1703 | diff = b->rdx - a->rdx; |
| 1704 | ptr = b->num; |
| 1705 | ptr_a = a->num; |
| 1706 | ptr_b = b->num + diff; |
| 1707 | } |
| 1708 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1709 | ptr_c = c->num; |
| 1710 | for (i = 0; i < diff; ++i, ++c->len) |
| 1711 | ptr_c[i] = ptr[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1712 | |
| 1713 | ptr_c += diff; |
| 1714 | a_int = BC_NUM_INT(a); |
| 1715 | b_int = BC_NUM_INT(b); |
| 1716 | |
| 1717 | if (a_int > b_int) { |
| 1718 | min_int = b_int; |
| 1719 | max = a_int; |
| 1720 | ptr = ptr_a; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1721 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1722 | min_int = a_int; |
| 1723 | max = b_int; |
| 1724 | ptr = ptr_b; |
| 1725 | } |
| 1726 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1727 | carry = 0; |
| 1728 | for (i = 0; i < min_rdx + min_int; ++i) { |
| 1729 | unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1730 | carry = in / 10; |
| 1731 | ptr_c[i] = (BcDig)(in % 10); |
| 1732 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1733 | for (; i < max + min_rdx; ++i) { |
| 1734 | unsigned in = (unsigned)ptr[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1735 | carry = in / 10; |
| 1736 | ptr_c[i] = (BcDig)(in % 10); |
| 1737 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1738 | c->len += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1739 | |
| 1740 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1741 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1742 | 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] | 1743 | } |
| 1744 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1745 | 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] | 1746 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1747 | ssize_t cmp; |
| 1748 | BcNum *minuend, *subtrahend; |
| 1749 | size_t start; |
| 1750 | bool aneg, bneg, neg; |
| 1751 | |
| 1752 | // Because this function doesn't need to use scale (per the bc spec), |
| 1753 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1754 | |
| 1755 | if (a->len == 0) { |
| 1756 | bc_num_copy(c, b); |
| 1757 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1758 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1759 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1760 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1761 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1762 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | aneg = a->neg; |
| 1766 | bneg = b->neg; |
| 1767 | a->neg = b->neg = false; |
| 1768 | |
| 1769 | cmp = bc_num_cmp(a, b); |
| 1770 | |
| 1771 | a->neg = aneg; |
| 1772 | b->neg = bneg; |
| 1773 | |
| 1774 | if (cmp == 0) { |
| 1775 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1776 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1777 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1778 | if (cmp > 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1779 | neg = a->neg; |
| 1780 | minuend = a; |
| 1781 | subtrahend = b; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1782 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1783 | neg = b->neg; |
| 1784 | if (sub) neg = !neg; |
| 1785 | minuend = b; |
| 1786 | subtrahend = a; |
| 1787 | } |
| 1788 | |
| 1789 | bc_num_copy(c, minuend); |
| 1790 | c->neg = neg; |
| 1791 | |
| 1792 | if (c->rdx < subtrahend->rdx) { |
| 1793 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1794 | start = 0; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1795 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1796 | start = c->rdx - subtrahend->rdx; |
| 1797 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1798 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1799 | |
| 1800 | bc_num_clean(c); |
| 1801 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1802 | 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] | 1803 | } |
| 1804 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1805 | 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] | 1806 | BcNum *restrict c) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1807 | #define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1808 | { |
| 1809 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1810 | 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] | 1811 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1812 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1813 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1814 | if (a->len == 0 || b->len == 0) { |
| 1815 | bc_num_zero(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1816 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1817 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1818 | aone = BC_NUM_ONE(a); |
| 1819 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1820 | bc_num_copy(c, aone ? b : a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1821 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1822 | } |
| 1823 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1824 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN |
| 1825 | || a->len < BC_NUM_KARATSUBA_LEN |
| 1826 | || b->len < BC_NUM_KARATSUBA_LEN |
| 1827 | ) { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1828 | size_t i, j, len; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1829 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1830 | bc_num_expand(c, a->len + b->len + 1); |
| 1831 | |
| 1832 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1833 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1834 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1835 | for (i = 0; i < b->len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1836 | unsigned carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1837 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1838 | unsigned in = c->num[i + j]; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1839 | in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1840 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1841 | carry = in / 10; |
| 1842 | c->num[i + j] = (BcDig)(in % 10); |
| 1843 | } |
| 1844 | |
| 1845 | c->num[i + j] += (BcDig) carry; |
| 1846 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1847 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 1848 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1849 | // a=2^1000000 |
| 1850 | // a*a <- without check below, this will not be interruptible |
| 1851 | if (G_interrupt) return BC_STATUS_FAILURE; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1852 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | c->len = len; |
| 1856 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1857 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | bc_num_init(&l1, max); |
| 1861 | bc_num_init(&h1, max); |
| 1862 | bc_num_init(&l2, max); |
| 1863 | bc_num_init(&h2, max); |
| 1864 | bc_num_init(&m1, max); |
| 1865 | bc_num_init(&m2, max); |
| 1866 | bc_num_init(&z0, max); |
| 1867 | bc_num_init(&z1, max); |
| 1868 | bc_num_init(&z2, max); |
| 1869 | bc_num_init(&temp, max + max); |
| 1870 | |
| 1871 | bc_num_split(a, max2, &l1, &h1); |
| 1872 | bc_num_split(b, max2, &l2, &h2); |
| 1873 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1874 | s = zbc_num_add(&h1, &l1, &m1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1875 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1876 | s = zbc_num_add(&h2, &l2, &m2, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1877 | if (s) goto err; |
| 1878 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1879 | s = zbc_num_k(&h1, &h2, &z0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1880 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1881 | s = zbc_num_k(&m1, &m2, &z1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1882 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1883 | s = zbc_num_k(&l1, &l2, &z2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1884 | if (s) goto err; |
| 1885 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1886 | s = zbc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1887 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1888 | s = zbc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1889 | if (s) goto err; |
| 1890 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1891 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1892 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1893 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1894 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1895 | s = zbc_num_add(&z0, &z1, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1896 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1897 | s = zbc_num_add(&temp, &z2, c, 0); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1898 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1899 | bc_num_free(&temp); |
| 1900 | bc_num_free(&z2); |
| 1901 | bc_num_free(&z1); |
| 1902 | bc_num_free(&z0); |
| 1903 | bc_num_free(&m2); |
| 1904 | bc_num_free(&m1); |
| 1905 | bc_num_free(&h2); |
| 1906 | bc_num_free(&l2); |
| 1907 | bc_num_free(&h1); |
| 1908 | bc_num_free(&l1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1909 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1910 | } |
| 1911 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1912 | 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] | 1913 | { |
| 1914 | BcStatus s; |
| 1915 | BcNum cpa, cpb; |
| 1916 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1917 | |
| 1918 | scale = BC_MAX(scale, a->rdx); |
| 1919 | scale = BC_MAX(scale, b->rdx); |
| 1920 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1921 | maxrdx = BC_MAX(maxrdx, scale); |
| 1922 | |
| 1923 | bc_num_init(&cpa, a->len); |
| 1924 | bc_num_init(&cpb, b->len); |
| 1925 | |
| 1926 | bc_num_copy(&cpa, a); |
| 1927 | bc_num_copy(&cpb, b); |
| 1928 | cpa.neg = cpb.neg = false; |
| 1929 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1930 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1931 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1932 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1933 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1934 | s = zbc_num_k(&cpa, &cpb, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1935 | if (s) goto err; |
| 1936 | |
| 1937 | maxrdx += scale; |
| 1938 | bc_num_expand(c, c->len + maxrdx); |
| 1939 | |
| 1940 | if (c->len < maxrdx) { |
| 1941 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 1942 | c->len += maxrdx; |
| 1943 | } |
| 1944 | |
| 1945 | c->rdx = maxrdx; |
| 1946 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1947 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1948 | bc_num_free(&cpb); |
| 1949 | bc_num_free(&cpa); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1950 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1951 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1952 | #define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1953 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1954 | 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] | 1955 | { |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 1956 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1957 | size_t len, end, i; |
| 1958 | BcNum cp; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1959 | |
| 1960 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1961 | RETURN_STATUS(bc_error("divide by zero")); |
| 1962 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1963 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1964 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1965 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1966 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1967 | bc_num_copy(c, a); |
| 1968 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1969 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 1973 | bc_num_copy(&cp, a); |
| 1974 | len = b->len; |
| 1975 | |
| 1976 | if (len > cp.len) { |
| 1977 | bc_num_expand(&cp, len + 2); |
| 1978 | bc_num_extend(&cp, len - cp.len); |
| 1979 | } |
| 1980 | |
| 1981 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 1982 | cp.rdx -= b->rdx; |
| 1983 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 1984 | |
| 1985 | if (b->rdx == b->len) { |
Denys Vlasenko | 71c82d1 | 2018-12-18 12:43:21 +0100 | [diff] [blame] | 1986 | for (;;) { |
| 1987 | if (len == 0) break; |
| 1988 | len--; |
| 1989 | if (b->num[len] != 0) |
| 1990 | break; |
| 1991 | } |
| 1992 | len++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 1996 | |
| 1997 | // We want an extra zero in front to make things simpler. |
| 1998 | cp.num[cp.len++] = 0; |
| 1999 | end = cp.len - len; |
| 2000 | |
| 2001 | bc_num_expand(c, cp.len); |
| 2002 | |
| 2003 | bc_num_zero(c); |
| 2004 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2005 | c->rdx = cp.rdx; |
| 2006 | c->len = cp.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2007 | |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2008 | s = BC_STATUS_SUCCESS; |
| 2009 | for (i = end - 1; i < end; --i) { |
| 2010 | BcDig *n, q; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2011 | n = cp.num + i; |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2012 | for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q) |
| 2013 | bc_num_subArrays(n, b->num, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2014 | c->num[i] = q; |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2015 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2016 | // a=2^100000 |
| 2017 | // scale=40000 |
| 2018 | // 1/a <- without check below, this will not be interruptible |
| 2019 | if (G_interrupt) { |
| 2020 | s = BC_STATUS_FAILURE; |
| 2021 | break; |
| 2022 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2023 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2024 | } |
| 2025 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2026 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2027 | bc_num_free(&cp); |
| 2028 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2029 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2030 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2031 | #define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2032 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2033 | 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] | 2034 | BcNum *restrict d, size_t scale, size_t ts) |
| 2035 | { |
| 2036 | BcStatus s; |
| 2037 | BcNum temp; |
| 2038 | bool neg; |
| 2039 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2040 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2041 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2042 | |
| 2043 | if (a->len == 0) { |
| 2044 | bc_num_setToZero(d, ts); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2045 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2049 | s = zbc_num_d(a, b, c, scale); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2050 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2051 | |
| 2052 | if (scale != 0) scale = ts; |
| 2053 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2054 | s = zbc_num_m(c, b, &temp, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2055 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2056 | s = zbc_num_sub(a, &temp, d, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2057 | if (s) goto err; |
| 2058 | |
| 2059 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2060 | |
| 2061 | neg = d->neg; |
| 2062 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2063 | d->neg = neg; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2064 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2065 | bc_num_free(&temp); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2066 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2067 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2068 | #define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2069 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2070 | 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] | 2071 | { |
| 2072 | BcStatus s; |
| 2073 | BcNum c1; |
| 2074 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2075 | |
| 2076 | bc_num_init(&c1, len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2077 | s = zbc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2078 | bc_num_free(&c1); |
| 2079 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2080 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2081 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2082 | #define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2083 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2084 | 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] | 2085 | { |
| 2086 | BcStatus s = BC_STATUS_SUCCESS; |
| 2087 | BcNum copy; |
| 2088 | unsigned long pow; |
| 2089 | size_t i, powrdx, resrdx; |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2090 | bool neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2091 | |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2092 | // GNU bc does not allow 2^2.0 - we do |
| 2093 | for (i = 0; i < b->rdx; i++) |
| 2094 | if (b->num[i] != 0) |
| 2095 | RETURN_STATUS(bc_error("not an integer")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2096 | |
| 2097 | if (b->len == 0) { |
| 2098 | bc_num_one(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2099 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2100 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2101 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2102 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2103 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2104 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2105 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2106 | if (!b->neg) |
| 2107 | bc_num_copy(c, a); |
| 2108 | else |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2109 | s = zbc_num_inv(a, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2110 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | neg = b->neg; |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 2114 | s = zbc_num_ulong_abs(b, &pow); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2115 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 2ea8ddf | 2018-12-22 21:45:18 +0100 | [diff] [blame] | 2116 | // b is not used beyond this point |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2117 | |
| 2118 | bc_num_init(©, a->len); |
| 2119 | bc_num_copy(©, a); |
| 2120 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2121 | if (!neg) { |
| 2122 | if (a->rdx > scale) |
| 2123 | scale = a->rdx; |
| 2124 | if (a->rdx * pow < scale) |
| 2125 | scale = a->rdx * pow; |
| 2126 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2127 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2128 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2129 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2130 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2131 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2132 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2133 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2134 | //if (G_interrupt) { |
| 2135 | // s = BC_STATUS_FAILURE; |
| 2136 | // goto err; |
| 2137 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2138 | } |
| 2139 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2140 | bc_num_copy(c, ©); |
| 2141 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2142 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2143 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2144 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2145 | if (s) goto err; |
| 2146 | |
| 2147 | if (pow & 1) { |
| 2148 | resrdx += powrdx; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2149 | s = zbc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2150 | if (s) goto err; |
| 2151 | } |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2152 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2153 | //if (G_interrupt) { |
| 2154 | // s = BC_STATUS_FAILURE; |
| 2155 | // goto err; |
| 2156 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | if (neg) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2160 | s = zbc_num_inv(c, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2161 | if (s) goto err; |
| 2162 | } |
| 2163 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2164 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2165 | |
| 2166 | // We can't use bc_num_clean() here. |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2167 | for (i = 0; i < c->len; ++i) |
| 2168 | if (c->num[i] != 0) |
| 2169 | goto skip; |
| 2170 | bc_num_setToZero(c, scale); |
| 2171 | skip: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2172 | |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2173 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2174 | bc_num_free(©); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2175 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2176 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2177 | #define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2178 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2179 | 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] | 2180 | { |
| 2181 | BcStatus s; |
| 2182 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2183 | BcDig half_digs[1]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2184 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2185 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2186 | |
| 2187 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2188 | bc_num_expand(b, req); |
| 2189 | |
| 2190 | if (a->len == 0) { |
| 2191 | bc_num_setToZero(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2192 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2193 | } |
| 2194 | if (a->neg) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2195 | RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2196 | } |
| 2197 | if (BC_NUM_ONE(a)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2198 | bc_num_one(b); |
| 2199 | bc_num_extend(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2200 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2204 | len = a->len + scale; |
| 2205 | |
| 2206 | bc_num_init(&num1, len); |
| 2207 | bc_num_init(&num2, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2208 | |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2209 | half.cap = ARRAY_SIZE(half_digs); |
| 2210 | half.num = half_digs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2211 | bc_num_one(&half); |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2212 | half_digs[0] = 5; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2213 | half.rdx = 1; |
| 2214 | |
| 2215 | bc_num_init(&f, len); |
| 2216 | bc_num_init(&fprime, len); |
| 2217 | |
| 2218 | x0 = &num1; |
| 2219 | x1 = &num2; |
| 2220 | |
| 2221 | bc_num_one(x0); |
| 2222 | pow = BC_NUM_INT(a); |
| 2223 | |
| 2224 | if (pow) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2225 | if (pow & 1) |
| 2226 | x0->num[0] = 2; |
| 2227 | else |
| 2228 | x0->num[0] = 6; |
| 2229 | |
| 2230 | pow -= 2 - (pow & 1); |
| 2231 | |
| 2232 | bc_num_extend(x0, pow); |
| 2233 | |
| 2234 | // Make sure to move the radix back. |
| 2235 | x0->rdx -= pow; |
| 2236 | } |
| 2237 | |
| 2238 | x0->rdx = digs = digs1 = 0; |
| 2239 | resrdx = scale + 2; |
| 2240 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2241 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2242 | while (cmp != 0 || digs < len) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2243 | s = zbc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2244 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2245 | s = zbc_num_add(x0, &f, &fprime, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2246 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2247 | s = zbc_num_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2248 | if (s) goto err; |
| 2249 | |
| 2250 | cmp = bc_num_cmp(x1, x0); |
| 2251 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2252 | |
| 2253 | if (cmp == cmp2 && digs == digs1) |
| 2254 | times += 1; |
| 2255 | else |
| 2256 | times = 0; |
| 2257 | |
| 2258 | resrdx += times > 4; |
| 2259 | |
| 2260 | cmp2 = cmp1; |
| 2261 | cmp1 = cmp; |
| 2262 | digs1 = digs; |
| 2263 | |
| 2264 | temp = x0; |
| 2265 | x0 = x1; |
| 2266 | x1 = temp; |
| 2267 | } |
| 2268 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2269 | bc_num_copy(b, x0); |
| 2270 | scale -= 1; |
| 2271 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2272 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2273 | bc_num_free(&fprime); |
| 2274 | bc_num_free(&f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2275 | bc_num_free(&num2); |
| 2276 | bc_num_free(&num1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2277 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2278 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2279 | #define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2280 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2281 | 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] | 2282 | size_t scale) |
| 2283 | { |
| 2284 | BcStatus s; |
| 2285 | BcNum num2, *ptr_a; |
| 2286 | bool init = false; |
| 2287 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2288 | |
| 2289 | if (c == a) { |
| 2290 | memcpy(&num2, c, sizeof(BcNum)); |
| 2291 | ptr_a = &num2; |
| 2292 | bc_num_init(c, len); |
| 2293 | init = true; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2294 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2295 | ptr_a = a; |
| 2296 | bc_num_expand(c, len); |
| 2297 | } |
| 2298 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2299 | s = zbc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2300 | |
| 2301 | if (init) bc_num_free(&num2); |
| 2302 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2303 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2304 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2305 | #define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2306 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2307 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2308 | 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] | 2309 | { |
| 2310 | BcStatus s; |
| 2311 | BcNum base, exp, two, temp; |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2312 | BcDig two_digs[1]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2313 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2314 | if (c->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2315 | RETURN_STATUS(bc_error("divide by zero")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2316 | if (a->rdx || b->rdx || c->rdx) |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2317 | RETURN_STATUS(bc_error("not an integer")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2318 | if (b->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2319 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2320 | |
| 2321 | bc_num_expand(d, c->len); |
| 2322 | bc_num_init(&base, c->len); |
| 2323 | bc_num_init(&exp, b->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2324 | bc_num_init(&temp, b->len); |
| 2325 | |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2326 | two.cap = ARRAY_SIZE(two_digs); |
| 2327 | two.num = two_digs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2328 | bc_num_one(&two); |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2329 | two_digs[0] = 2; |
| 2330 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2331 | bc_num_one(d); |
| 2332 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2333 | s = zbc_num_rem(a, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2334 | if (s) goto err; |
| 2335 | bc_num_copy(&exp, b); |
| 2336 | |
| 2337 | while (exp.len != 0) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2338 | s = zbc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2339 | if (s) goto err; |
| 2340 | |
| 2341 | if (BC_NUM_ONE(&temp)) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2342 | s = zbc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2343 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2344 | s = zbc_num_rem(&temp, c, d, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2345 | if (s) goto err; |
| 2346 | } |
| 2347 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2348 | s = zbc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2349 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2350 | s = zbc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2351 | if (s) goto err; |
| 2352 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2353 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2354 | bc_num_free(&temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2355 | bc_num_free(&exp); |
| 2356 | bc_num_free(&base); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2357 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2358 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2359 | #define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2360 | #endif // ENABLE_DC |
| 2361 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2362 | static FAST_FUNC void bc_string_free(void *string) |
| 2363 | { |
| 2364 | free(*(char**)string); |
| 2365 | } |
| 2366 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2367 | static void bc_func_init(BcFunc *f) |
| 2368 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2369 | bc_char_vec_init(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2370 | IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);) |
| 2371 | IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2372 | IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);) |
| 2373 | IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2374 | IF_BC(f->nparams = 0;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2375 | } |
| 2376 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2377 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2378 | { |
| 2379 | BcFunc *f = (BcFunc *) func; |
| 2380 | bc_vec_free(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2381 | IF_BC(bc_vec_free(&f->labels);) |
| 2382 | IF_BC(bc_vec_free(&f->autos);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2383 | IF_BC(bc_vec_free(&f->strs);) |
| 2384 | IF_BC(bc_vec_free(&f->consts);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2385 | } |
| 2386 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2387 | static void bc_array_expand(BcVec *a, size_t len); |
| 2388 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2389 | static void bc_array_init(BcVec *a, bool nums) |
| 2390 | { |
| 2391 | if (nums) |
| 2392 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2393 | else |
| 2394 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2395 | bc_array_expand(a, 1); |
| 2396 | } |
| 2397 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2398 | static void bc_array_expand(BcVec *a, size_t len) |
| 2399 | { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2400 | if (a->dtor == bc_num_free |
| 2401 | // && a->size == sizeof(BcNum) - always true |
| 2402 | ) { |
| 2403 | BcNum n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2404 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2405 | bc_num_init_DEF_SIZE(&n); |
| 2406 | bc_vec_push(a, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2407 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2408 | } else { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2409 | BcVec v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2410 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2411 | bc_array_init(&v, true); |
| 2412 | bc_vec_push(a, &v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2413 | } |
| 2414 | } |
| 2415 | } |
| 2416 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2417 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2418 | { |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2419 | BcNum *dnum, *snum; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2420 | size_t i; |
| 2421 | |
| 2422 | bc_vec_pop_all(d); |
| 2423 | bc_vec_expand(d, s->cap); |
| 2424 | d->len = s->len; |
| 2425 | |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2426 | dnum = (void*)d->v; |
| 2427 | snum = (void*)s->v; |
| 2428 | for (i = 0; i < s->len; i++, dnum++, snum++) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2429 | bc_num_init(dnum, snum->len); |
| 2430 | bc_num_copy(dnum, snum); |
| 2431 | } |
| 2432 | } |
| 2433 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2434 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2435 | static void dc_result_copy(BcResult *d, BcResult *src) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2436 | { |
| 2437 | d->t = src->t; |
| 2438 | |
| 2439 | switch (d->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2440 | case XC_RESULT_TEMP: |
| 2441 | case XC_RESULT_IBASE: |
| 2442 | case XC_RESULT_SCALE: |
| 2443 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2444 | bc_num_init(&d->d.n, src->d.n.len); |
| 2445 | bc_num_copy(&d->d.n, &src->d.n); |
| 2446 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2447 | case XC_RESULT_VAR: |
| 2448 | case XC_RESULT_ARRAY: |
| 2449 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2450 | d->d.id.name = xstrdup(src->d.id.name); |
| 2451 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2452 | case XC_RESULT_CONSTANT: |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 2453 | IF_BC(case BC_RESULT_LAST:) |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2454 | IF_BC(case BC_RESULT_ONE:) |
| 2455 | case XC_RESULT_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2456 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2457 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2458 | } |
| 2459 | } |
| 2460 | #endif // ENABLE_DC |
| 2461 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2462 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2463 | { |
| 2464 | BcResult *r = (BcResult *) result; |
| 2465 | |
| 2466 | switch (r->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2467 | case XC_RESULT_TEMP: |
| 2468 | case XC_RESULT_IBASE: |
| 2469 | case XC_RESULT_SCALE: |
| 2470 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2471 | bc_num_free(&r->d.n); |
| 2472 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2473 | case XC_RESULT_VAR: |
| 2474 | case XC_RESULT_ARRAY: |
| 2475 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2476 | free(r->d.id.name); |
| 2477 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2478 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2479 | // Do nothing. |
| 2480 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2484 | static int bad_input_byte(char c) |
| 2485 | { |
| 2486 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 2487 | || c > 0x7e |
| 2488 | ) { |
| 2489 | bc_error_fmt("illegal character 0x%02x", c); |
| 2490 | return 1; |
| 2491 | } |
| 2492 | return 0; |
| 2493 | } |
| 2494 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2495 | static void xc_read_line(BcVec *vec, FILE *fp) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2496 | { |
| 2497 | again: |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2498 | bc_vec_pop_all(vec); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2499 | fflush_and_check(); |
| 2500 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2501 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2502 | if (G_interrupt) { // ^C was pressed |
| 2503 | intr: |
| 2504 | if (fp != stdin) { |
| 2505 | // ^C while running a script (bc SCRIPT): die. |
| 2506 | // We do not return to interactive prompt: |
| 2507 | // user might be running us from a shell, |
| 2508 | // and SCRIPT might be intended to terminate |
| 2509 | // (e.g. contain a "halt" stmt). |
| 2510 | // ^C dropping user into a bc prompt instead of |
| 2511 | // the shell would be unexpected. |
| 2512 | xfunc_die(); |
| 2513 | } |
| 2514 | // ^C while interactive input |
| 2515 | G_interrupt = 0; |
| 2516 | // GNU bc says "interrupted execution." |
| 2517 | // GNU dc says "Interrupt!" |
| 2518 | fputs("\ninterrupted execution\n", stderr); |
| 2519 | } |
| 2520 | |
| 2521 | # if ENABLE_FEATURE_EDITING |
| 2522 | if (G_ttyin && fp == stdin) { |
| 2523 | int n, i; |
| 2524 | # define line_buf bb_common_bufsiz1 |
| 2525 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
| 2526 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 2527 | if (n == 0) // ^C |
| 2528 | goto intr; |
| 2529 | bc_vec_pushZeroByte(vec); // ^D or EOF (or error) |
| 2530 | return; |
| 2531 | } |
| 2532 | i = 0; |
| 2533 | for (;;) { |
| 2534 | char c = line_buf[i++]; |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2535 | if (c == '\0') break; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2536 | if (bad_input_byte(c)) goto again; |
| 2537 | } |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2538 | bc_vec_string(vec, n, line_buf); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2539 | # undef line_buf |
| 2540 | } else |
| 2541 | # endif |
| 2542 | #endif |
| 2543 | { |
| 2544 | int c; |
| 2545 | bool bad_chars = 0; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2546 | |
| 2547 | do { |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2548 | get_char: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2549 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2550 | if (G_interrupt) { |
| 2551 | // ^C was pressed: ignore entire line, get another one |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2552 | goto again; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2553 | } |
| 2554 | #endif |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2555 | c = fgetc(fp); |
| 2556 | if (c == '\0') |
| 2557 | goto get_char; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2558 | if (c == EOF) { |
| 2559 | if (ferror(fp)) |
| 2560 | bb_perror_msg_and_die("input error"); |
| 2561 | // Note: EOF does not append '\n' |
| 2562 | break; |
| 2563 | } |
| 2564 | bad_chars |= bad_input_byte(c); |
| 2565 | bc_vec_pushByte(vec, (char)c); |
| 2566 | } while (c != '\n'); |
| 2567 | |
| 2568 | if (bad_chars) { |
| 2569 | // Bad chars on this line |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2570 | if (!G.prs.lex_filename) { // stdin |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2571 | // ignore entire line, get another one |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2572 | goto again; |
| 2573 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2574 | 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] | 2575 | } |
| 2576 | bc_vec_pushZeroByte(vec); |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | // |
| 2581 | // Parsing routines |
| 2582 | // |
| 2583 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2584 | // "Input numbers may contain the characters 0-9 and A-Z. |
| 2585 | // (Note: They must be capitals. Lower case letters are variable names.) |
| 2586 | // Single digit numbers always have the value of the digit regardless of |
| 2587 | // the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes |
| 2588 | // all input digits greater or equal to ibase to the value of ibase-1. |
| 2589 | // This makes the number ZZZ always be the largest 3 digit number of the |
| 2590 | // input base." |
| 2591 | static bool xc_num_strValid(const char *val) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2592 | { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2593 | bool radix = false; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2594 | for (;;) { |
| 2595 | BcDig c = *val++; |
| 2596 | if (c == '\0') |
| 2597 | break; |
| 2598 | if (c == '.') { |
| 2599 | if (radix) return false; |
| 2600 | radix = true; |
| 2601 | continue; |
| 2602 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2603 | if ((c < '0' || c > '9') && (c < 'A' || c > 'Z')) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2604 | return false; |
| 2605 | } |
| 2606 | return true; |
| 2607 | } |
| 2608 | |
| 2609 | // Note: n is already "bc_num_zero()"ed, |
| 2610 | // leading zeroes in "val" are removed |
| 2611 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2612 | { |
| 2613 | size_t len, i; |
| 2614 | const char *ptr; |
| 2615 | |
| 2616 | len = strlen(val); |
| 2617 | if (len == 0) |
| 2618 | return; |
| 2619 | |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2620 | 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] | 2621 | |
| 2622 | ptr = strchr(val, '.'); |
| 2623 | |
| 2624 | n->rdx = 0; |
| 2625 | if (ptr != NULL) |
| 2626 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2627 | |
| 2628 | for (i = 0; val[i]; ++i) { |
| 2629 | if (val[i] != '0' && val[i] != '.') { |
| 2630 | // Not entirely zero value - convert it, and exit |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2631 | if (len == 1) { |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2632 | unsigned c = val[0] - '0'; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2633 | n->len = 1; |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2634 | if (c > 9) { // A-Z => 10-36 |
| 2635 | n->len = 2; |
| 2636 | c -= ('A' - '9' - 1); |
| 2637 | n->num[1] = c/10; |
| 2638 | c = c%10; |
| 2639 | } |
| 2640 | n->num[0] = c; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2641 | break; |
| 2642 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2643 | i = len - 1; |
| 2644 | for (;;) { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2645 | char c = val[i] - '0'; |
| 2646 | if (c > 9) // A-Z => 9 |
| 2647 | c = 9; |
| 2648 | n->num[n->len] = c; |
| 2649 | n->len++; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2650 | skip_dot: |
| 2651 | if (i == 0) break; |
| 2652 | if (val[--i] == '.') goto skip_dot; |
| 2653 | } |
| 2654 | break; |
| 2655 | } |
| 2656 | } |
| 2657 | // if for() exits without hitting if(), the value is entirely zero |
| 2658 | } |
| 2659 | |
| 2660 | // Note: n is already "bc_num_zero()"ed, |
| 2661 | // leading zeroes in "val" are removed |
| 2662 | static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t) |
| 2663 | { |
| 2664 | BcStatus s; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2665 | BcNum mult, result; |
| 2666 | BcNum temp; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2667 | BcNum base; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2668 | BcDig temp_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2669 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2670 | size_t digits; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2671 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2672 | bc_num_init_DEF_SIZE(&mult); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2673 | |
| 2674 | temp.cap = ARRAY_SIZE(temp_digs); |
| 2675 | temp.num = temp_digs; |
| 2676 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2677 | base.cap = ARRAY_SIZE(base_digs); |
| 2678 | base.num = base_digs; |
| 2679 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2680 | base_t--; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2681 | |
| 2682 | for (;;) { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2683 | unsigned v; |
Denys Vlasenko | 8797adc | 2018-12-31 19:50:06 +0100 | [diff] [blame] | 2684 | char c; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2685 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2686 | c = *val++; |
| 2687 | if (c == '\0') goto int_err; |
| 2688 | if (c == '.') break; |
| 2689 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2690 | v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10); |
| 2691 | if (v > base_t) v = base_t; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2692 | |
| 2693 | s = zbc_num_mul(n, &base, &mult, 0); |
| 2694 | if (s) goto int_err; |
| 2695 | bc_num_ulong2num(&temp, v); |
| 2696 | s = zbc_num_add(&mult, &temp, n, 0); |
| 2697 | if (s) goto int_err; |
| 2698 | } |
| 2699 | |
| 2700 | bc_num_init(&result, base.len); |
| 2701 | //bc_num_zero(&result); - already is |
| 2702 | bc_num_one(&mult); |
| 2703 | |
| 2704 | digits = 0; |
| 2705 | for (;;) { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2706 | unsigned v; |
Denys Vlasenko | 8797adc | 2018-12-31 19:50:06 +0100 | [diff] [blame] | 2707 | char c; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2708 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2709 | c = *val++; |
| 2710 | if (c == '\0') break; |
| 2711 | digits++; |
| 2712 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2713 | v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10); |
| 2714 | if (v > base_t) v = base_t; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2715 | |
| 2716 | s = zbc_num_mul(&result, &base, &result, 0); |
| 2717 | if (s) goto err; |
| 2718 | bc_num_ulong2num(&temp, v); |
| 2719 | s = zbc_num_add(&result, &temp, &result, 0); |
| 2720 | if (s) goto err; |
| 2721 | s = zbc_num_mul(&mult, &base, &mult, 0); |
| 2722 | if (s) goto err; |
| 2723 | } |
| 2724 | |
| 2725 | s = zbc_num_div(&result, &mult, &result, digits); |
| 2726 | if (s) goto err; |
| 2727 | s = zbc_num_add(n, &result, n, digits); |
| 2728 | if (s) goto err; |
| 2729 | |
| 2730 | if (n->len != 0) { |
| 2731 | if (n->rdx < digits) |
| 2732 | bc_num_extend(n, digits - n->rdx); |
| 2733 | } else |
| 2734 | bc_num_zero(n); |
| 2735 | err: |
| 2736 | bc_num_free(&result); |
| 2737 | int_err: |
| 2738 | bc_num_free(&mult); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2739 | } |
| 2740 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2741 | 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] | 2742 | { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2743 | size_t i; |
| 2744 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2745 | if (!xc_num_strValid(val)) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2746 | RETURN_STATUS(bc_error("bad number string")); |
| 2747 | |
| 2748 | bc_num_zero(n); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2749 | while (*val == '0') |
| 2750 | val++; |
| 2751 | for (i = 0; ; ++i) { |
| 2752 | if (val[i] == '\0') |
| 2753 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2754 | if (val[i] != '.' && val[i] != '0') |
| 2755 | break; |
| 2756 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2757 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2758 | if (base_t == 10 || val[1] == '\0') |
| 2759 | // Decimal, or single-digit number |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2760 | bc_num_parseDecimal(n, val); |
| 2761 | else |
| 2762 | bc_num_parseBase(n, val, base_t); |
| 2763 | |
| 2764 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2765 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2766 | #define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2767 | |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2768 | // p->lex_inbuf points to the current string to be parsed. |
| 2769 | // if p->lex_inbuf points to '\0', it's either EOF or it points after |
| 2770 | // last processed line's terminating '\n' (and more reading needs to be done |
| 2771 | // to get next character). |
| 2772 | // |
| 2773 | // If you are in a situation where that is a possibility, call peek_inbuf(). |
| 2774 | // If necessary, it performs more reading and changes p->lex_inbuf, |
| 2775 | // then it returns *p->lex_inbuf (which will be '\0' only if it's EOF). |
| 2776 | // After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0', |
| 2777 | // it's ok to do p->lex_inbuf++ once without end-of-buffer checking. |
| 2778 | // |
| 2779 | // eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++": |
| 2780 | // it returns current char and advances the pointer (if not EOF). |
| 2781 | // After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid. |
| 2782 | // |
| 2783 | // In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf(): |
| 2784 | // unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY |
| 2785 | // on real EOF, not end-of-buffer. |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2786 | // |
| 2787 | // bc cases to test interactively: |
| 2788 | // 1 #comment\ - prints "1<newline>" at once (comment is not continued) |
| 2789 | // 1 #comment/* - prints "1<newline>" at once |
| 2790 | // 1 #comment" - prints "1<newline>" at once |
| 2791 | // 1\#comment - error at once (\ is not a line continuation) |
| 2792 | // 1 + /*"*/2 - prints "3<newline>" at once |
| 2793 | // 1 + /*#*/2 - prints "3<newline>" at once |
| 2794 | // "str\" - prints "str\" at once |
| 2795 | // "str#" - prints "str#" at once |
| 2796 | // "str/*" - prints "str/*" at once |
| 2797 | // "str#\ - waits for second line |
| 2798 | // end" - ...prints "str#\<newline>end" |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2799 | static char peek_inbuf(void) |
| 2800 | { |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 2801 | if (*G.prs.lex_inbuf == '\0' |
| 2802 | && G.prs.lex_input_fp |
| 2803 | ) { |
| 2804 | xc_read_line(&G.input_buffer, G.prs.lex_input_fp); |
| 2805 | G.prs.lex_inbuf = G.input_buffer.v; |
| 2806 | if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte) |
| 2807 | G.prs.lex_input_fp = NULL; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2808 | } |
| 2809 | return *G.prs.lex_inbuf; |
| 2810 | } |
| 2811 | static char eat_inbuf(void) |
| 2812 | { |
| 2813 | char c = peek_inbuf(); |
| 2814 | if (c) G.prs.lex_inbuf++; |
| 2815 | return c; |
| 2816 | } |
| 2817 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2818 | static void xc_lex_lineComment(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2819 | { |
| 2820 | BcParse *p = &G.prs; |
| 2821 | char c; |
| 2822 | |
| 2823 | // Try: echo -n '#foo' | bc |
| 2824 | p->lex = XC_LEX_WHITESPACE; |
| 2825 | |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 2826 | // Not peek_inbuf(): we depend on input being done in whole lines: |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2827 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2828 | while ((c = *p->lex_inbuf) != '\n' && c != '\0') |
| 2829 | p->lex_inbuf++; |
| 2830 | } |
| 2831 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2832 | static void xc_lex_whitespace(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2833 | { |
| 2834 | BcParse *p = &G.prs; |
| 2835 | |
| 2836 | p->lex = XC_LEX_WHITESPACE; |
| 2837 | for (;;) { |
| 2838 | // We depend here on input being done in whole lines: |
| 2839 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2840 | char c = *p->lex_inbuf; |
| 2841 | if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
| 2842 | break; |
| 2843 | if (!isspace(c)) |
| 2844 | break; |
| 2845 | p->lex_inbuf++; |
| 2846 | } |
| 2847 | } |
| 2848 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2849 | static BC_STATUS zxc_lex_number(char last) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2850 | { |
| 2851 | BcParse *p = &G.prs; |
| 2852 | bool pt; |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2853 | char last_valid_ch; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2854 | |
| 2855 | bc_vec_pop_all(&p->lex_strnumbuf); |
| 2856 | bc_vec_pushByte(&p->lex_strnumbuf, last); |
| 2857 | |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2858 | // bc: "Input numbers may contain the characters 0-9 and A-Z. |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2859 | // (Note: They must be capitals. Lower case letters are variable names.) |
| 2860 | // Single digit numbers always have the value of the digit regardless of |
| 2861 | // the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes |
| 2862 | // all input digits greater or equal to ibase to the value of ibase-1. |
| 2863 | // This makes the number ZZZ always be the largest 3 digit number of the |
| 2864 | // input base." |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2865 | // dc only allows A-F, the rules about single-char and multi-char are the same. |
| 2866 | last_valid_ch = (IS_BC ? 'Z' : 'F'); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2867 | pt = (last == '.'); |
| 2868 | p->lex = XC_LEX_NUMBER; |
| 2869 | for (;;) { |
| 2870 | // We depend here on input being done in whole lines: |
| 2871 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2872 | char c = *p->lex_inbuf; |
| 2873 | check_c: |
| 2874 | if (c == '\0') |
| 2875 | break; |
| 2876 | if (c == '\\' && p->lex_inbuf[1] == '\n') { |
| 2877 | p->lex_inbuf += 2; |
| 2878 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 2879 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2880 | c = peek_inbuf(); // force next line to be read |
| 2881 | goto check_c; |
| 2882 | } |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2883 | if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2884 | if (c != '.') break; |
| 2885 | // if '.' was already seen, stop on second one: |
| 2886 | if (pt) break; |
| 2887 | pt = true; |
| 2888 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2889 | // c is one of "0-9A-Z." |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2890 | last = c; |
| 2891 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 2892 | p->lex_inbuf++; |
| 2893 | } |
| 2894 | if (last == '.') // remove trailing '.' if any |
| 2895 | bc_vec_pop(&p->lex_strnumbuf); |
| 2896 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 2897 | |
| 2898 | G.err_line = G.prs.lex_line; |
| 2899 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2900 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2901 | #define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2902 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2903 | static void xc_lex_name(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2904 | { |
| 2905 | BcParse *p = &G.prs; |
| 2906 | size_t i; |
| 2907 | const char *buf; |
| 2908 | |
| 2909 | p->lex = XC_LEX_NAME; |
| 2910 | |
| 2911 | // Since names can't cross lines with \<newline>, |
| 2912 | // we depend on the fact that whole line is in the buffer |
| 2913 | i = 0; |
| 2914 | buf = p->lex_inbuf - 1; |
| 2915 | for (;;) { |
| 2916 | char c = buf[i]; |
| 2917 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 2918 | i++; |
| 2919 | } |
| 2920 | |
| 2921 | #if 0 // We do not protect against people with gigabyte-long names |
| 2922 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2923 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2924 | if (i > BC_MAX_STRING) |
| 2925 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2926 | } |
| 2927 | #endif |
| 2928 | bc_vec_string(&p->lex_strnumbuf, i, buf); |
| 2929 | |
| 2930 | // Increment the index. We minus 1 because it has already been incremented. |
| 2931 | p->lex_inbuf += i - 1; |
| 2932 | |
| 2933 | //return BC_STATUS_SUCCESS; |
| 2934 | } |
| 2935 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2936 | IF_BC(static BC_STATUS zbc_lex_token(void);) |
| 2937 | IF_DC(static BC_STATUS zdc_lex_token(void);) |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 2938 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
| 2939 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2940 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2941 | static BC_STATUS zxc_lex_next(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2942 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2943 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2944 | BcStatus s; |
| 2945 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 2946 | G.err_line = p->lex_line; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2947 | p->lex_last = p->lex; |
Denys Vlasenko | 2f7352b | 2018-12-26 18:59:42 +0100 | [diff] [blame] | 2948 | //why? |
| 2949 | // if (p->lex_last == XC_LEX_EOF) |
| 2950 | // RETURN_STATUS(bc_error("end of file")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2951 | |
| 2952 | // Loop until failure or we don't have whitespace. This |
| 2953 | // is so the parser doesn't get inundated with whitespace. |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 2954 | // Comments are also XC_LEX_WHITESPACE tokens and eaten here. |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2955 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2956 | do { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2957 | if (*p->lex_inbuf == '\0') { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2958 | p->lex = XC_LEX_EOF; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2959 | if (peek_inbuf() == '\0') |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2960 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2961 | } |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 2962 | p->lex_next_at = p->lex_inbuf; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 2963 | dbg_lex("next string to parse:'%.*s'", |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2964 | (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at), |
| 2965 | p->lex_next_at |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 2966 | ); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2967 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2968 | IF_BC(s = zbc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2969 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2970 | IF_DC(s = zdc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2971 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2972 | } while (!s && p->lex == XC_LEX_WHITESPACE); |
| 2973 | dbg_lex("p->lex from string:%d", p->lex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2974 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2975 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2976 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2977 | #define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2978 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 2979 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2980 | static BC_STATUS zbc_lex_skip_if_at_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2981 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2982 | if (G.prs.lex == XC_LEX_NLINE) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2983 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2984 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2985 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2986 | #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] | 2987 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2988 | static BC_STATUS zbc_lex_next_and_skip_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2989 | { |
| 2990 | BcStatus s; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2991 | s = zxc_lex_next(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2992 | if (s) RETURN_STATUS(s); |
| 2993 | // if(cond)<newline>stmt is accepted too (but not 2+ newlines) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2994 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2995 | RETURN_STATUS(s); |
| 2996 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2997 | #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] | 2998 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 2999 | static BC_STATUS zbc_lex_identifier(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3000 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3001 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3002 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3003 | unsigned i; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3004 | const char *buf = p->lex_inbuf - 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3005 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3006 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 3007 | const char *keyword8 = bc_lex_kws[i].name8; |
| 3008 | unsigned j = 0; |
| 3009 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 3010 | j++; |
| 3011 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3012 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3013 | if (keyword8[j] != '\0') |
| 3014 | continue; |
| 3015 | match: |
| 3016 | // buf starts with keyword bc_lex_kws[i] |
Denys Vlasenko | 5acd14b | 2018-12-20 16:48:50 +0100 | [diff] [blame] | 3017 | if (isalnum(buf[j]) || buf[j]=='_') |
| 3018 | continue; // "ifz" does not match "if" keyword, "if." does |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3019 | p->lex = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 3020 | if (!keyword_is_POSIX(i)) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3021 | s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8); |
| 3022 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | // We minus 1 because the index has already been incremented. |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3026 | p->lex_inbuf += j - 1; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3027 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3028 | } |
| 3029 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3030 | xc_lex_name(); |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 3031 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3032 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3033 | if (p->lex_strnumbuf.len > 2) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3034 | // Prevent this: |
| 3035 | // >>> qwe=1 |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3036 | // bc: POSIX only allows one character names; this is bad: 'qwe=1 |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3037 | // ' |
| 3038 | unsigned len = strchrnul(buf, '\n') - buf; |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3039 | 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] | 3040 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3041 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3042 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3043 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3044 | #define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3045 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3046 | static BC_STATUS zbc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3047 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3048 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3049 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3050 | p->lex = XC_LEX_STR; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3051 | bc_vec_pop_all(&p->lex_strnumbuf); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3052 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3053 | char c = peek_inbuf(); // strings can cross lines |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3054 | if (c == '\0') { |
Denys Vlasenko | 8af1108 | 2018-12-26 21:01:41 +0100 | [diff] [blame] | 3055 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3056 | } |
| 3057 | if (c == '"') |
| 3058 | break; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 3059 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3060 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 3061 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3062 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3063 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 3064 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3065 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3066 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 3067 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3068 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3069 | G.err_line = p->lex_line; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3070 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3071 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3072 | #define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3073 | |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3074 | static void parse_lex_by_checking_eq_sign(unsigned with_and_without) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3075 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3076 | BcParse *p = &G.prs; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3077 | if (*p->lex_inbuf == '=') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3078 | // ^^^ not using peek_inbuf() since '==' etc can't be split across lines |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3079 | p->lex_inbuf++; |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3080 | with_and_without >>= 8; // store "with" value |
| 3081 | } // else store "without" value |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3082 | p->lex = (with_and_without & 0xff); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3083 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3084 | #define parse_lex_by_checking_eq_sign(with, without) \ |
| 3085 | parse_lex_by_checking_eq_sign(((with)<<8)|(without)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3086 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3087 | static BC_STATUS zbc_lex_comment(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3088 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3089 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3090 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3091 | p->lex = XC_LEX_WHITESPACE; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3092 | // here lex_inbuf is at '*' of opening comment delimiter |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3093 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3094 | char c; |
| 3095 | |
| 3096 | p->lex_inbuf++; |
| 3097 | c = peek_inbuf(); |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3098 | check_star: |
| 3099 | if (c == '*') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3100 | p->lex_inbuf++; |
Denys Vlasenko | 8af1108 | 2018-12-26 21:01:41 +0100 | [diff] [blame] | 3101 | c = *p->lex_inbuf; // no need to peek_inbuf() |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3102 | if (c == '/') |
| 3103 | break; |
| 3104 | goto check_star; |
| 3105 | } |
| 3106 | if (c == '\0') { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3107 | RETURN_STATUS(bc_error("unterminated comment")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3108 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 3109 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3110 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 3111 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3112 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3113 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3114 | p->lex_inbuf++; // skip trailing '/' |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3115 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3116 | G.err_line = p->lex_line; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3117 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3118 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3119 | #define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3120 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3121 | #undef zbc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3122 | static BC_STATUS zbc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3123 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3124 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3125 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3126 | char c = eat_inbuf(); |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3127 | char c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3128 | |
| 3129 | // This is the workhorse of the lexer. |
| 3130 | switch (c) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3131 | // case '\0': // probably never reached |
| 3132 | // p->lex_inbuf--; |
| 3133 | // p->lex = XC_LEX_EOF; |
| 3134 | // break; |
| 3135 | case '\n': |
| 3136 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 3137 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3138 | p->lex = XC_LEX_NLINE; |
| 3139 | break; |
| 3140 | case '\t': |
| 3141 | case '\v': |
| 3142 | case '\f': |
| 3143 | case '\r': |
| 3144 | case ' ': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3145 | xc_lex_whitespace(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3146 | break; |
| 3147 | case '!': |
| 3148 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3149 | if (p->lex == BC_LEX_OP_BOOL_NOT) { |
| 3150 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3151 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3152 | } |
| 3153 | break; |
| 3154 | case '"': |
| 3155 | s = zbc_lex_string(); |
| 3156 | break; |
| 3157 | case '#': |
| 3158 | s = zbc_POSIX_does_not_allow("'#' script comments"); |
| 3159 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3160 | xc_lex_lineComment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3161 | break; |
| 3162 | case '%': |
| 3163 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS); |
| 3164 | break; |
| 3165 | case '&': |
| 3166 | c2 = *p->lex_inbuf; |
| 3167 | if (c2 == '&') { |
| 3168 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
| 3169 | if (s) RETURN_STATUS(s); |
| 3170 | p->lex_inbuf++; |
| 3171 | p->lex = BC_LEX_OP_BOOL_AND; |
| 3172 | } else { |
| 3173 | p->lex = XC_LEX_INVALID; |
| 3174 | s = bc_error_bad_character('&'); |
| 3175 | } |
| 3176 | break; |
| 3177 | case '(': |
| 3178 | case ')': |
| 3179 | p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3180 | break; |
| 3181 | case '*': |
| 3182 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY); |
| 3183 | break; |
| 3184 | case '+': |
| 3185 | c2 = *p->lex_inbuf; |
| 3186 | if (c2 == '+') { |
| 3187 | p->lex_inbuf++; |
| 3188 | p->lex = BC_LEX_OP_INC; |
| 3189 | } else |
| 3190 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS); |
| 3191 | break; |
| 3192 | case ',': |
| 3193 | p->lex = BC_LEX_COMMA; |
| 3194 | break; |
| 3195 | case '-': |
| 3196 | c2 = *p->lex_inbuf; |
| 3197 | if (c2 == '-') { |
| 3198 | p->lex_inbuf++; |
| 3199 | p->lex = BC_LEX_OP_DEC; |
| 3200 | } else |
| 3201 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS); |
| 3202 | break; |
| 3203 | case '.': |
| 3204 | if (isdigit(*p->lex_inbuf)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3205 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3206 | else { |
| 3207 | p->lex = BC_LEX_KEY_LAST; |
| 3208 | s = zbc_POSIX_does_not_allow("'.' as 'last'"); |
| 3209 | } |
| 3210 | break; |
| 3211 | case '/': |
| 3212 | c2 = *p->lex_inbuf; |
| 3213 | if (c2 == '*') |
| 3214 | s = zbc_lex_comment(); |
| 3215 | else |
| 3216 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE); |
| 3217 | break; |
| 3218 | case '0': |
| 3219 | case '1': |
| 3220 | case '2': |
| 3221 | case '3': |
| 3222 | case '4': |
| 3223 | case '5': |
| 3224 | case '6': |
| 3225 | case '7': |
| 3226 | case '8': |
| 3227 | case '9': |
| 3228 | case 'A': |
| 3229 | case 'B': |
| 3230 | case 'C': |
| 3231 | case 'D': |
| 3232 | case 'E': |
| 3233 | case 'F': |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3234 | case 'G': |
| 3235 | case 'H': |
| 3236 | case 'I': |
| 3237 | case 'J': |
| 3238 | case 'K': |
| 3239 | case 'L': |
| 3240 | case 'M': |
| 3241 | case 'N': |
| 3242 | case 'O': |
| 3243 | case 'P': |
| 3244 | case 'Q': |
| 3245 | case 'R': |
| 3246 | case 'S': |
| 3247 | case 'T': |
| 3248 | case 'U': |
| 3249 | case 'V': |
| 3250 | case 'W': |
| 3251 | case 'X': |
| 3252 | case 'Y': |
| 3253 | case 'Z': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3254 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3255 | break; |
| 3256 | case ';': |
| 3257 | p->lex = BC_LEX_SCOLON; |
| 3258 | break; |
| 3259 | case '<': |
| 3260 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT); |
| 3261 | break; |
| 3262 | case '=': |
| 3263 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3264 | break; |
| 3265 | case '>': |
| 3266 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT); |
| 3267 | break; |
| 3268 | case '[': |
| 3269 | case ']': |
| 3270 | p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3271 | break; |
| 3272 | case '\\': |
| 3273 | if (*p->lex_inbuf == '\n') { |
| 3274 | p->lex = XC_LEX_WHITESPACE; |
| 3275 | p->lex_inbuf++; |
| 3276 | } else |
| 3277 | s = bc_error_bad_character(c); |
| 3278 | break; |
| 3279 | case '^': |
| 3280 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER); |
| 3281 | break; |
| 3282 | case 'a': |
| 3283 | case 'b': |
| 3284 | case 'c': |
| 3285 | case 'd': |
| 3286 | case 'e': |
| 3287 | case 'f': |
| 3288 | case 'g': |
| 3289 | case 'h': |
| 3290 | case 'i': |
| 3291 | case 'j': |
| 3292 | case 'k': |
| 3293 | case 'l': |
| 3294 | case 'm': |
| 3295 | case 'n': |
| 3296 | case 'o': |
| 3297 | case 'p': |
| 3298 | case 'q': |
| 3299 | case 'r': |
| 3300 | case 's': |
| 3301 | case 't': |
| 3302 | case 'u': |
| 3303 | case 'v': |
| 3304 | case 'w': |
| 3305 | case 'x': |
| 3306 | case 'y': |
| 3307 | case 'z': |
| 3308 | s = zbc_lex_identifier(); |
| 3309 | break; |
| 3310 | case '{': |
| 3311 | case '}': |
| 3312 | p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3313 | break; |
| 3314 | case '|': |
| 3315 | c2 = *p->lex_inbuf; |
| 3316 | if (c2 == '|') { |
| 3317 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
| 3318 | if (s) RETURN_STATUS(s); |
| 3319 | p->lex_inbuf++; |
| 3320 | p->lex = BC_LEX_OP_BOOL_OR; |
| 3321 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3322 | p->lex = XC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3323 | s = bc_error_bad_character(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3324 | } |
| 3325 | break; |
| 3326 | default: |
| 3327 | p->lex = XC_LEX_INVALID; |
| 3328 | s = bc_error_bad_character(c); |
| 3329 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3330 | } |
| 3331 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3332 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3333 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3334 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3335 | #endif // ENABLE_BC |
| 3336 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3337 | #if ENABLE_DC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3338 | static BC_STATUS zdc_lex_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3339 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3340 | BcParse *p = &G.prs; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3341 | if (G_exreg && isspace(*p->lex_inbuf)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3342 | xc_lex_whitespace(); // eats whitespace (but not newline) |
| 3343 | p->lex_inbuf++; // xc_lex_name() expects this |
| 3344 | xc_lex_name(); |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3345 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3346 | bc_vec_pop_all(&p->lex_strnumbuf); |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3347 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3348 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 3349 | p->lex = XC_LEX_NAME; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3350 | } |
| 3351 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3352 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3353 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3354 | #define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3355 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3356 | static BC_STATUS zdc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3357 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3358 | BcParse *p = &G.prs; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3359 | size_t depth; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3360 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3361 | p->lex = XC_LEX_STR; |
| 3362 | bc_vec_pop_all(&p->lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3363 | |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3364 | depth = 1; |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3365 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3366 | char c = peek_inbuf(); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3367 | if (c == '\0') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3368 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3369 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3370 | if (c == '[') depth++; |
| 3371 | if (c == ']') |
| 3372 | if (--depth == 0) |
| 3373 | break; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 3374 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3375 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 3376 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3377 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3378 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 3379 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3380 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3381 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3382 | p->lex_inbuf++; // skip trailing ']' |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3383 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3384 | G.err_line = p->lex_line; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3385 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3386 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3387 | #define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3388 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3389 | #undef zdc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3390 | static BC_STATUS zdc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3391 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3392 | static const //BcLexType - should be this type, but narrower type saves size: |
| 3393 | uint8_t |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 3394 | dc_lex_regs[] ALIGN1 = { |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3395 | 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] | 3396 | XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON, |
| 3397 | 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] | 3398 | DC_LEX_STORE_PUSH, |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3399 | }; |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3400 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3401 | BcParse *p = &G.prs; |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3402 | BcStatus s; |
| 3403 | char c, c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3404 | size_t i; |
| 3405 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3406 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3407 | if (p->lex_last == dc_lex_regs[i]) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3408 | RETURN_STATUS(zdc_lex_register()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3409 | } |
| 3410 | |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3411 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3412 | c = eat_inbuf(); |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3413 | if (c >= '%' && c <= '~' |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3414 | && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3415 | ) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3416 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | // This is the workhorse of the lexer. |
| 3420 | switch (c) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3421 | // case '\0': // probably never reached |
| 3422 | // p->lex = XC_LEX_EOF; |
| 3423 | // break; |
| 3424 | case '\n': |
| 3425 | // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
| 3426 | // (and "case '\n':" is not just empty here) |
| 3427 | // only to allow interactive dc have a way to exit |
| 3428 | // "parse" stage of "parse,execute" loop |
| 3429 | // on <enter>, not on _next_ token (which would mean |
| 3430 | // commands are not executed on pressing <enter>). |
| 3431 | // IOW: typing "1p<enter>" should print "1" _at once_, |
| 3432 | // not after some more input. |
| 3433 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 3434 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3435 | p->lex = XC_LEX_NLINE; |
| 3436 | break; |
| 3437 | case '\t': |
| 3438 | case '\v': |
| 3439 | case '\f': |
| 3440 | case '\r': |
| 3441 | case ' ': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3442 | xc_lex_whitespace(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3443 | break; |
| 3444 | case '!': |
| 3445 | c2 = *p->lex_inbuf; |
| 3446 | if (c2 == '=') |
| 3447 | p->lex = XC_LEX_OP_REL_NE; |
| 3448 | else if (c2 == '<') |
| 3449 | p->lex = XC_LEX_OP_REL_LE; |
| 3450 | else if (c2 == '>') |
| 3451 | p->lex = XC_LEX_OP_REL_GE; |
| 3452 | else |
| 3453 | RETURN_STATUS(bc_error_bad_character(c)); |
| 3454 | p->lex_inbuf++; |
| 3455 | break; |
| 3456 | case '#': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3457 | xc_lex_lineComment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3458 | break; |
| 3459 | case '.': |
| 3460 | if (isdigit(*p->lex_inbuf)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3461 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3462 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3463 | s = bc_error_bad_character(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3464 | break; |
| 3465 | case '0': |
| 3466 | case '1': |
| 3467 | case '2': |
| 3468 | case '3': |
| 3469 | case '4': |
| 3470 | case '5': |
| 3471 | case '6': |
| 3472 | case '7': |
| 3473 | case '8': |
| 3474 | case '9': |
| 3475 | case 'A': |
| 3476 | case 'B': |
| 3477 | case 'C': |
| 3478 | case 'D': |
| 3479 | case 'E': |
| 3480 | case 'F': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3481 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3482 | break; |
| 3483 | case '[': |
| 3484 | s = zdc_lex_string(); |
| 3485 | break; |
| 3486 | default: |
| 3487 | p->lex = XC_LEX_INVALID; |
| 3488 | s = bc_error_bad_character(c); |
| 3489 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3490 | } |
| 3491 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3492 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3493 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3494 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3495 | #endif // ENABLE_DC |
| 3496 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3497 | static void xc_parse_push(char i) |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3498 | { |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3499 | BcVec *code = &G.prs.func->code; |
| 3500 | dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i); |
| 3501 | bc_vec_pushByte(code, i); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3502 | } |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3503 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3504 | static void xc_parse_pushName(char *name) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3505 | { |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3506 | #if 1 |
| 3507 | BcVec *code = &G.prs.func->code; |
| 3508 | size_t pos = code->len; |
| 3509 | size_t len = strlen(name) + 1; |
| 3510 | |
| 3511 | bc_vec_expand(code, pos + len); |
| 3512 | strcpy(code->v + pos, name); |
| 3513 | code->len = pos + len; |
| 3514 | #else |
| 3515 | // Smaller code, but way slow: |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3516 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3517 | xc_parse_push(*name); |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3518 | } while (*name++); |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3519 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3520 | } |
| 3521 | |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3522 | // Indexes < 0xfc are encoded verbatim, else first byte is |
| 3523 | // 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes", |
| 3524 | // followed by that many bytes, lsb first. |
| 3525 | // (The above describes 32-bit case). |
| 3526 | #define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t)) |
| 3527 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3528 | static void xc_parse_pushIndex(size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3529 | { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3530 | size_t mask; |
| 3531 | unsigned amt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3532 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 3533 | dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx); |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3534 | if (idx < SMALL_INDEX_LIMIT) { |
| 3535 | goto push_idx; |
| 3536 | } |
| 3537 | |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3538 | mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8); |
| 3539 | amt = sizeof(idx); |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3540 | for (;;) { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3541 | if (idx & mask) break; |
| 3542 | mask >>= 8; |
| 3543 | amt--; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3544 | } |
| 3545 | // amt is at least 1 here - "one byte of length data follows" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3546 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3547 | xc_parse_push((SMALL_INDEX_LIMIT - 1) + amt); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3548 | |
| 3549 | while (idx != 0) { |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3550 | push_idx: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3551 | xc_parse_push((unsigned char)idx); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3552 | idx >>= 8; |
| 3553 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3554 | } |
| 3555 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3556 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3557 | static void bc_parse_pushJUMP(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3558 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3559 | xc_parse_push(BC_INST_JUMP); |
| 3560 | xc_parse_pushIndex(idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3561 | } |
| 3562 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3563 | static void bc_parse_pushJUMP_ZERO(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3564 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3565 | xc_parse_push(BC_INST_JUMP_ZERO); |
| 3566 | xc_parse_pushIndex(idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3567 | } |
| 3568 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3569 | static BC_STATUS zbc_parse_pushSTR(void) |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3570 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3571 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3572 | char *str = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3573 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3574 | xc_parse_push(XC_INST_STR); |
| 3575 | xc_parse_pushIndex(p->func->strs.len); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3576 | bc_vec_push(&p->func->strs, &str); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3577 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3578 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3579 | } |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 3580 | #define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3581 | #endif |
| 3582 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3583 | static void xc_parse_pushNUM(void) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3584 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3585 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3586 | char *num = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3587 | #if ENABLE_BC && ENABLE_DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3588 | 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] | 3589 | #elif ENABLE_BC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3590 | size_t idx = bc_vec_push(&p->func->consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3591 | #else // DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3592 | size_t idx = bc_vec_push(&G.prog.consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3593 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3594 | xc_parse_push(XC_INST_NUM); |
| 3595 | xc_parse_pushIndex(idx); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3596 | } |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3597 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3598 | static BC_STATUS zxc_parse_text_init(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3599 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3600 | G.prs.func = xc_program_func(G.prs.fidx); |
| 3601 | G.prs.lex_inbuf = text; |
| 3602 | G.prs.lex = G.prs.lex_last = XC_LEX_INVALID; |
| 3603 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3604 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3605 | #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] | 3606 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3607 | // Called when parsing or execution detects a failure, |
| 3608 | // resets execution structures. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3609 | static void xc_program_reset(void) |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3610 | { |
| 3611 | BcFunc *f; |
| 3612 | BcInstPtr *ip; |
| 3613 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3614 | bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3615 | bc_vec_pop_all(&G.prog.results); |
| 3616 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3617 | f = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3618 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 3619 | ip->inst_idx = f->code.len; |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3620 | } |
| 3621 | |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3622 | // Called when parsing code detects a failure, |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3623 | // resets parsing structures. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3624 | static void xc_parse_reset(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3625 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3626 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3627 | if (p->fidx != BC_PROG_MAIN) { |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3628 | bc_func_free(p->func); |
| 3629 | bc_func_init(p->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3630 | |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 3631 | p->fidx = BC_PROG_MAIN; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3632 | p->func = xc_program_func_BC_PROG_MAIN(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3633 | } |
| 3634 | |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 3635 | p->lex_inbuf += strlen(p->lex_inbuf); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3636 | p->lex = XC_LEX_EOF; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3637 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3638 | IF_BC(bc_vec_pop_all(&p->exits);) |
| 3639 | IF_BC(bc_vec_pop_all(&p->conds);) |
| 3640 | IF_BC(bc_vec_pop_all(&p->ops);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3641 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3642 | xc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3643 | } |
| 3644 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3645 | static void xc_parse_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3646 | { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3647 | IF_BC(bc_vec_free(&G.prs.exits);) |
| 3648 | IF_BC(bc_vec_free(&G.prs.conds);) |
| 3649 | IF_BC(bc_vec_free(&G.prs.ops);) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3650 | bc_vec_free(&G.prs.lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3651 | } |
| 3652 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3653 | static void xc_parse_create(size_t fidx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3654 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3655 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3656 | memset(p, 0, sizeof(BcParse)); |
| 3657 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3658 | bc_char_vec_init(&p->lex_strnumbuf); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3659 | IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);) |
| 3660 | IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);) |
| 3661 | IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3662 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 3663 | p->fidx = fidx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3664 | p->func = xc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3665 | } |
| 3666 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3667 | static void xc_program_add_fn(void) |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3668 | { |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3669 | //size_t idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3670 | BcFunc f; |
| 3671 | bc_func_init(&f); |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3672 | //idx = |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3673 | bc_vec_push(&G.prog.fns, &f); |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3674 | //return idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | #if ENABLE_BC |
| 3678 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3679 | // Note: takes ownership of 'name' (must be malloced) |
| 3680 | static size_t bc_program_addFunc(char *name) |
| 3681 | { |
| 3682 | size_t idx; |
| 3683 | BcId entry, *entry_ptr; |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3684 | int inserted; |
| 3685 | |
| 3686 | entry.name = name; |
| 3687 | entry.idx = G.prog.fns.len; |
| 3688 | |
| 3689 | inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx); |
| 3690 | if (!inserted) free(name); |
| 3691 | |
| 3692 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
| 3693 | idx = entry_ptr->idx; |
| 3694 | |
| 3695 | if (!inserted) { |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3696 | // There is already a function with this name. |
| 3697 | // It'll be redefined now, clear old definition. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3698 | BcFunc *func = xc_program_func(entry_ptr->idx); |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3699 | bc_func_free(func); |
| 3700 | bc_func_init(func); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3701 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3702 | xc_program_add_fn(); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | return idx; |
| 3706 | } |
| 3707 | |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 3708 | #define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops)) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3709 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3710 | // position of the first operator in the lex enum and adding the position of the |
| 3711 | // first in the expr enum. Note: This only works for binary operators. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3712 | #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] | 3713 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3714 | static BcStatus bc_parse_expr_empty_ok(uint8_t flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3715 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3716 | static BC_STATUS zbc_parse_expr(uint8_t flags) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3717 | { |
| 3718 | BcStatus s; |
| 3719 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3720 | s = bc_parse_expr_empty_ok(flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3721 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
| 3722 | RETURN_STATUS(bc_error("empty expression")); |
| 3723 | RETURN_STATUS(s); |
| 3724 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3725 | #define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3726 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3727 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3728 | #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] | 3729 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3730 | static BC_STATUS zbc_parse_stmt(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3731 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3732 | RETURN_STATUS(zbc_parse_stmt_possibly_auto(false)); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3733 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3734 | #define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3735 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3736 | 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] | 3737 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3738 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3739 | // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines. |
| 3740 | // Same for "else", "while()", "for()". |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3741 | BcStatus s = zbc_lex_next_and_skip_NLINE(); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3742 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3743 | if (p->lex == XC_LEX_NLINE) |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3744 | RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X)); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3745 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3746 | RETURN_STATUS(zbc_parse_stmt()); |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3747 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3748 | #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] | 3749 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3750 | 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] | 3751 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3752 | BcParse *p = &G.prs; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 3753 | char l, r = bc_operation_PREC(type - XC_LEX_1st_op); |
| 3754 | bool left = bc_operation_LEFT(type - XC_LEX_1st_op); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3755 | |
| 3756 | while (p->ops.len > start) { |
Denys Vlasenko | 7b1df3d | 2018-12-14 23:12:48 +0100 | [diff] [blame] | 3757 | BcLexType t = BC_PARSE_TOP_OP(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3758 | if (t == BC_LEX_LPAREN) break; |
| 3759 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 3760 | l = bc_operation_PREC(t - XC_LEX_1st_op); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3761 | if (l >= r && (l != r || !left)) break; |
| 3762 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3763 | xc_parse_push(BC_TOKEN_2_INST(t)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3764 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3765 | *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | bc_vec_push(&p->ops, &type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3769 | } |
| 3770 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3771 | 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] | 3772 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3773 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3774 | BcLexType top; |
| 3775 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3776 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3777 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3778 | top = BC_PARSE_TOP_OP(p); |
| 3779 | |
| 3780 | while (top != BC_LEX_LPAREN) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3781 | xc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3782 | |
| 3783 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3784 | *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3785 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3786 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3787 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3788 | top = BC_PARSE_TOP_OP(p); |
| 3789 | } |
| 3790 | |
| 3791 | bc_vec_pop(&p->ops); |
| 3792 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 3793 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3794 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3795 | #define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3796 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3797 | static BC_STATUS zbc_parse_params(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3798 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3799 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3800 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3801 | size_t nparams; |
| 3802 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3803 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3804 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3805 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3806 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3807 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3808 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3809 | nparams = 0; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3810 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3811 | for (;;) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3812 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3813 | if (s) RETURN_STATUS(s); |
| 3814 | nparams++; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3815 | if (p->lex != BC_LEX_COMMA) { |
| 3816 | if (p->lex == BC_LEX_RPAREN) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3817 | break; |
| 3818 | RETURN_STATUS(bc_error_bad_token()); |
| 3819 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3820 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3821 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3822 | } |
| 3823 | } |
| 3824 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3825 | xc_parse_push(BC_INST_CALL); |
| 3826 | xc_parse_pushIndex(nparams); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3827 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3828 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3829 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3830 | #define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3831 | |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 3832 | // Note: takes ownership of 'name' (must be malloced) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3833 | static BC_STATUS zbc_parse_call(char *name, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3834 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3835 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3836 | BcStatus s; |
| 3837 | BcId entry, *entry_ptr; |
| 3838 | size_t idx; |
| 3839 | |
| 3840 | entry.name = name; |
| 3841 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3842 | s = zbc_parse_params(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3843 | if (s) goto err; |
| 3844 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3845 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3846 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3847 | goto err; |
| 3848 | } |
| 3849 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3850 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3851 | |
| 3852 | if (idx == BC_VEC_INVALID_IDX) { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3853 | // No such function exists, create an empty one |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 3854 | bc_program_addFunc(name); |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3855 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3856 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3857 | free(name); |
| 3858 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3859 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3860 | xc_parse_pushIndex(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3861 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3862 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3863 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3864 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3865 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3866 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3867 | #define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3868 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3869 | static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3870 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3871 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3872 | BcStatus s; |
| 3873 | char *name; |
| 3874 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3875 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3876 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3877 | if (s) goto err; |
| 3878 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3879 | if (p->lex == BC_LEX_LBRACKET) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3880 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3881 | if (s) goto err; |
| 3882 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3883 | if (p->lex == BC_LEX_RBRACKET) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3884 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3885 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3886 | goto err; |
| 3887 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3888 | *type = XC_INST_ARRAY; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3889 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3890 | *type = XC_INST_ARRAY_ELEM; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3891 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3892 | s = zbc_parse_expr(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3893 | if (s) goto err; |
| 3894 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3895 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3896 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3897 | xc_parse_push(*type); |
| 3898 | xc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3899 | free(name); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3900 | } else if (p->lex == BC_LEX_LPAREN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3901 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3902 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3903 | goto err; |
| 3904 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3905 | *type = BC_INST_CALL; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3906 | s = zbc_parse_call(name, flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3907 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3908 | *type = XC_INST_VAR; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3909 | xc_parse_push(XC_INST_VAR); |
| 3910 | xc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3911 | free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3912 | } |
| 3913 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3914 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3915 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3916 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3917 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3918 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3919 | #define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3920 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3921 | static BC_STATUS zbc_parse_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3922 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3923 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3924 | BcStatus s; |
| 3925 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3926 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3927 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3928 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3929 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3930 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3931 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3932 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3933 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3934 | xc_parse_push(XC_INST_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3935 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 3936 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3937 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3938 | #define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3939 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3940 | 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] | 3941 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3942 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3943 | BcStatus s; |
| 3944 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3945 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3946 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3947 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3948 | |
| 3949 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3950 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3951 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3952 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3953 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3954 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3955 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3956 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3957 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3958 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3959 | *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3960 | xc_parse_push(*prev); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3961 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 3962 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3963 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3964 | #define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3965 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3966 | static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3967 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3968 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3969 | BcStatus s; |
| 3970 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3971 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3972 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3973 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3974 | if (p->lex != BC_LEX_LPAREN) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3975 | *type = XC_INST_SCALE; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3976 | xc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3977 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3978 | } |
| 3979 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3980 | *type = XC_INST_SCALE_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3981 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3982 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3983 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3984 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3985 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3986 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3987 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3988 | if (p->lex != BC_LEX_RPAREN) |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3989 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3990 | xc_parse_push(XC_INST_SCALE_FUNC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3991 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3992 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3993 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3994 | #define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3995 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 3996 | 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] | 3997 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3998 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3999 | BcStatus s; |
| 4000 | BcLexType type; |
| 4001 | char inst; |
| 4002 | BcInst etype = *prev; |
| 4003 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4004 | if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM |
| 4005 | || etype == XC_INST_SCALE || etype == BC_INST_LAST |
| 4006 | || etype == XC_INST_IBASE || etype == XC_INST_OBASE |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4007 | ) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4008 | *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4009 | xc_parse_push(inst); |
| 4010 | s = zxc_lex_next(); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4011 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4012 | *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4013 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4014 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4015 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4016 | type = p->lex; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4017 | |
| 4018 | // Because we parse the next part of the expression |
| 4019 | // right here, we need to increment this. |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4020 | *nexs = *nexs + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4021 | |
| 4022 | switch (type) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4023 | case XC_LEX_NAME: |
| 4024 | s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL); |
| 4025 | break; |
| 4026 | case BC_LEX_KEY_IBASE: |
| 4027 | case BC_LEX_KEY_LAST: |
| 4028 | case BC_LEX_KEY_OBASE: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4029 | xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
| 4030 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4031 | break; |
| 4032 | case BC_LEX_KEY_SCALE: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4033 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4034 | if (s) RETURN_STATUS(s); |
| 4035 | if (p->lex == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4036 | s = bc_error_bad_token(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4037 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4038 | xc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4039 | break; |
| 4040 | default: |
| 4041 | s = bc_error_bad_token(); |
| 4042 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4043 | } |
| 4044 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4045 | if (!s) xc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4046 | } |
| 4047 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4048 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4049 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4050 | #define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4051 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4052 | static int bc_parse_inst_isLeaf(BcInst p) |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4053 | { |
| 4054 | return (p >= XC_INST_NUM && p <= XC_INST_SQRT) |
| 4055 | || p == BC_INST_INC_POST |
| 4056 | || p == BC_INST_DEC_POST |
| 4057 | ; |
| 4058 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4059 | #define BC_PARSE_LEAF(prev, bin_last, rparen) \ |
| 4060 | (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev))) |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4061 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4062 | static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn, |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4063 | bool rparen, bool bin_last, size_t *nexprs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4064 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4065 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4066 | BcStatus s; |
| 4067 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4068 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4069 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4070 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4071 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4072 | 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] | 4073 | *prev = BC_TOKEN_2_INST(type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4074 | |
| 4075 | // We can just push onto the op stack because this is the largest |
| 4076 | // precedence operator that gets pushed. Inc/dec does not. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4077 | if (type != XC_LEX_OP_MINUS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4078 | bc_vec_push(&p->ops, &type); |
| 4079 | else |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4080 | bc_parse_operator(type, ops_bgn, nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4081 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4082 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4083 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4084 | #define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4085 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4086 | static BC_STATUS zbc_parse_print(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4087 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4088 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4089 | BcStatus s; |
| 4090 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4091 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4092 | for (;;) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4093 | s = zxc_lex_next(); |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4094 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4095 | type = p->lex; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4096 | if (type == XC_LEX_STR) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4097 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4098 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4099 | s = zbc_parse_expr(0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4100 | } |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4101 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4102 | xc_parse_push(XC_INST_PRINT_POP); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4103 | if (p->lex != BC_LEX_COMMA) |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4104 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4105 | } |
| 4106 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4107 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4108 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4109 | #define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4110 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4111 | static BC_STATUS zbc_parse_return(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4112 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4113 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4114 | BcStatus s; |
| 4115 | BcLexType t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4116 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4117 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4118 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4119 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4120 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4121 | t = p->lex; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4122 | if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4123 | xc_parse_push(BC_INST_RET0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4124 | else { |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4125 | bool paren = (t == BC_LEX_LPAREN); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4126 | s = bc_parse_expr_empty_ok(0); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4127 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4128 | xc_parse_push(BC_INST_RET0); |
| 4129 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4130 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4131 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4132 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4133 | if (!paren || p->lex_last != BC_LEX_RPAREN) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4134 | s = zbc_POSIX_requires("parentheses around return expressions"); |
| 4135 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4136 | } |
| 4137 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4138 | xc_parse_push(XC_INST_RET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4139 | } |
| 4140 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4141 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4142 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4143 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4144 | #define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4145 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4146 | static void rewrite_label_to_current(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4147 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4148 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4149 | size_t *label = bc_vec_item(&p->func->labels, idx); |
| 4150 | *label = p->func->code.len; |
| 4151 | } |
| 4152 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4153 | static BC_STATUS zbc_parse_if(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4154 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4155 | BcParse *p = &G.prs; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4156 | BcStatus s; |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4157 | size_t ip_idx; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4158 | |
| 4159 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4160 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4161 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4162 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4163 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4164 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4165 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4166 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4167 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4168 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4169 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4170 | // Encode "if zero, jump to ..." |
| 4171 | // Pushed value (destination of the jump) is uninitialized, |
| 4172 | // will be rewritten to be address of "end of if()" or of "else". |
| 4173 | ip_idx = bc_vec_push(&p->func->labels, &ip_idx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4174 | bc_parse_pushJUMP_ZERO(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4175 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4176 | s = zbc_parse_stmt_allow_NLINE_before(STRING_if); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4177 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4178 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4179 | dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex); |
| 4180 | if (p->lex == BC_LEX_KEY_ELSE) { |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4181 | size_t ip2_idx; |
Denys Vlasenko | 7415633 | 2018-12-16 21:21:27 +0100 | [diff] [blame] | 4182 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4183 | // Encode "after then_stmt, jump to end of if()" |
| 4184 | ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx); |
| 4185 | 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] | 4186 | bc_parse_pushJUMP(ip2_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4187 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4188 | 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] | 4189 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4190 | |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4191 | ip_idx = ip2_idx; |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4192 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4193 | s = zbc_parse_stmt_allow_NLINE_before(STRING_else); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4194 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4195 | } |
| 4196 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4197 | 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] | 4198 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4199 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4200 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
| 4201 | RETURN_STATUS(s); |
| 4202 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4203 | #define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4204 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4205 | static BC_STATUS zbc_parse_while(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4206 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4207 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4208 | BcStatus s; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4209 | size_t cond_idx; |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4210 | size_t ip_idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4211 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4212 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4213 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4214 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4215 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4216 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4217 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4218 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4219 | ip_idx = cond_idx + 1; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4220 | bc_vec_push(&p->conds, &cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4221 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4222 | bc_vec_push(&p->exits, &ip_idx); |
| 4223 | bc_vec_push(&p->func->labels, &ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4224 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4225 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4226 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4227 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4228 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4229 | bc_parse_pushJUMP_ZERO(ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4230 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4231 | s = zbc_parse_stmt_allow_NLINE_before(STRING_while); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4232 | if (s) RETURN_STATUS(s); |
| 4233 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4234 | 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] | 4235 | bc_parse_pushJUMP(cond_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4236 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4237 | 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] | 4238 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4239 | |
| 4240 | bc_vec_pop(&p->exits); |
| 4241 | bc_vec_pop(&p->conds); |
| 4242 | |
| 4243 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4244 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4245 | #define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS) |
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 | static BC_STATUS zbc_parse_for(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4248 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4249 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4250 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4251 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4252 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4253 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4254 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4255 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4256 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4257 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4258 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4259 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4260 | if (p->lex != BC_LEX_SCOLON) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4261 | s = zbc_parse_expr(0); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4262 | xc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4263 | if (s) RETURN_STATUS(s); |
| 4264 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4265 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
| 4266 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4267 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4268 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4269 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4270 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4271 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4272 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4273 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4274 | update_idx = cond_idx + 1; |
| 4275 | body_idx = update_idx + 1; |
| 4276 | exit_idx = body_idx + 1; |
| 4277 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4278 | if (p->lex != BC_LEX_SCOLON) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4279 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4280 | else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4281 | // Set this for the next call to xc_parse_pushNUM(). |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4282 | // This is safe to set because the current token is a semicolon, |
| 4283 | // which has no string requirement. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4284 | bc_vec_string(&p->lex_strnumbuf, 1, "1"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4285 | xc_parse_pushNUM(); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4286 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4287 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4288 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4289 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4290 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4291 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4292 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4293 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4294 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4295 | bc_parse_pushJUMP_ZERO(exit_idx); |
| 4296 | bc_parse_pushJUMP(body_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4297 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4298 | bc_vec_push(&p->conds, &update_idx); |
| 4299 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4300 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4301 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4302 | s = zbc_parse_expr(0); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4303 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4304 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4305 | xc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4306 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4307 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
| 4308 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4309 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4310 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4311 | bc_parse_pushJUMP(cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4312 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4313 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4314 | bc_vec_push(&p->exits, &exit_idx); |
| 4315 | bc_vec_push(&p->func->labels, &exit_idx); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4316 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4317 | s = zbc_parse_stmt_allow_NLINE_before(STRING_for); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4318 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4319 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4320 | 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] | 4321 | bc_parse_pushJUMP(update_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4322 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4323 | 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] | 4324 | rewrite_label_to_current(exit_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4325 | |
| 4326 | bc_vec_pop(&p->exits); |
| 4327 | bc_vec_pop(&p->conds); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4328 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4329 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4330 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4331 | #define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS) |
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 | static BC_STATUS zbc_parse_break_or_continue(BcLexType type) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4334 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4335 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4336 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4337 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4338 | if (type == BC_LEX_KEY_BREAK) { |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4339 | if (p->exits.len == 0) // none of the enclosing blocks is a loop |
| 4340 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4341 | i = *(size_t*)bc_vec_top(&p->exits); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4342 | } else { |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4343 | i = *(size_t*)bc_vec_top(&p->conds); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4344 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4345 | bc_parse_pushJUMP(i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4346 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4347 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4348 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4349 | #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] | 4350 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4351 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var) |
| 4352 | { |
| 4353 | BcId *autoid; |
| 4354 | BcId a; |
| 4355 | size_t i; |
| 4356 | |
| 4357 | autoid = (void*)f->autos.v; |
| 4358 | for (i = 0; i < f->autos.len; i++, autoid++) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4359 | if (strcmp(name, autoid->name) == 0 |
| 4360 | && var == autoid->idx |
| 4361 | ) { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 4362 | RETURN_STATUS(bc_error("duplicate function parameter or auto name")); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4363 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | a.idx = var; |
| 4367 | a.name = name; |
| 4368 | |
| 4369 | bc_vec_push(&f->autos, &a); |
| 4370 | |
| 4371 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 4372 | } |
| 4373 | #define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS) |
| 4374 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4375 | static BC_STATUS zbc_parse_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4376 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4377 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4378 | BcStatus s; |
| 4379 | bool var, comma = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4380 | char *name; |
| 4381 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4382 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4383 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4384 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4385 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4386 | RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4387 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4388 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4389 | p->fidx = bc_program_addFunc(name); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4390 | p->func = xc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4391 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4392 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4393 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4394 | if (p->lex != BC_LEX_LPAREN) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4395 | RETURN_STATUS(bc_error_bad_function_definition()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4396 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4397 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4398 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4399 | while (p->lex != BC_LEX_RPAREN) { |
| 4400 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4401 | RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4402 | |
| 4403 | ++p->func->nparams; |
| 4404 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4405 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4406 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4407 | if (s) goto err; |
| 4408 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4409 | var = p->lex != BC_LEX_LBRACKET; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4410 | |
| 4411 | if (!var) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4412 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4413 | if (s) goto err; |
| 4414 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4415 | if (p->lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4416 | s = bc_error_bad_function_definition(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4417 | goto err; |
| 4418 | } |
| 4419 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4420 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4421 | if (s) goto err; |
| 4422 | } |
| 4423 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4424 | comma = p->lex == BC_LEX_COMMA; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4425 | if (comma) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4426 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4427 | if (s) goto err; |
| 4428 | } |
| 4429 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4430 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4431 | if (s) goto err; |
| 4432 | } |
| 4433 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4434 | if (comma) RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4435 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4436 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4437 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4438 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4439 | if (p->lex != BC_LEX_LBRACE) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4440 | 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] | 4441 | if (s) RETURN_STATUS(s); |
| 4442 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4443 | |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4444 | // 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] | 4445 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4446 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4447 | //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] | 4448 | if (p->lex != BC_LEX_LBRACE) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4449 | RETURN_STATUS(bc_error("function { body } expected")); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4450 | |
| 4451 | p->in_funcdef++; // to determine whether "return" stmt is allowed, and such |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4452 | s = zbc_parse_stmt_possibly_auto(true); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4453 | p->in_funcdef--; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4454 | if (s) RETURN_STATUS(s); |
| 4455 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4456 | xc_parse_push(BC_INST_RET0); |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 4457 | |
| 4458 | // Subsequent code generation is into main program |
| 4459 | p->fidx = BC_PROG_MAIN; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4460 | p->func = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4461 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4462 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4463 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4464 | err: |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4465 | dbg_lex_done("%s:%d done (error)", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4466 | free(name); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4467 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4468 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4469 | #define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS) |
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 | static BC_STATUS zbc_parse_auto(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4472 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4473 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4474 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4475 | char *name; |
| 4476 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4477 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4478 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4479 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4480 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4481 | for (;;) { |
| 4482 | bool var; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4483 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4484 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4485 | RETURN_STATUS(bc_error_at("bad 'auto' syntax")); |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4486 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4487 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4488 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4489 | if (s) goto err; |
| 4490 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4491 | var = (p->lex != BC_LEX_LBRACKET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4492 | if (!var) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4493 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4494 | if (s) goto err; |
| 4495 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4496 | if (p->lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4497 | s = bc_error_at("bad 'auto' syntax"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4498 | goto err; |
| 4499 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4500 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4501 | if (s) goto err; |
| 4502 | } |
| 4503 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4504 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4505 | if (s) goto err; |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4506 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4507 | if (p->lex == XC_LEX_NLINE |
| 4508 | || p->lex == BC_LEX_SCOLON |
| 4509 | //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}" |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4510 | ) { |
| 4511 | break; |
| 4512 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4513 | if (p->lex != BC_LEX_COMMA) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4514 | RETURN_STATUS(bc_error_at("bad 'auto' syntax")); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4515 | s = zxc_lex_next(); // skip comma |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4516 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4517 | } |
| 4518 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4519 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4520 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4521 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4522 | free(name); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4523 | dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4524 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4525 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4526 | #define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4527 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4528 | #undef zbc_parse_stmt_possibly_auto |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4529 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4530 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4531 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4532 | BcStatus s = BC_STATUS_SUCCESS; |
| 4533 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4534 | 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] | 4535 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4536 | if (p->lex == XC_LEX_NLINE) { |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4537 | dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4538 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4539 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4540 | if (p->lex == BC_LEX_SCOLON) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4541 | dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4542 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4543 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4544 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4545 | if (p->lex == BC_LEX_LBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4546 | dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed); |
| 4547 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4548 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4549 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4550 | } while (p->lex == XC_LEX_NLINE); |
| 4551 | if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4552 | dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4553 | s = zbc_parse_auto(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4554 | if (s) RETURN_STATUS(s); |
| 4555 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4556 | while (p->lex != BC_LEX_RBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4557 | dbg_lex("%s:%d block parsing loop", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4558 | s = zbc_parse_stmt(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4559 | if (s) RETURN_STATUS(s); |
| 4560 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4561 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4562 | dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__); |
| 4563 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4564 | } |
| 4565 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4566 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
| 4567 | switch (p->lex) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4568 | case XC_LEX_OP_MINUS: |
| 4569 | case BC_LEX_OP_INC: |
| 4570 | case BC_LEX_OP_DEC: |
| 4571 | case BC_LEX_OP_BOOL_NOT: |
| 4572 | case BC_LEX_LPAREN: |
| 4573 | case XC_LEX_NAME: |
| 4574 | case XC_LEX_NUMBER: |
| 4575 | case BC_LEX_KEY_IBASE: |
| 4576 | case BC_LEX_KEY_LAST: |
| 4577 | case BC_LEX_KEY_LENGTH: |
| 4578 | case BC_LEX_KEY_OBASE: |
| 4579 | case BC_LEX_KEY_READ: |
| 4580 | case BC_LEX_KEY_SCALE: |
| 4581 | case BC_LEX_KEY_SQRT: |
| 4582 | s = zbc_parse_expr(BC_PARSE_PRINT); |
| 4583 | break; |
| 4584 | case XC_LEX_STR: |
| 4585 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4586 | xc_parse_push(XC_INST_PRINT_STR); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4587 | break; |
| 4588 | case BC_LEX_KEY_BREAK: |
| 4589 | case BC_LEX_KEY_CONTINUE: |
| 4590 | s = zbc_parse_break_or_continue(p->lex); |
| 4591 | break; |
| 4592 | case BC_LEX_KEY_FOR: |
| 4593 | s = zbc_parse_for(); |
| 4594 | break; |
| 4595 | case BC_LEX_KEY_HALT: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4596 | xc_parse_push(BC_INST_HALT); |
| 4597 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4598 | break; |
| 4599 | case BC_LEX_KEY_IF: |
| 4600 | s = zbc_parse_if(); |
| 4601 | break; |
| 4602 | case BC_LEX_KEY_LIMITS: |
| 4603 | // "limits" is a compile-time command, |
| 4604 | // the output is produced at _parse time_. |
| 4605 | printf( |
| 4606 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4607 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4608 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4609 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4610 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4611 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4612 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4613 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4614 | ); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4615 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4616 | break; |
| 4617 | case BC_LEX_KEY_PRINT: |
| 4618 | s = zbc_parse_print(); |
| 4619 | break; |
| 4620 | case BC_LEX_KEY_QUIT: |
| 4621 | // "quit" is a compile-time command. For example, |
| 4622 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4623 | // not when it is executed |
| 4624 | QUIT_OR_RETURN_TO_MAIN; |
| 4625 | case BC_LEX_KEY_RETURN: |
| 4626 | if (!p->in_funcdef) |
| 4627 | RETURN_STATUS(bc_error("'return' not in a function")); |
| 4628 | s = zbc_parse_return(); |
| 4629 | break; |
| 4630 | case BC_LEX_KEY_WHILE: |
| 4631 | s = zbc_parse_while(); |
| 4632 | break; |
| 4633 | default: |
| 4634 | s = bc_error_bad_token(); |
| 4635 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4636 | } |
| 4637 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4638 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4639 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4640 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4641 | #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] | 4642 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4643 | static BC_STATUS zbc_parse_stmt_or_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4644 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4645 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4646 | BcStatus s; |
| 4647 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4648 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4649 | if (p->lex == XC_LEX_EOF) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4650 | s = bc_error("end of file"); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4651 | else if (p->lex == BC_LEX_KEY_DEFINE) { |
| 4652 | dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4653 | s = zbc_parse_funcdef(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4654 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4655 | 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] | 4656 | s = zbc_parse_stmt(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4657 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4658 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4659 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4660 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4661 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4662 | #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] | 4663 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 4664 | // 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] | 4665 | static BcStatus bc_parse_expr_empty_ok(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4666 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4667 | BcParse *p = &G.prs; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4668 | BcInst prev = XC_INST_PRINT; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4669 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4670 | unsigned nparens, nrelops; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4671 | bool paren_first, rprn, assign, bin_last, incdec; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4672 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4673 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4674 | paren_first = (p->lex == BC_LEX_LPAREN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4675 | nparens = nrelops = 0; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4676 | rprn = assign = incdec = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4677 | bin_last = true; |
| 4678 | |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4679 | for (;;) { |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4680 | bool get_token; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4681 | BcStatus s; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4682 | BcLexType t = p->lex; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4683 | |
| 4684 | if (!lex_allowed_in_bc_expr(t)) |
| 4685 | break; |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4686 | |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4687 | dbg_lex("%s:%d t:%d", __func__, __LINE__, t); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4688 | get_token = false; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4689 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4690 | switch (t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4691 | case BC_LEX_OP_INC: |
| 4692 | case BC_LEX_OP_DEC: |
| 4693 | dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4694 | if (incdec) return bc_error_bad_assignment(); |
| 4695 | s = zbc_parse_incdec(&prev, &nexprs, flags); |
| 4696 | incdec = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4697 | rprn = bin_last = false; |
| 4698 | //get_token = false; - already is |
| 4699 | break; |
| 4700 | case XC_LEX_OP_MINUS: |
| 4701 | dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4702 | s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4703 | rprn = false; |
| 4704 | //get_token = false; - already is |
| 4705 | bin_last = (prev == XC_INST_MINUS); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4706 | if (bin_last) incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4707 | break; |
| 4708 | case BC_LEX_OP_ASSIGN_POWER: |
| 4709 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4710 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4711 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4712 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4713 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4714 | case BC_LEX_OP_ASSIGN: |
| 4715 | dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__); |
| 4716 | if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM |
| 4717 | && prev != XC_INST_SCALE && prev != XC_INST_IBASE |
| 4718 | && prev != XC_INST_OBASE && prev != BC_INST_LAST |
| 4719 | ) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4720 | return bc_error_bad_assignment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4721 | } |
| 4722 | // Fallthrough. |
| 4723 | case XC_LEX_OP_POWER: |
| 4724 | case XC_LEX_OP_MULTIPLY: |
| 4725 | case XC_LEX_OP_DIVIDE: |
| 4726 | case XC_LEX_OP_MODULUS: |
| 4727 | case XC_LEX_OP_PLUS: |
| 4728 | case XC_LEX_OP_REL_EQ: |
| 4729 | case XC_LEX_OP_REL_LE: |
| 4730 | case XC_LEX_OP_REL_GE: |
| 4731 | case XC_LEX_OP_REL_NE: |
| 4732 | case XC_LEX_OP_REL_LT: |
| 4733 | case XC_LEX_OP_REL_GT: |
| 4734 | case BC_LEX_OP_BOOL_NOT: |
| 4735 | case BC_LEX_OP_BOOL_OR: |
| 4736 | case BC_LEX_OP_BOOL_AND: |
| 4737 | dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4738 | if (t == BC_LEX_OP_BOOL_NOT) { |
| 4739 | if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT) |
| 4740 | return bc_error_bad_expression(); |
| 4741 | } else if (prev == XC_INST_BOOL_NOT) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4742 | return bc_error_bad_expression(); |
| 4743 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4744 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4745 | nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT); |
| 4746 | prev = BC_TOKEN_2_INST(t); |
| 4747 | bc_parse_operator(t, ops_bgn, &nexprs); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4748 | rprn = incdec = false; |
| 4749 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4750 | bin_last = (t != BC_LEX_OP_BOOL_NOT); |
| 4751 | break; |
| 4752 | case BC_LEX_LPAREN: |
| 4753 | dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4754 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4755 | return bc_error_bad_expression(); |
| 4756 | bc_vec_push(&p->ops, &t); |
| 4757 | nparens++; |
| 4758 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4759 | rprn = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4760 | break; |
| 4761 | case BC_LEX_RPAREN: |
| 4762 | dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4763 | if (p->lex_last == BC_LEX_LPAREN) { |
| 4764 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); |
| 4765 | return BC_STATUS_PARSE_EMPTY_EXP; |
| 4766 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4767 | if (bin_last || prev == XC_INST_BOOL_NOT) |
| 4768 | return bc_error_bad_expression(); |
| 4769 | if (nparens == 0) { |
| 4770 | goto exit_loop; |
| 4771 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4772 | s = zbc_parse_rightParen(ops_bgn, &nexprs); |
| 4773 | nparens--; |
| 4774 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4775 | rprn = true; |
| 4776 | bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4777 | break; |
| 4778 | case XC_LEX_NAME: |
| 4779 | dbg_lex("%s:%d LEX_NAME", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4780 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4781 | return bc_error_bad_expression(); |
| 4782 | s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4783 | rprn = (prev == BC_INST_CALL); |
| 4784 | bin_last = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4785 | //get_token = false; - already is |
| 4786 | nexprs++; |
| 4787 | break; |
| 4788 | case XC_LEX_NUMBER: |
| 4789 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4790 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4791 | return bc_error_bad_expression(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4792 | xc_parse_pushNUM(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4793 | prev = XC_INST_NUM; |
| 4794 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4795 | rprn = bin_last = false; |
| 4796 | nexprs++; |
| 4797 | break; |
| 4798 | case BC_LEX_KEY_IBASE: |
| 4799 | case BC_LEX_KEY_LAST: |
| 4800 | case BC_LEX_KEY_OBASE: |
| 4801 | dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4802 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4803 | return bc_error_bad_expression(); |
| 4804 | prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4805 | xc_parse_push((char) prev); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4806 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4807 | rprn = bin_last = false; |
| 4808 | nexprs++; |
| 4809 | break; |
| 4810 | case BC_LEX_KEY_LENGTH: |
| 4811 | case BC_LEX_KEY_SQRT: |
| 4812 | dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4813 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4814 | return bc_error_bad_expression(); |
| 4815 | s = zbc_parse_builtin(t, flags, &prev); |
| 4816 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4817 | rprn = bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4818 | nexprs++; |
| 4819 | break; |
| 4820 | case BC_LEX_KEY_READ: |
| 4821 | dbg_lex("%s:%d LEX_READ", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4822 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4823 | return bc_error_bad_expression(); |
| 4824 | s = zbc_parse_read(); |
| 4825 | prev = XC_INST_READ; |
| 4826 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4827 | rprn = bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4828 | nexprs++; |
| 4829 | break; |
| 4830 | case BC_LEX_KEY_SCALE: |
| 4831 | dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 4832 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4833 | return bc_error_bad_expression(); |
| 4834 | s = zbc_parse_scale(&prev, flags); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4835 | //get_token = false; - already is |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4836 | rprn = bin_last = false; |
| 4837 | nexprs++; |
| 4838 | break; |
| 4839 | default: |
| 4840 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4841 | } |
| 4842 | |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4843 | if (s || G_interrupt) // error, or ^C: stop parsing |
| 4844 | return BC_STATUS_FAILURE; |
| 4845 | if (get_token) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4846 | s = zxc_lex_next(); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4847 | if (s) return s; |
| 4848 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4849 | } |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4850 | exit_loop: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4851 | |
| 4852 | while (p->ops.len > ops_bgn) { |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4853 | BcLexType top = BC_PARSE_TOP_OP(p); |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 4854 | assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4855 | |
| 4856 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4857 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4858 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4859 | xc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4860 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4861 | nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4862 | bc_vec_pop(&p->ops); |
| 4863 | } |
| 4864 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4865 | if (prev == XC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4866 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4867 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4868 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4869 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4870 | s = zbc_POSIX_does_not_allow("comparison operators outside if or loops"); |
| 4871 | if (s) return s; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4872 | } else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4873 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4874 | s = zbc_POSIX_requires("exactly one comparison operator per condition"); |
| 4875 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | if (flags & BC_PARSE_PRINT) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4879 | if (paren_first || !assign) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4880 | xc_parse_push(XC_INST_PRINT); |
| 4881 | xc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4882 | } |
| 4883 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4884 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4885 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4886 | } |
| 4887 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4888 | #endif // ENABLE_BC |
| 4889 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 4890 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 4891 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4892 | static BC_STATUS zdc_parse_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4893 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4894 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4895 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4896 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4897 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4898 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4899 | if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4900 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4901 | xc_parse_pushName(p->lex_strnumbuf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4902 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4903 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4904 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4905 | #define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS) |
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 | static void dc_parse_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4908 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4909 | BcParse *p = &G.prs; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4910 | char *str; |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4911 | size_t len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4912 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4913 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4914 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4915 | str = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4916 | xc_parse_push(XC_INST_STR); |
| 4917 | xc_parse_pushIndex(len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4918 | bc_vec_push(&G.prog.strs, &str); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4919 | |
| 4920 | // Explanation needed here |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4921 | xc_program_add_fn(); |
| 4922 | p->func = xc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4923 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4924 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4925 | } |
| 4926 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4927 | 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] | 4928 | { |
| 4929 | BcStatus s; |
| 4930 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4931 | xc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4932 | if (name) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4933 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4934 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | if (store) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4938 | xc_parse_push(DC_INST_SWAP); |
| 4939 | xc_parse_push(XC_INST_ASSIGN); |
| 4940 | xc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4941 | } |
| 4942 | |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4943 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4944 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4945 | #define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS) |
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 | static BC_STATUS zdc_parse_cond(uint8_t inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4948 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4949 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4950 | BcStatus s; |
| 4951 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4952 | xc_parse_push(inst); |
| 4953 | xc_parse_push(DC_INST_EXEC_COND); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4954 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4955 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4956 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4957 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4958 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4959 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4960 | |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4961 | // Note that 'else' part can not be on the next line: |
| 4962 | // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2" |
| 4963 | // 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] | 4964 | if (p->lex == DC_LEX_ELSE) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4965 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4966 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4967 | s = zxc_lex_next(); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4968 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4969 | xc_parse_push('\0'); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4970 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4971 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4972 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4973 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4974 | #define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4975 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4976 | static BC_STATUS zdc_parse_token(BcLexType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4977 | { |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4978 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4979 | uint8_t inst; |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4980 | bool assign, get_token; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4981 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4982 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4983 | s = BC_STATUS_SUCCESS; |
| 4984 | get_token = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4985 | switch (t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4986 | case XC_LEX_OP_REL_EQ: |
| 4987 | case XC_LEX_OP_REL_LE: |
| 4988 | case XC_LEX_OP_REL_GE: |
| 4989 | case XC_LEX_OP_REL_NE: |
| 4990 | case XC_LEX_OP_REL_LT: |
| 4991 | case XC_LEX_OP_REL_GT: |
| 4992 | dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__); |
| 4993 | s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ); |
| 4994 | get_token = false; |
| 4995 | break; |
| 4996 | case DC_LEX_SCOLON: |
| 4997 | case DC_LEX_COLON: |
| 4998 | dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__); |
| 4999 | s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON); |
| 5000 | break; |
| 5001 | case XC_LEX_STR: |
| 5002 | dbg_lex("%s:%d LEX_STR", __func__, __LINE__); |
| 5003 | dc_parse_string(); |
| 5004 | break; |
| 5005 | case XC_LEX_NEG: |
| 5006 | dbg_lex("%s:%d LEX_NEG", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5007 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5008 | if (s) RETURN_STATUS(s); |
| 5009 | if (G.prs.lex != XC_LEX_NUMBER) |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5010 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5011 | xc_parse_pushNUM(); |
| 5012 | xc_parse_push(XC_INST_NEG); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5013 | break; |
| 5014 | case XC_LEX_NUMBER: |
| 5015 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5016 | xc_parse_pushNUM(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5017 | break; |
| 5018 | case DC_LEX_READ: |
| 5019 | dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5020 | xc_parse_push(XC_INST_READ); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5021 | break; |
| 5022 | case DC_LEX_OP_ASSIGN: |
| 5023 | case DC_LEX_STORE_PUSH: |
| 5024 | dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__); |
| 5025 | assign = (t == DC_LEX_OP_ASSIGN); |
| 5026 | inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR; |
| 5027 | s = zdc_parse_mem(inst, true, assign); |
| 5028 | break; |
| 5029 | case DC_LEX_LOAD: |
| 5030 | case DC_LEX_LOAD_POP: |
| 5031 | dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__); |
| 5032 | inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD; |
| 5033 | s = zdc_parse_mem(inst, true, false); |
| 5034 | break; |
| 5035 | case DC_LEX_STORE_IBASE: |
| 5036 | case DC_LEX_STORE_SCALE: |
| 5037 | case DC_LEX_STORE_OBASE: |
| 5038 | dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__); |
| 5039 | inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE; |
| 5040 | s = zdc_parse_mem(inst, false, true); |
| 5041 | break; |
| 5042 | default: |
| 5043 | dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__); |
| 5044 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5045 | } |
| 5046 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5047 | if (!s && get_token) s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5048 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5049 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5050 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5051 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5052 | #define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS) |
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 | static BC_STATUS zdc_parse_expr(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5055 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5056 | BcParse *p = &G.prs; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5057 | int i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5058 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5059 | i = (int)p->lex - (int)XC_LEX_OP_POWER; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5060 | if (i >= 0) { |
| 5061 | BcInst inst = dc_LEX_to_INST[i]; |
| 5062 | if (inst != DC_INST_INVALID) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5063 | xc_parse_push(inst); |
| 5064 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5065 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5066 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5067 | RETURN_STATUS(zdc_parse_token(p->lex)); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5068 | } |
| 5069 | #define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
| 5070 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5071 | static BC_STATUS zdc_parse_exprs_until_eof(void) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5072 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5073 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5074 | dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex); |
| 5075 | while (p->lex != XC_LEX_EOF) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5076 | BcStatus s = zdc_parse_expr(); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5077 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5078 | } |
| 5079 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5080 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5081 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5082 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5083 | #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] | 5084 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5085 | #endif // ENABLE_DC |
| 5086 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5087 | // |
| 5088 | // Execution engine |
| 5089 | // |
| 5090 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 5091 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 5092 | #define BC_PROG_NUM(r, n) \ |
| 5093 | ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n)) |
| 5094 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5095 | #define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n))) |
| 5096 | #define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n))) |
| 5097 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5098 | static BcVec* xc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5099 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5100 | BcId e, *ptr; |
| 5101 | BcVec *v, *map; |
| 5102 | size_t i; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5103 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5104 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5105 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5106 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5107 | |
| 5108 | e.name = id; |
| 5109 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5110 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5111 | |
| 5112 | if (new) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5113 | BcVec v2; |
| 5114 | bc_array_init(&v2, var); |
| 5115 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | ptr = bc_vec_item(map, i); |
| 5119 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5120 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5121 | } |
| 5122 | |
Denys Vlasenko | 8287b1c | 2018-12-21 22:43:53 +0100 | [diff] [blame] | 5123 | // 'num' need not be initialized on entry |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5124 | static BC_STATUS zxc_program_num(BcResult *r, BcNum **num) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5125 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5126 | switch (r->t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5127 | case XC_RESULT_STR: |
| 5128 | case XC_RESULT_TEMP: |
| 5129 | case XC_RESULT_IBASE: |
| 5130 | case XC_RESULT_SCALE: |
| 5131 | case XC_RESULT_OBASE: |
| 5132 | *num = &r->d.n; |
| 5133 | break; |
| 5134 | case XC_RESULT_CONSTANT: { |
| 5135 | BcStatus s; |
| 5136 | char *str; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5137 | size_t len; |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 5138 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5139 | str = *xc_program_const(r->d.id.idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5140 | len = strlen(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5141 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5142 | bc_num_init(&r->d.n, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5143 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5144 | s = zxc_num_parse(&r->d.n, str, G.prog.ib_t); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5145 | if (s) { |
| 5146 | bc_num_free(&r->d.n); |
| 5147 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5148 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5149 | *num = &r->d.n; |
| 5150 | r->t = XC_RESULT_TEMP; |
| 5151 | break; |
| 5152 | } |
| 5153 | case XC_RESULT_VAR: |
| 5154 | case XC_RESULT_ARRAY: |
| 5155 | case XC_RESULT_ARRAY_ELEM: { |
| 5156 | BcVec *v; |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5157 | void *p; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5158 | 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] | 5159 | // dc variables are all stacks, so here we have this: |
| 5160 | p = bc_vec_top(v); |
| 5161 | // TODO: eliminate these stacks for bc-only config? |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5162 | if (r->t == XC_RESULT_ARRAY_ELEM) { |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5163 | v = p; |
| 5164 | if (v->len <= r->d.id.idx) |
| 5165 | bc_array_expand(v, r->d.id.idx + 1); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5166 | *num = bc_vec_item(v, r->d.id.idx); |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5167 | } else { |
| 5168 | *num = p; |
| 5169 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5170 | break; |
| 5171 | } |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5172 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5173 | case BC_RESULT_LAST: |
| 5174 | *num = &G.prog.last; |
| 5175 | break; |
| 5176 | case BC_RESULT_ONE: |
| 5177 | *num = &G.prog.one; |
| 5178 | break; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5179 | #endif |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5180 | #if SANITY_CHECKS |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5181 | default: |
| 5182 | // Testing the theory that dc does not reach LAST/ONE |
| 5183 | bb_error_msg_and_die("BUG:%d", r->t); |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5184 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5185 | } |
| 5186 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5187 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5188 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5189 | #define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5190 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5191 | static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5192 | BcResult **r, BcNum **rn, bool assign) |
| 5193 | { |
| 5194 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5195 | BcResultType lt, rt; |
| 5196 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5197 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5198 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5199 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5200 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5201 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5202 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5203 | s = zxc_program_num(*l, ln); |
| 5204 | if (s) RETURN_STATUS(s); |
| 5205 | s = zxc_program_num(*r, rn); |
| 5206 | if (s) RETURN_STATUS(s); |
| 5207 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5208 | lt = (*l)->t; |
| 5209 | rt = (*r)->t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5210 | |
| 5211 | // We run this again under these conditions in case any vector has been |
| 5212 | // reallocated out from under the BcNums or arrays we had. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5213 | if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5214 | s = zxc_program_num(*l, ln); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5215 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5216 | } |
| 5217 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5218 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5219 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5220 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5221 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5222 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5223 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5224 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5225 | #define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5226 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5227 | static void xc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5228 | { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5229 | r->t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5230 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5231 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5232 | } |
| 5233 | |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5234 | // Note: *r and *n need not be initialized by caller |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5235 | static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5236 | { |
| 5237 | BcStatus s; |
| 5238 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5239 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5240 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5241 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5242 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5243 | s = zxc_program_num(*r, n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5244 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5245 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5246 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5247 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5248 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5249 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5250 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5251 | #define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5252 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5253 | static void xc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5254 | { |
| 5255 | r->t = t; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5256 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5257 | } |
| 5258 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5259 | static BC_STATUS zxc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5260 | { |
| 5261 | BcStatus s; |
| 5262 | BcResult *opd1, *opd2, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5263 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5264 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5265 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5266 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5267 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5268 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5269 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5270 | 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] | 5271 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5272 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5273 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5274 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5275 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5276 | bc_num_free(&res.d.n); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5277 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5278 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5279 | #define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5280 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5281 | static BC_STATUS zxc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5282 | { |
| 5283 | BcStatus s; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5284 | BcParse sv_parse; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5285 | BcVec buf; |
| 5286 | BcInstPtr ip; |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5287 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5288 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 5289 | bc_char_vec_init(&buf); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5290 | xc_read_line(&buf, stdin); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5291 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5292 | f = xc_program_func(BC_PROG_READ); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5293 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5294 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5295 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5296 | xc_parse_create(BC_PROG_READ); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 5297 | //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] | 5298 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5299 | s = zxc_parse_text_init(buf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5300 | if (s) goto exec_err; |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5301 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5302 | IF_BC(s = zbc_parse_expr(0)); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5303 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5304 | IF_DC(s = zdc_parse_exprs_until_eof()); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5305 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5306 | if (s) goto exec_err; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5307 | 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^] | 5308 | s = bc_error_at("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5309 | goto exec_err; |
| 5310 | } |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 5311 | xc_parse_push(XC_INST_RET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5312 | |
| 5313 | ip.func = BC_PROG_READ; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5314 | ip.inst_idx = 0; |
| 5315 | IF_BC(ip.results_len_before_call = G.prog.results.len;) |
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 | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5362 | if (!str[0]) { |
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) { |
| 5370 | int c = *str++; |
| 5371 | if (c != '\\' || !*str) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5372 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5373 | else { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5374 | c = *str++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5375 | switch (c) { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5376 | case 'a': |
| 5377 | bb_putchar('\a'); |
| 5378 | break; |
| 5379 | case 'b': |
| 5380 | bb_putchar('\b'); |
| 5381 | break; |
| 5382 | case '\\': |
| 5383 | case 'e': |
| 5384 | bb_putchar('\\'); |
| 5385 | break; |
| 5386 | case 'f': |
| 5387 | bb_putchar('\f'); |
| 5388 | break; |
| 5389 | case 'n': |
| 5390 | bb_putchar('\n'); |
| 5391 | G.prog.nchars = SIZE_MAX; |
| 5392 | break; |
| 5393 | case 'r': |
| 5394 | bb_putchar('\r'); |
| 5395 | break; |
| 5396 | case 'q': |
| 5397 | bb_putchar('"'); |
| 5398 | break; |
| 5399 | case 't': |
| 5400 | bb_putchar('\t'); |
| 5401 | break; |
| 5402 | default: |
| 5403 | // Just print the backslash and following character. |
| 5404 | bb_putchar('\\'); |
| 5405 | ++G.prog.nchars; |
| 5406 | bb_putchar(c); |
| 5407 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5408 | } |
| 5409 | } |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5410 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5411 | } |
| 5412 | } |
| 5413 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5414 | static void bc_num_printNewline(void) |
| 5415 | { |
| 5416 | if (G.prog.nchars == G.prog.len - 1) { |
| 5417 | bb_putchar('\\'); |
| 5418 | bb_putchar('\n'); |
| 5419 | G.prog.nchars = 0; |
| 5420 | } |
| 5421 | } |
| 5422 | |
| 5423 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5424 | 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] | 5425 | { |
| 5426 | (void) radix; |
| 5427 | bb_putchar((char) num); |
| 5428 | G.prog.nchars += width; |
| 5429 | } |
| 5430 | #endif |
| 5431 | |
| 5432 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
| 5433 | { |
| 5434 | size_t exp, pow; |
| 5435 | |
| 5436 | bc_num_printNewline(); |
| 5437 | bb_putchar(radix ? '.' : ' '); |
| 5438 | ++G.prog.nchars; |
| 5439 | |
| 5440 | bc_num_printNewline(); |
| 5441 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 5442 | continue; |
| 5443 | |
| 5444 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
| 5445 | size_t dig; |
| 5446 | bc_num_printNewline(); |
| 5447 | dig = num / pow; |
| 5448 | num -= dig * pow; |
| 5449 | bb_putchar(((char) dig) + '0'); |
| 5450 | } |
| 5451 | } |
| 5452 | |
| 5453 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
| 5454 | { |
| 5455 | if (radix) { |
| 5456 | bc_num_printNewline(); |
| 5457 | bb_putchar('.'); |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5458 | G.prog.nchars++; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | bc_num_printNewline(); |
| 5462 | bb_putchar(bb_hexdigits_upcase[num]); |
| 5463 | G.prog.nchars += width; |
| 5464 | } |
| 5465 | |
| 5466 | static void bc_num_printDecimal(BcNum *n) |
| 5467 | { |
| 5468 | size_t i, rdx = n->rdx - 1; |
| 5469 | |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5470 | if (n->neg) { |
| 5471 | bb_putchar('-'); |
| 5472 | G.prog.nchars++; |
| 5473 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5474 | |
| 5475 | for (i = n->len - 1; i < n->len; --i) |
| 5476 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
| 5477 | } |
| 5478 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5479 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
| 5480 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5481 | 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] | 5482 | { |
| 5483 | BcStatus s; |
| 5484 | BcVec stack; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5485 | BcNum base; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5486 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5487 | BcNum intp, fracp, digit, frac_len; |
| 5488 | unsigned long dig, *ptr; |
| 5489 | size_t i; |
| 5490 | bool radix; |
| 5491 | |
| 5492 | if (n->len == 0) { |
| 5493 | print(0, width, false); |
| 5494 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5495 | } |
| 5496 | |
| 5497 | bc_vec_init(&stack, sizeof(long), NULL); |
| 5498 | bc_num_init(&intp, n->len); |
| 5499 | bc_num_init(&fracp, n->rdx); |
| 5500 | bc_num_init(&digit, width); |
| 5501 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 5502 | bc_num_copy(&intp, n); |
| 5503 | bc_num_one(&frac_len); |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5504 | base.cap = ARRAY_SIZE(base_digs); |
| 5505 | base.num = base_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5506 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5507 | |
| 5508 | bc_num_truncate(&intp, intp.rdx); |
| 5509 | s = zbc_num_sub(n, &intp, &fracp, 0); |
| 5510 | if (s) goto err; |
| 5511 | |
| 5512 | while (intp.len != 0) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5513 | s = zbc_num_divmod(&intp, &base, &intp, &digit, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5514 | if (s) goto err; |
| 5515 | s = zbc_num_ulong(&digit, &dig); |
| 5516 | if (s) goto err; |
| 5517 | bc_vec_push(&stack, &dig); |
| 5518 | } |
| 5519 | |
| 5520 | for (i = 0; i < stack.len; ++i) { |
| 5521 | ptr = bc_vec_item_rev(&stack, i); |
| 5522 | print(*ptr, width, false); |
| 5523 | } |
| 5524 | |
| 5525 | if (!n->rdx) goto err; |
| 5526 | |
| 5527 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5528 | s = zbc_num_mul(&fracp, &base, &fracp, n->rdx); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5529 | if (s) goto err; |
| 5530 | s = zbc_num_ulong(&fracp, &dig); |
| 5531 | if (s) goto err; |
| 5532 | bc_num_ulong2num(&intp, dig); |
| 5533 | s = zbc_num_sub(&fracp, &intp, &fracp, 0); |
| 5534 | if (s) goto err; |
| 5535 | print(dig, width, radix); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5536 | s = zbc_num_mul(&frac_len, &base, &frac_len, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5537 | if (s) goto err; |
| 5538 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5539 | err: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5540 | bc_num_free(&frac_len); |
| 5541 | bc_num_free(&digit); |
| 5542 | bc_num_free(&fracp); |
| 5543 | bc_num_free(&intp); |
| 5544 | bc_vec_free(&stack); |
| 5545 | RETURN_STATUS(s); |
| 5546 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5547 | #define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5548 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5549 | static BC_STATUS zxc_num_printBase(BcNum *n) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5550 | { |
| 5551 | BcStatus s; |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5552 | size_t width; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5553 | BcNumDigitOp print; |
| 5554 | bool neg = n->neg; |
| 5555 | |
| 5556 | if (neg) { |
| 5557 | bb_putchar('-'); |
| 5558 | G.prog.nchars++; |
| 5559 | } |
| 5560 | |
| 5561 | n->neg = false; |
| 5562 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 5563 | if (G.prog.ob_t <= 16) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5564 | width = 1; |
| 5565 | print = bc_num_printHex; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5566 | } else { |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5567 | unsigned i = G.prog.ob_t - 1; |
| 5568 | width = 0; |
| 5569 | for (;;) { |
| 5570 | width++; |
| 5571 | i /= 10; |
| 5572 | if (i == 0) |
| 5573 | break; |
| 5574 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5575 | print = bc_num_printDigits; |
| 5576 | } |
| 5577 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5578 | s = zxc_num_printNum(n, G.prog.ob_t, width, print); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5579 | n->neg = neg; |
| 5580 | |
| 5581 | RETURN_STATUS(s); |
| 5582 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5583 | #define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5584 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5585 | static BC_STATUS zxc_num_print(BcNum *n, bool newline) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5586 | { |
| 5587 | BcStatus s = BC_STATUS_SUCCESS; |
| 5588 | |
| 5589 | bc_num_printNewline(); |
| 5590 | |
| 5591 | if (n->len == 0) { |
| 5592 | bb_putchar('0'); |
| 5593 | ++G.prog.nchars; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5594 | } else if (G.prog.ob_t == 10) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5595 | bc_num_printDecimal(n); |
| 5596 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5597 | s = zxc_num_printBase(n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5598 | |
| 5599 | if (newline) { |
| 5600 | bb_putchar('\n'); |
| 5601 | G.prog.nchars = 0; |
| 5602 | } |
| 5603 | |
| 5604 | RETURN_STATUS(s); |
| 5605 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5606 | #define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5607 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5608 | static BC_STATUS zxc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5609 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5610 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5611 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5612 | BcNum *num; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5613 | bool pop = (inst != XC_INST_PRINT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5614 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5615 | if (!STACK_HAS_MORE_THAN(&G.prog.results, idx)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5616 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5617 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5618 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5619 | s = zxc_program_num(r, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5620 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5621 | |
| 5622 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5623 | s = zxc_num_print(num, !pop); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5624 | #if ENABLE_BC |
| 5625 | if (!s && IS_BC) bc_num_copy(&G.prog.last, num); |
| 5626 | #endif |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5627 | } else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5628 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5629 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5630 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5631 | str = *xc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5632 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5633 | if (inst == XC_INST_PRINT_STR) { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5634 | for (;;) { |
| 5635 | char c = *str++; |
| 5636 | if (c == '\0') break; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5637 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5638 | ++G.prog.nchars; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5639 | if (c == '\n') G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5640 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5641 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5642 | xc_program_printString(str); |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5643 | if (inst == XC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5644 | } |
| 5645 | } |
| 5646 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5647 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5648 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5649 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5650 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5651 | #define zxc_program_print(...) (zxc_program_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5652 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5653 | static BC_STATUS zxc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5654 | { |
| 5655 | BcStatus s; |
| 5656 | BcResult res, *ptr; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5657 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5658 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5659 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5660 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5661 | |
| 5662 | bc_num_init(&res.d.n, num->len); |
| 5663 | bc_num_copy(&res.d.n, num); |
| 5664 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5665 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5666 | xc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5667 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5668 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5669 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5670 | #define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5671 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5672 | static BC_STATUS zxc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5673 | { |
| 5674 | BcStatus s; |
| 5675 | BcResult *opd1, *opd2, res; |
| 5676 | BcNum *n1, *n2; |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5677 | ssize_t cond; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5678 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5679 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5680 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5681 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5682 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5683 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5684 | if (inst == XC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5685 | 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] | 5686 | else if (inst == XC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5687 | 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] | 5688 | else { |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5689 | cond = bc_num_cmp(n1, n2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5690 | switch (inst) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5691 | case XC_INST_REL_EQ: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5692 | cond = (cond == 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5693 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5694 | case XC_INST_REL_LE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5695 | cond = (cond <= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5696 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5697 | case XC_INST_REL_GE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5698 | cond = (cond >= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5699 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5700 | case XC_INST_REL_LT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5701 | cond = (cond < 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5702 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5703 | case XC_INST_REL_GT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5704 | cond = (cond > 0); |
| 5705 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5706 | default: // = case XC_INST_REL_NE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5707 | //cond = (cond != 0); - not needed |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5708 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5709 | } |
| 5710 | } |
| 5711 | |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5712 | if (cond) bc_num_one(&res.d.n); |
| 5713 | //else bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5714 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5715 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5716 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5717 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5718 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5719 | #define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5720 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5721 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5722 | static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5723 | { |
| 5724 | BcNum n2; |
| 5725 | BcResult res; |
| 5726 | |
| 5727 | memset(&n2, 0, sizeof(BcNum)); |
| 5728 | n2.rdx = res.d.id.idx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5729 | res.t = XC_RESULT_STR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5730 | |
| 5731 | if (!push) { |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5732 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5733 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5734 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5735 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5736 | } |
| 5737 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5738 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5739 | bc_vec_push(v, &n2); |
| 5740 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5741 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5742 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5743 | #define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5744 | #endif // ENABLE_DC |
| 5745 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5746 | static BC_STATUS zxc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5747 | { |
| 5748 | BcStatus s; |
| 5749 | BcResult *ptr, r; |
| 5750 | BcVec *v; |
| 5751 | BcNum *n; |
| 5752 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5753 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5754 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5755 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5756 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5757 | if ((ptr->t == XC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5758 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5759 | v = xc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5760 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5761 | #if ENABLE_DC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5762 | if (ptr->t == XC_RESULT_STR && !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5763 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5764 | if (ptr->t == XC_RESULT_STR) |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5765 | RETURN_STATUS(zdc_program_assignStr(ptr, v, true)); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5766 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5767 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5768 | s = zxc_program_num(ptr, &n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5769 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5770 | |
| 5771 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5772 | v = xc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5773 | |
| 5774 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5775 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5776 | bc_num_copy(&r.d.n, n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5777 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5778 | bc_array_init(&r.d.v, true); |
| 5779 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 5780 | } |
| 5781 | |
| 5782 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5783 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5784 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5785 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5786 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5787 | #define zxc_program_copyToVar(...) (zxc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5788 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5789 | static BC_STATUS zxc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5790 | { |
| 5791 | BcStatus s; |
| 5792 | BcResult *left, *right, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5793 | BcNum *l, *r; |
| 5794 | bool assign = (inst == XC_INST_ASSIGN); |
| 5795 | bool ib, sc; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5796 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5797 | s = zxc_program_binOpPrep(&left, &l, &right, &r, assign); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5798 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5799 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5800 | ib = left->t == XC_RESULT_IBASE; |
| 5801 | sc = left->t == XC_RESULT_SCALE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5802 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5803 | #if ENABLE_DC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5804 | if (right->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5805 | BcVec *v; |
| 5806 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5807 | if (left->t != XC_RESULT_VAR) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5808 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5809 | v = xc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5810 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5811 | RETURN_STATUS(zdc_program_assignStr(right, v, false)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5812 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5813 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5814 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5815 | if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 5816 | RETURN_STATUS(bc_error_bad_assignment()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5817 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5818 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5819 | if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5820 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5821 | |
| 5822 | if (assign) |
| 5823 | bc_num_copy(l, r); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5824 | else { |
| 5825 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5826 | 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] | 5827 | } |
| 5828 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5829 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5830 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5831 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5832 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5833 | if (ib || sc || left->t == XC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5834 | static const char *const msg[] = { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5835 | "bad ibase; must be [2,16]", //XC_RESULT_IBASE |
| 5836 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE |
| 5837 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5838 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5839 | size_t *ptr; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5840 | size_t max; |
| 5841 | unsigned long val; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5842 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5843 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5844 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5845 | s = left->t - XC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5846 | if (sc) { |
| 5847 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5848 | ptr = &G.prog.scale; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5849 | } else { |
| 5850 | if (val < 2) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5851 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5852 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5853 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5854 | } |
| 5855 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5856 | if (val > max) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5857 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5858 | |
| 5859 | *ptr = (size_t) val; |
| 5860 | s = BC_STATUS_SUCCESS; |
| 5861 | } |
| 5862 | |
| 5863 | bc_num_init(&res.d.n, l->len); |
| 5864 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5865 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5866 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5867 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5868 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5869 | #define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5870 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5871 | #if !ENABLE_DC |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5872 | #define xc_program_pushVar(code, bgn, pop, copy) \ |
| 5873 | xc_program_pushVar(code, bgn) |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5874 | // for bc, 'pop' and 'copy' are always false |
| 5875 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5876 | static BC_STATUS xc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5877 | bool pop, bool copy) |
| 5878 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5879 | BcResult r; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5880 | char *name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5881 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5882 | r.t = XC_RESULT_VAR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5883 | r.d.id.name = name; |
| 5884 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5885 | #if ENABLE_DC |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5886 | if (pop || copy) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5887 | BcVec *v = xc_program_search(name, true); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5888 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5889 | |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5890 | free(name); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5891 | if (!STACK_HAS_MORE_THAN(v, 1 - copy)) { |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5892 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5893 | } |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5894 | |
| 5895 | if (!BC_PROG_STR(num)) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5896 | r.t = XC_RESULT_TEMP; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5897 | bc_num_init_DEF_SIZE(&r.d.n); |
| 5898 | bc_num_copy(&r.d.n, num); |
| 5899 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5900 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5901 | r.d.id.idx = num->rdx; |
| 5902 | } |
| 5903 | |
| 5904 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5905 | } |
| 5906 | #endif // ENABLE_DC |
| 5907 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5908 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5909 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 5910 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5911 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5912 | #define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5913 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5914 | 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] | 5915 | { |
| 5916 | BcStatus s = BC_STATUS_SUCCESS; |
| 5917 | BcResult r; |
| 5918 | BcNum *num; |
| 5919 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5920 | r.d.id.name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5921 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5922 | if (inst == XC_INST_ARRAY) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5923 | r.t = XC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5924 | bc_vec_push(&G.prog.results, &r); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5925 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5926 | BcResult *operand; |
| 5927 | unsigned long temp; |
| 5928 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5929 | s = zxc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5930 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5931 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5932 | if (s) goto err; |
| 5933 | |
| 5934 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 5935 | 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] | 5936 | goto err; |
| 5937 | } |
| 5938 | |
| 5939 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5940 | xc_program_retire(&r, XC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5941 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5942 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5943 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5944 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5945 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5946 | #define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5947 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5948 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5949 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5950 | { |
| 5951 | BcStatus s; |
| 5952 | BcResult *ptr, res, copy; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5953 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5954 | char inst2 = inst; |
| 5955 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5956 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5957 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5958 | |
| 5959 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5960 | copy.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5961 | bc_num_init(©.d.n, num->len); |
| 5962 | bc_num_copy(©.d.n, num); |
| 5963 | } |
| 5964 | |
| 5965 | res.t = BC_RESULT_ONE; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5966 | inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST) |
| 5967 | ? BC_INST_ASSIGN_PLUS |
| 5968 | : BC_INST_ASSIGN_MINUS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5969 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5970 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5971 | s = zxc_program_assign(inst); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5972 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5973 | |
| 5974 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5975 | bc_result_pop_and_push(©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5976 | } |
| 5977 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5978 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5979 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5980 | #define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5981 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5982 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5983 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5984 | BcInstPtr ip; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5985 | size_t i, nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5986 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5987 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5988 | BcResult *arg; |
| 5989 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5990 | nparams = xc_program_index(code, idx); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5991 | ip.inst_idx = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5992 | ip.func = xc_program_index(code, idx); |
| 5993 | func = xc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5994 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 5995 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5996 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 5997 | } |
| 5998 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5999 | 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] | 6000 | } |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6001 | ip.results_len_before_call = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6002 | |
| 6003 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6004 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6005 | |
| 6006 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6007 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6008 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6009 | if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6010 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6011 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6012 | s = zxc_program_copyToVar(a->name, a->idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6013 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6014 | } |
| 6015 | |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6016 | a = bc_vec_item(&func->autos, i); |
| 6017 | for (; i < func->autos.len; i++, a++) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6018 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6019 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6020 | v = xc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6021 | if (a->idx) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 6022 | BcNum n2; |
| 6023 | bc_num_init_DEF_SIZE(&n2); |
| 6024 | bc_vec_push(v, &n2); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6025 | } else { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 6026 | BcVec v2; |
| 6027 | bc_array_init(&v2, true); |
| 6028 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6029 | } |
| 6030 | } |
| 6031 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6032 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6033 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6034 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6035 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6036 | #define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6037 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6038 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6039 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6040 | BcResult res; |
| 6041 | BcFunc *f; |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6042 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6043 | size_t i; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6044 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6045 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6046 | if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->results_len_before_call + (inst == XC_INST_RET))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6047 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6048 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6049 | f = xc_program_func(ip->func); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6050 | res.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6051 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6052 | if (inst == XC_INST_RET) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6053 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6054 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6055 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6056 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6057 | s = zxc_program_num(operand, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6058 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6059 | bc_num_init(&res.d.n, num->len); |
| 6060 | bc_num_copy(&res.d.n, num); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6061 | } else { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6062 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6063 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6064 | } |
| 6065 | |
| 6066 | // We need to pop arguments as well, so this takes that into account. |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6067 | a = (void*)f->autos.v; |
| 6068 | for (i = 0; i < f->autos.len; i++, a++) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6069 | BcVec *v; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6070 | v = xc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6071 | bc_vec_pop(v); |
| 6072 | } |
| 6073 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6074 | bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6075 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6076 | bc_vec_pop(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6077 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6078 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6079 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6080 | #define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6081 | #endif // ENABLE_BC |
| 6082 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6083 | static unsigned long xc_program_scale(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6084 | { |
| 6085 | return (unsigned long) n->rdx; |
| 6086 | } |
| 6087 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6088 | static unsigned long xc_program_len(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6089 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6090 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6091 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6092 | if (n->rdx != len) return len; |
| 6093 | for (;;) { |
| 6094 | if (len == 0) break; |
| 6095 | len--; |
| 6096 | if (n->num[len] != 0) break; |
| 6097 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6098 | return len; |
| 6099 | } |
| 6100 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6101 | static BC_STATUS zxc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6102 | { |
| 6103 | BcStatus s; |
| 6104 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6105 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6106 | BcResult res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6107 | bool len = (inst == XC_INST_LENGTH); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6108 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6109 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6110 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6111 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6112 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6113 | s = zxc_program_num(opnd, &num); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6114 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6115 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6116 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6117 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6118 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6119 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6120 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6121 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6122 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6123 | if (inst == XC_INST_SQRT) |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6124 | s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6125 | #if ENABLE_BC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6126 | else if (len != 0 && opnd->t == XC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6127 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6128 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6129 | #endif |
| 6130 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6131 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6132 | char **str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6133 | 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] | 6134 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6135 | str = xc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6136 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6137 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6138 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6139 | else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6140 | 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] | 6141 | } |
| 6142 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6143 | xc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6144 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6145 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6146 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6147 | #define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6148 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6149 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6150 | static BC_STATUS zdc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6151 | { |
| 6152 | BcStatus s; |
| 6153 | BcResult *opd1, *opd2, res, res2; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6154 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6155 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6156 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6157 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6158 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6159 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6160 | bc_num_init(&res2.d.n, n2->len); |
| 6161 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6162 | 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] | 6163 | if (s) goto err; |
| 6164 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6165 | xc_program_binOpRetire(&res2); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6166 | res.t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6167 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6168 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6169 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6170 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6171 | bc_num_free(&res2.d.n); |
| 6172 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6173 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6174 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6175 | #define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6176 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6177 | static BC_STATUS zdc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6178 | { |
| 6179 | BcStatus s; |
| 6180 | BcResult *r1, *r2, *r3, res; |
| 6181 | BcNum *n1, *n2, *n3; |
| 6182 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6183 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 2)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6184 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6185 | s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6186 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6187 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6188 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6189 | s = zxc_program_num(r1, &n1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6190 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6191 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6192 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6193 | |
| 6194 | // Make sure that the values have their pointers updated, if necessary. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6195 | if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6196 | if (r1->t == r2->t) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6197 | s = zxc_program_num(r2, &n2); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6198 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6199 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6200 | if (r1->t == r3->t) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6201 | s = zxc_program_num(r3, &n3); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6202 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6203 | } |
| 6204 | } |
| 6205 | |
| 6206 | bc_num_init(&res.d.n, n3->len); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6207 | s = zdc_num_modexp(n1, n2, n3, &res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6208 | if (s) goto err; |
| 6209 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6210 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6211 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6212 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6213 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6214 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6215 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6216 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6217 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6218 | #define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6219 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6220 | static void dc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6221 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6222 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6223 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6224 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6225 | res.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6226 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6227 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6228 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6229 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6230 | } |
| 6231 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6232 | static BC_STATUS zdc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6233 | { |
| 6234 | BcStatus s; |
| 6235 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6236 | BcNum *num, n; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6237 | char **strs; |
| 6238 | char *str; |
| 6239 | char c; |
| 6240 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6241 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6242 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6243 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6244 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 6245 | r = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6246 | s = zxc_program_num(r, &num); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6247 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6248 | |
| 6249 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 6250 | unsigned long val; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6251 | BcNum strmb; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6252 | BcDig strmb_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6253 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6254 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6255 | bc_num_copy(&n, num); |
| 6256 | bc_num_truncate(&n, n.rdx); |
| 6257 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6258 | strmb.cap = ARRAY_SIZE(strmb_digs); |
| 6259 | strmb.num = strmb_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6260 | bc_num_ulong2num(&strmb, 0x100); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6261 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 6262 | s = zbc_num_mod(&n, &strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6263 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6264 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6265 | if (s) goto num_err; |
| 6266 | |
| 6267 | c = (char) val; |
| 6268 | |
| 6269 | bc_num_free(&n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6270 | } else { |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6271 | char *sp; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6272 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6273 | sp = *xc_program_str(idx); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6274 | c = sp[0]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6275 | } |
| 6276 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6277 | strs = (void*)G.prog.strs.v; |
| 6278 | for (idx = 0; idx < G.prog.strs.len; idx++) { |
| 6279 | if (strs[idx][0] == c && strs[idx][1] == '\0') { |
| 6280 | goto dup; |
| 6281 | } |
| 6282 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6283 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6284 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6285 | //str[1] = '\0'; - already is |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6286 | bc_vec_push(&G.prog.strs, &str); |
| 6287 | dup: |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6288 | res.t = XC_RESULT_STR; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6289 | res.d.id.idx = idx; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 6290 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6291 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6292 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6293 | num_err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6294 | bc_num_free(&n); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6295 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6296 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6297 | #define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6298 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6299 | static BC_STATUS zdc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6300 | { |
| 6301 | BcStatus s; |
| 6302 | BcResult *r; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6303 | BcNum *n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6304 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6305 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6306 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6307 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6308 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6309 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6310 | s = zxc_program_num(r, &n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6311 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6312 | |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6313 | if (BC_PROG_NUM(r, n)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6314 | s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar); |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6315 | } else { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6316 | char *str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6317 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6318 | str = *xc_program_str(idx); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6319 | fputs(str, stdout); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6320 | } |
| 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_printStream(...) (zdc_program_printStream(__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_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6327 | { |
| 6328 | BcStatus s; |
| 6329 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6330 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6331 | unsigned long val; |
| 6332 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6333 | s = zxc_program_prep(&opnd, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6334 | if (s) RETURN_STATUS(s); |
| 6335 | s = zbc_num_ulong(num, &val); |
| 6336 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6337 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6338 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6339 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6340 | if (G.prog.exestack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6341 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6342 | if (G.prog.exestack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6343 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6344 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6345 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6346 | bc_vec_npop(&G.prog.exestack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6347 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6348 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6349 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6350 | #define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6351 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6352 | 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] | 6353 | { |
| 6354 | BcStatus s = BC_STATUS_SUCCESS; |
| 6355 | BcResult *r; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6356 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6357 | BcInstPtr ip; |
| 6358 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6359 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6360 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6361 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6362 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6363 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6364 | |
| 6365 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6366 | BcNum *n = n; // for compiler |
| 6367 | bool exec; |
| 6368 | char *name; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6369 | char *then_name = xc_program_name(code, bgn); |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6370 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6371 | |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 6372 | if (code[*bgn] == '\0') |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6373 | (*bgn) += 1; |
| 6374 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6375 | else_name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6376 | |
| 6377 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6378 | name = then_name; |
| 6379 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6380 | exec = true; |
| 6381 | name = else_name; |
| 6382 | } |
| 6383 | |
| 6384 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6385 | BcVec *v; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6386 | v = xc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6387 | n = bc_vec_top(v); |
| 6388 | } |
| 6389 | |
| 6390 | free(then_name); |
| 6391 | free(else_name); |
| 6392 | |
| 6393 | if (!exec) goto exit; |
| 6394 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6395 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6396 | goto exit; |
| 6397 | } |
| 6398 | |
| 6399 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6400 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6401 | if (r->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6402 | sidx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6403 | } else if (r->t == XC_RESULT_VAR) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6404 | BcNum *n; |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6405 | s = zxc_program_num(r, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6406 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6407 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6408 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6409 | goto exit; |
| 6410 | } |
| 6411 | |
| 6412 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6413 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6414 | f = xc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6415 | |
| 6416 | if (f->code.len == 0) { |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6417 | BcParse sv_parse; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6418 | char *str; |
| 6419 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6420 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6421 | xc_parse_create(fidx); |
| 6422 | str = *xc_program_str(sidx); |
| 6423 | s = zxc_parse_text_init(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6424 | if (s) goto err; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6425 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6426 | s = zdc_parse_exprs_until_eof(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6427 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6428 | xc_parse_push(DC_INST_POP_EXEC); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6429 | if (G.prs.lex != XC_LEX_EOF) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6430 | s = bc_error_bad_expression(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6431 | xc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6432 | G.prs = sv_parse; // struct copy |
| 6433 | if (s) { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6434 | err: |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6435 | bc_vec_pop_all(&f->code); |
| 6436 | goto exit; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6437 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6438 | } |
| 6439 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6440 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6441 | ip.func = fidx; |
| 6442 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6443 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6444 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6445 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6446 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6447 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6448 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6449 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6450 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6451 | #define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6452 | #endif // ENABLE_DC |
| 6453 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6454 | static void xc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6455 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6456 | BcResult res; |
| 6457 | unsigned long val; |
| 6458 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6459 | res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6460 | if (inst == XC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6461 | val = (unsigned long) G.prog.ib_t; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6462 | else if (inst == XC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6463 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6464 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6465 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6466 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6467 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6468 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6469 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6470 | } |
| 6471 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6472 | static BC_STATUS zxc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6473 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6474 | BcResult r, *ptr; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6475 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6476 | BcFunc *func = xc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6477 | char *code = func->code.v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6478 | |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6479 | dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d", |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6480 | ip->func, func->code.len, ip->inst_idx, G.prog.results.len); |
| 6481 | while (ip->inst_idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6482 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6483 | char inst = code[ip->inst_idx++]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6484 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6485 | 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] | 6486 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6487 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6488 | case BC_INST_JUMP_ZERO: { |
| 6489 | BcNum *num; |
| 6490 | bool zero; |
| 6491 | dbg_exec("BC_INST_JUMP_ZERO:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6492 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6493 | if (s) RETURN_STATUS(s); |
| 6494 | zero = (bc_num_cmp(num, &G.prog.zero) == 0); |
| 6495 | bc_vec_pop(&G.prog.results); |
| 6496 | if (!zero) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6497 | xc_program_index(code, &ip->inst_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6498 | break; |
| 6499 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6500 | // else: fall through |
| 6501 | } |
| 6502 | case BC_INST_JUMP: { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6503 | size_t idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6504 | size_t *addr = bc_vec_item(&func->labels, idx); |
| 6505 | dbg_exec("BC_INST_JUMP: to %ld", (long)*addr); |
| 6506 | ip->inst_idx = *addr; |
| 6507 | break; |
| 6508 | } |
| 6509 | case BC_INST_CALL: |
| 6510 | dbg_exec("BC_INST_CALL:"); |
| 6511 | s = zbc_program_call(code, &ip->inst_idx); |
| 6512 | goto read_updated_ip; |
| 6513 | case BC_INST_INC_PRE: |
| 6514 | case BC_INST_DEC_PRE: |
| 6515 | case BC_INST_INC_POST: |
| 6516 | case BC_INST_DEC_POST: |
| 6517 | dbg_exec("BC_INST_INCDEC:"); |
| 6518 | s = zbc_program_incdec(inst); |
| 6519 | break; |
| 6520 | case BC_INST_HALT: |
| 6521 | dbg_exec("BC_INST_HALT:"); |
| 6522 | QUIT_OR_RETURN_TO_MAIN; |
| 6523 | break; |
| 6524 | case XC_INST_RET: |
| 6525 | case BC_INST_RET0: |
| 6526 | dbg_exec("BC_INST_RET[0]:"); |
| 6527 | s = zbc_program_return(inst); |
| 6528 | goto read_updated_ip; |
| 6529 | case XC_INST_BOOL_OR: |
| 6530 | case XC_INST_BOOL_AND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6531 | #endif // ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6532 | case XC_INST_REL_EQ: |
| 6533 | case XC_INST_REL_LE: |
| 6534 | case XC_INST_REL_GE: |
| 6535 | case XC_INST_REL_NE: |
| 6536 | case XC_INST_REL_LT: |
| 6537 | case XC_INST_REL_GT: |
| 6538 | dbg_exec("BC_INST_BOOL:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6539 | s = zxc_program_logical(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6540 | break; |
| 6541 | case XC_INST_READ: |
| 6542 | dbg_exec("XC_INST_READ:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6543 | s = zxc_program_read(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6544 | goto read_updated_ip; |
| 6545 | case XC_INST_VAR: |
| 6546 | dbg_exec("XC_INST_VAR:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6547 | s = zxc_program_pushVar(code, &ip->inst_idx, false, false); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6548 | break; |
| 6549 | case XC_INST_ARRAY_ELEM: |
| 6550 | case XC_INST_ARRAY: |
| 6551 | dbg_exec("XC_INST_ARRAY[_ELEM]:"); |
| 6552 | s = zbc_program_pushArray(code, &ip->inst_idx, inst); |
| 6553 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6554 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6555 | case BC_INST_LAST: |
| 6556 | dbg_exec("BC_INST_LAST:"); |
| 6557 | r.t = BC_RESULT_LAST; |
| 6558 | bc_vec_push(&G.prog.results, &r); |
| 6559 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6560 | #endif |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6561 | case XC_INST_IBASE: |
| 6562 | case XC_INST_OBASE: |
| 6563 | case XC_INST_SCALE: |
| 6564 | dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6565 | xc_program_pushGlobal(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6566 | break; |
| 6567 | case XC_INST_SCALE_FUNC: |
| 6568 | case XC_INST_LENGTH: |
| 6569 | case XC_INST_SQRT: |
| 6570 | dbg_exec("BC_INST_builtin:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6571 | s = zxc_program_builtin(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6572 | break; |
| 6573 | case XC_INST_NUM: |
| 6574 | dbg_exec("XC_INST_NUM:"); |
| 6575 | r.t = XC_RESULT_CONSTANT; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6576 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6577 | bc_vec_push(&G.prog.results, &r); |
| 6578 | break; |
| 6579 | case XC_INST_POP: |
| 6580 | dbg_exec("XC_INST_POP:"); |
| 6581 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
| 6582 | s = bc_error_stack_has_too_few_elements(); |
| 6583 | else |
| 6584 | bc_vec_pop(&G.prog.results); |
| 6585 | break; |
| 6586 | case XC_INST_PRINT: |
| 6587 | case XC_INST_PRINT_POP: |
| 6588 | case XC_INST_PRINT_STR: |
| 6589 | dbg_exec("XC_INST_PRINTxyz:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6590 | s = zxc_program_print(inst, 0); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6591 | break; |
| 6592 | case XC_INST_STR: |
| 6593 | dbg_exec("XC_INST_STR:"); |
| 6594 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6595 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6596 | bc_vec_push(&G.prog.results, &r); |
| 6597 | break; |
| 6598 | case XC_INST_POWER: |
| 6599 | case XC_INST_MULTIPLY: |
| 6600 | case XC_INST_DIVIDE: |
| 6601 | case XC_INST_MODULUS: |
| 6602 | case XC_INST_PLUS: |
| 6603 | case XC_INST_MINUS: |
| 6604 | dbg_exec("BC_INST_binaryop:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6605 | s = zxc_program_op(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6606 | break; |
| 6607 | case XC_INST_BOOL_NOT: { |
| 6608 | BcNum *num; |
| 6609 | dbg_exec("XC_INST_BOOL_NOT:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6610 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6611 | if (s) RETURN_STATUS(s); |
| 6612 | bc_num_init_DEF_SIZE(&r.d.n); |
| 6613 | if (bc_num_cmp(num, &G.prog.zero) == 0) |
| 6614 | bc_num_one(&r.d.n); |
| 6615 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6616 | xc_program_retire(&r, XC_RESULT_TEMP); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6617 | break; |
| 6618 | } |
| 6619 | case XC_INST_NEG: |
| 6620 | dbg_exec("XC_INST_NEG:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6621 | s = zxc_program_negate(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6622 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6623 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6624 | case BC_INST_ASSIGN_POWER: |
| 6625 | case BC_INST_ASSIGN_MULTIPLY: |
| 6626 | case BC_INST_ASSIGN_DIVIDE: |
| 6627 | case BC_INST_ASSIGN_MODULUS: |
| 6628 | case BC_INST_ASSIGN_PLUS: |
| 6629 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6630 | #endif |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6631 | case XC_INST_ASSIGN: |
| 6632 | dbg_exec("BC_INST_ASSIGNxyz:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6633 | s = zxc_program_assign(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6634 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6635 | #if ENABLE_DC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6636 | case DC_INST_POP_EXEC: |
| 6637 | dbg_exec("DC_INST_POP_EXEC:"); |
| 6638 | bc_vec_pop(&G.prog.exestack); |
| 6639 | goto read_updated_ip; |
| 6640 | case DC_INST_MODEXP: |
| 6641 | dbg_exec("DC_INST_MODEXP:"); |
| 6642 | s = zdc_program_modexp(); |
| 6643 | break; |
| 6644 | case DC_INST_DIVMOD: |
| 6645 | dbg_exec("DC_INST_DIVMOD:"); |
| 6646 | s = zdc_program_divmod(); |
| 6647 | break; |
| 6648 | case DC_INST_EXECUTE: |
| 6649 | case DC_INST_EXEC_COND: |
| 6650 | dbg_exec("DC_INST_EXEC[_COND]:"); |
| 6651 | s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND); |
| 6652 | goto read_updated_ip; |
| 6653 | case DC_INST_PRINT_STACK: { |
| 6654 | size_t idx; |
| 6655 | dbg_exec("DC_INST_PRINT_STACK:"); |
| 6656 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6657 | s = zxc_program_print(XC_INST_PRINT, idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6658 | if (s) break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6659 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6660 | break; |
| 6661 | } |
| 6662 | case DC_INST_CLEAR_STACK: |
| 6663 | dbg_exec("DC_INST_CLEAR_STACK:"); |
| 6664 | bc_vec_pop_all(&G.prog.results); |
| 6665 | break; |
| 6666 | case DC_INST_STACK_LEN: |
| 6667 | dbg_exec("DC_INST_STACK_LEN:"); |
| 6668 | dc_program_stackLen(); |
| 6669 | break; |
| 6670 | case DC_INST_DUPLICATE: |
| 6671 | dbg_exec("DC_INST_DUPLICATE:"); |
| 6672 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
| 6673 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| 6674 | ptr = bc_vec_top(&G.prog.results); |
| 6675 | dc_result_copy(&r, ptr); |
| 6676 | bc_vec_push(&G.prog.results, &r); |
| 6677 | break; |
| 6678 | case DC_INST_SWAP: { |
| 6679 | BcResult *ptr2; |
| 6680 | dbg_exec("DC_INST_SWAP:"); |
| 6681 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
| 6682 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| 6683 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6684 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
| 6685 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6686 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6687 | memcpy(ptr2, &r, sizeof(BcResult)); |
| 6688 | break; |
| 6689 | } |
| 6690 | case DC_INST_ASCIIFY: |
| 6691 | dbg_exec("DC_INST_ASCIIFY:"); |
| 6692 | s = zdc_program_asciify(); |
| 6693 | break; |
| 6694 | case DC_INST_PRINT_STREAM: |
| 6695 | dbg_exec("DC_INST_PRINT_STREAM:"); |
| 6696 | s = zdc_program_printStream(); |
| 6697 | break; |
| 6698 | case DC_INST_LOAD: |
| 6699 | case DC_INST_PUSH_VAR: { |
| 6700 | bool copy = inst == DC_INST_LOAD; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6701 | s = zxc_program_pushVar(code, &ip->inst_idx, true, copy); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6702 | break; |
| 6703 | } |
| 6704 | case DC_INST_PUSH_TO_VAR: { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6705 | char *name = xc_program_name(code, &ip->inst_idx); |
| 6706 | s = zxc_program_copyToVar(name, true); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6707 | free(name); |
| 6708 | break; |
| 6709 | } |
| 6710 | case DC_INST_QUIT: |
| 6711 | dbg_exec("DC_INST_QUIT:"); |
| 6712 | if (G.prog.exestack.len <= 2) |
| 6713 | QUIT_OR_RETURN_TO_MAIN; |
| 6714 | bc_vec_npop(&G.prog.exestack, 2); |
| 6715 | goto read_updated_ip; |
| 6716 | case DC_INST_NQUIT: |
| 6717 | dbg_exec("DC_INST_NQUIT:"); |
| 6718 | s = zdc_program_nquit(); |
| 6719 | //goto read_updated_ip; - just fall through to it |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6720 | #endif // ENABLE_DC |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6721 | read_updated_ip: |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6722 | // Instruction stack has changed, read new pointers |
| 6723 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6724 | func = xc_program_func(ip->func); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6725 | code = func->code.v; |
| 6726 | 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] | 6727 | } |
| 6728 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6729 | if (s || G_interrupt) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6730 | xc_program_reset(); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6731 | RETURN_STATUS(s); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6732 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6733 | |
Denys Vlasenko | c5774a3 | 2018-12-17 01:22:53 +0100 | [diff] [blame] | 6734 | fflush_and_check(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6735 | } |
| 6736 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6737 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6738 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6739 | #define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6740 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6741 | static unsigned xc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6742 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6743 | char *lenv; |
| 6744 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6745 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6746 | lenv = getenv(var); |
| 6747 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6748 | if (!lenv) return len; |
| 6749 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6750 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 6751 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6752 | len = BC_NUM_PRINT_WIDTH; |
| 6753 | |
| 6754 | return len; |
| 6755 | } |
| 6756 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6757 | static BC_STATUS zxc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6758 | { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6759 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6760 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6761 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6762 | s = zxc_parse_text_init(text); // does the first zxc_lex_next() |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6763 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6764 | |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6765 | IF_BC(check_eof:) |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6766 | while (G.prs.lex != XC_LEX_EOF) { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6767 | BcInstPtr *ip; |
| 6768 | BcFunc *f; |
| 6769 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6770 | 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] | 6771 | if (IS_BC) { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6772 | #if ENABLE_BC |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6773 | if (G.prs.lex == BC_LEX_SCOLON |
| 6774 | || G.prs.lex == XC_LEX_NLINE |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6775 | ) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6776 | s = zxc_lex_next(); |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6777 | if (s) goto err; |
| 6778 | goto check_eof; |
| 6779 | } |
| 6780 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6781 | s = zbc_parse_stmt_or_funcdef(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6782 | if (s) goto err; |
| 6783 | |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6784 | // Check that next token is a correct stmt delimiter - |
| 6785 | // disallows "print 1 print 2" and such. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6786 | if (G.prs.lex != BC_LEX_SCOLON |
| 6787 | && G.prs.lex != XC_LEX_NLINE |
| 6788 | && G.prs.lex != XC_LEX_EOF |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6789 | ) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 6790 | bc_error_at("bad statement terminator"); |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6791 | goto err; |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6792 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6793 | // The above logic is fragile. Check these examples: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6794 | // - interactive read() still works |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6795 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6796 | } else { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6797 | #if ENABLE_DC |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6798 | // Most of dc parsing assumes all whitespace, |
| 6799 | // including '\n', is eaten. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6800 | while (G.prs.lex == XC_LEX_NLINE) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6801 | s = zxc_lex_next(); |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6802 | if (s) goto err; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6803 | if (G.prs.lex == XC_LEX_EOF) |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6804 | goto done; |
| 6805 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6806 | s = zdc_parse_expr(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6807 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6808 | } |
| 6809 | if (s || G_interrupt) { |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6810 | err: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6811 | xc_parse_reset(); // includes xc_program_reset() |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6812 | RETURN_STATUS(BC_STATUS_FAILURE); |
| 6813 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6814 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 6815 | dbg_lex("%s:%d executing...", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6816 | s = zxc_program_exec(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6817 | if (s) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6818 | xc_program_reset(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6819 | break; |
| 6820 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6821 | |
| 6822 | ip = (void*)G.prog.exestack.v; |
| 6823 | #if SANITY_CHECKS |
| 6824 | if (G.prog.exestack.len != 1) // should have only main's IP |
| 6825 | bb_error_msg_and_die("BUG:call stack"); |
| 6826 | if (ip->func != BC_PROG_MAIN) |
| 6827 | bb_error_msg_and_die("BUG:not MAIN"); |
| 6828 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6829 | f = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6830 | // bc discards strings, constants and code after each |
| 6831 | // top-level statement in the "main program". |
| 6832 | // This prevents "yes 1 | bc" from growing its memory |
| 6833 | // without bound. This can be done because data stack |
| 6834 | // is empty and thus can't hold any references to |
| 6835 | // strings or constants, there is no generated code |
| 6836 | // which can hold references (after we discard one |
| 6837 | // we just executed). Code of functions can have references, |
| 6838 | // but bc stores function strings/constants in per-function |
| 6839 | // storage. |
| 6840 | if (IS_BC) { |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6841 | #if SANITY_CHECKS |
Denys Vlasenko | 7c1c9dc | 2018-12-22 14:18:47 +0100 | [diff] [blame] | 6842 | if (G.prog.results.len != 0) // should be empty |
| 6843 | bb_error_msg_and_die("BUG:data stack"); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6844 | #endif |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6845 | IF_BC(bc_vec_pop_all(&f->strs);) |
| 6846 | IF_BC(bc_vec_pop_all(&f->consts);) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6847 | } else { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6848 | if (G.prog.results.len == 0 |
| 6849 | && G.prog.vars.len == 0 |
| 6850 | ) { |
| 6851 | // If stack is empty and no registers exist (TODO: or they are all empty), |
| 6852 | // we can get rid of accumulated strings and constants. |
| 6853 | // In this example dc process should not grow |
| 6854 | // its memory consumption with time: |
| 6855 | // yes 1pc | dc |
| 6856 | IF_DC(bc_vec_pop_all(&G.prog.strs);) |
| 6857 | IF_DC(bc_vec_pop_all(&G.prog.consts);) |
| 6858 | } |
| 6859 | // The code is discarded always (below), thus this example |
| 6860 | // should also not grow its memory consumption with time, |
| 6861 | // even though its data stack is not empty: |
| 6862 | // { echo 1; yes dk; } | dc |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6863 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6864 | // We drop generated and executed code for both bc and dc: |
| 6865 | bc_vec_pop_all(&f->code); |
| 6866 | ip->inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6867 | } |
Denys Vlasenko | 8a56e36 | 2018-12-26 19:09:23 +0100 | [diff] [blame] | 6868 | IF_DC(done:) |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6869 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6870 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6871 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6872 | #define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6873 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6874 | static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6875 | { |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6876 | // 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] | 6877 | // therefore we know G.prs.lex_filename == NULL on entry |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6878 | //const char *sv_file; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6879 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6880 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6881 | G.prs.lex_filename = filename; |
| 6882 | G.prs.lex_input_fp = fp; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 6883 | G.err_line = G.prs.lex_line = 1; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame^] | 6884 | dbg_lex("p->lex_line reset to 1"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6885 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6886 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6887 | s = zxc_vm_process(""); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6888 | // We do not stop looping on errors here if reading stdin. |
| 6889 | // Example: start interactive bc and enter "return". |
| 6890 | // It should say "'return' not in a function" |
| 6891 | // but should not exit. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6892 | } while (G.prs.lex_input_fp == stdin); |
| 6893 | G.prs.lex_filename = NULL; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6894 | RETURN_STATUS(s); |
| 6895 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6896 | #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] | 6897 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6898 | static BC_STATUS zxc_vm_file(const char *file) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6899 | { |
| 6900 | BcStatus s; |
| 6901 | FILE *fp; |
| 6902 | |
| 6903 | fp = xfopen_for_read(file); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6904 | s = zxc_vm_execute_FILE(fp, file); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6905 | fclose(fp); |
| 6906 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6907 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6908 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6909 | #define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6910 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6911 | #if ENABLE_BC |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 6912 | static void bc_vm_info(void) |
| 6913 | { |
| 6914 | printf("%s "BB_VER"\n" |
Gavin Howard | fa495ce | 2018-12-18 10:03:14 -0700 | [diff] [blame] | 6915 | "Adapted from https://github.com/gavinhoward/bc\n" |
| 6916 | "Original code (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 6917 | , applet_name); |
| 6918 | } |
| 6919 | |
| 6920 | static void bc_args(char **argv) |
| 6921 | { |
| 6922 | unsigned opts; |
| 6923 | int i; |
| 6924 | |
| 6925 | GETOPT_RESET(); |
| 6926 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
| 6927 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
| 6928 | "warn\0" No_argument "w" |
| 6929 | "version\0" No_argument "v" |
| 6930 | "standard\0" No_argument "s" |
| 6931 | "quiet\0" No_argument "q" |
| 6932 | "mathlib\0" No_argument "l" |
| 6933 | "interactive\0" No_argument "i" |
| 6934 | ); |
| 6935 | #else |
| 6936 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
| 6937 | #endif |
| 6938 | if (getenv("POSIXLY_CORRECT")) |
| 6939 | option_mask32 |= BC_FLAG_S; |
| 6940 | |
| 6941 | if (opts & BC_FLAG_V) { |
| 6942 | bc_vm_info(); |
| 6943 | exit(0); |
| 6944 | } |
| 6945 | |
| 6946 | for (i = optind; argv[i]; ++i) |
| 6947 | bc_vec_push(&G.files, argv + i); |
| 6948 | } |
| 6949 | |
| 6950 | static void bc_vm_envArgs(void) |
| 6951 | { |
| 6952 | BcVec v; |
| 6953 | char *buf; |
| 6954 | char *env_args = getenv("BC_ENV_ARGS"); |
| 6955 | |
| 6956 | if (!env_args) return; |
| 6957 | |
| 6958 | G.env_args = xstrdup(env_args); |
| 6959 | buf = G.env_args; |
| 6960 | |
| 6961 | bc_vec_init(&v, sizeof(char *), NULL); |
| 6962 | |
| 6963 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 6964 | bc_vec_push(&v, &buf); |
| 6965 | buf = skip_non_whitespace(buf); |
| 6966 | if (!*buf) |
| 6967 | break; |
| 6968 | *buf++ = '\0'; |
| 6969 | } |
| 6970 | |
| 6971 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 6972 | if (sizeof(int) == sizeof(char*)) { |
| 6973 | bc_vec_push(&v, &const_int_0); |
| 6974 | } else { |
| 6975 | static char *const nullptr = NULL; |
| 6976 | bc_vec_push(&v, &nullptr); |
| 6977 | } |
| 6978 | bc_args(((char **)v.v) - 1); |
| 6979 | |
| 6980 | bc_vec_free(&v); |
| 6981 | } |
| 6982 | |
| 6983 | static const char bc_lib[] ALIGN1 = { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6984 | "scale=20" |
| 6985 | "\n" "define e(x){" |
| 6986 | "\n" "auto b,s,n,r,d,i,p,f,v" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 6987 | ////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below |
| 6988 | //^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc: |
| 6989 | //e(-.998896): GNU:.36828580434569428695 |
| 6990 | // above code:.36828580434569428696 |
| 6991 | // actual value:.3682858043456942869594... |
| 6992 | // but for now let's be "GNU compatible" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6993 | "\n" "b=ibase" |
| 6994 | "\n" "ibase=A" |
| 6995 | "\n" "if(x<0){" |
| 6996 | "\n" "n=1" |
| 6997 | "\n" "x=-x" |
| 6998 | "\n" "}" |
| 6999 | "\n" "s=scale" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 7000 | "\n" "r=6+s+.44*x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7001 | "\n" "scale=scale(x)+1" |
| 7002 | "\n" "while(x>1){" |
| 7003 | "\n" "d+=1" |
| 7004 | "\n" "x/=2" |
| 7005 | "\n" "scale+=1" |
| 7006 | "\n" "}" |
| 7007 | "\n" "scale=r" |
| 7008 | "\n" "r=x+1" |
| 7009 | "\n" "p=x" |
| 7010 | "\n" "f=v=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7011 | "\n" "for(i=2;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7012 | "\n" "p*=x" |
| 7013 | "\n" "f*=i" |
| 7014 | "\n" "v=p/f" |
| 7015 | "\n" "r+=v" |
| 7016 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7017 | "\n" "while(d--)r*=r" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7018 | "\n" "scale=s" |
| 7019 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7020 | "\n" "if(n)return(1/r)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7021 | "\n" "return(r/1)" |
| 7022 | "\n" "}" |
| 7023 | "\n" "define l(x){" |
| 7024 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7025 | "\n" "b=ibase" |
| 7026 | "\n" "ibase=A" |
| 7027 | "\n" "if(x<=0){" |
| 7028 | "\n" "r=(1-10^scale)/1" |
| 7029 | "\n" "ibase=b" |
| 7030 | "\n" "return(r)" |
| 7031 | "\n" "}" |
| 7032 | "\n" "s=scale" |
| 7033 | "\n" "scale+=6" |
| 7034 | "\n" "p=2" |
| 7035 | "\n" "while(x>=2){" |
| 7036 | "\n" "p*=2" |
| 7037 | "\n" "x=sqrt(x)" |
| 7038 | "\n" "}" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 7039 | "\n" "while(x<=.5){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7040 | "\n" "p*=2" |
| 7041 | "\n" "x=sqrt(x)" |
| 7042 | "\n" "}" |
| 7043 | "\n" "r=a=(x-1)/(x+1)" |
| 7044 | "\n" "q=a*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7045 | "\n" "v=1" |
| 7046 | "\n" "for(i=3;v;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7047 | "\n" "a*=q" |
| 7048 | "\n" "v=a/i" |
| 7049 | "\n" "r+=v" |
| 7050 | "\n" "}" |
| 7051 | "\n" "r*=p" |
| 7052 | "\n" "scale=s" |
| 7053 | "\n" "ibase=b" |
| 7054 | "\n" "return(r/1)" |
| 7055 | "\n" "}" |
| 7056 | "\n" "define s(x){" |
Denys Vlasenko | 240d7ee | 2018-12-14 11:27:09 +0100 | [diff] [blame] | 7057 | "\n" "auto b,s,r,a,q,i" |
| 7058 | "\n" "if(x<0)return(-s(-x))" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7059 | "\n" "b=ibase" |
| 7060 | "\n" "ibase=A" |
| 7061 | "\n" "s=scale" |
| 7062 | "\n" "scale=1.1*s+2" |
| 7063 | "\n" "a=a(1)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7064 | "\n" "scale=0" |
| 7065 | "\n" "q=(x/a+2)/4" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7066 | "\n" "x-=4*q*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7067 | "\n" "if(q%2)x=-x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7068 | "\n" "scale=s+2" |
| 7069 | "\n" "r=a=x" |
| 7070 | "\n" "q=-x*x" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7071 | "\n" "for(i=3;a;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7072 | "\n" "a*=q/(i*(i-1))" |
| 7073 | "\n" "r+=a" |
| 7074 | "\n" "}" |
| 7075 | "\n" "scale=s" |
| 7076 | "\n" "ibase=b" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7077 | "\n" "return(r/1)" |
| 7078 | "\n" "}" |
| 7079 | "\n" "define c(x){" |
| 7080 | "\n" "auto b,s" |
| 7081 | "\n" "b=ibase" |
| 7082 | "\n" "ibase=A" |
| 7083 | "\n" "s=scale" |
| 7084 | "\n" "scale*=1.2" |
| 7085 | "\n" "x=s(2*a(1)+x)" |
| 7086 | "\n" "scale=s" |
| 7087 | "\n" "ibase=b" |
| 7088 | "\n" "return(x/1)" |
| 7089 | "\n" "}" |
| 7090 | "\n" "define a(x){" |
| 7091 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7092 | "\n" "b=ibase" |
| 7093 | "\n" "ibase=A" |
| 7094 | "\n" "n=1" |
| 7095 | "\n" "if(x<0){" |
| 7096 | "\n" "n=-1" |
| 7097 | "\n" "x=-x" |
| 7098 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7099 | "\n" "if(scale<65){" |
| 7100 | "\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7101 | "\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7102 | "\n" "}" |
| 7103 | "\n" "s=scale" |
| 7104 | "\n" "if(x>.2){" |
| 7105 | "\n" "scale+=5" |
| 7106 | "\n" "a=a(.2)" |
| 7107 | "\n" "}" |
| 7108 | "\n" "scale=s+3" |
| 7109 | "\n" "while(x>.2){" |
| 7110 | "\n" "m+=1" |
| 7111 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7112 | "\n" "}" |
| 7113 | "\n" "r=u=x" |
| 7114 | "\n" "f=-x*x" |
| 7115 | "\n" "t=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7116 | "\n" "for(i=3;t;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7117 | "\n" "u*=f" |
| 7118 | "\n" "t=u/i" |
| 7119 | "\n" "r+=t" |
| 7120 | "\n" "}" |
| 7121 | "\n" "scale=s" |
| 7122 | "\n" "ibase=b" |
| 7123 | "\n" "return((m*a+r)/n)" |
| 7124 | "\n" "}" |
| 7125 | "\n" "define j(n,x){" |
| 7126 | "\n" "auto b,s,o,a,i,v,f" |
| 7127 | "\n" "b=ibase" |
| 7128 | "\n" "ibase=A" |
| 7129 | "\n" "s=scale" |
| 7130 | "\n" "scale=0" |
| 7131 | "\n" "n/=1" |
| 7132 | "\n" "if(n<0){" |
| 7133 | "\n" "n=-n" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7134 | "\n" "o=n%2" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7135 | "\n" "}" |
| 7136 | "\n" "a=1" |
| 7137 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7138 | "\n" "scale=1.5*s" |
| 7139 | "\n" "a=(x^n)/2^n/a" |
| 7140 | "\n" "r=v=1" |
| 7141 | "\n" "f=-x*x/4" |
Denys Vlasenko | c06537d | 2018-12-14 10:10:37 +0100 | [diff] [blame] | 7142 | "\n" "scale+=length(a)-scale(a)" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7143 | "\n" "for(i=1;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7144 | "\n" "v=v*f/i/(n+i)" |
| 7145 | "\n" "r+=v" |
| 7146 | "\n" "}" |
| 7147 | "\n" "scale=s" |
| 7148 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7149 | "\n" "if(o)a=-a" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7150 | "\n" "return(a*r/1)" |
| 7151 | "\n" "}" |
| 7152 | }; |
| 7153 | #endif // ENABLE_BC |
| 7154 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7155 | static BC_STATUS zxc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7156 | { |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7157 | char **fname; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7158 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7159 | size_t i; |
| 7160 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7161 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7162 | if (option_mask32 & BC_FLAG_L) { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7163 | // We know that internal library is not buggy, |
| 7164 | // thus error checking is normally disabled. |
| 7165 | # define DEBUG_LIB 0 |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7166 | s = zxc_vm_process(bc_lib); |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7167 | if (DEBUG_LIB && s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7168 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7169 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7170 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7171 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7172 | fname = (void*)G.files.v; |
| 7173 | for (i = 0; i < G.files.len; i++) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7174 | s = zxc_vm_file(*fname++); |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7175 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) { |
| 7176 | // Debug config, non-interactive mode: |
| 7177 | // return all the way back to main. |
| 7178 | // Non-debug builds do not come here |
| 7179 | // in non-interactive mode, they exit. |
| 7180 | RETURN_STATUS(s); |
| 7181 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7182 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7183 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7184 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7185 | s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7186 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7187 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7188 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7189 | #define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7190 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7191 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7192 | static void xc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7193 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7194 | bc_vec_free(&G.prog.fns); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7195 | IF_BC(bc_vec_free(&G.prog.fn_map);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7196 | bc_vec_free(&G.prog.vars); |
| 7197 | bc_vec_free(&G.prog.var_map); |
| 7198 | bc_vec_free(&G.prog.arrs); |
| 7199 | bc_vec_free(&G.prog.arr_map); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7200 | IF_DC(bc_vec_free(&G.prog.strs);) |
| 7201 | IF_DC(bc_vec_free(&G.prog.consts);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7202 | bc_vec_free(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7203 | bc_vec_free(&G.prog.exestack); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7204 | IF_BC(bc_num_free(&G.prog.last);) |
Denys Vlasenko | db8d607 | 2018-12-27 18:08:30 +0100 | [diff] [blame] | 7205 | //IF_BC(bc_num_free(&G.prog.zero);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7206 | IF_BC(bc_num_free(&G.prog.one);) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7207 | bc_vec_free(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7208 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7209 | #endif |
| 7210 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7211 | static void xc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7212 | { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7213 | BcInstPtr ip; |
| 7214 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7215 | // memset(&G.prog, 0, sizeof(G.prog)); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7216 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7217 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7218 | // G.prog.nchars = G.prog.scale = 0; - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7219 | G.prog.ib_t = 10; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7220 | G.prog.ob_t = 10; |
| 7221 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7222 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);) |
| 7223 | //IF_BC(bc_num_zero(&G.prog.last);) - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7224 | |
Denys Vlasenko | db8d607 | 2018-12-27 18:08:30 +0100 | [diff] [blame] | 7225 | //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7226 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7227 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7228 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);) |
| 7229 | IF_BC(bc_num_one(&G.prog.one);) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7230 | |
| 7231 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7232 | 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] | 7233 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7234 | if (IS_BC) { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7235 | // Names are chosen simply to be distinct and never match |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 7236 | // a valid function name (and be short) |
| 7237 | IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7238 | IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read() |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7239 | } else { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7240 | // in dc, functions have no names |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7241 | xc_program_add_fn(); |
| 7242 | xc_program_add_fn(); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7243 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7244 | |
| 7245 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7246 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7247 | |
| 7248 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7249 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7250 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7251 | IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);) |
| 7252 | 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] | 7253 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7254 | bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL); |
| 7255 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7256 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7257 | bc_char_vec_init(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7258 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7259 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7260 | static int xc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7261 | { |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7262 | G.prog.len = xc_vm_envLen(env_len); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7263 | #if ENABLE_FEATURE_EDITING |
| 7264 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7265 | #endif |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7266 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7267 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7268 | xc_program_init(); |
Denys Vlasenko | 5f263f4 | 2018-12-13 22:49:59 +0100 | [diff] [blame] | 7269 | IF_BC(if (IS_BC) bc_vm_envArgs();) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7270 | xc_parse_create(BC_PROG_MAIN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7271 | |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7272 | //TODO: in GNU bc, the check is (isatty(0) && isatty(1)), |
| 7273 | //-i option unconditionally enables this regardless of isatty(): |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7274 | if (isatty(0)) { |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 7275 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7276 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7277 | // With SA_RESTART, most system calls will restart |
| 7278 | // (IOW: they won't fail with EINTR). |
| 7279 | // In particular, this means ^C won't cause |
| 7280 | // stdout to get into "error state" if SIGINT hits |
| 7281 | // within write() syscall. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7282 | // |
| 7283 | // The downside is that ^C while tty input is taken |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7284 | // will only be handled after [Enter] since read() |
| 7285 | // from stdin is not interrupted by ^C either, |
| 7286 | // it restarts, thus fgetc() does not return on ^C. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7287 | // (This problem manifests only if line editing is disabled) |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7288 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7289 | |
| 7290 | // Without SA_RESTART, this exhibits a bug: |
| 7291 | // "while (1) print 1" and try ^C-ing it. |
| 7292 | // Intermittently, instead of returning to input line, |
| 7293 | // you'll get "output error: Interrupted system call" |
| 7294 | // and exit. |
| 7295 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7296 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7297 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7298 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7299 | return 0; // "not a tty" |
| 7300 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7301 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7302 | static BcStatus xc_vm_run(void) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7303 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7304 | BcStatus st = zxc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7305 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7306 | if (G_exiting) // it was actually "halt" or "quit" |
| 7307 | st = EXIT_SUCCESS; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7308 | |
| 7309 | bc_vec_free(&G.files); |
| 7310 | xc_program_free(); |
| 7311 | xc_parse_free(); |
| 7312 | free(G.env_args); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7313 | # if ENABLE_FEATURE_EDITING |
| 7314 | free_line_input_t(G.line_input_state); |
| 7315 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7316 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7317 | #endif |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7318 | dbg_exec("exiting with exitcode %d", st); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7319 | return st; |
| 7320 | } |
| 7321 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7322 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7323 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7324 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7325 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7326 | int is_tty; |
| 7327 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7328 | INIT_G(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7329 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7330 | is_tty = xc_vm_init("BC_LINE_LENGTH"); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7331 | |
| 7332 | bc_args(argv); |
| 7333 | |
| 7334 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7335 | bc_vm_info(); |
| 7336 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7337 | return xc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7338 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7339 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7340 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7341 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7342 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7343 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7344 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7345 | int noscript; |
| 7346 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7347 | INIT_G(); |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7348 | |
| 7349 | // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7350 | // 1 char wider than bc from the same package. |
| 7351 | // Both default width, and xC_LINE_LENGTH=N are wider: |
| 7352 | // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7353 | // |1234\ | |
| 7354 | // |56 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7355 | // "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7356 | // |123\ | |
| 7357 | // |456 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7358 | // Do the same, or it's a bug? |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7359 | xc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7360 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7361 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7362 | noscript = BC_FLAG_I; |
| 7363 | for (;;) { |
| 7364 | int n = getopt(argc, argv, "e:f:x"); |
| 7365 | if (n <= 0) |
| 7366 | break; |
| 7367 | switch (n) { |
| 7368 | case 'e': |
| 7369 | noscript = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7370 | n = zxc_vm_process(optarg); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7371 | if (n) return n; |
| 7372 | break; |
| 7373 | case 'f': |
| 7374 | noscript = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7375 | n = zxc_vm_file(optarg); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7376 | if (n) return n; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7377 | break; |
| 7378 | case 'x': |
| 7379 | option_mask32 |= DC_FLAG_X; |
| 7380 | break; |
| 7381 | default: |
| 7382 | bb_show_usage(); |
| 7383 | } |
| 7384 | } |
| 7385 | argv += optind; |
| 7386 | |
| 7387 | while (*argv) { |
| 7388 | noscript = 0; |
| 7389 | bc_vec_push(&G.files, argv++); |
| 7390 | } |
| 7391 | |
| 7392 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7393 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7394 | return xc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7395 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7396 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7397 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 7398 | #endif // DC_BIG |
| 7399 | |