Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Gavin Howard | fa495ce | 2018-12-18 10:03:14 -0700 | [diff] [blame] | 4 | * Adapted from https://github.com/gavinhoward/bc |
| 5 | * Original code copyright (c) 2018 Gavin D. Howard and contributors. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6 | */ |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 7 | //TODO: |
| 8 | // maybe implement a^b for non-integer b? |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 9 | |
| 10 | #define DEBUG_LEXER 0 |
| 11 | #define DEBUG_COMPILE 0 |
| 12 | #define DEBUG_EXEC 0 |
| 13 | // This can be left enabled for production as well: |
| 14 | #define SANITY_CHECKS 1 |
| 15 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 16 | //config:config BC |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 17 | //config: bool "bc (45 kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 18 | //config: default y |
Denys Vlasenko | cdadad5 | 2018-12-28 15:13:23 +0100 | [diff] [blame] | 19 | //config: select FEATURE_DC_BIG |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 20 | //config: help |
| 21 | //config: bc is a command-line, arbitrary-precision calculator with a |
| 22 | //config: Turing-complete language. See the GNU bc manual |
| 23 | //config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 24 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 25 | //config: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 26 | //config: This bc has five differences to the GNU bc: |
| 27 | //config: 1) The period (.) is a shortcut for "last", as in the BSD bc. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 28 | //config: 2) Arrays are copied before being passed as arguments to |
| 29 | //config: functions. This behavior is required by the bc spec. |
| 30 | //config: 3) Arrays can be passed to the builtin "length" function to get |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 31 | //config: the number of elements in the array. This prints "1": |
| 32 | //config: a[0] = 0; length(a[]) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 33 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 34 | //config: that of the unary minus (-) negation operator. This still |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 35 | //config: allows POSIX-compliant scripts to work while somewhat |
| 36 | //config: preserving expected behavior (versus C) and making parsing |
| 37 | //config: easier. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 38 | //config: 5) "read()" accepts expressions, not only numeric literals. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 39 | //config: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 40 | //config:config DC |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 41 | //config: bool "dc (36 kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 42 | //config: default y |
| 43 | //config: help |
| 44 | //config: dc is a reverse-polish notation command-line calculator which |
| 45 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 46 | //config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 47 | //config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 48 | //config: |
| 49 | //config: This dc has a few differences from the two above: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 50 | //config: 1) When printing a byte stream (command "P"), this dc follows what |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 51 | //config: the FreeBSD dc does. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 52 | //config: 2) Implements the GNU extensions for divmod ("~") and |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 53 | //config: modular exponentiation ("|"). |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 54 | //config: 3) Implements all FreeBSD extensions, except for "J" and "M". |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 55 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 56 | //config: However, they are implemented differently. When it encounters |
| 57 | //config: whitespace where a register should be, it skips the whitespace. |
| 58 | //config: If the character following is not a lowercase letter, an error |
| 59 | //config: is issued. Otherwise, the register name is parsed by the |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 60 | //config: following regex: [a-z][a-z0-9_]* |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 61 | //config: This generally means that register names will be surrounded by |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 62 | //config: whitespace. Examples: |
| 63 | //config: l idx s temp L index S temp2 < do_thing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 64 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 65 | //config: allowed unless the "-x" option is given. |
| 66 | //config: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 67 | //config:if BC || DC # for menuconfig indenting |
| 68 | //config: |
| 69 | //config:config FEATURE_DC_BIG |
| 70 | //config: bool "Use bc code base for dc (larger, more features)" |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 71 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 72 | //config: |
| 73 | //config:config FEATURE_DC_LIBM |
| 74 | //config: bool "Enable power and exp functions (requires libm)" |
| 75 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 76 | //config: depends on DC && !BC && !FEATURE_DC_BIG |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 77 | //config: help |
| 78 | //config: Enable power and exp functions. |
| 79 | //config: NOTE: This will require libm to be present for linking. |
| 80 | //config: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 81 | //config:config FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 82 | //config: bool "Interactive mode (+4kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 83 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 84 | //config: depends on BC || (DC && FEATURE_DC_BIG) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 85 | //config: help |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 86 | //config: Enable interactive mode: when started on a tty, |
| 87 | //config: ^C interrupts execution and returns to command line, |
| 88 | //config: errors also return to command line instead of exiting, |
| 89 | //config: line editing with history is available. |
| 90 | //config: |
| 91 | //config: With this option off, input can still be taken from tty, |
| 92 | //config: but all errors are fatal, ^C is fatal, |
| 93 | //config: tty is treated exactly the same as any other |
| 94 | //config: standard input (IOW: no line editing). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 95 | //config: |
| 96 | //config:config FEATURE_BC_LONG_OPTIONS |
| 97 | //config: bool "Enable bc/dc long options" |
| 98 | //config: default y |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 99 | //config: depends on BC || (DC && FEATURE_DC_BIG) |
| 100 | //config: |
| 101 | //config:endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 102 | |
| 103 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 104 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 105 | |
| 106 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 107 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 108 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 109 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 110 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 09fe0aa | 2018-12-18 16:32:25 +0100 | [diff] [blame] | 111 | //usage: "[-sqlw] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 112 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 113 | //usage:#define bc_full_usage "\n" |
| 114 | //usage: "\nArbitrary precision calculator" |
| 115 | //usage: "\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 116 | ///////: "\n -i Interactive" - has no effect for now |
| 117 | //usage: "\n -q Quiet" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 118 | //usage: "\n -l Load standard math library" |
| 119 | //usage: "\n -s Be POSIX compatible" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 120 | //usage: "\n -w Warn if extensions are used" |
| 121 | ///////: "\n -v Version" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 122 | //usage: "\n" |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 123 | //usage: "\n$BC_LINE_LENGTH changes output width" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 124 | //usage: |
| 125 | //usage:#define bc_example_usage |
| 126 | //usage: "3 + 4.129\n" |
| 127 | //usage: "1903 - 2893\n" |
| 128 | //usage: "-129 * 213.28935\n" |
| 129 | //usage: "12 / -1932\n" |
| 130 | //usage: "12 % 12\n" |
| 131 | //usage: "34 ^ 189\n" |
| 132 | //usage: "scale = 13\n" |
| 133 | //usage: "ibase = 2\n" |
| 134 | //usage: "obase = A\n" |
| 135 | //usage: |
| 136 | //usage:#define dc_trivial_usage |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 137 | //usage: IF_FEATURE_DC_BIG("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 138 | //usage: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 139 | //usage:#define dc_full_usage "\n" |
| 140 | //usage: "\nTiny RPN calculator. Operations:" |
Denys Vlasenko | 3d88cc1 | 2021-02-26 13:26:48 +0100 | [diff] [blame] | 141 | //usage: "\nArithmetic: + - * / % ^" |
| 142 | //usage: IF_FEATURE_DC_BIG( |
| 143 | //usage: "\n~ - divide with remainder" |
| 144 | //usage: "\n| - modular exponentiation" |
| 145 | //usage: "\nv - square root" |
| 146 | //////// "\n_NNN - push neagtive number -NNN |
| 147 | //////// "\n[string] - push string |
| 148 | //////// "\nR - DC_LEX_POP pop and discard |
| 149 | //////// "\nc - DC_LEX_CLEAR_STACK clear stack |
| 150 | //////// "\nd - DC_LEX_DUPLICATE pop, push, push |
| 151 | //////// "\nr - DC_LEX_SWAP pop 1, pop 2, push 1, push 2 |
| 152 | //////// "\n:r - DC_LEX_COLON pop index, pop value, store to array 'r' |
| 153 | //////// "\n;r - DC_LEX_SCOLON pop index, fetch from array 'r', push |
| 154 | //////// "\nLr - DC_LEX_LOAD_PO, pop register 'r', push |
| 155 | //////// "\nSr - DC_LEX_STORE_PUSH pop, push to register 'r' |
| 156 | //////// "\nlr - DC_LEX_LOAD read register 'r', push |
| 157 | //////// "\nsr - DC_LEX_OP_ASSIGN pop, assign to register 'r' |
| 158 | //////// "\n? - DC_LEX_READ read line and execute |
| 159 | //////// "\nx - DC_LEX_EXECUTE pop string and execute |
| 160 | //////// "\n<r - XC_LEX_OP_REL_GT pop, pop, execute register 'r' if top-of-stack was greater |
| 161 | //////// "\n>r - XC_LEX_OP_REL_LT pop, pop, execute register 'r' if top-of-stack was less |
| 162 | //////// "\n=r - XC_LEX_OP_REL_EQ pop, pop, execute register 'r' if equal |
| 163 | //////// "\n!<r !>r !=r - negated forms |
| 164 | //////// "\ne - DC_LEX_ELSE >tef: "if greater execute 't' else execute 'f'" |
| 165 | //////// "\nQ - DC_LEX_NQUIT pop, "break N" from macro invocations |
| 166 | //////// "\nq - DC_LEX_QUIT "break 2" (if less than 2 levels of macros, exit dc) |
| 167 | //////// "\nX - DC_LEX_SCALE_FACTOR pop, push number of fractional digits |
| 168 | //////// "\nZ - DC_LEX_LENGTH pop, push number of digits it has (or number of characters in string) |
| 169 | //////// "\na - DC_LEX_ASCIIFY pop, push low-order byte as char or 1st string char |
| 170 | //////// "\n( - DC_LEX_LPAREN ? |
| 171 | //////// "\n{ - DC_LEX_LBRACE ? |
| 172 | //////// "\n_ - XC_LEX_NEG (not a command - starts negative number) |
| 173 | //////// "\nG - DC_LEX_EQ_NO_REG (? GNU dc has no such cmd) |
| 174 | //////// "\nN - DC_LEX_OP_BOOL_NOT (? GNU dc has no such cmd) |
| 175 | //////// "\nn - DC_LEX_PRINT_POP pop, print without newline |
| 176 | //////// "\nP - DC_LEX_PRINT_STREAM pop, print string or hex bytes |
| 177 | //usage: ) |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 178 | //usage: "\np - print top of the stack without popping" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 179 | //usage: "\nf - print entire stack" |
Denys Vlasenko | 3d88cc1 | 2021-02-26 13:26:48 +0100 | [diff] [blame] | 180 | //////// "\nz - DC_LEX_STACK_LEVEL push stack depth |
| 181 | //////// "\nK - DC_LEX_SCALE push precision |
| 182 | //////// "\nI - DC_LEX_IBASE push input radix |
| 183 | //////// "\nO - DC_LEX_OBASE push output radix |
| 184 | //usage: IF_FEATURE_DC_BIG( |
| 185 | //usage: "\nk - pop the value and set precision" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 186 | //usage: "\ni - pop the value and set input radix" |
Denys Vlasenko | 3d88cc1 | 2021-02-26 13:26:48 +0100 | [diff] [blame] | 187 | //usage: ) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 188 | //usage: "\no - pop the value and set output radix" |
| 189 | //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] | 190 | //usage: |
| 191 | //usage:#define dc_example_usage |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 192 | //usage: "$ dc -e'2 2 + p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 193 | //usage: "4\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 194 | //usage: "$ dc -e'8 8 \\* 2 2 + / p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 195 | //usage: "16\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 196 | //usage: "$ dc -e'0 1 & p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 197 | //usage: "0\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 198 | //usage: "$ dc -e'0 1 | p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 199 | //usage: "1\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 200 | //usage: "$ echo '72 9 / 8 * p' | dc\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 201 | //usage: "64\n" |
| 202 | |
| 203 | #include "libbb.h" |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 204 | #include "common_bufsiz.h" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 205 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 206 | #if !ENABLE_BC && !ENABLE_FEATURE_DC_BIG |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 207 | # include "dc.c" |
| 208 | #else |
| 209 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 210 | #if DEBUG_LEXER |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 211 | static uint8_t lex_indent; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 212 | #define dbg_lex(...) \ |
| 213 | do { \ |
| 214 | fprintf(stderr, "%*s", lex_indent, ""); \ |
| 215 | bb_error_msg(__VA_ARGS__); \ |
| 216 | } while (0) |
| 217 | #define dbg_lex_enter(...) \ |
| 218 | do { \ |
| 219 | dbg_lex(__VA_ARGS__); \ |
| 220 | lex_indent++; \ |
| 221 | } while (0) |
| 222 | #define dbg_lex_done(...) \ |
| 223 | do { \ |
| 224 | lex_indent--; \ |
| 225 | dbg_lex(__VA_ARGS__); \ |
| 226 | } while (0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 227 | #else |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 228 | # define dbg_lex(...) ((void)0) |
| 229 | # define dbg_lex_enter(...) ((void)0) |
| 230 | # define dbg_lex_done(...) ((void)0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 231 | #endif |
| 232 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 233 | #if DEBUG_COMPILE |
| 234 | # define dbg_compile(...) bb_error_msg(__VA_ARGS__) |
| 235 | #else |
| 236 | # define dbg_compile(...) ((void)0) |
| 237 | #endif |
| 238 | |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 239 | #if DEBUG_EXEC |
| 240 | # define dbg_exec(...) bb_error_msg(__VA_ARGS__) |
| 241 | #else |
| 242 | # define dbg_exec(...) ((void)0) |
| 243 | #endif |
| 244 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 245 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 246 | BC_STATUS_SUCCESS = 0, |
| 247 | BC_STATUS_FAILURE = 1, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 248 | } BcStatus; |
| 249 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 250 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 251 | #define BC_VEC_START_CAP (1 << 5) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 252 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 253 | typedef void (*BcVecFree)(void *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 254 | |
| 255 | typedef struct BcVec { |
| 256 | char *v; |
| 257 | size_t len; |
| 258 | size_t cap; |
| 259 | size_t size; |
| 260 | BcVecFree dtor; |
| 261 | } BcVec; |
| 262 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 263 | typedef signed char BcDig; |
| 264 | |
| 265 | typedef struct BcNum { |
| 266 | BcDig *restrict num; |
| 267 | size_t rdx; |
| 268 | size_t len; |
| 269 | size_t cap; |
| 270 | bool neg; |
| 271 | } BcNum; |
| 272 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 273 | #define BC_NUM_MAX_IBASE 36 |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 274 | // larger value might speed up BIGNUM calculations a bit: |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 275 | #define BC_NUM_DEF_SIZE 16 |
Denys Vlasenko | 29a9043 | 2020-12-29 18:50:56 +0100 | [diff] [blame] | 276 | #define BC_NUM_PRINT_WIDTH 70 |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 277 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 278 | #define BC_NUM_KARATSUBA_LEN 32 |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 279 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 280 | typedef enum BcInst { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 281 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 282 | BC_INST_INC_PRE, |
| 283 | BC_INST_DEC_PRE, |
| 284 | BC_INST_INC_POST, |
| 285 | BC_INST_DEC_POST, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 286 | #endif |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 287 | XC_INST_NEG, // order |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 288 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 289 | XC_INST_REL_EQ, // should |
| 290 | XC_INST_REL_LE, // match |
| 291 | XC_INST_REL_GE, // LEX |
| 292 | XC_INST_REL_NE, // constants |
| 293 | XC_INST_REL_LT, // for |
| 294 | XC_INST_REL_GT, // these |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 295 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 296 | XC_INST_POWER, // operations |
| 297 | XC_INST_MULTIPLY, // | |
| 298 | XC_INST_DIVIDE, // | |
| 299 | XC_INST_MODULUS, // | |
| 300 | XC_INST_PLUS, // | |
| 301 | XC_INST_MINUS, // | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 302 | |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 303 | XC_INST_BOOL_NOT, // | |
| 304 | XC_INST_BOOL_OR, // | |
| 305 | XC_INST_BOOL_AND, // | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 306 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 307 | BC_INST_ASSIGN_POWER, // | |
| 308 | BC_INST_ASSIGN_MULTIPLY,// | |
| 309 | BC_INST_ASSIGN_DIVIDE, // | |
| 310 | BC_INST_ASSIGN_MODULUS, // | |
| 311 | BC_INST_ASSIGN_PLUS, // | |
| 312 | BC_INST_ASSIGN_MINUS, // | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 313 | #endif |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 314 | XC_INST_ASSIGN, // V |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 315 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 316 | XC_INST_NUM, |
| 317 | XC_INST_VAR, |
| 318 | XC_INST_ARRAY_ELEM, |
| 319 | XC_INST_ARRAY, |
| 320 | XC_INST_SCALE_FUNC, |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 321 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 322 | XC_INST_IBASE, // order of these constans should match other enums |
| 323 | XC_INST_OBASE, // order of these constans should match other enums |
| 324 | XC_INST_SCALE, // order of these constans should match other enums |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 325 | 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] | 326 | XC_INST_LENGTH, |
| 327 | XC_INST_READ, |
| 328 | XC_INST_SQRT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 329 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 330 | XC_INST_PRINT, |
| 331 | XC_INST_PRINT_POP, |
| 332 | XC_INST_STR, |
| 333 | XC_INST_PRINT_STR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 334 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 335 | #if ENABLE_BC |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 336 | BC_INST_HALT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 337 | BC_INST_JUMP, |
| 338 | BC_INST_JUMP_ZERO, |
| 339 | |
| 340 | BC_INST_CALL, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 341 | BC_INST_RET0, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 342 | #endif |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 343 | XC_INST_RET, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 344 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 345 | XC_INST_POP, |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 346 | #if ENABLE_DC |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 347 | DC_INST_POP_EXEC, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 348 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 349 | DC_INST_MODEXP, |
| 350 | DC_INST_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 351 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 352 | DC_INST_EXECUTE, |
| 353 | DC_INST_EXEC_COND, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 354 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 355 | DC_INST_ASCIIFY, |
| 356 | DC_INST_PRINT_STREAM, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 357 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 358 | DC_INST_PRINT_STACK, |
| 359 | DC_INST_CLEAR_STACK, |
| 360 | DC_INST_STACK_LEN, |
| 361 | DC_INST_DUPLICATE, |
| 362 | DC_INST_SWAP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 363 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 364 | DC_INST_LOAD, |
| 365 | DC_INST_PUSH_VAR, |
| 366 | DC_INST_PUSH_TO_VAR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 367 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 368 | DC_INST_QUIT, |
| 369 | DC_INST_NQUIT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 370 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 371 | DC_INST_INVALID = -1, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 372 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 373 | } BcInst; |
| 374 | |
| 375 | typedef struct BcId { |
| 376 | char *name; |
| 377 | size_t idx; |
| 378 | } BcId; |
| 379 | |
| 380 | typedef struct BcFunc { |
| 381 | BcVec code; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 382 | IF_BC(BcVec labels;) |
| 383 | IF_BC(BcVec autos;) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 384 | IF_BC(BcVec strs;) |
| 385 | IF_BC(BcVec consts;) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 386 | IF_BC(size_t nparams;) |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 387 | IF_BC(bool voidfunc;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 388 | } BcFunc; |
| 389 | |
| 390 | typedef enum BcResultType { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 391 | XC_RESULT_TEMP, |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 392 | IF_BC(BC_RESULT_VOID,) // same as TEMP, but INST_PRINT will ignore it |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 393 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 394 | XC_RESULT_VAR, |
| 395 | XC_RESULT_ARRAY_ELEM, |
| 396 | XC_RESULT_ARRAY, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 397 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 398 | XC_RESULT_STR, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 399 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 400 | //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct, |
| 401 | XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE |
| 402 | XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE |
| 403 | XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 404 | 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] | 405 | XC_RESULT_CONSTANT, |
| 406 | IF_BC(BC_RESULT_ONE,) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 407 | } BcResultType; |
| 408 | |
| 409 | typedef union BcResultData { |
| 410 | BcNum n; |
| 411 | BcVec v; |
| 412 | BcId id; |
| 413 | } BcResultData; |
| 414 | |
| 415 | typedef struct BcResult { |
| 416 | BcResultType t; |
| 417 | BcResultData d; |
| 418 | } BcResult; |
| 419 | |
| 420 | typedef struct BcInstPtr { |
| 421 | size_t func; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 422 | size_t inst_idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 423 | } BcInstPtr; |
| 424 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 425 | typedef enum BcType { |
| 426 | BC_TYPE_VAR, |
| 427 | BC_TYPE_ARRAY, |
| 428 | BC_TYPE_REF, |
| 429 | } BcType; |
| 430 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 431 | typedef enum BcLexType { |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 432 | XC_LEX_EOF, |
| 433 | XC_LEX_INVALID, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 434 | |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 435 | XC_LEX_NLINE, |
| 436 | XC_LEX_WHITESPACE, |
| 437 | XC_LEX_STR, |
| 438 | XC_LEX_NAME, |
| 439 | XC_LEX_NUMBER, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 440 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 441 | XC_LEX_1st_op, |
| 442 | XC_LEX_NEG = XC_LEX_1st_op, // order |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 443 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 444 | XC_LEX_OP_REL_EQ, // should |
| 445 | XC_LEX_OP_REL_LE, // match |
| 446 | XC_LEX_OP_REL_GE, // INST |
| 447 | XC_LEX_OP_REL_NE, // constants |
| 448 | XC_LEX_OP_REL_LT, // for |
| 449 | XC_LEX_OP_REL_GT, // these |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 450 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 451 | XC_LEX_OP_POWER, // operations |
| 452 | XC_LEX_OP_MULTIPLY, // | |
| 453 | XC_LEX_OP_DIVIDE, // | |
| 454 | XC_LEX_OP_MODULUS, // | |
| 455 | XC_LEX_OP_PLUS, // | |
| 456 | XC_LEX_OP_MINUS, // | |
| 457 | XC_LEX_OP_last = XC_LEX_OP_MINUS, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 458 | #if ENABLE_BC |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 459 | BC_LEX_OP_BOOL_NOT, // | |
| 460 | BC_LEX_OP_BOOL_OR, // | |
| 461 | BC_LEX_OP_BOOL_AND, // | |
| 462 | |
| 463 | BC_LEX_OP_ASSIGN_POWER, // | |
| 464 | BC_LEX_OP_ASSIGN_MULTIPLY, // | |
| 465 | BC_LEX_OP_ASSIGN_DIVIDE, // | |
| 466 | BC_LEX_OP_ASSIGN_MODULUS, // | |
| 467 | BC_LEX_OP_ASSIGN_PLUS, // | |
| 468 | BC_LEX_OP_ASSIGN_MINUS, // | |
| 469 | |
| 470 | BC_LEX_OP_ASSIGN, // V |
| 471 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 472 | BC_LEX_OP_INC, |
| 473 | BC_LEX_OP_DEC, |
| 474 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 475 | BC_LEX_LPAREN, // () are 0x28 and 0x29 |
| 476 | 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] | 477 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 478 | BC_LEX_LBRACKET, // [] are 0x5B and 0x5D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 479 | BC_LEX_COMMA, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 480 | 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] | 481 | |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 482 | BC_LEX_LBRACE, // {} are 0x7B and 0x7D |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 483 | BC_LEX_SCOLON, |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 484 | 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] | 485 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 486 | BC_LEX_KEY_1st_keyword, |
| 487 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 488 | BC_LEX_KEY_BREAK, |
| 489 | BC_LEX_KEY_CONTINUE, |
| 490 | BC_LEX_KEY_DEFINE, |
| 491 | BC_LEX_KEY_ELSE, |
| 492 | BC_LEX_KEY_FOR, |
| 493 | BC_LEX_KEY_HALT, |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 494 | // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct, |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 495 | BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE |
| 496 | BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 497 | BC_LEX_KEY_IF, |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 498 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 499 | BC_LEX_KEY_LENGTH, |
| 500 | BC_LEX_KEY_LIMITS, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 501 | BC_LEX_KEY_PRINT, |
| 502 | BC_LEX_KEY_QUIT, |
| 503 | BC_LEX_KEY_READ, |
| 504 | BC_LEX_KEY_RETURN, |
| 505 | BC_LEX_KEY_SCALE, |
| 506 | BC_LEX_KEY_SQRT, |
| 507 | BC_LEX_KEY_WHILE, |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 508 | #endif // ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 509 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 510 | #if ENABLE_DC |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 511 | DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1, |
| 512 | DC_LEX_OP_ASSIGN, |
| 513 | |
| 514 | DC_LEX_LPAREN, |
| 515 | DC_LEX_SCOLON, |
| 516 | DC_LEX_READ, |
| 517 | DC_LEX_IBASE, |
| 518 | DC_LEX_SCALE, |
| 519 | DC_LEX_OBASE, |
| 520 | DC_LEX_LENGTH, |
| 521 | DC_LEX_PRINT, |
| 522 | DC_LEX_QUIT, |
| 523 | DC_LEX_SQRT, |
| 524 | DC_LEX_LBRACE, |
| 525 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 526 | DC_LEX_EQ_NO_REG, |
| 527 | DC_LEX_OP_MODEXP, |
| 528 | DC_LEX_OP_DIVMOD, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 529 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 530 | DC_LEX_COLON, |
| 531 | DC_LEX_ELSE, |
| 532 | DC_LEX_EXECUTE, |
| 533 | DC_LEX_PRINT_STACK, |
| 534 | DC_LEX_CLEAR_STACK, |
| 535 | DC_LEX_STACK_LEVEL, |
| 536 | DC_LEX_DUPLICATE, |
| 537 | DC_LEX_SWAP, |
| 538 | DC_LEX_POP, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 539 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 540 | DC_LEX_ASCIIFY, |
| 541 | DC_LEX_PRINT_STREAM, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 542 | |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 543 | // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct, |
| 544 | DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE |
| 545 | DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE |
| 546 | DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE |
| 547 | DC_LEX_LOAD, |
| 548 | DC_LEX_LOAD_POP, |
| 549 | DC_LEX_STORE_PUSH, |
| 550 | DC_LEX_PRINT_POP, |
| 551 | DC_LEX_NQUIT, |
| 552 | DC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 553 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 554 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 555 | // must match order of BC_LEX_KEY_foo etc above |
| 556 | #if ENABLE_BC |
| 557 | struct BcLexKeyword { |
| 558 | char name8[8]; |
| 559 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 560 | #define LEX_KW_ENTRY(a, b) \ |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 561 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 6cc4962 | 2020-11-30 14:58:02 +0100 | [diff] [blame] | 562 | static const struct BcLexKeyword bc_lex_kws[20] ALIGN8 = { |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 563 | LEX_KW_ENTRY("auto" , 1), // 0 |
| 564 | LEX_KW_ENTRY("break" , 1), // 1 |
| 565 | LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 566 | LEX_KW_ENTRY("define" , 1), // 3 |
| 567 | LEX_KW_ENTRY("else" , 0), // 4 |
| 568 | LEX_KW_ENTRY("for" , 1), // 5 |
| 569 | LEX_KW_ENTRY("halt" , 0), // 6 |
| 570 | LEX_KW_ENTRY("ibase" , 1), // 7 |
| 571 | LEX_KW_ENTRY("obase" , 1), // 8 |
| 572 | LEX_KW_ENTRY("if" , 1), // 9 |
| 573 | LEX_KW_ENTRY("last" , 0), // 10 |
| 574 | LEX_KW_ENTRY("length" , 1), // 11 |
| 575 | LEX_KW_ENTRY("limits" , 0), // 12 |
| 576 | LEX_KW_ENTRY("print" , 0), // 13 |
| 577 | LEX_KW_ENTRY("quit" , 1), // 14 |
| 578 | LEX_KW_ENTRY("read" , 0), // 15 |
| 579 | LEX_KW_ENTRY("return" , 1), // 16 |
| 580 | LEX_KW_ENTRY("scale" , 1), // 17 |
| 581 | LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 582 | LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 583 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 584 | #undef LEX_KW_ENTRY |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 585 | #define STRING_else (bc_lex_kws[4].name8) |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 586 | #define STRING_for (bc_lex_kws[5].name8) |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 587 | #define STRING_if (bc_lex_kws[9].name8) |
| 588 | #define STRING_while (bc_lex_kws[19].name8) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 589 | enum { |
| 590 | POSIX_KWORD_MASK = 0 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 591 | | (1 << 0) // 0 |
| 592 | | (1 << 1) // 1 |
| 593 | | (0 << 2) // 2 |
| 594 | | (1 << 3) // 3 |
| 595 | | (0 << 4) // 4 |
| 596 | | (1 << 5) // 5 |
| 597 | | (0 << 6) // 6 |
| 598 | | (1 << 7) // 7 |
| 599 | | (1 << 8) // 8 |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 600 | | (1 << 9) // 9 |
| 601 | | (0 << 10) // 10 |
| 602 | | (1 << 11) // 11 |
| 603 | | (0 << 12) // 12 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 604 | | (0 << 13) // 13 |
| 605 | | (1 << 14) // 14 |
| 606 | | (0 << 15) // 15 |
| 607 | | (1 << 16) // 16 |
| 608 | | (1 << 17) // 17 |
| 609 | | (1 << 18) // 18 |
| 610 | | (1 << 19) // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 611 | }; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 612 | #define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 613 | |
| 614 | // This is a bit array that corresponds to token types. An entry is |
| 615 | // true if the token is valid in an expression, false otherwise. |
| 616 | // Used to figure out when expr parsing should stop *without error message* |
| 617 | // - 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] | 618 | // 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] | 619 | // by later processing. |
| 620 | enum { |
| 621 | #define EXBITS(a,b,c,d,e,f,g,h) \ |
| 622 | ((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] | 623 | BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz: |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 624 | + (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] | 625 | + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ * |
| 626 | + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^= |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 627 | + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ -- |
| 628 | + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; } |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 629 | + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase |
| 630 | + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read |
| 631 | + (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] | 632 | #undef EXBITS |
| 633 | }; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 634 | static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 635 | { |
| 636 | #if ULONG_MAX > 0xffffffff |
| 637 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 638 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 639 | #else |
| 640 | // 32-bit version |
| 641 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 642 | if (i >= 32) { |
| 643 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 644 | i &= 31; |
| 645 | } |
| 646 | return m & (1UL << i); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 647 | #endif |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | // This is an array of data for operators that correspond to |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 651 | // [XC_LEX_1st_op...] token types. |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 652 | static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 653 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 654 | OP(1, false), // neg |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 655 | 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] | 656 | OP(2, false), // pow |
| 657 | OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod |
| 658 | OP(4, true ), OP( 4, true ), // + - |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 659 | OP(1, false), // not |
| 660 | OP(7, true ), OP( 7, true ), // or and |
| 661 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 662 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | abf6cf6 | 2018-12-24 13:20:57 +0100 | [diff] [blame] | 663 | OP(0, false), OP( 0, false ), // inc dec |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 664 | #undef OP |
| 665 | }; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 666 | #define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f) |
| 667 | #define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 668 | #endif // ENABLE_BC |
| 669 | |
| 670 | #if ENABLE_DC |
Denys Vlasenko | 73b2c60 | 2018-12-24 01:02:59 +0100 | [diff] [blame] | 671 | static const //BcLexType - should be this type |
| 672 | uint8_t |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 673 | dc_char_to_LEX[] ALIGN1 = { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 674 | // %&'( |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 675 | 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] | 676 | // )*+, |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 677 | 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] | 678 | // -./ |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 679 | XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 680 | // 0123456789 |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 681 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 682 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 683 | XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 684 | // :;<=>?@ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 685 | DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ, |
| 686 | XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 687 | // ABCDEFGH |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 688 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
| 689 | 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] | 690 | // IJKLMNOP |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 691 | DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP, |
| 692 | XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM, |
Denys Vlasenko | f11b5b9 | 2019-01-04 15:54:40 +0100 | [diff] [blame] | 693 | // QRSTUVWX |
| 694 | DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, |
| 695 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, |
| 696 | // YZ |
| 697 | XC_LEX_INVALID, DC_LEX_LENGTH, |
| 698 | // [\] |
| 699 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 700 | // ^_` |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 701 | XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 702 | // abcdefgh |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 703 | DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE, |
| 704 | 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] | 705 | // ijklmnop |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 706 | 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] | 707 | 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] | 708 | // qrstuvwx |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 709 | DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID, |
| 710 | XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 711 | // yz |
Denys Vlasenko | 7d9be0b | 2018-12-24 12:25:20 +0100 | [diff] [blame] | 712 | XC_LEX_INVALID, DC_LEX_STACK_LEVEL, |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 713 | // {|}~ |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 714 | 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] | 715 | }; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 716 | 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] | 717 | int8_t |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 718 | dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz: |
| 719 | XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY |
| 720 | XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS |
| 721 | 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] | 722 | XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT |
| 723 | DC_INST_INVALID, // DC_LEX_OP_ASSIGN |
| 724 | XC_INST_REL_GT, // DC_LEX_LPAREN |
| 725 | DC_INST_INVALID, // DC_LEX_SCOLON |
| 726 | DC_INST_INVALID, // DC_LEX_READ |
| 727 | XC_INST_IBASE, // DC_LEX_IBASE |
| 728 | XC_INST_SCALE, // DC_LEX_SCALE |
| 729 | XC_INST_OBASE, // DC_LEX_OBASE |
| 730 | XC_INST_LENGTH, // DC_LEX_LENGTH |
| 731 | XC_INST_PRINT, // DC_LEX_PRINT |
| 732 | DC_INST_QUIT, // DC_LEX_QUIT |
| 733 | XC_INST_SQRT, // DC_LEX_SQRT |
| 734 | XC_INST_REL_GE, // DC_LEX_LBRACE |
| 735 | XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 736 | DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD |
| 737 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE |
| 738 | DC_INST_EXECUTE, // DC_LEX_EXECUTE |
| 739 | DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK |
| 740 | DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE |
| 741 | DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP |
| 742 | DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM |
| 743 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE |
| 744 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD |
| 745 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH |
| 746 | XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT |
| 747 | XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR |
Denys Vlasenko | 9d9c97e | 2018-12-24 15:00:56 +0100 | [diff] [blame] | 748 | // DC_INST_INVALID in this table either means that corresponding LEX |
| 749 | // is not possible for dc, or that it does not compile one-to-one |
| 750 | // to a single INST. |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 751 | }; |
| 752 | #endif // ENABLE_DC |
| 753 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 754 | typedef struct BcParse { |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 755 | smallint lex; // was BcLexType // first member is most used |
| 756 | smallint lex_last; // was BcLexType |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 757 | size_t lex_line; |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 758 | const char *lex_inbuf; |
| 759 | 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] | 760 | const char *lex_filename; |
| 761 | FILE *lex_input_fp; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 762 | BcVec lex_strnumbuf; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 763 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 764 | BcFunc *func; |
| 765 | size_t fidx; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 766 | IF_BC(size_t in_funcdef;) |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 767 | IF_BC(BcVec exits;) |
| 768 | IF_BC(BcVec conds;) |
| 769 | IF_BC(BcVec ops;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 770 | } BcParse; |
| 771 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 772 | typedef struct BcProgram { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 773 | size_t len; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 774 | size_t nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 775 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 776 | size_t scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 777 | size_t ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 778 | size_t ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 779 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 780 | BcVec results; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 781 | BcVec exestack; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 782 | |
| 783 | BcVec fns; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 784 | IF_BC(BcVec fn_map;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 785 | |
| 786 | BcVec vars; |
| 787 | BcVec var_map; |
| 788 | |
| 789 | BcVec arrs; |
| 790 | BcVec arr_map; |
| 791 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 792 | IF_DC(BcVec strs;) |
| 793 | IF_DC(BcVec consts;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 794 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 795 | BcNum zero; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 796 | IF_BC(BcNum one;) |
| 797 | IF_BC(BcNum last;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 798 | } BcProgram; |
| 799 | |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 800 | struct globals { |
| 801 | BcParse prs; // first member is most used |
| 802 | |
| 803 | // For error messages. Can be set to current parsed line, |
| 804 | // or [TODO] to current executing line (can be before last parsed one) |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 805 | size_t err_line; |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 806 | |
| 807 | BcVec input_buffer; |
| 808 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 809 | IF_FEATURE_BC_INTERACTIVE(smallint ttyin;) |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 810 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
| 811 | |
| 812 | BcProgram prog; |
| 813 | |
| 814 | BcVec files; |
| 815 | |
| 816 | char *env_args; |
| 817 | |
| 818 | #if ENABLE_FEATURE_EDITING |
| 819 | line_input_t *line_input_state; |
| 820 | #endif |
| 821 | } FIX_ALIASING; |
| 822 | #define G (*ptr_to_globals) |
| 823 | #define INIT_G() do { \ |
| 824 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 825 | } while (0) |
| 826 | #define FREE_G() do { \ |
| 827 | FREE_PTR_TO_GLOBALS(); \ |
| 828 | } while (0) |
| 829 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 830 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
| 831 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 832 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 833 | # define G_interrupt bb_got_signal |
| 834 | # define G_ttyin G.ttyin |
| 835 | #else |
| 836 | # define G_interrupt 0 |
| 837 | # define G_ttyin 0 |
| 838 | #endif |
| 839 | #if ENABLE_FEATURE_CLEAN_UP |
| 840 | # define G_exiting G.exiting |
| 841 | #else |
| 842 | # define G_exiting 0 |
| 843 | #endif |
| 844 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
| 845 | #define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b')) |
| 846 | |
Denys Vlasenko | 6e61823 | 2018-12-25 22:20:14 +0100 | [diff] [blame] | 847 | #if ENABLE_BC |
| 848 | # define BC_PARSE_REL (1 << 0) |
| 849 | # define BC_PARSE_PRINT (1 << 1) |
| 850 | # define BC_PARSE_ARRAY (1 << 2) |
| 851 | # define BC_PARSE_NOCALL (1 << 3) |
| 852 | #endif |
| 853 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 854 | #define BC_PROG_MAIN 0 |
| 855 | #define BC_PROG_READ 1 |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 856 | #if ENABLE_DC |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 857 | #define BC_PROG_REQ_FUNCS 2 |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 858 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 859 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 860 | #define BC_FLAG_W (1 << 0) |
| 861 | #define BC_FLAG_V (1 << 1) |
| 862 | #define BC_FLAG_S (1 << 2) |
| 863 | #define BC_FLAG_Q (1 << 3) |
| 864 | #define BC_FLAG_L (1 << 4) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 865 | #define BC_FLAG_I ((1 << 5) * ENABLE_DC) |
| 866 | #define DC_FLAG_X ((1 << 6) * ENABLE_DC) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 867 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 868 | #define BC_MAX_OBASE ((unsigned) 999) |
| 869 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 870 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 871 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 872 | #define BC_MAX_NUM BC_MAX_STRING |
| 873 | // Unused apart from "limits" message. Just show a "biggish number" there. |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 874 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 875 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 876 | #define BC_MAX_EXP_STR "999999999" |
| 877 | #define BC_MAX_VARS_STR "999999999" |
| 878 | |
| 879 | #define BC_MAX_OBASE_STR "999" |
| 880 | |
| 881 | #if INT_MAX == 2147483647 |
| 882 | # define BC_MAX_DIM_STR "2147483647" |
| 883 | #elif INT_MAX == 9223372036854775807 |
| 884 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 885 | #else |
| 886 | # error Strange INT_MAX |
| 887 | #endif |
| 888 | |
Kang-Che Sung | f159352 | 2019-09-05 23:40:38 +0800 | [diff] [blame] | 889 | #if UINT_MAX == 4294967295U |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 890 | # define BC_MAX_SCALE_STR "4294967295" |
| 891 | # define BC_MAX_STRING_STR "4294967294" |
Kang-Che Sung | f159352 | 2019-09-05 23:40:38 +0800 | [diff] [blame] | 892 | #elif UINT_MAX == 18446744073709551615U |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 893 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 894 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 895 | #else |
| 896 | # error Strange UINT_MAX |
| 897 | #endif |
| 898 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 899 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 900 | // In configurations where errors abort instead of propagating error |
| 901 | // return code up the call chain, functions returning BC_STATUS |
| 902 | // actually don't return anything, they always succeed and return "void". |
| 903 | // A macro wrapper is provided, which makes this statement work: |
| 904 | // s = zbc_func(...) |
| 905 | // and makes it visible to the compiler that s is always zero, |
| 906 | // allowing compiler to optimize dead code after the statement. |
| 907 | // |
| 908 | // To make code more readable, each such function has a "z" |
| 909 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. |
| 910 | // |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 911 | #if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 912 | # define ERRORS_ARE_FATAL 0 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 913 | # define ERRORFUNC /*nothing*/ |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 914 | # define IF_ERROR_RETURN_POSSIBLE(a) a |
| 915 | # define BC_STATUS BcStatus |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 916 | # define RETURN_STATUS(v) return (v) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 917 | # define COMMA_SUCCESS /*nothing*/ |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 918 | #else |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 919 | # define ERRORS_ARE_FATAL 1 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 920 | # define ERRORFUNC NORETURN |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 921 | # define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/ |
| 922 | # define BC_STATUS void |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 923 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 924 | # define COMMA_SUCCESS ,BC_STATUS_SUCCESS |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 925 | #endif |
| 926 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 927 | // |
| 928 | // Utility routines |
| 929 | // |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 930 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 931 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 932 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 933 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 934 | static void fflush_and_check(void) |
| 935 | { |
| 936 | fflush_all(); |
| 937 | if (ferror(stdout) || ferror(stderr)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 938 | bb_simple_perror_msg_and_die("output error"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 941 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 942 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 943 | do { \ |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 944 | 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] | 945 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 946 | return BC_STATUS_FAILURE; \ |
| 947 | } while (0) |
| 948 | #else |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 949 | static void quit(void) NORETURN; |
| 950 | static void quit(void) |
| 951 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 952 | if (ferror(stdin)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 953 | bb_simple_perror_msg_and_die("input error"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 954 | fflush_and_check(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 955 | dbg_exec("quit(): exiting with exitcode SUCCESS"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 956 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 957 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 958 | #define QUIT_OR_RETURN_TO_MAIN quit() |
| 959 | #endif |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 960 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 961 | static void bc_verror_msg(const char *fmt, va_list p) |
| 962 | { |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 963 | const char *sv = sv; // for compiler |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 964 | if (G.prs.lex_filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 965 | sv = applet_name; |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 966 | applet_name = xasprintf("%s: %s:%lu", applet_name, |
| 967 | G.prs.lex_filename, (unsigned long)G.err_line |
| 968 | ); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 969 | } |
| 970 | bb_verror_msg(fmt, p, NULL); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 971 | if (G.prs.lex_filename) { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 972 | free((char*)applet_name); |
| 973 | applet_name = sv; |
| 974 | } |
| 975 | } |
| 976 | |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 977 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 978 | { |
| 979 | va_list p; |
| 980 | |
| 981 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 982 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 983 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 984 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 985 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
| 986 | IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE); |
| 987 | exit(1); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 988 | } |
| 989 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 990 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 991 | static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 992 | { |
| 993 | va_list p; |
| 994 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 995 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 996 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 997 | RETURN_STATUS(BC_STATUS_SUCCESS); // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 998 | |
| 999 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1000 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1001 | va_end(p); |
| 1002 | |
| 1003 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1004 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1005 | RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning |
| 1006 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1007 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1008 | RETURN_STATUS(BC_STATUS_FAILURE); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1009 | exit(1); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1010 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1011 | #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] | 1012 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1013 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1014 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 1015 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1016 | // function must not have caller-cleaned parameters on stack. |
| 1017 | // Unfortunately, vararg function API does exactly that on most arches. |
| 1018 | // 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] | 1019 | static ERRORFUNC int bc_error(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1020 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1021 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1022 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 1023 | static ERRORFUNC int bc_error_at(const char *msg) |
| 1024 | { |
| 1025 | const char *err_at = G.prs.lex_next_at; |
| 1026 | if (err_at) { |
| 1027 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt( |
| 1028 | "%s at '%.*s'", |
| 1029 | msg, |
| 1030 | (int)(strchrnul(err_at, '\n') - err_at), |
| 1031 | err_at |
| 1032 | ); |
| 1033 | } |
| 1034 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
| 1035 | } |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1036 | static ERRORFUNC int bc_error_bad_character(char c) |
| 1037 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 1038 | if (!c) |
| 1039 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1040 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1041 | } |
Denys Vlasenko | 7a4e554 | 2019-06-08 12:39:30 +0200 | [diff] [blame] | 1042 | #if ENABLE_BC |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 1043 | static ERRORFUNC int bc_error_bad_function_definition(void) |
| 1044 | { |
| 1045 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad function definition"); |
| 1046 | } |
Denys Vlasenko | 7a4e554 | 2019-06-08 12:39:30 +0200 | [diff] [blame] | 1047 | #endif |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1048 | static ERRORFUNC int bc_error_bad_expression(void) |
| 1049 | { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 1050 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad expression"); |
| 1051 | } |
| 1052 | static ERRORFUNC int bc_error_bad_assignment(void) |
| 1053 | { |
| 1054 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at( |
| 1055 | "bad assignment: left side must be variable or array element" |
| 1056 | ); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1057 | } |
| 1058 | static ERRORFUNC int bc_error_bad_token(void) |
| 1059 | { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 1060 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad token"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1061 | } |
| 1062 | static ERRORFUNC int bc_error_stack_has_too_few_elements(void) |
| 1063 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1064 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1065 | } |
| 1066 | static ERRORFUNC int bc_error_variable_is_wrong_type(void) |
| 1067 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1068 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1069 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1070 | #if ENABLE_BC |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1071 | static BC_STATUS zbc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1072 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1073 | RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg)); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1074 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1075 | #define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS) |
| 1076 | static BC_STATUS zbc_POSIX_does_not_allow(const char *msg) |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1077 | { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1078 | 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] | 1079 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1080 | #define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS) |
| 1081 | 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] | 1082 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1083 | 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] | 1084 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1085 | #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) |
| 1086 | 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] | 1087 | { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 1088 | 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] | 1089 | } |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 1090 | #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] | 1091 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1092 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1093 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1094 | { |
| 1095 | size_t cap = v->cap * 2; |
| 1096 | while (cap < v->len + n) cap *= 2; |
| 1097 | v->v = xrealloc(v->v, v->size * cap); |
| 1098 | v->cap = cap; |
| 1099 | } |
| 1100 | |
| 1101 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1102 | { |
| 1103 | v->size = esize; |
| 1104 | v->cap = BC_VEC_START_CAP; |
| 1105 | v->len = 0; |
| 1106 | v->dtor = dtor; |
| 1107 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1108 | } |
| 1109 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1110 | static void bc_char_vec_init(BcVec *v) |
| 1111 | { |
| 1112 | bc_vec_init(v, sizeof(char), NULL); |
| 1113 | } |
| 1114 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1115 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1116 | { |
| 1117 | if (v->cap < req) { |
| 1118 | v->v = xrealloc(v->v, v->size * req); |
| 1119 | v->cap = req; |
| 1120 | } |
| 1121 | } |
| 1122 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1123 | static void bc_vec_pop(BcVec *v) |
| 1124 | { |
| 1125 | v->len--; |
| 1126 | if (v->dtor) |
| 1127 | v->dtor(v->v + (v->size * v->len)); |
| 1128 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1129 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1130 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1131 | { |
| 1132 | if (!v->dtor) |
| 1133 | v->len -= n; |
| 1134 | else { |
| 1135 | size_t len = v->len - n; |
| 1136 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1137 | } |
| 1138 | } |
| 1139 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1140 | static void bc_vec_pop_all(BcVec *v) |
| 1141 | { |
| 1142 | bc_vec_npop(v, v->len); |
| 1143 | } |
| 1144 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 1145 | static size_t bc_vec_npush(BcVec *v, size_t n, const void *data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1146 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1147 | size_t len = v->len; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 1148 | if (len + n > v->cap) bc_vec_grow(v, n); |
| 1149 | memmove(v->v + (v->size * len), data, v->size * n); |
| 1150 | v->len = len + n; |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1151 | return len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1152 | } |
| 1153 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 1154 | static size_t bc_vec_push(BcVec *v, const void *data) |
| 1155 | { |
| 1156 | return bc_vec_npush(v, 1, data); |
| 1157 | //size_t len = v->len; |
| 1158 | //if (len >= v->cap) bc_vec_grow(v, 1); |
| 1159 | //memmove(v->v + (v->size * len), data, v->size); |
| 1160 | //v->len = len + 1; |
| 1161 | //return len; |
| 1162 | } |
| 1163 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 1164 | // G.prog.results often needs "pop old operand, push result" idiom. |
| 1165 | // Can do this without a few extra ops |
| 1166 | static size_t bc_result_pop_and_push(const void *data) |
| 1167 | { |
| 1168 | BcVec *v = &G.prog.results; |
| 1169 | char *last; |
| 1170 | size_t len = v->len - 1; |
| 1171 | |
| 1172 | last = v->v + (v->size * len); |
| 1173 | if (v->dtor) |
| 1174 | v->dtor(last); |
| 1175 | memmove(last, data, v->size); |
| 1176 | return len; |
| 1177 | } |
| 1178 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1179 | static size_t bc_vec_pushByte(BcVec *v, char data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1180 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1181 | return bc_vec_push(v, &data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1182 | } |
| 1183 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1184 | static size_t bc_vec_pushZeroByte(BcVec *v) |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1185 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1186 | //return bc_vec_pushByte(v, '\0'); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1187 | // better: |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1188 | return bc_vec_push(v, &const_int_0); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1191 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1192 | { |
| 1193 | if (idx == v->len) |
| 1194 | bc_vec_push(v, data); |
| 1195 | else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1196 | char *ptr; |
| 1197 | |
| 1198 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1199 | |
| 1200 | ptr = v->v + v->size * idx; |
| 1201 | |
| 1202 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1203 | memmove(ptr, data, v->size); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1208 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1209 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1210 | bc_vec_expand(v, len + 1); |
| 1211 | memcpy(v->v, str, len); |
| 1212 | v->len = len; |
| 1213 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1214 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1215 | } |
| 1216 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1217 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1218 | { |
| 1219 | return v->v + v->size * idx; |
| 1220 | } |
| 1221 | |
| 1222 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1223 | { |
| 1224 | return v->v + v->size * (v->len - idx - 1); |
| 1225 | } |
| 1226 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1227 | static void *bc_vec_top(const BcVec *v) |
| 1228 | { |
| 1229 | return v->v + v->size * (v->len - 1); |
| 1230 | } |
| 1231 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1232 | static FAST_FUNC void bc_vec_free(void *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1233 | { |
| 1234 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1235 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1236 | free(v->v); |
| 1237 | } |
| 1238 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1239 | static BcFunc* xc_program_func(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1240 | { |
| 1241 | return bc_vec_item(&G.prog.fns, idx); |
| 1242 | } |
| 1243 | // BC_PROG_MAIN is zeroth element, so: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1244 | #define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v)) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1245 | |
| 1246 | #if ENABLE_BC |
| 1247 | static BcFunc* bc_program_current_func(void) |
| 1248 | { |
| 1249 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1250 | BcFunc *func = xc_program_func(ip->func); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1251 | return func; |
| 1252 | } |
| 1253 | #endif |
| 1254 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1255 | static char** xc_program_str(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1256 | { |
| 1257 | #if ENABLE_BC |
| 1258 | if (IS_BC) { |
| 1259 | BcFunc *func = bc_program_current_func(); |
| 1260 | return bc_vec_item(&func->strs, idx); |
| 1261 | } |
| 1262 | #endif |
| 1263 | IF_DC(return bc_vec_item(&G.prog.strs, idx);) |
| 1264 | } |
| 1265 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1266 | static char** xc_program_const(size_t idx) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1267 | { |
| 1268 | #if ENABLE_BC |
| 1269 | if (IS_BC) { |
| 1270 | BcFunc *func = bc_program_current_func(); |
| 1271 | return bc_vec_item(&func->consts, idx); |
| 1272 | } |
| 1273 | #endif |
| 1274 | IF_DC(return bc_vec_item(&G.prog.consts, idx);) |
| 1275 | } |
| 1276 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1277 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1278 | { |
| 1279 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1280 | } |
| 1281 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1282 | static FAST_FUNC void bc_id_free(void *id) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1283 | { |
| 1284 | free(((BcId *) id)->name); |
| 1285 | } |
| 1286 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1287 | 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] | 1288 | { |
| 1289 | size_t low = 0, high = v->len; |
| 1290 | |
| 1291 | while (low < high) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1292 | size_t mid = (low + high) / 2; |
| 1293 | BcId *id = bc_vec_item(v, mid); |
| 1294 | int result = bc_id_cmp(ptr, id); |
| 1295 | |
| 1296 | if (result == 0) |
| 1297 | return mid; |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 1298 | if (result < 0) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1299 | high = mid; |
| 1300 | else |
| 1301 | low = mid + 1; |
| 1302 | } |
| 1303 | |
| 1304 | return low; |
| 1305 | } |
| 1306 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1307 | 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] | 1308 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1309 | size_t n = *i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1310 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1311 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1312 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1313 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1314 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1315 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1316 | bc_vec_pushAt(v, ptr, n); |
| 1317 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1318 | } |
| 1319 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1320 | 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] | 1321 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1322 | size_t i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1323 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1324 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1325 | } |
| 1326 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1327 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1328 | { |
| 1329 | n->len = 0; |
| 1330 | n->neg = false; |
| 1331 | n->rdx = scale; |
| 1332 | } |
| 1333 | |
| 1334 | static void bc_num_zero(BcNum *n) |
| 1335 | { |
| 1336 | bc_num_setToZero(n, 0); |
| 1337 | } |
| 1338 | |
| 1339 | static void bc_num_one(BcNum *n) |
| 1340 | { |
| 1341 | bc_num_setToZero(n, 0); |
| 1342 | n->len = 1; |
| 1343 | n->num[0] = 1; |
| 1344 | } |
| 1345 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1346 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1347 | static void bc_num_init(BcNum *n, size_t req) |
| 1348 | { |
| 1349 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1350 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1351 | n->num = xmalloc(req); |
| 1352 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1353 | n->rdx = 0; |
| 1354 | n->len = 0; |
| 1355 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1356 | } |
| 1357 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1358 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1359 | { |
| 1360 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1361 | } |
| 1362 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1363 | static void bc_num_expand(BcNum *n, size_t req) |
| 1364 | { |
| 1365 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1366 | if (req > n->cap) { |
| 1367 | n->num = xrealloc(n->num, req); |
| 1368 | n->cap = req; |
| 1369 | } |
| 1370 | } |
| 1371 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1372 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1373 | { |
| 1374 | free(((BcNum *) num)->num); |
| 1375 | } |
| 1376 | |
| 1377 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1378 | { |
| 1379 | if (d != s) { |
| 1380 | bc_num_expand(d, s->cap); |
| 1381 | d->len = s->len; |
| 1382 | d->neg = s->neg; |
| 1383 | d->rdx = s->rdx; |
| 1384 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1385 | } |
| 1386 | } |
| 1387 | |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1388 | 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] | 1389 | { |
| 1390 | size_t i; |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1391 | unsigned long result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1392 | |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1393 | result = 0; |
| 1394 | i = n->len; |
| 1395 | while (i > n->rdx) { |
| 1396 | unsigned long prev = result; |
| 1397 | result = result * 10 + n->num[--i]; |
| 1398 | // Even overflowed N*10 can still satisfy N*10>=N. For example, |
| 1399 | // 0x1ff00000 * 10 is 0x13f600000, |
| 1400 | // or 0x3f600000 truncated to 32 bits. Which is larger. |
| 1401 | // However, (N*10)/8 < N check is always correct. |
| 1402 | if ((result / 8) < prev) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1403 | RETURN_STATUS(bc_error("overflow")); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1404 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1405 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1406 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1407 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1408 | } |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1409 | #define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS) |
| 1410 | |
| 1411 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
| 1412 | { |
| 1413 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
| 1414 | |
| 1415 | RETURN_STATUS(zbc_num_ulong_abs(n, result_p)); |
| 1416 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1417 | #define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1418 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1419 | #if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295 |
| 1420 | # define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE) |
| 1421 | #elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615 |
| 1422 | # define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE) |
| 1423 | #endif |
| 1424 | // minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num() |
| 1425 | // would not hit realloc() code path - not good if num[] is not malloced |
| 1426 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1427 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1428 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1429 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1430 | |
| 1431 | bc_num_zero(n); |
| 1432 | |
| 1433 | if (val == 0) return; |
| 1434 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1435 | bc_num_expand(n, ULONG_NUM_BUFSIZE); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1436 | |
| 1437 | ptr = n->num; |
| 1438 | for (;;) { |
| 1439 | n->len++; |
| 1440 | *ptr++ = val % 10; |
| 1441 | val /= 10; |
| 1442 | if (val == 0) break; |
| 1443 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1444 | } |
| 1445 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1446 | 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] | 1447 | { |
| 1448 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1449 | for (i = 0; i < len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1450 | a[i] -= b[i]; |
| 1451 | for (j = i; a[j] < 0;) { |
| 1452 | a[j++] += 10; |
| 1453 | a[j] -= 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1454 | } |
| 1455 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1459 | { |
Denys Vlasenko | 4113e1f | 2018-12-18 00:39:24 +0100 | [diff] [blame] | 1460 | size_t i = len; |
| 1461 | for (;;) { |
| 1462 | int c; |
| 1463 | if (i == 0) |
| 1464 | return 0; |
| 1465 | i--; |
| 1466 | c = a[i] - b[i]; |
| 1467 | if (c != 0) { |
| 1468 | i++; |
| 1469 | if (c < 0) |
| 1470 | return -i; |
| 1471 | return i; |
| 1472 | } |
| 1473 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1474 | } |
| 1475 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1476 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 1477 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 1478 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 1479 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 1480 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 1481 | { |
| 1482 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 1483 | } |
| 1484 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 1485 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 1486 | { |
| 1487 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 1488 | } |
| 1489 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1490 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1491 | { |
| 1492 | size_t i, min, a_int, b_int, diff; |
| 1493 | BcDig *max_num, *min_num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1494 | bool a_max, neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1495 | ssize_t cmp; |
| 1496 | |
| 1497 | if (a == b) return 0; |
| 1498 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1499 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1500 | |
| 1501 | if (a->neg != b->neg) // signs of a and b differ |
| 1502 | // +a,-b = a>b = 1 or -a,+b = a<b = -1 |
| 1503 | return (int)b->neg - (int)a->neg; |
| 1504 | neg = a->neg; // 1 if both negative, 0 if both positive |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1505 | |
| 1506 | a_int = BC_NUM_INT(a); |
| 1507 | b_int = BC_NUM_INT(b); |
| 1508 | a_int -= b_int; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1509 | |
Denys Vlasenko | 0084137 | 2019-11-23 17:25:21 +0100 | [diff] [blame] | 1510 | if (a_int != 0) { |
| 1511 | if (neg) return - (ssize_t) a_int; |
| 1512 | return (ssize_t) a_int; |
| 1513 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1514 | |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1515 | a_max = (a->rdx > b->rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1516 | if (a_max) { |
| 1517 | min = b->rdx; |
| 1518 | diff = a->rdx - b->rdx; |
| 1519 | max_num = a->num + diff; |
| 1520 | min_num = b->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1521 | // neg = (a_max == neg); - NOP (maps 1->1 and 0->0) |
| 1522 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1523 | min = a->rdx; |
| 1524 | diff = b->rdx - a->rdx; |
| 1525 | max_num = b->num + diff; |
| 1526 | min_num = a->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1527 | neg = !neg; // same as "neg = (a_max == neg)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1531 | if (cmp != 0) return BC_NUM_NEG(cmp, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1532 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1533 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1534 | if (max_num[i]) return BC_NUM_NEG(1, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
| 1540 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1541 | { |
| 1542 | if (places == 0) return; |
| 1543 | |
| 1544 | n->rdx -= places; |
| 1545 | |
| 1546 | if (n->len != 0) { |
| 1547 | n->len -= places; |
| 1548 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | static void bc_num_extend(BcNum *n, size_t places) |
| 1553 | { |
| 1554 | size_t len = n->len + places; |
| 1555 | |
| 1556 | if (places != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1557 | if (n->cap < len) bc_num_expand(n, len); |
| 1558 | |
| 1559 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1560 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1561 | |
| 1562 | n->len += places; |
| 1563 | n->rdx += places; |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | static void bc_num_clean(BcNum *n) |
| 1568 | { |
| 1569 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1570 | if (n->len == 0) |
| 1571 | n->neg = false; |
| 1572 | else if (n->len < n->rdx) |
| 1573 | n->len = n->rdx; |
| 1574 | } |
| 1575 | |
| 1576 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1577 | { |
| 1578 | if (n->rdx < scale) |
| 1579 | bc_num_extend(n, scale - n->rdx); |
| 1580 | else |
| 1581 | bc_num_truncate(n, n->rdx - scale); |
| 1582 | |
| 1583 | bc_num_clean(n); |
| 1584 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1585 | } |
| 1586 | |
| 1587 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1588 | BcNum *restrict b) |
| 1589 | { |
| 1590 | if (idx < n->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1591 | b->len = n->len - idx; |
| 1592 | a->len = idx; |
| 1593 | a->rdx = b->rdx = 0; |
| 1594 | |
| 1595 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1596 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1597 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1598 | bc_num_zero(b); |
| 1599 | bc_num_copy(a, n); |
| 1600 | } |
| 1601 | |
| 1602 | bc_num_clean(a); |
| 1603 | bc_num_clean(b); |
| 1604 | } |
| 1605 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1606 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1607 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1608 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1609 | |
| 1610 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1611 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1612 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1613 | 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] | 1614 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1615 | |
| 1616 | if (n->rdx >= places) |
| 1617 | n->rdx -= places; |
| 1618 | else { |
| 1619 | bc_num_extend(n, places - n->rdx); |
| 1620 | n->rdx = 0; |
| 1621 | } |
| 1622 | |
| 1623 | bc_num_clean(n); |
| 1624 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1625 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1626 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1627 | #define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1628 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1629 | typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
| 1630 | |
| 1631 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 1632 | BcNumBinaryOp op, size_t req) |
| 1633 | { |
| 1634 | BcStatus s; |
| 1635 | BcNum num2, *ptr_a, *ptr_b; |
| 1636 | bool init = false; |
| 1637 | |
| 1638 | if (c == a) { |
| 1639 | ptr_a = &num2; |
| 1640 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 1641 | init = true; |
| 1642 | } else |
| 1643 | ptr_a = a; |
| 1644 | |
| 1645 | if (c == b) { |
| 1646 | ptr_b = &num2; |
| 1647 | if (c != a) { |
| 1648 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 1649 | init = true; |
| 1650 | } |
| 1651 | } else |
| 1652 | ptr_b = b; |
| 1653 | |
| 1654 | if (init) |
| 1655 | bc_num_init(c, req); |
| 1656 | else |
| 1657 | bc_num_expand(c, req); |
| 1658 | |
| 1659 | s = BC_STATUS_SUCCESS; |
| 1660 | IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale); |
| 1661 | |
| 1662 | if (init) bc_num_free(&num2); |
| 1663 | |
| 1664 | RETURN_STATUS(s); |
| 1665 | } |
| 1666 | #define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS) |
| 1667 | |
| 1668 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1669 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1670 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1671 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1672 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1673 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 1674 | |
| 1675 | static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1676 | { |
| 1677 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s; |
| 1678 | (void) scale; |
| 1679 | RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b))); |
| 1680 | } |
| 1681 | |
| 1682 | static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1683 | { |
| 1684 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a; |
| 1685 | (void) scale; |
| 1686 | RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b))); |
| 1687 | } |
| 1688 | |
| 1689 | static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1690 | { |
| 1691 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1692 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req)); |
| 1693 | } |
| 1694 | |
| 1695 | static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1696 | { |
| 1697 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1698 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req)); |
| 1699 | } |
| 1700 | |
| 1701 | static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1702 | { |
| 1703 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1704 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req)); |
| 1705 | } |
| 1706 | |
| 1707 | static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1708 | { |
| 1709 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); |
| 1710 | } |
| 1711 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 1712 | static const BcNumBinaryOp zxc_program_ops[] = { |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 1713 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, |
| 1714 | }; |
| 1715 | #define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS) |
| 1716 | #define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS) |
| 1717 | #define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS) |
| 1718 | #define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS) |
| 1719 | #define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS) |
| 1720 | #define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS) |
| 1721 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1722 | 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] | 1723 | { |
| 1724 | BcNum one; |
| 1725 | BcDig num[2]; |
| 1726 | |
| 1727 | one.cap = 2; |
| 1728 | one.num = num; |
| 1729 | bc_num_one(&one); |
| 1730 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1731 | RETURN_STATUS(zbc_num_div(&one, a, b, scale)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1732 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1733 | #define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1734 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1735 | 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] | 1736 | { |
| 1737 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1738 | 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] | 1739 | unsigned carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1740 | |
| 1741 | // Because this function doesn't need to use scale (per the bc spec), |
| 1742 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1743 | |
| 1744 | if (a->len == 0) { |
| 1745 | bc_num_copy(c, b); |
| 1746 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1747 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1748 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1749 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1750 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1751 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | c->neg = a->neg; |
| 1755 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1756 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1757 | c->len = 0; |
| 1758 | |
| 1759 | if (a->rdx > b->rdx) { |
| 1760 | diff = a->rdx - b->rdx; |
| 1761 | ptr = a->num; |
| 1762 | ptr_a = a->num + diff; |
| 1763 | ptr_b = b->num; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1764 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1765 | diff = b->rdx - a->rdx; |
| 1766 | ptr = b->num; |
| 1767 | ptr_a = a->num; |
| 1768 | ptr_b = b->num + diff; |
| 1769 | } |
| 1770 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1771 | ptr_c = c->num; |
| 1772 | for (i = 0; i < diff; ++i, ++c->len) |
| 1773 | ptr_c[i] = ptr[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1774 | |
| 1775 | ptr_c += diff; |
| 1776 | a_int = BC_NUM_INT(a); |
| 1777 | b_int = BC_NUM_INT(b); |
| 1778 | |
| 1779 | if (a_int > b_int) { |
| 1780 | min_int = b_int; |
| 1781 | max = a_int; |
| 1782 | ptr = ptr_a; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1783 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1784 | min_int = a_int; |
| 1785 | max = b_int; |
| 1786 | ptr = ptr_b; |
| 1787 | } |
| 1788 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1789 | carry = 0; |
| 1790 | for (i = 0; i < min_rdx + min_int; ++i) { |
| 1791 | unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1792 | carry = in / 10; |
| 1793 | ptr_c[i] = (BcDig)(in % 10); |
| 1794 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1795 | for (; i < max + min_rdx; ++i) { |
| 1796 | unsigned in = (unsigned)ptr[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1797 | carry = in / 10; |
| 1798 | ptr_c[i] = (BcDig)(in % 10); |
| 1799 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1800 | c->len += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1801 | |
| 1802 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1803 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1804 | 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] | 1805 | } |
| 1806 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1807 | 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] | 1808 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1809 | ssize_t cmp; |
| 1810 | BcNum *minuend, *subtrahend; |
| 1811 | size_t start; |
| 1812 | bool aneg, bneg, neg; |
| 1813 | |
| 1814 | // Because this function doesn't need to use scale (per the bc spec), |
| 1815 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1816 | |
| 1817 | if (a->len == 0) { |
| 1818 | bc_num_copy(c, b); |
| 1819 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1820 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1821 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1822 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1823 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1824 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | aneg = a->neg; |
| 1828 | bneg = b->neg; |
| 1829 | a->neg = b->neg = false; |
| 1830 | |
| 1831 | cmp = bc_num_cmp(a, b); |
| 1832 | |
| 1833 | a->neg = aneg; |
| 1834 | b->neg = bneg; |
| 1835 | |
| 1836 | if (cmp == 0) { |
| 1837 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1838 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1839 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1840 | if (cmp > 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1841 | neg = a->neg; |
| 1842 | minuend = a; |
| 1843 | subtrahend = b; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1844 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1845 | neg = b->neg; |
| 1846 | if (sub) neg = !neg; |
| 1847 | minuend = b; |
| 1848 | subtrahend = a; |
| 1849 | } |
| 1850 | |
| 1851 | bc_num_copy(c, minuend); |
| 1852 | c->neg = neg; |
| 1853 | |
| 1854 | if (c->rdx < subtrahend->rdx) { |
| 1855 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1856 | start = 0; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1857 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1858 | start = c->rdx - subtrahend->rdx; |
| 1859 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1860 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1861 | |
| 1862 | bc_num_clean(c); |
| 1863 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1864 | 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] | 1865 | } |
| 1866 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1867 | 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] | 1868 | BcNum *restrict c) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1869 | #define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1870 | { |
| 1871 | BcStatus s; |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 1872 | size_t max, max2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1873 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1874 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1875 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1876 | if (a->len == 0 || b->len == 0) { |
| 1877 | bc_num_zero(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1878 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1879 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1880 | aone = BC_NUM_ONE(a); |
| 1881 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1882 | bc_num_copy(c, aone ? b : a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1883 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1884 | } |
| 1885 | |
Denys Vlasenko | cc4303f | 2021-01-06 14:00:53 +0100 | [diff] [blame] | 1886 | if (a->len < BC_NUM_KARATSUBA_LEN |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1887 | || b->len < BC_NUM_KARATSUBA_LEN |
Denys Vlasenko | cc4303f | 2021-01-06 14:00:53 +0100 | [diff] [blame] | 1888 | /* || a->len + b->len < BC_NUM_KARATSUBA_LEN - redundant check */ |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1889 | ) { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1890 | size_t i, j, len; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1891 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1892 | bc_num_expand(c, a->len + b->len + 1); |
| 1893 | |
| 1894 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1895 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1896 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1897 | for (i = 0; i < b->len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1898 | unsigned carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1899 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1900 | unsigned in = c->num[i + j]; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1901 | in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1902 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1903 | carry = in / 10; |
| 1904 | c->num[i + j] = (BcDig)(in % 10); |
| 1905 | } |
| 1906 | |
| 1907 | c->num[i + j] += (BcDig) carry; |
| 1908 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1909 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 1910 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1911 | // a=2^1000000 |
| 1912 | // a*a <- without check below, this will not be interruptible |
| 1913 | if (G_interrupt) return BC_STATUS_FAILURE; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1914 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | c->len = len; |
| 1918 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1919 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1920 | } |
| 1921 | |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 1922 | max = BC_MAX(a->len, b->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1923 | bc_num_init(&l1, max); |
| 1924 | bc_num_init(&h1, max); |
| 1925 | bc_num_init(&l2, max); |
| 1926 | bc_num_init(&h2, max); |
| 1927 | bc_num_init(&m1, max); |
| 1928 | bc_num_init(&m2, max); |
| 1929 | bc_num_init(&z0, max); |
| 1930 | bc_num_init(&z1, max); |
| 1931 | bc_num_init(&z2, max); |
| 1932 | bc_num_init(&temp, max + max); |
| 1933 | |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 1934 | max2 = (max + 1) / 2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1935 | bc_num_split(a, max2, &l1, &h1); |
| 1936 | bc_num_split(b, max2, &l2, &h2); |
| 1937 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1938 | s = zbc_num_add(&h1, &l1, &m1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1939 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1940 | s = zbc_num_add(&h2, &l2, &m2, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1941 | if (s) goto err; |
| 1942 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1943 | s = zbc_num_k(&h1, &h2, &z0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1944 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1945 | s = zbc_num_k(&m1, &m2, &z1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1946 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1947 | s = zbc_num_k(&l1, &l2, &z2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1948 | if (s) goto err; |
| 1949 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1950 | s = zbc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1951 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1952 | s = zbc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1953 | if (s) goto err; |
| 1954 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1955 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1956 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1957 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1958 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1959 | s = zbc_num_add(&z0, &z1, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1960 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1961 | s = zbc_num_add(&temp, &z2, c, 0); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1962 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1963 | bc_num_free(&temp); |
| 1964 | bc_num_free(&z2); |
| 1965 | bc_num_free(&z1); |
| 1966 | bc_num_free(&z0); |
| 1967 | bc_num_free(&m2); |
| 1968 | bc_num_free(&m1); |
| 1969 | bc_num_free(&h2); |
| 1970 | bc_num_free(&l2); |
| 1971 | bc_num_free(&h1); |
| 1972 | bc_num_free(&l1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1973 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1974 | } |
| 1975 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1976 | 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] | 1977 | { |
| 1978 | BcStatus s; |
| 1979 | BcNum cpa, cpb; |
| 1980 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1981 | |
| 1982 | scale = BC_MAX(scale, a->rdx); |
| 1983 | scale = BC_MAX(scale, b->rdx); |
| 1984 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1985 | maxrdx = BC_MAX(maxrdx, scale); |
| 1986 | |
| 1987 | bc_num_init(&cpa, a->len); |
| 1988 | bc_num_init(&cpb, b->len); |
| 1989 | |
| 1990 | bc_num_copy(&cpa, a); |
| 1991 | bc_num_copy(&cpb, b); |
| 1992 | cpa.neg = cpb.neg = false; |
| 1993 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1994 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1995 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1996 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1997 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1998 | s = zbc_num_k(&cpa, &cpb, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1999 | if (s) goto err; |
| 2000 | |
| 2001 | maxrdx += scale; |
| 2002 | bc_num_expand(c, c->len + maxrdx); |
| 2003 | |
| 2004 | if (c->len < maxrdx) { |
| 2005 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 2006 | c->len += maxrdx; |
| 2007 | } |
| 2008 | |
| 2009 | c->rdx = maxrdx; |
| 2010 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2011 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2012 | bc_num_free(&cpb); |
| 2013 | bc_num_free(&cpa); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2014 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2015 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2016 | #define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2017 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2018 | 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] | 2019 | { |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2020 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2021 | size_t len, end, i; |
| 2022 | BcNum cp; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2023 | |
| 2024 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2025 | RETURN_STATUS(bc_error("divide by zero")); |
| 2026 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2027 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2028 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2029 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2030 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2031 | bc_num_copy(c, a); |
| 2032 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2033 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 2037 | bc_num_copy(&cp, a); |
| 2038 | len = b->len; |
| 2039 | |
| 2040 | if (len > cp.len) { |
| 2041 | bc_num_expand(&cp, len + 2); |
| 2042 | bc_num_extend(&cp, len - cp.len); |
| 2043 | } |
| 2044 | |
| 2045 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 2046 | cp.rdx -= b->rdx; |
| 2047 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 2048 | |
| 2049 | if (b->rdx == b->len) { |
Denys Vlasenko | 71c82d1 | 2018-12-18 12:43:21 +0100 | [diff] [blame] | 2050 | for (;;) { |
| 2051 | if (len == 0) break; |
| 2052 | len--; |
| 2053 | if (b->num[len] != 0) |
| 2054 | break; |
| 2055 | } |
| 2056 | len++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 2060 | |
| 2061 | // We want an extra zero in front to make things simpler. |
| 2062 | cp.num[cp.len++] = 0; |
| 2063 | end = cp.len - len; |
| 2064 | |
| 2065 | bc_num_expand(c, cp.len); |
| 2066 | |
| 2067 | bc_num_zero(c); |
| 2068 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2069 | c->rdx = cp.rdx; |
| 2070 | c->len = cp.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2071 | |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2072 | s = BC_STATUS_SUCCESS; |
| 2073 | for (i = end - 1; i < end; --i) { |
| 2074 | BcDig *n, q; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2075 | n = cp.num + i; |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2076 | for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q) |
| 2077 | bc_num_subArrays(n, b->num, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2078 | c->num[i] = q; |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2079 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2080 | // a=2^100000 |
| 2081 | // scale=40000 |
| 2082 | // 1/a <- without check below, this will not be interruptible |
| 2083 | if (G_interrupt) { |
| 2084 | s = BC_STATUS_FAILURE; |
| 2085 | break; |
| 2086 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2087 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2088 | } |
| 2089 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2090 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2091 | bc_num_free(&cp); |
| 2092 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2093 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2094 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2095 | #define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2096 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2097 | 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] | 2098 | BcNum *restrict d, size_t scale, size_t ts) |
| 2099 | { |
| 2100 | BcStatus s; |
| 2101 | BcNum temp; |
| 2102 | bool neg; |
| 2103 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2104 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2105 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2106 | |
| 2107 | if (a->len == 0) { |
| 2108 | bc_num_setToZero(d, ts); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2109 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2113 | s = zbc_num_d(a, b, c, scale); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2114 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2115 | |
| 2116 | if (scale != 0) scale = ts; |
| 2117 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2118 | s = zbc_num_m(c, b, &temp, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2119 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2120 | s = zbc_num_sub(a, &temp, d, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2121 | if (s) goto err; |
| 2122 | |
| 2123 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2124 | |
| 2125 | neg = d->neg; |
| 2126 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2127 | d->neg = neg; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2128 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2129 | bc_num_free(&temp); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2130 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2131 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2132 | #define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2133 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2134 | 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] | 2135 | { |
| 2136 | BcStatus s; |
| 2137 | BcNum c1; |
| 2138 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2139 | |
| 2140 | bc_num_init(&c1, len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2141 | s = zbc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2142 | bc_num_free(&c1); |
| 2143 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2144 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2145 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2146 | #define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2147 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2148 | 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] | 2149 | { |
| 2150 | BcStatus s = BC_STATUS_SUCCESS; |
| 2151 | BcNum copy; |
| 2152 | unsigned long pow; |
| 2153 | size_t i, powrdx, resrdx; |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2154 | bool neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2155 | |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2156 | // GNU bc does not allow 2^2.0 - we do |
| 2157 | for (i = 0; i < b->rdx; i++) |
| 2158 | if (b->num[i] != 0) |
| 2159 | RETURN_STATUS(bc_error("not an integer")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2160 | |
| 2161 | if (b->len == 0) { |
| 2162 | bc_num_one(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2163 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2164 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2165 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2166 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2167 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2168 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2169 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2170 | if (!b->neg) |
| 2171 | bc_num_copy(c, a); |
| 2172 | else |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2173 | s = zbc_num_inv(a, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2174 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | neg = b->neg; |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 2178 | s = zbc_num_ulong_abs(b, &pow); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2179 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 2ea8ddf | 2018-12-22 21:45:18 +0100 | [diff] [blame] | 2180 | // b is not used beyond this point |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2181 | |
| 2182 | bc_num_init(©, a->len); |
| 2183 | bc_num_copy(©, a); |
| 2184 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2185 | if (!neg) { |
| 2186 | if (a->rdx > scale) |
| 2187 | scale = a->rdx; |
| 2188 | if (a->rdx * pow < scale) |
| 2189 | scale = a->rdx * pow; |
| 2190 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2191 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2192 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2193 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2194 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2195 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2196 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2197 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2198 | //if (G_interrupt) { |
| 2199 | // s = BC_STATUS_FAILURE; |
| 2200 | // goto err; |
| 2201 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2202 | } |
| 2203 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2204 | bc_num_copy(c, ©); |
| 2205 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2206 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2207 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2208 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2209 | if (s) goto err; |
| 2210 | |
| 2211 | if (pow & 1) { |
| 2212 | resrdx += powrdx; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2213 | s = zbc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2214 | if (s) goto err; |
| 2215 | } |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2216 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2217 | //if (G_interrupt) { |
| 2218 | // s = BC_STATUS_FAILURE; |
| 2219 | // goto err; |
| 2220 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | if (neg) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2224 | s = zbc_num_inv(c, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2225 | if (s) goto err; |
| 2226 | } |
| 2227 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2228 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2229 | |
| 2230 | // We can't use bc_num_clean() here. |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2231 | for (i = 0; i < c->len; ++i) |
| 2232 | if (c->num[i] != 0) |
| 2233 | goto skip; |
| 2234 | bc_num_setToZero(c, scale); |
| 2235 | skip: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2236 | |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2237 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2238 | bc_num_free(©); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2239 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2240 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2241 | #define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2242 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2243 | 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] | 2244 | { |
| 2245 | BcStatus s; |
| 2246 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2247 | BcDig half_digs[1]; |
Denys Vlasenko | 40f9fe2 | 2020-12-29 16:54:37 +0100 | [diff] [blame] | 2248 | size_t pow, len, digs, digs1, resrdx, req, times; |
| 2249 | ssize_t cmp, cmp1, cmp2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2250 | |
| 2251 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2252 | bc_num_expand(b, req); |
| 2253 | |
| 2254 | if (a->len == 0) { |
| 2255 | bc_num_setToZero(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2256 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2257 | } |
| 2258 | if (a->neg) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2259 | RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2260 | } |
| 2261 | if (BC_NUM_ONE(a)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2262 | bc_num_one(b); |
| 2263 | bc_num_extend(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2264 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2268 | len = a->len + scale; |
| 2269 | |
| 2270 | bc_num_init(&num1, len); |
| 2271 | bc_num_init(&num2, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2272 | |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2273 | half.cap = ARRAY_SIZE(half_digs); |
| 2274 | half.num = half_digs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2275 | bc_num_one(&half); |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2276 | half_digs[0] = 5; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2277 | half.rdx = 1; |
| 2278 | |
| 2279 | bc_num_init(&f, len); |
| 2280 | bc_num_init(&fprime, len); |
| 2281 | |
| 2282 | x0 = &num1; |
| 2283 | x1 = &num2; |
| 2284 | |
| 2285 | bc_num_one(x0); |
| 2286 | pow = BC_NUM_INT(a); |
| 2287 | |
| 2288 | if (pow) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2289 | if (pow & 1) |
| 2290 | x0->num[0] = 2; |
| 2291 | else |
| 2292 | x0->num[0] = 6; |
| 2293 | |
| 2294 | pow -= 2 - (pow & 1); |
| 2295 | |
| 2296 | bc_num_extend(x0, pow); |
| 2297 | |
| 2298 | // Make sure to move the radix back. |
| 2299 | x0->rdx -= pow; |
| 2300 | } |
| 2301 | |
Denys Vlasenko | 40f9fe2 | 2020-12-29 16:54:37 +0100 | [diff] [blame] | 2302 | x0->rdx = digs = digs1 = times = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2303 | resrdx = scale + 2; |
Denys Vlasenko | 40f9fe2 | 2020-12-29 16:54:37 +0100 | [diff] [blame] | 2304 | len = x0->len + resrdx - 1; |
| 2305 | cmp = 1; |
| 2306 | cmp1 = cmp2 = SSIZE_MAX; |
| 2307 | do { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2308 | s = zbc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2309 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2310 | s = zbc_num_add(x0, &f, &fprime, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2311 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2312 | s = zbc_num_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2313 | if (s) goto err; |
| 2314 | |
| 2315 | cmp = bc_num_cmp(x1, x0); |
| 2316 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2317 | |
| 2318 | if (cmp == cmp2 && digs == digs1) |
| 2319 | times += 1; |
| 2320 | else |
| 2321 | times = 0; |
| 2322 | |
| 2323 | resrdx += times > 4; |
| 2324 | |
| 2325 | cmp2 = cmp1; |
| 2326 | cmp1 = cmp; |
| 2327 | digs1 = digs; |
| 2328 | |
| 2329 | temp = x0; |
| 2330 | x0 = x1; |
| 2331 | x1 = temp; |
Denys Vlasenko | 40f9fe2 | 2020-12-29 16:54:37 +0100 | [diff] [blame] | 2332 | } while (cmp != 0 || digs < len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2333 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2334 | bc_num_copy(b, x0); |
| 2335 | scale -= 1; |
Denys Vlasenko | 40f9fe2 | 2020-12-29 16:54:37 +0100 | [diff] [blame] | 2336 | if (b->rdx > scale) |
| 2337 | bc_num_truncate(b, b->rdx - scale); |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2338 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2339 | bc_num_free(&fprime); |
| 2340 | bc_num_free(&f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2341 | bc_num_free(&num2); |
| 2342 | bc_num_free(&num1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2343 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2344 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2345 | #define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2346 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2347 | 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] | 2348 | size_t scale) |
| 2349 | { |
| 2350 | BcStatus s; |
| 2351 | BcNum num2, *ptr_a; |
| 2352 | bool init = false; |
| 2353 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2354 | |
| 2355 | if (c == a) { |
| 2356 | memcpy(&num2, c, sizeof(BcNum)); |
| 2357 | ptr_a = &num2; |
| 2358 | bc_num_init(c, len); |
| 2359 | init = true; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2360 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2361 | ptr_a = a; |
| 2362 | bc_num_expand(c, len); |
| 2363 | } |
| 2364 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2365 | s = zbc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2366 | |
| 2367 | if (init) bc_num_free(&num2); |
| 2368 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2369 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2370 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2371 | #define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2372 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2373 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2374 | 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] | 2375 | { |
| 2376 | BcStatus s; |
| 2377 | BcNum base, exp, two, temp; |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 2378 | BcDig two_digs[1]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2379 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2380 | if (c->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2381 | RETURN_STATUS(bc_error("divide by zero")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2382 | if (a->rdx || b->rdx || c->rdx) |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame] | 2383 | RETURN_STATUS(bc_error("not an integer")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2384 | if (b->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2385 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2386 | |
| 2387 | bc_num_expand(d, c->len); |
| 2388 | bc_num_init(&base, c->len); |
| 2389 | bc_num_init(&exp, b->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2390 | bc_num_init(&temp, b->len); |
| 2391 | |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2392 | two.cap = ARRAY_SIZE(two_digs); |
| 2393 | two.num = two_digs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2394 | bc_num_one(&two); |
Denys Vlasenko | 01eb5e9 | 2018-12-22 23:59:21 +0100 | [diff] [blame] | 2395 | two_digs[0] = 2; |
| 2396 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2397 | bc_num_one(d); |
| 2398 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2399 | s = zbc_num_rem(a, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2400 | if (s) goto err; |
| 2401 | bc_num_copy(&exp, b); |
| 2402 | |
| 2403 | while (exp.len != 0) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2404 | s = zbc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2405 | if (s) goto err; |
| 2406 | |
| 2407 | if (BC_NUM_ONE(&temp)) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2408 | s = zbc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2409 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2410 | s = zbc_num_rem(&temp, c, d, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2411 | if (s) goto err; |
| 2412 | } |
| 2413 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2414 | s = zbc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2415 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2416 | s = zbc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2417 | if (s) goto err; |
| 2418 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2419 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2420 | bc_num_free(&temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2421 | bc_num_free(&exp); |
| 2422 | bc_num_free(&base); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2423 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2424 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2425 | #define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2426 | #endif // ENABLE_DC |
| 2427 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2428 | static FAST_FUNC void bc_string_free(void *string) |
| 2429 | { |
| 2430 | free(*(char**)string); |
| 2431 | } |
| 2432 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2433 | static void bc_func_init(BcFunc *f) |
| 2434 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2435 | bc_char_vec_init(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2436 | IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);) |
| 2437 | IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2438 | IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);) |
| 2439 | IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2440 | IF_BC(f->nparams = 0;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2441 | } |
| 2442 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2443 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2444 | { |
| 2445 | BcFunc *f = (BcFunc *) func; |
| 2446 | bc_vec_free(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2447 | IF_BC(bc_vec_free(&f->labels);) |
| 2448 | IF_BC(bc_vec_free(&f->autos);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2449 | IF_BC(bc_vec_free(&f->strs);) |
| 2450 | IF_BC(bc_vec_free(&f->consts);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2451 | } |
| 2452 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2453 | static void bc_array_expand(BcVec *a, size_t len); |
| 2454 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2455 | static void bc_array_init(BcVec *a, bool nums) |
| 2456 | { |
| 2457 | if (nums) |
| 2458 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2459 | else |
| 2460 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2461 | bc_array_expand(a, 1); |
| 2462 | } |
| 2463 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2464 | static void bc_array_expand(BcVec *a, size_t len) |
| 2465 | { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2466 | if (a->dtor == bc_num_free |
| 2467 | // && a->size == sizeof(BcNum) - always true |
| 2468 | ) { |
| 2469 | BcNum n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2470 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2471 | bc_num_init_DEF_SIZE(&n); |
| 2472 | bc_vec_push(a, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2473 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2474 | } else { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2475 | BcVec v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2476 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2477 | bc_array_init(&v, true); |
| 2478 | bc_vec_push(a, &v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2483 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2484 | { |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2485 | BcNum *dnum, *snum; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2486 | size_t i; |
| 2487 | |
| 2488 | bc_vec_pop_all(d); |
| 2489 | bc_vec_expand(d, s->cap); |
| 2490 | d->len = s->len; |
| 2491 | |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2492 | dnum = (void*)d->v; |
| 2493 | snum = (void*)s->v; |
| 2494 | for (i = 0; i < s->len; i++, dnum++, snum++) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2495 | bc_num_init(dnum, snum->len); |
| 2496 | bc_num_copy(dnum, snum); |
| 2497 | } |
| 2498 | } |
| 2499 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2500 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2501 | static void dc_result_copy(BcResult *d, BcResult *src) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2502 | { |
| 2503 | d->t = src->t; |
| 2504 | |
| 2505 | switch (d->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2506 | case XC_RESULT_TEMP: |
| 2507 | case XC_RESULT_IBASE: |
| 2508 | case XC_RESULT_SCALE: |
| 2509 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2510 | bc_num_init(&d->d.n, src->d.n.len); |
| 2511 | bc_num_copy(&d->d.n, &src->d.n); |
| 2512 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2513 | case XC_RESULT_VAR: |
| 2514 | case XC_RESULT_ARRAY: |
| 2515 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2516 | d->d.id.name = xstrdup(src->d.id.name); |
| 2517 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2518 | case XC_RESULT_CONSTANT: |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2519 | case XC_RESULT_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2520 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2521 | break; |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 2522 | default: // placate compiler |
| 2523 | // BC_RESULT_VOID, BC_RESULT_LAST, BC_RESULT_ONE - do not happen |
| 2524 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2525 | } |
| 2526 | } |
| 2527 | #endif // ENABLE_DC |
| 2528 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2529 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2530 | { |
| 2531 | BcResult *r = (BcResult *) result; |
| 2532 | |
| 2533 | switch (r->t) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2534 | case XC_RESULT_TEMP: |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 2535 | IF_BC(case BC_RESULT_VOID:) |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2536 | case XC_RESULT_IBASE: |
| 2537 | case XC_RESULT_SCALE: |
| 2538 | case XC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2539 | bc_num_free(&r->d.n); |
| 2540 | break; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 2541 | case XC_RESULT_VAR: |
| 2542 | case XC_RESULT_ARRAY: |
| 2543 | case XC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2544 | free(r->d.id.name); |
| 2545 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2546 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2547 | // Do nothing. |
| 2548 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2549 | } |
| 2550 | } |
| 2551 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2552 | static int bad_input_byte(char c) |
| 2553 | { |
| 2554 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 2555 | || c > 0x7e |
| 2556 | ) { |
| 2557 | bc_error_fmt("illegal character 0x%02x", c); |
| 2558 | return 1; |
| 2559 | } |
| 2560 | return 0; |
| 2561 | } |
| 2562 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2563 | static void xc_read_line(BcVec *vec, FILE *fp) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2564 | { |
| 2565 | again: |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2566 | bc_vec_pop_all(vec); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2567 | fflush_and_check(); |
| 2568 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2569 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2570 | if (G_interrupt) { // ^C was pressed |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2571 | if (fp != stdin) { |
| 2572 | // ^C while running a script (bc SCRIPT): die. |
| 2573 | // We do not return to interactive prompt: |
| 2574 | // user might be running us from a shell, |
| 2575 | // and SCRIPT might be intended to terminate |
| 2576 | // (e.g. contain a "halt" stmt). |
| 2577 | // ^C dropping user into a bc prompt instead of |
| 2578 | // the shell would be unexpected. |
| 2579 | xfunc_die(); |
| 2580 | } |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 2581 | // There was ^C while running calculations |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2582 | G_interrupt = 0; |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 2583 | // GNU bc says "interrupted execution." (to stdout, not stderr) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2584 | // GNU dc says "Interrupt!" |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 2585 | puts("\ninterrupted execution"); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | # if ENABLE_FEATURE_EDITING |
| 2589 | if (G_ttyin && fp == stdin) { |
| 2590 | int n, i; |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 2591 | if (!G.line_input_state) |
| 2592 | G.line_input_state = new_line_input_t(DO_HISTORY); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2593 | # define line_buf bb_common_bufsiz1 |
| 2594 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
| 2595 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
Denys Vlasenko | 0197fbf | 2021-01-04 14:41:20 +0100 | [diff] [blame] | 2596 | //GNU bc prints this on ^C: |
| 2597 | //if (n == 0) // ^C |
| 2598 | // puts("(interrupt) Exiting bc."); |
| 2599 | bc_vec_pushZeroByte(vec); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2600 | return; |
| 2601 | } |
| 2602 | i = 0; |
| 2603 | for (;;) { |
| 2604 | char c = line_buf[i++]; |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2605 | if (c == '\0') break; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2606 | if (bad_input_byte(c)) goto again; |
| 2607 | } |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2608 | bc_vec_string(vec, n, line_buf); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2609 | # undef line_buf |
| 2610 | } else |
| 2611 | # endif |
| 2612 | #endif |
| 2613 | { |
| 2614 | int c; |
| 2615 | bool bad_chars = 0; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2616 | |
| 2617 | do { |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2618 | get_char: |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 2619 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2620 | if (G_interrupt) { |
| 2621 | // ^C was pressed: ignore entire line, get another one |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2622 | goto again; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2623 | } |
| 2624 | #endif |
Denys Vlasenko | 51b510a | 2019-01-01 02:19:02 +0100 | [diff] [blame] | 2625 | c = fgetc(fp); |
| 2626 | if (c == '\0') |
| 2627 | goto get_char; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2628 | if (c == EOF) { |
| 2629 | if (ferror(fp)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2630 | bb_simple_perror_msg_and_die("input error"); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2631 | // Note: EOF does not append '\n' |
| 2632 | break; |
| 2633 | } |
| 2634 | bad_chars |= bad_input_byte(c); |
| 2635 | bc_vec_pushByte(vec, (char)c); |
| 2636 | } while (c != '\n'); |
| 2637 | |
| 2638 | if (bad_chars) { |
| 2639 | // Bad chars on this line |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2640 | if (!G.prs.lex_filename) { // stdin |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2641 | // ignore entire line, get another one |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2642 | goto again; |
| 2643 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 2644 | 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] | 2645 | } |
| 2646 | bc_vec_pushZeroByte(vec); |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | // |
| 2651 | // Parsing routines |
| 2652 | // |
| 2653 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2654 | // "Input numbers may contain the characters 0-9 and A-Z. |
| 2655 | // (Note: They must be capitals. Lower case letters are variable names.) |
| 2656 | // Single digit numbers always have the value of the digit regardless of |
| 2657 | // the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes |
| 2658 | // all input digits greater or equal to ibase to the value of ibase-1. |
| 2659 | // This makes the number ZZZ always be the largest 3 digit number of the |
| 2660 | // input base." |
| 2661 | static bool xc_num_strValid(const char *val) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2662 | { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2663 | bool radix = false; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2664 | for (;;) { |
| 2665 | BcDig c = *val++; |
| 2666 | if (c == '\0') |
| 2667 | break; |
| 2668 | if (c == '.') { |
| 2669 | if (radix) return false; |
| 2670 | radix = true; |
| 2671 | continue; |
| 2672 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2673 | if ((c < '0' || c > '9') && (c < 'A' || c > 'Z')) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2674 | return false; |
| 2675 | } |
| 2676 | return true; |
| 2677 | } |
| 2678 | |
| 2679 | // Note: n is already "bc_num_zero()"ed, |
| 2680 | // leading zeroes in "val" are removed |
| 2681 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2682 | { |
| 2683 | size_t len, i; |
| 2684 | const char *ptr; |
| 2685 | |
| 2686 | len = strlen(val); |
| 2687 | if (len == 0) |
| 2688 | return; |
| 2689 | |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2690 | 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] | 2691 | |
| 2692 | ptr = strchr(val, '.'); |
| 2693 | |
| 2694 | n->rdx = 0; |
| 2695 | if (ptr != NULL) |
| 2696 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2697 | |
| 2698 | for (i = 0; val[i]; ++i) { |
| 2699 | if (val[i] != '0' && val[i] != '.') { |
| 2700 | // Not entirely zero value - convert it, and exit |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2701 | if (len == 1) { |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2702 | unsigned c = val[0] - '0'; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2703 | n->len = 1; |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2704 | if (c > 9) { // A-Z => 10-36 |
| 2705 | n->len = 2; |
| 2706 | c -= ('A' - '9' - 1); |
| 2707 | n->num[1] = c/10; |
| 2708 | c = c%10; |
| 2709 | } |
| 2710 | n->num[0] = c; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2711 | break; |
| 2712 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2713 | i = len - 1; |
| 2714 | for (;;) { |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2715 | char c = val[i] - '0'; |
| 2716 | if (c > 9) // A-Z => 9 |
| 2717 | c = 9; |
| 2718 | n->num[n->len] = c; |
| 2719 | n->len++; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2720 | skip_dot: |
| 2721 | if (i == 0) break; |
| 2722 | if (val[--i] == '.') goto skip_dot; |
| 2723 | } |
| 2724 | break; |
| 2725 | } |
| 2726 | } |
| 2727 | // if for() exits without hitting if(), the value is entirely zero |
| 2728 | } |
| 2729 | |
| 2730 | // Note: n is already "bc_num_zero()"ed, |
| 2731 | // leading zeroes in "val" are removed |
| 2732 | static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t) |
| 2733 | { |
| 2734 | BcStatus s; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2735 | BcNum mult, result; |
| 2736 | BcNum temp; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2737 | BcNum base; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2738 | BcDig temp_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2739 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2740 | size_t digits; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2741 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2742 | bc_num_init_DEF_SIZE(&mult); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2743 | |
| 2744 | temp.cap = ARRAY_SIZE(temp_digs); |
| 2745 | temp.num = temp_digs; |
| 2746 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2747 | base.cap = ARRAY_SIZE(base_digs); |
| 2748 | base.num = base_digs; |
| 2749 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2750 | base_t--; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2751 | |
| 2752 | for (;;) { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2753 | unsigned v; |
Denys Vlasenko | 8797adc | 2018-12-31 19:50:06 +0100 | [diff] [blame] | 2754 | char c; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2755 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2756 | c = *val++; |
| 2757 | if (c == '\0') goto int_err; |
| 2758 | if (c == '.') break; |
| 2759 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2760 | v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10); |
| 2761 | if (v > base_t) v = base_t; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2762 | |
| 2763 | s = zbc_num_mul(n, &base, &mult, 0); |
| 2764 | if (s) goto int_err; |
| 2765 | bc_num_ulong2num(&temp, v); |
| 2766 | s = zbc_num_add(&mult, &temp, n, 0); |
| 2767 | if (s) goto int_err; |
| 2768 | } |
| 2769 | |
| 2770 | bc_num_init(&result, base.len); |
| 2771 | //bc_num_zero(&result); - already is |
| 2772 | bc_num_one(&mult); |
| 2773 | |
| 2774 | digits = 0; |
| 2775 | for (;;) { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2776 | unsigned v; |
Denys Vlasenko | 8797adc | 2018-12-31 19:50:06 +0100 | [diff] [blame] | 2777 | char c; |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2778 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2779 | c = *val++; |
| 2780 | if (c == '\0') break; |
| 2781 | digits++; |
| 2782 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2783 | v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10); |
| 2784 | if (v > base_t) v = base_t; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2785 | |
| 2786 | s = zbc_num_mul(&result, &base, &result, 0); |
| 2787 | if (s) goto err; |
| 2788 | bc_num_ulong2num(&temp, v); |
| 2789 | s = zbc_num_add(&result, &temp, &result, 0); |
| 2790 | if (s) goto err; |
| 2791 | s = zbc_num_mul(&mult, &base, &mult, 0); |
| 2792 | if (s) goto err; |
| 2793 | } |
| 2794 | |
| 2795 | s = zbc_num_div(&result, &mult, &result, digits); |
| 2796 | if (s) goto err; |
| 2797 | s = zbc_num_add(n, &result, n, digits); |
| 2798 | if (s) goto err; |
| 2799 | |
| 2800 | if (n->len != 0) { |
| 2801 | if (n->rdx < digits) |
| 2802 | bc_num_extend(n, digits - n->rdx); |
| 2803 | } else |
| 2804 | bc_num_zero(n); |
| 2805 | err: |
| 2806 | bc_num_free(&result); |
| 2807 | int_err: |
| 2808 | bc_num_free(&mult); |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2809 | } |
| 2810 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2811 | 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] | 2812 | { |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2813 | size_t i; |
| 2814 | |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2815 | if (!xc_num_strValid(val)) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2816 | RETURN_STATUS(bc_error("bad number string")); |
| 2817 | |
| 2818 | bc_num_zero(n); |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2819 | while (*val == '0') |
| 2820 | val++; |
| 2821 | for (i = 0; ; ++i) { |
| 2822 | if (val[i] == '\0') |
| 2823 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2824 | if (val[i] != '.' && val[i] != '0') |
| 2825 | break; |
| 2826 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2827 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 2828 | if (base_t == 10 || val[1] == '\0') |
| 2829 | // Decimal, or single-digit number |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2830 | bc_num_parseDecimal(n, val); |
| 2831 | else |
| 2832 | bc_num_parseBase(n, val, base_t); |
| 2833 | |
| 2834 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2835 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2836 | #define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 2837 | |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2838 | // p->lex_inbuf points to the current string to be parsed. |
| 2839 | // if p->lex_inbuf points to '\0', it's either EOF or it points after |
| 2840 | // last processed line's terminating '\n' (and more reading needs to be done |
| 2841 | // to get next character). |
| 2842 | // |
| 2843 | // If you are in a situation where that is a possibility, call peek_inbuf(). |
| 2844 | // If necessary, it performs more reading and changes p->lex_inbuf, |
| 2845 | // then it returns *p->lex_inbuf (which will be '\0' only if it's EOF). |
| 2846 | // After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0', |
| 2847 | // it's ok to do p->lex_inbuf++ once without end-of-buffer checking. |
| 2848 | // |
| 2849 | // eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++": |
| 2850 | // it returns current char and advances the pointer (if not EOF). |
| 2851 | // After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid. |
| 2852 | // |
| 2853 | // In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf(): |
| 2854 | // unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY |
| 2855 | // on real EOF, not end-of-buffer. |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 2856 | // |
| 2857 | // bc cases to test interactively: |
| 2858 | // 1 #comment\ - prints "1<newline>" at once (comment is not continued) |
| 2859 | // 1 #comment/* - prints "1<newline>" at once |
| 2860 | // 1 #comment" - prints "1<newline>" at once |
| 2861 | // 1\#comment - error at once (\ is not a line continuation) |
| 2862 | // 1 + /*"*/2 - prints "3<newline>" at once |
| 2863 | // 1 + /*#*/2 - prints "3<newline>" at once |
| 2864 | // "str\" - prints "str\" at once |
| 2865 | // "str#" - prints "str#" at once |
| 2866 | // "str/*" - prints "str/*" at once |
| 2867 | // "str#\ - waits for second line |
| 2868 | // end" - ...prints "str#\<newline>end" |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2869 | static char peek_inbuf(void) |
| 2870 | { |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 2871 | if (*G.prs.lex_inbuf == '\0' |
| 2872 | && G.prs.lex_input_fp |
| 2873 | ) { |
| 2874 | xc_read_line(&G.input_buffer, G.prs.lex_input_fp); |
| 2875 | G.prs.lex_inbuf = G.input_buffer.v; |
| 2876 | if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte) |
| 2877 | G.prs.lex_input_fp = NULL; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2878 | } |
| 2879 | return *G.prs.lex_inbuf; |
| 2880 | } |
| 2881 | static char eat_inbuf(void) |
| 2882 | { |
| 2883 | char c = peek_inbuf(); |
| 2884 | if (c) G.prs.lex_inbuf++; |
| 2885 | return c; |
| 2886 | } |
| 2887 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2888 | static void xc_lex_lineComment(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2889 | { |
| 2890 | BcParse *p = &G.prs; |
| 2891 | char c; |
| 2892 | |
| 2893 | // Try: echo -n '#foo' | bc |
| 2894 | p->lex = XC_LEX_WHITESPACE; |
| 2895 | |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 2896 | // Not peek_inbuf(): we depend on input being done in whole lines: |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2897 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2898 | while ((c = *p->lex_inbuf) != '\n' && c != '\0') |
| 2899 | p->lex_inbuf++; |
| 2900 | } |
| 2901 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2902 | static void xc_lex_whitespace(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2903 | { |
| 2904 | BcParse *p = &G.prs; |
| 2905 | |
| 2906 | p->lex = XC_LEX_WHITESPACE; |
| 2907 | for (;;) { |
| 2908 | // We depend here on input being done in whole lines: |
| 2909 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2910 | char c = *p->lex_inbuf; |
| 2911 | if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
| 2912 | break; |
| 2913 | if (!isspace(c)) |
| 2914 | break; |
| 2915 | p->lex_inbuf++; |
| 2916 | } |
| 2917 | } |
| 2918 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2919 | static BC_STATUS zxc_lex_number(char last) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2920 | { |
| 2921 | BcParse *p = &G.prs; |
| 2922 | bool pt; |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2923 | char last_valid_ch; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2924 | |
| 2925 | bc_vec_pop_all(&p->lex_strnumbuf); |
| 2926 | bc_vec_pushByte(&p->lex_strnumbuf, last); |
| 2927 | |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2928 | // bc: "Input numbers may contain the characters 0-9 and A-Z. |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2929 | // (Note: They must be capitals. Lower case letters are variable names.) |
| 2930 | // Single digit numbers always have the value of the digit regardless of |
| 2931 | // the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes |
| 2932 | // all input digits greater or equal to ibase to the value of ibase-1. |
| 2933 | // This makes the number ZZZ always be the largest 3 digit number of the |
| 2934 | // input base." |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2935 | // dc only allows A-F, the rules about single-char and multi-char are the same. |
| 2936 | last_valid_ch = (IS_BC ? 'Z' : 'F'); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2937 | pt = (last == '.'); |
| 2938 | p->lex = XC_LEX_NUMBER; |
| 2939 | for (;;) { |
| 2940 | // We depend here on input being done in whole lines: |
| 2941 | // '\0' which isn't the EOF can only be seen after '\n'. |
| 2942 | char c = *p->lex_inbuf; |
| 2943 | check_c: |
| 2944 | if (c == '\0') |
| 2945 | break; |
| 2946 | if (c == '\\' && p->lex_inbuf[1] == '\n') { |
| 2947 | p->lex_inbuf += 2; |
| 2948 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 2949 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2950 | c = peek_inbuf(); // force next line to be read |
| 2951 | goto check_c; |
| 2952 | } |
Denys Vlasenko | d5b0fa6 | 2018-12-29 02:40:03 +0100 | [diff] [blame] | 2953 | if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2954 | if (c != '.') break; |
| 2955 | // if '.' was already seen, stop on second one: |
| 2956 | if (pt) break; |
| 2957 | pt = true; |
| 2958 | } |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 2959 | // c is one of "0-9A-Z." |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2960 | last = c; |
| 2961 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 2962 | p->lex_inbuf++; |
| 2963 | } |
| 2964 | if (last == '.') // remove trailing '.' if any |
| 2965 | bc_vec_pop(&p->lex_strnumbuf); |
| 2966 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 2967 | |
| 2968 | G.err_line = G.prs.lex_line; |
| 2969 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2970 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2971 | #define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2972 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 2973 | static void xc_lex_name(void) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 2974 | { |
| 2975 | BcParse *p = &G.prs; |
| 2976 | size_t i; |
| 2977 | const char *buf; |
| 2978 | |
| 2979 | p->lex = XC_LEX_NAME; |
| 2980 | |
| 2981 | // Since names can't cross lines with \<newline>, |
| 2982 | // we depend on the fact that whole line is in the buffer |
| 2983 | i = 0; |
| 2984 | buf = p->lex_inbuf - 1; |
| 2985 | for (;;) { |
| 2986 | char c = buf[i]; |
| 2987 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 2988 | i++; |
| 2989 | } |
| 2990 | |
| 2991 | #if 0 // We do not protect against people with gigabyte-long names |
| 2992 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2993 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2994 | if (i > BC_MAX_STRING) |
| 2995 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2996 | } |
| 2997 | #endif |
| 2998 | bc_vec_string(&p->lex_strnumbuf, i, buf); |
| 2999 | |
| 3000 | // Increment the index. We minus 1 because it has already been incremented. |
| 3001 | p->lex_inbuf += i - 1; |
| 3002 | |
| 3003 | //return BC_STATUS_SUCCESS; |
| 3004 | } |
| 3005 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3006 | IF_BC(static BC_STATUS zbc_lex_token(void);) |
| 3007 | IF_DC(static BC_STATUS zdc_lex_token(void);) |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3008 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
| 3009 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3010 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3011 | static BC_STATUS zxc_lex_next(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3012 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3013 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3014 | BcStatus s; |
| 3015 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3016 | G.err_line = p->lex_line; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3017 | p->lex_last = p->lex; |
Denys Vlasenko | 2f7352b | 2018-12-26 18:59:42 +0100 | [diff] [blame] | 3018 | //why? |
| 3019 | // if (p->lex_last == XC_LEX_EOF) |
| 3020 | // RETURN_STATUS(bc_error("end of file")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3021 | |
| 3022 | // Loop until failure or we don't have whitespace. This |
| 3023 | // is so the parser doesn't get inundated with whitespace. |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 3024 | // Comments are also XC_LEX_WHITESPACE tokens and eaten here. |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3025 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3026 | do { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3027 | if (*p->lex_inbuf == '\0') { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3028 | p->lex = XC_LEX_EOF; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3029 | if (peek_inbuf() == '\0') |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 3030 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 3031 | } |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3032 | p->lex_next_at = p->lex_inbuf; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3033 | dbg_lex("next string to parse:'%.*s'", |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3034 | (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at), |
| 3035 | p->lex_next_at |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 3036 | ); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3037 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3038 | IF_BC(s = zbc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3039 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3040 | IF_DC(s = zdc_lex_token()); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3041 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3042 | } while (!s && p->lex == XC_LEX_WHITESPACE); |
| 3043 | dbg_lex("p->lex from string:%d", p->lex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3044 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3045 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3046 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3047 | #define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3048 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3049 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3050 | static BC_STATUS zbc_lex_skip_if_at_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3051 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3052 | if (G.prs.lex == XC_LEX_NLINE) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3053 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3054 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 3055 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3056 | #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] | 3057 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3058 | static BC_STATUS zbc_lex_next_and_skip_NLINE(void) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3059 | { |
| 3060 | BcStatus s; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3061 | s = zxc_lex_next(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3062 | if (s) RETURN_STATUS(s); |
| 3063 | // if(cond)<newline>stmt is accepted too (but not 2+ newlines) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3064 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 3065 | RETURN_STATUS(s); |
| 3066 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3067 | #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] | 3068 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3069 | static BC_STATUS zbc_lex_identifier(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3070 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3071 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3072 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3073 | unsigned i; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3074 | const char *buf = p->lex_inbuf - 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3075 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3076 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 3077 | const char *keyword8 = bc_lex_kws[i].name8; |
| 3078 | unsigned j = 0; |
| 3079 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 3080 | j++; |
| 3081 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3082 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3083 | if (keyword8[j] != '\0') |
| 3084 | continue; |
| 3085 | match: |
| 3086 | // buf starts with keyword bc_lex_kws[i] |
Denys Vlasenko | 5acd14b | 2018-12-20 16:48:50 +0100 | [diff] [blame] | 3087 | if (isalnum(buf[j]) || buf[j]=='_') |
| 3088 | continue; // "ifz" does not match "if" keyword, "if." does |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3089 | p->lex = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | 0b0e8d0 | 2018-12-25 21:44:10 +0100 | [diff] [blame] | 3090 | if (!keyword_is_POSIX(i)) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3091 | s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8); |
| 3092 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | // We minus 1 because the index has already been incremented. |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3096 | p->lex_inbuf += j - 1; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3097 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3098 | } |
| 3099 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3100 | xc_lex_name(); |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 3101 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3102 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3103 | if (p->lex_strnumbuf.len > 2) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3104 | // Prevent this: |
| 3105 | // >>> qwe=1 |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3106 | // bc: POSIX only allows one character names; this is bad: 'qwe=1 |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3107 | // ' |
| 3108 | unsigned len = strchrnul(buf, '\n') - buf; |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3109 | 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] | 3110 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3111 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3112 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3113 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3114 | #define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3115 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3116 | static BC_STATUS zbc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3117 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3118 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3119 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3120 | p->lex = XC_LEX_STR; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3121 | bc_vec_pop_all(&p->lex_strnumbuf); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3122 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3123 | char c = peek_inbuf(); // strings can cross lines |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3124 | if (c == '\0') { |
Denys Vlasenko | 8af1108 | 2018-12-26 21:01:41 +0100 | [diff] [blame] | 3125 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3126 | } |
| 3127 | if (c == '"') |
| 3128 | break; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3129 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3130 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3131 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3132 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3133 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 3134 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3135 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3136 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 3137 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3138 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3139 | G.err_line = p->lex_line; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3140 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3141 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3142 | #define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3143 | |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3144 | static void parse_lex_by_checking_eq_sign(unsigned with_and_without) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3145 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3146 | BcParse *p = &G.prs; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3147 | if (*p->lex_inbuf == '=') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3148 | // ^^^ not using peek_inbuf() since '==' etc can't be split across lines |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3149 | p->lex_inbuf++; |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3150 | with_and_without >>= 8; // store "with" value |
| 3151 | } // else store "without" value |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3152 | p->lex = (with_and_without & 0xff); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3153 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3154 | #define parse_lex_by_checking_eq_sign(with, without) \ |
| 3155 | parse_lex_by_checking_eq_sign(((with)<<8)|(without)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3156 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3157 | static BC_STATUS zbc_lex_comment(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3158 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3159 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3160 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3161 | p->lex = XC_LEX_WHITESPACE; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3162 | // here lex_inbuf is at '*' of opening comment delimiter |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3163 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3164 | char c; |
| 3165 | |
| 3166 | p->lex_inbuf++; |
| 3167 | c = peek_inbuf(); |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3168 | check_star: |
| 3169 | if (c == '*') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3170 | p->lex_inbuf++; |
Denys Vlasenko | 8af1108 | 2018-12-26 21:01:41 +0100 | [diff] [blame] | 3171 | c = *p->lex_inbuf; // no need to peek_inbuf() |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3172 | if (c == '/') |
| 3173 | break; |
| 3174 | goto check_star; |
| 3175 | } |
| 3176 | if (c == '\0') { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 3177 | RETURN_STATUS(bc_error("unterminated comment")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3178 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3179 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3180 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3181 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3182 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3183 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3184 | p->lex_inbuf++; // skip trailing '/' |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3185 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3186 | G.err_line = p->lex_line; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3187 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3188 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3189 | #define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3190 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3191 | #undef zbc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3192 | static BC_STATUS zbc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3193 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3194 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3195 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3196 | char c = eat_inbuf(); |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3197 | char c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3198 | |
| 3199 | // This is the workhorse of the lexer. |
| 3200 | switch (c) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3201 | // case '\0': // probably never reached |
| 3202 | // p->lex_inbuf--; |
| 3203 | // p->lex = XC_LEX_EOF; |
| 3204 | // break; |
| 3205 | case '\n': |
| 3206 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3207 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3208 | p->lex = XC_LEX_NLINE; |
| 3209 | break; |
| 3210 | case '\t': |
| 3211 | case '\v': |
| 3212 | case '\f': |
| 3213 | case '\r': |
| 3214 | case ' ': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3215 | xc_lex_whitespace(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3216 | break; |
| 3217 | case '!': |
| 3218 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3219 | if (p->lex == BC_LEX_OP_BOOL_NOT) { |
| 3220 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 3221 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3222 | } |
| 3223 | break; |
| 3224 | case '"': |
| 3225 | s = zbc_lex_string(); |
| 3226 | break; |
| 3227 | case '#': |
| 3228 | s = zbc_POSIX_does_not_allow("'#' script comments"); |
| 3229 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3230 | xc_lex_lineComment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3231 | break; |
| 3232 | case '%': |
| 3233 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS); |
| 3234 | break; |
| 3235 | case '&': |
| 3236 | c2 = *p->lex_inbuf; |
| 3237 | if (c2 == '&') { |
| 3238 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
| 3239 | if (s) RETURN_STATUS(s); |
| 3240 | p->lex_inbuf++; |
| 3241 | p->lex = BC_LEX_OP_BOOL_AND; |
| 3242 | } else { |
| 3243 | p->lex = XC_LEX_INVALID; |
| 3244 | s = bc_error_bad_character('&'); |
| 3245 | } |
| 3246 | break; |
| 3247 | case '(': |
| 3248 | case ')': |
| 3249 | p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3250 | break; |
| 3251 | case '*': |
| 3252 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY); |
| 3253 | break; |
| 3254 | case '+': |
| 3255 | c2 = *p->lex_inbuf; |
| 3256 | if (c2 == '+') { |
| 3257 | p->lex_inbuf++; |
| 3258 | p->lex = BC_LEX_OP_INC; |
| 3259 | } else |
| 3260 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS); |
| 3261 | break; |
| 3262 | case ',': |
| 3263 | p->lex = BC_LEX_COMMA; |
| 3264 | break; |
| 3265 | case '-': |
| 3266 | c2 = *p->lex_inbuf; |
| 3267 | if (c2 == '-') { |
| 3268 | p->lex_inbuf++; |
| 3269 | p->lex = BC_LEX_OP_DEC; |
| 3270 | } else |
| 3271 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS); |
| 3272 | break; |
| 3273 | case '.': |
| 3274 | if (isdigit(*p->lex_inbuf)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3275 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3276 | else { |
| 3277 | p->lex = BC_LEX_KEY_LAST; |
| 3278 | s = zbc_POSIX_does_not_allow("'.' as 'last'"); |
| 3279 | } |
| 3280 | break; |
| 3281 | case '/': |
| 3282 | c2 = *p->lex_inbuf; |
| 3283 | if (c2 == '*') |
| 3284 | s = zbc_lex_comment(); |
| 3285 | else |
| 3286 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE); |
| 3287 | break; |
| 3288 | case '0': |
| 3289 | case '1': |
| 3290 | case '2': |
| 3291 | case '3': |
| 3292 | case '4': |
| 3293 | case '5': |
| 3294 | case '6': |
| 3295 | case '7': |
| 3296 | case '8': |
| 3297 | case '9': |
| 3298 | case 'A': |
| 3299 | case 'B': |
| 3300 | case 'C': |
| 3301 | case 'D': |
| 3302 | case 'E': |
| 3303 | case 'F': |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3304 | case 'G': |
| 3305 | case 'H': |
| 3306 | case 'I': |
| 3307 | case 'J': |
| 3308 | case 'K': |
| 3309 | case 'L': |
| 3310 | case 'M': |
| 3311 | case 'N': |
| 3312 | case 'O': |
| 3313 | case 'P': |
| 3314 | case 'Q': |
| 3315 | case 'R': |
| 3316 | case 'S': |
| 3317 | case 'T': |
| 3318 | case 'U': |
| 3319 | case 'V': |
| 3320 | case 'W': |
| 3321 | case 'X': |
| 3322 | case 'Y': |
| 3323 | case 'Z': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3324 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3325 | break; |
| 3326 | case ';': |
| 3327 | p->lex = BC_LEX_SCOLON; |
| 3328 | break; |
| 3329 | case '<': |
| 3330 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT); |
| 3331 | break; |
| 3332 | case '=': |
| 3333 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3334 | break; |
| 3335 | case '>': |
| 3336 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT); |
| 3337 | break; |
| 3338 | case '[': |
| 3339 | case ']': |
| 3340 | p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3341 | break; |
| 3342 | case '\\': |
| 3343 | if (*p->lex_inbuf == '\n') { |
| 3344 | p->lex = XC_LEX_WHITESPACE; |
| 3345 | p->lex_inbuf++; |
| 3346 | } else |
| 3347 | s = bc_error_bad_character(c); |
| 3348 | break; |
| 3349 | case '^': |
| 3350 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER); |
| 3351 | break; |
| 3352 | case 'a': |
| 3353 | case 'b': |
| 3354 | case 'c': |
| 3355 | case 'd': |
| 3356 | case 'e': |
| 3357 | case 'f': |
| 3358 | case 'g': |
| 3359 | case 'h': |
| 3360 | case 'i': |
| 3361 | case 'j': |
| 3362 | case 'k': |
| 3363 | case 'l': |
| 3364 | case 'm': |
| 3365 | case 'n': |
| 3366 | case 'o': |
| 3367 | case 'p': |
| 3368 | case 'q': |
| 3369 | case 'r': |
| 3370 | case 's': |
| 3371 | case 't': |
| 3372 | case 'u': |
| 3373 | case 'v': |
| 3374 | case 'w': |
| 3375 | case 'x': |
| 3376 | case 'y': |
| 3377 | case 'z': |
| 3378 | s = zbc_lex_identifier(); |
| 3379 | break; |
| 3380 | case '{': |
| 3381 | case '}': |
| 3382 | p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3383 | break; |
| 3384 | case '|': |
| 3385 | c2 = *p->lex_inbuf; |
| 3386 | if (c2 == '|') { |
| 3387 | s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
| 3388 | if (s) RETURN_STATUS(s); |
| 3389 | p->lex_inbuf++; |
| 3390 | p->lex = BC_LEX_OP_BOOL_OR; |
| 3391 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3392 | p->lex = XC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3393 | s = bc_error_bad_character(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3394 | } |
| 3395 | break; |
| 3396 | default: |
| 3397 | p->lex = XC_LEX_INVALID; |
| 3398 | s = bc_error_bad_character(c); |
| 3399 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3400 | } |
| 3401 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3402 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3403 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3404 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3405 | #endif // ENABLE_BC |
| 3406 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3407 | #if ENABLE_DC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3408 | static BC_STATUS zdc_lex_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3409 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3410 | BcParse *p = &G.prs; |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3411 | if (G_exreg && isspace(*p->lex_inbuf)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3412 | xc_lex_whitespace(); // eats whitespace (but not newline) |
| 3413 | p->lex_inbuf++; // xc_lex_name() expects this |
| 3414 | xc_lex_name(); |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3415 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3416 | bc_vec_pop_all(&p->lex_strnumbuf); |
Denys Vlasenko | c192b04 | 2018-12-25 23:15:59 +0100 | [diff] [blame] | 3417 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3418 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
| 3419 | p->lex = XC_LEX_NAME; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3420 | } |
| 3421 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3422 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3423 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3424 | #define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3425 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3426 | static BC_STATUS zdc_lex_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3427 | { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3428 | BcParse *p = &G.prs; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3429 | size_t depth; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3430 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3431 | p->lex = XC_LEX_STR; |
| 3432 | bc_vec_pop_all(&p->lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3433 | |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3434 | depth = 1; |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3435 | for (;;) { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3436 | char c = peek_inbuf(); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3437 | if (c == '\0') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3438 | RETURN_STATUS(bc_error("unterminated string")); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3439 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3440 | if (c == '[') depth++; |
| 3441 | if (c == ']') |
| 3442 | if (--depth == 0) |
| 3443 | break; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3444 | if (c == '\n') { |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3445 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3446 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
| 3447 | } |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3448 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf); |
| 3449 | p->lex_inbuf++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3450 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3451 | bc_vec_pushZeroByte(&p->lex_strnumbuf); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3452 | p->lex_inbuf++; // skip trailing ']' |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3453 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3454 | G.err_line = p->lex_line; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3455 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3456 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3457 | #define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3458 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3459 | #undef zdc_lex_token |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3460 | static BC_STATUS zdc_lex_token(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3461 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3462 | static const //BcLexType - should be this type, but narrower type saves size: |
| 3463 | uint8_t |
Denys Vlasenko | 2beb1f6 | 2018-12-26 21:17:12 +0100 | [diff] [blame] | 3464 | dc_lex_regs[] ALIGN1 = { |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3465 | 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] | 3466 | XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON, |
| 3467 | 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] | 3468 | DC_LEX_STORE_PUSH, |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3469 | }; |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3470 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3471 | BcParse *p = &G.prs; |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3472 | BcStatus s; |
| 3473 | char c, c2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3474 | size_t i; |
| 3475 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3476 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3477 | if (p->lex_last == dc_lex_regs[i]) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3478 | RETURN_STATUS(zdc_lex_register()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3479 | } |
| 3480 | |
Denys Vlasenko | 81293c8 | 2018-12-24 01:53:55 +0100 | [diff] [blame] | 3481 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3482 | c = eat_inbuf(); |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3483 | if (c >= '%' && c <= '~' |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3484 | && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3485 | ) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3486 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | // This is the workhorse of the lexer. |
| 3490 | switch (c) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3491 | // case '\0': // probably never reached |
| 3492 | // p->lex = XC_LEX_EOF; |
| 3493 | // break; |
| 3494 | case '\n': |
| 3495 | // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE |
| 3496 | // (and "case '\n':" is not just empty here) |
| 3497 | // only to allow interactive dc have a way to exit |
| 3498 | // "parse" stage of "parse,execute" loop |
| 3499 | // on <enter>, not on _next_ token (which would mean |
| 3500 | // commands are not executed on pressing <enter>). |
| 3501 | // IOW: typing "1p<enter>" should print "1" _at once_, |
| 3502 | // not after some more input. |
| 3503 | p->lex_line++; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 3504 | dbg_lex("++p->lex_line=%zd", p->lex_line); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3505 | p->lex = XC_LEX_NLINE; |
| 3506 | break; |
| 3507 | case '\t': |
| 3508 | case '\v': |
| 3509 | case '\f': |
| 3510 | case '\r': |
| 3511 | case ' ': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3512 | xc_lex_whitespace(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3513 | break; |
| 3514 | case '!': |
| 3515 | c2 = *p->lex_inbuf; |
| 3516 | if (c2 == '=') |
| 3517 | p->lex = XC_LEX_OP_REL_NE; |
| 3518 | else if (c2 == '<') |
| 3519 | p->lex = XC_LEX_OP_REL_LE; |
| 3520 | else if (c2 == '>') |
| 3521 | p->lex = XC_LEX_OP_REL_GE; |
| 3522 | else |
| 3523 | RETURN_STATUS(bc_error_bad_character(c)); |
| 3524 | p->lex_inbuf++; |
| 3525 | break; |
| 3526 | case '#': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3527 | xc_lex_lineComment(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3528 | break; |
| 3529 | case '.': |
| 3530 | if (isdigit(*p->lex_inbuf)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3531 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3532 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3533 | s = bc_error_bad_character(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3534 | break; |
| 3535 | case '0': |
| 3536 | case '1': |
| 3537 | case '2': |
| 3538 | case '3': |
| 3539 | case '4': |
| 3540 | case '5': |
| 3541 | case '6': |
| 3542 | case '7': |
| 3543 | case '8': |
| 3544 | case '9': |
| 3545 | case 'A': |
| 3546 | case 'B': |
| 3547 | case 'C': |
| 3548 | case 'D': |
| 3549 | case 'E': |
| 3550 | case 'F': |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3551 | s = zxc_lex_number(c); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3552 | break; |
| 3553 | case '[': |
| 3554 | s = zdc_lex_string(); |
| 3555 | break; |
| 3556 | default: |
| 3557 | p->lex = XC_LEX_INVALID; |
| 3558 | s = bc_error_bad_character(c); |
| 3559 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3560 | } |
| 3561 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3562 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3563 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3564 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3565 | #endif // ENABLE_DC |
| 3566 | |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3567 | static void xc_parse_push(unsigned i) |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3568 | { |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3569 | BcVec *code = &G.prs.func->code; |
| 3570 | dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i); |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3571 | bc_vec_pushByte(code, (uint8_t)i); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3572 | } |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3573 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3574 | static void xc_parse_pushName(char *name) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3575 | { |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3576 | #if 1 |
| 3577 | BcVec *code = &G.prs.func->code; |
| 3578 | size_t pos = code->len; |
| 3579 | size_t len = strlen(name) + 1; |
| 3580 | |
| 3581 | bc_vec_expand(code, pos + len); |
| 3582 | strcpy(code->v + pos, name); |
| 3583 | code->len = pos + len; |
| 3584 | #else |
| 3585 | // Smaller code, but way slow: |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3586 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3587 | xc_parse_push(*name); |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 3588 | } while (*name++); |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3589 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3590 | } |
| 3591 | |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3592 | // Indexes < 0xfc are encoded verbatim, else first byte is |
| 3593 | // 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes", |
| 3594 | // followed by that many bytes, lsb first. |
| 3595 | // (The above describes 32-bit case). |
| 3596 | #define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t)) |
| 3597 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 3598 | static void bc_vec_pushIndex(BcVec *v, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3599 | { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3600 | size_t mask; |
| 3601 | unsigned amt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3602 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 3603 | dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx); |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3604 | if (idx < SMALL_INDEX_LIMIT) { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 3605 | bc_vec_pushByte(v, idx); |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 3606 | return; |
Denys Vlasenko | ab9a986 | 2018-12-26 20:30:47 +0100 | [diff] [blame] | 3607 | } |
| 3608 | |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3609 | mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8); |
| 3610 | amt = sizeof(idx); |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3611 | for (;;) { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3612 | if (idx & mask) break; |
| 3613 | mask >>= 8; |
| 3614 | amt--; |
Denys Vlasenko | e16a522 | 2018-12-29 02:24:19 +0100 | [diff] [blame] | 3615 | } |
| 3616 | // amt is at least 1 here - "one byte of length data follows" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3617 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 3618 | bc_vec_pushByte(v, (SMALL_INDEX_LIMIT - 1) + amt); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3619 | |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 3620 | do { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 3621 | bc_vec_pushByte(v, (unsigned char)idx); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3622 | idx >>= 8; |
Denys Vlasenko | ae6c44e | 2019-01-02 16:30:24 +0100 | [diff] [blame] | 3623 | } while (idx != 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3624 | } |
| 3625 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 3626 | static void xc_parse_pushIndex(size_t idx) |
| 3627 | { |
| 3628 | bc_vec_pushIndex(&G.prs.func->code, idx); |
| 3629 | } |
| 3630 | |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3631 | static void xc_parse_pushInst_and_Index(unsigned inst, size_t idx) |
| 3632 | { |
| 3633 | xc_parse_push(inst); |
| 3634 | xc_parse_pushIndex(idx); |
| 3635 | } |
| 3636 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3637 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3638 | static void bc_parse_pushJUMP(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3639 | { |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3640 | xc_parse_pushInst_and_Index(BC_INST_JUMP, idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3641 | } |
| 3642 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3643 | static void bc_parse_pushJUMP_ZERO(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3644 | { |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3645 | xc_parse_pushInst_and_Index(BC_INST_JUMP_ZERO, idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3646 | } |
| 3647 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3648 | static BC_STATUS zbc_parse_pushSTR(void) |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3649 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3650 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3651 | char *str = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3652 | |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3653 | xc_parse_pushInst_and_Index(XC_INST_STR, p->func->strs.len); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3654 | bc_vec_push(&p->func->strs, &str); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3655 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3656 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3657 | } |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 3658 | #define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3659 | #endif |
| 3660 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3661 | static void xc_parse_pushNUM(void) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3662 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3663 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3664 | char *num = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3665 | #if ENABLE_BC && ENABLE_DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3666 | 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] | 3667 | #elif ENABLE_BC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3668 | size_t idx = bc_vec_push(&p->func->consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3669 | #else // DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3670 | size_t idx = bc_vec_push(&G.prog.consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3671 | #endif |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3672 | xc_parse_pushInst_and_Index(XC_INST_NUM, idx); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3673 | } |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3674 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3675 | static BC_STATUS zxc_parse_text_init(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3676 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3677 | G.prs.func = xc_program_func(G.prs.fidx); |
| 3678 | G.prs.lex_inbuf = text; |
| 3679 | G.prs.lex = G.prs.lex_last = XC_LEX_INVALID; |
| 3680 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3681 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3682 | #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] | 3683 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3684 | // Called when parsing or execution detects a failure, |
| 3685 | // resets execution structures. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3686 | static void xc_program_reset(void) |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3687 | { |
| 3688 | BcFunc *f; |
| 3689 | BcInstPtr *ip; |
| 3690 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3691 | bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3692 | bc_vec_pop_all(&G.prog.results); |
| 3693 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3694 | f = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3695 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 3696 | ip->inst_idx = f->code.len; |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3697 | } |
| 3698 | |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3699 | // Called when parsing code detects a failure, |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3700 | // resets parsing structures. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3701 | static void xc_parse_reset(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3702 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3703 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3704 | if (p->fidx != BC_PROG_MAIN) { |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3705 | bc_func_free(p->func); |
| 3706 | bc_func_init(p->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3707 | |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 3708 | p->fidx = BC_PROG_MAIN; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3709 | p->func = xc_program_func_BC_PROG_MAIN(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3710 | } |
| 3711 | |
Denys Vlasenko | b1b7996 | 2018-12-26 18:46:03 +0100 | [diff] [blame] | 3712 | p->lex_inbuf += strlen(p->lex_inbuf); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3713 | p->lex = XC_LEX_EOF; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3714 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3715 | IF_BC(bc_vec_pop_all(&p->exits);) |
| 3716 | IF_BC(bc_vec_pop_all(&p->conds);) |
| 3717 | IF_BC(bc_vec_pop_all(&p->ops);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3718 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3719 | xc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3720 | } |
| 3721 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3722 | static void xc_parse_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3723 | { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3724 | IF_BC(bc_vec_free(&G.prs.exits);) |
| 3725 | IF_BC(bc_vec_free(&G.prs.conds);) |
| 3726 | IF_BC(bc_vec_free(&G.prs.ops);) |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 3727 | bc_vec_free(&G.prs.lex_strnumbuf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3728 | } |
| 3729 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3730 | static void xc_parse_create(size_t fidx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3731 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3732 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3733 | memset(p, 0, sizeof(BcParse)); |
| 3734 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 3735 | bc_char_vec_init(&p->lex_strnumbuf); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3736 | IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);) |
| 3737 | IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);) |
| 3738 | IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3739 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 3740 | p->fidx = fidx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3741 | p->func = xc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3742 | } |
| 3743 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3744 | static void xc_program_add_fn(void) |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3745 | { |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3746 | //size_t idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3747 | BcFunc f; |
| 3748 | bc_func_init(&f); |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3749 | //idx = |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3750 | bc_vec_push(&G.prog.fns, &f); |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3751 | //return idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | #if ENABLE_BC |
| 3755 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3756 | // Note: takes ownership of 'name' (must be malloced) |
| 3757 | static size_t bc_program_addFunc(char *name) |
| 3758 | { |
| 3759 | size_t idx; |
| 3760 | BcId entry, *entry_ptr; |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3761 | int inserted; |
| 3762 | |
| 3763 | entry.name = name; |
| 3764 | entry.idx = G.prog.fns.len; |
| 3765 | |
| 3766 | inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx); |
| 3767 | if (!inserted) free(name); |
| 3768 | |
| 3769 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
| 3770 | idx = entry_ptr->idx; |
| 3771 | |
| 3772 | if (!inserted) { |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3773 | // There is already a function with this name. |
| 3774 | // It'll be redefined now, clear old definition. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3775 | BcFunc *func = xc_program_func(entry_ptr->idx); |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3776 | bc_func_free(func); |
| 3777 | bc_func_init(func); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3778 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3779 | xc_program_add_fn(); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | return idx; |
| 3783 | } |
| 3784 | |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 3785 | #define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops)) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3786 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3787 | // position of the first operator in the lex enum and adding the position of the |
| 3788 | // first in the expr enum. Note: This only works for binary operators. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3789 | #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] | 3790 | |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 3791 | static BC_STATUS zbc_parse_expr(uint8_t flags); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3792 | #define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3793 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3794 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3795 | #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] | 3796 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3797 | static BC_STATUS zbc_parse_stmt(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3798 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3799 | RETURN_STATUS(zbc_parse_stmt_possibly_auto(false)); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3800 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3801 | #define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3802 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3803 | 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] | 3804 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3805 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3806 | // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines. |
| 3807 | // Same for "else", "while()", "for()". |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3808 | BcStatus s = zbc_lex_next_and_skip_NLINE(); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3809 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3810 | if (p->lex == XC_LEX_NLINE) |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3811 | RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X)); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3812 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3813 | RETURN_STATUS(zbc_parse_stmt()); |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3814 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3815 | #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] | 3816 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3817 | 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] | 3818 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3819 | BcParse *p = &G.prs; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 3820 | char l, r = bc_operation_PREC(type - XC_LEX_1st_op); |
| 3821 | bool left = bc_operation_LEFT(type - XC_LEX_1st_op); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3822 | |
| 3823 | while (p->ops.len > start) { |
Denys Vlasenko | 7b1df3d | 2018-12-14 23:12:48 +0100 | [diff] [blame] | 3824 | BcLexType t = BC_PARSE_TOP_OP(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3825 | if (t == BC_LEX_LPAREN) break; |
| 3826 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 3827 | l = bc_operation_PREC(t - XC_LEX_1st_op); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3828 | if (l >= r && (l != r || !left)) break; |
| 3829 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3830 | xc_parse_push(BC_TOKEN_2_INST(t)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3831 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3832 | *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | bc_vec_push(&p->ops, &type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3836 | } |
| 3837 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3838 | 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] | 3839 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3840 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3841 | BcLexType top; |
| 3842 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3843 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3844 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3845 | top = BC_PARSE_TOP_OP(p); |
| 3846 | |
| 3847 | while (top != BC_LEX_LPAREN) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3848 | xc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3849 | |
| 3850 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 3851 | *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3852 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3853 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3854 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3855 | top = BC_PARSE_TOP_OP(p); |
| 3856 | } |
| 3857 | |
| 3858 | bc_vec_pop(&p->ops); |
| 3859 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 3860 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3861 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3862 | #define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3863 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3864 | static BC_STATUS zbc_parse_params(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3865 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3866 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3867 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3868 | size_t nparams; |
| 3869 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3870 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3871 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3872 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3873 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3874 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3875 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3876 | nparams = 0; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3877 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3878 | for (;;) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3879 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3880 | if (s) RETURN_STATUS(s); |
| 3881 | nparams++; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3882 | if (p->lex != BC_LEX_COMMA) { |
| 3883 | if (p->lex == BC_LEX_RPAREN) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3884 | break; |
| 3885 | RETURN_STATUS(bc_error_bad_token()); |
| 3886 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3887 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3888 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3889 | } |
| 3890 | } |
| 3891 | |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 3892 | xc_parse_pushInst_and_Index(BC_INST_CALL, nparams); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3893 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3894 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3895 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3896 | #define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3897 | |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 3898 | // Note: takes ownership of 'name' (must be malloced) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3899 | static BC_STATUS zbc_parse_call(char *name, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3900 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3901 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3902 | BcStatus s; |
| 3903 | BcId entry, *entry_ptr; |
| 3904 | size_t idx; |
| 3905 | |
| 3906 | entry.name = name; |
| 3907 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3908 | s = zbc_parse_params(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3909 | if (s) goto err; |
| 3910 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3911 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3912 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3913 | goto err; |
| 3914 | } |
| 3915 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3916 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3917 | |
| 3918 | if (idx == BC_VEC_INVALID_IDX) { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3919 | // No such function exists, create an empty one |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 3920 | bc_program_addFunc(name); |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3921 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3922 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3923 | free(name); |
| 3924 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3925 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3926 | xc_parse_pushIndex(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3927 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3928 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3929 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3930 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3931 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3932 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3933 | #define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3934 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3935 | static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3936 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3937 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3938 | BcStatus s; |
| 3939 | char *name; |
| 3940 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3941 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3942 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3943 | if (s) goto err; |
| 3944 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3945 | if (p->lex == BC_LEX_LBRACKET) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3946 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3947 | if (s) goto err; |
| 3948 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3949 | if (p->lex == BC_LEX_RBRACKET) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3950 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3951 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3952 | goto err; |
| 3953 | } |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3954 | *type = XC_INST_ARRAY; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3955 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3956 | *type = XC_INST_ARRAY_ELEM; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3957 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3958 | s = zbc_parse_expr(flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3959 | if (s) goto err; |
| 3960 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3961 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3962 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3963 | xc_parse_push(*type); |
| 3964 | xc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3965 | free(name); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3966 | } else if (p->lex == BC_LEX_LPAREN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3967 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3968 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3969 | goto err; |
| 3970 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3971 | *type = BC_INST_CALL; |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3972 | s = zbc_parse_call(name, flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3973 | } else { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 3974 | *type = XC_INST_VAR; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3975 | xc_parse_push(XC_INST_VAR); |
| 3976 | xc_parse_pushName(name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3977 | free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3978 | } |
| 3979 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3980 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3981 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3982 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3983 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3984 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3985 | #define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3986 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3987 | static BC_STATUS zbc_parse_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3988 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 3989 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3990 | BcStatus s; |
| 3991 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3992 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3993 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3994 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3995 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 3996 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3997 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 3998 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3999 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4000 | xc_parse_push(XC_INST_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4001 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 4002 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4003 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4004 | #define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4005 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4006 | 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] | 4007 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4008 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4009 | BcStatus s; |
| 4010 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4011 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4012 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4013 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4014 | |
| 4015 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 4016 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4017 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4018 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4019 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4020 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4021 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4022 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4023 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4024 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4025 | *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4026 | xc_parse_push(*prev); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4027 | |
Denys Vlasenko | 5fa74b9 | 2018-12-25 17:07:51 +0100 | [diff] [blame] | 4028 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4029 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4030 | #define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4031 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4032 | static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4033 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4034 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4035 | BcStatus s; |
| 4036 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4037 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4038 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4039 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4040 | if (p->lex != BC_LEX_LPAREN) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4041 | *type = XC_INST_SCALE; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4042 | xc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4043 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4044 | } |
| 4045 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4046 | *type = XC_INST_SCALE_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4047 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 4048 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4049 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4050 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4051 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4052 | s = zbc_parse_expr(flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4053 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4054 | if (p->lex != BC_LEX_RPAREN) |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4055 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4056 | xc_parse_push(XC_INST_SCALE_FUNC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4057 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4058 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4059 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4060 | #define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4061 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4062 | 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] | 4063 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4064 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4065 | BcStatus s; |
| 4066 | BcLexType type; |
| 4067 | char inst; |
| 4068 | BcInst etype = *prev; |
| 4069 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4070 | if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM |
| 4071 | || etype == XC_INST_SCALE || etype == BC_INST_LAST |
| 4072 | || etype == XC_INST_IBASE || etype == XC_INST_OBASE |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4073 | ) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4074 | *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4075 | xc_parse_push(inst); |
| 4076 | s = zxc_lex_next(); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4077 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4078 | *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4079 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4080 | s = zxc_lex_next(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4081 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4082 | type = p->lex; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4083 | |
| 4084 | // Because we parse the next part of the expression |
| 4085 | // right here, we need to increment this. |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4086 | *nexs = *nexs + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4087 | |
| 4088 | switch (type) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4089 | case XC_LEX_NAME: |
| 4090 | s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL); |
| 4091 | break; |
| 4092 | case BC_LEX_KEY_IBASE: |
| 4093 | case BC_LEX_KEY_LAST: |
| 4094 | case BC_LEX_KEY_OBASE: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4095 | xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
| 4096 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4097 | break; |
| 4098 | case BC_LEX_KEY_SCALE: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4099 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4100 | if (s) RETURN_STATUS(s); |
| 4101 | if (p->lex == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4102 | s = bc_error_bad_token(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4103 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4104 | xc_parse_push(XC_INST_SCALE); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4105 | break; |
| 4106 | default: |
| 4107 | s = bc_error_bad_token(); |
| 4108 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4109 | } |
| 4110 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4111 | if (!s) xc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4112 | } |
| 4113 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4114 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4115 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4116 | #define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4117 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4118 | static int bc_parse_inst_isLeaf(BcInst p) |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4119 | { |
| 4120 | return (p >= XC_INST_NUM && p <= XC_INST_SQRT) |
| 4121 | || p == BC_INST_INC_POST |
| 4122 | || p == BC_INST_DEC_POST |
| 4123 | ; |
| 4124 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4125 | #define BC_PARSE_LEAF(prev, bin_last, rparen) \ |
| 4126 | (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev))) |
Denys Vlasenko | 0c45bb22 | 2018-12-24 23:22:40 +0100 | [diff] [blame] | 4127 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4128 | static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn, |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4129 | bool rparen, bool bin_last, size_t *nexprs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4130 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4131 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4132 | BcStatus s; |
| 4133 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4134 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4135 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4136 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4137 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4138 | 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] | 4139 | *prev = BC_TOKEN_2_INST(type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4140 | |
| 4141 | // We can just push onto the op stack because this is the largest |
| 4142 | // precedence operator that gets pushed. Inc/dec does not. |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4143 | if (type != XC_LEX_OP_MINUS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4144 | bc_vec_push(&p->ops, &type); |
| 4145 | else |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4146 | bc_parse_operator(type, ops_bgn, nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4147 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4148 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4149 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4150 | #define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4151 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4152 | static BC_STATUS zbc_parse_print(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4153 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4154 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4155 | BcStatus s; |
| 4156 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4157 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4158 | for (;;) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4159 | s = zxc_lex_next(); |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4160 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4161 | type = p->lex; |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4162 | if (type == XC_LEX_STR) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4163 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4164 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4165 | s = zbc_parse_expr(0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4166 | } |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4167 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4168 | xc_parse_push(XC_INST_PRINT_POP); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4169 | if (p->lex != BC_LEX_COMMA) |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4170 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4171 | } |
| 4172 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4173 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4174 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4175 | #define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4176 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4177 | static BC_STATUS zbc_parse_return(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4178 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4179 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4180 | BcStatus s; |
| 4181 | BcLexType t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4182 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4183 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4184 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4185 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4186 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4187 | t = p->lex; |
Denys Vlasenko | 96b5ec1 | 2019-01-03 23:34:36 +0100 | [diff] [blame] | 4188 | if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON || t == BC_LEX_RBRACE) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4189 | xc_parse_push(BC_INST_RET0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4190 | else { |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4191 | //TODO: if (p->func->voidfunc) ERROR |
Denys Vlasenko | 96b5ec1 | 2019-01-03 23:34:36 +0100 | [diff] [blame] | 4192 | s = zbc_parse_expr(0); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4193 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4194 | |
Denys Vlasenko | 96b5ec1 | 2019-01-03 23:34:36 +0100 | [diff] [blame] | 4195 | if (t != BC_LEX_LPAREN // "return EXPR", no () |
| 4196 | || p->lex_last != BC_LEX_RPAREN // example: "return (a) + b" |
| 4197 | ) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4198 | s = zbc_POSIX_requires("parentheses around return expressions"); |
| 4199 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4200 | } |
| 4201 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4202 | xc_parse_push(XC_INST_RET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4203 | } |
| 4204 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4205 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4206 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4207 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4208 | #define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4209 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4210 | static void rewrite_label_to_current(size_t idx) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4211 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4212 | BcParse *p = &G.prs; |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4213 | size_t *label = bc_vec_item(&p->func->labels, idx); |
| 4214 | *label = p->func->code.len; |
| 4215 | } |
| 4216 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4217 | static BC_STATUS zbc_parse_if(void) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4218 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4219 | BcParse *p = &G.prs; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4220 | BcStatus s; |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4221 | size_t ip_idx; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4222 | |
| 4223 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4224 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4225 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4226 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4227 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4228 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4229 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4230 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4231 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4232 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4233 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4234 | // Encode "if zero, jump to ..." |
| 4235 | // Pushed value (destination of the jump) is uninitialized, |
| 4236 | // will be rewritten to be address of "end of if()" or of "else". |
| 4237 | ip_idx = bc_vec_push(&p->func->labels, &ip_idx); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4238 | bc_parse_pushJUMP_ZERO(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4239 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4240 | s = zbc_parse_stmt_allow_NLINE_before(STRING_if); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4241 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4242 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4243 | dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex); |
| 4244 | if (p->lex == BC_LEX_KEY_ELSE) { |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4245 | size_t ip2_idx; |
Denys Vlasenko | 7415633 | 2018-12-16 21:21:27 +0100 | [diff] [blame] | 4246 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4247 | // Encode "after then_stmt, jump to end of if()" |
| 4248 | ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx); |
| 4249 | 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] | 4250 | bc_parse_pushJUMP(ip2_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4251 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4252 | 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] | 4253 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4254 | |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4255 | ip_idx = ip2_idx; |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4256 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4257 | s = zbc_parse_stmt_allow_NLINE_before(STRING_else); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4258 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4259 | } |
| 4260 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4261 | 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] | 4262 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4263 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4264 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
| 4265 | RETURN_STATUS(s); |
| 4266 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4267 | #define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4268 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4269 | static BC_STATUS zbc_parse_while(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4270 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4271 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4272 | BcStatus s; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4273 | size_t cond_idx; |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4274 | size_t ip_idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4275 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4276 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4277 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4278 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4279 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4280 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4281 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4282 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4283 | ip_idx = cond_idx + 1; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4284 | bc_vec_push(&p->conds, &cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4285 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4286 | bc_vec_push(&p->exits, &ip_idx); |
| 4287 | bc_vec_push(&p->func->labels, &ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4288 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4289 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4290 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4291 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4292 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4293 | bc_parse_pushJUMP_ZERO(ip_idx); |
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 | s = zbc_parse_stmt_allow_NLINE_before(STRING_while); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4296 | if (s) RETURN_STATUS(s); |
| 4297 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4298 | 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] | 4299 | bc_parse_pushJUMP(cond_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4300 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4301 | 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] | 4302 | rewrite_label_to_current(ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4303 | |
| 4304 | bc_vec_pop(&p->exits); |
| 4305 | bc_vec_pop(&p->conds); |
| 4306 | |
| 4307 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4308 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4309 | #define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS) |
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 | static BC_STATUS zbc_parse_for(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4312 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4313 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4314 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4315 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4316 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4317 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4318 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4319 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4320 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4321 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4322 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4323 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4324 | if (p->lex != BC_LEX_SCOLON) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4325 | s = zbc_parse_expr(0); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4326 | xc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4327 | if (s) RETURN_STATUS(s); |
| 4328 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4329 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
| 4330 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4331 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4332 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4333 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4334 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4335 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4336 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4337 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4338 | update_idx = cond_idx + 1; |
| 4339 | body_idx = update_idx + 1; |
| 4340 | exit_idx = body_idx + 1; |
| 4341 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4342 | if (p->lex != BC_LEX_SCOLON) |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4343 | s = zbc_parse_expr(BC_PARSE_REL); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4344 | else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4345 | // Set this for the next call to xc_parse_pushNUM(). |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4346 | // This is safe to set because the current token is a semicolon, |
| 4347 | // which has no string requirement. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4348 | bc_vec_string(&p->lex_strnumbuf, 1, "1"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4349 | xc_parse_pushNUM(); |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4350 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4351 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4352 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4353 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4354 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4355 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4356 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4357 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4358 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4359 | bc_parse_pushJUMP_ZERO(exit_idx); |
| 4360 | bc_parse_pushJUMP(body_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4361 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4362 | bc_vec_push(&p->conds, &update_idx); |
| 4363 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4364 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4365 | if (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4366 | s = zbc_parse_expr(0); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4367 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4368 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4369 | xc_parse_push(XC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4370 | } else { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4371 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
| 4372 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4373 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4374 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4375 | bc_parse_pushJUMP(cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4376 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4377 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4378 | bc_vec_push(&p->exits, &exit_idx); |
| 4379 | bc_vec_push(&p->func->labels, &exit_idx); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4380 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4381 | s = zbc_parse_stmt_allow_NLINE_before(STRING_for); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4382 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4383 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4384 | 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] | 4385 | bc_parse_pushJUMP(update_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4386 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4387 | 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] | 4388 | rewrite_label_to_current(exit_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4389 | |
| 4390 | bc_vec_pop(&p->exits); |
| 4391 | bc_vec_pop(&p->conds); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4392 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4393 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4394 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4395 | #define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4396 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4397 | static BC_STATUS zbc_parse_break_or_continue(BcLexType type) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4398 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4399 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4400 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4401 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4402 | if (type == BC_LEX_KEY_BREAK) { |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4403 | if (p->exits.len == 0) // none of the enclosing blocks is a loop |
| 4404 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4405 | i = *(size_t*)bc_vec_top(&p->exits); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4406 | } else { |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4407 | i = *(size_t*)bc_vec_top(&p->conds); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4408 | } |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4409 | bc_parse_pushJUMP(i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4410 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4411 | RETURN_STATUS(zxc_lex_next()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4412 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4413 | #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] | 4414 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4415 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, BcType type) |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4416 | { |
| 4417 | BcId *autoid; |
| 4418 | BcId a; |
| 4419 | size_t i; |
| 4420 | |
| 4421 | autoid = (void*)f->autos.v; |
| 4422 | for (i = 0; i < f->autos.len; i++, autoid++) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4423 | if (strcmp(name, autoid->name) == 0 |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4424 | && type == (BcType) autoid->idx |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4425 | ) { |
Denys Vlasenko | 3f8752c | 2018-12-25 21:28:25 +0100 | [diff] [blame] | 4426 | RETURN_STATUS(bc_error("duplicate function parameter or auto name")); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4427 | } |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4428 | } |
| 4429 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4430 | a.idx = type; |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 4431 | a.name = name; |
| 4432 | |
| 4433 | bc_vec_push(&f->autos, &a); |
| 4434 | |
| 4435 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 4436 | } |
| 4437 | #define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS) |
| 4438 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4439 | static BC_STATUS zbc_parse_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4440 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4441 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4442 | BcStatus s; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4443 | bool comma, voidfunc; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4444 | char *name; |
| 4445 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4446 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4447 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4448 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4449 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4450 | RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4451 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4452 | // To be maximally both POSIX and GNU-compatible, |
| 4453 | // "void" is not treated as a normal keyword: |
| 4454 | // you can have variable named "void", and even a function |
| 4455 | // named "void": "define void() { return 6; }" is ok. |
| 4456 | // _Only_ "define void f() ..." syntax treats "void" |
| 4457 | // specially. |
| 4458 | voidfunc = (strcmp(p->lex_strnumbuf.v, "void") == 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4459 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4460 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4461 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4462 | |
| 4463 | voidfunc = (voidfunc && p->lex == XC_LEX_NAME); |
| 4464 | if (voidfunc) { |
| 4465 | s = zxc_lex_next(); |
| 4466 | if (s) RETURN_STATUS(s); |
| 4467 | } |
| 4468 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4469 | if (p->lex != BC_LEX_LPAREN) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4470 | RETURN_STATUS(bc_error_bad_function_definition()); |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4471 | |
| 4472 | p->fidx = bc_program_addFunc(xstrdup(p->lex_strnumbuf.v)); |
| 4473 | p->func = xc_program_func(p->fidx); |
| 4474 | p->func->voidfunc = voidfunc; |
| 4475 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4476 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4477 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4478 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4479 | comma = false; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4480 | while (p->lex != BC_LEX_RPAREN) { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4481 | BcType t = BC_TYPE_VAR; |
| 4482 | |
| 4483 | if (p->lex == XC_LEX_OP_MULTIPLY) { |
| 4484 | t = BC_TYPE_REF; |
| 4485 | s = zxc_lex_next(); |
| 4486 | if (s) RETURN_STATUS(s); |
| 4487 | s = zbc_POSIX_does_not_allow("references"); |
| 4488 | if (s) RETURN_STATUS(s); |
| 4489 | } |
| 4490 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4491 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4492 | RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4493 | |
| 4494 | ++p->func->nparams; |
| 4495 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4496 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4497 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4498 | if (s) goto err; |
| 4499 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4500 | if (p->lex == BC_LEX_LBRACKET) { |
| 4501 | if (t == BC_TYPE_VAR) t = BC_TYPE_ARRAY; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4502 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4503 | if (s) goto err; |
| 4504 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4505 | if (p->lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4506 | s = bc_error_bad_function_definition(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4507 | goto err; |
| 4508 | } |
| 4509 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4510 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4511 | if (s) goto err; |
| 4512 | } |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4513 | else if (t == BC_TYPE_REF) { |
| 4514 | s = bc_error_at("vars can't be references"); |
| 4515 | goto err; |
| 4516 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4517 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4518 | comma = p->lex == BC_LEX_COMMA; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4519 | if (comma) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4520 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4521 | if (s) goto err; |
| 4522 | } |
| 4523 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4524 | s = zbc_func_insert(p->func, name, t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4525 | if (s) goto err; |
| 4526 | } |
| 4527 | |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4528 | if (comma) RETURN_STATUS(bc_error_bad_function_definition()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4529 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4530 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4531 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4532 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4533 | if (p->lex != BC_LEX_LBRACE) { |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4534 | 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] | 4535 | if (s) RETURN_STATUS(s); |
| 4536 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4537 | |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4538 | // 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] | 4539 | s = zbc_lex_skip_if_at_NLINE(); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4540 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 4541 | // 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] | 4542 | if (p->lex != BC_LEX_LBRACE) |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4543 | RETURN_STATUS(bc_error("function { body } expected")); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4544 | |
| 4545 | p->in_funcdef++; // to determine whether "return" stmt is allowed, and such |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4546 | s = zbc_parse_stmt_possibly_auto(true); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4547 | p->in_funcdef--; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4548 | if (s) RETURN_STATUS(s); |
| 4549 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4550 | xc_parse_push(BC_INST_RET0); |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 4551 | |
| 4552 | // Subsequent code generation is into main program |
| 4553 | p->fidx = BC_PROG_MAIN; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4554 | p->func = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4555 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4556 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4557 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4558 | err: |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4559 | dbg_lex_done("%s:%d done (error)", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4560 | free(name); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4561 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4562 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4563 | #define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4564 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4565 | static BC_STATUS zbc_parse_auto(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4566 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4567 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4568 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4569 | char *name; |
| 4570 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4571 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4572 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4573 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4574 | |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4575 | for (;;) { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4576 | BcType t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4577 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4578 | if (p->lex != XC_LEX_NAME) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4579 | RETURN_STATUS(bc_error_at("bad 'auto' syntax")); |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4580 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4581 | name = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4582 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4583 | if (s) goto err; |
| 4584 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4585 | t = BC_TYPE_VAR; |
| 4586 | if (p->lex == BC_LEX_LBRACKET) { |
| 4587 | t = BC_TYPE_ARRAY; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4588 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4589 | if (s) goto err; |
| 4590 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4591 | if (p->lex != BC_LEX_RBRACKET) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4592 | s = bc_error_at("bad 'auto' syntax"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4593 | goto err; |
| 4594 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4595 | s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4596 | if (s) goto err; |
| 4597 | } |
| 4598 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 4599 | s = zbc_func_insert(p->func, name, t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4600 | if (s) goto err; |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4601 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4602 | if (p->lex == XC_LEX_NLINE |
| 4603 | || p->lex == BC_LEX_SCOLON |
| 4604 | //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}" |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4605 | ) { |
| 4606 | break; |
| 4607 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4608 | if (p->lex != BC_LEX_COMMA) |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4609 | RETURN_STATUS(bc_error_at("bad 'auto' syntax")); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4610 | s = zxc_lex_next(); // skip comma |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4611 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4612 | } |
| 4613 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4614 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d4b721c | 2018-12-25 16:39:01 +0100 | [diff] [blame] | 4615 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4616 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4617 | free(name); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4618 | dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4619 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4620 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4621 | #define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4622 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4623 | #undef zbc_parse_stmt_possibly_auto |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4624 | static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4625 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4626 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4627 | BcStatus s = BC_STATUS_SUCCESS; |
| 4628 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4629 | 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] | 4630 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4631 | if (p->lex == XC_LEX_NLINE) { |
Denys Vlasenko | 23ea073 | 2018-12-24 15:05:49 +0100 | [diff] [blame] | 4632 | dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__); |
Denys Vlasenko | fc7aa7a | 2019-01-08 18:08:48 +0100 | [diff] [blame] | 4633 | RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4634 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4635 | if (p->lex == BC_LEX_SCOLON) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4636 | dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__); |
Denys Vlasenko | fc7aa7a | 2019-01-08 18:08:48 +0100 | [diff] [blame] | 4637 | RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4638 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4639 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4640 | if (p->lex == BC_LEX_LBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4641 | dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed); |
| 4642 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4643 | s = zxc_lex_next(); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4644 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4645 | } while (p->lex == XC_LEX_NLINE); |
| 4646 | if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4647 | dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4648 | s = zbc_parse_auto(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4649 | if (s) RETURN_STATUS(s); |
| 4650 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4651 | while (p->lex != BC_LEX_RBRACE) { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4652 | dbg_lex("%s:%d block parsing loop", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4653 | s = zbc_parse_stmt(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4654 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | fc7aa7a | 2019-01-08 18:08:48 +0100 | [diff] [blame] | 4655 | // Check that next token is a correct stmt delimiter - |
| 4656 | // disallows "print 1 print 2" and such. |
| 4657 | if (p->lex == BC_LEX_RBRACE) |
| 4658 | break; |
| 4659 | if (p->lex != BC_LEX_SCOLON |
| 4660 | && p->lex != XC_LEX_NLINE |
| 4661 | ) { |
| 4662 | RETURN_STATUS(bc_error_at("bad statement terminator")); |
| 4663 | } |
| 4664 | s = zxc_lex_next(); |
| 4665 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4666 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4667 | s = zxc_lex_next(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4668 | dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__); |
| 4669 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4670 | } |
| 4671 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4672 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
| 4673 | switch (p->lex) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4674 | case XC_LEX_OP_MINUS: |
| 4675 | case BC_LEX_OP_INC: |
| 4676 | case BC_LEX_OP_DEC: |
| 4677 | case BC_LEX_OP_BOOL_NOT: |
| 4678 | case BC_LEX_LPAREN: |
| 4679 | case XC_LEX_NAME: |
| 4680 | case XC_LEX_NUMBER: |
| 4681 | case BC_LEX_KEY_IBASE: |
| 4682 | case BC_LEX_KEY_LAST: |
| 4683 | case BC_LEX_KEY_LENGTH: |
| 4684 | case BC_LEX_KEY_OBASE: |
| 4685 | case BC_LEX_KEY_READ: |
| 4686 | case BC_LEX_KEY_SCALE: |
| 4687 | case BC_LEX_KEY_SQRT: |
| 4688 | s = zbc_parse_expr(BC_PARSE_PRINT); |
| 4689 | break; |
| 4690 | case XC_LEX_STR: |
| 4691 | s = zbc_parse_pushSTR(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4692 | xc_parse_push(XC_INST_PRINT_STR); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4693 | break; |
| 4694 | case BC_LEX_KEY_BREAK: |
| 4695 | case BC_LEX_KEY_CONTINUE: |
| 4696 | s = zbc_parse_break_or_continue(p->lex); |
| 4697 | break; |
| 4698 | case BC_LEX_KEY_FOR: |
| 4699 | s = zbc_parse_for(); |
| 4700 | break; |
| 4701 | case BC_LEX_KEY_HALT: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4702 | xc_parse_push(BC_INST_HALT); |
| 4703 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4704 | break; |
| 4705 | case BC_LEX_KEY_IF: |
| 4706 | s = zbc_parse_if(); |
| 4707 | break; |
| 4708 | case BC_LEX_KEY_LIMITS: |
| 4709 | // "limits" is a compile-time command, |
| 4710 | // the output is produced at _parse time_. |
| 4711 | printf( |
| 4712 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4713 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4714 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4715 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
Denys Vlasenko | e05ec6e | 2019-01-04 16:26:19 +0100 | [diff] [blame] | 4716 | // "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" - GNU bc does not show this |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4717 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4718 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4719 | ); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4720 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4721 | break; |
| 4722 | case BC_LEX_KEY_PRINT: |
| 4723 | s = zbc_parse_print(); |
| 4724 | break; |
| 4725 | case BC_LEX_KEY_QUIT: |
| 4726 | // "quit" is a compile-time command. For example, |
| 4727 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4728 | // not when it is executed |
| 4729 | QUIT_OR_RETURN_TO_MAIN; |
| 4730 | case BC_LEX_KEY_RETURN: |
| 4731 | if (!p->in_funcdef) |
| 4732 | RETURN_STATUS(bc_error("'return' not in a function")); |
| 4733 | s = zbc_parse_return(); |
| 4734 | break; |
| 4735 | case BC_LEX_KEY_WHILE: |
| 4736 | s = zbc_parse_while(); |
| 4737 | break; |
| 4738 | default: |
| 4739 | s = bc_error_bad_token(); |
| 4740 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4741 | } |
| 4742 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4743 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4744 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4745 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4746 | #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] | 4747 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4748 | static BC_STATUS zbc_parse_stmt_or_funcdef(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4749 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4750 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4751 | BcStatus s; |
| 4752 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4753 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | fc7aa7a | 2019-01-08 18:08:48 +0100 | [diff] [blame] | 4754 | //why? |
| 4755 | // if (p->lex == XC_LEX_EOF) |
| 4756 | // s = bc_error("end of file"); |
| 4757 | // else |
| 4758 | if (p->lex == BC_LEX_KEY_DEFINE) { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4759 | dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__); |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4760 | s = zbc_parse_funcdef(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4761 | } else { |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4762 | 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] | 4763 | s = zbc_parse_stmt(); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4764 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4765 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4766 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4767 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4768 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4769 | #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] | 4770 | |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4771 | #undef zbc_parse_expr |
| 4772 | static BC_STATUS zbc_parse_expr(uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4773 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 4774 | BcParse *p = &G.prs; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4775 | BcInst prev = XC_INST_PRINT; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4776 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4777 | unsigned nparens, nrelops; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4778 | bool paren_first, rprn, assign, bin_last, incdec; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4779 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4780 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4781 | paren_first = (p->lex == BC_LEX_LPAREN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4782 | nparens = nrelops = 0; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4783 | rprn = assign = incdec = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4784 | bin_last = true; |
| 4785 | |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4786 | for (;;) { |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4787 | bool get_token; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4788 | BcStatus s; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 4789 | BcLexType t = p->lex; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4790 | |
| 4791 | if (!lex_allowed_in_bc_expr(t)) |
| 4792 | break; |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4793 | |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4794 | dbg_lex("%s:%d t:%d", __func__, __LINE__, t); |
Denys Vlasenko | 73b3ebc | 2018-12-25 01:43:52 +0100 | [diff] [blame] | 4795 | get_token = false; |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4796 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4797 | switch (t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4798 | case BC_LEX_OP_INC: |
| 4799 | case BC_LEX_OP_DEC: |
| 4800 | dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__); |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4801 | if (incdec) RETURN_STATUS(bc_error_bad_assignment()); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4802 | s = zbc_parse_incdec(&prev, &nexprs, flags); |
| 4803 | incdec = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4804 | rprn = bin_last = false; |
| 4805 | //get_token = false; - already is |
| 4806 | break; |
| 4807 | case XC_LEX_OP_MINUS: |
| 4808 | dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4809 | s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4810 | rprn = false; |
| 4811 | //get_token = false; - already is |
| 4812 | bin_last = (prev == XC_INST_MINUS); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4813 | if (bin_last) incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4814 | break; |
| 4815 | case BC_LEX_OP_ASSIGN_POWER: |
| 4816 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4817 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4818 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4819 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4820 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4821 | case BC_LEX_OP_ASSIGN: |
| 4822 | dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__); |
| 4823 | if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM |
| 4824 | && prev != XC_INST_SCALE && prev != XC_INST_IBASE |
| 4825 | && prev != XC_INST_OBASE && prev != BC_INST_LAST |
| 4826 | ) { |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4827 | RETURN_STATUS(bc_error_bad_assignment()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4828 | } |
| 4829 | // Fallthrough. |
| 4830 | case XC_LEX_OP_POWER: |
| 4831 | case XC_LEX_OP_MULTIPLY: |
| 4832 | case XC_LEX_OP_DIVIDE: |
| 4833 | case XC_LEX_OP_MODULUS: |
| 4834 | case XC_LEX_OP_PLUS: |
| 4835 | case XC_LEX_OP_REL_EQ: |
| 4836 | case XC_LEX_OP_REL_LE: |
| 4837 | case XC_LEX_OP_REL_GE: |
| 4838 | case XC_LEX_OP_REL_NE: |
| 4839 | case XC_LEX_OP_REL_LT: |
| 4840 | case XC_LEX_OP_REL_GT: |
| 4841 | case BC_LEX_OP_BOOL_NOT: |
| 4842 | case BC_LEX_OP_BOOL_OR: |
| 4843 | case BC_LEX_OP_BOOL_AND: |
| 4844 | dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4845 | if (t == BC_LEX_OP_BOOL_NOT) { |
| 4846 | if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4847 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4848 | } else if (prev == XC_INST_BOOL_NOT) { |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4849 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4850 | } |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4851 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4852 | nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT); |
| 4853 | prev = BC_TOKEN_2_INST(t); |
| 4854 | bc_parse_operator(t, ops_bgn, &nexprs); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4855 | rprn = incdec = false; |
| 4856 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4857 | bin_last = (t != BC_LEX_OP_BOOL_NOT); |
| 4858 | break; |
| 4859 | case BC_LEX_LPAREN: |
| 4860 | dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4861 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4862 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4863 | bc_vec_push(&p->ops, &t); |
| 4864 | nparens++; |
| 4865 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4866 | rprn = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4867 | break; |
| 4868 | case BC_LEX_RPAREN: |
| 4869 | dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__); |
Denys Vlasenko | a1698a1 | 2019-01-08 19:32:38 +0100 | [diff] [blame] | 4870 | //why? |
| 4871 | // if (p->lex_last == BC_LEX_LPAREN) { |
| 4872 | // RETURN_STATUS(bc_error_at("empty expression")); |
| 4873 | // } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4874 | if (bin_last || prev == XC_INST_BOOL_NOT) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4875 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4876 | if (nparens == 0) { |
| 4877 | goto exit_loop; |
| 4878 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4879 | s = zbc_parse_rightParen(ops_bgn, &nexprs); |
| 4880 | nparens--; |
| 4881 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4882 | rprn = true; |
| 4883 | bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4884 | break; |
| 4885 | case XC_LEX_NAME: |
| 4886 | dbg_lex("%s:%d LEX_NAME", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4887 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4888 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4889 | s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4890 | rprn = (prev == BC_INST_CALL); |
| 4891 | bin_last = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4892 | //get_token = false; - already is |
| 4893 | nexprs++; |
| 4894 | break; |
| 4895 | case XC_LEX_NUMBER: |
| 4896 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4897 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4898 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4899 | xc_parse_pushNUM(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4900 | prev = XC_INST_NUM; |
| 4901 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4902 | rprn = bin_last = false; |
| 4903 | nexprs++; |
| 4904 | break; |
| 4905 | case BC_LEX_KEY_IBASE: |
| 4906 | case BC_LEX_KEY_LAST: |
| 4907 | case BC_LEX_KEY_OBASE: |
| 4908 | dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4909 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4910 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4911 | prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4912 | xc_parse_push((char) prev); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4913 | get_token = true; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4914 | rprn = bin_last = false; |
| 4915 | nexprs++; |
| 4916 | break; |
| 4917 | case BC_LEX_KEY_LENGTH: |
| 4918 | case BC_LEX_KEY_SQRT: |
| 4919 | dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4920 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4921 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4922 | s = zbc_parse_builtin(t, flags, &prev); |
| 4923 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4924 | rprn = bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4925 | nexprs++; |
| 4926 | break; |
| 4927 | case BC_LEX_KEY_READ: |
| 4928 | dbg_lex("%s:%d LEX_READ", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4929 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4930 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4931 | s = zbc_parse_read(); |
| 4932 | prev = XC_INST_READ; |
| 4933 | get_token = true; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4934 | rprn = bin_last = incdec = false; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4935 | nexprs++; |
| 4936 | break; |
| 4937 | case BC_LEX_KEY_SCALE: |
| 4938 | dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__); |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 4939 | if (BC_PARSE_LEAF(prev, bin_last, rprn)) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4940 | RETURN_STATUS(bc_error_bad_expression()); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4941 | s = zbc_parse_scale(&prev, flags); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4942 | //get_token = false; - already is |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 4943 | rprn = bin_last = false; |
| 4944 | nexprs++; |
| 4945 | break; |
| 4946 | default: |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4947 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4948 | } |
| 4949 | |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4950 | if (s || G_interrupt) // error, or ^C: stop parsing |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4951 | RETURN_STATUS(BC_STATUS_FAILURE); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4952 | if (get_token) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4953 | s = zxc_lex_next(); |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4954 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4955 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4956 | } |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4957 | exit_loop: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4958 | |
| 4959 | while (p->ops.len > ops_bgn) { |
Denys Vlasenko | d0238d8 | 2018-12-25 01:21:16 +0100 | [diff] [blame] | 4960 | BcLexType top = BC_PARSE_TOP_OP(p); |
Denys Vlasenko | a5bf53e | 2018-12-24 17:06:37 +0100 | [diff] [blame] | 4961 | assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4962 | |
| 4963 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4964 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4965 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4966 | xc_parse_push(BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4967 | |
Denys Vlasenko | 69560f4 | 2018-12-24 14:14:23 +0100 | [diff] [blame] | 4968 | nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4969 | bc_vec_pop(&p->ops); |
| 4970 | } |
| 4971 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 4972 | if (prev == XC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4973 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4974 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4975 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4976 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4977 | s = zbc_POSIX_does_not_allow("comparison operators outside if or loops"); |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4978 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4979 | } else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4980 | BcStatus s; |
Denys Vlasenko | 79587cb | 2018-12-24 18:11:41 +0100 | [diff] [blame] | 4981 | s = zbc_POSIX_requires("exactly one comparison operator per condition"); |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4982 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4983 | } |
| 4984 | |
| 4985 | if (flags & BC_PARSE_PRINT) { |
Denys Vlasenko | bb11603 | 2018-12-25 01:16:37 +0100 | [diff] [blame] | 4986 | if (paren_first || !assign) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 4987 | xc_parse_push(XC_INST_PRINT); |
| 4988 | xc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4989 | } |
| 4990 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4991 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4992 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4993 | } |
Denys Vlasenko | 132d7c0 | 2019-01-08 19:29:35 +0100 | [diff] [blame] | 4994 | #define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4995 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4996 | #endif // ENABLE_BC |
| 4997 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 4998 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 4999 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5000 | static BC_STATUS zdc_parse_register(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5001 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5002 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5003 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5004 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5005 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5006 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5007 | if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5008 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5009 | xc_parse_pushName(p->lex_strnumbuf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5010 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5011 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5012 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5013 | #define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5014 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5015 | static void dc_parse_string(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5016 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5017 | BcParse *p = &G.prs; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5018 | char *str; |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 5019 | size_t len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5020 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5021 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5022 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5023 | str = xstrdup(p->lex_strnumbuf.v); |
Denys Vlasenko | cfc2546 | 2019-01-09 11:17:19 +0100 | [diff] [blame] | 5024 | xc_parse_pushInst_and_Index(XC_INST_STR, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5025 | bc_vec_push(&G.prog.strs, &str); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5026 | |
Brian Foley | b64470b | 2019-09-05 10:50:13 +0200 | [diff] [blame] | 5027 | // Add an empty function so that if zdc_program_execStr ever needs to |
| 5028 | // parse the string into code (from the 'x' command) there's somewhere |
| 5029 | // to store the bytecode. |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5030 | xc_program_add_fn(); |
| 5031 | p->func = xc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5032 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5033 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5034 | } |
| 5035 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5036 | 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] | 5037 | { |
| 5038 | BcStatus s; |
| 5039 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5040 | xc_parse_push(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5041 | if (name) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5042 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5043 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | if (store) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5047 | xc_parse_push(DC_INST_SWAP); |
| 5048 | xc_parse_push(XC_INST_ASSIGN); |
| 5049 | xc_parse_push(XC_INST_POP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5050 | } |
| 5051 | |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5052 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5053 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5054 | #define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS) |
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 | static BC_STATUS zdc_parse_cond(uint8_t inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5057 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5058 | BcParse *p = &G.prs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5059 | BcStatus s; |
| 5060 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5061 | xc_parse_push(inst); |
| 5062 | xc_parse_push(DC_INST_EXEC_COND); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5063 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5064 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5065 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5066 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5067 | s = zxc_lex_next(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5068 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5069 | |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5070 | // Note that 'else' part can not be on the next line: |
| 5071 | // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2" |
| 5072 | // 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] | 5073 | if (p->lex == DC_LEX_ELSE) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5074 | s = zdc_parse_register(); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5075 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5076 | s = zxc_lex_next(); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5077 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5078 | xc_parse_push('\0'); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5079 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5080 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5081 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5082 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5083 | #define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5084 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5085 | static BC_STATUS zdc_parse_token(BcLexType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5086 | { |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5087 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5088 | uint8_t inst; |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5089 | bool assign, get_token; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5090 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5091 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5092 | s = BC_STATUS_SUCCESS; |
| 5093 | get_token = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5094 | switch (t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5095 | case XC_LEX_OP_REL_EQ: |
| 5096 | case XC_LEX_OP_REL_LE: |
| 5097 | case XC_LEX_OP_REL_GE: |
| 5098 | case XC_LEX_OP_REL_NE: |
| 5099 | case XC_LEX_OP_REL_LT: |
| 5100 | case XC_LEX_OP_REL_GT: |
| 5101 | dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__); |
| 5102 | s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ); |
| 5103 | get_token = false; |
| 5104 | break; |
| 5105 | case DC_LEX_SCOLON: |
| 5106 | case DC_LEX_COLON: |
| 5107 | dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__); |
| 5108 | s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON); |
| 5109 | break; |
| 5110 | case XC_LEX_STR: |
| 5111 | dbg_lex("%s:%d LEX_STR", __func__, __LINE__); |
| 5112 | dc_parse_string(); |
| 5113 | break; |
| 5114 | case XC_LEX_NEG: |
| 5115 | dbg_lex("%s:%d LEX_NEG", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5116 | s = zxc_lex_next(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5117 | if (s) RETURN_STATUS(s); |
| 5118 | if (G.prs.lex != XC_LEX_NUMBER) |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 5119 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5120 | xc_parse_pushNUM(); |
| 5121 | xc_parse_push(XC_INST_NEG); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5122 | break; |
| 5123 | case XC_LEX_NUMBER: |
| 5124 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5125 | xc_parse_pushNUM(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5126 | break; |
| 5127 | case DC_LEX_READ: |
| 5128 | dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5129 | xc_parse_push(XC_INST_READ); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5130 | break; |
| 5131 | case DC_LEX_OP_ASSIGN: |
| 5132 | case DC_LEX_STORE_PUSH: |
| 5133 | dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__); |
| 5134 | assign = (t == DC_LEX_OP_ASSIGN); |
| 5135 | inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR; |
| 5136 | s = zdc_parse_mem(inst, true, assign); |
| 5137 | break; |
| 5138 | case DC_LEX_LOAD: |
| 5139 | case DC_LEX_LOAD_POP: |
| 5140 | dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__); |
| 5141 | inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD; |
| 5142 | s = zdc_parse_mem(inst, true, false); |
| 5143 | break; |
| 5144 | case DC_LEX_STORE_IBASE: |
| 5145 | case DC_LEX_STORE_SCALE: |
| 5146 | case DC_LEX_STORE_OBASE: |
| 5147 | dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__); |
| 5148 | inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE; |
| 5149 | s = zdc_parse_mem(inst, false, true); |
| 5150 | break; |
| 5151 | default: |
| 5152 | dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__); |
| 5153 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5154 | } |
| 5155 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5156 | if (!s && get_token) s = zxc_lex_next(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5157 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5158 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5159 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5160 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5161 | #define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5162 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5163 | static BC_STATUS zdc_parse_expr(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5164 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5165 | BcParse *p = &G.prs; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5166 | int i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5167 | |
Denys Vlasenko | 6842c60 | 2019-01-04 05:41:47 +0100 | [diff] [blame] | 5168 | if (p->lex == XC_LEX_NLINE) |
| 5169 | RETURN_STATUS(zxc_lex_next()); |
| 5170 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5171 | i = (int)p->lex - (int)XC_LEX_OP_POWER; |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5172 | if (i >= 0) { |
| 5173 | BcInst inst = dc_LEX_to_INST[i]; |
| 5174 | if (inst != DC_INST_INVALID) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5175 | xc_parse_push(inst); |
| 5176 | RETURN_STATUS(zxc_lex_next()); |
Denys Vlasenko | 4accb6b | 2018-12-24 15:29:08 +0100 | [diff] [blame] | 5177 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5178 | } |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5179 | RETURN_STATUS(zdc_parse_token(p->lex)); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5180 | } |
| 5181 | #define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
| 5182 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5183 | static BC_STATUS zdc_parse_exprs_until_eof(void) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5184 | { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5185 | BcParse *p = &G.prs; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5186 | dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex); |
| 5187 | while (p->lex != XC_LEX_EOF) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5188 | BcStatus s = zdc_parse_expr(); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5189 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5190 | } |
| 5191 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 5192 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5193 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5194 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5195 | #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] | 5196 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5197 | #endif // ENABLE_DC |
| 5198 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5199 | // |
| 5200 | // Execution engine |
| 5201 | // |
| 5202 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 5203 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 5204 | #define BC_PROG_NUM(r, n) \ |
| 5205 | ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n)) |
| 5206 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5207 | #define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n))) |
| 5208 | #define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n))) |
| 5209 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5210 | static size_t xc_program_index(char *code, size_t *bgn) |
| 5211 | { |
| 5212 | unsigned char *bytes = (void*)(code + *bgn); |
| 5213 | unsigned amt; |
| 5214 | unsigned i; |
| 5215 | size_t res; |
| 5216 | |
| 5217 | amt = *bytes++; |
| 5218 | if (amt < SMALL_INDEX_LIMIT) { |
| 5219 | *bgn += 1; |
| 5220 | return amt; |
| 5221 | } |
| 5222 | amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here |
| 5223 | *bgn += amt + 1; |
| 5224 | |
| 5225 | res = 0; |
| 5226 | i = 0; |
| 5227 | do { |
| 5228 | res |= (size_t)(*bytes++) << i; |
| 5229 | i += 8; |
| 5230 | } while (--amt != 0); |
| 5231 | |
| 5232 | return res; |
| 5233 | } |
| 5234 | |
| 5235 | static char *xc_program_name(char *code, size_t *bgn) |
| 5236 | { |
| 5237 | code += *bgn; |
| 5238 | *bgn += strlen(code) + 1; |
| 5239 | |
| 5240 | return xstrdup(code); |
| 5241 | } |
| 5242 | |
| 5243 | static BcVec* xc_program_dereference(BcVec *vec) |
| 5244 | { |
| 5245 | BcVec *v; |
| 5246 | size_t vidx, nidx, i = 0; |
| 5247 | |
| 5248 | //assert(vec->size == sizeof(uint8_t)); |
| 5249 | |
| 5250 | vidx = xc_program_index(vec->v, &i); |
| 5251 | nidx = xc_program_index(vec->v, &i); |
| 5252 | |
| 5253 | v = bc_vec_item(&G.prog.arrs, vidx); |
| 5254 | v = bc_vec_item(v, nidx); |
| 5255 | |
| 5256 | //assert(v->size != sizeof(uint8_t)); |
| 5257 | |
| 5258 | return v; |
| 5259 | } |
| 5260 | |
| 5261 | static BcVec* xc_program_search(char *id, BcType type) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5262 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5263 | BcId e, *ptr; |
| 5264 | BcVec *v, *map; |
| 5265 | size_t i; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5266 | int new; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5267 | bool var = (type == BC_TYPE_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5268 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5269 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5270 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5271 | |
| 5272 | e.name = id; |
| 5273 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5274 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5275 | |
| 5276 | if (new) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5277 | BcVec v2; |
| 5278 | bc_array_init(&v2, var); |
| 5279 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5280 | } |
| 5281 | |
| 5282 | ptr = bc_vec_item(map, i); |
| 5283 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5284 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5285 | } |
| 5286 | |
Denys Vlasenko | 8287b1c | 2018-12-21 22:43:53 +0100 | [diff] [blame] | 5287 | // 'num' need not be initialized on entry |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5288 | static BC_STATUS zxc_program_num(BcResult *r, BcNum **num) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5289 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5290 | switch (r->t) { |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5291 | case XC_RESULT_STR: |
| 5292 | case XC_RESULT_TEMP: |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5293 | IF_BC(case BC_RESULT_VOID:) |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5294 | case XC_RESULT_IBASE: |
| 5295 | case XC_RESULT_SCALE: |
| 5296 | case XC_RESULT_OBASE: |
| 5297 | *num = &r->d.n; |
| 5298 | break; |
| 5299 | case XC_RESULT_CONSTANT: { |
| 5300 | BcStatus s; |
| 5301 | char *str; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5302 | size_t len; |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 5303 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5304 | str = *xc_program_const(r->d.id.idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5305 | len = strlen(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5306 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5307 | bc_num_init(&r->d.n, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5308 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5309 | s = zxc_num_parse(&r->d.n, str, G.prog.ib_t); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5310 | if (s) { |
| 5311 | bc_num_free(&r->d.n); |
| 5312 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5313 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5314 | *num = &r->d.n; |
| 5315 | r->t = XC_RESULT_TEMP; |
| 5316 | break; |
| 5317 | } |
| 5318 | case XC_RESULT_VAR: |
| 5319 | case XC_RESULT_ARRAY: |
| 5320 | case XC_RESULT_ARRAY_ELEM: { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5321 | BcType type = (r->t == XC_RESULT_VAR) ? BC_TYPE_VAR : BC_TYPE_ARRAY; |
| 5322 | BcVec *v = xc_program_search(r->d.id.name, type); |
| 5323 | void *p = bc_vec_top(v); |
| 5324 | |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5325 | if (r->t == XC_RESULT_ARRAY_ELEM) { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5326 | size_t idx = r->d.id.idx; |
| 5327 | |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5328 | v = p; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5329 | if (v->size == sizeof(uint8_t)) |
| 5330 | v = xc_program_dereference(v); |
| 5331 | //assert(v->size == sizeof(BcNum)); |
| 5332 | if (v->len <= idx) |
| 5333 | bc_array_expand(v, idx + 1); |
| 5334 | *num = bc_vec_item(v, idx); |
Denys Vlasenko | 8ab209f | 2018-12-29 16:23:34 +0100 | [diff] [blame] | 5335 | } else { |
| 5336 | *num = p; |
| 5337 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5338 | break; |
| 5339 | } |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5340 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5341 | case BC_RESULT_LAST: |
| 5342 | *num = &G.prog.last; |
| 5343 | break; |
| 5344 | case BC_RESULT_ONE: |
| 5345 | *num = &G.prog.one; |
| 5346 | break; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5347 | #endif |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5348 | #if SANITY_CHECKS |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 5349 | default: |
| 5350 | // Testing the theory that dc does not reach LAST/ONE |
| 5351 | bb_error_msg_and_die("BUG:%d", r->t); |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 5352 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5353 | } |
| 5354 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5355 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5356 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5357 | #define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5358 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5359 | static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5360 | BcResult **r, BcNum **rn, bool assign) |
| 5361 | { |
| 5362 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5363 | BcResultType lt, rt; |
| 5364 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5365 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5366 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5367 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5368 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5369 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5370 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5371 | s = zxc_program_num(*l, ln); |
| 5372 | if (s) RETURN_STATUS(s); |
| 5373 | s = zxc_program_num(*r, rn); |
| 5374 | if (s) RETURN_STATUS(s); |
| 5375 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5376 | lt = (*l)->t; |
| 5377 | rt = (*r)->t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5378 | |
| 5379 | // We run this again under these conditions in case any vector has been |
| 5380 | // reallocated out from under the BcNums or arrays we had. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5381 | if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5382 | s = zxc_program_num(*l, ln); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5383 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5384 | } |
| 5385 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5386 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5387 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5388 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5389 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5390 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5391 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5392 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5393 | #define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5394 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5395 | static void xc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5396 | { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5397 | r->t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5398 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5399 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5400 | } |
| 5401 | |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5402 | // Note: *r and *n need not be initialized by caller |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5403 | static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5404 | { |
| 5405 | BcStatus s; |
| 5406 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5407 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5408 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5409 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5410 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5411 | s = zxc_program_num(*r, n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5412 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5413 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5414 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5415 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5416 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5417 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5418 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5419 | #define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5420 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5421 | static void xc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5422 | { |
| 5423 | r->t = t; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5424 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5425 | } |
| 5426 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5427 | static BC_STATUS zxc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5428 | { |
| 5429 | BcStatus s; |
| 5430 | BcResult *opd1, *opd2, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5431 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5432 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5433 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5434 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5435 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5436 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5437 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5438 | 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] | 5439 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5440 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5441 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5442 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5443 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5444 | bc_num_free(&res.d.n); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5445 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5446 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5447 | #define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5448 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5449 | static BC_STATUS zxc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5450 | { |
| 5451 | BcStatus s; |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5452 | BcParse sv_parse; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5453 | BcVec buf; |
| 5454 | BcInstPtr ip; |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5455 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5456 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 5457 | bc_char_vec_init(&buf); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5458 | xc_read_line(&buf, stdin); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5459 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5460 | f = xc_program_func(BC_PROG_READ); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5461 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5462 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5463 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5464 | xc_parse_create(BC_PROG_READ); |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 5465 | //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] | 5466 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5467 | s = zxc_parse_text_init(buf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5468 | if (s) goto exec_err; |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5469 | if (IS_BC) { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5470 | IF_BC(s = zbc_parse_expr(0)); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5471 | } else { |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 5472 | IF_DC(s = zdc_parse_exprs_until_eof()); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5473 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5474 | if (s) goto exec_err; |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 5475 | 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] | 5476 | s = bc_error_at("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5477 | goto exec_err; |
| 5478 | } |
Denys Vlasenko | 2747f61 | 2018-12-31 18:48:10 +0100 | [diff] [blame] | 5479 | xc_parse_push(XC_INST_RET); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5480 | |
| 5481 | ip.func = BC_PROG_READ; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5482 | ip.inst_idx = 0; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5483 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5484 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5485 | exec_err: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5486 | xc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 5487 | G.prs = sv_parse; // struct copy |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5488 | bc_vec_free(&buf); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5489 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5490 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5491 | #define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5492 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5493 | static void xc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5494 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5495 | #if ENABLE_DC |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5496 | if (!str[0] && IS_DC) { |
Denys Vlasenko | 2c6f563 | 2018-12-11 21:21:14 +0100 | [diff] [blame] | 5497 | // Example: echo '[]ap' | dc |
| 5498 | // should print two bytes: 0x00, 0x0A |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5499 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5500 | return; |
| 5501 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5502 | #endif |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5503 | while (*str) { |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5504 | char c = *str++; |
| 5505 | if (c == '\\') { |
| 5506 | static const char esc[] ALIGN1 = "nabfrt""e\\"; |
| 5507 | char *n; |
| 5508 | |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5509 | c = *str++; |
Brian Foley | 10509a7 | 2019-09-05 10:53:21 +0200 | [diff] [blame] | 5510 | n = strchr(esc, c); // note: if c is NUL, n = \0 at end of esc |
| 5511 | if (!n || !c) { |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5512 | // Just print the backslash and following character |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5513 | bb_putchar('\\'); |
| 5514 | ++G.prog.nchars; |
Brian Foley | 10509a7 | 2019-09-05 10:53:21 +0200 | [diff] [blame] | 5515 | // But if we're at the end of the string, stop |
| 5516 | if (!c) break; |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5517 | } else { |
| 5518 | if (n - esc == 0) // "\n" ? |
| 5519 | G.prog.nchars = SIZE_MAX; |
| 5520 | c = "\n\a\b\f\r\t""\\\\""\\"[n - esc]; |
| 5521 | // n a b f r t e \ \<end of line> |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5522 | } |
| 5523 | } |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5524 | putchar(c); |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5525 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5526 | } |
| 5527 | } |
| 5528 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5529 | static void bc_num_printNewline(void) |
| 5530 | { |
| 5531 | if (G.prog.nchars == G.prog.len - 1) { |
| 5532 | bb_putchar('\\'); |
| 5533 | bb_putchar('\n'); |
| 5534 | G.prog.nchars = 0; |
| 5535 | } |
| 5536 | } |
| 5537 | |
| 5538 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5539 | 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] | 5540 | { |
| 5541 | (void) radix; |
| 5542 | bb_putchar((char) num); |
| 5543 | G.prog.nchars += width; |
| 5544 | } |
| 5545 | #endif |
| 5546 | |
| 5547 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
| 5548 | { |
| 5549 | size_t exp, pow; |
| 5550 | |
| 5551 | bc_num_printNewline(); |
| 5552 | bb_putchar(radix ? '.' : ' '); |
| 5553 | ++G.prog.nchars; |
| 5554 | |
| 5555 | bc_num_printNewline(); |
| 5556 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 5557 | continue; |
| 5558 | |
| 5559 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
| 5560 | size_t dig; |
| 5561 | bc_num_printNewline(); |
| 5562 | dig = num / pow; |
| 5563 | num -= dig * pow; |
| 5564 | bb_putchar(((char) dig) + '0'); |
| 5565 | } |
| 5566 | } |
| 5567 | |
| 5568 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
| 5569 | { |
| 5570 | if (radix) { |
| 5571 | bc_num_printNewline(); |
| 5572 | bb_putchar('.'); |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5573 | G.prog.nchars++; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5574 | } |
| 5575 | |
| 5576 | bc_num_printNewline(); |
| 5577 | bb_putchar(bb_hexdigits_upcase[num]); |
| 5578 | G.prog.nchars += width; |
| 5579 | } |
| 5580 | |
| 5581 | static void bc_num_printDecimal(BcNum *n) |
| 5582 | { |
| 5583 | size_t i, rdx = n->rdx - 1; |
| 5584 | |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5585 | if (n->neg) { |
| 5586 | bb_putchar('-'); |
| 5587 | G.prog.nchars++; |
| 5588 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5589 | |
| 5590 | for (i = n->len - 1; i < n->len; --i) |
| 5591 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
| 5592 | } |
| 5593 | |
Denys Vlasenko | 2097ac8 | 2018-12-24 05:00:36 +0100 | [diff] [blame] | 5594 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
| 5595 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5596 | 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] | 5597 | { |
| 5598 | BcStatus s; |
| 5599 | BcVec stack; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5600 | BcNum base; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5601 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5602 | BcNum intp, fracp, digit, frac_len; |
| 5603 | unsigned long dig, *ptr; |
| 5604 | size_t i; |
| 5605 | bool radix; |
| 5606 | |
| 5607 | if (n->len == 0) { |
| 5608 | print(0, width, false); |
| 5609 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5610 | } |
| 5611 | |
| 5612 | bc_vec_init(&stack, sizeof(long), NULL); |
| 5613 | bc_num_init(&intp, n->len); |
| 5614 | bc_num_init(&fracp, n->rdx); |
| 5615 | bc_num_init(&digit, width); |
| 5616 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 5617 | bc_num_copy(&intp, n); |
| 5618 | bc_num_one(&frac_len); |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5619 | base.cap = ARRAY_SIZE(base_digs); |
| 5620 | base.num = base_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5621 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5622 | |
| 5623 | bc_num_truncate(&intp, intp.rdx); |
| 5624 | s = zbc_num_sub(n, &intp, &fracp, 0); |
| 5625 | if (s) goto err; |
| 5626 | |
| 5627 | while (intp.len != 0) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5628 | s = zbc_num_divmod(&intp, &base, &intp, &digit, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5629 | if (s) goto err; |
| 5630 | s = zbc_num_ulong(&digit, &dig); |
| 5631 | if (s) goto err; |
| 5632 | bc_vec_push(&stack, &dig); |
| 5633 | } |
| 5634 | |
| 5635 | for (i = 0; i < stack.len; ++i) { |
| 5636 | ptr = bc_vec_item_rev(&stack, i); |
| 5637 | print(*ptr, width, false); |
| 5638 | } |
| 5639 | |
| 5640 | if (!n->rdx) goto err; |
| 5641 | |
| 5642 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5643 | s = zbc_num_mul(&fracp, &base, &fracp, n->rdx); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5644 | if (s) goto err; |
| 5645 | s = zbc_num_ulong(&fracp, &dig); |
| 5646 | if (s) goto err; |
| 5647 | bc_num_ulong2num(&intp, dig); |
| 5648 | s = zbc_num_sub(&fracp, &intp, &fracp, 0); |
| 5649 | if (s) goto err; |
| 5650 | print(dig, width, radix); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5651 | s = zbc_num_mul(&frac_len, &base, &frac_len, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5652 | if (s) goto err; |
| 5653 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5654 | err: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5655 | bc_num_free(&frac_len); |
| 5656 | bc_num_free(&digit); |
| 5657 | bc_num_free(&fracp); |
| 5658 | bc_num_free(&intp); |
| 5659 | bc_vec_free(&stack); |
| 5660 | RETURN_STATUS(s); |
| 5661 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5662 | #define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5663 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5664 | static BC_STATUS zxc_num_printBase(BcNum *n) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5665 | { |
| 5666 | BcStatus s; |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5667 | size_t width; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5668 | BcNumDigitOp print; |
| 5669 | bool neg = n->neg; |
| 5670 | |
| 5671 | if (neg) { |
| 5672 | bb_putchar('-'); |
| 5673 | G.prog.nchars++; |
| 5674 | } |
| 5675 | |
| 5676 | n->neg = false; |
| 5677 | |
Denys Vlasenko | 680ccd3 | 2018-12-31 19:42:13 +0100 | [diff] [blame] | 5678 | if (G.prog.ob_t <= 16) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5679 | width = 1; |
| 5680 | print = bc_num_printHex; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5681 | } else { |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5682 | unsigned i = G.prog.ob_t - 1; |
| 5683 | width = 0; |
| 5684 | for (;;) { |
| 5685 | width++; |
| 5686 | i /= 10; |
| 5687 | if (i == 0) |
| 5688 | break; |
| 5689 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5690 | print = bc_num_printDigits; |
| 5691 | } |
| 5692 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5693 | s = zxc_num_printNum(n, G.prog.ob_t, width, print); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5694 | n->neg = neg; |
| 5695 | |
| 5696 | RETURN_STATUS(s); |
| 5697 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5698 | #define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5699 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5700 | static BC_STATUS zxc_num_print(BcNum *n, bool newline) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5701 | { |
| 5702 | BcStatus s = BC_STATUS_SUCCESS; |
| 5703 | |
| 5704 | bc_num_printNewline(); |
| 5705 | |
| 5706 | if (n->len == 0) { |
| 5707 | bb_putchar('0'); |
| 5708 | ++G.prog.nchars; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5709 | } else if (G.prog.ob_t == 10) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5710 | bc_num_printDecimal(n); |
| 5711 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5712 | s = zxc_num_printBase(n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5713 | |
| 5714 | if (newline) { |
| 5715 | bb_putchar('\n'); |
| 5716 | G.prog.nchars = 0; |
| 5717 | } |
| 5718 | |
| 5719 | RETURN_STATUS(s); |
| 5720 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5721 | #define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5722 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5723 | #if !ENABLE_DC |
| 5724 | // for bc, idx is always 0 |
| 5725 | #define xc_program_print(inst, idx) \ |
| 5726 | xc_program_print(inst) |
| 5727 | #endif |
| 5728 | static BC_STATUS xc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5729 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5730 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5731 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5732 | BcNum *num; |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5733 | IF_NOT_DC(size_t idx = 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5734 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5735 | if (!STACK_HAS_MORE_THAN(&G.prog.results, idx)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5736 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5737 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5738 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5739 | #if ENABLE_BC |
| 5740 | if (inst == XC_INST_PRINT && r->t == BC_RESULT_VOID) |
| 5741 | // void function's result on stack, ignore |
| 5742 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5743 | #endif |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5744 | s = zxc_program_num(r, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5745 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5746 | |
| 5747 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5748 | s = zxc_num_print(num, /*newline:*/ inst == XC_INST_PRINT); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5749 | #if ENABLE_BC |
| 5750 | if (!s && IS_BC) bc_num_copy(&G.prog.last, num); |
| 5751 | #endif |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5752 | } else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5753 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5754 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5755 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5756 | str = *xc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5757 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5758 | if (inst == XC_INST_PRINT_STR) { |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5759 | char *nl; |
| 5760 | G.prog.nchars += printf("%s", str); |
| 5761 | nl = strrchr(str, '\n'); |
| 5762 | if (nl) |
| 5763 | G.prog.nchars = strlen(nl + 1); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5764 | } else { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5765 | xc_program_printString(str); |
Denys Vlasenko | 266bec8 | 2019-01-02 05:03:53 +0100 | [diff] [blame] | 5766 | if (inst == XC_INST_PRINT) |
| 5767 | bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5768 | } |
| 5769 | } |
| 5770 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5771 | if (!s && inst != XC_INST_PRINT) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5772 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5773 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5774 | } |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5775 | #define zxc_program_print(...) (xc_program_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5776 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5777 | static BC_STATUS zxc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5778 | { |
| 5779 | BcStatus s; |
| 5780 | BcResult res, *ptr; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5781 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5782 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5783 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5784 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5785 | |
| 5786 | bc_num_init(&res.d.n, num->len); |
| 5787 | bc_num_copy(&res.d.n, num); |
| 5788 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5789 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5790 | xc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5791 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5792 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5793 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5794 | #define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5795 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5796 | static BC_STATUS zxc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5797 | { |
| 5798 | BcStatus s; |
| 5799 | BcResult *opd1, *opd2, res; |
| 5800 | BcNum *n1, *n2; |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5801 | ssize_t cond; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5802 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5803 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5804 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5805 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5806 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5807 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5808 | if (inst == XC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5809 | 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] | 5810 | else if (inst == XC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5811 | 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] | 5812 | else { |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5813 | cond = bc_num_cmp(n1, n2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5814 | switch (inst) { |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5815 | case XC_INST_REL_EQ: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5816 | cond = (cond == 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5817 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5818 | case XC_INST_REL_LE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5819 | cond = (cond <= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5820 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5821 | case XC_INST_REL_GE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5822 | cond = (cond >= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5823 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5824 | case XC_INST_REL_LT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5825 | cond = (cond < 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5826 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5827 | case XC_INST_REL_GT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5828 | cond = (cond > 0); |
| 5829 | break; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 5830 | default: // = case XC_INST_REL_NE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5831 | //cond = (cond != 0); - not needed |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5832 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5833 | } |
| 5834 | } |
| 5835 | |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5836 | if (cond) bc_num_one(&res.d.n); |
| 5837 | //else bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5838 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5839 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5840 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5841 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5842 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5843 | #define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5844 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5845 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5846 | static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5847 | { |
| 5848 | BcNum n2; |
| 5849 | BcResult res; |
| 5850 | |
| 5851 | memset(&n2, 0, sizeof(BcNum)); |
| 5852 | n2.rdx = res.d.id.idx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5853 | res.t = XC_RESULT_STR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5854 | |
| 5855 | if (!push) { |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5856 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5857 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5858 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5859 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5860 | } |
| 5861 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5862 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5863 | bc_vec_push(v, &n2); |
| 5864 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5865 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5866 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5867 | #define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5868 | #endif // ENABLE_DC |
| 5869 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5870 | static BC_STATUS zxc_program_popResultAndCopyToVar(char *name, BcType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5871 | { |
| 5872 | BcStatus s; |
| 5873 | BcResult *ptr, r; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5874 | BcVec *vec; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5875 | BcNum *n; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5876 | bool var = (t == BC_TYPE_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5877 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5878 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5879 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5880 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5881 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5882 | if ((ptr->t == XC_RESULT_ARRAY) == var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5883 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5884 | vec = xc_program_search(name, t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5885 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5886 | #if ENABLE_DC |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5887 | if (ptr->t == XC_RESULT_STR) { |
| 5888 | if (!var) |
| 5889 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
| 5890 | RETURN_STATUS(zdc_program_assignStr(ptr, vec, true)); |
| 5891 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5892 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5893 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 5894 | s = zxc_program_num(ptr, &n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5895 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5896 | |
| 5897 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5898 | vec = xc_program_search(name, t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5899 | |
| 5900 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5901 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5902 | bc_num_copy(&r.d.n, n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5903 | } else { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5904 | BcVec *v = (BcVec*) n; |
| 5905 | bool ref, ref_size; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5906 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5907 | ref = (v->size == sizeof(BcVec) && t != BC_TYPE_ARRAY); |
| 5908 | ref_size = (v->size == sizeof(uint8_t)); |
| 5909 | |
| 5910 | if (ref || (ref_size && t == BC_TYPE_REF)) { |
| 5911 | bc_vec_init(&r.d.v, sizeof(uint8_t), NULL); |
| 5912 | if (ref) { |
| 5913 | size_t vidx, idx; |
| 5914 | BcId id; |
| 5915 | |
| 5916 | id.name = ptr->d.id.name; |
| 5917 | v = xc_program_search(ptr->d.id.name, BC_TYPE_REF); |
| 5918 | |
| 5919 | // Make sure the pointer was not invalidated. |
| 5920 | vec = xc_program_search(name, t); |
| 5921 | |
| 5922 | vidx = bc_map_find_exact(&G.prog.arr_map, &id); |
| 5923 | //assert(vidx != BC_VEC_INVALID_IDX); |
| 5924 | vidx = ((BcId*) bc_vec_item(&G.prog.arr_map, vidx))->idx; |
| 5925 | idx = v->len - 1; |
| 5926 | |
| 5927 | bc_vec_pushIndex(&r.d.v, vidx); |
| 5928 | bc_vec_pushIndex(&r.d.v, idx); |
| 5929 | } |
| 5930 | // If we get here, we are copying a ref to a ref. |
| 5931 | else bc_vec_npush(&r.d.v, v->len, v->v); |
| 5932 | |
| 5933 | // We need to return early. |
| 5934 | goto ret; |
| 5935 | } |
| 5936 | |
| 5937 | if (ref_size && t != BC_TYPE_REF) |
| 5938 | v = xc_program_dereference(v); |
| 5939 | |
| 5940 | bc_array_init(&r.d.v, true); |
| 5941 | bc_array_copy(&r.d.v, v); |
| 5942 | } |
| 5943 | ret: |
| 5944 | bc_vec_push(vec, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5945 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5946 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5947 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5948 | } |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 5949 | #define zxc_program_popResultAndCopyToVar(...) (zxc_program_popResultAndCopyToVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5950 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5951 | static BC_STATUS zxc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5952 | { |
| 5953 | BcStatus s; |
| 5954 | BcResult *left, *right, res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 5955 | BcNum *l, *r; |
| 5956 | bool assign = (inst == XC_INST_ASSIGN); |
| 5957 | bool ib, sc; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5958 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5959 | s = zxc_program_binOpPrep(&left, &l, &right, &r, assign); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5960 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5961 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5962 | ib = left->t == XC_RESULT_IBASE; |
| 5963 | sc = left->t == XC_RESULT_SCALE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5964 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5965 | #if ENABLE_DC |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5966 | if (right->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5967 | BcVec *v; |
| 5968 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5969 | if (left->t != XC_RESULT_VAR) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5970 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 5971 | v = xc_program_search(left->d.id.name, BC_TYPE_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5972 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5973 | RETURN_STATUS(zdc_program_assignStr(right, v, false)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5974 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5975 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5976 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5977 | if (left->t == XC_RESULT_CONSTANT |
| 5978 | || left->t == XC_RESULT_TEMP |
| 5979 | IF_BC(|| left->t == BC_RESULT_VOID) |
| 5980 | ) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 5981 | RETURN_STATUS(bc_error_bad_assignment()); |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 5982 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5983 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5984 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5985 | if (assign) |
| 5986 | bc_num_copy(l, r); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5987 | else { |
| 5988 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 5989 | 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] | 5990 | } |
| 5991 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5992 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5993 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5994 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5995 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5996 | if (ib || sc || left->t == XC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5997 | static const char *const msg[] = { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 5998 | "bad ibase; must be [2,16]", //XC_RESULT_IBASE |
| 5999 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE |
| 6000 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6001 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6002 | size_t *ptr; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 6003 | size_t max; |
| 6004 | unsigned long val; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6005 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6006 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6007 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6008 | s = left->t - XC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6009 | if (sc) { |
| 6010 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6011 | ptr = &G.prog.scale; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 6012 | } else { |
| 6013 | if (val < 2) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6014 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6015 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6016 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6017 | } |
| 6018 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6019 | if (val > max) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6020 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6021 | |
| 6022 | *ptr = (size_t) val; |
| 6023 | s = BC_STATUS_SUCCESS; |
| 6024 | } |
| 6025 | |
| 6026 | bc_num_init(&res.d.n, l->len); |
| 6027 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6028 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6029 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6030 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6031 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6032 | #define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6033 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6034 | #if !ENABLE_DC |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6035 | #define xc_program_pushVar(code, bgn, pop, copy) \ |
| 6036 | xc_program_pushVar(code, bgn) |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6037 | // for bc, 'pop' and 'copy' are always false |
| 6038 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6039 | static BC_STATUS xc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6040 | bool pop, bool copy) |
| 6041 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6042 | BcResult r; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6043 | char *name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6044 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6045 | r.t = XC_RESULT_VAR; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6046 | r.d.id.name = name; |
| 6047 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6048 | #if ENABLE_DC |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6049 | if (pop || copy) { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6050 | BcVec *v = xc_program_search(name, BC_TYPE_VAR); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6051 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6052 | |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6053 | free(name); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6054 | if (!STACK_HAS_MORE_THAN(v, 1 - copy)) { |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6055 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6056 | } |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6057 | |
| 6058 | if (!BC_PROG_STR(num)) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6059 | r.t = XC_RESULT_TEMP; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6060 | bc_num_init_DEF_SIZE(&r.d.n); |
| 6061 | bc_num_copy(&r.d.n, num); |
| 6062 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6063 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 6064 | r.d.id.idx = num->rdx; |
| 6065 | } |
| 6066 | |
| 6067 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6068 | } |
| 6069 | #endif // ENABLE_DC |
| 6070 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6071 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6072 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6073 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6074 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6075 | #define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6076 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6077 | 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] | 6078 | { |
| 6079 | BcStatus s = BC_STATUS_SUCCESS; |
| 6080 | BcResult r; |
| 6081 | BcNum *num; |
| 6082 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6083 | r.d.id.name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6084 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6085 | if (inst == XC_INST_ARRAY) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6086 | r.t = XC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6087 | bc_vec_push(&G.prog.results, &r); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6088 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6089 | BcResult *operand; |
| 6090 | unsigned long temp; |
| 6091 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6092 | s = zxc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6093 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6094 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6095 | if (s) goto err; |
| 6096 | |
| 6097 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6098 | 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] | 6099 | goto err; |
| 6100 | } |
| 6101 | |
| 6102 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6103 | xc_program_retire(&r, XC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6104 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6105 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6106 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6107 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6108 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6109 | #define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6110 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6111 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6112 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6113 | { |
| 6114 | BcStatus s; |
| 6115 | BcResult *ptr, res, copy; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6116 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6117 | char inst2 = inst; |
| 6118 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6119 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6120 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6121 | |
| 6122 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6123 | copy.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6124 | bc_num_init(©.d.n, num->len); |
| 6125 | bc_num_copy(©.d.n, num); |
| 6126 | } |
| 6127 | |
| 6128 | res.t = BC_RESULT_ONE; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6129 | inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST) |
| 6130 | ? BC_INST_ASSIGN_PLUS |
| 6131 | : BC_INST_ASSIGN_MINUS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6132 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6133 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6134 | s = zxc_program_assign(inst); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6135 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6136 | |
| 6137 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 6138 | bc_result_pop_and_push(©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6139 | } |
| 6140 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6141 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6142 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6143 | #define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6144 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6145 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6146 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6147 | BcInstPtr ip; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6148 | size_t i, nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6149 | BcId *a; |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 6150 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6151 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6152 | nparams = xc_program_index(code, idx); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6153 | ip.func = xc_program_index(code, idx); |
| 6154 | func = xc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6155 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6156 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6157 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6158 | } |
| 6159 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6160 | 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] | 6161 | } |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 6162 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6163 | |
| 6164 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 6165 | BcResult *arg; |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6166 | BcStatus s; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6167 | bool arr; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6168 | |
| 6169 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6170 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6171 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6172 | arr = (a->idx == BC_TYPE_ARRAY || a->idx == BC_TYPE_REF); |
| 6173 | |
| 6174 | if (arr != (arg->t == XC_RESULT_ARRAY) // array/variable mismatch |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 6175 | // || arg->t == XC_RESULT_STR - impossible, f("str") is not a legal syntax (strings are not bc expressions) |
| 6176 | ) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6177 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 19c3eb0 | 2019-01-04 00:05:07 +0100 | [diff] [blame] | 6178 | } |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6179 | s = zxc_program_popResultAndCopyToVar(a->name, (BcType) a->idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6180 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6181 | } |
| 6182 | |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6183 | a = bc_vec_item(&func->autos, i); |
| 6184 | for (; i < func->autos.len; i++, a++) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6185 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6186 | |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6187 | v = xc_program_search(a->name, (BcType) a->idx); |
| 6188 | if (a->idx == BC_TYPE_VAR) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 6189 | BcNum n2; |
| 6190 | bc_num_init_DEF_SIZE(&n2); |
| 6191 | bc_vec_push(v, &n2); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6192 | } else { |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6193 | //assert(a->idx == BC_TYPE_ARRAY); |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 6194 | BcVec v2; |
| 6195 | bc_array_init(&v2, true); |
| 6196 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6197 | } |
| 6198 | } |
| 6199 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6200 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6201 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6202 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6203 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6204 | #define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6205 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6206 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6207 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6208 | BcResult res; |
| 6209 | BcFunc *f; |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 6210 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6211 | size_t i; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6212 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6213 | |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 6214 | f = xc_program_func(ip->func); |
| 6215 | |
| 6216 | res.t = XC_RESULT_TEMP; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6217 | if (inst == XC_INST_RET) { |
Denys Vlasenko | 1db367a | 2019-01-04 06:18:00 +0100 | [diff] [blame] | 6218 | // bc needs this for e.g. RESULT_CONSTANT ("return 5") |
| 6219 | // because bc constants are per-function. |
| 6220 | // TODO: maybe avoid if value is already RESULT_TEMP? |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6221 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6222 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6223 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6224 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6225 | s = zxc_program_num(operand, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6226 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6227 | bc_num_init(&res.d.n, num->len); |
| 6228 | bc_num_copy(&res.d.n, num); |
Denys Vlasenko | 377cc97 | 2019-01-04 00:34:52 +0100 | [diff] [blame] | 6229 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6230 | } else { |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 6231 | if (f->voidfunc) |
| 6232 | res.t = BC_RESULT_VOID; |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6233 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6234 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6235 | } |
Denys Vlasenko | 02c3d7a | 2019-01-04 00:21:29 +0100 | [diff] [blame] | 6236 | bc_vec_push(&G.prog.results, &res); |
| 6237 | |
| 6238 | bc_vec_pop(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6239 | |
| 6240 | // 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] | 6241 | a = (void*)f->autos.v; |
| 6242 | for (i = 0; i < f->autos.len; i++, a++) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6243 | BcVec *v; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6244 | v = xc_program_search(a->name, (BcType) a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6245 | bc_vec_pop(v); |
| 6246 | } |
| 6247 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6248 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6249 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6250 | #define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6251 | #endif // ENABLE_BC |
| 6252 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6253 | static unsigned long xc_program_scale(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6254 | { |
| 6255 | return (unsigned long) n->rdx; |
| 6256 | } |
| 6257 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6258 | static unsigned long xc_program_len(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6259 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6260 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6261 | |
Denys Vlasenko | ace81cd | 2021-02-26 14:05:28 +0100 | [diff] [blame^] | 6262 | if (n->rdx != len) |
| 6263 | // length(100): rdx 0 len 3, return 3 |
| 6264 | // length(0.01-0.01): rdx 2 len 0, return 2 |
| 6265 | // dc: 0.01 0.01 - Zp: rdx 2 len 0, return 1 |
| 6266 | return len != 0 ? len : (IS_BC ? n->rdx : 1); |
| 6267 | |
| 6268 | // length(0): return 1 |
| 6269 | // length(0.000nnn): count nnn |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6270 | for (;;) { |
| 6271 | if (len == 0) break; |
| 6272 | len--; |
| 6273 | if (n->num[len] != 0) break; |
| 6274 | } |
Denys Vlasenko | ace81cd | 2021-02-26 14:05:28 +0100 | [diff] [blame^] | 6275 | return len + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6276 | } |
| 6277 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6278 | static BC_STATUS zxc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6279 | { |
| 6280 | BcStatus s; |
| 6281 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6282 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6283 | BcResult res; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6284 | bool len = (inst == XC_INST_LENGTH); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6285 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6286 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6287 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6288 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6289 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6290 | s = zxc_program_num(opnd, &num); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6291 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6292 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6293 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6294 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6295 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6296 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6297 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6298 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6299 | |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6300 | if (inst == XC_INST_SQRT) |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6301 | s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6302 | #if ENABLE_BC |
Denys Vlasenko | ace81cd | 2021-02-26 14:05:28 +0100 | [diff] [blame^] | 6303 | else if (len && opnd->t == XC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6304 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6305 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6306 | #endif |
| 6307 | #if ENABLE_DC |
Denys Vlasenko | ace81cd | 2021-02-26 14:05:28 +0100 | [diff] [blame^] | 6308 | else if (len && !BC_PROG_NUM(opnd, num)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6309 | char **str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6310 | 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] | 6311 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6312 | str = xc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6313 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6314 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6315 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6316 | else { |
Denys Vlasenko | ace81cd | 2021-02-26 14:05:28 +0100 | [diff] [blame^] | 6317 | //TODO: length(.00) and scale(.00) should return 2, they return 1 and 0 now |
| 6318 | //(don't forget to check that dc Z and X commands do not break) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6319 | 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] | 6320 | } |
| 6321 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6322 | xc_program_retire(&res, XC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6323 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6324 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6325 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6326 | #define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6327 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6328 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6329 | static BC_STATUS zdc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6330 | { |
| 6331 | BcStatus s; |
| 6332 | BcResult *opd1, *opd2, res, res2; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6333 | BcNum *n1, *n2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6334 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6335 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6336 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6337 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6338 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6339 | bc_num_init(&res2.d.n, n2->len); |
| 6340 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6341 | 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] | 6342 | if (s) goto err; |
| 6343 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6344 | xc_program_binOpRetire(&res2); |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6345 | res.t = XC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6346 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6347 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6348 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6349 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6350 | bc_num_free(&res2.d.n); |
| 6351 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6352 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6353 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6354 | #define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6355 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6356 | static BC_STATUS zdc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6357 | { |
| 6358 | BcStatus s; |
| 6359 | BcResult *r1, *r2, *r3, res; |
| 6360 | BcNum *n1, *n2, *n3; |
| 6361 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6362 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 2)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6363 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6364 | s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6365 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6366 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6367 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6368 | s = zxc_program_num(r1, &n1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6369 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6370 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6371 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6372 | |
| 6373 | // Make sure that the values have their pointers updated, if necessary. |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6374 | if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6375 | if (r1->t == r2->t) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6376 | s = zxc_program_num(r2, &n2); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6377 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6378 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6379 | if (r1->t == r3->t) { |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6380 | s = zxc_program_num(r3, &n3); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6381 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6382 | } |
| 6383 | } |
| 6384 | |
| 6385 | bc_num_init(&res.d.n, n3->len); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6386 | s = zdc_num_modexp(n1, n2, n3, &res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6387 | if (s) goto err; |
| 6388 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6389 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6390 | xc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6391 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6392 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6393 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6394 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6395 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6396 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6397 | #define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6398 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6399 | static void dc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6400 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6401 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6402 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6403 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6404 | res.t = XC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6405 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6406 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6407 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6408 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6409 | } |
| 6410 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6411 | static BC_STATUS zdc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6412 | { |
| 6413 | BcStatus s; |
| 6414 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6415 | BcNum *num, n; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6416 | char **strs; |
| 6417 | char *str; |
| 6418 | char c; |
| 6419 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6420 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6421 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6422 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6423 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 6424 | r = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6425 | s = zxc_program_num(r, &num); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6426 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6427 | |
| 6428 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 6429 | unsigned long val; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6430 | BcNum strmb; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6431 | BcDig strmb_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6432 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6433 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6434 | bc_num_copy(&n, num); |
| 6435 | bc_num_truncate(&n, n.rdx); |
| 6436 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6437 | strmb.cap = ARRAY_SIZE(strmb_digs); |
| 6438 | strmb.num = strmb_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6439 | bc_num_ulong2num(&strmb, 0x100); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6440 | |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 6441 | s = zbc_num_mod(&n, &strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6442 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6443 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6444 | if (s) goto num_err; |
| 6445 | |
| 6446 | c = (char) val; |
| 6447 | |
| 6448 | bc_num_free(&n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6449 | } else { |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6450 | char *sp; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6451 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6452 | sp = *xc_program_str(idx); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6453 | c = sp[0]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6454 | } |
| 6455 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6456 | strs = (void*)G.prog.strs.v; |
| 6457 | for (idx = 0; idx < G.prog.strs.len; idx++) { |
| 6458 | if (strs[idx][0] == c && strs[idx][1] == '\0') { |
| 6459 | goto dup; |
| 6460 | } |
| 6461 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6462 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6463 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6464 | //str[1] = '\0'; - already is |
Brian Foley | b64470b | 2019-09-05 10:50:13 +0200 | [diff] [blame] | 6465 | idx = bc_vec_push(&G.prog.strs, &str); |
| 6466 | // Add an empty function so that if zdc_program_execStr ever needs to |
| 6467 | // parse the string into code (from the 'x' command) there's somewhere |
| 6468 | // to store the bytecode. |
| 6469 | xc_program_add_fn(); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6470 | dup: |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6471 | res.t = XC_RESULT_STR; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6472 | res.d.id.idx = idx; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 6473 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6474 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6475 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6476 | num_err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6477 | bc_num_free(&n); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6478 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6479 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6480 | #define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6481 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6482 | static BC_STATUS zdc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6483 | { |
| 6484 | BcStatus s; |
| 6485 | BcResult *r; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6486 | BcNum *n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6487 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6488 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6489 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6490 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6491 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6492 | |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6493 | s = zxc_program_num(r, &n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6494 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6495 | |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6496 | if (BC_PROG_NUM(r, n)) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6497 | s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar); |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6498 | } else { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6499 | char *str; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6500 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6501 | str = *xc_program_str(idx); |
Ron Yorston | cad3fc7 | 2021-02-03 20:47:14 +0100 | [diff] [blame] | 6502 | fputs_stdout(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6503 | } |
| 6504 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6505 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6506 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6507 | #define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6508 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6509 | static BC_STATUS zdc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6510 | { |
| 6511 | BcStatus s; |
| 6512 | BcResult *opnd; |
Denys Vlasenko | 65b6fe0 | 2018-12-24 17:15:34 +0100 | [diff] [blame] | 6513 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6514 | unsigned long val; |
| 6515 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6516 | s = zxc_program_prep(&opnd, &num); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6517 | if (s) RETURN_STATUS(s); |
| 6518 | s = zbc_num_ulong(num, &val); |
| 6519 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6520 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6521 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6522 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6523 | if (G.prog.exestack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6524 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6525 | if (G.prog.exestack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6526 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6527 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6528 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6529 | bc_vec_npop(&G.prog.exestack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6530 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6531 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6532 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6533 | #define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6534 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6535 | 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] | 6536 | { |
| 6537 | BcStatus s = BC_STATUS_SUCCESS; |
| 6538 | BcResult *r; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6539 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6540 | BcInstPtr ip; |
| 6541 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6542 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6543 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6544 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6545 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6546 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6547 | |
| 6548 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6549 | BcNum *n = n; // for compiler |
| 6550 | bool exec; |
| 6551 | char *name; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6552 | char *then_name = xc_program_name(code, bgn); |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6553 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6554 | |
Denys Vlasenko | f706a18 | 2018-12-26 20:02:09 +0100 | [diff] [blame] | 6555 | if (code[*bgn] == '\0') |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6556 | (*bgn) += 1; |
| 6557 | else |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6558 | else_name = xc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6559 | |
| 6560 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6561 | name = then_name; |
| 6562 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6563 | exec = true; |
| 6564 | name = else_name; |
| 6565 | } |
| 6566 | |
| 6567 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6568 | BcVec *v; |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6569 | v = xc_program_search(name, BC_TYPE_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6570 | n = bc_vec_top(v); |
| 6571 | } |
| 6572 | |
| 6573 | free(then_name); |
| 6574 | free(else_name); |
| 6575 | |
| 6576 | if (!exec) goto exit; |
| 6577 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6578 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6579 | goto exit; |
| 6580 | } |
| 6581 | |
| 6582 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6583 | } else { |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6584 | if (r->t == XC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6585 | sidx = r->d.id.idx; |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6586 | } else if (r->t == XC_RESULT_VAR) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6587 | BcNum *n; |
Denys Vlasenko | 374d2c4 | 2018-12-29 14:52:30 +0100 | [diff] [blame] | 6588 | s = zxc_program_num(r, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6589 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6590 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6591 | } else |
Brian Foley | 7454879 | 2019-09-05 10:46:22 +0200 | [diff] [blame] | 6592 | goto exit_nopop; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6593 | } |
| 6594 | |
| 6595 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6596 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6597 | f = xc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6598 | |
| 6599 | if (f->code.len == 0) { |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6600 | BcParse sv_parse; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6601 | char *str; |
| 6602 | |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6603 | sv_parse = G.prs; // struct copy |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6604 | xc_parse_create(fidx); |
| 6605 | str = *xc_program_str(sidx); |
| 6606 | s = zxc_parse_text_init(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6607 | if (s) goto err; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6608 | |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6609 | s = zdc_parse_exprs_until_eof(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6610 | if (s) goto err; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6611 | xc_parse_push(DC_INST_POP_EXEC); |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6612 | if (G.prs.lex != XC_LEX_EOF) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6613 | s = bc_error_bad_expression(); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6614 | xc_parse_free(); |
Denys Vlasenko | 1fbe35a | 2018-12-25 19:38:13 +0100 | [diff] [blame] | 6615 | G.prs = sv_parse; // struct copy |
| 6616 | if (s) { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6617 | err: |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6618 | bc_vec_pop_all(&f->code); |
| 6619 | goto exit; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6620 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6621 | } |
| 6622 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6623 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6624 | ip.func = fidx; |
| 6625 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6626 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6627 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6628 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6629 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6630 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6631 | bc_vec_pop(&G.prog.results); |
Brian Foley | 7454879 | 2019-09-05 10:46:22 +0200 | [diff] [blame] | 6632 | exit_nopop: |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6633 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6634 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6635 | #define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6636 | #endif // ENABLE_DC |
| 6637 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6638 | static void xc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6639 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6640 | BcResult res; |
| 6641 | unsigned long val; |
| 6642 | |
Denys Vlasenko | d897c9a | 2018-12-24 23:41:31 +0100 | [diff] [blame] | 6643 | res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6644 | if (inst == XC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6645 | val = (unsigned long) G.prog.ib_t; |
Denys Vlasenko | a7732d1 | 2018-12-24 04:26:07 +0100 | [diff] [blame] | 6646 | else if (inst == XC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6647 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6648 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6649 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6650 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6651 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6652 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6653 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6654 | } |
| 6655 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6656 | static BC_STATUS zxc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6657 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6658 | BcResult r, *ptr; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6659 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6660 | BcFunc *func = xc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6661 | char *code = func->code.v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6662 | |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6663 | dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d", |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6664 | ip->func, func->code.len, ip->inst_idx, G.prog.results.len); |
| 6665 | while (ip->inst_idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6666 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6667 | char inst = code[ip->inst_idx++]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6668 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6669 | 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] | 6670 | switch (inst) { |
Denys Vlasenko | 1db367a | 2019-01-04 06:18:00 +0100 | [diff] [blame] | 6671 | case XC_INST_RET: |
| 6672 | if (IS_DC) { // end of '?' reached |
| 6673 | bc_vec_pop(&G.prog.exestack); |
| 6674 | goto read_updated_ip; |
| 6675 | } |
| 6676 | // bc: fall through |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6677 | #if ENABLE_BC |
Denys Vlasenko | 1db367a | 2019-01-04 06:18:00 +0100 | [diff] [blame] | 6678 | case BC_INST_RET0: |
| 6679 | dbg_exec("BC_INST_RET[0]:"); |
| 6680 | s = zbc_program_return(inst); |
| 6681 | goto read_updated_ip; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6682 | case BC_INST_JUMP_ZERO: { |
| 6683 | BcNum *num; |
| 6684 | bool zero; |
| 6685 | dbg_exec("BC_INST_JUMP_ZERO:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6686 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6687 | if (s) RETURN_STATUS(s); |
| 6688 | zero = (bc_num_cmp(num, &G.prog.zero) == 0); |
| 6689 | bc_vec_pop(&G.prog.results); |
| 6690 | if (!zero) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6691 | xc_program_index(code, &ip->inst_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6692 | break; |
| 6693 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6694 | // else: fall through |
| 6695 | } |
| 6696 | case BC_INST_JUMP: { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6697 | size_t idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6698 | size_t *addr = bc_vec_item(&func->labels, idx); |
| 6699 | dbg_exec("BC_INST_JUMP: to %ld", (long)*addr); |
| 6700 | ip->inst_idx = *addr; |
| 6701 | break; |
| 6702 | } |
| 6703 | case BC_INST_CALL: |
| 6704 | dbg_exec("BC_INST_CALL:"); |
| 6705 | s = zbc_program_call(code, &ip->inst_idx); |
| 6706 | goto read_updated_ip; |
| 6707 | case BC_INST_INC_PRE: |
| 6708 | case BC_INST_DEC_PRE: |
| 6709 | case BC_INST_INC_POST: |
| 6710 | case BC_INST_DEC_POST: |
| 6711 | dbg_exec("BC_INST_INCDEC:"); |
| 6712 | s = zbc_program_incdec(inst); |
| 6713 | break; |
| 6714 | case BC_INST_HALT: |
| 6715 | dbg_exec("BC_INST_HALT:"); |
| 6716 | QUIT_OR_RETURN_TO_MAIN; |
| 6717 | break; |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6718 | case XC_INST_BOOL_OR: |
| 6719 | case XC_INST_BOOL_AND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6720 | #endif // ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6721 | case XC_INST_REL_EQ: |
| 6722 | case XC_INST_REL_LE: |
| 6723 | case XC_INST_REL_GE: |
| 6724 | case XC_INST_REL_NE: |
| 6725 | case XC_INST_REL_LT: |
| 6726 | case XC_INST_REL_GT: |
| 6727 | dbg_exec("BC_INST_BOOL:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6728 | s = zxc_program_logical(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6729 | break; |
| 6730 | case XC_INST_READ: |
| 6731 | dbg_exec("XC_INST_READ:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6732 | s = zxc_program_read(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6733 | goto read_updated_ip; |
| 6734 | case XC_INST_VAR: |
| 6735 | dbg_exec("XC_INST_VAR:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6736 | s = zxc_program_pushVar(code, &ip->inst_idx, false, false); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6737 | break; |
| 6738 | case XC_INST_ARRAY_ELEM: |
| 6739 | case XC_INST_ARRAY: |
| 6740 | dbg_exec("XC_INST_ARRAY[_ELEM]:"); |
| 6741 | s = zbc_program_pushArray(code, &ip->inst_idx, inst); |
| 6742 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6743 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6744 | case BC_INST_LAST: |
| 6745 | dbg_exec("BC_INST_LAST:"); |
| 6746 | r.t = BC_RESULT_LAST; |
| 6747 | bc_vec_push(&G.prog.results, &r); |
| 6748 | break; |
Denys Vlasenko | ad0bd38 | 2018-12-24 00:50:32 +0100 | [diff] [blame] | 6749 | #endif |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6750 | case XC_INST_IBASE: |
| 6751 | case XC_INST_OBASE: |
| 6752 | case XC_INST_SCALE: |
| 6753 | dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6754 | xc_program_pushGlobal(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6755 | break; |
| 6756 | case XC_INST_SCALE_FUNC: |
| 6757 | case XC_INST_LENGTH: |
| 6758 | case XC_INST_SQRT: |
| 6759 | dbg_exec("BC_INST_builtin:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6760 | s = zxc_program_builtin(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6761 | break; |
| 6762 | case XC_INST_NUM: |
| 6763 | dbg_exec("XC_INST_NUM:"); |
| 6764 | r.t = XC_RESULT_CONSTANT; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6765 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6766 | bc_vec_push(&G.prog.results, &r); |
| 6767 | break; |
| 6768 | case XC_INST_POP: |
| 6769 | dbg_exec("XC_INST_POP:"); |
| 6770 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
| 6771 | s = bc_error_stack_has_too_few_elements(); |
| 6772 | else |
| 6773 | bc_vec_pop(&G.prog.results); |
| 6774 | break; |
| 6775 | case XC_INST_PRINT: |
| 6776 | case XC_INST_PRINT_POP: |
| 6777 | case XC_INST_PRINT_STR: |
Denys Vlasenko | 54f5c1d | 2019-01-04 13:58:46 +0100 | [diff] [blame] | 6778 | dbg_exec("XC_INST_PRINTxyz(%d):", inst - XC_INST_PRINT); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6779 | s = zxc_program_print(inst, 0); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6780 | break; |
| 6781 | case XC_INST_STR: |
| 6782 | dbg_exec("XC_INST_STR:"); |
| 6783 | r.t = XC_RESULT_STR; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6784 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6785 | bc_vec_push(&G.prog.results, &r); |
| 6786 | break; |
| 6787 | case XC_INST_POWER: |
| 6788 | case XC_INST_MULTIPLY: |
| 6789 | case XC_INST_DIVIDE: |
| 6790 | case XC_INST_MODULUS: |
| 6791 | case XC_INST_PLUS: |
| 6792 | case XC_INST_MINUS: |
| 6793 | dbg_exec("BC_INST_binaryop:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6794 | s = zxc_program_op(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6795 | break; |
| 6796 | case XC_INST_BOOL_NOT: { |
| 6797 | BcNum *num; |
| 6798 | dbg_exec("XC_INST_BOOL_NOT:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6799 | s = zxc_program_prep(&ptr, &num); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6800 | if (s) RETURN_STATUS(s); |
| 6801 | bc_num_init_DEF_SIZE(&r.d.n); |
| 6802 | if (bc_num_cmp(num, &G.prog.zero) == 0) |
| 6803 | bc_num_one(&r.d.n); |
| 6804 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6805 | xc_program_retire(&r, XC_RESULT_TEMP); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6806 | break; |
| 6807 | } |
| 6808 | case XC_INST_NEG: |
| 6809 | dbg_exec("XC_INST_NEG:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6810 | s = zxc_program_negate(); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6811 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6812 | #if ENABLE_BC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6813 | case BC_INST_ASSIGN_POWER: |
| 6814 | case BC_INST_ASSIGN_MULTIPLY: |
| 6815 | case BC_INST_ASSIGN_DIVIDE: |
| 6816 | case BC_INST_ASSIGN_MODULUS: |
| 6817 | case BC_INST_ASSIGN_PLUS: |
| 6818 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6819 | #endif |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6820 | case XC_INST_ASSIGN: |
| 6821 | dbg_exec("BC_INST_ASSIGNxyz:"); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6822 | s = zxc_program_assign(inst); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6823 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6824 | #if ENABLE_DC |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6825 | case DC_INST_POP_EXEC: |
| 6826 | dbg_exec("DC_INST_POP_EXEC:"); |
| 6827 | bc_vec_pop(&G.prog.exestack); |
| 6828 | goto read_updated_ip; |
| 6829 | case DC_INST_MODEXP: |
| 6830 | dbg_exec("DC_INST_MODEXP:"); |
| 6831 | s = zdc_program_modexp(); |
| 6832 | break; |
| 6833 | case DC_INST_DIVMOD: |
| 6834 | dbg_exec("DC_INST_DIVMOD:"); |
| 6835 | s = zdc_program_divmod(); |
| 6836 | break; |
| 6837 | case DC_INST_EXECUTE: |
| 6838 | case DC_INST_EXEC_COND: |
| 6839 | dbg_exec("DC_INST_EXEC[_COND]:"); |
| 6840 | s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND); |
| 6841 | goto read_updated_ip; |
| 6842 | case DC_INST_PRINT_STACK: { |
| 6843 | size_t idx; |
| 6844 | dbg_exec("DC_INST_PRINT_STACK:"); |
| 6845 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6846 | s = zxc_program_print(XC_INST_PRINT, idx); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6847 | if (s) break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6848 | } |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6849 | break; |
| 6850 | } |
| 6851 | case DC_INST_CLEAR_STACK: |
| 6852 | dbg_exec("DC_INST_CLEAR_STACK:"); |
| 6853 | bc_vec_pop_all(&G.prog.results); |
| 6854 | break; |
| 6855 | case DC_INST_STACK_LEN: |
| 6856 | dbg_exec("DC_INST_STACK_LEN:"); |
| 6857 | dc_program_stackLen(); |
| 6858 | break; |
| 6859 | case DC_INST_DUPLICATE: |
| 6860 | dbg_exec("DC_INST_DUPLICATE:"); |
| 6861 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
| 6862 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| 6863 | ptr = bc_vec_top(&G.prog.results); |
| 6864 | dc_result_copy(&r, ptr); |
| 6865 | bc_vec_push(&G.prog.results, &r); |
| 6866 | break; |
| 6867 | case DC_INST_SWAP: { |
| 6868 | BcResult *ptr2; |
| 6869 | dbg_exec("DC_INST_SWAP:"); |
| 6870 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
| 6871 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| 6872 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6873 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
| 6874 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6875 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6876 | memcpy(ptr2, &r, sizeof(BcResult)); |
| 6877 | break; |
| 6878 | } |
| 6879 | case DC_INST_ASCIIFY: |
| 6880 | dbg_exec("DC_INST_ASCIIFY:"); |
| 6881 | s = zdc_program_asciify(); |
| 6882 | break; |
| 6883 | case DC_INST_PRINT_STREAM: |
| 6884 | dbg_exec("DC_INST_PRINT_STREAM:"); |
| 6885 | s = zdc_program_printStream(); |
| 6886 | break; |
| 6887 | case DC_INST_LOAD: |
| 6888 | case DC_INST_PUSH_VAR: { |
| 6889 | bool copy = inst == DC_INST_LOAD; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6890 | s = zxc_program_pushVar(code, &ip->inst_idx, true, copy); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6891 | break; |
| 6892 | } |
| 6893 | case DC_INST_PUSH_TO_VAR: { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6894 | char *name = xc_program_name(code, &ip->inst_idx); |
Denys Vlasenko | 5379950 | 2019-01-25 14:24:03 +0100 | [diff] [blame] | 6895 | s = zxc_program_popResultAndCopyToVar(name, BC_TYPE_VAR); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6896 | free(name); |
| 6897 | break; |
| 6898 | } |
| 6899 | case DC_INST_QUIT: |
| 6900 | dbg_exec("DC_INST_QUIT:"); |
| 6901 | if (G.prog.exestack.len <= 2) |
| 6902 | QUIT_OR_RETURN_TO_MAIN; |
| 6903 | bc_vec_npop(&G.prog.exestack, 2); |
| 6904 | goto read_updated_ip; |
| 6905 | case DC_INST_NQUIT: |
| 6906 | dbg_exec("DC_INST_NQUIT:"); |
| 6907 | s = zdc_program_nquit(); |
| 6908 | //goto read_updated_ip; - just fall through to it |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6909 | #endif // ENABLE_DC |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6910 | read_updated_ip: |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6911 | // Instruction stack has changed, read new pointers |
| 6912 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6913 | func = xc_program_func(ip->func); |
Denys Vlasenko | 1c69ec1 | 2018-12-26 19:24:15 +0100 | [diff] [blame] | 6914 | code = func->code.v; |
| 6915 | 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] | 6916 | } |
| 6917 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6918 | if (s || G_interrupt) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6919 | xc_program_reset(); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6920 | RETURN_STATUS(s); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6921 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6922 | |
Denys Vlasenko | c5774a3 | 2018-12-17 01:22:53 +0100 | [diff] [blame] | 6923 | fflush_and_check(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6924 | } |
| 6925 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6926 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6927 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6928 | #define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6929 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6930 | static BC_STATUS zxc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6931 | { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6932 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6933 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6934 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6935 | s = zxc_parse_text_init(text); // does the first zxc_lex_next() |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6936 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6937 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6938 | while (G.prs.lex != XC_LEX_EOF) { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6939 | BcInstPtr *ip; |
| 6940 | BcFunc *f; |
| 6941 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6942 | 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] | 6943 | if (IS_BC) { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6944 | #if ENABLE_BC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6945 | s = zbc_parse_stmt_or_funcdef(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6946 | if (s) goto err; |
| 6947 | |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6948 | // Check that next token is a correct stmt delimiter - |
| 6949 | // disallows "print 1 print 2" and such. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 6950 | if (G.prs.lex != BC_LEX_SCOLON |
| 6951 | && G.prs.lex != XC_LEX_NLINE |
| 6952 | && G.prs.lex != XC_LEX_EOF |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6953 | ) { |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 6954 | bc_error_at("bad statement terminator"); |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6955 | goto err; |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6956 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6957 | // The above logic is fragile. Check these examples: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6958 | // - interactive read() still works |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6959 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6960 | } else { |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6961 | #if ENABLE_DC |
Denys Vlasenko | a2e62e3 | 2018-12-25 20:40:55 +0100 | [diff] [blame] | 6962 | s = zdc_parse_expr(); |
Denys Vlasenko | 2638454 | 2018-12-25 18:37:52 +0100 | [diff] [blame] | 6963 | #endif |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6964 | } |
| 6965 | if (s || G_interrupt) { |
Denys Vlasenko | 9471bd4 | 2018-12-23 00:13:15 +0100 | [diff] [blame] | 6966 | err: |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6967 | xc_parse_reset(); // includes xc_program_reset() |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6968 | RETURN_STATUS(BC_STATUS_FAILURE); |
| 6969 | } |
Denys Vlasenko | 53e569c | 2018-12-25 19:37:23 +0100 | [diff] [blame] | 6970 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 6971 | dbg_lex("%s:%d executing...", __func__, __LINE__); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6972 | s = zxc_program_exec(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6973 | if (s) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6974 | xc_program_reset(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6975 | break; |
| 6976 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6977 | |
| 6978 | ip = (void*)G.prog.exestack.v; |
| 6979 | #if SANITY_CHECKS |
| 6980 | if (G.prog.exestack.len != 1) // should have only main's IP |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 6981 | bb_simple_error_msg_and_die("BUG:call stack"); |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6982 | if (ip->func != BC_PROG_MAIN) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 6983 | bb_simple_error_msg_and_die("BUG:not MAIN"); |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6984 | #endif |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 6985 | f = xc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6986 | // bc discards strings, constants and code after each |
| 6987 | // top-level statement in the "main program". |
| 6988 | // This prevents "yes 1 | bc" from growing its memory |
| 6989 | // without bound. This can be done because data stack |
| 6990 | // is empty and thus can't hold any references to |
| 6991 | // strings or constants, there is no generated code |
| 6992 | // which can hold references (after we discard one |
| 6993 | // we just executed). Code of functions can have references, |
| 6994 | // but bc stores function strings/constants in per-function |
| 6995 | // storage. |
| 6996 | if (IS_BC) { |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6997 | #if SANITY_CHECKS |
Denys Vlasenko | 7c1c9dc | 2018-12-22 14:18:47 +0100 | [diff] [blame] | 6998 | if (G.prog.results.len != 0) // should be empty |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 6999 | bb_simple_error_msg_and_die("BUG:data stack"); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 7000 | #endif |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7001 | IF_BC(bc_vec_pop_all(&f->strs);) |
| 7002 | IF_BC(bc_vec_pop_all(&f->consts);) |
Denys Vlasenko | fc7aa7a | 2019-01-08 18:08:48 +0100 | [diff] [blame] | 7003 | // We are at SCOLON/NLINE, skip it: |
| 7004 | s = zxc_lex_next(); |
| 7005 | if (s) goto err; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 7006 | } else { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 7007 | if (G.prog.results.len == 0 |
| 7008 | && G.prog.vars.len == 0 |
| 7009 | ) { |
| 7010 | // If stack is empty and no registers exist (TODO: or they are all empty), |
| 7011 | // we can get rid of accumulated strings and constants. |
| 7012 | // In this example dc process should not grow |
| 7013 | // its memory consumption with time: |
| 7014 | // yes 1pc | dc |
| 7015 | IF_DC(bc_vec_pop_all(&G.prog.strs);) |
| 7016 | IF_DC(bc_vec_pop_all(&G.prog.consts);) |
| 7017 | } |
| 7018 | // The code is discarded always (below), thus this example |
| 7019 | // should also not grow its memory consumption with time, |
| 7020 | // even though its data stack is not empty: |
| 7021 | // { echo 1; yes dk; } | dc |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7022 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 7023 | // We drop generated and executed code for both bc and dc: |
| 7024 | bc_vec_pop_all(&f->code); |
| 7025 | ip->inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7026 | } |
Denys Vlasenko | 6842c60 | 2019-01-04 05:41:47 +0100 | [diff] [blame] | 7027 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 7028 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7029 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7030 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7031 | #define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7032 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7033 | static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7034 | { |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 7035 | // 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] | 7036 | // therefore we know G.prs.lex_filename == NULL on entry |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 7037 | //const char *sv_file; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7038 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7039 | |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 7040 | G.prs.lex_filename = filename; |
| 7041 | G.prs.lex_input_fp = fp; |
Denys Vlasenko | 7d32e25 | 2018-12-26 18:32:43 +0100 | [diff] [blame] | 7042 | G.err_line = G.prs.lex_line = 1; |
Denys Vlasenko | 2231468 | 2019-01-01 21:50:14 +0100 | [diff] [blame] | 7043 | dbg_lex("p->lex_line reset to 1"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7044 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7045 | do { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7046 | s = zxc_vm_process(""); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7047 | // We do not stop looping on errors here if reading stdin. |
| 7048 | // Example: start interactive bc and enter "return". |
| 7049 | // It should say "'return' not in a function" |
| 7050 | // but should not exit. |
Denys Vlasenko | ecb62ed | 2018-12-25 22:32:41 +0100 | [diff] [blame] | 7051 | } while (G.prs.lex_input_fp == stdin); |
| 7052 | G.prs.lex_filename = NULL; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7053 | RETURN_STATUS(s); |
| 7054 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7055 | #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] | 7056 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7057 | static BC_STATUS zxc_vm_file(const char *file) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7058 | { |
| 7059 | BcStatus s; |
| 7060 | FILE *fp; |
| 7061 | |
| 7062 | fp = xfopen_for_read(file); |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7063 | s = zxc_vm_execute_FILE(fp, file); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7064 | fclose(fp); |
| 7065 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7066 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7067 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7068 | #define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7069 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7070 | #if ENABLE_BC |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 7071 | static void bc_vm_info(void) |
| 7072 | { |
| 7073 | printf("%s "BB_VER"\n" |
Gavin Howard | fa495ce | 2018-12-18 10:03:14 -0700 | [diff] [blame] | 7074 | "Adapted from https://github.com/gavinhoward/bc\n" |
| 7075 | "Original code (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 7076 | , applet_name); |
| 7077 | } |
| 7078 | |
| 7079 | static void bc_args(char **argv) |
| 7080 | { |
| 7081 | unsigned opts; |
| 7082 | int i; |
| 7083 | |
| 7084 | GETOPT_RESET(); |
| 7085 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
| 7086 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
| 7087 | "warn\0" No_argument "w" |
| 7088 | "version\0" No_argument "v" |
| 7089 | "standard\0" No_argument "s" |
| 7090 | "quiet\0" No_argument "q" |
| 7091 | "mathlib\0" No_argument "l" |
| 7092 | "interactive\0" No_argument "i" |
| 7093 | ); |
| 7094 | #else |
| 7095 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
| 7096 | #endif |
| 7097 | if (getenv("POSIXLY_CORRECT")) |
| 7098 | option_mask32 |= BC_FLAG_S; |
| 7099 | |
| 7100 | if (opts & BC_FLAG_V) { |
| 7101 | bc_vm_info(); |
| 7102 | exit(0); |
| 7103 | } |
| 7104 | |
| 7105 | for (i = optind; argv[i]; ++i) |
| 7106 | bc_vec_push(&G.files, argv + i); |
| 7107 | } |
| 7108 | |
| 7109 | static void bc_vm_envArgs(void) |
| 7110 | { |
| 7111 | BcVec v; |
| 7112 | char *buf; |
| 7113 | char *env_args = getenv("BC_ENV_ARGS"); |
| 7114 | |
| 7115 | if (!env_args) return; |
| 7116 | |
| 7117 | G.env_args = xstrdup(env_args); |
| 7118 | buf = G.env_args; |
| 7119 | |
| 7120 | bc_vec_init(&v, sizeof(char *), NULL); |
| 7121 | |
| 7122 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 7123 | bc_vec_push(&v, &buf); |
| 7124 | buf = skip_non_whitespace(buf); |
| 7125 | if (!*buf) |
| 7126 | break; |
| 7127 | *buf++ = '\0'; |
| 7128 | } |
| 7129 | |
| 7130 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 7131 | if (sizeof(int) == sizeof(char*)) { |
| 7132 | bc_vec_push(&v, &const_int_0); |
| 7133 | } else { |
| 7134 | static char *const nullptr = NULL; |
| 7135 | bc_vec_push(&v, &nullptr); |
| 7136 | } |
| 7137 | bc_args(((char **)v.v) - 1); |
| 7138 | |
| 7139 | bc_vec_free(&v); |
| 7140 | } |
| 7141 | |
| 7142 | static const char bc_lib[] ALIGN1 = { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7143 | "scale=20" |
| 7144 | "\n" "define e(x){" |
| 7145 | "\n" "auto b,s,n,r,d,i,p,f,v" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7146 | ////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below |
| 7147 | //^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc: |
| 7148 | //e(-.998896): GNU:.36828580434569428695 |
| 7149 | // above code:.36828580434569428696 |
| 7150 | // actual value:.3682858043456942869594... |
| 7151 | // but for now let's be "GNU compatible" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7152 | "\n" "b=ibase" |
| 7153 | "\n" "ibase=A" |
| 7154 | "\n" "if(x<0){" |
| 7155 | "\n" "n=1" |
| 7156 | "\n" "x=-x" |
| 7157 | "\n" "}" |
| 7158 | "\n" "s=scale" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 7159 | "\n" "r=6+s+.44*x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7160 | "\n" "scale=scale(x)+1" |
| 7161 | "\n" "while(x>1){" |
| 7162 | "\n" "d+=1" |
| 7163 | "\n" "x/=2" |
| 7164 | "\n" "scale+=1" |
| 7165 | "\n" "}" |
| 7166 | "\n" "scale=r" |
| 7167 | "\n" "r=x+1" |
| 7168 | "\n" "p=x" |
| 7169 | "\n" "f=v=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7170 | "\n" "for(i=2;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7171 | "\n" "p*=x" |
| 7172 | "\n" "f*=i" |
| 7173 | "\n" "v=p/f" |
| 7174 | "\n" "r+=v" |
| 7175 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7176 | "\n" "while(d--)r*=r" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7177 | "\n" "scale=s" |
| 7178 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7179 | "\n" "if(n)return(1/r)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7180 | "\n" "return(r/1)" |
| 7181 | "\n" "}" |
| 7182 | "\n" "define l(x){" |
| 7183 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7184 | "\n" "b=ibase" |
| 7185 | "\n" "ibase=A" |
| 7186 | "\n" "if(x<=0){" |
| 7187 | "\n" "r=(1-10^scale)/1" |
| 7188 | "\n" "ibase=b" |
| 7189 | "\n" "return(r)" |
| 7190 | "\n" "}" |
| 7191 | "\n" "s=scale" |
| 7192 | "\n" "scale+=6" |
| 7193 | "\n" "p=2" |
| 7194 | "\n" "while(x>=2){" |
| 7195 | "\n" "p*=2" |
| 7196 | "\n" "x=sqrt(x)" |
| 7197 | "\n" "}" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 7198 | "\n" "while(x<=.5){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7199 | "\n" "p*=2" |
| 7200 | "\n" "x=sqrt(x)" |
| 7201 | "\n" "}" |
| 7202 | "\n" "r=a=(x-1)/(x+1)" |
| 7203 | "\n" "q=a*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7204 | "\n" "v=1" |
| 7205 | "\n" "for(i=3;v;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7206 | "\n" "a*=q" |
| 7207 | "\n" "v=a/i" |
| 7208 | "\n" "r+=v" |
| 7209 | "\n" "}" |
| 7210 | "\n" "r*=p" |
| 7211 | "\n" "scale=s" |
| 7212 | "\n" "ibase=b" |
| 7213 | "\n" "return(r/1)" |
| 7214 | "\n" "}" |
| 7215 | "\n" "define s(x){" |
Denys Vlasenko | 240d7ee | 2018-12-14 11:27:09 +0100 | [diff] [blame] | 7216 | "\n" "auto b,s,r,a,q,i" |
| 7217 | "\n" "if(x<0)return(-s(-x))" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7218 | "\n" "b=ibase" |
| 7219 | "\n" "ibase=A" |
| 7220 | "\n" "s=scale" |
| 7221 | "\n" "scale=1.1*s+2" |
| 7222 | "\n" "a=a(1)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7223 | "\n" "scale=0" |
| 7224 | "\n" "q=(x/a+2)/4" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7225 | "\n" "x-=4*q*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7226 | "\n" "if(q%2)x=-x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7227 | "\n" "scale=s+2" |
| 7228 | "\n" "r=a=x" |
| 7229 | "\n" "q=-x*x" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7230 | "\n" "for(i=3;a;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7231 | "\n" "a*=q/(i*(i-1))" |
| 7232 | "\n" "r+=a" |
| 7233 | "\n" "}" |
| 7234 | "\n" "scale=s" |
| 7235 | "\n" "ibase=b" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7236 | "\n" "return(r/1)" |
| 7237 | "\n" "}" |
| 7238 | "\n" "define c(x){" |
| 7239 | "\n" "auto b,s" |
| 7240 | "\n" "b=ibase" |
| 7241 | "\n" "ibase=A" |
| 7242 | "\n" "s=scale" |
| 7243 | "\n" "scale*=1.2" |
| 7244 | "\n" "x=s(2*a(1)+x)" |
| 7245 | "\n" "scale=s" |
| 7246 | "\n" "ibase=b" |
| 7247 | "\n" "return(x/1)" |
| 7248 | "\n" "}" |
| 7249 | "\n" "define a(x){" |
| 7250 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7251 | "\n" "b=ibase" |
| 7252 | "\n" "ibase=A" |
| 7253 | "\n" "n=1" |
| 7254 | "\n" "if(x<0){" |
| 7255 | "\n" "n=-1" |
| 7256 | "\n" "x=-x" |
| 7257 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7258 | "\n" "if(scale<65){" |
| 7259 | "\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7260 | "\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7261 | "\n" "}" |
| 7262 | "\n" "s=scale" |
| 7263 | "\n" "if(x>.2){" |
| 7264 | "\n" "scale+=5" |
| 7265 | "\n" "a=a(.2)" |
| 7266 | "\n" "}" |
| 7267 | "\n" "scale=s+3" |
| 7268 | "\n" "while(x>.2){" |
| 7269 | "\n" "m+=1" |
| 7270 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7271 | "\n" "}" |
| 7272 | "\n" "r=u=x" |
| 7273 | "\n" "f=-x*x" |
| 7274 | "\n" "t=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7275 | "\n" "for(i=3;t;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7276 | "\n" "u*=f" |
| 7277 | "\n" "t=u/i" |
| 7278 | "\n" "r+=t" |
| 7279 | "\n" "}" |
| 7280 | "\n" "scale=s" |
| 7281 | "\n" "ibase=b" |
| 7282 | "\n" "return((m*a+r)/n)" |
| 7283 | "\n" "}" |
| 7284 | "\n" "define j(n,x){" |
| 7285 | "\n" "auto b,s,o,a,i,v,f" |
| 7286 | "\n" "b=ibase" |
| 7287 | "\n" "ibase=A" |
| 7288 | "\n" "s=scale" |
| 7289 | "\n" "scale=0" |
| 7290 | "\n" "n/=1" |
| 7291 | "\n" "if(n<0){" |
| 7292 | "\n" "n=-n" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7293 | "\n" "o=n%2" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7294 | "\n" "}" |
| 7295 | "\n" "a=1" |
| 7296 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7297 | "\n" "scale=1.5*s" |
| 7298 | "\n" "a=(x^n)/2^n/a" |
| 7299 | "\n" "r=v=1" |
| 7300 | "\n" "f=-x*x/4" |
Denys Vlasenko | c06537d | 2018-12-14 10:10:37 +0100 | [diff] [blame] | 7301 | "\n" "scale+=length(a)-scale(a)" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7302 | "\n" "for(i=1;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7303 | "\n" "v=v*f/i/(n+i)" |
| 7304 | "\n" "r+=v" |
| 7305 | "\n" "}" |
| 7306 | "\n" "scale=s" |
| 7307 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7308 | "\n" "if(o)a=-a" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7309 | "\n" "return(a*r/1)" |
| 7310 | "\n" "}" |
| 7311 | }; |
| 7312 | #endif // ENABLE_BC |
| 7313 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7314 | static BC_STATUS zxc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7315 | { |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7316 | char **fname; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7317 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7318 | size_t i; |
| 7319 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7320 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7321 | if (option_mask32 & BC_FLAG_L) { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7322 | // We know that internal library is not buggy, |
| 7323 | // thus error checking is normally disabled. |
| 7324 | # define DEBUG_LIB 0 |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7325 | s = zxc_vm_process(bc_lib); |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7326 | if (DEBUG_LIB && s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7327 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7328 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7329 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7330 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7331 | fname = (void*)G.files.v; |
| 7332 | for (i = 0; i < G.files.len; i++) { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7333 | s = zxc_vm_file(*fname++); |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7334 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) { |
| 7335 | // Debug config, non-interactive mode: |
| 7336 | // return all the way back to main. |
| 7337 | // Non-debug builds do not come here |
| 7338 | // in non-interactive mode, they exit. |
| 7339 | RETURN_STATUS(s); |
| 7340 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7341 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7342 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7343 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7344 | s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7345 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7346 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7347 | } |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7348 | #define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7349 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7350 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7351 | static void xc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7352 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7353 | bc_vec_free(&G.prog.fns); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7354 | IF_BC(bc_vec_free(&G.prog.fn_map);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7355 | bc_vec_free(&G.prog.vars); |
| 7356 | bc_vec_free(&G.prog.var_map); |
| 7357 | bc_vec_free(&G.prog.arrs); |
| 7358 | bc_vec_free(&G.prog.arr_map); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7359 | IF_DC(bc_vec_free(&G.prog.strs);) |
| 7360 | IF_DC(bc_vec_free(&G.prog.consts);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7361 | bc_vec_free(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7362 | bc_vec_free(&G.prog.exestack); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7363 | IF_BC(bc_num_free(&G.prog.last);) |
Denys Vlasenko | db8d607 | 2018-12-27 18:08:30 +0100 | [diff] [blame] | 7364 | //IF_BC(bc_num_free(&G.prog.zero);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7365 | IF_BC(bc_num_free(&G.prog.one);) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7366 | bc_vec_free(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7367 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7368 | #endif |
| 7369 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7370 | static void xc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7371 | { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7372 | BcInstPtr ip; |
| 7373 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7374 | // memset(&G.prog, 0, sizeof(G.prog)); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7375 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7376 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7377 | // G.prog.nchars = G.prog.scale = 0; - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7378 | G.prog.ib_t = 10; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7379 | G.prog.ob_t = 10; |
| 7380 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7381 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);) |
| 7382 | //IF_BC(bc_num_zero(&G.prog.last);) - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7383 | |
Denys Vlasenko | db8d607 | 2018-12-27 18:08:30 +0100 | [diff] [blame] | 7384 | //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7385 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7386 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7387 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);) |
| 7388 | IF_BC(bc_num_one(&G.prog.one);) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7389 | |
| 7390 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7391 | 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] | 7392 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7393 | if (IS_BC) { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7394 | // Names are chosen simply to be distinct and never match |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 7395 | // a valid function name (and be short) |
| 7396 | IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7397 | IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read() |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7398 | } else { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7399 | // in dc, functions have no names |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7400 | xc_program_add_fn(); |
| 7401 | xc_program_add_fn(); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7402 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7403 | |
| 7404 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7405 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7406 | |
| 7407 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7408 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7409 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7410 | IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);) |
| 7411 | 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] | 7412 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7413 | bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL); |
| 7414 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7415 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7416 | bc_char_vec_init(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7417 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7418 | |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 7419 | static unsigned xc_vm_envLen(const char *var) |
| 7420 | { |
| 7421 | char *lenv; |
| 7422 | unsigned len; |
| 7423 | |
| 7424 | lenv = getenv(var); |
| 7425 | len = BC_NUM_PRINT_WIDTH; |
Denys Vlasenko | 29a9043 | 2020-12-29 18:50:56 +0100 | [diff] [blame] | 7426 | if (lenv) { |
| 7427 | len = bb_strtou(lenv, NULL, 10); |
| 7428 | if (len == 0 || len > INT_MAX) |
| 7429 | len = INT_MAX; |
| 7430 | if (errno) |
| 7431 | len = BC_NUM_PRINT_WIDTH; |
| 7432 | } |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 7433 | |
Denys Vlasenko | 29a9043 | 2020-12-29 18:50:56 +0100 | [diff] [blame] | 7434 | // dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7435 | // 1 char wider than bc from the same package. |
| 7436 | // Both default width, and xC_LINE_LENGTH=N are wider: |
| 7437 | // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
| 7438 | // |1234\ | |
| 7439 | // |56 | |
| 7440 | // "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
| 7441 | // |123\ | |
| 7442 | // |456 | |
| 7443 | // Do the same, but it might be a bug in GNU package |
| 7444 | if (IS_BC) |
| 7445 | len--; |
| 7446 | |
| 7447 | if (len < 2) |
| 7448 | len = IS_BC ? BC_NUM_PRINT_WIDTH - 1 : BC_NUM_PRINT_WIDTH; |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 7449 | |
| 7450 | return len; |
| 7451 | } |
| 7452 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7453 | static int xc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7454 | { |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7455 | G.prog.len = xc_vm_envLen(env_len); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7456 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7457 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7458 | xc_program_init(); |
Denys Vlasenko | 5f263f4 | 2018-12-13 22:49:59 +0100 | [diff] [blame] | 7459 | IF_BC(if (IS_BC) bc_vm_envArgs();) |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7460 | xc_parse_create(BC_PROG_MAIN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7461 | |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7462 | //TODO: in GNU bc, the check is (isatty(0) && isatty(1)), |
| 7463 | //-i option unconditionally enables this regardless of isatty(): |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7464 | if (isatty(0)) { |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 7465 | #if ENABLE_FEATURE_BC_INTERACTIVE |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7466 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7467 | // With SA_RESTART, most system calls will restart |
| 7468 | // (IOW: they won't fail with EINTR). |
| 7469 | // In particular, this means ^C won't cause |
| 7470 | // stdout to get into "error state" if SIGINT hits |
| 7471 | // within write() syscall. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7472 | // |
| 7473 | // The downside is that ^C while tty input is taken |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7474 | // will only be handled after [Enter] since read() |
| 7475 | // from stdin is not interrupted by ^C either, |
| 7476 | // it restarts, thus fgetc() does not return on ^C. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7477 | // (This problem manifests only if line editing is disabled) |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7478 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7479 | |
| 7480 | // Without SA_RESTART, this exhibits a bug: |
| 7481 | // "while (1) print 1" and try ^C-ing it. |
| 7482 | // Intermittently, instead of returning to input line, |
| 7483 | // you'll get "output error: Interrupted system call" |
| 7484 | // and exit. |
| 7485 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7486 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7487 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7488 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7489 | return 0; // "not a tty" |
| 7490 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7491 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7492 | static BcStatus xc_vm_run(void) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7493 | { |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7494 | BcStatus st = zxc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7495 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7496 | if (G_exiting) // it was actually "halt" or "quit" |
| 7497 | st = EXIT_SUCCESS; |
Denys Vlasenko | 2cd8c04 | 2018-12-30 15:56:36 +0100 | [diff] [blame] | 7498 | |
| 7499 | bc_vec_free(&G.files); |
| 7500 | xc_program_free(); |
| 7501 | xc_parse_free(); |
| 7502 | free(G.env_args); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7503 | # if ENABLE_FEATURE_EDITING |
| 7504 | free_line_input_t(G.line_input_state); |
| 7505 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7506 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7507 | #endif |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7508 | dbg_exec("exiting with exitcode %d", st); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7509 | return st; |
| 7510 | } |
| 7511 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7512 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7513 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7514 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7515 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7516 | int is_tty; |
| 7517 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7518 | INIT_G(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7519 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7520 | is_tty = xc_vm_init("BC_LINE_LENGTH"); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7521 | |
| 7522 | bc_args(argv); |
| 7523 | |
| 7524 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7525 | bc_vm_info(); |
| 7526 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7527 | return xc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7528 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7529 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7530 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7531 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7532 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7533 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7534 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7535 | int noscript; |
| 7536 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7537 | INIT_G(); |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7538 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7539 | xc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7540 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7541 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7542 | noscript = BC_FLAG_I; |
| 7543 | for (;;) { |
| 7544 | int n = getopt(argc, argv, "e:f:x"); |
| 7545 | if (n <= 0) |
| 7546 | break; |
| 7547 | switch (n) { |
| 7548 | case 'e': |
| 7549 | noscript = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7550 | n = zxc_vm_process(optarg); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7551 | if (n) return n; |
| 7552 | break; |
| 7553 | case 'f': |
| 7554 | noscript = 0; |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7555 | n = zxc_vm_file(optarg); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7556 | if (n) return n; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7557 | break; |
| 7558 | case 'x': |
| 7559 | option_mask32 |= DC_FLAG_X; |
| 7560 | break; |
| 7561 | default: |
| 7562 | bb_show_usage(); |
| 7563 | } |
| 7564 | } |
| 7565 | argv += optind; |
| 7566 | |
| 7567 | while (*argv) { |
| 7568 | noscript = 0; |
| 7569 | bc_vec_push(&G.files, argv++); |
| 7570 | } |
| 7571 | |
| 7572 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7573 | |
Denys Vlasenko | 10bde14 | 2018-12-27 18:23:58 +0100 | [diff] [blame] | 7574 | return xc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7575 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7576 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7577 | |
Denys Vlasenko | 1476760 | 2018-12-27 22:52:13 +0100 | [diff] [blame] | 7578 | #endif // DC_BIG |