blob: 1b9cdce5e3255f8147641d20e77200938992acbb [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Gavin Howardfa495ce2018-12-18 10:03:14 -07004 * Adapted from https://github.com/gavinhoward/bc
5 * Original code copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06006 */
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007//TODO: GNU extensions:
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01008// support "define void f()..."
9// support "define f(*param[])" - "pass array by reference" syntax
10
11#define DEBUG_LEXER 0
12#define DEBUG_COMPILE 0
13#define DEBUG_EXEC 0
14// This can be left enabled for production as well:
15#define SANITY_CHECKS 1
16
Gavin Howard01055ba2018-11-03 11:00:21 -060017//config:config BC
Denys Vlasenkob097a842018-12-28 03:20:17 +010018//config: bool "bc (45 kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060019//config: default y
Denys Vlasenkocdadad52018-12-28 15:13:23 +010020//config: select FEATURE_DC_BIG
Gavin Howard01055ba2018-11-03 11:00:21 -060021//config: help
22//config: bc is a command-line, arbitrary-precision calculator with a
23//config: Turing-complete language. See the GNU bc manual
24//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010025//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060026//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010027//config: This bc has five differences to the GNU bc:
28//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060029//config: 2) Arrays are copied before being passed as arguments to
30//config: functions. This behavior is required by the bc spec.
31//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010032//config: the number of elements in the array. This prints "1":
33//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060034//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010035//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060036//config: allows POSIX-compliant scripts to work while somewhat
37//config: preserving expected behavior (versus C) and making parsing
38//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010039//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060040//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060041//config:config DC
Denys Vlasenkob097a842018-12-28 03:20:17 +010042//config: bool "dc (36 kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060043//config: default y
44//config: help
45//config: dc is a reverse-polish notation command-line calculator which
46//config: supports unlimited precision arithmetic. See the FreeBSD man page
47//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010048//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060049//config:
50//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010051//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060052//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010053//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060054//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010055//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060056//config: 4) Like the FreeBSD dc, this dc supports extended registers.
57//config: However, they are implemented differently. When it encounters
58//config: whitespace where a register should be, it skips the whitespace.
59//config: If the character following is not a lowercase letter, an error
60//config: is issued. Otherwise, the register name is parsed by the
Denys Vlasenko14767602018-12-27 22:52:13 +010061//config: following regex: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060062//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010063//config: whitespace. Examples:
64//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060065//config: Also note that, like the FreeBSD dc, extended registers are not
66//config: allowed unless the "-x" option is given.
67//config:
Denys Vlasenko14767602018-12-27 22:52:13 +010068//config:if BC || DC # for menuconfig indenting
69//config:
70//config:config FEATURE_DC_BIG
71//config: bool "Use bc code base for dc (larger, more features)"
Denys Vlasenko14767602018-12-27 22:52:13 +010072//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010073//config:
74//config:config FEATURE_DC_LIBM
75//config: bool "Enable power and exp functions (requires libm)"
76//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010077//config: depends on DC && !BC && !FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010078//config: help
79//config: Enable power and exp functions.
80//config: NOTE: This will require libm to be present for linking.
81//config:
Denys Vlasenko14767602018-12-27 22:52:13 +010082//config:config FEATURE_BC_INTERACTIVE
Denys Vlasenko89e785a2018-12-13 16:35:52 +010083//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060084//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +010085//config: depends on BC || (DC && FEATURE_DC_BIG)
Gavin Howard01055ba2018-11-03 11:00:21 -060086//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010087//config: Enable interactive mode: when started on a tty,
88//config: ^C interrupts execution and returns to command line,
89//config: errors also return to command line instead of exiting,
90//config: line editing with history is available.
91//config:
92//config: With this option off, input can still be taken from tty,
93//config: but all errors are fatal, ^C is fatal,
94//config: tty is treated exactly the same as any other
95//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060096//config:
97//config:config FEATURE_BC_LONG_OPTIONS
98//config: bool "Enable bc/dc long options"
99//config: default y
Denys Vlasenko14767602018-12-27 22:52:13 +0100100//config: depends on BC || (DC && FEATURE_DC_BIG)
101//config:
102//config:endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600103
104//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
105//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
106
107//kbuild:lib-$(CONFIG_BC) += bc.o
108//kbuild:lib-$(CONFIG_DC) += bc.o
109
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100110//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600111//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100112//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600113//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100114//usage:#define bc_full_usage "\n"
115//usage: "\nArbitrary precision calculator"
116//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100117///////: "\n -i Interactive" - has no effect for now
118//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100119//usage: "\n -l Load standard math library"
120//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100121//usage: "\n -w Warn if extensions are used"
122///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100123//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100124//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600125//usage:
126//usage:#define bc_example_usage
127//usage: "3 + 4.129\n"
128//usage: "1903 - 2893\n"
129//usage: "-129 * 213.28935\n"
130//usage: "12 / -1932\n"
131//usage: "12 % 12\n"
132//usage: "34 ^ 189\n"
133//usage: "scale = 13\n"
134//usage: "ibase = 2\n"
135//usage: "obase = A\n"
136//usage:
137//usage:#define dc_trivial_usage
Denys Vlasenko14767602018-12-27 22:52:13 +0100138//usage: IF_FEATURE_DC_BIG("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600139//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100140//usage:#define dc_full_usage "\n"
141//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko14767602018-12-27 22:52:13 +0100142//usage: "\n+, -, *, /, %, ~, ^," IF_FEATURE_DC_BIG(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100143//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100144//usage: "\nf - print entire stack"
145//usage: "\nk - pop the value and set the precision"
146//usage: "\ni - pop the value and set input radix"
147//usage: "\no - pop the value and set output radix"
148//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600149//usage:
150//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100151//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600152//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100153//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600154//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100155//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600156//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100157//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600158//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100159//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600160//usage: "64\n"
161
162#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100163#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600164
Denys Vlasenko14767602018-12-27 22:52:13 +0100165#if !ENABLE_BC && !ENABLE_FEATURE_DC_BIG
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100166# include "dc.c"
167#else
168
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100169#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100170static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100171#define dbg_lex(...) \
172 do { \
173 fprintf(stderr, "%*s", lex_indent, ""); \
174 bb_error_msg(__VA_ARGS__); \
175 } while (0)
176#define dbg_lex_enter(...) \
177 do { \
178 dbg_lex(__VA_ARGS__); \
179 lex_indent++; \
180 } while (0)
181#define dbg_lex_done(...) \
182 do { \
183 lex_indent--; \
184 dbg_lex(__VA_ARGS__); \
185 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100186#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100187# define dbg_lex(...) ((void)0)
188# define dbg_lex_enter(...) ((void)0)
189# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100190#endif
191
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100192#if DEBUG_COMPILE
193# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
194#else
195# define dbg_compile(...) ((void)0)
196#endif
197
Denys Vlasenko99b37622018-12-15 20:06:59 +0100198#if DEBUG_EXEC
199# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
200#else
201# define dbg_exec(...) ((void)0)
202#endif
203
Gavin Howard01055ba2018-11-03 11:00:21 -0600204typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100205 BC_STATUS_SUCCESS = 0,
206 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100207 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600208} BcStatus;
209
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100210#define BC_VEC_INVALID_IDX ((size_t) -1)
211#define BC_VEC_START_CAP (1 << 5)
Gavin Howard01055ba2018-11-03 11:00:21 -0600212
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100213typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600214
215typedef struct BcVec {
216 char *v;
217 size_t len;
218 size_t cap;
219 size_t size;
220 BcVecFree dtor;
221} BcVec;
222
Gavin Howard01055ba2018-11-03 11:00:21 -0600223typedef signed char BcDig;
224
225typedef struct BcNum {
226 BcDig *restrict num;
227 size_t rdx;
228 size_t len;
229 size_t cap;
230 bool neg;
231} BcNum;
232
Denys Vlasenko680ccd32018-12-31 19:42:13 +0100233#define BC_NUM_MAX_IBASE 36
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100234// larger value might speed up BIGNUM calculations a bit:
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100235#define BC_NUM_DEF_SIZE 16
236#define BC_NUM_PRINT_WIDTH 69
Gavin Howard01055ba2018-11-03 11:00:21 -0600237
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100238#define BC_NUM_KARATSUBA_LEN 32
Gavin Howard01055ba2018-11-03 11:00:21 -0600239
Gavin Howard01055ba2018-11-03 11:00:21 -0600240typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100241#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600242 BC_INST_INC_PRE,
243 BC_INST_DEC_PRE,
244 BC_INST_INC_POST,
245 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100246#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100247 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600248
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100249 XC_INST_REL_EQ, // should
250 XC_INST_REL_LE, // match
251 XC_INST_REL_GE, // LEX
252 XC_INST_REL_NE, // constants
253 XC_INST_REL_LT, // for
254 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600255
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100256 XC_INST_POWER, // operations
257 XC_INST_MULTIPLY, // |
258 XC_INST_DIVIDE, // |
259 XC_INST_MODULUS, // |
260 XC_INST_PLUS, // |
261 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600262
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100263 XC_INST_BOOL_NOT, // |
264 XC_INST_BOOL_OR, // |
265 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100266#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100267 BC_INST_ASSIGN_POWER, // |
268 BC_INST_ASSIGN_MULTIPLY,// |
269 BC_INST_ASSIGN_DIVIDE, // |
270 BC_INST_ASSIGN_MODULUS, // |
271 BC_INST_ASSIGN_PLUS, // |
272 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100273#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100274 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600275
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100276 XC_INST_NUM,
277 XC_INST_VAR,
278 XC_INST_ARRAY_ELEM,
279 XC_INST_ARRAY,
280 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100281
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100282 XC_INST_IBASE, // order of these constans should match other enums
283 XC_INST_OBASE, // order of these constans should match other enums
284 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100285 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100286 XC_INST_LENGTH,
287 XC_INST_READ,
288 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600289
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100290 XC_INST_PRINT,
291 XC_INST_PRINT_POP,
292 XC_INST_STR,
293 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600294
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100295#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100296 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600297 BC_INST_JUMP,
298 BC_INST_JUMP_ZERO,
299
300 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600301 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100302#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100303 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600304
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100305 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100306#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100307 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600308
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100309 DC_INST_MODEXP,
310 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600311
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100312 DC_INST_EXECUTE,
313 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600314
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100315 DC_INST_ASCIIFY,
316 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600317
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100318 DC_INST_PRINT_STACK,
319 DC_INST_CLEAR_STACK,
320 DC_INST_STACK_LEN,
321 DC_INST_DUPLICATE,
322 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600323
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100324 DC_INST_LOAD,
325 DC_INST_PUSH_VAR,
326 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600327
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100328 DC_INST_QUIT,
329 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600330
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100331 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100332#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600333} BcInst;
334
335typedef struct BcId {
336 char *name;
337 size_t idx;
338} BcId;
339
340typedef struct BcFunc {
341 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100342 IF_BC(BcVec labels;)
343 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100344 IF_BC(BcVec strs;)
345 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100346 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600347} BcFunc;
348
349typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100350 XC_RESULT_TEMP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600351
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100352 XC_RESULT_VAR,
353 XC_RESULT_ARRAY_ELEM,
354 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600355
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100356 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600357
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100358 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
359 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
360 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
361 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100362 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100363 XC_RESULT_CONSTANT,
364 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600365} BcResultType;
366
367typedef union BcResultData {
368 BcNum n;
369 BcVec v;
370 BcId id;
371} BcResultData;
372
373typedef struct BcResult {
374 BcResultType t;
375 BcResultData d;
376} BcResult;
377
378typedef struct BcInstPtr {
379 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100380 size_t inst_idx;
381 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600382} BcInstPtr;
383
Gavin Howard01055ba2018-11-03 11:00:21 -0600384typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100385 XC_LEX_EOF,
386 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600387
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100388 XC_LEX_NLINE,
389 XC_LEX_WHITESPACE,
390 XC_LEX_STR,
391 XC_LEX_NAME,
392 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100393
Denys Vlasenko69560f42018-12-24 14:14:23 +0100394 XC_LEX_1st_op,
395 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100396
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100397 XC_LEX_OP_REL_EQ, // should
398 XC_LEX_OP_REL_LE, // match
399 XC_LEX_OP_REL_GE, // INST
400 XC_LEX_OP_REL_NE, // constants
401 XC_LEX_OP_REL_LT, // for
402 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100403
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100404 XC_LEX_OP_POWER, // operations
405 XC_LEX_OP_MULTIPLY, // |
406 XC_LEX_OP_DIVIDE, // |
407 XC_LEX_OP_MODULUS, // |
408 XC_LEX_OP_PLUS, // |
409 XC_LEX_OP_MINUS, // |
410 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100411#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100412 BC_LEX_OP_BOOL_NOT, // |
413 BC_LEX_OP_BOOL_OR, // |
414 BC_LEX_OP_BOOL_AND, // |
415
416 BC_LEX_OP_ASSIGN_POWER, // |
417 BC_LEX_OP_ASSIGN_MULTIPLY, // |
418 BC_LEX_OP_ASSIGN_DIVIDE, // |
419 BC_LEX_OP_ASSIGN_MODULUS, // |
420 BC_LEX_OP_ASSIGN_PLUS, // |
421 BC_LEX_OP_ASSIGN_MINUS, // |
422
423 BC_LEX_OP_ASSIGN, // V
424
Gavin Howard01055ba2018-11-03 11:00:21 -0600425 BC_LEX_OP_INC,
426 BC_LEX_OP_DEC,
427
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100428 BC_LEX_LPAREN, // () are 0x28 and 0x29
429 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600430
Denys Vlasenko10bde142018-12-27 18:23:58 +0100431 BC_LEX_LBRACKET, // [] are 0x5B and 0x5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600432 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100433 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600434
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100435 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600436 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100437 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600438
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100439 BC_LEX_KEY_1st_keyword,
440 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600441 BC_LEX_KEY_BREAK,
442 BC_LEX_KEY_CONTINUE,
443 BC_LEX_KEY_DEFINE,
444 BC_LEX_KEY_ELSE,
445 BC_LEX_KEY_FOR,
446 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100447 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100448 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
449 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600450 BC_LEX_KEY_IF,
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100451 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600452 BC_LEX_KEY_LENGTH,
453 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600454 BC_LEX_KEY_PRINT,
455 BC_LEX_KEY_QUIT,
456 BC_LEX_KEY_READ,
457 BC_LEX_KEY_RETURN,
458 BC_LEX_KEY_SCALE,
459 BC_LEX_KEY_SQRT,
460 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100461#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600462
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100463#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100464 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
465 DC_LEX_OP_ASSIGN,
466
467 DC_LEX_LPAREN,
468 DC_LEX_SCOLON,
469 DC_LEX_READ,
470 DC_LEX_IBASE,
471 DC_LEX_SCALE,
472 DC_LEX_OBASE,
473 DC_LEX_LENGTH,
474 DC_LEX_PRINT,
475 DC_LEX_QUIT,
476 DC_LEX_SQRT,
477 DC_LEX_LBRACE,
478
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100479 DC_LEX_EQ_NO_REG,
480 DC_LEX_OP_MODEXP,
481 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600482
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100483 DC_LEX_COLON,
484 DC_LEX_ELSE,
485 DC_LEX_EXECUTE,
486 DC_LEX_PRINT_STACK,
487 DC_LEX_CLEAR_STACK,
488 DC_LEX_STACK_LEVEL,
489 DC_LEX_DUPLICATE,
490 DC_LEX_SWAP,
491 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600492
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100493 DC_LEX_ASCIIFY,
494 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600495
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100496 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
497 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
498 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
499 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
500 DC_LEX_LOAD,
501 DC_LEX_LOAD_POP,
502 DC_LEX_STORE_PUSH,
503 DC_LEX_PRINT_POP,
504 DC_LEX_NQUIT,
505 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100506#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600507} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100508// must match order of BC_LEX_KEY_foo etc above
509#if ENABLE_BC
510struct BcLexKeyword {
511 char name8[8];
512};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100513#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100514 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100515static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100516 LEX_KW_ENTRY("auto" , 1), // 0
517 LEX_KW_ENTRY("break" , 1), // 1
518 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
519 LEX_KW_ENTRY("define" , 1), // 3
520 LEX_KW_ENTRY("else" , 0), // 4
521 LEX_KW_ENTRY("for" , 1), // 5
522 LEX_KW_ENTRY("halt" , 0), // 6
523 LEX_KW_ENTRY("ibase" , 1), // 7
524 LEX_KW_ENTRY("obase" , 1), // 8
525 LEX_KW_ENTRY("if" , 1), // 9
526 LEX_KW_ENTRY("last" , 0), // 10
527 LEX_KW_ENTRY("length" , 1), // 11
528 LEX_KW_ENTRY("limits" , 0), // 12
529 LEX_KW_ENTRY("print" , 0), // 13
530 LEX_KW_ENTRY("quit" , 1), // 14
531 LEX_KW_ENTRY("read" , 0), // 15
532 LEX_KW_ENTRY("return" , 1), // 16
533 LEX_KW_ENTRY("scale" , 1), // 17
534 LEX_KW_ENTRY("sqrt" , 1), // 18
535 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100536};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100537#undef LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100538#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100539#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100540#define STRING_if (bc_lex_kws[9].name8)
541#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100542enum {
543 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100544 | (1 << 0) // 0
545 | (1 << 1) // 1
546 | (0 << 2) // 2
547 | (1 << 3) // 3
548 | (0 << 4) // 4
549 | (1 << 5) // 5
550 | (0 << 6) // 6
551 | (1 << 7) // 7
552 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100553 | (1 << 9) // 9
554 | (0 << 10) // 10
555 | (1 << 11) // 11
556 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100557 | (0 << 13) // 13
558 | (1 << 14) // 14
559 | (0 << 15) // 15
560 | (1 << 16) // 16
561 | (1 << 17) // 17
562 | (1 << 18) // 18
563 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100564};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100565#define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100566
567// This is a bit array that corresponds to token types. An entry is
568// true if the token is valid in an expression, false otherwise.
569// Used to figure out when expr parsing should stop *without error message*
570// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100571// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100572// by later processing.
573enum {
574#define EXBITS(a,b,c,d,e,f,g,h) \
575 ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7)))
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100576 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100577 + (EXBITS(0,0,0,0,0,1,1,1) << (0*8)) // 0: EOF INVAL NL WS STR NAME NUM -
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100578 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
579 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100580 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
581 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100582 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
583 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
584 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100585#undef EXBITS
586};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100587static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100588{
589#if ULONG_MAX > 0xffffffff
590 // 64-bit version (will not work correctly for 32-bit longs!)
591 return BC_PARSE_EXPRS_BITS & (1UL << i);
592#else
593 // 32-bit version
594 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
595 if (i >= 32) {
596 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
597 i &= 31;
598 }
599 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100600#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100601}
602
603// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100604// [XC_LEX_1st_op...] token types.
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100605static const uint8_t bc_ops_prec_and_assoc[] ALIGN1 = {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100606#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100607 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100608 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100609 OP(2, false), // pow
610 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
611 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100612 OP(1, false), // not
613 OP(7, true ), OP( 7, true ), // or and
614 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
615 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100616 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100617#undef OP
618};
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100619#define bc_operation_PREC(i) (bc_ops_prec_and_assoc[i] & 0x0f)
620#define bc_operation_LEFT(i) (bc_ops_prec_and_assoc[i] & 0x10)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100621#endif // ENABLE_BC
622
623#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100624static const //BcLexType - should be this type
625uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +0100626dc_char_to_LEX[] ALIGN1 = {
Denys Vlasenko10bde142018-12-27 18:23:58 +0100627 // %&'(
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100628 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100629 // )*+,
Denys Vlasenko69560f42018-12-24 14:14:23 +0100630 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100631 // -./
Denys Vlasenko69560f42018-12-24 14:14:23 +0100632 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100633 // 0123456789
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100634 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
635 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
636 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100637 // :;<=>?@
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100638 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
639 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100640 // ABCDEFGH
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100641 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
642 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100643 // IJKLMNOP
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100644 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
645 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100646 // QRSTUVWXY
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100647 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
648 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100649 // Z[\]
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100650 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100651 // ^_`
Denys Vlasenko69560f42018-12-24 14:14:23 +0100652 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100653 // abcdefgh
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100654 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
655 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100656 // ijklmnop
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100657 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100658 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100659 // qrstuvwx
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100660 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
661 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100662 // yz
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100663 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko10bde142018-12-27 18:23:58 +0100664 // {|}~
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100665 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100666};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100667static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100668int8_t
Denys Vlasenko10bde142018-12-27 18:23:58 +0100669dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
670 XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY
671 XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS
672 XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100673 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
674 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
675 XC_INST_REL_GT, // DC_LEX_LPAREN
676 DC_INST_INVALID, // DC_LEX_SCOLON
677 DC_INST_INVALID, // DC_LEX_READ
678 XC_INST_IBASE, // DC_LEX_IBASE
679 XC_INST_SCALE, // DC_LEX_SCALE
680 XC_INST_OBASE, // DC_LEX_OBASE
681 XC_INST_LENGTH, // DC_LEX_LENGTH
682 XC_INST_PRINT, // DC_LEX_PRINT
683 DC_INST_QUIT, // DC_LEX_QUIT
684 XC_INST_SQRT, // DC_LEX_SQRT
685 XC_INST_REL_GE, // DC_LEX_LBRACE
686 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
Denys Vlasenko10bde142018-12-27 18:23:58 +0100687 DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD
688 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE
689 DC_INST_EXECUTE, // DC_LEX_EXECUTE
690 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK
691 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE
692 DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP
693 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM
694 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE
695 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD
696 DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH
697 XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT
698 XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100699 // DC_INST_INVALID in this table either means that corresponding LEX
700 // is not possible for dc, or that it does not compile one-to-one
701 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100702};
703#endif // ENABLE_DC
704
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100705typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100706 smallint lex; // was BcLexType // first member is most used
707 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100708 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100709 const char *lex_inbuf;
710 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100711 const char *lex_filename;
712 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100713 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100714
Gavin Howard01055ba2018-11-03 11:00:21 -0600715 BcFunc *func;
716 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100717 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100718 IF_BC(BcVec exits;)
719 IF_BC(BcVec conds;)
720 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600721} BcParse;
722
Gavin Howard01055ba2018-11-03 11:00:21 -0600723typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600724 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100725 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600726
Denys Vlasenko503faf92018-12-20 16:24:18 +0100727 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600728 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600729 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600730
Gavin Howard01055ba2018-11-03 11:00:21 -0600731 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100732 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600733
734 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100735 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600736
737 BcVec vars;
738 BcVec var_map;
739
740 BcVec arrs;
741 BcVec arr_map;
742
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100743 IF_DC(BcVec strs;)
744 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600745
Gavin Howard01055ba2018-11-03 11:00:21 -0600746 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100747 IF_BC(BcNum one;)
748 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600749} BcProgram;
750
Denys Vlasenko6e618232018-12-25 22:20:14 +0100751struct globals {
752 BcParse prs; // first member is most used
753
754 // For error messages. Can be set to current parsed line,
755 // or [TODO] to current executing line (can be before last parsed one)
756 unsigned err_line;
757
758 BcVec input_buffer;
759
Denys Vlasenko14767602018-12-27 22:52:13 +0100760 IF_FEATURE_BC_INTERACTIVE(smallint ttyin;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100761 IF_FEATURE_CLEAN_UP(smallint exiting;)
762
763 BcProgram prog;
764
765 BcVec files;
766
767 char *env_args;
768
769#if ENABLE_FEATURE_EDITING
770 line_input_t *line_input_state;
771#endif
772} FIX_ALIASING;
773#define G (*ptr_to_globals)
774#define INIT_G() do { \
775 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
776} while (0)
777#define FREE_G() do { \
778 FREE_PTR_TO_GLOBALS(); \
779} while (0)
780#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
781#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
782#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko14767602018-12-27 22:52:13 +0100783#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko6e618232018-12-25 22:20:14 +0100784# define G_interrupt bb_got_signal
785# define G_ttyin G.ttyin
786#else
787# define G_interrupt 0
788# define G_ttyin 0
789#endif
790#if ENABLE_FEATURE_CLEAN_UP
791# define G_exiting G.exiting
792#else
793# define G_exiting 0
794#endif
795#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
796#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
797
Denys Vlasenko6e618232018-12-25 22:20:14 +0100798#if ENABLE_BC
799# define BC_PARSE_REL (1 << 0)
800# define BC_PARSE_PRINT (1 << 1)
801# define BC_PARSE_ARRAY (1 << 2)
802# define BC_PARSE_NOCALL (1 << 3)
803#endif
804
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100805#define BC_PROG_MAIN 0
806#define BC_PROG_READ 1
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100807#if ENABLE_DC
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100808#define BC_PROG_REQ_FUNCS 2
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100809#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600810
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100811#define BC_FLAG_W (1 << 0)
812#define BC_FLAG_V (1 << 1)
813#define BC_FLAG_S (1 << 2)
814#define BC_FLAG_Q (1 << 3)
815#define BC_FLAG_L (1 << 4)
Denys Vlasenko10bde142018-12-27 18:23:58 +0100816#define BC_FLAG_I ((1 << 5) * ENABLE_DC)
817#define DC_FLAG_X ((1 << 6) * ENABLE_DC)
Gavin Howard01055ba2018-11-03 11:00:21 -0600818
Denys Vlasenko64074a12018-12-07 15:50:14 +0100819#define BC_MAX_OBASE ((unsigned) 999)
820#define BC_MAX_DIM ((unsigned) INT_MAX)
821#define BC_MAX_SCALE ((unsigned) UINT_MAX)
822#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
823#define BC_MAX_NUM BC_MAX_STRING
824// Unused apart from "limits" message. Just show a "biggish number" there.
825//#define BC_MAX_NAME BC_MAX_STRING
826//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
827//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
828#define BC_MAX_NAME_STR "999999999"
829#define BC_MAX_EXP_STR "999999999"
830#define BC_MAX_VARS_STR "999999999"
831
832#define BC_MAX_OBASE_STR "999"
833
834#if INT_MAX == 2147483647
835# define BC_MAX_DIM_STR "2147483647"
836#elif INT_MAX == 9223372036854775807
837# define BC_MAX_DIM_STR "9223372036854775807"
838#else
839# error Strange INT_MAX
840#endif
841
842#if UINT_MAX == 4294967295
843# define BC_MAX_SCALE_STR "4294967295"
844# define BC_MAX_STRING_STR "4294967294"
845#elif UINT_MAX == 18446744073709551615
846# define BC_MAX_SCALE_STR "18446744073709551615"
847# define BC_MAX_STRING_STR "18446744073709551614"
848#else
849# error Strange UINT_MAX
850#endif
851#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600852
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100853// In configurations where errors abort instead of propagating error
854// return code up the call chain, functions returning BC_STATUS
855// actually don't return anything, they always succeed and return "void".
856// A macro wrapper is provided, which makes this statement work:
857// s = zbc_func(...)
858// and makes it visible to the compiler that s is always zero,
859// allowing compiler to optimize dead code after the statement.
860//
861// To make code more readable, each such function has a "z"
862// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
863//
Denys Vlasenko14767602018-12-27 22:52:13 +0100864#if ENABLE_FEATURE_BC_INTERACTIVE || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100865# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100866# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100867# define IF_ERROR_RETURN_POSSIBLE(a) a
868# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100869# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100870# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100871#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100872# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100873# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100874# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
875# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100876# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100877# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100878#endif
879
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100880//
881// Utility routines
882//
Gavin Howard01055ba2018-11-03 11:00:21 -0600883
Denys Vlasenko2cd8c042018-12-30 15:56:36 +0100884#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
885#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
886
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100887static void fflush_and_check(void)
888{
889 fflush_all();
890 if (ferror(stdout) || ferror(stderr))
891 bb_perror_msg_and_die("output error");
892}
893
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100894#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100895#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100896do { \
Denys Vlasenko14767602018-12-27 22:52:13 +0100897 IF_FEATURE_BC_INTERACTIVE(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100898 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100899 return BC_STATUS_FAILURE; \
900} while (0)
901#else
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100902static void quit(void) NORETURN;
903static void quit(void)
904{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100905 if (ferror(stdin))
906 bb_perror_msg_and_die("input error");
907 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100908 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100909 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100910}
Denys Vlasenko10bde142018-12-27 18:23:58 +0100911#define QUIT_OR_RETURN_TO_MAIN quit()
912#endif
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100913
Denys Vlasenko5318f812018-12-05 17:48:01 +0100914static void bc_verror_msg(const char *fmt, va_list p)
915{
Denys Vlasenko82269122018-12-14 16:30:56 +0100916 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100917 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100918 sv = applet_name;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100919 applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.lex_filename, G.err_line);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100920 }
921 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100922 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100923 free((char*)applet_name);
924 applet_name = sv;
925 }
926}
927
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100928static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100929{
930 va_list p;
931
932 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100933 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100934 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100935
Denys Vlasenkoec603182018-12-17 10:34:02 +0100936 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
937 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
938 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100939}
940
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100941#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100942static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100943{
944 va_list p;
945
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100946 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100947 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100948 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100949
950 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100951 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100952 va_end(p);
953
954 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100955 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100956 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
957
Denys Vlasenkoec603182018-12-17 10:34:02 +0100958 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100959 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100960 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100961}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100962#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100963#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100964
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100965// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
966// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100967// function must not have caller-cleaned parameters on stack.
968// Unfortunately, vararg function API does exactly that on most arches.
969// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100970static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100971{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100972 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100973}
Denys Vlasenko22314682019-01-01 21:50:14 +0100974static ERRORFUNC int bc_error_at(const char *msg)
975{
976 const char *err_at = G.prs.lex_next_at;
977 if (err_at) {
978 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt(
979 "%s at '%.*s'",
980 msg,
981 (int)(strchrnul(err_at, '\n') - err_at),
982 err_at
983 );
984 }
985 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
986}
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100987static ERRORFUNC int bc_error_bad_character(char c)
988{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100989 if (!c)
990 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100991 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100992}
Denys Vlasenko22314682019-01-01 21:50:14 +0100993static ERRORFUNC int bc_error_bad_function_definition(void)
994{
995 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad function definition");
996}
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100997static ERRORFUNC int bc_error_bad_expression(void)
998{
Denys Vlasenko22314682019-01-01 21:50:14 +0100999 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad expression");
1000}
1001static ERRORFUNC int bc_error_bad_assignment(void)
1002{
1003 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at(
1004 "bad assignment: left side must be variable or array element"
1005 );
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001006}
1007static ERRORFUNC int bc_error_bad_token(void)
1008{
Denys Vlasenko22314682019-01-01 21:50:14 +01001009 IF_ERROR_RETURN_POSSIBLE(return) bc_error_at("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001010}
1011static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1012{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001013 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001014}
1015static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1016{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001017 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001018}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001019#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001020static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001021{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001022 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001023}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001024#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1025static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001026{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001027 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001028}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001029#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1030static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001031{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001032 RETURN_STATUS(zbc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; this is bad:", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001033}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001034#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)
1035static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001036{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001037 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001038}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001039#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 Vlasenko23c2e9f2018-12-06 11:43:17 +01001040#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001041
Gavin Howard01055ba2018-11-03 11:00:21 -06001042static void bc_vec_grow(BcVec *v, size_t n)
1043{
1044 size_t cap = v->cap * 2;
1045 while (cap < v->len + n) cap *= 2;
1046 v->v = xrealloc(v->v, v->size * cap);
1047 v->cap = cap;
1048}
1049
1050static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1051{
1052 v->size = esize;
1053 v->cap = BC_VEC_START_CAP;
1054 v->len = 0;
1055 v->dtor = dtor;
1056 v->v = xmalloc(esize * BC_VEC_START_CAP);
1057}
1058
Denys Vlasenko7d628012018-12-04 21:46:47 +01001059static void bc_char_vec_init(BcVec *v)
1060{
1061 bc_vec_init(v, sizeof(char), NULL);
1062}
1063
Gavin Howard01055ba2018-11-03 11:00:21 -06001064static void bc_vec_expand(BcVec *v, size_t req)
1065{
1066 if (v->cap < req) {
1067 v->v = xrealloc(v->v, v->size * req);
1068 v->cap = req;
1069 }
1070}
1071
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001072static void bc_vec_pop(BcVec *v)
1073{
1074 v->len--;
1075 if (v->dtor)
1076 v->dtor(v->v + (v->size * v->len));
1077}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001078
Gavin Howard01055ba2018-11-03 11:00:21 -06001079static void bc_vec_npop(BcVec *v, size_t n)
1080{
1081 if (!v->dtor)
1082 v->len -= n;
1083 else {
1084 size_t len = v->len - n;
1085 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1086 }
1087}
1088
Denys Vlasenko7d628012018-12-04 21:46:47 +01001089static void bc_vec_pop_all(BcVec *v)
1090{
1091 bc_vec_npop(v, v->len);
1092}
1093
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001094static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001095{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001096 size_t len = v->len;
1097 if (len >= v->cap) bc_vec_grow(v, 1);
1098 memmove(v->v + (v->size * len), data, v->size);
1099 v->len++;
1100 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001101}
1102
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001103// G.prog.results often needs "pop old operand, push result" idiom.
1104// Can do this without a few extra ops
1105static size_t bc_result_pop_and_push(const void *data)
1106{
1107 BcVec *v = &G.prog.results;
1108 char *last;
1109 size_t len = v->len - 1;
1110
1111 last = v->v + (v->size * len);
1112 if (v->dtor)
1113 v->dtor(last);
1114 memmove(last, data, v->size);
1115 return len;
1116}
1117
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001118static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001119{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001120 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001121}
1122
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001123static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001124{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001125 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001126 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001127 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001128}
1129
Gavin Howard01055ba2018-11-03 11:00:21 -06001130static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1131{
1132 if (idx == v->len)
1133 bc_vec_push(v, data);
1134 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001135 char *ptr;
1136
1137 if (v->len == v->cap) bc_vec_grow(v, 1);
1138
1139 ptr = v->v + v->size * idx;
1140
1141 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1142 memmove(ptr, data, v->size);
1143 }
1144}
1145
1146static void bc_vec_string(BcVec *v, size_t len, const char *str)
1147{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001148 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001149 bc_vec_expand(v, len + 1);
1150 memcpy(v->v, str, len);
1151 v->len = len;
1152
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001153 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001154}
1155
Gavin Howard01055ba2018-11-03 11:00:21 -06001156static void *bc_vec_item(const BcVec *v, size_t idx)
1157{
1158 return v->v + v->size * idx;
1159}
1160
1161static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1162{
1163 return v->v + v->size * (v->len - idx - 1);
1164}
1165
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001166static void *bc_vec_top(const BcVec *v)
1167{
1168 return v->v + v->size * (v->len - 1);
1169}
1170
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001171static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001172{
1173 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001174 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001175 free(v->v);
1176}
1177
Denys Vlasenko10bde142018-12-27 18:23:58 +01001178static BcFunc* xc_program_func(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001179{
1180 return bc_vec_item(&G.prog.fns, idx);
1181}
1182// BC_PROG_MAIN is zeroth element, so:
Denys Vlasenko10bde142018-12-27 18:23:58 +01001183#define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001184
1185#if ENABLE_BC
1186static BcFunc* bc_program_current_func(void)
1187{
1188 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01001189 BcFunc *func = xc_program_func(ip->func);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001190 return func;
1191}
1192#endif
1193
Denys Vlasenko10bde142018-12-27 18:23:58 +01001194static char** xc_program_str(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001195{
1196#if ENABLE_BC
1197 if (IS_BC) {
1198 BcFunc *func = bc_program_current_func();
1199 return bc_vec_item(&func->strs, idx);
1200 }
1201#endif
1202 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1203}
1204
Denys Vlasenko10bde142018-12-27 18:23:58 +01001205static char** xc_program_const(size_t idx)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001206{
1207#if ENABLE_BC
1208 if (IS_BC) {
1209 BcFunc *func = bc_program_current_func();
1210 return bc_vec_item(&func->consts, idx);
1211 }
1212#endif
1213 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1214}
1215
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001216static int bc_id_cmp(const void *e1, const void *e2)
1217{
1218 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1219}
1220
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001221static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001222{
1223 free(((BcId *) id)->name);
1224}
1225
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001226static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001227{
1228 size_t low = 0, high = v->len;
1229
1230 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001231 size_t mid = (low + high) / 2;
1232 BcId *id = bc_vec_item(v, mid);
1233 int result = bc_id_cmp(ptr, id);
1234
1235 if (result == 0)
1236 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001237 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001238 high = mid;
1239 else
1240 low = mid + 1;
1241 }
1242
1243 return low;
1244}
1245
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001246static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001247{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001248 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001249
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001250 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001251 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001252 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1253 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001254 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001255 bc_vec_pushAt(v, ptr, n);
1256 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001257}
1258
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001259#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001260static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001261{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001262 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001263 if (i >= v->len) return BC_VEC_INVALID_IDX;
1264 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1265}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001266#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001267
Gavin Howard01055ba2018-11-03 11:00:21 -06001268static void bc_num_setToZero(BcNum *n, size_t scale)
1269{
1270 n->len = 0;
1271 n->neg = false;
1272 n->rdx = scale;
1273}
1274
1275static void bc_num_zero(BcNum *n)
1276{
1277 bc_num_setToZero(n, 0);
1278}
1279
1280static void bc_num_one(BcNum *n)
1281{
1282 bc_num_setToZero(n, 0);
1283 n->len = 1;
1284 n->num[0] = 1;
1285}
1286
Denys Vlasenko3129f702018-12-09 12:04:44 +01001287// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001288static void bc_num_init(BcNum *n, size_t req)
1289{
1290 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001291 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001292 n->num = xmalloc(req);
1293 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001294 n->rdx = 0;
1295 n->len = 0;
1296 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001297}
1298
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001299static void bc_num_init_DEF_SIZE(BcNum *n)
1300{
1301 bc_num_init(n, BC_NUM_DEF_SIZE);
1302}
1303
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001304static void bc_num_expand(BcNum *n, size_t req)
1305{
1306 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1307 if (req > n->cap) {
1308 n->num = xrealloc(n->num, req);
1309 n->cap = req;
1310 }
1311}
1312
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001313static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001314{
1315 free(((BcNum *) num)->num);
1316}
1317
1318static void bc_num_copy(BcNum *d, BcNum *s)
1319{
1320 if (d != s) {
1321 bc_num_expand(d, s->cap);
1322 d->len = s->len;
1323 d->neg = s->neg;
1324 d->rdx = s->rdx;
1325 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1326 }
1327}
1328
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001329static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001330{
1331 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001332 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001333
Denys Vlasenko1557b762018-12-22 21:37:46 +01001334 result = 0;
1335 i = n->len;
1336 while (i > n->rdx) {
1337 unsigned long prev = result;
1338 result = result * 10 + n->num[--i];
1339 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1340 // 0x1ff00000 * 10 is 0x13f600000,
1341 // or 0x3f600000 truncated to 32 bits. Which is larger.
1342 // However, (N*10)/8 < N check is always correct.
1343 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001344 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001345 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001346 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001347
Denys Vlasenko29301232018-12-11 15:29:32 +01001348 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001349}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001350#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1351
1352static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1353{
1354 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1355
1356 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1357}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001358#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001359
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001360#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1361# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1362#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1363# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1364#endif
1365// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1366// would not hit realloc() code path - not good if num[] is not malloced
1367
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001368static void bc_num_ulong2num(BcNum *n, unsigned long val)
1369{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001370 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001371
1372 bc_num_zero(n);
1373
1374 if (val == 0) return;
1375
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001376 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001377
1378 ptr = n->num;
1379 for (;;) {
1380 n->len++;
1381 *ptr++ = val % 10;
1382 val /= 10;
1383 if (val == 0) break;
1384 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001385}
1386
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001387static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001388{
1389 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001390 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001391 a[i] -= b[i];
1392 for (j = i; a[j] < 0;) {
1393 a[j++] += 10;
1394 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001395 }
1396 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001397}
1398
1399static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1400{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001401 size_t i = len;
1402 for (;;) {
1403 int c;
1404 if (i == 0)
1405 return 0;
1406 i--;
1407 c = a[i] - b[i];
1408 if (c != 0) {
1409 i++;
1410 if (c < 0)
1411 return -i;
1412 return i;
1413 }
1414 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001415}
1416
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001417#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1418#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1419#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1420//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1421static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1422{
1423 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1424}
1425//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1426static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1427{
1428 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1429}
1430
Gavin Howard01055ba2018-11-03 11:00:21 -06001431static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1432{
1433 size_t i, min, a_int, b_int, diff;
1434 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001435 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001436 ssize_t cmp;
1437
1438 if (a == b) return 0;
1439 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1440 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001441
1442 if (a->neg != b->neg) // signs of a and b differ
1443 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1444 return (int)b->neg - (int)a->neg;
1445 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001446
1447 a_int = BC_NUM_INT(a);
1448 b_int = BC_NUM_INT(b);
1449 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001450
1451 if (a_int != 0) return (ssize_t) a_int;
1452
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001453 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001454 if (a_max) {
1455 min = b->rdx;
1456 diff = a->rdx - b->rdx;
1457 max_num = a->num + diff;
1458 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001459 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1460 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001461 min = a->rdx;
1462 diff = b->rdx - a->rdx;
1463 max_num = b->num + diff;
1464 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001465 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001466 }
1467
1468 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001469 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001470
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001471 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001472 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001473 }
1474
1475 return 0;
1476}
1477
1478static void bc_num_truncate(BcNum *n, size_t places)
1479{
1480 if (places == 0) return;
1481
1482 n->rdx -= places;
1483
1484 if (n->len != 0) {
1485 n->len -= places;
1486 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1487 }
1488}
1489
1490static void bc_num_extend(BcNum *n, size_t places)
1491{
1492 size_t len = n->len + places;
1493
1494 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001495 if (n->cap < len) bc_num_expand(n, len);
1496
1497 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1498 memset(n->num, 0, sizeof(BcDig) * places);
1499
1500 n->len += places;
1501 n->rdx += places;
1502 }
1503}
1504
1505static void bc_num_clean(BcNum *n)
1506{
1507 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1508 if (n->len == 0)
1509 n->neg = false;
1510 else if (n->len < n->rdx)
1511 n->len = n->rdx;
1512}
1513
1514static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1515{
1516 if (n->rdx < scale)
1517 bc_num_extend(n, scale - n->rdx);
1518 else
1519 bc_num_truncate(n, n->rdx - scale);
1520
1521 bc_num_clean(n);
1522 if (n->len != 0) n->neg = !neg1 != !neg2;
1523}
1524
1525static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1526 BcNum *restrict b)
1527{
1528 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001529 b->len = n->len - idx;
1530 a->len = idx;
1531 a->rdx = b->rdx = 0;
1532
1533 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1534 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001535 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001536 bc_num_zero(b);
1537 bc_num_copy(a, n);
1538 }
1539
1540 bc_num_clean(a);
1541 bc_num_clean(b);
1542}
1543
Denys Vlasenko29301232018-12-11 15:29:32 +01001544static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001545{
Denys Vlasenko29301232018-12-11 15:29:32 +01001546 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001547
1548 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1549 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1550 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001551 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001552 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001553
1554 if (n->rdx >= places)
1555 n->rdx -= places;
1556 else {
1557 bc_num_extend(n, places - n->rdx);
1558 n->rdx = 0;
1559 }
1560
1561 bc_num_clean(n);
1562
Denys Vlasenko29301232018-12-11 15:29:32 +01001563 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001564}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001565#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001566
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001567typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1568
1569static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1570 BcNumBinaryOp op, size_t req)
1571{
1572 BcStatus s;
1573 BcNum num2, *ptr_a, *ptr_b;
1574 bool init = false;
1575
1576 if (c == a) {
1577 ptr_a = &num2;
1578 memcpy(ptr_a, c, sizeof(BcNum));
1579 init = true;
1580 } else
1581 ptr_a = a;
1582
1583 if (c == b) {
1584 ptr_b = &num2;
1585 if (c != a) {
1586 memcpy(ptr_b, c, sizeof(BcNum));
1587 init = true;
1588 }
1589 } else
1590 ptr_b = b;
1591
1592 if (init)
1593 bc_num_init(c, req);
1594 else
1595 bc_num_expand(c, req);
1596
1597 s = BC_STATUS_SUCCESS;
1598 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1599
1600 if (init) bc_num_free(&num2);
1601
1602 RETURN_STATUS(s);
1603}
1604#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1605
1606static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1607static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1608static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1609static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1610static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1611static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1612
1613static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1614{
1615 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1616 (void) scale;
1617 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1618}
1619
1620static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1621{
1622 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1623 (void) scale;
1624 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1625}
1626
1627static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1628{
1629 size_t req = BC_NUM_MREQ(a, b, scale);
1630 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1631}
1632
1633static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1634{
1635 size_t req = BC_NUM_MREQ(a, b, scale);
1636 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1637}
1638
1639static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1640{
1641 size_t req = BC_NUM_MREQ(a, b, scale);
1642 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1643}
1644
1645static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1646{
1647 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1648}
1649
Denys Vlasenko10bde142018-12-27 18:23:58 +01001650static const BcNumBinaryOp zxc_program_ops[] = {
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001651 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1652};
1653#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1654#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1655#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1656#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1657#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1658#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1659
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001660static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001661{
1662 BcNum one;
1663 BcDig num[2];
1664
1665 one.cap = 2;
1666 one.num = num;
1667 bc_num_one(&one);
1668
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001669 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001670}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001671#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001672
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001673static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001674{
1675 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1676 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001677 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001678
1679 // Because this function doesn't need to use scale (per the bc spec),
1680 // I am hijacking it to say whether it's doing an add or a subtract.
1681
1682 if (a->len == 0) {
1683 bc_num_copy(c, b);
1684 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001685 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001686 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001687 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001688 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001689 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001690 }
1691
1692 c->neg = a->neg;
1693 c->rdx = BC_MAX(a->rdx, b->rdx);
1694 min_rdx = BC_MIN(a->rdx, b->rdx);
1695 c->len = 0;
1696
1697 if (a->rdx > b->rdx) {
1698 diff = a->rdx - b->rdx;
1699 ptr = a->num;
1700 ptr_a = a->num + diff;
1701 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001702 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001703 diff = b->rdx - a->rdx;
1704 ptr = b->num;
1705 ptr_a = a->num;
1706 ptr_b = b->num + diff;
1707 }
1708
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001709 ptr_c = c->num;
1710 for (i = 0; i < diff; ++i, ++c->len)
1711 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001712
1713 ptr_c += diff;
1714 a_int = BC_NUM_INT(a);
1715 b_int = BC_NUM_INT(b);
1716
1717 if (a_int > b_int) {
1718 min_int = b_int;
1719 max = a_int;
1720 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001721 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001722 min_int = a_int;
1723 max = b_int;
1724 ptr = ptr_b;
1725 }
1726
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001727 carry = 0;
1728 for (i = 0; i < min_rdx + min_int; ++i) {
1729 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001730 carry = in / 10;
1731 ptr_c[i] = (BcDig)(in % 10);
1732 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001733 for (; i < max + min_rdx; ++i) {
1734 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001735 carry = in / 10;
1736 ptr_c[i] = (BcDig)(in % 10);
1737 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001738 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001739
1740 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1741
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001742 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001743}
1744
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001745static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001746{
Gavin Howard01055ba2018-11-03 11:00:21 -06001747 ssize_t cmp;
1748 BcNum *minuend, *subtrahend;
1749 size_t start;
1750 bool aneg, bneg, neg;
1751
1752 // Because this function doesn't need to use scale (per the bc spec),
1753 // I am hijacking it to say whether it's doing an add or a subtract.
1754
1755 if (a->len == 0) {
1756 bc_num_copy(c, b);
1757 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001758 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001759 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001760 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001761 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001763 }
1764
1765 aneg = a->neg;
1766 bneg = b->neg;
1767 a->neg = b->neg = false;
1768
1769 cmp = bc_num_cmp(a, b);
1770
1771 a->neg = aneg;
1772 b->neg = bneg;
1773
1774 if (cmp == 0) {
1775 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001776 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001777 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001778 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001779 neg = a->neg;
1780 minuend = a;
1781 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001782 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001783 neg = b->neg;
1784 if (sub) neg = !neg;
1785 minuend = b;
1786 subtrahend = a;
1787 }
1788
1789 bc_num_copy(c, minuend);
1790 c->neg = neg;
1791
1792 if (c->rdx < subtrahend->rdx) {
1793 bc_num_extend(c, subtrahend->rdx - c->rdx);
1794 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001795 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001796 start = c->rdx - subtrahend->rdx;
1797
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001798 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001799
1800 bc_num_clean(c);
1801
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001802 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001803}
1804
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001805static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001806 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001807#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001808{
1809 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001810 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001811 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001812 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001813
Gavin Howard01055ba2018-11-03 11:00:21 -06001814 if (a->len == 0 || b->len == 0) {
1815 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001816 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001817 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001818 aone = BC_NUM_ONE(a);
1819 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001820 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001821 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001822 }
1823
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001824 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1825 || a->len < BC_NUM_KARATSUBA_LEN
1826 || b->len < BC_NUM_KARATSUBA_LEN
1827 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001828 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001829
Gavin Howard01055ba2018-11-03 11:00:21 -06001830 bc_num_expand(c, a->len + b->len + 1);
1831
1832 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001833 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001834
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001835 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001836 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001837 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001838 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001839 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001840 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001841 carry = in / 10;
1842 c->num[i + j] = (BcDig)(in % 10);
1843 }
1844
1845 c->num[i + j] += (BcDig) carry;
1846 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001847
Denys Vlasenko14767602018-12-27 22:52:13 +01001848#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001849 // a=2^1000000
1850 // a*a <- without check below, this will not be interruptible
1851 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001852#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001853 }
1854
1855 c->len = len;
1856
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001857 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001858 }
1859
1860 bc_num_init(&l1, max);
1861 bc_num_init(&h1, max);
1862 bc_num_init(&l2, max);
1863 bc_num_init(&h2, max);
1864 bc_num_init(&m1, max);
1865 bc_num_init(&m2, max);
1866 bc_num_init(&z0, max);
1867 bc_num_init(&z1, max);
1868 bc_num_init(&z2, max);
1869 bc_num_init(&temp, max + max);
1870
1871 bc_num_split(a, max2, &l1, &h1);
1872 bc_num_split(b, max2, &l2, &h2);
1873
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001874 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001875 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001876 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001877 if (s) goto err;
1878
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001879 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001880 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001881 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001882 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001883 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001884 if (s) goto err;
1885
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001886 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001887 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001888 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001889 if (s) goto err;
1890
Denys Vlasenko29301232018-12-11 15:29:32 +01001891 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001892 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001893 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001894 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001895 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001896 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001897 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001898 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001899 bc_num_free(&temp);
1900 bc_num_free(&z2);
1901 bc_num_free(&z1);
1902 bc_num_free(&z0);
1903 bc_num_free(&m2);
1904 bc_num_free(&m1);
1905 bc_num_free(&h2);
1906 bc_num_free(&l2);
1907 bc_num_free(&h1);
1908 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001909 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001910}
1911
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001912static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001913{
1914 BcStatus s;
1915 BcNum cpa, cpb;
1916 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1917
1918 scale = BC_MAX(scale, a->rdx);
1919 scale = BC_MAX(scale, b->rdx);
1920 scale = BC_MIN(a->rdx + b->rdx, scale);
1921 maxrdx = BC_MAX(maxrdx, scale);
1922
1923 bc_num_init(&cpa, a->len);
1924 bc_num_init(&cpb, b->len);
1925
1926 bc_num_copy(&cpa, a);
1927 bc_num_copy(&cpb, b);
1928 cpa.neg = cpb.neg = false;
1929
Denys Vlasenko29301232018-12-11 15:29:32 +01001930 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001931 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001932 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001933 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001934 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001935 if (s) goto err;
1936
1937 maxrdx += scale;
1938 bc_num_expand(c, c->len + maxrdx);
1939
1940 if (c->len < maxrdx) {
1941 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1942 c->len += maxrdx;
1943 }
1944
1945 c->rdx = maxrdx;
1946 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001947 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001948 bc_num_free(&cpb);
1949 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001950 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001951}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001952#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001953
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001954static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001955{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001956 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001957 size_t len, end, i;
1958 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001959
1960 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001961 RETURN_STATUS(bc_error("divide by zero"));
1962 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001963 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001964 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001965 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001966 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001967 bc_num_copy(c, a);
1968 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001969 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001970 }
1971
1972 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1973 bc_num_copy(&cp, a);
1974 len = b->len;
1975
1976 if (len > cp.len) {
1977 bc_num_expand(&cp, len + 2);
1978 bc_num_extend(&cp, len - cp.len);
1979 }
1980
1981 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1982 cp.rdx -= b->rdx;
1983 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1984
1985 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001986 for (;;) {
1987 if (len == 0) break;
1988 len--;
1989 if (b->num[len] != 0)
1990 break;
1991 }
1992 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001993 }
1994
1995 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1996
1997 // We want an extra zero in front to make things simpler.
1998 cp.num[cp.len++] = 0;
1999 end = cp.len - len;
2000
2001 bc_num_expand(c, cp.len);
2002
2003 bc_num_zero(c);
2004 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2005 c->rdx = cp.rdx;
2006 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002007
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002008 s = BC_STATUS_SUCCESS;
2009 for (i = end - 1; i < end; --i) {
2010 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002011 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002012 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2013 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002014 c->num[i] = q;
Denys Vlasenko14767602018-12-27 22:52:13 +01002015#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenkof381a882018-12-05 19:21:34 +01002016 // a=2^100000
2017 // scale=40000
2018 // 1/a <- without check below, this will not be interruptible
2019 if (G_interrupt) {
2020 s = BC_STATUS_FAILURE;
2021 break;
2022 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002023#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002024 }
2025
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002026 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002027 bc_num_free(&cp);
2028
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002029 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002030}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002031#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002032
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002033static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002034 BcNum *restrict d, size_t scale, size_t ts)
2035{
2036 BcStatus s;
2037 BcNum temp;
2038 bool neg;
2039
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002040 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002041 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002042
2043 if (a->len == 0) {
2044 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002045 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002046 }
2047
2048 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002049 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002050 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002051
2052 if (scale != 0) scale = ts;
2053
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002054 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002055 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002056 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002057 if (s) goto err;
2058
2059 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2060
2061 neg = d->neg;
2062 bc_num_retireMul(d, ts, a->neg, b->neg);
2063 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002064 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002065 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002066 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002067}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002068#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002069
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002070static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002071{
2072 BcStatus s;
2073 BcNum c1;
2074 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2075
2076 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002077 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002078 bc_num_free(&c1);
2079
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002080 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002081}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002082#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002083
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002084static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002085{
2086 BcStatus s = BC_STATUS_SUCCESS;
2087 BcNum copy;
2088 unsigned long pow;
2089 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002090 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002091
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002092 // GNU bc does not allow 2^2.0 - we do
2093 for (i = 0; i < b->rdx; i++)
2094 if (b->num[i] != 0)
2095 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002096
2097 if (b->len == 0) {
2098 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002099 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002100 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002101 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002102 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002103 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002104 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002105 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002106 if (!b->neg)
2107 bc_num_copy(c, a);
2108 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002109 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002110 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002111 }
2112
2113 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002114 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002115 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002116 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002117
2118 bc_num_init(&copy, a->len);
2119 bc_num_copy(&copy, a);
2120
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002121 if (!neg) {
2122 if (a->rdx > scale)
2123 scale = a->rdx;
2124 if (a->rdx * pow < scale)
2125 scale = a->rdx * pow;
2126 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002127
Gavin Howard01055ba2018-11-03 11:00:21 -06002128
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002129 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002130 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002131 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002132 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002133 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002134 //if (G_interrupt) {
2135 // s = BC_STATUS_FAILURE;
2136 // goto err;
2137 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002138 }
2139
Gavin Howard01055ba2018-11-03 11:00:21 -06002140 bc_num_copy(c, &copy);
2141
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002142 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002143 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002144 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002145 if (s) goto err;
2146
2147 if (pow & 1) {
2148 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002149 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002150 if (s) goto err;
2151 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002152 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002153 //if (G_interrupt) {
2154 // s = BC_STATUS_FAILURE;
2155 // goto err;
2156 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002157 }
2158
2159 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002160 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002161 if (s) goto err;
2162 }
2163
Gavin Howard01055ba2018-11-03 11:00:21 -06002164 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2165
2166 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002167 for (i = 0; i < c->len; ++i)
2168 if (c->num[i] != 0)
2169 goto skip;
2170 bc_num_setToZero(c, scale);
2171 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002172
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002173 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002174 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002175 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002176}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002177#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002178
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002179static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002180{
2181 BcStatus s;
2182 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002183 BcDig half_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002184 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2185 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2186
2187 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2188 bc_num_expand(b, req);
2189
2190 if (a->len == 0) {
2191 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002192 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002193 }
2194 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002195 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002196 }
2197 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002198 bc_num_one(b);
2199 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002200 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002201 }
2202
2203 scale = BC_MAX(scale, a->rdx) + 1;
2204 len = a->len + scale;
2205
2206 bc_num_init(&num1, len);
2207 bc_num_init(&num2, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002208
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002209 half.cap = ARRAY_SIZE(half_digs);
2210 half.num = half_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002211 bc_num_one(&half);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002212 half_digs[0] = 5;
Gavin Howard01055ba2018-11-03 11:00:21 -06002213 half.rdx = 1;
2214
2215 bc_num_init(&f, len);
2216 bc_num_init(&fprime, len);
2217
2218 x0 = &num1;
2219 x1 = &num2;
2220
2221 bc_num_one(x0);
2222 pow = BC_NUM_INT(a);
2223
2224 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002225 if (pow & 1)
2226 x0->num[0] = 2;
2227 else
2228 x0->num[0] = 6;
2229
2230 pow -= 2 - (pow & 1);
2231
2232 bc_num_extend(x0, pow);
2233
2234 // Make sure to move the radix back.
2235 x0->rdx -= pow;
2236 }
2237
2238 x0->rdx = digs = digs1 = 0;
2239 resrdx = scale + 2;
2240 len = BC_NUM_INT(x0) + resrdx - 1;
2241
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002242 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002243 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002244 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002245 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002246 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002247 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002248 if (s) goto err;
2249
2250 cmp = bc_num_cmp(x1, x0);
2251 digs = x1->len - (unsigned long long) llabs(cmp);
2252
2253 if (cmp == cmp2 && digs == digs1)
2254 times += 1;
2255 else
2256 times = 0;
2257
2258 resrdx += times > 4;
2259
2260 cmp2 = cmp1;
2261 cmp1 = cmp;
2262 digs1 = digs;
2263
2264 temp = x0;
2265 x0 = x1;
2266 x1 = temp;
2267 }
2268
Gavin Howard01055ba2018-11-03 11:00:21 -06002269 bc_num_copy(b, x0);
2270 scale -= 1;
2271 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002272 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002273 bc_num_free(&fprime);
2274 bc_num_free(&f);
Gavin Howard01055ba2018-11-03 11:00:21 -06002275 bc_num_free(&num2);
2276 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002277 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002278}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002279#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002280
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002281static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002282 size_t scale)
2283{
2284 BcStatus s;
2285 BcNum num2, *ptr_a;
2286 bool init = false;
2287 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2288
2289 if (c == a) {
2290 memcpy(&num2, c, sizeof(BcNum));
2291 ptr_a = &num2;
2292 bc_num_init(c, len);
2293 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002294 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002295 ptr_a = a;
2296 bc_num_expand(c, len);
2297 }
2298
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002299 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002300
2301 if (init) bc_num_free(&num2);
2302
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002303 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002304}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002305#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002306
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002307#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002308static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002309{
2310 BcStatus s;
2311 BcNum base, exp, two, temp;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01002312 BcDig two_digs[1];
Gavin Howard01055ba2018-11-03 11:00:21 -06002313
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002314 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002315 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002316 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002317 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002318 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002319 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002320
2321 bc_num_expand(d, c->len);
2322 bc_num_init(&base, c->len);
2323 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002324 bc_num_init(&temp, b->len);
2325
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002326 two.cap = ARRAY_SIZE(two_digs);
2327 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002328 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002329 two_digs[0] = 2;
2330
Gavin Howard01055ba2018-11-03 11:00:21 -06002331 bc_num_one(d);
2332
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002333 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002334 if (s) goto err;
2335 bc_num_copy(&exp, b);
2336
2337 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002338 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002339 if (s) goto err;
2340
2341 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002342 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002343 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002344 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002345 if (s) goto err;
2346 }
2347
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002348 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002349 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002350 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002351 if (s) goto err;
2352 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002353 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002354 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002355 bc_num_free(&exp);
2356 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002357 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002358}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002359#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002360#endif // ENABLE_DC
2361
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002362static FAST_FUNC void bc_string_free(void *string)
2363{
2364 free(*(char**)string);
2365}
2366
Gavin Howard01055ba2018-11-03 11:00:21 -06002367static void bc_func_init(BcFunc *f)
2368{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002369 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002370 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2371 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002372 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2373 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002374 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002375}
2376
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002377static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002378{
2379 BcFunc *f = (BcFunc *) func;
2380 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002381 IF_BC(bc_vec_free(&f->labels);)
2382 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002383 IF_BC(bc_vec_free(&f->strs);)
2384 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002385}
2386
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002387static void bc_array_expand(BcVec *a, size_t len);
2388
Gavin Howard01055ba2018-11-03 11:00:21 -06002389static void bc_array_init(BcVec *a, bool nums)
2390{
2391 if (nums)
2392 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2393 else
2394 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2395 bc_array_expand(a, 1);
2396}
2397
Gavin Howard01055ba2018-11-03 11:00:21 -06002398static void bc_array_expand(BcVec *a, size_t len)
2399{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002400 if (a->dtor == bc_num_free
2401 // && a->size == sizeof(BcNum) - always true
2402 ) {
2403 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002404 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002405 bc_num_init_DEF_SIZE(&n);
2406 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002407 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002408 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002409 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002410 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002411 bc_array_init(&v, true);
2412 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002413 }
2414 }
2415}
2416
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002417static void bc_array_copy(BcVec *d, const BcVec *s)
2418{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002419 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002420 size_t i;
2421
2422 bc_vec_pop_all(d);
2423 bc_vec_expand(d, s->cap);
2424 d->len = s->len;
2425
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002426 dnum = (void*)d->v;
2427 snum = (void*)s->v;
2428 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002429 bc_num_init(dnum, snum->len);
2430 bc_num_copy(dnum, snum);
2431 }
2432}
2433
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002434#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002435static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002436{
2437 d->t = src->t;
2438
2439 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002440 case XC_RESULT_TEMP:
2441 case XC_RESULT_IBASE:
2442 case XC_RESULT_SCALE:
2443 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002444 bc_num_init(&d->d.n, src->d.n.len);
2445 bc_num_copy(&d->d.n, &src->d.n);
2446 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002447 case XC_RESULT_VAR:
2448 case XC_RESULT_ARRAY:
2449 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002450 d->d.id.name = xstrdup(src->d.id.name);
2451 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002452 case XC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002453 IF_BC(case BC_RESULT_LAST:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002454 IF_BC(case BC_RESULT_ONE:)
2455 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002456 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2457 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002458 }
2459}
2460#endif // ENABLE_DC
2461
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002462static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002463{
2464 BcResult *r = (BcResult *) result;
2465
2466 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002467 case XC_RESULT_TEMP:
2468 case XC_RESULT_IBASE:
2469 case XC_RESULT_SCALE:
2470 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002471 bc_num_free(&r->d.n);
2472 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002473 case XC_RESULT_VAR:
2474 case XC_RESULT_ARRAY:
2475 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002476 free(r->d.id.name);
2477 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002478 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002479 // Do nothing.
2480 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002481 }
2482}
2483
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002484static int bad_input_byte(char c)
2485{
2486 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2487 || c > 0x7e
2488 ) {
2489 bc_error_fmt("illegal character 0x%02x", c);
2490 return 1;
2491 }
2492 return 0;
2493}
2494
Denys Vlasenko10bde142018-12-27 18:23:58 +01002495static void xc_read_line(BcVec *vec, FILE *fp)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002496{
2497 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002498 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002499 fflush_and_check();
2500
Denys Vlasenko14767602018-12-27 22:52:13 +01002501#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002502 if (G_interrupt) { // ^C was pressed
2503 intr:
2504 if (fp != stdin) {
2505 // ^C while running a script (bc SCRIPT): die.
2506 // We do not return to interactive prompt:
2507 // user might be running us from a shell,
2508 // and SCRIPT might be intended to terminate
2509 // (e.g. contain a "halt" stmt).
2510 // ^C dropping user into a bc prompt instead of
2511 // the shell would be unexpected.
2512 xfunc_die();
2513 }
2514 // ^C while interactive input
2515 G_interrupt = 0;
2516 // GNU bc says "interrupted execution."
2517 // GNU dc says "Interrupt!"
2518 fputs("\ninterrupted execution\n", stderr);
2519 }
2520
2521# if ENABLE_FEATURE_EDITING
2522 if (G_ttyin && fp == stdin) {
2523 int n, i;
2524# define line_buf bb_common_bufsiz1
2525 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2526 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2527 if (n == 0) // ^C
2528 goto intr;
2529 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2530 return;
2531 }
2532 i = 0;
2533 for (;;) {
2534 char c = line_buf[i++];
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002535 if (c == '\0') break;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002536 if (bad_input_byte(c)) goto again;
2537 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002538 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002539# undef line_buf
2540 } else
2541# endif
2542#endif
2543 {
2544 int c;
2545 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002546
2547 do {
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002548 get_char:
Denys Vlasenko14767602018-12-27 22:52:13 +01002549#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002550 if (G_interrupt) {
2551 // ^C was pressed: ignore entire line, get another one
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002552 goto again;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002553 }
2554#endif
Denys Vlasenko51b510a2019-01-01 02:19:02 +01002555 c = fgetc(fp);
2556 if (c == '\0')
2557 goto get_char;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002558 if (c == EOF) {
2559 if (ferror(fp))
2560 bb_perror_msg_and_die("input error");
2561 // Note: EOF does not append '\n'
2562 break;
2563 }
2564 bad_chars |= bad_input_byte(c);
2565 bc_vec_pushByte(vec, (char)c);
2566 } while (c != '\n');
2567
2568 if (bad_chars) {
2569 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002570 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002571 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002572 goto again;
2573 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002574 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002575 }
2576 bc_vec_pushZeroByte(vec);
2577 }
2578}
2579
2580//
2581// Parsing routines
2582//
2583
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002584// "Input numbers may contain the characters 0-9 and A-Z.
2585// (Note: They must be capitals. Lower case letters are variable names.)
2586// Single digit numbers always have the value of the digit regardless of
2587// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2588// all input digits greater or equal to ibase to the value of ibase-1.
2589// This makes the number ZZZ always be the largest 3 digit number of the
2590// input base."
2591static bool xc_num_strValid(const char *val)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002592{
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002593 bool radix = false;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002594 for (;;) {
2595 BcDig c = *val++;
2596 if (c == '\0')
2597 break;
2598 if (c == '.') {
2599 if (radix) return false;
2600 radix = true;
2601 continue;
2602 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002603 if ((c < '0' || c > '9') && (c < 'A' || c > 'Z'))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002604 return false;
2605 }
2606 return true;
2607}
2608
2609// Note: n is already "bc_num_zero()"ed,
2610// leading zeroes in "val" are removed
2611static void bc_num_parseDecimal(BcNum *n, const char *val)
2612{
2613 size_t len, i;
2614 const char *ptr;
2615
2616 len = strlen(val);
2617 if (len == 0)
2618 return;
2619
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002620 bc_num_expand(n, len + 1); // +1 for e.g. "A" converting into 10
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002621
2622 ptr = strchr(val, '.');
2623
2624 n->rdx = 0;
2625 if (ptr != NULL)
2626 n->rdx = (size_t)((val + len) - (ptr + 1));
2627
2628 for (i = 0; val[i]; ++i) {
2629 if (val[i] != '0' && val[i] != '.') {
2630 // Not entirely zero value - convert it, and exit
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002631 if (len == 1) {
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002632 unsigned c = val[0] - '0';
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002633 n->len = 1;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002634 if (c > 9) { // A-Z => 10-36
2635 n->len = 2;
2636 c -= ('A' - '9' - 1);
2637 n->num[1] = c/10;
2638 c = c%10;
2639 }
2640 n->num[0] = c;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002641 break;
2642 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002643 i = len - 1;
2644 for (;;) {
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002645 char c = val[i] - '0';
2646 if (c > 9) // A-Z => 9
2647 c = 9;
2648 n->num[n->len] = c;
2649 n->len++;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002650 skip_dot:
2651 if (i == 0) break;
2652 if (val[--i] == '.') goto skip_dot;
2653 }
2654 break;
2655 }
2656 }
2657 // if for() exits without hitting if(), the value is entirely zero
2658}
2659
2660// Note: n is already "bc_num_zero()"ed,
2661// leading zeroes in "val" are removed
2662static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2663{
2664 BcStatus s;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002665 BcNum mult, result;
2666 BcNum temp;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002667 BcNum base;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002668 BcDig temp_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002669 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002670 size_t digits;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002671
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002672 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002673
2674 temp.cap = ARRAY_SIZE(temp_digs);
2675 temp.num = temp_digs;
2676
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002677 base.cap = ARRAY_SIZE(base_digs);
2678 base.num = base_digs;
2679 bc_num_ulong2num(&base, base_t);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002680 base_t--;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002681
2682 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002683 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002684 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002685
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002686 c = *val++;
2687 if (c == '\0') goto int_err;
2688 if (c == '.') break;
2689
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002690 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2691 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002692
2693 s = zbc_num_mul(n, &base, &mult, 0);
2694 if (s) goto int_err;
2695 bc_num_ulong2num(&temp, v);
2696 s = zbc_num_add(&mult, &temp, n, 0);
2697 if (s) goto int_err;
2698 }
2699
2700 bc_num_init(&result, base.len);
2701 //bc_num_zero(&result); - already is
2702 bc_num_one(&mult);
2703
2704 digits = 0;
2705 for (;;) {
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002706 unsigned v;
Denys Vlasenko8797adc2018-12-31 19:50:06 +01002707 char c;
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002708
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002709 c = *val++;
2710 if (c == '\0') break;
2711 digits++;
2712
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002713 v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
2714 if (v > base_t) v = base_t;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002715
2716 s = zbc_num_mul(&result, &base, &result, 0);
2717 if (s) goto err;
2718 bc_num_ulong2num(&temp, v);
2719 s = zbc_num_add(&result, &temp, &result, 0);
2720 if (s) goto err;
2721 s = zbc_num_mul(&mult, &base, &mult, 0);
2722 if (s) goto err;
2723 }
2724
2725 s = zbc_num_div(&result, &mult, &result, digits);
2726 if (s) goto err;
2727 s = zbc_num_add(n, &result, n, digits);
2728 if (s) goto err;
2729
2730 if (n->len != 0) {
2731 if (n->rdx < digits)
2732 bc_num_extend(n, digits - n->rdx);
2733 } else
2734 bc_num_zero(n);
2735 err:
2736 bc_num_free(&result);
2737 int_err:
2738 bc_num_free(&mult);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002739}
2740
Denys Vlasenko10bde142018-12-27 18:23:58 +01002741static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002742{
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002743 size_t i;
2744
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002745 if (!xc_num_strValid(val))
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002746 RETURN_STATUS(bc_error("bad number string"));
2747
2748 bc_num_zero(n);
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002749 while (*val == '0')
2750 val++;
2751 for (i = 0; ; ++i) {
2752 if (val[i] == '\0')
2753 RETURN_STATUS(BC_STATUS_SUCCESS);
2754 if (val[i] != '.' && val[i] != '0')
2755 break;
2756 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002757
Denys Vlasenko680ccd32018-12-31 19:42:13 +01002758 if (base_t == 10 || val[1] == '\0')
2759 // Decimal, or single-digit number
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002760 bc_num_parseDecimal(n, val);
2761 else
2762 bc_num_parseBase(n, val, base_t);
2763
2764 RETURN_STATUS(BC_STATUS_SUCCESS);
2765}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002766#define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002767
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002768// p->lex_inbuf points to the current string to be parsed.
2769// if p->lex_inbuf points to '\0', it's either EOF or it points after
2770// last processed line's terminating '\n' (and more reading needs to be done
2771// to get next character).
2772//
2773// If you are in a situation where that is a possibility, call peek_inbuf().
2774// If necessary, it performs more reading and changes p->lex_inbuf,
2775// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2776// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2777// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2778//
2779// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2780// it returns current char and advances the pointer (if not EOF).
2781// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2782//
2783// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2784// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2785// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002786//
2787// bc cases to test interactively:
2788// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2789// 1 #comment/* - prints "1<newline>" at once
2790// 1 #comment" - prints "1<newline>" at once
2791// 1\#comment - error at once (\ is not a line continuation)
2792// 1 + /*"*/2 - prints "3<newline>" at once
2793// 1 + /*#*/2 - prints "3<newline>" at once
2794// "str\" - prints "str\" at once
2795// "str#" - prints "str#" at once
2796// "str/*" - prints "str/*" at once
2797// "str#\ - waits for second line
2798// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002799static char peek_inbuf(void)
2800{
Denys Vlasenko2747f612018-12-31 18:48:10 +01002801 if (*G.prs.lex_inbuf == '\0'
2802 && G.prs.lex_input_fp
2803 ) {
2804 xc_read_line(&G.input_buffer, G.prs.lex_input_fp);
2805 G.prs.lex_inbuf = G.input_buffer.v;
2806 if (G.input_buffer.len <= 1) // on EOF, len is 1 (NUL byte)
2807 G.prs.lex_input_fp = NULL;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002808 }
2809 return *G.prs.lex_inbuf;
2810}
2811static char eat_inbuf(void)
2812{
2813 char c = peek_inbuf();
2814 if (c) G.prs.lex_inbuf++;
2815 return c;
2816}
2817
Denys Vlasenko10bde142018-12-27 18:23:58 +01002818static void xc_lex_lineComment(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002819{
2820 BcParse *p = &G.prs;
2821 char c;
2822
2823 // Try: echo -n '#foo' | bc
2824 p->lex = XC_LEX_WHITESPACE;
2825
Denys Vlasenko2747f612018-12-31 18:48:10 +01002826 // Not peek_inbuf(): we depend on input being done in whole lines:
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002827 // '\0' which isn't the EOF can only be seen after '\n'.
2828 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2829 p->lex_inbuf++;
2830}
2831
Denys Vlasenko10bde142018-12-27 18:23:58 +01002832static void xc_lex_whitespace(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002833{
2834 BcParse *p = &G.prs;
2835
2836 p->lex = XC_LEX_WHITESPACE;
2837 for (;;) {
2838 // We depend here on input being done in whole lines:
2839 // '\0' which isn't the EOF can only be seen after '\n'.
2840 char c = *p->lex_inbuf;
2841 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2842 break;
2843 if (!isspace(c))
2844 break;
2845 p->lex_inbuf++;
2846 }
2847}
2848
Denys Vlasenko10bde142018-12-27 18:23:58 +01002849static BC_STATUS zxc_lex_number(char last)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002850{
2851 BcParse *p = &G.prs;
2852 bool pt;
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002853 char last_valid_ch;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002854
2855 bc_vec_pop_all(&p->lex_strnumbuf);
2856 bc_vec_pushByte(&p->lex_strnumbuf, last);
2857
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002858// bc: "Input numbers may contain the characters 0-9 and A-Z.
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002859// (Note: They must be capitals. Lower case letters are variable names.)
2860// Single digit numbers always have the value of the digit regardless of
2861// the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes
2862// all input digits greater or equal to ibase to the value of ibase-1.
2863// This makes the number ZZZ always be the largest 3 digit number of the
2864// input base."
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002865// dc only allows A-F, the rules about single-char and multi-char are the same.
2866 last_valid_ch = (IS_BC ? 'Z' : 'F');
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002867 pt = (last == '.');
2868 p->lex = XC_LEX_NUMBER;
2869 for (;;) {
2870 // We depend here on input being done in whole lines:
2871 // '\0' which isn't the EOF can only be seen after '\n'.
2872 char c = *p->lex_inbuf;
2873 check_c:
2874 if (c == '\0')
2875 break;
2876 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2877 p->lex_inbuf += 2;
2878 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01002879 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002880 c = peek_inbuf(); // force next line to be read
2881 goto check_c;
2882 }
Denys Vlasenkod5b0fa62018-12-29 02:40:03 +01002883 if (!isdigit(c) && (c < 'A' || c > last_valid_ch)) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002884 if (c != '.') break;
2885 // if '.' was already seen, stop on second one:
2886 if (pt) break;
2887 pt = true;
2888 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01002889 // c is one of "0-9A-Z."
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002890 last = c;
2891 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2892 p->lex_inbuf++;
2893 }
2894 if (last == '.') // remove trailing '.' if any
2895 bc_vec_pop(&p->lex_strnumbuf);
2896 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2897
2898 G.err_line = G.prs.lex_line;
2899 RETURN_STATUS(BC_STATUS_SUCCESS);
2900}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002901#define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002902
Denys Vlasenko10bde142018-12-27 18:23:58 +01002903static void xc_lex_name(void)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002904{
2905 BcParse *p = &G.prs;
2906 size_t i;
2907 const char *buf;
2908
2909 p->lex = XC_LEX_NAME;
2910
2911 // Since names can't cross lines with \<newline>,
2912 // we depend on the fact that whole line is in the buffer
2913 i = 0;
2914 buf = p->lex_inbuf - 1;
2915 for (;;) {
2916 char c = buf[i];
2917 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2918 i++;
2919 }
2920
2921#if 0 // We do not protect against people with gigabyte-long names
2922 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2923 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2924 if (i > BC_MAX_STRING)
2925 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2926 }
2927#endif
2928 bc_vec_string(&p->lex_strnumbuf, i, buf);
2929
2930 // Increment the index. We minus 1 because it has already been incremented.
2931 p->lex_inbuf += i - 1;
2932
2933 //return BC_STATUS_SUCCESS;
2934}
2935
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002936IF_BC(static BC_STATUS zbc_lex_token(void);)
2937IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002938#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2939#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002940
Denys Vlasenko10bde142018-12-27 18:23:58 +01002941static BC_STATUS zxc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002942{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002943 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002944 BcStatus s;
2945
Denys Vlasenko22314682019-01-01 21:50:14 +01002946 G.err_line = p->lex_line;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002947 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002948//why?
2949// if (p->lex_last == XC_LEX_EOF)
2950// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002951
2952 // Loop until failure or we don't have whitespace. This
2953 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002954 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002955 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002956 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002957 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002958 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002959 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002960 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002961 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002962 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002963 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002964 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2965 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002966 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002967 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002968 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002969 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002970 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002971 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002972 } while (!s && p->lex == XC_LEX_WHITESPACE);
2973 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06002974
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002975 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002976}
Denys Vlasenko10bde142018-12-27 18:23:58 +01002977#define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002978
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002979#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002980static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002981{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002982 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenko10bde142018-12-27 18:23:58 +01002983 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01002984 RETURN_STATUS(BC_STATUS_SUCCESS);
2985}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002986#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002987
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002988static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002989{
2990 BcStatus s;
Denys Vlasenko10bde142018-12-27 18:23:58 +01002991 s = zxc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002992 if (s) RETURN_STATUS(s);
2993 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002994 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002995 RETURN_STATUS(s);
2996}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002997#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002998
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002999static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003000{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003001 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003002 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003003 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003004 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003005
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003006 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3007 const char *keyword8 = bc_lex_kws[i].name8;
3008 unsigned j = 0;
3009 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3010 j++;
3011 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003012 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003013 if (keyword8[j] != '\0')
3014 continue;
3015 match:
3016 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003017 if (isalnum(buf[j]) || buf[j]=='_')
3018 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003019 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01003020 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003021 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3022 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003023 }
3024
3025 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003026 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003027 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003028 }
3029
Denys Vlasenko10bde142018-12-27 18:23:58 +01003030 xc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003031 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003032
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003033 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003034 // Prevent this:
3035 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003036 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003037 // '
3038 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003039 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003040 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003041
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003042 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003043}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003044#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003045
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003046static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003047{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003048 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003049
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003050 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003051 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003052 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003053 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003054 if (c == '\0') {
Denys Vlasenko8af11082018-12-26 21:01:41 +01003055 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003056 }
3057 if (c == '"')
3058 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003059 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003060 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003061 dbg_lex("++p->lex_line=%zd", p->lex_line);
3062 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003063 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3064 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003065 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003066 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3067 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003068
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003069 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003070 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003071}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003072#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003073
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003074static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003075{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003076 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003077 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003078 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003079 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003080 with_and_without >>= 8; // store "with" value
3081 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003082 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003083}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003084#define parse_lex_by_checking_eq_sign(with, without) \
3085 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003086
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003087static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003088{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003089 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003090
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003091 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003092 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003093 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003094 char c;
3095
3096 p->lex_inbuf++;
3097 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003098 check_star:
3099 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003100 p->lex_inbuf++;
Denys Vlasenko8af11082018-12-26 21:01:41 +01003101 c = *p->lex_inbuf; // no need to peek_inbuf()
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003102 if (c == '/')
3103 break;
3104 goto check_star;
3105 }
3106 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003107 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003108 }
Denys Vlasenko22314682019-01-01 21:50:14 +01003109 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003110 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003111 dbg_lex("++p->lex_line=%zd", p->lex_line);
3112 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003113 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003114 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003115
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003116 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003117 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003118}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003119#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003120
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003121#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003122static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003123{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003124 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003126 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003127 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003128
3129 // This is the workhorse of the lexer.
3130 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003131// case '\0': // probably never reached
3132// p->lex_inbuf--;
3133// p->lex = XC_LEX_EOF;
3134// break;
3135 case '\n':
3136 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003137 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003138 p->lex = XC_LEX_NLINE;
3139 break;
3140 case '\t':
3141 case '\v':
3142 case '\f':
3143 case '\r':
3144 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003145 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003146 break;
3147 case '!':
3148 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3149 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3150 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003151 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003152 }
3153 break;
3154 case '"':
3155 s = zbc_lex_string();
3156 break;
3157 case '#':
3158 s = zbc_POSIX_does_not_allow("'#' script comments");
3159 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003160 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003161 break;
3162 case '%':
3163 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3164 break;
3165 case '&':
3166 c2 = *p->lex_inbuf;
3167 if (c2 == '&') {
3168 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3169 if (s) RETURN_STATUS(s);
3170 p->lex_inbuf++;
3171 p->lex = BC_LEX_OP_BOOL_AND;
3172 } else {
3173 p->lex = XC_LEX_INVALID;
3174 s = bc_error_bad_character('&');
3175 }
3176 break;
3177 case '(':
3178 case ')':
3179 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3180 break;
3181 case '*':
3182 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3183 break;
3184 case '+':
3185 c2 = *p->lex_inbuf;
3186 if (c2 == '+') {
3187 p->lex_inbuf++;
3188 p->lex = BC_LEX_OP_INC;
3189 } else
3190 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3191 break;
3192 case ',':
3193 p->lex = BC_LEX_COMMA;
3194 break;
3195 case '-':
3196 c2 = *p->lex_inbuf;
3197 if (c2 == '-') {
3198 p->lex_inbuf++;
3199 p->lex = BC_LEX_OP_DEC;
3200 } else
3201 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3202 break;
3203 case '.':
3204 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003205 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003206 else {
3207 p->lex = BC_LEX_KEY_LAST;
3208 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3209 }
3210 break;
3211 case '/':
3212 c2 = *p->lex_inbuf;
3213 if (c2 == '*')
3214 s = zbc_lex_comment();
3215 else
3216 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3217 break;
3218 case '0':
3219 case '1':
3220 case '2':
3221 case '3':
3222 case '4':
3223 case '5':
3224 case '6':
3225 case '7':
3226 case '8':
3227 case '9':
3228 case 'A':
3229 case 'B':
3230 case 'C':
3231 case 'D':
3232 case 'E':
3233 case 'F':
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003234 case 'G':
3235 case 'H':
3236 case 'I':
3237 case 'J':
3238 case 'K':
3239 case 'L':
3240 case 'M':
3241 case 'N':
3242 case 'O':
3243 case 'P':
3244 case 'Q':
3245 case 'R':
3246 case 'S':
3247 case 'T':
3248 case 'U':
3249 case 'V':
3250 case 'W':
3251 case 'X':
3252 case 'Y':
3253 case 'Z':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003254 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003255 break;
3256 case ';':
3257 p->lex = BC_LEX_SCOLON;
3258 break;
3259 case '<':
3260 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3261 break;
3262 case '=':
3263 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3264 break;
3265 case '>':
3266 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3267 break;
3268 case '[':
3269 case ']':
3270 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3271 break;
3272 case '\\':
3273 if (*p->lex_inbuf == '\n') {
3274 p->lex = XC_LEX_WHITESPACE;
3275 p->lex_inbuf++;
3276 } else
3277 s = bc_error_bad_character(c);
3278 break;
3279 case '^':
3280 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3281 break;
3282 case 'a':
3283 case 'b':
3284 case 'c':
3285 case 'd':
3286 case 'e':
3287 case 'f':
3288 case 'g':
3289 case 'h':
3290 case 'i':
3291 case 'j':
3292 case 'k':
3293 case 'l':
3294 case 'm':
3295 case 'n':
3296 case 'o':
3297 case 'p':
3298 case 'q':
3299 case 'r':
3300 case 's':
3301 case 't':
3302 case 'u':
3303 case 'v':
3304 case 'w':
3305 case 'x':
3306 case 'y':
3307 case 'z':
3308 s = zbc_lex_identifier();
3309 break;
3310 case '{':
3311 case '}':
3312 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3313 break;
3314 case '|':
3315 c2 = *p->lex_inbuf;
3316 if (c2 == '|') {
3317 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3318 if (s) RETURN_STATUS(s);
3319 p->lex_inbuf++;
3320 p->lex = BC_LEX_OP_BOOL_OR;
3321 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003322 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003323 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003324 }
3325 break;
3326 default:
3327 p->lex = XC_LEX_INVALID;
3328 s = bc_error_bad_character(c);
3329 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003330 }
3331
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003332 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003333}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003334#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003335#endif // ENABLE_BC
3336
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003337#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003338static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003339{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003340 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003341 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003342 xc_lex_whitespace(); // eats whitespace (but not newline)
3343 p->lex_inbuf++; // xc_lex_name() expects this
3344 xc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003345 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003346 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003347 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003348 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3349 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003350 }
3351
Denys Vlasenko29301232018-12-11 15:29:32 +01003352 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003353}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003354#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003355
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003356static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003357{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003358 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003359 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003360
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003361 p->lex = XC_LEX_STR;
3362 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003363
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003364 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003365 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003366 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003367 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003368 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003369 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003370 if (c == '[') depth++;
3371 if (c == ']')
3372 if (--depth == 0)
3373 break;
Denys Vlasenko22314682019-01-01 21:50:14 +01003374 if (c == '\n') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003375 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003376 dbg_lex("++p->lex_line=%zd", p->lex_line);
3377 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003378 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3379 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003380 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003381 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003382 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003383
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003384 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003385 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003386}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003387#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003388
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003389#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003390static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003391{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003392 static const //BcLexType - should be this type, but narrower type saves size:
3393 uint8_t
Denys Vlasenko2beb1f62018-12-26 21:17:12 +01003394 dc_lex_regs[] ALIGN1 = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003395 XC_LEX_OP_REL_EQ, XC_LEX_OP_REL_LE, XC_LEX_OP_REL_GE, XC_LEX_OP_REL_NE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01003396 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3397 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003398 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003399 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003400
Denys Vlasenko10bde142018-12-27 18:23:58 +01003401 BcParse *p = &G.prs;
Denys Vlasenko81293c82018-12-24 01:53:55 +01003402 BcStatus s;
3403 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003404 size_t i;
3405
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003406 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003407 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003408 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003409 }
3410
Denys Vlasenko81293c82018-12-24 01:53:55 +01003411 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003412 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003413 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003414 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003415 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003416 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003417 }
3418
3419 // This is the workhorse of the lexer.
3420 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003421// case '\0': // probably never reached
3422// p->lex = XC_LEX_EOF;
3423// break;
3424 case '\n':
3425 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3426 // (and "case '\n':" is not just empty here)
3427 // only to allow interactive dc have a way to exit
3428 // "parse" stage of "parse,execute" loop
3429 // on <enter>, not on _next_ token (which would mean
3430 // commands are not executed on pressing <enter>).
3431 // IOW: typing "1p<enter>" should print "1" _at once_,
3432 // not after some more input.
3433 p->lex_line++;
Denys Vlasenko22314682019-01-01 21:50:14 +01003434 dbg_lex("++p->lex_line=%zd", p->lex_line);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003435 p->lex = XC_LEX_NLINE;
3436 break;
3437 case '\t':
3438 case '\v':
3439 case '\f':
3440 case '\r':
3441 case ' ':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003442 xc_lex_whitespace();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003443 break;
3444 case '!':
3445 c2 = *p->lex_inbuf;
3446 if (c2 == '=')
3447 p->lex = XC_LEX_OP_REL_NE;
3448 else if (c2 == '<')
3449 p->lex = XC_LEX_OP_REL_LE;
3450 else if (c2 == '>')
3451 p->lex = XC_LEX_OP_REL_GE;
3452 else
3453 RETURN_STATUS(bc_error_bad_character(c));
3454 p->lex_inbuf++;
3455 break;
3456 case '#':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003457 xc_lex_lineComment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003458 break;
3459 case '.':
3460 if (isdigit(*p->lex_inbuf))
Denys Vlasenko10bde142018-12-27 18:23:58 +01003461 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003462 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003463 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003464 break;
3465 case '0':
3466 case '1':
3467 case '2':
3468 case '3':
3469 case '4':
3470 case '5':
3471 case '6':
3472 case '7':
3473 case '8':
3474 case '9':
3475 case 'A':
3476 case 'B':
3477 case 'C':
3478 case 'D':
3479 case 'E':
3480 case 'F':
Denys Vlasenko10bde142018-12-27 18:23:58 +01003481 s = zxc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003482 break;
3483 case '[':
3484 s = zdc_lex_string();
3485 break;
3486 default:
3487 p->lex = XC_LEX_INVALID;
3488 s = bc_error_bad_character(c);
3489 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003490 }
3491
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003492 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003493}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003494#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003495#endif // ENABLE_DC
3496
Denys Vlasenko10bde142018-12-27 18:23:58 +01003497static void xc_parse_push(char i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003498{
Denys Vlasenkof706a182018-12-26 20:02:09 +01003499 BcVec *code = &G.prs.func->code;
3500 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
3501 bc_vec_pushByte(code, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003502}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003503
Denys Vlasenko10bde142018-12-27 18:23:58 +01003504static void xc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003505{
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003506#if 1
3507 BcVec *code = &G.prs.func->code;
3508 size_t pos = code->len;
3509 size_t len = strlen(name) + 1;
3510
3511 bc_vec_expand(code, pos + len);
3512 strcpy(code->v + pos, name);
3513 code->len = pos + len;
3514#else
3515 // Smaller code, but way slow:
Denys Vlasenkof706a182018-12-26 20:02:09 +01003516 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003517 xc_parse_push(*name);
Denys Vlasenkof706a182018-12-26 20:02:09 +01003518 } while (*name++);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003519#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003520}
3521
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003522// Indexes < 0xfc are encoded verbatim, else first byte is
3523// 0xfc, 0xfd, 0xfe or 0xff, encoding "1..4 bytes",
3524// followed by that many bytes, lsb first.
3525// (The above describes 32-bit case).
3526#define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t))
3527
Denys Vlasenko10bde142018-12-27 18:23:58 +01003528static void xc_parse_pushIndex(size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003529{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003530 size_t mask;
3531 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003532
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003533 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003534 if (idx < SMALL_INDEX_LIMIT) {
3535 goto push_idx;
3536 }
3537
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003538 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3539 amt = sizeof(idx);
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003540 for (;;) {
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003541 if (idx & mask) break;
3542 mask >>= 8;
3543 amt--;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003544 }
3545 // amt is at least 1 here - "one byte of length data follows"
Gavin Howard01055ba2018-11-03 11:00:21 -06003546
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01003547 xc_parse_push((SMALL_INDEX_LIMIT - 1) + amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003548
3549 while (idx != 0) {
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01003550 push_idx:
Denys Vlasenko10bde142018-12-27 18:23:58 +01003551 xc_parse_push((unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003552 idx >>= 8;
3553 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003554}
3555
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003556#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003557static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003558{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003559 xc_parse_push(BC_INST_JUMP);
3560 xc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003561}
3562
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003563static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003564{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003565 xc_parse_push(BC_INST_JUMP_ZERO);
3566 xc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003567}
3568
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003569static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003570{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003571 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003572 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003573
Denys Vlasenko10bde142018-12-27 18:23:58 +01003574 xc_parse_push(XC_INST_STR);
3575 xc_parse_pushIndex(p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003576 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003577
Denys Vlasenko10bde142018-12-27 18:23:58 +01003578 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003579}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003580#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003581#endif
3582
Denys Vlasenko10bde142018-12-27 18:23:58 +01003583static void xc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003584{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003585 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003586 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003587#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003588 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003589#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003590 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003591#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003592 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003593#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01003594 xc_parse_push(XC_INST_NUM);
3595 xc_parse_pushIndex(idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003596}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003597
Denys Vlasenko10bde142018-12-27 18:23:58 +01003598static BC_STATUS zxc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003599{
Denys Vlasenko10bde142018-12-27 18:23:58 +01003600 G.prs.func = xc_program_func(G.prs.fidx);
3601 G.prs.lex_inbuf = text;
3602 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
3603 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003604}
Denys Vlasenko10bde142018-12-27 18:23:58 +01003605#define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003606
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003607// Called when parsing or execution detects a failure,
3608// resets execution structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003609static void xc_program_reset(void)
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003610{
3611 BcFunc *f;
3612 BcInstPtr *ip;
3613
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003614 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003615 bc_vec_pop_all(&G.prog.results);
3616
Denys Vlasenko10bde142018-12-27 18:23:58 +01003617 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003618 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003619 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003620}
3621
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003622// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003623// resets parsing structures.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003624static void xc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003625{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003626 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003627 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003628 bc_func_free(p->func);
3629 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003630
Denys Vlasenko65e10462018-12-19 15:13:14 +01003631 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003632 p->func = xc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003633 }
3634
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003635 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003636 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003637
Denys Vlasenko503faf92018-12-20 16:24:18 +01003638 IF_BC(bc_vec_pop_all(&p->exits);)
3639 IF_BC(bc_vec_pop_all(&p->conds);)
3640 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003641
Denys Vlasenko10bde142018-12-27 18:23:58 +01003642 xc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003643}
3644
Denys Vlasenko10bde142018-12-27 18:23:58 +01003645static void xc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003646{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003647 IF_BC(bc_vec_free(&G.prs.exits);)
3648 IF_BC(bc_vec_free(&G.prs.conds);)
3649 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003650 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003651}
3652
Denys Vlasenko10bde142018-12-27 18:23:58 +01003653static void xc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003654{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003655 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003656 memset(p, 0, sizeof(BcParse));
3657
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003658 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003659 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3660 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3661 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003662
Denys Vlasenkofa210792018-12-19 19:35:40 +01003663 p->fidx = fidx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003664 p->func = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003665}
3666
Denys Vlasenko10bde142018-12-27 18:23:58 +01003667static void xc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003668{
Denys Vlasenko04715442018-12-21 00:10:26 +01003669 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003670 BcFunc f;
3671 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003672 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003673 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003674 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003675}
3676
3677#if ENABLE_BC
3678
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003679// Note: takes ownership of 'name' (must be malloced)
3680static size_t bc_program_addFunc(char *name)
3681{
3682 size_t idx;
3683 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003684 int inserted;
3685
3686 entry.name = name;
3687 entry.idx = G.prog.fns.len;
3688
3689 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3690 if (!inserted) free(name);
3691
3692 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3693 idx = entry_ptr->idx;
3694
3695 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003696 // There is already a function with this name.
3697 // It'll be redefined now, clear old definition.
Denys Vlasenko10bde142018-12-27 18:23:58 +01003698 BcFunc *func = xc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003699 bc_func_free(func);
3700 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003701 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003702 xc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003703 }
3704
3705 return idx;
3706}
3707
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003708#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003709// We can calculate the conversion between tokens and exprs by subtracting the
3710// position of the first operator in the lex enum and adding the position of the
3711// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003712#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003713
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003714static BcStatus bc_parse_expr_empty_ok(uint8_t flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003715
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003716static BC_STATUS zbc_parse_expr(uint8_t flags)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003717{
3718 BcStatus s;
3719
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003720 s = bc_parse_expr_empty_ok(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003721 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3722 RETURN_STATUS(bc_error("empty expression"));
3723 RETURN_STATUS(s);
3724}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003725#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003726
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003727static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003728#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003729
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003730static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003731{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003732 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003733}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003734#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003735
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003736static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003737{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003738 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003739 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3740 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003741 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003742 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003743 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003744 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003745
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003746 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003747}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003748#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003749
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003750static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003751{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003752 BcParse *p = &G.prs;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003753 char l, r = bc_operation_PREC(type - XC_LEX_1st_op);
3754 bool left = bc_operation_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003755
3756 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003757 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003758 if (t == BC_LEX_LPAREN) break;
3759
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01003760 l = bc_operation_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003761 if (l >= r && (l != r || !left)) break;
3762
Denys Vlasenko10bde142018-12-27 18:23:58 +01003763 xc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003764 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003765 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003766 }
3767
3768 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003769}
3770
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003771static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003772{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003773 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003774 BcLexType top;
3775
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003776 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003777 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003778 top = BC_PARSE_TOP_OP(p);
3779
3780 while (top != BC_LEX_LPAREN) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003781 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003782
3783 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003784 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003785
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003786 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003787 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003788 top = BC_PARSE_TOP_OP(p);
3789 }
3790
3791 bc_vec_pop(&p->ops);
3792
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003793 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003794}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003795#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003796
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003797static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003798{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003799 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003800 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003801 size_t nparams;
3802
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003803 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003804 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3805
Denys Vlasenko10bde142018-12-27 18:23:58 +01003806 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003807 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003808
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003809 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003810 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003811 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003812 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003813 if (s) RETURN_STATUS(s);
3814 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003815 if (p->lex != BC_LEX_COMMA) {
3816 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003817 break;
3818 RETURN_STATUS(bc_error_bad_token());
3819 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003820 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003821 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003822 }
3823 }
3824
Denys Vlasenko10bde142018-12-27 18:23:58 +01003825 xc_parse_push(BC_INST_CALL);
3826 xc_parse_pushIndex(nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003827
Denys Vlasenko26819db2018-12-12 16:08:46 +01003828 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003829}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003830#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003831
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003832// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003833static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003834{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003835 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003836 BcStatus s;
3837 BcId entry, *entry_ptr;
3838 size_t idx;
3839
3840 entry.name = name;
3841
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003842 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003843 if (s) goto err;
3844
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003845 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003846 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003847 goto err;
3848 }
3849
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003850 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003851
3852 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003853 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003854 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003855 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003856 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003857 free(name);
3858
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003859 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003860 xc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003861
Denys Vlasenko10bde142018-12-27 18:23:58 +01003862 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003863 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003864 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003865 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003866}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003867#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003868
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003869static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003870{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003871 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003872 BcStatus s;
3873 char *name;
3874
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003875 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01003876 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003877 if (s) goto err;
3878
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003879 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01003880 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003881 if (s) goto err;
3882
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003883 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003884 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003885 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003886 goto err;
3887 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003888 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003889 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003890 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003891 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003892 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003893 if (s) goto err;
3894 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01003895 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003896 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003897 xc_parse_push(*type);
3898 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003899 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003900 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003901 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003902 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003903 goto err;
3904 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003906 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003907 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003908 *type = XC_INST_VAR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003909 xc_parse_push(XC_INST_VAR);
3910 xc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003911 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003912 }
3913
Denys Vlasenko26819db2018-12-12 16:08:46 +01003914 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003915 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003916 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003917 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003918}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003919#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003920
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003921static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003922{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003923 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003924 BcStatus s;
3925
Denys Vlasenko10bde142018-12-27 18:23:58 +01003926 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003927 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003928 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003929
Denys Vlasenko10bde142018-12-27 18:23:58 +01003930 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003931 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003932 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003933
Denys Vlasenko10bde142018-12-27 18:23:58 +01003934 xc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003935
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003936 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003937}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003938#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003939
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003940static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003941{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003942 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003943 BcStatus s;
3944
Denys Vlasenko10bde142018-12-27 18:23:58 +01003945 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003946 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003947 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003948
3949 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3950
Denys Vlasenko10bde142018-12-27 18:23:58 +01003951 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003952 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003953
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003954 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003955 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003956
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003957 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003958
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003959 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003960 xc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003961
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003962 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003963}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003964#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003965
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003966static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003967{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003968 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003969 BcStatus s;
3970
Denys Vlasenko10bde142018-12-27 18:23:58 +01003971 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003972 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003973
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003974 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003975 *type = XC_INST_SCALE;
Denys Vlasenko10bde142018-12-27 18:23:58 +01003976 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003977 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003978 }
3979
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003980 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003981 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3982
Denys Vlasenko10bde142018-12-27 18:23:58 +01003983 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003984 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003985
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003986 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003987 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003988 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003989 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01003990 xc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003991
Denys Vlasenko10bde142018-12-27 18:23:58 +01003992 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003993}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003994#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003995
Denys Vlasenko22314682019-01-01 21:50:14 +01003996static BC_STATUS zbc_parse_incdec(BcInst *prev, size_t *nexs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003997{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003998 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003999 BcStatus s;
4000 BcLexType type;
4001 char inst;
4002 BcInst etype = *prev;
4003
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004004 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4005 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4006 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004007 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004008 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004009 xc_parse_push(inst);
4010 s = zxc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004011 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004012 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004013
Denys Vlasenko10bde142018-12-27 18:23:58 +01004014 s = zxc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004015 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004016 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004017
4018 // Because we parse the next part of the expression
4019 // right here, we need to increment this.
Denys Vlasenko22314682019-01-01 21:50:14 +01004020 *nexs = *nexs + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06004021
4022 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004023 case XC_LEX_NAME:
4024 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
4025 break;
4026 case BC_LEX_KEY_IBASE:
4027 case BC_LEX_KEY_LAST:
4028 case BC_LEX_KEY_OBASE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004029 xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4030 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004031 break;
4032 case BC_LEX_KEY_SCALE:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004033 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004034 if (s) RETURN_STATUS(s);
4035 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004036 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004037 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01004038 xc_parse_push(XC_INST_SCALE);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004039 break;
4040 default:
4041 s = bc_error_bad_token();
4042 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004043 }
4044
Denys Vlasenko10bde142018-12-27 18:23:58 +01004045 if (!s) xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004046 }
4047
Denys Vlasenko26819db2018-12-12 16:08:46 +01004048 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004049}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004050#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004051
Denys Vlasenko22314682019-01-01 21:50:14 +01004052static int bc_parse_inst_isLeaf(BcInst p)
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004053{
4054 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4055 || p == BC_INST_INC_POST
4056 || p == BC_INST_DEC_POST
4057 ;
4058}
Denys Vlasenko22314682019-01-01 21:50:14 +01004059#define BC_PARSE_LEAF(prev, bin_last, rparen) \
4060 (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev)))
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004061
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004062static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
Denys Vlasenko22314682019-01-01 21:50:14 +01004063 bool rparen, bool bin_last, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06004064{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004065 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004066 BcStatus s;
4067 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004068
Denys Vlasenko10bde142018-12-27 18:23:58 +01004069 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004070 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004071
Denys Vlasenko22314682019-01-01 21:50:14 +01004072 type = BC_PARSE_LEAF(*prev, bin_last, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004073 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004074
4075 // We can just push onto the op stack because this is the largest
4076 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004077 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004078 bc_vec_push(&p->ops, &type);
4079 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004080 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004081
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004082 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004083}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004084#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004085
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004086static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004087{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004088 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004089 BcStatus s;
4090 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004091
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004092 for (;;) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004093 s = zxc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004094 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004095 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004096 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004097 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004098 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004099 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004100 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004101 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004102 xc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004103 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004104 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004105 }
4106
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004107 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004108}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004109#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004110
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004111static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004112{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004113 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004114 BcStatus s;
4115 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004116
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004117 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004118 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004119 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004120
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004121 t = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004122 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004123 xc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004124 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004125 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004126 s = bc_parse_expr_empty_ok(0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004127 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004128 xc_parse_push(BC_INST_RET0);
4129 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004130 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004131 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004132
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004133 if (!paren || p->lex_last != BC_LEX_RPAREN) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004134 s = zbc_POSIX_requires("parentheses around return expressions");
4135 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004136 }
4137
Denys Vlasenko10bde142018-12-27 18:23:58 +01004138 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004139 }
4140
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004141 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004142 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004143}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004144#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004145
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004146static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004147{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004148 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004149 size_t *label = bc_vec_item(&p->func->labels, idx);
4150 *label = p->func->code.len;
4151}
4152
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004153static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004154{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004155 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004156 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004157 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004158
4159 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004160 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004161 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004162 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004163
Denys Vlasenko10bde142018-12-27 18:23:58 +01004164 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004165 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004166 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004167 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004168 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004169
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004170 // Encode "if zero, jump to ..."
4171 // Pushed value (destination of the jump) is uninitialized,
4172 // will be rewritten to be address of "end of if()" or of "else".
4173 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004174 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004175
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004176 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004177 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004178
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004179 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4180 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004181 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004182
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004183 // Encode "after then_stmt, jump to end of if()"
4184 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4185 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004186 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004187
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004188 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004189 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004190
Denys Vlasenko15850832018-12-16 21:40:54 +01004191 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004192
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004193 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004194 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004195 }
4196
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004197 dbg_lex("%s:%d rewriting label to jump after 'if' body-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004198 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004199
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004200 dbg_lex_done("%s:%d done", __func__, __LINE__);
4201 RETURN_STATUS(s);
4202}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004203#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004204
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004205static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004206{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004207 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004208 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004209 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004210 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004211
Denys Vlasenko10bde142018-12-27 18:23:58 +01004212 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004213 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004214 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004215 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004216 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004217
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004218 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004219 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004220 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004221
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004222 bc_vec_push(&p->exits, &ip_idx);
4223 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004224
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004225 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004226 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004227 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004228
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004229 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004230
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004231 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004232 if (s) RETURN_STATUS(s);
4233
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004234 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004235 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004236
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004237 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004238 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004239
4240 bc_vec_pop(&p->exits);
4241 bc_vec_pop(&p->conds);
4242
4243 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004244}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004245#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004246
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004247static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004248{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004249 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004250 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004251 size_t cond_idx, exit_idx, body_idx, update_idx;
4252
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004253 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004254 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004255 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004256 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004257 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004258 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004259
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004260 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004261 s = zbc_parse_expr(0);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004262 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004263 if (s) RETURN_STATUS(s);
4264 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004265 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4266 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004267 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004268
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004269 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004270 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004271 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004272
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004273 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004274 update_idx = cond_idx + 1;
4275 body_idx = update_idx + 1;
4276 exit_idx = body_idx + 1;
4277
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004278 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004279 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004280 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004281 // Set this for the next call to xc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004282 // This is safe to set because the current token is a semicolon,
4283 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004284 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenko10bde142018-12-27 18:23:58 +01004285 xc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004286 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004287 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004288 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004289
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004290 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004291
Denys Vlasenko10bde142018-12-27 18:23:58 +01004292 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004293 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004294
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004295 bc_parse_pushJUMP_ZERO(exit_idx);
4296 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004297
Gavin Howard01055ba2018-11-03 11:00:21 -06004298 bc_vec_push(&p->conds, &update_idx);
4299 bc_vec_push(&p->func->labels, &p->func->code.len);
4300
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004301 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004302 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004303 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004304 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004305 xc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004306 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004307 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4308 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004309 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004310
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004311 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004312 bc_vec_push(&p->func->labels, &p->func->code.len);
4313
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004314 bc_vec_push(&p->exits, &exit_idx);
4315 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004316
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004317 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004318 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004319
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004320 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004321 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004322
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004323 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004324 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004325
4326 bc_vec_pop(&p->exits);
4327 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004328
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004329 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004330}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004331#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004332
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004333static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004334{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004335 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004336 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004337
Gavin Howard01055ba2018-11-03 11:00:21 -06004338 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004339 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4340 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004341 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004342 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004343 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004344 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004345 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004346
Denys Vlasenko10bde142018-12-27 18:23:58 +01004347 RETURN_STATUS(zxc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004348}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004349#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004350
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004351static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4352{
4353 BcId *autoid;
4354 BcId a;
4355 size_t i;
4356
4357 autoid = (void*)f->autos.v;
4358 for (i = 0; i < f->autos.len; i++, autoid++) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004359 if (strcmp(name, autoid->name) == 0
4360 && var == autoid->idx
4361 ) {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004362 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko22314682019-01-01 21:50:14 +01004363 }
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004364 }
4365
4366 a.idx = var;
4367 a.name = name;
4368
4369 bc_vec_push(&f->autos, &a);
4370
4371 RETURN_STATUS(BC_STATUS_SUCCESS);
4372}
4373#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4374
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004375static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004376{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004377 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004378 BcStatus s;
4379 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004380 char *name;
4381
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004382 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004383 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004384 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004385 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004386 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004387
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004388 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004389 p->fidx = bc_program_addFunc(name);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004390 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004391
Denys Vlasenko10bde142018-12-27 18:23:58 +01004392 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004393 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004394 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko22314682019-01-01 21:50:14 +01004395 RETURN_STATUS(bc_error_bad_function_definition());
Denys Vlasenko10bde142018-12-27 18:23:58 +01004396 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004397 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004398
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004399 while (p->lex != BC_LEX_RPAREN) {
4400 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004401 RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004402
4403 ++p->func->nparams;
4404
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004405 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004406 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004407 if (s) goto err;
4408
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004409 var = p->lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004410
4411 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004412 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004413 if (s) goto err;
4414
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004415 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004416 s = bc_error_bad_function_definition();
Gavin Howard01055ba2018-11-03 11:00:21 -06004417 goto err;
4418 }
4419
Denys Vlasenko10bde142018-12-27 18:23:58 +01004420 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004421 if (s) goto err;
4422 }
4423
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004424 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004425 if (comma) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004426 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004427 if (s) goto err;
4428 }
4429
Denys Vlasenko29301232018-12-11 15:29:32 +01004430 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004431 if (s) goto err;
4432 }
4433
Denys Vlasenko22314682019-01-01 21:50:14 +01004434 if (comma) RETURN_STATUS(bc_error_bad_function_definition());
Gavin Howard01055ba2018-11-03 11:00:21 -06004435
Denys Vlasenko10bde142018-12-27 18:23:58 +01004436 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004437 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004438
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004439 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004440 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004441 if (s) RETURN_STATUS(s);
4442 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004443
Denys Vlasenko202dd192018-12-16 17:30:35 +01004444 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004445 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004446 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004447 //GNU bc requires a {} block even if function body has single stmt, enforce this?
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004448 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004449 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004450
4451 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004452 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004453 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004454 if (s) RETURN_STATUS(s);
4455
Denys Vlasenko10bde142018-12-27 18:23:58 +01004456 xc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004457
4458 // Subsequent code generation is into main program
4459 p->fidx = BC_PROG_MAIN;
Denys Vlasenko10bde142018-12-27 18:23:58 +01004460 p->func = xc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004461
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004462 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004463 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004464 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004465 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004466 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004467 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004468}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004469#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004470
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004471static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004472{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004473 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004474 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004475 char *name;
4476
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004477 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004478 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004479 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004480
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004481 for (;;) {
4482 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004483
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004484 if (p->lex != XC_LEX_NAME)
Denys Vlasenko22314682019-01-01 21:50:14 +01004485 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004486
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004487 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004488 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004489 if (s) goto err;
4490
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004491 var = (p->lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004492 if (!var) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004493 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004494 if (s) goto err;
4495
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004496 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004497 s = bc_error_at("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004498 goto err;
4499 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004500 s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004501 if (s) goto err;
4502 }
4503
Denys Vlasenko29301232018-12-11 15:29:32 +01004504 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004505 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004506
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004507 if (p->lex == XC_LEX_NLINE
4508 || p->lex == BC_LEX_SCOLON
4509 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004510 ) {
4511 break;
4512 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004513 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko22314682019-01-01 21:50:14 +01004514 RETURN_STATUS(bc_error_at("bad 'auto' syntax"));
Denys Vlasenko10bde142018-12-27 18:23:58 +01004515 s = zxc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004516 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004517 }
4518
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004519 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004520 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004521 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004522 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004523 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004524 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004525}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004526#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004527
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004528#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004529static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004530{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004531 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004532 BcStatus s = BC_STATUS_SUCCESS;
4533
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004534 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004535
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004536 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004537 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004538 RETURN_STATUS(zxc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004539 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004540 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004541 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004542 RETURN_STATUS(zxc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004543 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004544
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004545 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004546 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4547 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004548 s = zxc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004549 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004550 } while (p->lex == XC_LEX_NLINE);
4551 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004552 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004553 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004554 if (s) RETURN_STATUS(s);
4555 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004556 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004557 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004558 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004559 if (s) RETURN_STATUS(s);
4560 }
Denys Vlasenko10bde142018-12-27 18:23:58 +01004561 s = zxc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004562 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4563 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004564 }
4565
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004566 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4567 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004568 case XC_LEX_OP_MINUS:
4569 case BC_LEX_OP_INC:
4570 case BC_LEX_OP_DEC:
4571 case BC_LEX_OP_BOOL_NOT:
4572 case BC_LEX_LPAREN:
4573 case XC_LEX_NAME:
4574 case XC_LEX_NUMBER:
4575 case BC_LEX_KEY_IBASE:
4576 case BC_LEX_KEY_LAST:
4577 case BC_LEX_KEY_LENGTH:
4578 case BC_LEX_KEY_OBASE:
4579 case BC_LEX_KEY_READ:
4580 case BC_LEX_KEY_SCALE:
4581 case BC_LEX_KEY_SQRT:
4582 s = zbc_parse_expr(BC_PARSE_PRINT);
4583 break;
4584 case XC_LEX_STR:
4585 s = zbc_parse_pushSTR();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004586 xc_parse_push(XC_INST_PRINT_STR);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004587 break;
4588 case BC_LEX_KEY_BREAK:
4589 case BC_LEX_KEY_CONTINUE:
4590 s = zbc_parse_break_or_continue(p->lex);
4591 break;
4592 case BC_LEX_KEY_FOR:
4593 s = zbc_parse_for();
4594 break;
4595 case BC_LEX_KEY_HALT:
Denys Vlasenko10bde142018-12-27 18:23:58 +01004596 xc_parse_push(BC_INST_HALT);
4597 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004598 break;
4599 case BC_LEX_KEY_IF:
4600 s = zbc_parse_if();
4601 break;
4602 case BC_LEX_KEY_LIMITS:
4603 // "limits" is a compile-time command,
4604 // the output is produced at _parse time_.
4605 printf(
4606 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4607 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4608 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4609 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4610 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4611 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4612 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4613 "Number of vars = "BC_MAX_VARS_STR "\n"
4614 );
Denys Vlasenko10bde142018-12-27 18:23:58 +01004615 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004616 break;
4617 case BC_LEX_KEY_PRINT:
4618 s = zbc_parse_print();
4619 break;
4620 case BC_LEX_KEY_QUIT:
4621 // "quit" is a compile-time command. For example,
4622 // "if (0 == 1) quit" terminates when parsing the statement,
4623 // not when it is executed
4624 QUIT_OR_RETURN_TO_MAIN;
4625 case BC_LEX_KEY_RETURN:
4626 if (!p->in_funcdef)
4627 RETURN_STATUS(bc_error("'return' not in a function"));
4628 s = zbc_parse_return();
4629 break;
4630 case BC_LEX_KEY_WHILE:
4631 s = zbc_parse_while();
4632 break;
4633 default:
4634 s = bc_error_bad_token();
4635 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004636 }
4637
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004638 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004639 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004640}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004641#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004642
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004643static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004644{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004645 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004646 BcStatus s;
4647
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004648 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004649 if (p->lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004650 s = bc_error("end of file");
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004651 else if (p->lex == BC_LEX_KEY_DEFINE) {
4652 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004653 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004654 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004655 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004656 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004657 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004658
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004659 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004660 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004661}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004662#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004663
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004664// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004665static BcStatus bc_parse_expr_empty_ok(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004666{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004667 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004668 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004669 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004670 unsigned nparens, nrelops;
Denys Vlasenko22314682019-01-01 21:50:14 +01004671 bool paren_first, rprn, assign, bin_last, incdec;
Gavin Howard01055ba2018-11-03 11:00:21 -06004672
Denys Vlasenko17df8822018-12-14 23:00:24 +01004673 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004674 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004675 nparens = nrelops = 0;
Denys Vlasenko22314682019-01-01 21:50:14 +01004676 rprn = assign = incdec = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004677 bin_last = true;
4678
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004679 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004680 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004681 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004682 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004683
4684 if (!lex_allowed_in_bc_expr(t))
4685 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004686
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004687 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004688 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004689 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004690 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004691 case BC_LEX_OP_INC:
4692 case BC_LEX_OP_DEC:
4693 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004694 if (incdec) return bc_error_bad_assignment();
4695 s = zbc_parse_incdec(&prev, &nexprs, flags);
4696 incdec = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004697 rprn = bin_last = false;
4698 //get_token = false; - already is
4699 break;
4700 case XC_LEX_OP_MINUS:
4701 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004702 s = zbc_parse_minus(&prev, ops_bgn, rprn, bin_last, &nexprs);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004703 rprn = false;
4704 //get_token = false; - already is
4705 bin_last = (prev == XC_INST_MINUS);
Denys Vlasenko22314682019-01-01 21:50:14 +01004706 if (bin_last) incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004707 break;
4708 case BC_LEX_OP_ASSIGN_POWER:
4709 case BC_LEX_OP_ASSIGN_MULTIPLY:
4710 case BC_LEX_OP_ASSIGN_DIVIDE:
4711 case BC_LEX_OP_ASSIGN_MODULUS:
4712 case BC_LEX_OP_ASSIGN_PLUS:
4713 case BC_LEX_OP_ASSIGN_MINUS:
4714 case BC_LEX_OP_ASSIGN:
4715 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4716 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4717 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4718 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4719 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01004720 return bc_error_bad_assignment();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004721 }
4722 // Fallthrough.
4723 case XC_LEX_OP_POWER:
4724 case XC_LEX_OP_MULTIPLY:
4725 case XC_LEX_OP_DIVIDE:
4726 case XC_LEX_OP_MODULUS:
4727 case XC_LEX_OP_PLUS:
4728 case XC_LEX_OP_REL_EQ:
4729 case XC_LEX_OP_REL_LE:
4730 case XC_LEX_OP_REL_GE:
4731 case XC_LEX_OP_REL_NE:
4732 case XC_LEX_OP_REL_LT:
4733 case XC_LEX_OP_REL_GT:
4734 case BC_LEX_OP_BOOL_NOT:
4735 case BC_LEX_OP_BOOL_OR:
4736 case BC_LEX_OP_BOOL_AND:
4737 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004738 if (t == BC_LEX_OP_BOOL_NOT) {
4739 if (!bin_last && p->lex_last != BC_LEX_OP_BOOL_NOT)
4740 return bc_error_bad_expression();
4741 } else if (prev == XC_INST_BOOL_NOT) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004742 return bc_error_bad_expression();
4743 }
Denys Vlasenko22314682019-01-01 21:50:14 +01004744
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004745 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4746 prev = BC_TOKEN_2_INST(t);
4747 bc_parse_operator(t, ops_bgn, &nexprs);
Denys Vlasenko22314682019-01-01 21:50:14 +01004748 rprn = incdec = false;
4749 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004750 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4751 break;
4752 case BC_LEX_LPAREN:
4753 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004754 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004755 return bc_error_bad_expression();
4756 bc_vec_push(&p->ops, &t);
4757 nparens++;
4758 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004759 rprn = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004760 break;
4761 case BC_LEX_RPAREN:
4762 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004763 if (p->lex_last == BC_LEX_LPAREN) {
4764 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
4765 return BC_STATUS_PARSE_EMPTY_EXP;
4766 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004767 if (bin_last || prev == XC_INST_BOOL_NOT)
4768 return bc_error_bad_expression();
4769 if (nparens == 0) {
4770 goto exit_loop;
4771 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004772 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4773 nparens--;
4774 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004775 rprn = true;
4776 bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004777 break;
4778 case XC_LEX_NAME:
4779 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004780 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004781 return bc_error_bad_expression();
4782 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko22314682019-01-01 21:50:14 +01004783 rprn = (prev == BC_INST_CALL);
4784 bin_last = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004785 //get_token = false; - already is
4786 nexprs++;
4787 break;
4788 case XC_LEX_NUMBER:
4789 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004790 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004791 return bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01004792 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004793 prev = XC_INST_NUM;
4794 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004795 rprn = bin_last = false;
4796 nexprs++;
4797 break;
4798 case BC_LEX_KEY_IBASE:
4799 case BC_LEX_KEY_LAST:
4800 case BC_LEX_KEY_OBASE:
4801 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004802 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004803 return bc_error_bad_expression();
4804 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004805 xc_parse_push((char) prev);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004806 get_token = true;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004807 rprn = bin_last = false;
4808 nexprs++;
4809 break;
4810 case BC_LEX_KEY_LENGTH:
4811 case BC_LEX_KEY_SQRT:
4812 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004813 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004814 return bc_error_bad_expression();
4815 s = zbc_parse_builtin(t, flags, &prev);
4816 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004817 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004818 nexprs++;
4819 break;
4820 case BC_LEX_KEY_READ:
4821 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004822 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004823 return bc_error_bad_expression();
4824 s = zbc_parse_read();
4825 prev = XC_INST_READ;
4826 get_token = true;
Denys Vlasenko22314682019-01-01 21:50:14 +01004827 rprn = bin_last = incdec = false;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004828 nexprs++;
4829 break;
4830 case BC_LEX_KEY_SCALE:
4831 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
Denys Vlasenko22314682019-01-01 21:50:14 +01004832 if (BC_PARSE_LEAF(prev, bin_last, rprn))
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004833 return bc_error_bad_expression();
4834 s = zbc_parse_scale(&prev, flags);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004835 //get_token = false; - already is
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004836 rprn = bin_last = false;
4837 nexprs++;
4838 break;
4839 default:
4840 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004841 }
4842
Denys Vlasenkobb116032018-12-25 01:16:37 +01004843 if (s || G_interrupt) // error, or ^C: stop parsing
4844 return BC_STATUS_FAILURE;
4845 if (get_token) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004846 s = zxc_lex_next();
Denys Vlasenkobb116032018-12-25 01:16:37 +01004847 if (s) return s;
4848 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004849 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004850 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004851
4852 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004853 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004854 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004855
4856 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004857 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004858
Denys Vlasenko10bde142018-12-27 18:23:58 +01004859 xc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004860
Denys Vlasenko69560f42018-12-24 14:14:23 +01004861 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004862 bc_vec_pop(&p->ops);
4863 }
4864
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004865 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004866 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004867
Gavin Howard01055ba2018-11-03 11:00:21 -06004868 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004869 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004870 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4871 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004872 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004873 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004874 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4875 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004876 }
4877
4878 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004879 if (paren_first || !assign)
Denys Vlasenko10bde142018-12-27 18:23:58 +01004880 xc_parse_push(XC_INST_PRINT);
4881 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004882 }
4883
Denys Vlasenko17df8822018-12-14 23:00:24 +01004884 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004885 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004886}
4887
Gavin Howard01055ba2018-11-03 11:00:21 -06004888#endif // ENABLE_BC
4889
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004890#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004891
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004892static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004893{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004894 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004895 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004896
Denys Vlasenko10bde142018-12-27 18:23:58 +01004897 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004898 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004899 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004900
Denys Vlasenko10bde142018-12-27 18:23:58 +01004901 xc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004902
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004903 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004904}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004905#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004906
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004907static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004908{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004909 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004910 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004911 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004912
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004913 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004914
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004915 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004916 xc_parse_push(XC_INST_STR);
4917 xc_parse_pushIndex(len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004918 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004919
4920 // Explanation needed here
Denys Vlasenko10bde142018-12-27 18:23:58 +01004921 xc_program_add_fn();
4922 p->func = xc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004923
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004924 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004925}
4926
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004927static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004928{
4929 BcStatus s;
4930
Denys Vlasenko10bde142018-12-27 18:23:58 +01004931 xc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004932 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004933 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004934 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004935 }
4936
4937 if (store) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004938 xc_parse_push(DC_INST_SWAP);
4939 xc_parse_push(XC_INST_ASSIGN);
4940 xc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004941 }
4942
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004943 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004944}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004945#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004946
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004947static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004948{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004949 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004950 BcStatus s;
4951
Denys Vlasenko10bde142018-12-27 18:23:58 +01004952 xc_parse_push(inst);
4953 xc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004954
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004955 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004956 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004957
Denys Vlasenko10bde142018-12-27 18:23:58 +01004958 s = zxc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004959 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004960
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004961 // Note that 'else' part can not be on the next line:
4962 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4963 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004964 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004965 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004966 if (s) RETURN_STATUS(s);
Denys Vlasenko10bde142018-12-27 18:23:58 +01004967 s = zxc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004968 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01004969 xc_parse_push('\0');
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004970 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004971
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004972 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004973}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004974#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004975
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004976static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004977{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004978 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004979 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004980 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004981
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004982 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004983 s = BC_STATUS_SUCCESS;
4984 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004985 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004986 case XC_LEX_OP_REL_EQ:
4987 case XC_LEX_OP_REL_LE:
4988 case XC_LEX_OP_REL_GE:
4989 case XC_LEX_OP_REL_NE:
4990 case XC_LEX_OP_REL_LT:
4991 case XC_LEX_OP_REL_GT:
4992 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
4993 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
4994 get_token = false;
4995 break;
4996 case DC_LEX_SCOLON:
4997 case DC_LEX_COLON:
4998 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
4999 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
5000 break;
5001 case XC_LEX_STR:
5002 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
5003 dc_parse_string();
5004 break;
5005 case XC_LEX_NEG:
5006 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005007 s = zxc_lex_next();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005008 if (s) RETURN_STATUS(s);
5009 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005010 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005011 xc_parse_pushNUM();
5012 xc_parse_push(XC_INST_NEG);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005013 break;
5014 case XC_LEX_NUMBER:
5015 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005016 xc_parse_pushNUM();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005017 break;
5018 case DC_LEX_READ:
5019 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005020 xc_parse_push(XC_INST_READ);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005021 break;
5022 case DC_LEX_OP_ASSIGN:
5023 case DC_LEX_STORE_PUSH:
5024 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
5025 assign = (t == DC_LEX_OP_ASSIGN);
5026 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
5027 s = zdc_parse_mem(inst, true, assign);
5028 break;
5029 case DC_LEX_LOAD:
5030 case DC_LEX_LOAD_POP:
5031 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
5032 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
5033 s = zdc_parse_mem(inst, true, false);
5034 break;
5035 case DC_LEX_STORE_IBASE:
5036 case DC_LEX_STORE_SCALE:
5037 case DC_LEX_STORE_OBASE:
5038 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
5039 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
5040 s = zdc_parse_mem(inst, false, true);
5041 break;
5042 default:
5043 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5044 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005045 }
5046
Denys Vlasenko10bde142018-12-27 18:23:58 +01005047 if (!s && get_token) s = zxc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06005048
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005049 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005050 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005051}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005052#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005053
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005054static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005055{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005056 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005057 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005058
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005059 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005060 if (i >= 0) {
5061 BcInst inst = dc_LEX_to_INST[i];
5062 if (inst != DC_INST_INVALID) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005063 xc_parse_push(inst);
5064 RETURN_STATUS(zxc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005065 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005066 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005067 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005068}
5069#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5070
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005071static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005072{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005073 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005074 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5075 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005076 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005077 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005078 }
5079
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005080 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005081 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005082}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005083#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005084
Gavin Howard01055ba2018-11-03 11:00:21 -06005085#endif // ENABLE_DC
5086
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005087//
5088// Execution engine
5089//
5090
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01005091#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
5092#define BC_PROG_NUM(r, n) \
5093 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
5094
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005095#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5096#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5097
Denys Vlasenko10bde142018-12-27 18:23:58 +01005098static BcVec* xc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005099{
Gavin Howard01055ba2018-11-03 11:00:21 -06005100 BcId e, *ptr;
5101 BcVec *v, *map;
5102 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005103 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005104
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005105 v = var ? &G.prog.vars : &G.prog.arrs;
5106 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005107
5108 e.name = id;
5109 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005110 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005111
5112 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005113 BcVec v2;
5114 bc_array_init(&v2, var);
5115 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005116 }
5117
5118 ptr = bc_vec_item(map, i);
5119 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005120 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005121}
5122
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005123// 'num' need not be initialized on entry
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005124static BC_STATUS zxc_program_num(BcResult *r, BcNum **num)
Gavin Howard01055ba2018-11-03 11:00:21 -06005125{
Gavin Howard01055ba2018-11-03 11:00:21 -06005126 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005127 case XC_RESULT_STR:
5128 case XC_RESULT_TEMP:
5129 case XC_RESULT_IBASE:
5130 case XC_RESULT_SCALE:
5131 case XC_RESULT_OBASE:
5132 *num = &r->d.n;
5133 break;
5134 case XC_RESULT_CONSTANT: {
5135 BcStatus s;
5136 char *str;
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005137 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005138
Denys Vlasenko10bde142018-12-27 18:23:58 +01005139 str = *xc_program_const(r->d.id.idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005140 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005141
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005142 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005143
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005144 s = zxc_num_parse(&r->d.n, str, G.prog.ib_t);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005145 if (s) {
5146 bc_num_free(&r->d.n);
5147 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005148 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005149 *num = &r->d.n;
5150 r->t = XC_RESULT_TEMP;
5151 break;
5152 }
5153 case XC_RESULT_VAR:
5154 case XC_RESULT_ARRAY:
5155 case XC_RESULT_ARRAY_ELEM: {
5156 BcVec *v;
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005157 void *p;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005158 v = xc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005159// dc variables are all stacks, so here we have this:
5160 p = bc_vec_top(v);
5161// TODO: eliminate these stacks for bc-only config?
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005162 if (r->t == XC_RESULT_ARRAY_ELEM) {
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005163 v = p;
5164 if (v->len <= r->d.id.idx)
5165 bc_array_expand(v, r->d.id.idx + 1);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005166 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko8ab209f2018-12-29 16:23:34 +01005167 } else {
5168 *num = p;
5169 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005170 break;
5171 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005172#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005173 case BC_RESULT_LAST:
5174 *num = &G.prog.last;
5175 break;
5176 case BC_RESULT_ONE:
5177 *num = &G.prog.one;
5178 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005179#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005180#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005181 default:
5182 // Testing the theory that dc does not reach LAST/ONE
5183 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005184#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005185 }
5186
Denys Vlasenko29301232018-12-11 15:29:32 +01005187 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005188}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005189#define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005190
Denys Vlasenko10bde142018-12-27 18:23:58 +01005191static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005192 BcResult **r, BcNum **rn, bool assign)
5193{
5194 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005195 BcResultType lt, rt;
5196
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005197 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005198 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005199
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005200 *r = bc_vec_item_rev(&G.prog.results, 0);
5201 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005202
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005203 s = zxc_program_num(*l, ln);
5204 if (s) RETURN_STATUS(s);
5205 s = zxc_program_num(*r, rn);
5206 if (s) RETURN_STATUS(s);
5207
Gavin Howard01055ba2018-11-03 11:00:21 -06005208 lt = (*l)->t;
5209 rt = (*r)->t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005210
5211 // We run this again under these conditions in case any vector has been
5212 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005213 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005214 s = zxc_program_num(*l, ln);
Denys Vlasenko29301232018-12-11 15:29:32 +01005215 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005216 }
5217
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005218 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005219 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005220 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005221 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005222
Denys Vlasenko29301232018-12-11 15:29:32 +01005223 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005224}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005225#define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005226
Denys Vlasenko10bde142018-12-27 18:23:58 +01005227static void xc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005228{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005229 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005230 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005231 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005232}
5233
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005234// Note: *r and *n need not be initialized by caller
Denys Vlasenko10bde142018-12-27 18:23:58 +01005235static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005236{
5237 BcStatus s;
5238
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005239 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005240 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005241 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005242
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005243 s = zxc_program_num(*r, n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005244 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005245
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005246 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005247 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005248
Denys Vlasenko29301232018-12-11 15:29:32 +01005249 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005250}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005251#define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005252
Denys Vlasenko10bde142018-12-27 18:23:58 +01005253static void xc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005254{
5255 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005256 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005257}
5258
Denys Vlasenko10bde142018-12-27 18:23:58 +01005259static BC_STATUS zxc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005260{
5261 BcStatus s;
5262 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005263 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005264
Denys Vlasenko10bde142018-12-27 18:23:58 +01005265 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005266 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005267 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005268
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005269 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005270 IF_ERROR_RETURN_POSSIBLE(s =) zxc_program_ops[inst - XC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005271 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005272 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005273
Denys Vlasenko259137d2018-12-11 19:42:05 +01005274 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005275 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005276 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005277 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005278}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005279#define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005280
Denys Vlasenko10bde142018-12-27 18:23:58 +01005281static BC_STATUS zxc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005282{
5283 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005284 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005285 BcVec buf;
5286 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005287 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005288
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005289 bc_char_vec_init(&buf);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005290 xc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005291
Denys Vlasenko10bde142018-12-27 18:23:58 +01005292 f = xc_program_func(BC_PROG_READ);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005293 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005294
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005295 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01005296 xc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005297 //G.err_line = G.prs.lex_line = 1; - not needed, error line info is not printed for read()
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005298
Denys Vlasenko10bde142018-12-27 18:23:58 +01005299 s = zxc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005300 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005301 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005302 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005303 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005304 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005305 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005306 if (s) goto exec_err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005307 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko22314682019-01-01 21:50:14 +01005308 s = bc_error_at("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005309 goto exec_err;
5310 }
Denys Vlasenko2747f612018-12-31 18:48:10 +01005311 xc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06005312
5313 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005314 ip.inst_idx = 0;
5315 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005316 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005317
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005318 exec_err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01005319 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005320 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005321 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005322 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005323}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005324#define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005325
Denys Vlasenko10bde142018-12-27 18:23:58 +01005326static size_t xc_program_index(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005327{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005328 unsigned char *bytes = (void*)(code + *bgn);
5329 unsigned amt;
5330 unsigned i;
5331 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005332
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005333 amt = *bytes++;
Denys Vlasenkoab9a9862018-12-26 20:30:47 +01005334 if (amt < SMALL_INDEX_LIMIT) {
5335 *bgn += 1;
5336 return amt;
5337 }
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005338 amt -= (SMALL_INDEX_LIMIT - 1); // amt is 1 or more here
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005339 *bgn += amt + 1;
5340
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005341 res = 0;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005342 i = 0;
5343 do {
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005344 res |= (size_t)(*bytes++) << i;
Denys Vlasenkoe16a5222018-12-29 02:24:19 +01005345 i += 8;
5346 } while (--amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005347
5348 return res;
5349}
5350
Denys Vlasenko10bde142018-12-27 18:23:58 +01005351static char *xc_program_name(char *code, size_t *bgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06005352{
Denys Vlasenko694d2982018-12-18 19:17:11 +01005353 code += *bgn;
Denys Vlasenkof706a182018-12-26 20:02:09 +01005354 *bgn += strlen(code) + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005355
Denys Vlasenkof706a182018-12-26 20:02:09 +01005356 return xstrdup(code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005357}
5358
Denys Vlasenko10bde142018-12-27 18:23:58 +01005359static void xc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005360{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005361#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005362 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005363 // Example: echo '[]ap' | dc
5364 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005365 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005366 return;
5367 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005368#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005369 while (*str) {
5370 int c = *str++;
5371 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005372 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005373 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005374 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005375 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005376 case 'a':
5377 bb_putchar('\a');
5378 break;
5379 case 'b':
5380 bb_putchar('\b');
5381 break;
5382 case '\\':
5383 case 'e':
5384 bb_putchar('\\');
5385 break;
5386 case 'f':
5387 bb_putchar('\f');
5388 break;
5389 case 'n':
5390 bb_putchar('\n');
5391 G.prog.nchars = SIZE_MAX;
5392 break;
5393 case 'r':
5394 bb_putchar('\r');
5395 break;
5396 case 'q':
5397 bb_putchar('"');
5398 break;
5399 case 't':
5400 bb_putchar('\t');
5401 break;
5402 default:
5403 // Just print the backslash and following character.
5404 bb_putchar('\\');
5405 ++G.prog.nchars;
5406 bb_putchar(c);
5407 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005408 }
5409 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005410 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005411 }
5412}
5413
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005414static void bc_num_printNewline(void)
5415{
5416 if (G.prog.nchars == G.prog.len - 1) {
5417 bb_putchar('\\');
5418 bb_putchar('\n');
5419 G.prog.nchars = 0;
5420 }
5421}
5422
5423#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005424static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005425{
5426 (void) radix;
5427 bb_putchar((char) num);
5428 G.prog.nchars += width;
5429}
5430#endif
5431
5432static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5433{
5434 size_t exp, pow;
5435
5436 bc_num_printNewline();
5437 bb_putchar(radix ? '.' : ' ');
5438 ++G.prog.nchars;
5439
5440 bc_num_printNewline();
5441 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5442 continue;
5443
5444 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5445 size_t dig;
5446 bc_num_printNewline();
5447 dig = num / pow;
5448 num -= dig * pow;
5449 bb_putchar(((char) dig) + '0');
5450 }
5451}
5452
5453static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5454{
5455 if (radix) {
5456 bc_num_printNewline();
5457 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005458 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005459 }
5460
5461 bc_num_printNewline();
5462 bb_putchar(bb_hexdigits_upcase[num]);
5463 G.prog.nchars += width;
5464}
5465
5466static void bc_num_printDecimal(BcNum *n)
5467{
5468 size_t i, rdx = n->rdx - 1;
5469
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005470 if (n->neg) {
5471 bb_putchar('-');
5472 G.prog.nchars++;
5473 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005474
5475 for (i = n->len - 1; i < n->len; --i)
5476 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5477}
5478
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005479typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5480
Denys Vlasenko10bde142018-12-27 18:23:58 +01005481static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005482{
5483 BcStatus s;
5484 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005485 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005486 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005487 BcNum intp, fracp, digit, frac_len;
5488 unsigned long dig, *ptr;
5489 size_t i;
5490 bool radix;
5491
5492 if (n->len == 0) {
5493 print(0, width, false);
5494 RETURN_STATUS(BC_STATUS_SUCCESS);
5495 }
5496
5497 bc_vec_init(&stack, sizeof(long), NULL);
5498 bc_num_init(&intp, n->len);
5499 bc_num_init(&fracp, n->rdx);
5500 bc_num_init(&digit, width);
5501 bc_num_init(&frac_len, BC_NUM_INT(n));
5502 bc_num_copy(&intp, n);
5503 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005504 base.cap = ARRAY_SIZE(base_digs);
5505 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005506 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005507
5508 bc_num_truncate(&intp, intp.rdx);
5509 s = zbc_num_sub(n, &intp, &fracp, 0);
5510 if (s) goto err;
5511
5512 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005513 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005514 if (s) goto err;
5515 s = zbc_num_ulong(&digit, &dig);
5516 if (s) goto err;
5517 bc_vec_push(&stack, &dig);
5518 }
5519
5520 for (i = 0; i < stack.len; ++i) {
5521 ptr = bc_vec_item_rev(&stack, i);
5522 print(*ptr, width, false);
5523 }
5524
5525 if (!n->rdx) goto err;
5526
5527 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005528 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005529 if (s) goto err;
5530 s = zbc_num_ulong(&fracp, &dig);
5531 if (s) goto err;
5532 bc_num_ulong2num(&intp, dig);
5533 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5534 if (s) goto err;
5535 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005536 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005537 if (s) goto err;
5538 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005539 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005540 bc_num_free(&frac_len);
5541 bc_num_free(&digit);
5542 bc_num_free(&fracp);
5543 bc_num_free(&intp);
5544 bc_vec_free(&stack);
5545 RETURN_STATUS(s);
5546}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005547#define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005548
Denys Vlasenko10bde142018-12-27 18:23:58 +01005549static BC_STATUS zxc_num_printBase(BcNum *n)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005550{
5551 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005552 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005553 BcNumDigitOp print;
5554 bool neg = n->neg;
5555
5556 if (neg) {
5557 bb_putchar('-');
5558 G.prog.nchars++;
5559 }
5560
5561 n->neg = false;
5562
Denys Vlasenko680ccd32018-12-31 19:42:13 +01005563 if (G.prog.ob_t <= 16) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005564 width = 1;
5565 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005566 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005567 unsigned i = G.prog.ob_t - 1;
5568 width = 0;
5569 for (;;) {
5570 width++;
5571 i /= 10;
5572 if (i == 0)
5573 break;
5574 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005575 print = bc_num_printDigits;
5576 }
5577
Denys Vlasenko10bde142018-12-27 18:23:58 +01005578 s = zxc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005579 n->neg = neg;
5580
5581 RETURN_STATUS(s);
5582}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005583#define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005584
Denys Vlasenko10bde142018-12-27 18:23:58 +01005585static BC_STATUS zxc_num_print(BcNum *n, bool newline)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005586{
5587 BcStatus s = BC_STATUS_SUCCESS;
5588
5589 bc_num_printNewline();
5590
5591 if (n->len == 0) {
5592 bb_putchar('0');
5593 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005594 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005595 bc_num_printDecimal(n);
5596 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01005597 s = zxc_num_printBase(n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005598
5599 if (newline) {
5600 bb_putchar('\n');
5601 G.prog.nchars = 0;
5602 }
5603
5604 RETURN_STATUS(s);
5605}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005606#define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005607
Denys Vlasenko10bde142018-12-27 18:23:58 +01005608static BC_STATUS zxc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005609{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005610 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005611 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005612 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005613 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005614
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005615 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005616 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005617
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005618 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005619 s = zxc_program_num(r, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005620 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005621
5622 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005623 s = zxc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005624#if ENABLE_BC
5625 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5626#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005627 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005628 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005629
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005630 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005631 str = *xc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005632
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005633 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005634 for (;;) {
5635 char c = *str++;
5636 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005637 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005638 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005639 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005640 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005641 } else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005642 xc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005643 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005644 }
5645 }
5646
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005647 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005648
Denys Vlasenko29301232018-12-11 15:29:32 +01005649 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005650}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005651#define zxc_program_print(...) (zxc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005652
Denys Vlasenko10bde142018-12-27 18:23:58 +01005653static BC_STATUS zxc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005654{
5655 BcStatus s;
5656 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005657 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005658
Denys Vlasenko10bde142018-12-27 18:23:58 +01005659 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005660 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005661
5662 bc_num_init(&res.d.n, num->len);
5663 bc_num_copy(&res.d.n, num);
5664 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5665
Denys Vlasenko10bde142018-12-27 18:23:58 +01005666 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005667
Denys Vlasenko29301232018-12-11 15:29:32 +01005668 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005669}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005670#define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005671
Denys Vlasenko10bde142018-12-27 18:23:58 +01005672static BC_STATUS zxc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005673{
5674 BcStatus s;
5675 BcResult *opd1, *opd2, res;
5676 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005677 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005678
Denys Vlasenko10bde142018-12-27 18:23:58 +01005679 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005680 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005681
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005682 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005683
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005684 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005685 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005686 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005687 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005688 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005689 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005690 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005691 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005692 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005693 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005694 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005695 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005696 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005697 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005698 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005699 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005700 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005701 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005702 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005703 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005704 cond = (cond > 0);
5705 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005706 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005707 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005708 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005709 }
5710 }
5711
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005712 if (cond) bc_num_one(&res.d.n);
5713 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005714
Denys Vlasenko10bde142018-12-27 18:23:58 +01005715 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005716
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005717 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005718}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005719#define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005720
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005721#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005722static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005723{
5724 BcNum n2;
5725 BcResult res;
5726
5727 memset(&n2, 0, sizeof(BcNum));
5728 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005729 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005730
5731 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005732 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005733 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005734 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005735 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005736 }
5737
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005738 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005739 bc_vec_push(v, &n2);
5740
Denys Vlasenko29301232018-12-11 15:29:32 +01005741 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005742}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005743#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005744#endif // ENABLE_DC
5745
Denys Vlasenko10bde142018-12-27 18:23:58 +01005746static BC_STATUS zxc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005747{
5748 BcStatus s;
5749 BcResult *ptr, r;
5750 BcVec *v;
5751 BcNum *n;
5752
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005753 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005754 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005755
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005756 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005757 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005758 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005759 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005760
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005761#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005762 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005763 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005764 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005765 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005766#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005767
Denys Vlasenko374d2c42018-12-29 14:52:30 +01005768 s = zxc_program_num(ptr, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005769 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005770
5771 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenko10bde142018-12-27 18:23:58 +01005772 v = xc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005773
5774 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005775 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005776 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005777 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005778 bc_array_init(&r.d.v, true);
5779 bc_array_copy(&r.d.v, (BcVec *) n);
5780 }
5781
5782 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005783 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005784
Denys Vlasenko29301232018-12-11 15:29:32 +01005785 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005786}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005787#define zxc_program_copyToVar(...) (zxc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005788
Denys Vlasenko10bde142018-12-27 18:23:58 +01005789static BC_STATUS zxc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005790{
5791 BcStatus s;
5792 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005793 BcNum *l, *r;
5794 bool assign = (inst == XC_INST_ASSIGN);
5795 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005796
Denys Vlasenko10bde142018-12-27 18:23:58 +01005797 s = zxc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005798 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005799
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005800 ib = left->t == XC_RESULT_IBASE;
5801 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005802
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005803#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005804 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005805 BcVec *v;
5806
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005807 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005808 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko10bde142018-12-27 18:23:58 +01005809 v = xc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005810
Denys Vlasenkofa210792018-12-19 19:35:40 +01005811 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005812 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005813#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005814
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005815 if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP)
Denys Vlasenko22314682019-01-01 21:50:14 +01005816 RETURN_STATUS(bc_error_bad_assignment());
Gavin Howard01055ba2018-11-03 11:00:21 -06005817
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005818#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005819 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005820 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005821
5822 if (assign)
5823 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005824 else {
5825 s = BC_STATUS_SUCCESS;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005826 IF_ERROR_RETURN_POSSIBLE(s =) zxc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005827 }
5828 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005829#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005830 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005831#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005832
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005833 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005834 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005835 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5836 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5837 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005838 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005839 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005840 size_t max;
5841 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005842
Denys Vlasenko29301232018-12-11 15:29:32 +01005843 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005844 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005845 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005846 if (sc) {
5847 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005848 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005849 } else {
5850 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005851 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005852 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005853 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005854 }
5855
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005856 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005857 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005858
5859 *ptr = (size_t) val;
5860 s = BC_STATUS_SUCCESS;
5861 }
5862
5863 bc_num_init(&res.d.n, l->len);
5864 bc_num_copy(&res.d.n, l);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005865 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005866
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005867 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005868}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005869#define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005870
Denys Vlasenko416ce762018-12-02 20:57:17 +01005871#if !ENABLE_DC
Denys Vlasenko10bde142018-12-27 18:23:58 +01005872#define xc_program_pushVar(code, bgn, pop, copy) \
5873 xc_program_pushVar(code, bgn)
Denys Vlasenko416ce762018-12-02 20:57:17 +01005874// for bc, 'pop' and 'copy' are always false
5875#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01005876static BC_STATUS xc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005877 bool pop, bool copy)
5878{
Gavin Howard01055ba2018-11-03 11:00:21 -06005879 BcResult r;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005880 char *name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005881
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005882 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005883 r.d.id.name = name;
5884
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005885#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005886 if (pop || copy) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01005887 BcVec *v = xc_program_search(name, true);
Denys Vlasenko416ce762018-12-02 20:57:17 +01005888 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005889
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005890 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005891 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005892 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005893 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005894
5895 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005896 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005897 bc_num_init_DEF_SIZE(&r.d.n);
5898 bc_num_copy(&r.d.n, num);
5899 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005900 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005901 r.d.id.idx = num->rdx;
5902 }
5903
5904 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005905 }
5906#endif // ENABLE_DC
5907
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005908 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005909
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005910 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005911}
Denys Vlasenko10bde142018-12-27 18:23:58 +01005912#define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005913
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005914static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005915{
5916 BcStatus s = BC_STATUS_SUCCESS;
5917 BcResult r;
5918 BcNum *num;
5919
Denys Vlasenko10bde142018-12-27 18:23:58 +01005920 r.d.id.name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005921
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005922 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005923 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005924 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005925 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005926 BcResult *operand;
5927 unsigned long temp;
5928
Denys Vlasenko10bde142018-12-27 18:23:58 +01005929 s = zxc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005930 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005931 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005932 if (s) goto err;
5933
5934 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005935 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005936 goto err;
5937 }
5938
5939 r.d.id.idx = (size_t) temp;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005940 xc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005941 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005942 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005944 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005945}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005946#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005947
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005948#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005949static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005950{
5951 BcStatus s;
5952 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005953 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005954 char inst2 = inst;
5955
Denys Vlasenko10bde142018-12-27 18:23:58 +01005956 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01005957 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005958
5959 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005960 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005961 bc_num_init(&copy.d.n, num->len);
5962 bc_num_copy(&copy.d.n, num);
5963 }
5964
5965 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005966 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5967 ? BC_INST_ASSIGN_PLUS
5968 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005969
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005970 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko10bde142018-12-27 18:23:58 +01005971 s = zxc_program_assign(inst);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005972 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005973
5974 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005975 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005976 }
5977
Denys Vlasenko29301232018-12-11 15:29:32 +01005978 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005979}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005980#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005981
Denys Vlasenko29301232018-12-11 15:29:32 +01005982static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005983{
Gavin Howard01055ba2018-11-03 11:00:21 -06005984 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005985 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005986 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005987 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005988 BcResult *arg;
5989
Denys Vlasenko10bde142018-12-27 18:23:58 +01005990 nparams = xc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005991 ip.inst_idx = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01005992 ip.func = xc_program_index(code, idx);
5993 func = xc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005994
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005995 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005996 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005997 }
5998 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005999 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006000 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01006001 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006002
6003 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006004 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006005
6006 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006007 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006008
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006009 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006010 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006011
Denys Vlasenko10bde142018-12-27 18:23:58 +01006012 s = zxc_program_copyToVar(a->name, a->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006013 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006014 }
6015
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006016 a = bc_vec_item(&func->autos, i);
6017 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006018 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006019
Denys Vlasenko10bde142018-12-27 18:23:58 +01006020 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006021 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006022 BcNum n2;
6023 bc_num_init_DEF_SIZE(&n2);
6024 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006025 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006026 BcVec v2;
6027 bc_array_init(&v2, true);
6028 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006029 }
6030 }
6031
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006032 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006033
Denys Vlasenko29301232018-12-11 15:29:32 +01006034 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006035}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006036#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006037
Denys Vlasenko29301232018-12-11 15:29:32 +01006038static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006039{
Gavin Howard01055ba2018-11-03 11:00:21 -06006040 BcResult res;
6041 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006042 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006043 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006044 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006045
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006046 if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->results_len_before_call + (inst == XC_INST_RET)))
Denys Vlasenko29301232018-12-11 15:29:32 +01006047 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006048
Denys Vlasenko10bde142018-12-27 18:23:58 +01006049 f = xc_program_func(ip->func);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006050 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006051
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006052 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006053 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006054 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006055 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006056
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006057 s = zxc_program_num(operand, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006058 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006059 bc_num_init(&res.d.n, num->len);
6060 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006061 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006062 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006063 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006064 }
6065
6066 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006067 a = (void*)f->autos.v;
6068 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006069 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006070 v = xc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006071 bc_vec_pop(v);
6072 }
6073
Denys Vlasenko24e41942018-12-21 23:01:26 +01006074 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006075 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006076 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006077
Denys Vlasenko29301232018-12-11 15:29:32 +01006078 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006079}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006080#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006081#endif // ENABLE_BC
6082
Denys Vlasenko10bde142018-12-27 18:23:58 +01006083static unsigned long xc_program_scale(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006084{
6085 return (unsigned long) n->rdx;
6086}
6087
Denys Vlasenko10bde142018-12-27 18:23:58 +01006088static unsigned long xc_program_len(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06006089{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006090 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006091
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006092 if (n->rdx != len) return len;
6093 for (;;) {
6094 if (len == 0) break;
6095 len--;
6096 if (n->num[len] != 0) break;
6097 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006098 return len;
6099}
6100
Denys Vlasenko10bde142018-12-27 18:23:58 +01006101static BC_STATUS zxc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006102{
6103 BcStatus s;
6104 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006105 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006106 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006107 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006108
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006109 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006110 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006111 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006112
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006113 s = zxc_program_num(opnd, &num);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006114 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006115
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006116#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006117 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006118 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006119#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006120
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006121 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006122
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006123 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006124 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006125#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006126 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006127 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006128 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006129#endif
6130#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006131 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006132 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006133 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006134
Denys Vlasenko10bde142018-12-27 18:23:58 +01006135 str = xc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006136 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006137 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006138#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006139 else {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006140 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006141 }
6142
Denys Vlasenko10bde142018-12-27 18:23:58 +01006143 xc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006144
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006145 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006146}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006147#define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006148
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006149#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006150static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006151{
6152 BcStatus s;
6153 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006154 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006155
Denys Vlasenko10bde142018-12-27 18:23:58 +01006156 s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006157 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006158
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006159 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006160 bc_num_init(&res2.d.n, n2->len);
6161
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006162 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006163 if (s) goto err;
6164
Denys Vlasenko10bde142018-12-27 18:23:58 +01006165 xc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006166 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006167 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006168
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006169 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006170 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006171 bc_num_free(&res2.d.n);
6172 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006173 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006174}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006175#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006176
Denys Vlasenkofa210792018-12-19 19:35:40 +01006177static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006178{
6179 BcStatus s;
6180 BcResult *r1, *r2, *r3, res;
6181 BcNum *n1, *n2, *n3;
6182
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006183 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006184 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko10bde142018-12-27 18:23:58 +01006185 s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006186 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006187
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006188 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006189 s = zxc_program_num(r1, &n1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006190 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006191 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006192 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006193
6194 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006195 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006196 if (r1->t == r2->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006197 s = zxc_program_num(r2, &n2);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006198 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006199 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006200 if (r1->t == r3->t) {
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006201 s = zxc_program_num(r3, &n3);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006202 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203 }
6204 }
6205
6206 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006207 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006208 if (s) goto err;
6209
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006210 bc_vec_pop(&G.prog.results);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006211 xc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006212
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006213 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006214 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006215 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006216 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006217}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006218#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006219
Denys Vlasenkofa210792018-12-19 19:35:40 +01006220static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006221{
Gavin Howard01055ba2018-11-03 11:00:21 -06006222 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006223 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006224
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006225 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006226
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006227 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006228 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006229 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006230}
6231
Denys Vlasenkofa210792018-12-19 19:35:40 +01006232static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006233{
6234 BcStatus s;
6235 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006236 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006237 char **strs;
6238 char *str;
6239 char c;
6240 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006241
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006242 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006243 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006244
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006245 r = bc_vec_top(&G.prog.results);
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006246 s = zxc_program_num(r, &num);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006247 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006248
6249 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006250 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006251 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006252 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006253
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006254 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006255 bc_num_copy(&n, num);
6256 bc_num_truncate(&n, n.rdx);
6257
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006258 strmb.cap = ARRAY_SIZE(strmb_digs);
6259 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006260 bc_num_ulong2num(&strmb, 0x100);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006261
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01006262 s = zbc_num_mod(&n, &strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006263 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006264 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006265 if (s) goto num_err;
6266
6267 c = (char) val;
6268
6269 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006270 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006271 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006272 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006273 sp = *xc_program_str(idx);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006274 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006275 }
6276
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006277 strs = (void*)G.prog.strs.v;
6278 for (idx = 0; idx < G.prog.strs.len; idx++) {
6279 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6280 goto dup;
6281 }
6282 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006283 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006284 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006285 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006286 bc_vec_push(&G.prog.strs, &str);
6287 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006288 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006289 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006290 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006291
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006292 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006293 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006294 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006295 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006296}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006297#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006298
Denys Vlasenkofa210792018-12-19 19:35:40 +01006299static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006300{
6301 BcStatus s;
6302 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006303 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006304 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006305
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006306 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006307 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006308 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006309
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006310 s = zxc_program_num(r, &n);
Denys Vlasenko29301232018-12-11 15:29:32 +01006311 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006312
Denys Vlasenko57734c92018-12-17 21:14:05 +01006313 if (BC_PROG_NUM(r, n)) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006314 s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006315 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006316 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006317 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006318 str = *xc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006319 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006320 }
6321
Denys Vlasenko29301232018-12-11 15:29:32 +01006322 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006323}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006324#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006325
Denys Vlasenkofa210792018-12-19 19:35:40 +01006326static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006327{
6328 BcStatus s;
6329 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006330 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006331 unsigned long val;
6332
Denys Vlasenko10bde142018-12-27 18:23:58 +01006333 s = zxc_program_prep(&opnd, &num);
Denys Vlasenko29301232018-12-11 15:29:32 +01006334 if (s) RETURN_STATUS(s);
6335 s = zbc_num_ulong(num, &val);
6336 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006337
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006338 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006339
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006340 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006341 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006342 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006343 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006344 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006345
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006346 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006347
Denys Vlasenko29301232018-12-11 15:29:32 +01006348 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006349}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006350#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006351
Denys Vlasenkofa210792018-12-19 19:35:40 +01006352static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006353{
6354 BcStatus s = BC_STATUS_SUCCESS;
6355 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006356 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006357 BcInstPtr ip;
6358 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006359
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006360 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006361 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006362
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006363 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006364
6365 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006366 BcNum *n = n; // for compiler
6367 bool exec;
6368 char *name;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006369 char *then_name = xc_program_name(code, bgn);
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006370 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006371
Denys Vlasenkof706a182018-12-26 20:02:09 +01006372 if (code[*bgn] == '\0')
Gavin Howard01055ba2018-11-03 11:00:21 -06006373 (*bgn) += 1;
6374 else
Denys Vlasenko10bde142018-12-27 18:23:58 +01006375 else_name = xc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006376
6377 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006378 name = then_name;
6379 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006380 exec = true;
6381 name = else_name;
6382 }
6383
6384 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006385 BcVec *v;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006386 v = xc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006387 n = bc_vec_top(v);
6388 }
6389
6390 free(then_name);
6391 free(else_name);
6392
6393 if (!exec) goto exit;
6394 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006395 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006396 goto exit;
6397 }
6398
6399 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006400 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006401 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006402 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006403 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006404 BcNum *n;
Denys Vlasenko374d2c42018-12-29 14:52:30 +01006405 s = zxc_program_num(r, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006406 if (s || !BC_PROG_STR(n)) goto exit;
6407 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006408 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006409 goto exit;
6410 }
6411
6412 fidx = sidx + BC_PROG_REQ_FUNCS;
6413
Denys Vlasenko10bde142018-12-27 18:23:58 +01006414 f = xc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006415
6416 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006417 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006418 char *str;
6419
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006420 sv_parse = G.prs; // struct copy
Denys Vlasenko10bde142018-12-27 18:23:58 +01006421 xc_parse_create(fidx);
6422 str = *xc_program_str(sidx);
6423 s = zxc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006424 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006425
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006426 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006427 if (s) goto err;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006428 xc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006429 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006430 s = bc_error_bad_expression();
Denys Vlasenko10bde142018-12-27 18:23:58 +01006431 xc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006432 G.prs = sv_parse; // struct copy
6433 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006434 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006435 bc_vec_pop_all(&f->code);
6436 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006437 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006438 }
6439
Denys Vlasenko24e41942018-12-21 23:01:26 +01006440 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006441 ip.func = fidx;
6442
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006443 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006444 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006445
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006446 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006447 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006448 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006449 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006450}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006451#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006452#endif // ENABLE_DC
6453
Denys Vlasenko10bde142018-12-27 18:23:58 +01006454static void xc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006455{
Gavin Howard01055ba2018-11-03 11:00:21 -06006456 BcResult res;
6457 unsigned long val;
6458
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006459 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006460 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006461 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006462 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006463 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006464 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006465 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006466
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006467 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006468 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006469 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006470}
6471
Denys Vlasenko10bde142018-12-27 18:23:58 +01006472static BC_STATUS zxc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006473{
Gavin Howard01055ba2018-11-03 11:00:21 -06006474 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006475 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006476 BcFunc *func = xc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006477 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006478
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006479 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006480 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6481 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006482 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006483 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006484
Denys Vlasenko24e41942018-12-21 23:01:26 +01006485 dbg_exec("inst at %zd:%d results.len:%d", ip->inst_idx - 1, inst, G.prog.results.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006486 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006487#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006488 case BC_INST_JUMP_ZERO: {
6489 BcNum *num;
6490 bool zero;
6491 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006492 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006493 if (s) RETURN_STATUS(s);
6494 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6495 bc_vec_pop(&G.prog.results);
6496 if (!zero) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006497 xc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006498 break;
6499 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006500 // else: fall through
6501 }
6502 case BC_INST_JUMP: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006503 size_t idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006504 size_t *addr = bc_vec_item(&func->labels, idx);
6505 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6506 ip->inst_idx = *addr;
6507 break;
6508 }
6509 case BC_INST_CALL:
6510 dbg_exec("BC_INST_CALL:");
6511 s = zbc_program_call(code, &ip->inst_idx);
6512 goto read_updated_ip;
6513 case BC_INST_INC_PRE:
6514 case BC_INST_DEC_PRE:
6515 case BC_INST_INC_POST:
6516 case BC_INST_DEC_POST:
6517 dbg_exec("BC_INST_INCDEC:");
6518 s = zbc_program_incdec(inst);
6519 break;
6520 case BC_INST_HALT:
6521 dbg_exec("BC_INST_HALT:");
6522 QUIT_OR_RETURN_TO_MAIN;
6523 break;
6524 case XC_INST_RET:
6525 case BC_INST_RET0:
6526 dbg_exec("BC_INST_RET[0]:");
6527 s = zbc_program_return(inst);
6528 goto read_updated_ip;
6529 case XC_INST_BOOL_OR:
6530 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006531#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006532 case XC_INST_REL_EQ:
6533 case XC_INST_REL_LE:
6534 case XC_INST_REL_GE:
6535 case XC_INST_REL_NE:
6536 case XC_INST_REL_LT:
6537 case XC_INST_REL_GT:
6538 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006539 s = zxc_program_logical(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006540 break;
6541 case XC_INST_READ:
6542 dbg_exec("XC_INST_READ:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006543 s = zxc_program_read();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006544 goto read_updated_ip;
6545 case XC_INST_VAR:
6546 dbg_exec("XC_INST_VAR:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006547 s = zxc_program_pushVar(code, &ip->inst_idx, false, false);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006548 break;
6549 case XC_INST_ARRAY_ELEM:
6550 case XC_INST_ARRAY:
6551 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6552 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6553 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006554#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006555 case BC_INST_LAST:
6556 dbg_exec("BC_INST_LAST:");
6557 r.t = BC_RESULT_LAST;
6558 bc_vec_push(&G.prog.results, &r);
6559 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006560#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006561 case XC_INST_IBASE:
6562 case XC_INST_OBASE:
6563 case XC_INST_SCALE:
6564 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006565 xc_program_pushGlobal(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006566 break;
6567 case XC_INST_SCALE_FUNC:
6568 case XC_INST_LENGTH:
6569 case XC_INST_SQRT:
6570 dbg_exec("BC_INST_builtin:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006571 s = zxc_program_builtin(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006572 break;
6573 case XC_INST_NUM:
6574 dbg_exec("XC_INST_NUM:");
6575 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006576 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006577 bc_vec_push(&G.prog.results, &r);
6578 break;
6579 case XC_INST_POP:
6580 dbg_exec("XC_INST_POP:");
6581 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6582 s = bc_error_stack_has_too_few_elements();
6583 else
6584 bc_vec_pop(&G.prog.results);
6585 break;
6586 case XC_INST_PRINT:
6587 case XC_INST_PRINT_POP:
6588 case XC_INST_PRINT_STR:
6589 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006590 s = zxc_program_print(inst, 0);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006591 break;
6592 case XC_INST_STR:
6593 dbg_exec("XC_INST_STR:");
6594 r.t = XC_RESULT_STR;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006595 r.d.id.idx = xc_program_index(code, &ip->inst_idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006596 bc_vec_push(&G.prog.results, &r);
6597 break;
6598 case XC_INST_POWER:
6599 case XC_INST_MULTIPLY:
6600 case XC_INST_DIVIDE:
6601 case XC_INST_MODULUS:
6602 case XC_INST_PLUS:
6603 case XC_INST_MINUS:
6604 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006605 s = zxc_program_op(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006606 break;
6607 case XC_INST_BOOL_NOT: {
6608 BcNum *num;
6609 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006610 s = zxc_program_prep(&ptr, &num);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006611 if (s) RETURN_STATUS(s);
6612 bc_num_init_DEF_SIZE(&r.d.n);
6613 if (bc_num_cmp(num, &G.prog.zero) == 0)
6614 bc_num_one(&r.d.n);
6615 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenko10bde142018-12-27 18:23:58 +01006616 xc_program_retire(&r, XC_RESULT_TEMP);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006617 break;
6618 }
6619 case XC_INST_NEG:
6620 dbg_exec("XC_INST_NEG:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006621 s = zxc_program_negate();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006622 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006623#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006624 case BC_INST_ASSIGN_POWER:
6625 case BC_INST_ASSIGN_MULTIPLY:
6626 case BC_INST_ASSIGN_DIVIDE:
6627 case BC_INST_ASSIGN_MODULUS:
6628 case BC_INST_ASSIGN_PLUS:
6629 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006630#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006631 case XC_INST_ASSIGN:
6632 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko10bde142018-12-27 18:23:58 +01006633 s = zxc_program_assign(inst);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006634 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006635#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006636 case DC_INST_POP_EXEC:
6637 dbg_exec("DC_INST_POP_EXEC:");
6638 bc_vec_pop(&G.prog.exestack);
6639 goto read_updated_ip;
6640 case DC_INST_MODEXP:
6641 dbg_exec("DC_INST_MODEXP:");
6642 s = zdc_program_modexp();
6643 break;
6644 case DC_INST_DIVMOD:
6645 dbg_exec("DC_INST_DIVMOD:");
6646 s = zdc_program_divmod();
6647 break;
6648 case DC_INST_EXECUTE:
6649 case DC_INST_EXEC_COND:
6650 dbg_exec("DC_INST_EXEC[_COND]:");
6651 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6652 goto read_updated_ip;
6653 case DC_INST_PRINT_STACK: {
6654 size_t idx;
6655 dbg_exec("DC_INST_PRINT_STACK:");
6656 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006657 s = zxc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006658 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006659 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006660 break;
6661 }
6662 case DC_INST_CLEAR_STACK:
6663 dbg_exec("DC_INST_CLEAR_STACK:");
6664 bc_vec_pop_all(&G.prog.results);
6665 break;
6666 case DC_INST_STACK_LEN:
6667 dbg_exec("DC_INST_STACK_LEN:");
6668 dc_program_stackLen();
6669 break;
6670 case DC_INST_DUPLICATE:
6671 dbg_exec("DC_INST_DUPLICATE:");
6672 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6673 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6674 ptr = bc_vec_top(&G.prog.results);
6675 dc_result_copy(&r, ptr);
6676 bc_vec_push(&G.prog.results, &r);
6677 break;
6678 case DC_INST_SWAP: {
6679 BcResult *ptr2;
6680 dbg_exec("DC_INST_SWAP:");
6681 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6682 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6683 ptr = bc_vec_item_rev(&G.prog.results, 0);
6684 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6685 memcpy(&r, ptr, sizeof(BcResult));
6686 memcpy(ptr, ptr2, sizeof(BcResult));
6687 memcpy(ptr2, &r, sizeof(BcResult));
6688 break;
6689 }
6690 case DC_INST_ASCIIFY:
6691 dbg_exec("DC_INST_ASCIIFY:");
6692 s = zdc_program_asciify();
6693 break;
6694 case DC_INST_PRINT_STREAM:
6695 dbg_exec("DC_INST_PRINT_STREAM:");
6696 s = zdc_program_printStream();
6697 break;
6698 case DC_INST_LOAD:
6699 case DC_INST_PUSH_VAR: {
6700 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko10bde142018-12-27 18:23:58 +01006701 s = zxc_program_pushVar(code, &ip->inst_idx, true, copy);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006702 break;
6703 }
6704 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006705 char *name = xc_program_name(code, &ip->inst_idx);
6706 s = zxc_program_copyToVar(name, true);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006707 free(name);
6708 break;
6709 }
6710 case DC_INST_QUIT:
6711 dbg_exec("DC_INST_QUIT:");
6712 if (G.prog.exestack.len <= 2)
6713 QUIT_OR_RETURN_TO_MAIN;
6714 bc_vec_npop(&G.prog.exestack, 2);
6715 goto read_updated_ip;
6716 case DC_INST_NQUIT:
6717 dbg_exec("DC_INST_NQUIT:");
6718 s = zdc_program_nquit();
6719 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006720#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006721 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006722 // Instruction stack has changed, read new pointers
6723 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006724 func = xc_program_func(ip->func);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006725 code = func->code.v;
6726 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006727 }
6728
Denys Vlasenkod38af482018-12-04 19:11:02 +01006729 if (s || G_interrupt) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006730 xc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006731 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006732 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006733
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006734 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006735 }
6736
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006737 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006738}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006739#define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006740
Denys Vlasenko10bde142018-12-27 18:23:58 +01006741static unsigned xc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006742{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006743 char *lenv;
6744 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006745
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006746 lenv = getenv(var);
6747 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006748 if (!lenv) return len;
6749
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006750 len = bb_strtou(lenv, NULL, 10) - 1;
6751 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006752 len = BC_NUM_PRINT_WIDTH;
6753
6754 return len;
6755}
6756
Denys Vlasenko10bde142018-12-27 18:23:58 +01006757static BC_STATUS zxc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006758{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006759 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006760
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006761 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006762 s = zxc_parse_text_init(text); // does the first zxc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006763 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006764
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006765 IF_BC(check_eof:)
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006766 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006767 BcInstPtr *ip;
6768 BcFunc *f;
6769
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006770 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006771 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006772#if ENABLE_BC
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006773 if (G.prs.lex == BC_LEX_SCOLON
6774 || G.prs.lex == XC_LEX_NLINE
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006775 ) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006776 s = zxc_lex_next();
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006777 if (s) goto err;
6778 goto check_eof;
6779 }
6780
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006781 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006782 if (s) goto err;
6783
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006784 // Check that next token is a correct stmt delimiter -
6785 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006786 if (G.prs.lex != BC_LEX_SCOLON
6787 && G.prs.lex != XC_LEX_NLINE
6788 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006789 ) {
Denys Vlasenko22314682019-01-01 21:50:14 +01006790 bc_error_at("bad statement terminator");
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006791 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006792 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006793 // The above logic is fragile. Check these examples:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006794 // - interactive read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006795#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006796 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006797#if ENABLE_DC
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006798 // Most of dc parsing assumes all whitespace,
6799 // including '\n', is eaten.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006800 while (G.prs.lex == XC_LEX_NLINE) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006801 s = zxc_lex_next();
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006802 if (s) goto err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006803 if (G.prs.lex == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006804 goto done;
6805 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006806 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006807#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006808 }
6809 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006810 err:
Denys Vlasenko10bde142018-12-27 18:23:58 +01006811 xc_parse_reset(); // includes xc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006812 RETURN_STATUS(BC_STATUS_FAILURE);
6813 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006814
Denys Vlasenko39287e02018-12-22 02:23:08 +01006815 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006816 s = zxc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006817 if (s) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006818 xc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006819 break;
6820 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006821
6822 ip = (void*)G.prog.exestack.v;
6823#if SANITY_CHECKS
6824 if (G.prog.exestack.len != 1) // should have only main's IP
6825 bb_error_msg_and_die("BUG:call stack");
6826 if (ip->func != BC_PROG_MAIN)
6827 bb_error_msg_and_die("BUG:not MAIN");
6828#endif
Denys Vlasenko10bde142018-12-27 18:23:58 +01006829 f = xc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006830 // bc discards strings, constants and code after each
6831 // top-level statement in the "main program".
6832 // This prevents "yes 1 | bc" from growing its memory
6833 // without bound. This can be done because data stack
6834 // is empty and thus can't hold any references to
6835 // strings or constants, there is no generated code
6836 // which can hold references (after we discard one
6837 // we just executed). Code of functions can have references,
6838 // but bc stores function strings/constants in per-function
6839 // storage.
6840 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006841#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006842 if (G.prog.results.len != 0) // should be empty
6843 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006844#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006845 IF_BC(bc_vec_pop_all(&f->strs);)
6846 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006847 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006848 if (G.prog.results.len == 0
6849 && G.prog.vars.len == 0
6850 ) {
6851 // If stack is empty and no registers exist (TODO: or they are all empty),
6852 // we can get rid of accumulated strings and constants.
6853 // In this example dc process should not grow
6854 // its memory consumption with time:
6855 // yes 1pc | dc
6856 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6857 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6858 }
6859 // The code is discarded always (below), thus this example
6860 // should also not grow its memory consumption with time,
6861 // even though its data stack is not empty:
6862 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006863 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006864 // We drop generated and executed code for both bc and dc:
6865 bc_vec_pop_all(&f->code);
6866 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006867 }
Denys Vlasenko8a56e362018-12-26 19:09:23 +01006868 IF_DC(done:)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006869 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006870 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006871}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006872#define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006873
Denys Vlasenko10bde142018-12-27 18:23:58 +01006874static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006875{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006876 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006877 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006878 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006879 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006880
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006881 G.prs.lex_filename = filename;
6882 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006883 G.err_line = G.prs.lex_line = 1;
Denys Vlasenko22314682019-01-01 21:50:14 +01006884 dbg_lex("p->lex_line reset to 1");
Gavin Howard01055ba2018-11-03 11:00:21 -06006885
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006886 do {
Denys Vlasenko10bde142018-12-27 18:23:58 +01006887 s = zxc_vm_process("");
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006888 // We do not stop looping on errors here if reading stdin.
6889 // Example: start interactive bc and enter "return".
6890 // It should say "'return' not in a function"
6891 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006892 } while (G.prs.lex_input_fp == stdin);
6893 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006894 RETURN_STATUS(s);
6895}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006896#define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006897
Denys Vlasenko10bde142018-12-27 18:23:58 +01006898static BC_STATUS zxc_vm_file(const char *file)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006899{
6900 BcStatus s;
6901 FILE *fp;
6902
6903 fp = xfopen_for_read(file);
Denys Vlasenko10bde142018-12-27 18:23:58 +01006904 s = zxc_vm_execute_FILE(fp, file);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006905 fclose(fp);
6906
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006907 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006908}
Denys Vlasenko10bde142018-12-27 18:23:58 +01006909#define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006910
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006911#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006912static void bc_vm_info(void)
6913{
6914 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07006915 "Adapted from https://github.com/gavinhoward/bc\n"
6916 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01006917 , applet_name);
6918}
6919
6920static void bc_args(char **argv)
6921{
6922 unsigned opts;
6923 int i;
6924
6925 GETOPT_RESET();
6926#if ENABLE_FEATURE_BC_LONG_OPTIONS
6927 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6928 "warn\0" No_argument "w"
6929 "version\0" No_argument "v"
6930 "standard\0" No_argument "s"
6931 "quiet\0" No_argument "q"
6932 "mathlib\0" No_argument "l"
6933 "interactive\0" No_argument "i"
6934 );
6935#else
6936 opts = option_mask32 |= getopt32(argv, "wvsqli");
6937#endif
6938 if (getenv("POSIXLY_CORRECT"))
6939 option_mask32 |= BC_FLAG_S;
6940
6941 if (opts & BC_FLAG_V) {
6942 bc_vm_info();
6943 exit(0);
6944 }
6945
6946 for (i = optind; argv[i]; ++i)
6947 bc_vec_push(&G.files, argv + i);
6948}
6949
6950static void bc_vm_envArgs(void)
6951{
6952 BcVec v;
6953 char *buf;
6954 char *env_args = getenv("BC_ENV_ARGS");
6955
6956 if (!env_args) return;
6957
6958 G.env_args = xstrdup(env_args);
6959 buf = G.env_args;
6960
6961 bc_vec_init(&v, sizeof(char *), NULL);
6962
6963 while (*(buf = skip_whitespace(buf)) != '\0') {
6964 bc_vec_push(&v, &buf);
6965 buf = skip_non_whitespace(buf);
6966 if (!*buf)
6967 break;
6968 *buf++ = '\0';
6969 }
6970
6971 // NULL terminate, and pass argv[] so that first arg is argv[1]
6972 if (sizeof(int) == sizeof(char*)) {
6973 bc_vec_push(&v, &const_int_0);
6974 } else {
6975 static char *const nullptr = NULL;
6976 bc_vec_push(&v, &nullptr);
6977 }
6978 bc_args(((char **)v.v) - 1);
6979
6980 bc_vec_free(&v);
6981}
6982
6983static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006984 "scale=20"
6985"\n" "define e(x){"
6986"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006987////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6988//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6989//e(-.998896): GNU:.36828580434569428695
6990// above code:.36828580434569428696
6991// actual value:.3682858043456942869594...
6992// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006993"\n" "b=ibase"
6994"\n" "ibase=A"
6995"\n" "if(x<0){"
6996"\n" "n=1"
6997"\n" "x=-x"
6998"\n" "}"
6999"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007000"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007001"\n" "scale=scale(x)+1"
7002"\n" "while(x>1){"
7003"\n" "d+=1"
7004"\n" "x/=2"
7005"\n" "scale+=1"
7006"\n" "}"
7007"\n" "scale=r"
7008"\n" "r=x+1"
7009"\n" "p=x"
7010"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007011"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007012"\n" "p*=x"
7013"\n" "f*=i"
7014"\n" "v=p/f"
7015"\n" "r+=v"
7016"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007017"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007018"\n" "scale=s"
7019"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007020"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007021"\n" "return(r/1)"
7022"\n" "}"
7023"\n" "define l(x){"
7024"\n" "auto b,s,r,p,a,q,i,v"
7025"\n" "b=ibase"
7026"\n" "ibase=A"
7027"\n" "if(x<=0){"
7028"\n" "r=(1-10^scale)/1"
7029"\n" "ibase=b"
7030"\n" "return(r)"
7031"\n" "}"
7032"\n" "s=scale"
7033"\n" "scale+=6"
7034"\n" "p=2"
7035"\n" "while(x>=2){"
7036"\n" "p*=2"
7037"\n" "x=sqrt(x)"
7038"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007039"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007040"\n" "p*=2"
7041"\n" "x=sqrt(x)"
7042"\n" "}"
7043"\n" "r=a=(x-1)/(x+1)"
7044"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007045"\n" "v=1"
7046"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007047"\n" "a*=q"
7048"\n" "v=a/i"
7049"\n" "r+=v"
7050"\n" "}"
7051"\n" "r*=p"
7052"\n" "scale=s"
7053"\n" "ibase=b"
7054"\n" "return(r/1)"
7055"\n" "}"
7056"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007057"\n" "auto b,s,r,a,q,i"
7058"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007059"\n" "b=ibase"
7060"\n" "ibase=A"
7061"\n" "s=scale"
7062"\n" "scale=1.1*s+2"
7063"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007064"\n" "scale=0"
7065"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007066"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007067"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007068"\n" "scale=s+2"
7069"\n" "r=a=x"
7070"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007071"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007072"\n" "a*=q/(i*(i-1))"
7073"\n" "r+=a"
7074"\n" "}"
7075"\n" "scale=s"
7076"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007077"\n" "return(r/1)"
7078"\n" "}"
7079"\n" "define c(x){"
7080"\n" "auto b,s"
7081"\n" "b=ibase"
7082"\n" "ibase=A"
7083"\n" "s=scale"
7084"\n" "scale*=1.2"
7085"\n" "x=s(2*a(1)+x)"
7086"\n" "scale=s"
7087"\n" "ibase=b"
7088"\n" "return(x/1)"
7089"\n" "}"
7090"\n" "define a(x){"
7091"\n" "auto b,s,r,n,a,m,t,f,i,u"
7092"\n" "b=ibase"
7093"\n" "ibase=A"
7094"\n" "n=1"
7095"\n" "if(x<0){"
7096"\n" "n=-1"
7097"\n" "x=-x"
7098"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007099"\n" "if(scale<65){"
7100"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7101"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007102"\n" "}"
7103"\n" "s=scale"
7104"\n" "if(x>.2){"
7105"\n" "scale+=5"
7106"\n" "a=a(.2)"
7107"\n" "}"
7108"\n" "scale=s+3"
7109"\n" "while(x>.2){"
7110"\n" "m+=1"
7111"\n" "x=(x-.2)/(1+.2*x)"
7112"\n" "}"
7113"\n" "r=u=x"
7114"\n" "f=-x*x"
7115"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007116"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007117"\n" "u*=f"
7118"\n" "t=u/i"
7119"\n" "r+=t"
7120"\n" "}"
7121"\n" "scale=s"
7122"\n" "ibase=b"
7123"\n" "return((m*a+r)/n)"
7124"\n" "}"
7125"\n" "define j(n,x){"
7126"\n" "auto b,s,o,a,i,v,f"
7127"\n" "b=ibase"
7128"\n" "ibase=A"
7129"\n" "s=scale"
7130"\n" "scale=0"
7131"\n" "n/=1"
7132"\n" "if(n<0){"
7133"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007134"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007135"\n" "}"
7136"\n" "a=1"
7137"\n" "for(i=2;i<=n;++i)a*=i"
7138"\n" "scale=1.5*s"
7139"\n" "a=(x^n)/2^n/a"
7140"\n" "r=v=1"
7141"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007142"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007143"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007144"\n" "v=v*f/i/(n+i)"
7145"\n" "r+=v"
7146"\n" "}"
7147"\n" "scale=s"
7148"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007149"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007150"\n" "return(a*r/1)"
7151"\n" "}"
7152};
7153#endif // ENABLE_BC
7154
Denys Vlasenko10bde142018-12-27 18:23:58 +01007155static BC_STATUS zxc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007156{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007157 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007158 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007159 size_t i;
7160
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007161#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007162 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007163 // We know that internal library is not buggy,
7164 // thus error checking is normally disabled.
7165# define DEBUG_LIB 0
Denys Vlasenko10bde142018-12-27 18:23:58 +01007166 s = zxc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007167 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007168 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007169#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007170
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007171 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007172 fname = (void*)G.files.v;
7173 for (i = 0; i < G.files.len; i++) {
Denys Vlasenko10bde142018-12-27 18:23:58 +01007174 s = zxc_vm_file(*fname++);
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007175 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7176 // Debug config, non-interactive mode:
7177 // return all the way back to main.
7178 // Non-debug builds do not come here
7179 // in non-interactive mode, they exit.
7180 RETURN_STATUS(s);
7181 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007182 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007183
Denys Vlasenko91cde952018-12-10 20:56:08 +01007184 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko10bde142018-12-27 18:23:58 +01007185 s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007186
Denys Vlasenko19f11072018-12-12 22:48:19 +01007187 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007188}
Denys Vlasenko10bde142018-12-27 18:23:58 +01007189#define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007190
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007191#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko10bde142018-12-27 18:23:58 +01007192static void xc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007193{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007194 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007195 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007196 bc_vec_free(&G.prog.vars);
7197 bc_vec_free(&G.prog.var_map);
7198 bc_vec_free(&G.prog.arrs);
7199 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007200 IF_DC(bc_vec_free(&G.prog.strs);)
7201 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007202 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007203 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007204 IF_BC(bc_num_free(&G.prog.last);)
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007205 //IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007206 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007207 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007208}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007209#endif
7210
Denys Vlasenko10bde142018-12-27 18:23:58 +01007211static void xc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007212{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007213 BcInstPtr ip;
7214
Denys Vlasenko82269122018-12-14 16:30:56 +01007215 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007216 memset(&ip, 0, sizeof(BcInstPtr));
7217
Denys Vlasenko82269122018-12-14 16:30:56 +01007218 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007219 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007220 G.prog.ob_t = 10;
7221
Denys Vlasenko503faf92018-12-20 16:24:18 +01007222 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7223 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007224
Denys Vlasenkodb8d6072018-12-27 18:08:30 +01007225 //bc_num_init_DEF_SIZE(&G.prog.zero); - not needed
Denys Vlasenko3129f702018-12-09 12:04:44 +01007226 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007227
Denys Vlasenko503faf92018-12-20 16:24:18 +01007228 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7229 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007230
7231 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007232 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007233
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007234 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007235 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007236 // a valid function name (and be short)
7237 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007238 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007239 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007240 // in dc, functions have no names
Denys Vlasenko10bde142018-12-27 18:23:58 +01007241 xc_program_add_fn();
7242 xc_program_add_fn();
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007243 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007244
7245 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007246 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007247
7248 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007249 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007250
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007251 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7252 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007253 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007254 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7255 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007256
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007257 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007258}
Gavin Howard01055ba2018-11-03 11:00:21 -06007259
Denys Vlasenko10bde142018-12-27 18:23:58 +01007260static int xc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007261{
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007262 G.prog.len = xc_vm_envLen(env_len);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007263#if ENABLE_FEATURE_EDITING
7264 G.line_input_state = new_line_input_t(DO_HISTORY);
7265#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007266 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007267
Denys Vlasenko10bde142018-12-27 18:23:58 +01007268 xc_program_init();
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007269 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko10bde142018-12-27 18:23:58 +01007270 xc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007271
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007272//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7273//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007274 if (isatty(0)) {
Denys Vlasenko14767602018-12-27 22:52:13 +01007275#if ENABLE_FEATURE_BC_INTERACTIVE
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007276 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007277 // With SA_RESTART, most system calls will restart
7278 // (IOW: they won't fail with EINTR).
7279 // In particular, this means ^C won't cause
7280 // stdout to get into "error state" if SIGINT hits
7281 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007282 //
7283 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007284 // will only be handled after [Enter] since read()
7285 // from stdin is not interrupted by ^C either,
7286 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007287 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007288 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7289
7290 // Without SA_RESTART, this exhibits a bug:
7291 // "while (1) print 1" and try ^C-ing it.
7292 // Intermittently, instead of returning to input line,
7293 // you'll get "output error: Interrupted system call"
7294 // and exit.
7295 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007296#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007297 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007298 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007299 return 0; // "not a tty"
7300}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007301
Denys Vlasenko10bde142018-12-27 18:23:58 +01007302static BcStatus xc_vm_run(void)
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007303{
Denys Vlasenko10bde142018-12-27 18:23:58 +01007304 BcStatus st = zxc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007305#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007306 if (G_exiting) // it was actually "halt" or "quit"
7307 st = EXIT_SUCCESS;
Denys Vlasenko2cd8c042018-12-30 15:56:36 +01007308
7309 bc_vec_free(&G.files);
7310 xc_program_free();
7311 xc_parse_free();
7312 free(G.env_args);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007313# if ENABLE_FEATURE_EDITING
7314 free_line_input_t(G.line_input_state);
7315# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007316 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007317#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007318 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007319 return st;
7320}
7321
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007322#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007323int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007324int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007325{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007326 int is_tty;
7327
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007328 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007329
Denys Vlasenko10bde142018-12-27 18:23:58 +01007330 is_tty = xc_vm_init("BC_LINE_LENGTH");
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007331
7332 bc_args(argv);
7333
7334 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7335 bc_vm_info();
7336
Denys Vlasenko10bde142018-12-27 18:23:58 +01007337 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007338}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007339#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007340
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007341#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007342int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007343int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007344{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007345 int noscript;
7346
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007347 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007348
7349 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7350 // 1 char wider than bc from the same package.
7351 // Both default width, and xC_LINE_LENGTH=N are wider:
7352 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007353 // |1234\ |
7354 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007355 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007356 // |123\ |
7357 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007358 // Do the same, or it's a bug?
Denys Vlasenko10bde142018-12-27 18:23:58 +01007359 xc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007360
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007361 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7362 noscript = BC_FLAG_I;
7363 for (;;) {
7364 int n = getopt(argc, argv, "e:f:x");
7365 if (n <= 0)
7366 break;
7367 switch (n) {
7368 case 'e':
7369 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007370 n = zxc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007371 if (n) return n;
7372 break;
7373 case 'f':
7374 noscript = 0;
Denys Vlasenko10bde142018-12-27 18:23:58 +01007375 n = zxc_vm_file(optarg);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007376 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007377 break;
7378 case 'x':
7379 option_mask32 |= DC_FLAG_X;
7380 break;
7381 default:
7382 bb_show_usage();
7383 }
7384 }
7385 argv += optind;
7386
7387 while (*argv) {
7388 noscript = 0;
7389 bc_vec_push(&G.files, argv++);
7390 }
7391
7392 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7393
Denys Vlasenko10bde142018-12-27 18:23:58 +01007394 return xc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007395}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007396#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007397
Denys Vlasenko14767602018-12-27 22:52:13 +01007398#endif // DC_BIG
7399