blob: 2b370888c1347e5cd6c86b5538609c9e13f66d46 [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.
4 * Copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06005 */
6//config:config BC
7//config: bool "bc (45 kb; 49 kb when combined with dc)"
8//config: default y
9//config: help
10//config: bc is a command-line, arbitrary-precision calculator with a
11//config: Turing-complete language. See the GNU bc manual
12//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010013//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060014//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010015//config: This bc has five differences to the GNU bc:
16//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060017//config: 2) Arrays are copied before being passed as arguments to
18//config: functions. This behavior is required by the bc spec.
19//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010020//config: the number of elements in the array. This prints "1":
21//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060022//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010023//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060024//config: allows POSIX-compliant scripts to work while somewhat
25//config: preserving expected behavior (versus C) and making parsing
26//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010027//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060028//config:
29//config: Options:
Gavin Howard01055ba2018-11-03 11:00:21 -060030//config: -i --interactive force interactive mode
Denys Vlasenko89e785a2018-12-13 16:35:52 +010031//config: -q --quiet don't print version and copyright
32//config: -s --standard error if any non-POSIX extensions are used
33//config: -w --warn warn if any non-POSIX extensions are used
Gavin Howard01055ba2018-11-03 11:00:21 -060034//config: -l --mathlib use predefined math routines:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010035//config: s(expr) sine in radians
36//config: c(expr) cosine in radians
37//config: a(expr) arctangent, returning radians
38//config: l(expr) natural log
39//config: e(expr) raises e to the power of expr
40//config: j(n, x) Bessel function of integer order n of x
Gavin Howard01055ba2018-11-03 11:00:21 -060041//config:
42//config:config DC
43//config: bool "dc (38 kb; 49 kb when combined with bc)"
44//config: default y
45//config: help
46//config: dc is a reverse-polish notation command-line calculator which
47//config: supports unlimited precision arithmetic. See the FreeBSD man page
48//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010049//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060050//config:
51//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060053//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010054//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060055//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010056//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060057//config: 4) Like the FreeBSD dc, this dc supports extended registers.
58//config: However, they are implemented differently. When it encounters
59//config: whitespace where a register should be, it skips the whitespace.
60//config: If the character following is not a lowercase letter, an error
61//config: is issued. Otherwise, the register name is parsed by the
62//config: following regex:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010063//config: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060064//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010065//config: whitespace. Examples:
66//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060067//config: Also note that, like the FreeBSD dc, extended registers are not
68//config: allowed unless the "-x" option is given.
69//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010070//config:config FEATURE_DC_SMALL
71//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
72//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010073//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010074//config:
75//config:config FEATURE_DC_LIBM
76//config: bool "Enable power and exp functions (requires libm)"
77//config: default y
78//config: depends on FEATURE_DC_SMALL
79//config: help
80//config: Enable power and exp functions.
81//config: NOTE: This will require libm to be present for linking.
82//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060083//config:config FEATURE_BC_SIGNALS
Denys Vlasenko89e785a2018-12-13 16:35:52 +010084//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060085//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010086//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -060087//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010088//config: Enable interactive mode: when started on a tty,
89//config: ^C interrupts execution and returns to command line,
90//config: errors also return to command line instead of exiting,
91//config: line editing with history is available.
92//config:
93//config: With this option off, input can still be taken from tty,
94//config: but all errors are fatal, ^C is fatal,
95//config: tty is treated exactly the same as any other
96//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060097//config:
98//config:config FEATURE_BC_LONG_OPTIONS
99//config: bool "Enable bc/dc long options"
100//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100101//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600102//config: help
103//config: Enable long options for bc and dc.
104
105//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
106//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
107
108//kbuild:lib-$(CONFIG_BC) += bc.o
109//kbuild:lib-$(CONFIG_DC) += bc.o
110
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100111//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600112//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100113//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600114//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100115//usage:#define bc_full_usage "\n"
116//usage: "\nArbitrary precision calculator"
117//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100118///////: "\n -i Interactive" - has no effect for now
119//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100120//usage: "\n -l Load standard math library"
121//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100122//usage: "\n -w Warn if extensions are used"
123///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100124//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100125//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600126//usage:
127//usage:#define bc_example_usage
128//usage: "3 + 4.129\n"
129//usage: "1903 - 2893\n"
130//usage: "-129 * 213.28935\n"
131//usage: "12 / -1932\n"
132//usage: "12 % 12\n"
133//usage: "34 ^ 189\n"
134//usage: "scale = 13\n"
135//usage: "ibase = 2\n"
136//usage: "obase = A\n"
137//usage:
138//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100139//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600140//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100141//usage:#define dc_full_usage "\n"
142//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100143//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100144//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100145//usage: "\nf - print entire stack"
146//usage: "\nk - pop the value and set the precision"
147//usage: "\ni - pop the value and set input radix"
148//usage: "\no - pop the value and set output radix"
149//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600150//usage:
151//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100152//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600153//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100154//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600155//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100156//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600157//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100158//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600159//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600161//usage: "64\n"
162
163#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100164#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600165
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100166#if ENABLE_FEATURE_DC_SMALL
167# include "dc.c"
168#else
169
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100170#define DEBUG_LEXER 0
171#define DEBUG_COMPILE 0
172#define DEBUG_EXEC 0
Denys Vlasenko19eee8e2018-12-21 20:29:34 +0100173// This can be left enabled for production as well:
174#define SANITY_CHECKS 1
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100175
176#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100177static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100178#define dbg_lex(...) \
179 do { \
180 fprintf(stderr, "%*s", lex_indent, ""); \
181 bb_error_msg(__VA_ARGS__); \
182 } while (0)
183#define dbg_lex_enter(...) \
184 do { \
185 dbg_lex(__VA_ARGS__); \
186 lex_indent++; \
187 } while (0)
188#define dbg_lex_done(...) \
189 do { \
190 lex_indent--; \
191 dbg_lex(__VA_ARGS__); \
192 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100193#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100194# define dbg_lex(...) ((void)0)
195# define dbg_lex_enter(...) ((void)0)
196# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100197#endif
198
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100199#if DEBUG_COMPILE
200# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
201#else
202# define dbg_compile(...) ((void)0)
203#endif
204
Denys Vlasenko99b37622018-12-15 20:06:59 +0100205#if DEBUG_EXEC
206# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
207#else
208# define dbg_exec(...) ((void)0)
209#endif
210
Gavin Howard01055ba2018-11-03 11:00:21 -0600211typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100212 BC_STATUS_SUCCESS = 0,
213 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100214 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600215} BcStatus;
216
Gavin Howard01055ba2018-11-03 11:00:21 -0600217#define BC_VEC_INVALID_IDX ((size_t) -1)
218#define BC_VEC_START_CAP (1 << 5)
219
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100220typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600221
222typedef struct BcVec {
223 char *v;
224 size_t len;
225 size_t cap;
226 size_t size;
227 BcVecFree dtor;
228} BcVec;
229
Gavin Howard01055ba2018-11-03 11:00:21 -0600230typedef signed char BcDig;
231
232typedef struct BcNum {
233 BcDig *restrict num;
234 size_t rdx;
235 size_t len;
236 size_t cap;
237 bool neg;
238} BcNum;
239
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100240#define BC_NUM_MAX_IBASE ((unsigned long) 16)
241// larger value might speed up BIGNUM calculations a bit:
242#define BC_NUM_DEF_SIZE (16)
243#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600244
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100245#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600246
Gavin Howard01055ba2018-11-03 11:00:21 -0600247typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100248#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600249 BC_INST_INC_PRE,
250 BC_INST_DEC_PRE,
251 BC_INST_INC_POST,
252 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100253#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100254 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600255
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100256 XC_INST_REL_EQ, // should
257 XC_INST_REL_LE, // match
258 XC_INST_REL_GE, // LEX
259 XC_INST_REL_NE, // constants
260 XC_INST_REL_LT, // for
261 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600262
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100263 XC_INST_POWER, // operations
264 XC_INST_MULTIPLY, // |
265 XC_INST_DIVIDE, // |
266 XC_INST_MODULUS, // |
267 XC_INST_PLUS, // |
268 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600269
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100270 XC_INST_BOOL_NOT, // |
271 XC_INST_BOOL_OR, // |
272 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100273#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100274 BC_INST_ASSIGN_POWER, // |
275 BC_INST_ASSIGN_MULTIPLY,// |
276 BC_INST_ASSIGN_DIVIDE, // |
277 BC_INST_ASSIGN_MODULUS, // |
278 BC_INST_ASSIGN_PLUS, // |
279 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100280#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100281 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600282
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100283 XC_INST_NUM,
284 XC_INST_VAR,
285 XC_INST_ARRAY_ELEM,
286 XC_INST_ARRAY,
287 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100288
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100289 XC_INST_IBASE, // order of these constans should match other enums
290 XC_INST_OBASE, // order of these constans should match other enums
291 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100292 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100293 XC_INST_LENGTH,
294 XC_INST_READ,
295 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600296
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100297 XC_INST_PRINT,
298 XC_INST_PRINT_POP,
299 XC_INST_STR,
300 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600301
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100302#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100303 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600304 BC_INST_JUMP,
305 BC_INST_JUMP_ZERO,
306
307 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600308 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100309#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100310 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600311
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100312 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100313#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100314 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600315
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100316 DC_INST_MODEXP,
317 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600318
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100319 DC_INST_EXECUTE,
320 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600321
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100322 DC_INST_ASCIIFY,
323 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600324
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100325 DC_INST_PRINT_STACK,
326 DC_INST_CLEAR_STACK,
327 DC_INST_STACK_LEN,
328 DC_INST_DUPLICATE,
329 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600330
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100331 DC_INST_LOAD,
332 DC_INST_PUSH_VAR,
333 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600334
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100335 DC_INST_QUIT,
336 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600337
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100338 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100339#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600340} BcInst;
341
342typedef struct BcId {
343 char *name;
344 size_t idx;
345} BcId;
346
347typedef struct BcFunc {
348 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100349 IF_BC(BcVec labels;)
350 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100351 IF_BC(BcVec strs;)
352 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100353 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600354} BcFunc;
355
356typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100357 XC_RESULT_TEMP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600358
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100359 XC_RESULT_VAR,
360 XC_RESULT_ARRAY_ELEM,
361 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600362
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100363 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600364
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100365 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
366 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
367 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
368 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100369 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100370 XC_RESULT_CONSTANT,
371 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600372} BcResultType;
373
374typedef union BcResultData {
375 BcNum n;
376 BcVec v;
377 BcId id;
378} BcResultData;
379
380typedef struct BcResult {
381 BcResultType t;
382 BcResultData d;
383} BcResult;
384
385typedef struct BcInstPtr {
386 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100387 size_t inst_idx;
388 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600389} BcInstPtr;
390
Gavin Howard01055ba2018-11-03 11:00:21 -0600391typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100392 XC_LEX_EOF,
393 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600394
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100395 XC_LEX_NLINE,
396 XC_LEX_WHITESPACE,
397 XC_LEX_STR,
398 XC_LEX_NAME,
399 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100400
Denys Vlasenko69560f42018-12-24 14:14:23 +0100401 XC_LEX_1st_op,
402 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100403
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100404 XC_LEX_OP_REL_EQ, // should
405 XC_LEX_OP_REL_LE, // match
406 XC_LEX_OP_REL_GE, // INST
407 XC_LEX_OP_REL_NE, // constants
408 XC_LEX_OP_REL_LT, // for
409 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100410
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100411 XC_LEX_OP_POWER, // operations
412 XC_LEX_OP_MULTIPLY, // |
413 XC_LEX_OP_DIVIDE, // |
414 XC_LEX_OP_MODULUS, // |
415 XC_LEX_OP_PLUS, // |
416 XC_LEX_OP_MINUS, // |
417 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100418#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100419 BC_LEX_OP_BOOL_NOT, // |
420 BC_LEX_OP_BOOL_OR, // |
421 BC_LEX_OP_BOOL_AND, // |
422
423 BC_LEX_OP_ASSIGN_POWER, // |
424 BC_LEX_OP_ASSIGN_MULTIPLY, // |
425 BC_LEX_OP_ASSIGN_DIVIDE, // |
426 BC_LEX_OP_ASSIGN_MODULUS, // |
427 BC_LEX_OP_ASSIGN_PLUS, // |
428 BC_LEX_OP_ASSIGN_MINUS, // |
429
430 BC_LEX_OP_ASSIGN, // V
431
Gavin Howard01055ba2018-11-03 11:00:21 -0600432 BC_LEX_OP_INC,
433 BC_LEX_OP_DEC,
434
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100435 BC_LEX_LPAREN, // () are 0x28 and 0x29
436 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600437
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100438 BC_LEX_LBRACKET, // [] are 0x5B and 5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600439 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100440 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600441
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100442 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600443 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100444 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600445
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100446 BC_LEX_KEY_1st_keyword,
447 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600448 BC_LEX_KEY_BREAK,
449 BC_LEX_KEY_CONTINUE,
450 BC_LEX_KEY_DEFINE,
451 BC_LEX_KEY_ELSE,
452 BC_LEX_KEY_FOR,
453 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100454 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
455 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
456 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600457 BC_LEX_KEY_IF,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100458 IF_BC(BC_LEX_KEY_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600459 BC_LEX_KEY_LENGTH,
460 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600461 BC_LEX_KEY_PRINT,
462 BC_LEX_KEY_QUIT,
463 BC_LEX_KEY_READ,
464 BC_LEX_KEY_RETURN,
465 BC_LEX_KEY_SCALE,
466 BC_LEX_KEY_SQRT,
467 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100468#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600469
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100470#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100471 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
472 DC_LEX_OP_ASSIGN,
473
474 DC_LEX_LPAREN,
475 DC_LEX_SCOLON,
476 DC_LEX_READ,
477 DC_LEX_IBASE,
478 DC_LEX_SCALE,
479 DC_LEX_OBASE,
480 DC_LEX_LENGTH,
481 DC_LEX_PRINT,
482 DC_LEX_QUIT,
483 DC_LEX_SQRT,
484 DC_LEX_LBRACE,
485
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100486 DC_LEX_EQ_NO_REG,
487 DC_LEX_OP_MODEXP,
488 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600489
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100490 DC_LEX_COLON,
491 DC_LEX_ELSE,
492 DC_LEX_EXECUTE,
493 DC_LEX_PRINT_STACK,
494 DC_LEX_CLEAR_STACK,
495 DC_LEX_STACK_LEVEL,
496 DC_LEX_DUPLICATE,
497 DC_LEX_SWAP,
498 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600499
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100500 DC_LEX_ASCIIFY,
501 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600502
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100503 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
504 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
505 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
506 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
507 DC_LEX_LOAD,
508 DC_LEX_LOAD_POP,
509 DC_LEX_STORE_PUSH,
510 DC_LEX_PRINT_POP,
511 DC_LEX_NQUIT,
512 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100513#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600514} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100515// must match order of BC_LEX_KEY_foo etc above
516#if ENABLE_BC
517struct BcLexKeyword {
518 char name8[8];
519};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100520#define BC_LEX_KW_ENTRY(a, b) \
521 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100522static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100523 BC_LEX_KW_ENTRY("auto" , 1), // 0
524 BC_LEX_KW_ENTRY("break" , 1), // 1
525 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
526 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100527 BC_LEX_KW_ENTRY("else" , 0), // 4
528 BC_LEX_KW_ENTRY("for" , 1), // 5
529 BC_LEX_KW_ENTRY("halt" , 0), // 6
530 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100531 BC_LEX_KW_ENTRY("obase" , 1), // 8
532 BC_LEX_KW_ENTRY("if" , 1), // 9
533 BC_LEX_KW_ENTRY("last" , 0), // 10
534 BC_LEX_KW_ENTRY("length" , 1), // 11
535 BC_LEX_KW_ENTRY("limits" , 0), // 12
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100536 BC_LEX_KW_ENTRY("print" , 0), // 13
537 BC_LEX_KW_ENTRY("quit" , 1), // 14
538 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100539 BC_LEX_KW_ENTRY("return" , 1), // 16
540 BC_LEX_KW_ENTRY("scale" , 1), // 17
541 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
542 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100543};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100544#undef BC_LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100545#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100546#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100547#define STRING_if (bc_lex_kws[9].name8)
548#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100549enum {
550 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100551 | (1 << 0) // 0
552 | (1 << 1) // 1
553 | (0 << 2) // 2
554 | (1 << 3) // 3
555 | (0 << 4) // 4
556 | (1 << 5) // 5
557 | (0 << 6) // 6
558 | (1 << 7) // 7
559 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100560 | (1 << 9) // 9
561 | (0 << 10) // 10
562 | (1 << 11) // 11
563 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100564 | (0 << 13) // 13
565 | (1 << 14) // 14
566 | (0 << 15) // 15
567 | (1 << 16) // 16
568 | (1 << 17) // 17
569 | (1 << 18) // 18
570 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100571};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100572#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100573
574// This is a bit array that corresponds to token types. An entry is
575// true if the token is valid in an expression, false otherwise.
576// Used to figure out when expr parsing should stop *without error message*
577// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100578// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100579// by later processing.
580enum {
581#define EXBITS(a,b,c,d,e,f,g,h) \
582 ((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 +0100583 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100584 + (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 +0100585 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
586 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100587 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
588 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100589 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
590 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
591 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100592#undef EXBITS
593};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100594static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100595{
596#if ULONG_MAX > 0xffffffff
597 // 64-bit version (will not work correctly for 32-bit longs!)
598 return BC_PARSE_EXPRS_BITS & (1UL << i);
599#else
600 // 32-bit version
601 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
602 if (i >= 32) {
603 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
604 i &= 31;
605 }
606 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100607#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100608}
609
610// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100611// [XC_LEX_1st_op...] token types.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100612static const uint8_t bc_parse_ops[] = {
613#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100614 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100615 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 +0100616 OP(2, false), // pow
617 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
618 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100619 OP(1, false), // not
620 OP(7, true ), OP( 7, true ), // or and
621 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
622 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100623 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100624#undef OP
625};
626#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
627#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
628#endif // ENABLE_BC
629
630#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100631static const //BcLexType - should be this type
632uint8_t
633dc_char_to_LEX[] = {
634 /* %&'( */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100635 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100636 /* )*+, */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100637 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100638 /* -./ */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100639 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100640 /* 0123456789 */
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, XC_LEX_INVALID, XC_LEX_INVALID,
643 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100644 /* :;<=>?@ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100645 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
646 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100647 /* ABCDEFGH */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100648 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
649 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100650 /* IJKLMNOP */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100651 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
652 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100653 /* QRSTUVWXY */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100654 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
655 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100656 /* Z[\] */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100657 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100658 /* ^_` */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100659 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100660 /* abcdefgh */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100661 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
662 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100663 /* ijklmnop */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100664 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100665 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100666 /* qrstuvwx */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100667 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
668 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100669 /* yz */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100670 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100671 /* {|}~ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100672 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100673};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100674static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100675int8_t
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100676dc_LEX_to_INST[] = { // starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
677 XC_INST_POWER, XC_INST_MULTIPLY, // OP_POWER OP_MULTIPLY
678 XC_INST_DIVIDE, XC_INST_MODULUS, // OP_DIVIDE OP_MODULUS
679 XC_INST_PLUS, XC_INST_MINUS, // OP_PLUS OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100680 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
681 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
682 XC_INST_REL_GT, // DC_LEX_LPAREN
683 DC_INST_INVALID, // DC_LEX_SCOLON
684 DC_INST_INVALID, // DC_LEX_READ
685 XC_INST_IBASE, // DC_LEX_IBASE
686 XC_INST_SCALE, // DC_LEX_SCALE
687 XC_INST_OBASE, // DC_LEX_OBASE
688 XC_INST_LENGTH, // DC_LEX_LENGTH
689 XC_INST_PRINT, // DC_LEX_PRINT
690 DC_INST_QUIT, // DC_LEX_QUIT
691 XC_INST_SQRT, // DC_LEX_SQRT
692 XC_INST_REL_GE, // DC_LEX_LBRACE
693 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
694 DC_INST_MODEXP, DC_INST_DIVMOD, // OP_MODEXP OP_DIVMOD
695 DC_INST_INVALID, DC_INST_INVALID, // COLON ELSE
696 DC_INST_EXECUTE, // EXECUTE
697 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // PRINT_STACK CLEAR_STACK
698 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // STACK_LEVEL DUPLICATE
699 DC_INST_SWAP, XC_INST_POP, // SWAP POP
700 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // ASCIIFY PRINT_STREAM
701 DC_INST_INVALID, DC_INST_INVALID, // STORE_IBASE STORE_OBASE
702 DC_INST_INVALID, DC_INST_INVALID, // STORE_SCALE LOAD
703 DC_INST_INVALID, DC_INST_INVALID, // LOAD_POP STORE_PUSH
704 XC_INST_PRINT, DC_INST_NQUIT, // PRINT_POP NQUIT
705 XC_INST_SCALE_FUNC, // SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100706 // DC_INST_INVALID in this table either means that corresponding LEX
707 // is not possible for dc, or that it does not compile one-to-one
708 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100709};
710#endif // ENABLE_DC
711
Gavin Howard01055ba2018-11-03 11:00:21 -0600712typedef struct BcLex {
Gavin Howard01055ba2018-11-03 11:00:21 -0600713 const char *buf;
714 size_t i;
715 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600716 size_t len;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +0100717 bool newline;
718 smallint lex; // was BcLexType
719 smallint lex_last; // was BcLexType
720 BcVec lex_buf;
Gavin Howard01055ba2018-11-03 11:00:21 -0600721} BcLex;
722
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100723#define BC_PARSE_STREND (0xff)
Gavin Howard01055ba2018-11-03 11:00:21 -0600724
Denys Vlasenko39287e02018-12-22 02:23:08 +0100725#if ENABLE_BC
726# define BC_PARSE_REL (1 << 0)
727# define BC_PARSE_PRINT (1 << 1)
728# define BC_PARSE_ARRAY (1 << 2)
729# define BC_PARSE_NOCALL (1 << 3)
730#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600731
Gavin Howard01055ba2018-11-03 11:00:21 -0600732typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600733 BcLex l;
734
Denys Vlasenko503faf92018-12-20 16:24:18 +0100735 IF_BC(BcVec exits;)
736 IF_BC(BcVec conds;)
737 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600738
Gavin Howard01055ba2018-11-03 11:00:21 -0600739 BcFunc *func;
740 size_t fidx;
741
Denys Vlasenko503faf92018-12-20 16:24:18 +0100742 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600743} BcParse;
744
Gavin Howard01055ba2018-11-03 11:00:21 -0600745typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600746 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100747 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600748
Denys Vlasenko503faf92018-12-20 16:24:18 +0100749 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600750 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600751 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600752
Gavin Howard01055ba2018-11-03 11:00:21 -0600753 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100754 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600755
756 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100757 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600758
759 BcVec vars;
760 BcVec var_map;
761
762 BcVec arrs;
763 BcVec arr_map;
764
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100765 IF_DC(BcVec strs;)
766 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600767
768 const char *file;
769
Gavin Howard01055ba2018-11-03 11:00:21 -0600770 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100771 IF_BC(BcNum one;)
772 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600773} BcProgram;
774
Gavin Howard01055ba2018-11-03 11:00:21 -0600775#define BC_PROG_MAIN (0)
776#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100777#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600778#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100779#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600780
781#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
782#define BC_PROG_NUM(r, n) \
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100783 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
Gavin Howard01055ba2018-11-03 11:00:21 -0600784
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100785#define BC_FLAG_W (1 << 0)
786#define BC_FLAG_V (1 << 1)
787#define BC_FLAG_S (1 << 2)
788#define BC_FLAG_Q (1 << 3)
789#define BC_FLAG_L (1 << 4)
790#define BC_FLAG_I (1 << 5)
791#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600792
793#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
794#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
795
Denys Vlasenko64074a12018-12-07 15:50:14 +0100796#define BC_MAX_OBASE ((unsigned) 999)
797#define BC_MAX_DIM ((unsigned) INT_MAX)
798#define BC_MAX_SCALE ((unsigned) UINT_MAX)
799#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
800#define BC_MAX_NUM BC_MAX_STRING
801// Unused apart from "limits" message. Just show a "biggish number" there.
802//#define BC_MAX_NAME BC_MAX_STRING
803//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
804//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
805#define BC_MAX_NAME_STR "999999999"
806#define BC_MAX_EXP_STR "999999999"
807#define BC_MAX_VARS_STR "999999999"
808
809#define BC_MAX_OBASE_STR "999"
810
811#if INT_MAX == 2147483647
812# define BC_MAX_DIM_STR "2147483647"
813#elif INT_MAX == 9223372036854775807
814# define BC_MAX_DIM_STR "9223372036854775807"
815#else
816# error Strange INT_MAX
817#endif
818
819#if UINT_MAX == 4294967295
820# define BC_MAX_SCALE_STR "4294967295"
821# define BC_MAX_STRING_STR "4294967294"
822#elif UINT_MAX == 18446744073709551615
823# define BC_MAX_SCALE_STR "18446744073709551615"
824# define BC_MAX_STRING_STR "18446744073709551614"
825#else
826# error Strange UINT_MAX
827#endif
828#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600829
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100830struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100831 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100832 IF_FEATURE_CLEAN_UP(smallint exiting;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600833
834 BcParse prs;
835 BcProgram prog;
836
Denys Vlasenko5318f812018-12-05 17:48:01 +0100837 // For error messages. Can be set to current parsed line,
838 // or [TODO] to current executing line (can be before last parsed one)
839 unsigned err_line;
840
Gavin Howard01055ba2018-11-03 11:00:21 -0600841 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100842 BcVec input_buffer;
843 FILE *input_fp;
Gavin Howard01055ba2018-11-03 11:00:21 -0600844
845 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100846
847#if ENABLE_FEATURE_EDITING
848 line_input_t *line_input_state;
849#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100850} FIX_ALIASING;
851#define G (*ptr_to_globals)
852#define INIT_G() do { \
853 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
854} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100855#define FREE_G() do { \
856 FREE_PTR_TO_GLOBALS(); \
857} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100858#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
859#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100860#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100861#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100862# define G_interrupt bb_got_signal
863# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100864#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100865# define G_interrupt 0
866# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100867#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100868#if ENABLE_FEATURE_CLEAN_UP
869# define G_exiting G.exiting
870#else
871# define G_exiting 0
872#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100873#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100874#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100875
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100876// In configurations where errors abort instead of propagating error
877// return code up the call chain, functions returning BC_STATUS
878// actually don't return anything, they always succeed and return "void".
879// A macro wrapper is provided, which makes this statement work:
880// s = zbc_func(...)
881// and makes it visible to the compiler that s is always zero,
882// allowing compiler to optimize dead code after the statement.
883//
884// To make code more readable, each such function has a "z"
885// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
886//
887#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100888# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100889# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100890# define IF_ERROR_RETURN_POSSIBLE(a) a
891# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100892# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100893# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100894#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100895# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100896# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100897# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
898# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100899# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100900# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100901#endif
902
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100903//
904// Utility routines
905//
Gavin Howard01055ba2018-11-03 11:00:21 -0600906
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100907static void fflush_and_check(void)
908{
909 fflush_all();
910 if (ferror(stdout) || ferror(stderr))
911 bb_perror_msg_and_die("output error");
912}
913
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100914#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100915#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100916do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100917 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100918 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100919 return BC_STATUS_FAILURE; \
920} while (0)
921#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100922#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100923#endif
924
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100925static void quit(void) NORETURN;
926static void quit(void)
927{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100928 if (ferror(stdin))
929 bb_perror_msg_and_die("input error");
930 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100931 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100932 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100933}
934
Denys Vlasenko5318f812018-12-05 17:48:01 +0100935static void bc_verror_msg(const char *fmt, va_list p)
936{
Denys Vlasenko82269122018-12-14 16:30:56 +0100937 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +0100938 if (G.prog.file) {
939 sv = applet_name;
940 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
941 }
942 bb_verror_msg(fmt, p, NULL);
943 if (G.prog.file) {
944 free((char*)applet_name);
945 applet_name = sv;
946 }
947}
948
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100949static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100950{
951 va_list p;
952
953 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100954 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100955 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100956
Denys Vlasenkoec603182018-12-17 10:34:02 +0100957 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
958 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
959 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100960}
961
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100962#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100963static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100964{
965 va_list p;
966
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100967 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100968 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100969 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100970
971 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100972 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100973 va_end(p);
974
975 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100976 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100977 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
978
Denys Vlasenkoec603182018-12-17 10:34:02 +0100979 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100980 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100981 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100982}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100983#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100984#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100985
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100986// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
987// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100988// function must not have caller-cleaned parameters on stack.
989// Unfortunately, vararg function API does exactly that on most arches.
990// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100991static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100992{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100993 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100994}
995static ERRORFUNC int bc_error_bad_character(char c)
996{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100997 if (!c)
998 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100999 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001000}
1001static ERRORFUNC int bc_error_bad_expression(void)
1002{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001003 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001004}
1005static ERRORFUNC int bc_error_bad_token(void)
1006{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001007 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001008}
1009static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1010{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001011 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001012}
1013static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1014{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001015 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001016}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001017#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001018static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001019{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001020 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001021}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001022#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1023static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001024{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001025 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001026}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001027#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1028static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001029{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001030 RETURN_STATUS(zbc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001031}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001032#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)
1033static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001034{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001035 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001036}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001037#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 +01001038#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001039
Gavin Howard01055ba2018-11-03 11:00:21 -06001040static void bc_vec_grow(BcVec *v, size_t n)
1041{
1042 size_t cap = v->cap * 2;
1043 while (cap < v->len + n) cap *= 2;
1044 v->v = xrealloc(v->v, v->size * cap);
1045 v->cap = cap;
1046}
1047
1048static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1049{
1050 v->size = esize;
1051 v->cap = BC_VEC_START_CAP;
1052 v->len = 0;
1053 v->dtor = dtor;
1054 v->v = xmalloc(esize * BC_VEC_START_CAP);
1055}
1056
Denys Vlasenko7d628012018-12-04 21:46:47 +01001057static void bc_char_vec_init(BcVec *v)
1058{
1059 bc_vec_init(v, sizeof(char), NULL);
1060}
1061
Gavin Howard01055ba2018-11-03 11:00:21 -06001062static void bc_vec_expand(BcVec *v, size_t req)
1063{
1064 if (v->cap < req) {
1065 v->v = xrealloc(v->v, v->size * req);
1066 v->cap = req;
1067 }
1068}
1069
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001070static void bc_vec_pop(BcVec *v)
1071{
1072 v->len--;
1073 if (v->dtor)
1074 v->dtor(v->v + (v->size * v->len));
1075}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001076
Gavin Howard01055ba2018-11-03 11:00:21 -06001077static void bc_vec_npop(BcVec *v, size_t n)
1078{
1079 if (!v->dtor)
1080 v->len -= n;
1081 else {
1082 size_t len = v->len - n;
1083 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1084 }
1085}
1086
Denys Vlasenko7d628012018-12-04 21:46:47 +01001087static void bc_vec_pop_all(BcVec *v)
1088{
1089 bc_vec_npop(v, v->len);
1090}
1091
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001092static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001093{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001094 size_t len = v->len;
1095 if (len >= v->cap) bc_vec_grow(v, 1);
1096 memmove(v->v + (v->size * len), data, v->size);
1097 v->len++;
1098 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001099}
1100
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001101// G.prog.results often needs "pop old operand, push result" idiom.
1102// Can do this without a few extra ops
1103static size_t bc_result_pop_and_push(const void *data)
1104{
1105 BcVec *v = &G.prog.results;
1106 char *last;
1107 size_t len = v->len - 1;
1108
1109 last = v->v + (v->size * len);
1110 if (v->dtor)
1111 v->dtor(last);
1112 memmove(last, data, v->size);
1113 return len;
1114}
1115
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001116static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001117{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001118 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001119}
1120
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001121static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001122{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001123 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001124 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001125 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001126}
1127
Gavin Howard01055ba2018-11-03 11:00:21 -06001128static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1129{
1130 if (idx == v->len)
1131 bc_vec_push(v, data);
1132 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001133 char *ptr;
1134
1135 if (v->len == v->cap) bc_vec_grow(v, 1);
1136
1137 ptr = v->v + v->size * idx;
1138
1139 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1140 memmove(ptr, data, v->size);
1141 }
1142}
1143
1144static void bc_vec_string(BcVec *v, size_t len, const char *str)
1145{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001146 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001147 bc_vec_expand(v, len + 1);
1148 memcpy(v->v, str, len);
1149 v->len = len;
1150
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001151 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001152}
1153
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001154#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001155static void bc_vec_concat(BcVec *v, const char *str)
1156{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001157 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001158
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001159 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001160
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001161 slen = strlen(str);
1162 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001163
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001164 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001165 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001166
1167 v->len = len;
1168}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001169#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001170
1171static void *bc_vec_item(const BcVec *v, size_t idx)
1172{
1173 return v->v + v->size * idx;
1174}
1175
1176static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1177{
1178 return v->v + v->size * (v->len - idx - 1);
1179}
1180
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001181static void *bc_vec_top(const BcVec *v)
1182{
1183 return v->v + v->size * (v->len - 1);
1184}
1185
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001186static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001187{
1188 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001189 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001190 free(v->v);
1191}
1192
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001193static BcFunc* bc_program_func(size_t idx)
1194{
1195 return bc_vec_item(&G.prog.fns, idx);
1196}
1197// BC_PROG_MAIN is zeroth element, so:
1198#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1199
1200#if ENABLE_BC
1201static BcFunc* bc_program_current_func(void)
1202{
1203 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1204 BcFunc *func = bc_program_func(ip->func);
1205 return func;
1206}
1207#endif
1208
1209static char** bc_program_str(size_t idx)
1210{
1211#if ENABLE_BC
1212 if (IS_BC) {
1213 BcFunc *func = bc_program_current_func();
1214 return bc_vec_item(&func->strs, idx);
1215 }
1216#endif
1217 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1218}
1219
1220static char** bc_program_const(size_t idx)
1221{
1222#if ENABLE_BC
1223 if (IS_BC) {
1224 BcFunc *func = bc_program_current_func();
1225 return bc_vec_item(&func->consts, idx);
1226 }
1227#endif
1228 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1229}
1230
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001231static int bc_id_cmp(const void *e1, const void *e2)
1232{
1233 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1234}
1235
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001236static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001237{
1238 free(((BcId *) id)->name);
1239}
1240
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001241static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001242{
1243 size_t low = 0, high = v->len;
1244
1245 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001246 size_t mid = (low + high) / 2;
1247 BcId *id = bc_vec_item(v, mid);
1248 int result = bc_id_cmp(ptr, id);
1249
1250 if (result == 0)
1251 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001252 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001253 high = mid;
1254 else
1255 low = mid + 1;
1256 }
1257
1258 return low;
1259}
1260
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001261static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001262{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001263 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001264
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001265 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001266 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001267 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1268 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001269 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001270 bc_vec_pushAt(v, ptr, n);
1271 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001272}
1273
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001274#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001275static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001276{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001277 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001278 if (i >= v->len) return BC_VEC_INVALID_IDX;
1279 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1280}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001281#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001282
Gavin Howard01055ba2018-11-03 11:00:21 -06001283static void bc_num_setToZero(BcNum *n, size_t scale)
1284{
1285 n->len = 0;
1286 n->neg = false;
1287 n->rdx = scale;
1288}
1289
1290static void bc_num_zero(BcNum *n)
1291{
1292 bc_num_setToZero(n, 0);
1293}
1294
1295static void bc_num_one(BcNum *n)
1296{
1297 bc_num_setToZero(n, 0);
1298 n->len = 1;
1299 n->num[0] = 1;
1300}
1301
Denys Vlasenko3129f702018-12-09 12:04:44 +01001302// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001303static void bc_num_init(BcNum *n, size_t req)
1304{
1305 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001306 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001307 n->num = xmalloc(req);
1308 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001309 n->rdx = 0;
1310 n->len = 0;
1311 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001312}
1313
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001314static void bc_num_init_DEF_SIZE(BcNum *n)
1315{
1316 bc_num_init(n, BC_NUM_DEF_SIZE);
1317}
1318
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001319static void bc_num_expand(BcNum *n, size_t req)
1320{
1321 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1322 if (req > n->cap) {
1323 n->num = xrealloc(n->num, req);
1324 n->cap = req;
1325 }
1326}
1327
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001328static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001329{
1330 free(((BcNum *) num)->num);
1331}
1332
1333static void bc_num_copy(BcNum *d, BcNum *s)
1334{
1335 if (d != s) {
1336 bc_num_expand(d, s->cap);
1337 d->len = s->len;
1338 d->neg = s->neg;
1339 d->rdx = s->rdx;
1340 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1341 }
1342}
1343
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001344static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001345{
1346 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001347 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001348
Denys Vlasenko1557b762018-12-22 21:37:46 +01001349 result = 0;
1350 i = n->len;
1351 while (i > n->rdx) {
1352 unsigned long prev = result;
1353 result = result * 10 + n->num[--i];
1354 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1355 // 0x1ff00000 * 10 is 0x13f600000,
1356 // or 0x3f600000 truncated to 32 bits. Which is larger.
1357 // However, (N*10)/8 < N check is always correct.
1358 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001359 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001360 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001361 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001362
Denys Vlasenko29301232018-12-11 15:29:32 +01001363 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001364}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001365#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1366
1367static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1368{
1369 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1370
1371 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1372}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001373#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001374
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001375#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1376# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1377#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1378# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1379#endif
1380// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1381// would not hit realloc() code path - not good if num[] is not malloced
1382
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001383static void bc_num_ulong2num(BcNum *n, unsigned long val)
1384{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001385 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001386
1387 bc_num_zero(n);
1388
1389 if (val == 0) return;
1390
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001391 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001392
1393 ptr = n->num;
1394 for (;;) {
1395 n->len++;
1396 *ptr++ = val % 10;
1397 val /= 10;
1398 if (val == 0) break;
1399 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001400}
1401
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001402static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001403{
1404 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001405 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001406 a[i] -= b[i];
1407 for (j = i; a[j] < 0;) {
1408 a[j++] += 10;
1409 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001410 }
1411 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001412}
1413
1414static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1415{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001416 size_t i = len;
1417 for (;;) {
1418 int c;
1419 if (i == 0)
1420 return 0;
1421 i--;
1422 c = a[i] - b[i];
1423 if (c != 0) {
1424 i++;
1425 if (c < 0)
1426 return -i;
1427 return i;
1428 }
1429 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001430}
1431
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001432#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1433#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1434#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1435//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1436static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1437{
1438 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1439}
1440//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1441static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1442{
1443 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1444}
1445
Gavin Howard01055ba2018-11-03 11:00:21 -06001446static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1447{
1448 size_t i, min, a_int, b_int, diff;
1449 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001450 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001451 ssize_t cmp;
1452
1453 if (a == b) return 0;
1454 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1455 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001456
1457 if (a->neg != b->neg) // signs of a and b differ
1458 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1459 return (int)b->neg - (int)a->neg;
1460 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001461
1462 a_int = BC_NUM_INT(a);
1463 b_int = BC_NUM_INT(b);
1464 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001465
1466 if (a_int != 0) return (ssize_t) a_int;
1467
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001468 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001469 if (a_max) {
1470 min = b->rdx;
1471 diff = a->rdx - b->rdx;
1472 max_num = a->num + diff;
1473 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001474 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1475 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001476 min = a->rdx;
1477 diff = b->rdx - a->rdx;
1478 max_num = b->num + diff;
1479 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001480 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001481 }
1482
1483 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001484 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001485
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001486 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001487 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001488 }
1489
1490 return 0;
1491}
1492
1493static void bc_num_truncate(BcNum *n, size_t places)
1494{
1495 if (places == 0) return;
1496
1497 n->rdx -= places;
1498
1499 if (n->len != 0) {
1500 n->len -= places;
1501 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1502 }
1503}
1504
1505static void bc_num_extend(BcNum *n, size_t places)
1506{
1507 size_t len = n->len + places;
1508
1509 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001510 if (n->cap < len) bc_num_expand(n, len);
1511
1512 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1513 memset(n->num, 0, sizeof(BcDig) * places);
1514
1515 n->len += places;
1516 n->rdx += places;
1517 }
1518}
1519
1520static void bc_num_clean(BcNum *n)
1521{
1522 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1523 if (n->len == 0)
1524 n->neg = false;
1525 else if (n->len < n->rdx)
1526 n->len = n->rdx;
1527}
1528
1529static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1530{
1531 if (n->rdx < scale)
1532 bc_num_extend(n, scale - n->rdx);
1533 else
1534 bc_num_truncate(n, n->rdx - scale);
1535
1536 bc_num_clean(n);
1537 if (n->len != 0) n->neg = !neg1 != !neg2;
1538}
1539
1540static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1541 BcNum *restrict b)
1542{
1543 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001544 b->len = n->len - idx;
1545 a->len = idx;
1546 a->rdx = b->rdx = 0;
1547
1548 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1549 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001550 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001551 bc_num_zero(b);
1552 bc_num_copy(a, n);
1553 }
1554
1555 bc_num_clean(a);
1556 bc_num_clean(b);
1557}
1558
Denys Vlasenko29301232018-12-11 15:29:32 +01001559static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001560{
Denys Vlasenko29301232018-12-11 15:29:32 +01001561 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001562
1563 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1564 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1565 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001566 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001567 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001568
1569 if (n->rdx >= places)
1570 n->rdx -= places;
1571 else {
1572 bc_num_extend(n, places - n->rdx);
1573 n->rdx = 0;
1574 }
1575
1576 bc_num_clean(n);
1577
Denys Vlasenko29301232018-12-11 15:29:32 +01001578 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001579}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001580#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001581
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001582typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1583
1584static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1585 BcNumBinaryOp op, size_t req)
1586{
1587 BcStatus s;
1588 BcNum num2, *ptr_a, *ptr_b;
1589 bool init = false;
1590
1591 if (c == a) {
1592 ptr_a = &num2;
1593 memcpy(ptr_a, c, sizeof(BcNum));
1594 init = true;
1595 } else
1596 ptr_a = a;
1597
1598 if (c == b) {
1599 ptr_b = &num2;
1600 if (c != a) {
1601 memcpy(ptr_b, c, sizeof(BcNum));
1602 init = true;
1603 }
1604 } else
1605 ptr_b = b;
1606
1607 if (init)
1608 bc_num_init(c, req);
1609 else
1610 bc_num_expand(c, req);
1611
1612 s = BC_STATUS_SUCCESS;
1613 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1614
1615 if (init) bc_num_free(&num2);
1616
1617 RETURN_STATUS(s);
1618}
1619#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1620
1621static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1622static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1623static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1624static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1625static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1626static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1627
1628static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1629{
1630 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1631 (void) scale;
1632 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1633}
1634
1635static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1636{
1637 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1638 (void) scale;
1639 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1640}
1641
1642static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1643{
1644 size_t req = BC_NUM_MREQ(a, b, scale);
1645 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1646}
1647
1648static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1649{
1650 size_t req = BC_NUM_MREQ(a, b, scale);
1651 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1652}
1653
1654static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1655{
1656 size_t req = BC_NUM_MREQ(a, b, scale);
1657 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1658}
1659
1660static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1661{
1662 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1663}
1664
1665static const BcNumBinaryOp zbc_program_ops[] = {
1666 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1667};
1668#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1669#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1670#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1671#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1672#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1673#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1674
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001675static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001676{
1677 BcNum one;
1678 BcDig num[2];
1679
1680 one.cap = 2;
1681 one.num = num;
1682 bc_num_one(&one);
1683
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001684 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001685}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001686#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001687
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001688static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001689{
1690 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1691 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001692 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001693
1694 // Because this function doesn't need to use scale (per the bc spec),
1695 // I am hijacking it to say whether it's doing an add or a subtract.
1696
1697 if (a->len == 0) {
1698 bc_num_copy(c, b);
1699 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001700 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001701 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001702 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001703 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001704 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001705 }
1706
1707 c->neg = a->neg;
1708 c->rdx = BC_MAX(a->rdx, b->rdx);
1709 min_rdx = BC_MIN(a->rdx, b->rdx);
1710 c->len = 0;
1711
1712 if (a->rdx > b->rdx) {
1713 diff = a->rdx - b->rdx;
1714 ptr = a->num;
1715 ptr_a = a->num + diff;
1716 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001717 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001718 diff = b->rdx - a->rdx;
1719 ptr = b->num;
1720 ptr_a = a->num;
1721 ptr_b = b->num + diff;
1722 }
1723
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001724 ptr_c = c->num;
1725 for (i = 0; i < diff; ++i, ++c->len)
1726 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001727
1728 ptr_c += diff;
1729 a_int = BC_NUM_INT(a);
1730 b_int = BC_NUM_INT(b);
1731
1732 if (a_int > b_int) {
1733 min_int = b_int;
1734 max = a_int;
1735 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001736 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001737 min_int = a_int;
1738 max = b_int;
1739 ptr = ptr_b;
1740 }
1741
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001742 carry = 0;
1743 for (i = 0; i < min_rdx + min_int; ++i) {
1744 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001745 carry = in / 10;
1746 ptr_c[i] = (BcDig)(in % 10);
1747 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001748 for (; i < max + min_rdx; ++i) {
1749 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001750 carry = in / 10;
1751 ptr_c[i] = (BcDig)(in % 10);
1752 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001753 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001754
1755 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1756
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001757 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001758}
1759
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001760static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001761{
Gavin Howard01055ba2018-11-03 11:00:21 -06001762 ssize_t cmp;
1763 BcNum *minuend, *subtrahend;
1764 size_t start;
1765 bool aneg, bneg, neg;
1766
1767 // Because this function doesn't need to use scale (per the bc spec),
1768 // I am hijacking it to say whether it's doing an add or a subtract.
1769
1770 if (a->len == 0) {
1771 bc_num_copy(c, b);
1772 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001773 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001774 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001775 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001776 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001777 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001778 }
1779
1780 aneg = a->neg;
1781 bneg = b->neg;
1782 a->neg = b->neg = false;
1783
1784 cmp = bc_num_cmp(a, b);
1785
1786 a->neg = aneg;
1787 b->neg = bneg;
1788
1789 if (cmp == 0) {
1790 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001791 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001792 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001793 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001794 neg = a->neg;
1795 minuend = a;
1796 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001797 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001798 neg = b->neg;
1799 if (sub) neg = !neg;
1800 minuend = b;
1801 subtrahend = a;
1802 }
1803
1804 bc_num_copy(c, minuend);
1805 c->neg = neg;
1806
1807 if (c->rdx < subtrahend->rdx) {
1808 bc_num_extend(c, subtrahend->rdx - c->rdx);
1809 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001810 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001811 start = c->rdx - subtrahend->rdx;
1812
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001813 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001814
1815 bc_num_clean(c);
1816
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001817 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001818}
1819
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001820static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001821 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001822#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001823{
1824 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001825 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001826 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001827 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001828
Gavin Howard01055ba2018-11-03 11:00:21 -06001829 if (a->len == 0 || b->len == 0) {
1830 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001831 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001832 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001833 aone = BC_NUM_ONE(a);
1834 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001835 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001836 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001837 }
1838
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001839 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1840 || a->len < BC_NUM_KARATSUBA_LEN
1841 || b->len < BC_NUM_KARATSUBA_LEN
1842 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001843 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001844
Gavin Howard01055ba2018-11-03 11:00:21 -06001845 bc_num_expand(c, a->len + b->len + 1);
1846
1847 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001848 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001849
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001850 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001851 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001852 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001853 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001854 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001855 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001856 carry = in / 10;
1857 c->num[i + j] = (BcDig)(in % 10);
1858 }
1859
1860 c->num[i + j] += (BcDig) carry;
1861 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001862
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001863#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001864 // a=2^1000000
1865 // a*a <- without check below, this will not be interruptible
1866 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001867#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001868 }
1869
1870 c->len = len;
1871
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001872 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001873 }
1874
1875 bc_num_init(&l1, max);
1876 bc_num_init(&h1, max);
1877 bc_num_init(&l2, max);
1878 bc_num_init(&h2, max);
1879 bc_num_init(&m1, max);
1880 bc_num_init(&m2, max);
1881 bc_num_init(&z0, max);
1882 bc_num_init(&z1, max);
1883 bc_num_init(&z2, max);
1884 bc_num_init(&temp, max + max);
1885
1886 bc_num_split(a, max2, &l1, &h1);
1887 bc_num_split(b, max2, &l2, &h2);
1888
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001889 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001890 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001891 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001892 if (s) goto err;
1893
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001894 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001895 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001896 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001897 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001898 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001899 if (s) goto err;
1900
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001901 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001902 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001903 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001904 if (s) goto err;
1905
Denys Vlasenko29301232018-12-11 15:29:32 +01001906 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001907 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001908 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001909 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001910 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001911 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001912 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001913 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001914 bc_num_free(&temp);
1915 bc_num_free(&z2);
1916 bc_num_free(&z1);
1917 bc_num_free(&z0);
1918 bc_num_free(&m2);
1919 bc_num_free(&m1);
1920 bc_num_free(&h2);
1921 bc_num_free(&l2);
1922 bc_num_free(&h1);
1923 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001924 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001925}
1926
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001927static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001928{
1929 BcStatus s;
1930 BcNum cpa, cpb;
1931 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1932
1933 scale = BC_MAX(scale, a->rdx);
1934 scale = BC_MAX(scale, b->rdx);
1935 scale = BC_MIN(a->rdx + b->rdx, scale);
1936 maxrdx = BC_MAX(maxrdx, scale);
1937
1938 bc_num_init(&cpa, a->len);
1939 bc_num_init(&cpb, b->len);
1940
1941 bc_num_copy(&cpa, a);
1942 bc_num_copy(&cpb, b);
1943 cpa.neg = cpb.neg = false;
1944
Denys Vlasenko29301232018-12-11 15:29:32 +01001945 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001946 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001947 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001948 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001949 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001950 if (s) goto err;
1951
1952 maxrdx += scale;
1953 bc_num_expand(c, c->len + maxrdx);
1954
1955 if (c->len < maxrdx) {
1956 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1957 c->len += maxrdx;
1958 }
1959
1960 c->rdx = maxrdx;
1961 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001962 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001963 bc_num_free(&cpb);
1964 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001965 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001966}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001967#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001968
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001969static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001970{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001971 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001972 size_t len, end, i;
1973 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001974
1975 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001976 RETURN_STATUS(bc_error("divide by zero"));
1977 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001978 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001979 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001980 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001981 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001982 bc_num_copy(c, a);
1983 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001984 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001985 }
1986
1987 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1988 bc_num_copy(&cp, a);
1989 len = b->len;
1990
1991 if (len > cp.len) {
1992 bc_num_expand(&cp, len + 2);
1993 bc_num_extend(&cp, len - cp.len);
1994 }
1995
1996 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1997 cp.rdx -= b->rdx;
1998 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1999
2000 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01002001 for (;;) {
2002 if (len == 0) break;
2003 len--;
2004 if (b->num[len] != 0)
2005 break;
2006 }
2007 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002008 }
2009
2010 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2011
2012 // We want an extra zero in front to make things simpler.
2013 cp.num[cp.len++] = 0;
2014 end = cp.len - len;
2015
2016 bc_num_expand(c, cp.len);
2017
2018 bc_num_zero(c);
2019 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2020 c->rdx = cp.rdx;
2021 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002022
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002023 s = BC_STATUS_SUCCESS;
2024 for (i = end - 1; i < end; --i) {
2025 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002026 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002027 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2028 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002029 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002030#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002031 // a=2^100000
2032 // scale=40000
2033 // 1/a <- without check below, this will not be interruptible
2034 if (G_interrupt) {
2035 s = BC_STATUS_FAILURE;
2036 break;
2037 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002038#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002039 }
2040
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002041 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002042 bc_num_free(&cp);
2043
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002044 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002045}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002046#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002047
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002048static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002049 BcNum *restrict d, size_t scale, size_t ts)
2050{
2051 BcStatus s;
2052 BcNum temp;
2053 bool neg;
2054
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002055 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002056 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002057
2058 if (a->len == 0) {
2059 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002060 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002061 }
2062
2063 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002064 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002065 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002066
2067 if (scale != 0) scale = ts;
2068
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002069 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002070 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002071 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002072 if (s) goto err;
2073
2074 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2075
2076 neg = d->neg;
2077 bc_num_retireMul(d, ts, a->neg, b->neg);
2078 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002079 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002080 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002081 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002082}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002083#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002084
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002085static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002086{
2087 BcStatus s;
2088 BcNum c1;
2089 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2090
2091 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002092 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002093 bc_num_free(&c1);
2094
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002095 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002096}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002097#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002098
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002099static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002100{
2101 BcStatus s = BC_STATUS_SUCCESS;
2102 BcNum copy;
2103 unsigned long pow;
2104 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002105 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002106
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002107 // GNU bc does not allow 2^2.0 - we do
2108 for (i = 0; i < b->rdx; i++)
2109 if (b->num[i] != 0)
2110 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002111
2112 if (b->len == 0) {
2113 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002114 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002115 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002116 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002118 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002119 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002120 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002121 if (!b->neg)
2122 bc_num_copy(c, a);
2123 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002124 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002125 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002126 }
2127
2128 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002129 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002130 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002131 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002132
2133 bc_num_init(&copy, a->len);
2134 bc_num_copy(&copy, a);
2135
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002136 if (!neg) {
2137 if (a->rdx > scale)
2138 scale = a->rdx;
2139 if (a->rdx * pow < scale)
2140 scale = a->rdx * pow;
2141 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002142
Gavin Howard01055ba2018-11-03 11:00:21 -06002143
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002144 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002145 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002146 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002147 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002148 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002149 //if (G_interrupt) {
2150 // s = BC_STATUS_FAILURE;
2151 // goto err;
2152 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002153 }
2154
Gavin Howard01055ba2018-11-03 11:00:21 -06002155 bc_num_copy(c, &copy);
2156
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002157 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002158 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002159 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002160 if (s) goto err;
2161
2162 if (pow & 1) {
2163 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002164 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002165 if (s) goto err;
2166 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002167 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002168 //if (G_interrupt) {
2169 // s = BC_STATUS_FAILURE;
2170 // goto err;
2171 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002172 }
2173
2174 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002175 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002176 if (s) goto err;
2177 }
2178
Gavin Howard01055ba2018-11-03 11:00:21 -06002179 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2180
2181 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002182 for (i = 0; i < c->len; ++i)
2183 if (c->num[i] != 0)
2184 goto skip;
2185 bc_num_setToZero(c, scale);
2186 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002187
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002188 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002189 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002190 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002191}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002192#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002193
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002194static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002195{
2196 BcStatus s;
2197 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2198 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2199 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2200
2201 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2202 bc_num_expand(b, req);
2203
2204 if (a->len == 0) {
2205 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002206 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002207 }
2208 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002209 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002210 }
2211 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002212 bc_num_one(b);
2213 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002214 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002215 }
2216
2217 scale = BC_MAX(scale, a->rdx) + 1;
2218 len = a->len + scale;
2219
2220 bc_num_init(&num1, len);
2221 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002222 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002223
2224 bc_num_one(&half);
2225 half.num[0] = 5;
2226 half.rdx = 1;
2227
2228 bc_num_init(&f, len);
2229 bc_num_init(&fprime, len);
2230
2231 x0 = &num1;
2232 x1 = &num2;
2233
2234 bc_num_one(x0);
2235 pow = BC_NUM_INT(a);
2236
2237 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002238 if (pow & 1)
2239 x0->num[0] = 2;
2240 else
2241 x0->num[0] = 6;
2242
2243 pow -= 2 - (pow & 1);
2244
2245 bc_num_extend(x0, pow);
2246
2247 // Make sure to move the radix back.
2248 x0->rdx -= pow;
2249 }
2250
2251 x0->rdx = digs = digs1 = 0;
2252 resrdx = scale + 2;
2253 len = BC_NUM_INT(x0) + resrdx - 1;
2254
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002255 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002256 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002257 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002258 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002259 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002260 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002261 if (s) goto err;
2262
2263 cmp = bc_num_cmp(x1, x0);
2264 digs = x1->len - (unsigned long long) llabs(cmp);
2265
2266 if (cmp == cmp2 && digs == digs1)
2267 times += 1;
2268 else
2269 times = 0;
2270
2271 resrdx += times > 4;
2272
2273 cmp2 = cmp1;
2274 cmp1 = cmp;
2275 digs1 = digs;
2276
2277 temp = x0;
2278 x0 = x1;
2279 x1 = temp;
2280 }
2281
Gavin Howard01055ba2018-11-03 11:00:21 -06002282 bc_num_copy(b, x0);
2283 scale -= 1;
2284 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002285 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002286 bc_num_free(&fprime);
2287 bc_num_free(&f);
2288 bc_num_free(&half);
2289 bc_num_free(&num2);
2290 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002291 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002292}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002293#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002294
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002295static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002296 size_t scale)
2297{
2298 BcStatus s;
2299 BcNum num2, *ptr_a;
2300 bool init = false;
2301 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2302
2303 if (c == a) {
2304 memcpy(&num2, c, sizeof(BcNum));
2305 ptr_a = &num2;
2306 bc_num_init(c, len);
2307 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002308 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002309 ptr_a = a;
2310 bc_num_expand(c, len);
2311 }
2312
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002313 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002314
2315 if (init) bc_num_free(&num2);
2316
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002317 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002318}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002319#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002320
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002321#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002322static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002323{
2324 BcStatus s;
2325 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002326 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002327
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002328 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002329 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002330 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002331 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002332 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002333 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002334
2335 bc_num_expand(d, c->len);
2336 bc_num_init(&base, c->len);
2337 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002338 bc_num_init(&temp, b->len);
2339
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002340 two.cap = ARRAY_SIZE(two_digs);
2341 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002342 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002343 two_digs[0] = 2;
2344
Gavin Howard01055ba2018-11-03 11:00:21 -06002345 bc_num_one(d);
2346
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002347 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002348 if (s) goto err;
2349 bc_num_copy(&exp, b);
2350
2351 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002352 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002353 if (s) goto err;
2354
2355 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002356 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002357 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002358 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002359 if (s) goto err;
2360 }
2361
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002362 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002363 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002364 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002365 if (s) goto err;
2366 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002367 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002368 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002369 bc_num_free(&exp);
2370 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002371 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002372}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002373#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002374#endif // ENABLE_DC
2375
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002376static FAST_FUNC void bc_string_free(void *string)
2377{
2378 free(*(char**)string);
2379}
2380
Gavin Howard01055ba2018-11-03 11:00:21 -06002381static void bc_func_init(BcFunc *f)
2382{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002383 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002384 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2385 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002386 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2387 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002388 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002389}
2390
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002391static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002392{
2393 BcFunc *f = (BcFunc *) func;
2394 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002395 IF_BC(bc_vec_free(&f->labels);)
2396 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002397 IF_BC(bc_vec_free(&f->strs);)
2398 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002399}
2400
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002401static void bc_array_expand(BcVec *a, size_t len);
2402
Gavin Howard01055ba2018-11-03 11:00:21 -06002403static void bc_array_init(BcVec *a, bool nums)
2404{
2405 if (nums)
2406 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2407 else
2408 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2409 bc_array_expand(a, 1);
2410}
2411
Gavin Howard01055ba2018-11-03 11:00:21 -06002412static void bc_array_expand(BcVec *a, size_t len)
2413{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002414 if (a->dtor == bc_num_free
2415 // && a->size == sizeof(BcNum) - always true
2416 ) {
2417 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002418 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002419 bc_num_init_DEF_SIZE(&n);
2420 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002421 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002422 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002423 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002424 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002425 bc_array_init(&v, true);
2426 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002427 }
2428 }
2429}
2430
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002431static void bc_array_copy(BcVec *d, const BcVec *s)
2432{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002433 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002434 size_t i;
2435
2436 bc_vec_pop_all(d);
2437 bc_vec_expand(d, s->cap);
2438 d->len = s->len;
2439
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002440 dnum = (void*)d->v;
2441 snum = (void*)s->v;
2442 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002443 bc_num_init(dnum, snum->len);
2444 bc_num_copy(dnum, snum);
2445 }
2446}
2447
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002448#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002449static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002450{
2451 d->t = src->t;
2452
2453 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002454 case XC_RESULT_TEMP:
2455 case XC_RESULT_IBASE:
2456 case XC_RESULT_SCALE:
2457 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002458 bc_num_init(&d->d.n, src->d.n.len);
2459 bc_num_copy(&d->d.n, &src->d.n);
2460 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002461 case XC_RESULT_VAR:
2462 case XC_RESULT_ARRAY:
2463 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002464 d->d.id.name = xstrdup(src->d.id.name);
2465 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002466 case XC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002467 IF_BC(case BC_RESULT_LAST:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002468 IF_BC(case BC_RESULT_ONE:)
2469 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002470 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2471 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002472 }
2473}
2474#endif // ENABLE_DC
2475
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002476static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002477{
2478 BcResult *r = (BcResult *) result;
2479
2480 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002481 case XC_RESULT_TEMP:
2482 case XC_RESULT_IBASE:
2483 case XC_RESULT_SCALE:
2484 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002485 bc_num_free(&r->d.n);
2486 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002487 case XC_RESULT_VAR:
2488 case XC_RESULT_ARRAY:
2489 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002490 free(r->d.id.name);
2491 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002492 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002493 // Do nothing.
2494 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002495 }
2496}
2497
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002498static int bad_input_byte(char c)
2499{
2500 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2501 || c > 0x7e
2502 ) {
2503 bc_error_fmt("illegal character 0x%02x", c);
2504 return 1;
2505 }
2506 return 0;
2507}
2508
2509// Note: it _appends_ data from fp to vec.
2510static void bc_read_line(BcVec *vec, FILE *fp)
2511{
2512 again:
2513 fflush_and_check();
2514
2515#if ENABLE_FEATURE_BC_SIGNALS
2516 if (G_interrupt) { // ^C was pressed
2517 intr:
2518 if (fp != stdin) {
2519 // ^C while running a script (bc SCRIPT): die.
2520 // We do not return to interactive prompt:
2521 // user might be running us from a shell,
2522 // and SCRIPT might be intended to terminate
2523 // (e.g. contain a "halt" stmt).
2524 // ^C dropping user into a bc prompt instead of
2525 // the shell would be unexpected.
2526 xfunc_die();
2527 }
2528 // ^C while interactive input
2529 G_interrupt = 0;
2530 // GNU bc says "interrupted execution."
2531 // GNU dc says "Interrupt!"
2532 fputs("\ninterrupted execution\n", stderr);
2533 }
2534
2535# if ENABLE_FEATURE_EDITING
2536 if (G_ttyin && fp == stdin) {
2537 int n, i;
2538# define line_buf bb_common_bufsiz1
2539 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2540 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2541 if (n == 0) // ^C
2542 goto intr;
2543 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2544 return;
2545 }
2546 i = 0;
2547 for (;;) {
2548 char c = line_buf[i++];
2549 if (!c) break;
2550 if (bad_input_byte(c)) goto again;
2551 }
2552 bc_vec_concat(vec, line_buf);
2553# undef line_buf
2554 } else
2555# endif
2556#endif
2557 {
2558 int c;
2559 bool bad_chars = 0;
2560 size_t len = vec->len;
2561
2562 do {
2563#if ENABLE_FEATURE_BC_SIGNALS
2564 if (G_interrupt) {
2565 // ^C was pressed: ignore entire line, get another one
2566 vec->len = len;
2567 goto intr;
2568 }
2569#endif
2570 do c = fgetc(fp); while (c == '\0');
2571 if (c == EOF) {
2572 if (ferror(fp))
2573 bb_perror_msg_and_die("input error");
2574 // Note: EOF does not append '\n'
2575 break;
2576 }
2577 bad_chars |= bad_input_byte(c);
2578 bc_vec_pushByte(vec, (char)c);
2579 } while (c != '\n');
2580
2581 if (bad_chars) {
2582 // Bad chars on this line
2583 if (!G.prog.file) { // stdin
2584 // ignore entire line, get another one
2585 vec->len = len;
2586 goto again;
2587 }
2588 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
2589 }
2590 bc_vec_pushZeroByte(vec);
2591 }
2592}
2593
2594//
2595// Parsing routines
2596//
2597
2598static bool bc_num_strValid(const char *val, size_t base)
2599{
2600 BcDig b;
2601 bool radix;
2602
2603 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2604 radix = false;
2605 for (;;) {
2606 BcDig c = *val++;
2607 if (c == '\0')
2608 break;
2609 if (c == '.') {
2610 if (radix) return false;
2611 radix = true;
2612 continue;
2613 }
2614 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2615 return false;
2616 }
2617 return true;
2618}
2619
2620// Note: n is already "bc_num_zero()"ed,
2621// leading zeroes in "val" are removed
2622static void bc_num_parseDecimal(BcNum *n, const char *val)
2623{
2624 size_t len, i;
2625 const char *ptr;
2626
2627 len = strlen(val);
2628 if (len == 0)
2629 return;
2630
2631 bc_num_expand(n, len);
2632
2633 ptr = strchr(val, '.');
2634
2635 n->rdx = 0;
2636 if (ptr != NULL)
2637 n->rdx = (size_t)((val + len) - (ptr + 1));
2638
2639 for (i = 0; val[i]; ++i) {
2640 if (val[i] != '0' && val[i] != '.') {
2641 // Not entirely zero value - convert it, and exit
2642 i = len - 1;
2643 for (;;) {
2644 n->num[n->len] = val[i] - '0';
2645 ++n->len;
2646 skip_dot:
2647 if (i == 0) break;
2648 if (val[--i] == '.') goto skip_dot;
2649 }
2650 break;
2651 }
2652 }
2653 // if for() exits without hitting if(), the value is entirely zero
2654}
2655
2656// Note: n is already "bc_num_zero()"ed,
2657// leading zeroes in "val" are removed
2658static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2659{
2660 BcStatus s;
2661 BcNum temp, mult, result;
2662 BcNum base;
2663 BcDig base_digs[ULONG_NUM_BUFSIZE];
2664 BcDig c = '\0';
2665 unsigned long v;
2666 size_t i, digits;
2667
2668 for (i = 0; ; ++i) {
2669 if (val[i] == '\0')
2670 return;
2671 if (val[i] != '.' && val[i] != '0')
2672 break;
2673 }
2674
2675 bc_num_init_DEF_SIZE(&temp);
2676 bc_num_init_DEF_SIZE(&mult);
2677 base.cap = ARRAY_SIZE(base_digs);
2678 base.num = base_digs;
2679 bc_num_ulong2num(&base, base_t);
2680
2681 for (;;) {
2682 c = *val++;
2683 if (c == '\0') goto int_err;
2684 if (c == '.') break;
2685
2686 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2687
2688 s = zbc_num_mul(n, &base, &mult, 0);
2689 if (s) goto int_err;
2690 bc_num_ulong2num(&temp, v);
2691 s = zbc_num_add(&mult, &temp, n, 0);
2692 if (s) goto int_err;
2693 }
2694
2695 bc_num_init(&result, base.len);
2696 //bc_num_zero(&result); - already is
2697 bc_num_one(&mult);
2698
2699 digits = 0;
2700 for (;;) {
2701 c = *val++;
2702 if (c == '\0') break;
2703 digits++;
2704
2705 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2706
2707 s = zbc_num_mul(&result, &base, &result, 0);
2708 if (s) goto err;
2709 bc_num_ulong2num(&temp, v);
2710 s = zbc_num_add(&result, &temp, &result, 0);
2711 if (s) goto err;
2712 s = zbc_num_mul(&mult, &base, &mult, 0);
2713 if (s) goto err;
2714 }
2715
2716 s = zbc_num_div(&result, &mult, &result, digits);
2717 if (s) goto err;
2718 s = zbc_num_add(n, &result, n, digits);
2719 if (s) goto err;
2720
2721 if (n->len != 0) {
2722 if (n->rdx < digits)
2723 bc_num_extend(n, digits - n->rdx);
2724 } else
2725 bc_num_zero(n);
2726 err:
2727 bc_num_free(&result);
2728 int_err:
2729 bc_num_free(&mult);
2730 bc_num_free(&temp);
2731}
2732
2733static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
2734{
2735 if (!bc_num_strValid(val, base_t))
2736 RETURN_STATUS(bc_error("bad number string"));
2737
2738 bc_num_zero(n);
2739 while (*val == '0') val++;
2740
2741 if (base_t == 10)
2742 bc_num_parseDecimal(n, val);
2743 else
2744 bc_num_parseBase(n, val, base_t);
2745
2746 RETURN_STATUS(BC_STATUS_SUCCESS);
2747}
2748#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
2749
Gavin Howard01055ba2018-11-03 11:00:21 -06002750static void bc_lex_lineComment(BcLex *l)
2751{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002752 // Try: echo -n '#foo' | bc
2753 size_t i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002754 l->lex = XC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002755 i = l->i;
2756 while (i < l->len && l->buf[i] != '\n')
2757 i++;
2758 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002759}
2760
2761static void bc_lex_whitespace(BcLex *l)
2762{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002763 l->lex = XC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002764 for (;;) {
2765 char c = l->buf[l->i];
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002766 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002767 break;
2768 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002769 break;
2770 l->i++;
2771 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002772}
2773
Denys Vlasenko29301232018-12-11 15:29:32 +01002774static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002775{
2776 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002777 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002778 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002779
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002780 pt = (start == '.');
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002781 l->lex = XC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002782 ccnt = i = 0;
2783 for (;;) {
2784 char c = buf[i];
2785 if (c == '\0')
2786 break;
2787 if (c == '\\' && buf[i + 1] == '\n') {
2788 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002789 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002790 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002791 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002792 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2793 if (c != '.') break;
2794 // if '.' was already seen, stop on second one:
2795 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002796 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002797 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002798 // buf[i] is one of "0-9A-F."
2799 i++;
2800 if (c != '.')
2801 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002802 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002803 //ccnt is the number of chars in the number string, excluding possible
2804 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2805 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002806 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002807
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002808 // This might overestimate the size, if there are "\<NL>"'s
2809 // in the number. Subtracting number_of_backslashes*2 correctly
2810 // is not that easy: consider that in the case of "NNN.\<NL>"
2811 // loop above will count "\<NL>" before it realizes it is not
2812 // in fact *inside* the number:
2813 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002814
Denys Vlasenko64074a12018-12-07 15:50:14 +01002815 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2816 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2817 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002818 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002819 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002820
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002821 bc_vec_pop_all(&l->lex_buf);
2822 bc_vec_expand(&l->lex_buf, 1 + len);
2823 bc_vec_push(&l->lex_buf, &start);
Gavin Howard01055ba2018-11-03 11:00:21 -06002824
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002825 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002826 // If we have hit a backslash, skip it. We don't have
2827 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002828 if (*buf == '\\') {
2829 buf += 2;
2830 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002831 continue;
2832 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002833 bc_vec_push(&l->lex_buf, buf);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002834 buf++;
2835 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002836 }
2837
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002838 bc_vec_pushZeroByte(&l->lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002839
Denys Vlasenko29301232018-12-11 15:29:32 +01002840 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002841}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002842#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002843
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002844static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002845{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002846 size_t i;
2847 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002848
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002849 l->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06002850
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002851 i = 0;
2852 buf = l->buf + l->i - 1;
2853 for (;;) {
2854 char c = buf[i];
2855 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2856 i++;
2857 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002858
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002859#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002860 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2861 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2862 if (i > BC_MAX_STRING)
2863 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2864 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002865#endif
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002866 bc_vec_string(&l->lex_buf, i, buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002867
2868 // Increment the index. We minus 1 because it has already been incremented.
2869 l->i += i - 1;
2870
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002871 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002872}
2873
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002874static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002875{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002876 bc_char_vec_init(&l->lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002877}
2878
2879static void bc_lex_free(BcLex *l)
2880{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002881 bc_vec_free(&l->lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002882}
2883
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002884static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002885{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002886 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002887 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002888}
2889
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002890static bool bc_lex_more_input(BcLex *l)
2891{
2892 size_t str;
2893 bool comment;
2894
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002895 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002896
2897 // This loop is complex because the vm tries not to send any lines that end
2898 // with a backslash to the parser. The reason for that is because the parser
2899 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2900 // case, and for strings and comments, the parser will expect more stuff.
2901 comment = false;
2902 str = 0;
2903 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002904 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002905 char *string;
2906
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002907 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002908 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002909 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002910 break;
2911
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002912 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002913 while (*string) {
2914 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002915 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002916 if (IS_BC)
2917 str ^= (c == '"');
2918 else {
2919 if (c == ']')
2920 str -= 1;
2921 else if (c == '[')
2922 str += 1;
2923 }
2924 }
2925 string++;
2926 if (c == '/' && *string == '*') {
2927 comment = true;
2928 string++;
2929 continue;
2930 }
2931 if (c == '*' && *string == '/') {
2932 comment = false;
2933 string++;
2934 }
2935 }
2936 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002937 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002938 continue;
2939 }
2940
2941 // Check for backslash+newline.
2942 // we do not check that last char is '\n' -
2943 // if it is not, then it's EOF, and looping back
2944 // to bc_read_line() will detect it:
2945 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002946 if (string >= G.input_buffer.v && *string == '\\') {
2947 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002948 continue;
2949 }
2950
2951 break;
2952 }
2953
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002954 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002955 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002956// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2957 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002958
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002959 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002960}
2961
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002962IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2963IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002964#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2965#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002966
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002967static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002968{
2969 BcStatus s;
2970
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002971 l->lex_last = l->lex;
2972 if (l->lex_last == XC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002973
2974 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002975 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002976 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002977
2978 // Loop until failure or we don't have whitespace. This
2979 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002980 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002981 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002982 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002983 if (l->i == l->len) {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002984 l->lex = XC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002985 if (!G.input_fp)
2986 RETURN_STATUS(BC_STATUS_SUCCESS);
2987 if (!bc_lex_more_input(l)) {
2988 G.input_fp = NULL;
2989 RETURN_STATUS(BC_STATUS_SUCCESS);
2990 }
2991 // here it's guaranteed that l->i is below l->len
2992 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002993 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002994 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2995 l->buf + l->i);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002996 if (IS_BC) {
2997 IF_BC(s = zbc_lex_token(l));
2998 } else {
2999 IF_DC(s = zdc_lex_token(l));
3000 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003001 } while (!s && l->lex == XC_LEX_WHITESPACE);
3002 dbg_lex("l->lex from string:%d", l->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06003003
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003004 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003005}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003006#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003007
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003008#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01003009static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
3010{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003011 if (l->lex == XC_LEX_NLINE)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003012 RETURN_STATUS(zbc_lex_next(l));
3013 RETURN_STATUS(BC_STATUS_SUCCESS);
3014}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003015#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003016
3017static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
3018{
3019 BcStatus s;
3020 s = zbc_lex_next(l);
3021 if (s) RETURN_STATUS(s);
3022 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
3023 s = zbc_lex_skip_if_at_NLINE(l);
3024 RETURN_STATUS(s);
3025}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003026#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003027#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01003028
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003029static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003030{
3031 l->buf = text;
3032 l->i = 0;
3033 l->len = strlen(text);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003034 l->lex = l->lex_last = XC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003035 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003036}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003037#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003038
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003039#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003040static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003041{
3042 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003043 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003044 const char *buf = l->buf + l->i - 1;
3045
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003046 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3047 const char *keyword8 = bc_lex_kws[i].name8;
3048 unsigned j = 0;
3049 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3050 j++;
3051 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003052 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003053 if (keyword8[j] != '\0')
3054 continue;
3055 match:
3056 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003057 if (isalnum(buf[j]) || buf[j]=='_')
3058 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003059 l->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003060 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003061 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3062 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003063 }
3064
3065 // We minus 1 because the index has already been incremented.
3066 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003067 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003068 }
3069
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003070 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003071 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003072
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003073 if (l->lex_buf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003074 // Prevent this:
3075 // >>> qwe=1
3076 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3077 // '
3078 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003079 s = zbc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003080 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003081
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003082 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003083}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003084#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003085
Denys Vlasenko26819db2018-12-12 16:08:46 +01003086static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003087{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003088 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003089
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003090 l->lex = XC_LEX_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06003091
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003092 nls = 0;
3093 i = l->i;
3094 for (;;) {
3095 char c = l->buf[i];
3096 if (c == '\0') {
3097 l->i = i;
3098 RETURN_STATUS(bc_error("string end could not be found"));
3099 }
3100 if (c == '"')
3101 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003102 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003103 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003104 }
3105
3106 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003107 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3108 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3109 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003110 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003111 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003112 bc_vec_string(&l->lex_buf, len, l->buf + l->i);
Gavin Howard01055ba2018-11-03 11:00:21 -06003113
3114 l->i = i + 1;
3115 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003116 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003117
Denys Vlasenko26819db2018-12-12 16:08:46 +01003118 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003119}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003120#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003121
Denys Vlasenko0a238142018-12-14 16:48:34 +01003122static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003123{
3124 if (l->buf[l->i] == '=') {
3125 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003126 with_and_without >>= 8; // store "with" value
3127 } // else store "without" value
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003128 l->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003129}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003130#define bc_lex_assign(l, with, without) \
3131 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003132
Denys Vlasenko29301232018-12-11 15:29:32 +01003133static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003134{
3135 size_t i, nls = 0;
3136 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003137
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003138 l->lex = XC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003139 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003140 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003141 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003142 check_star:
3143 if (c == '*') {
3144 c = buf[++i];
3145 if (c == '/')
3146 break;
3147 goto check_star;
3148 }
3149 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003151 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003152 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003153 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003154 }
3155
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003156 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003157 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003158 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003159
Denys Vlasenko29301232018-12-11 15:29:32 +01003160 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003161}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003162#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003163
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003164#undef zbc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003165static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003166{
3167 BcStatus s = BC_STATUS_SUCCESS;
3168 char c = l->buf[l->i++], c2;
3169
3170 // This is the workhorse of the lexer.
3171 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003172// case '\0': // probably never reached
3173// l->i--;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003174// l->lex = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003175// l->newline = true;
3176// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003177 case '\n':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003178 l->lex = XC_LEX_NLINE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003179 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003180 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003181 case '\t':
3182 case '\v':
3183 case '\f':
3184 case '\r':
3185 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003186 bc_lex_whitespace(l);
3187 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003188 case '!':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003189 bc_lex_assign(l, XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003190 if (l->lex == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003191 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
3192 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003193 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003194 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003195 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003196 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003198 case '#':
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003199 s = zbc_POSIX_does_not_allow("'#' script comments");
3200 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003201 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003202 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 case '%':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003204 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003207 c2 = l->buf[l->i];
3208 if (c2 == '&') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003209 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3210 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003211 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003212 l->lex = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003213 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003214 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003215 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003217 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 case '(':
3219 case ')':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003220 l->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 case '*':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003223 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
Gavin Howard01055ba2018-11-03 11:00:21 -06003224 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003225 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003226 c2 = l->buf[l->i];
3227 if (c2 == '+') {
3228 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003229 l->lex = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003230 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003231 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003232 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003233 case ',':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003234 l->lex = BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06003235 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003236 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003237 c2 = l->buf[l->i];
3238 if (c2 == '-') {
3239 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003240 l->lex = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003241 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003242 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003243 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003244 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003245 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003246 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003247 else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003248 l->lex = BC_LEX_KEY_LAST;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003249 s = zbc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 }
3251 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003253 c2 = l->buf[l->i];
3254 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003255 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003256 else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003257 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003258 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003259 case '0':
3260 case '1':
3261 case '2':
3262 case '3':
3263 case '4':
3264 case '5':
3265 case '6':
3266 case '7':
3267 case '8':
3268 case '9':
3269 case 'A':
3270 case 'B':
3271 case 'C':
3272 case 'D':
3273 case 'E':
3274 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003275 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003276 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003277 case ';':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003278 l->lex = BC_LEX_SCOLON;
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003280 case '<':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003281 bc_lex_assign(l, XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003282 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003283 case '=':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003284 bc_lex_assign(l, XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003286 case '>':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003287 bc_lex_assign(l, XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003288 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003289 case '[':
3290 case ']':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003291 l->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06003292 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003293 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003294 if (l->buf[l->i] == '\n') {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003295 l->lex = XC_LEX_WHITESPACE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003296 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003297 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003298 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003299 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003300 case '^':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003301 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
Gavin Howard01055ba2018-11-03 11:00:21 -06003302 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003303 case 'a':
3304 case 'b':
3305 case 'c':
3306 case 'd':
3307 case 'e':
3308 case 'f':
3309 case 'g':
3310 case 'h':
3311 case 'i':
3312 case 'j':
3313 case 'k':
3314 case 'l':
3315 case 'm':
3316 case 'n':
3317 case 'o':
3318 case 'p':
3319 case 'q':
3320 case 'r':
3321 case 's':
3322 case 't':
3323 case 'u':
3324 case 'v':
3325 case 'w':
3326 case 'x':
3327 case 'y':
3328 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003329 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003330 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003331 case '{':
3332 case '}':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003333 l->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003334 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003335 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003336 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003337 if (c2 == '|') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003338 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3339 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003340 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003341 l->lex = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003342 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003343 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003344 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003345 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003346 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003347 default:
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003348 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003349 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003350 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003351 }
3352
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003353 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003354}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003355#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003356#endif // ENABLE_BC
3357
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003358#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003359static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003360{
Denys Vlasenko81293c82018-12-24 01:53:55 +01003361 if (G_exreg && isspace(l->buf[l->i])) {
3362 bc_lex_whitespace(l); // eats whitespace (but not newline)
3363 l->i++; // bc_lex_name() expects this
Denys Vlasenko29301232018-12-11 15:29:32 +01003364 bc_lex_name(l);
Denys Vlasenko81293c82018-12-24 01:53:55 +01003365 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003366 bc_vec_pop_all(&l->lex_buf);
3367 bc_vec_push(&l->lex_buf, &l->buf[l->i++]);
3368 bc_vec_pushZeroByte(&l->lex_buf);
3369 l->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003370 }
3371
Denys Vlasenko29301232018-12-11 15:29:32 +01003372 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003373}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003374#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003375
Denys Vlasenko29301232018-12-11 15:29:32 +01003376static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003377{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003378 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003379
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003380 l->lex = XC_LEX_STR;
3381 bc_vec_pop_all(&l->lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003382
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003383 nls = 0;
3384 depth = 1;
3385 i = l->i;
3386 for (;;) {
3387 char c = l->buf[i];
3388 if (c == '\0') {
3389 l->i = i;
3390 RETURN_STATUS(bc_error("string end could not be found"));
3391 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003392 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003393 if (i == l->i || l->buf[i - 1] != '\\') {
3394 if (c == '[') depth++;
3395 if (c == ']')
3396 if (--depth == 0)
3397 break;
3398 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003399 bc_vec_push(&l->lex_buf, &l->buf[i]);
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003400 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003401 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003402 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003403
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003404 bc_vec_pushZeroByte(&l->lex_buf);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003405 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3406 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3407 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003408 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003409 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003410
3411 l->i = i;
3412 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003413 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003414
Denys Vlasenko29301232018-12-11 15:29:32 +01003415 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003416}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003417#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003418
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003419#undef zdc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003420static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003421{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003422 static const //BcLexType - should be this type, but narrower type saves size:
3423 uint8_t
3424 dc_lex_regs[] = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003425 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 +01003426 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3427 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003428 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003429 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003430
Denys Vlasenko81293c82018-12-24 01:53:55 +01003431 BcStatus s;
3432 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003433 size_t i;
3434
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003435 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003436 if (l->lex_last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003437 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003438 }
3439
Denys Vlasenko81293c82018-12-24 01:53:55 +01003440 s = BC_STATUS_SUCCESS;
3441 c = l->buf[l->i++];
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003442 if (c >= '%' && c <= '~'
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003443 && (l->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003444 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003445 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003446 }
3447
3448 // This is the workhorse of the lexer.
3449 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003450// case '\0': // probably never reached
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003451// l->lex = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003452// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003453 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003454 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003455 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003456 // only to allow interactive dc have a way to exit
3457 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003458 // on <enter>, not on _next_ token (which would mean
3459 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003460 // IOW: typing "1p<enter>" should print "1" _at once_,
3461 // not after some more input.
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003462 l->lex = XC_LEX_NLINE;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003463 l->newline = true;
3464 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003465 case '\t':
3466 case '\v':
3467 case '\f':
3468 case '\r':
3469 case ' ':
Denys Vlasenko81293c82018-12-24 01:53:55 +01003470 l->newline = 0; // was (c == '\n')
Gavin Howard01055ba2018-11-03 11:00:21 -06003471 bc_lex_whitespace(l);
3472 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003473 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003474 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003475 if (c2 == '=')
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003476 l->lex = XC_LEX_OP_REL_NE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003477 else if (c2 == '<')
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003478 l->lex = XC_LEX_OP_REL_LE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003479 else if (c2 == '>')
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003480 l->lex = XC_LEX_OP_REL_GE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003481 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003482 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003483 ++l->i;
3484 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003485 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003486 bc_lex_lineComment(l);
3487 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003488 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003489 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003490 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003491 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003492 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003493 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003494 case '0':
3495 case '1':
3496 case '2':
3497 case '3':
3498 case '4':
3499 case '5':
3500 case '6':
3501 case '7':
3502 case '8':
3503 case '9':
3504 case 'A':
3505 case 'B':
3506 case 'C':
3507 case 'D':
3508 case 'E':
3509 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003510 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003511 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003512 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003513 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003514 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003515 default:
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003516 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003517 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003518 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003519 }
3520
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003521 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003522}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003523#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003524#endif // ENABLE_DC
3525
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003526static void bc_parse_push(BcParse *p, char i)
3527{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003528 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003529 bc_vec_pushByte(&p->func->code, i);
3530}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003531
Gavin Howard01055ba2018-11-03 11:00:21 -06003532static void bc_parse_pushName(BcParse *p, char *name)
3533{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003534 while (*name)
3535 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003536 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003537}
3538
3539static void bc_parse_pushIndex(BcParse *p, size_t idx)
3540{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003541 size_t mask;
3542 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003543
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003544 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003545 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3546 amt = sizeof(idx);
3547 do {
3548 if (idx & mask) break;
3549 mask >>= 8;
3550 amt--;
3551 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003552
3553 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003554
3555 while (idx != 0) {
3556 bc_parse_push(p, (unsigned char)idx);
3557 idx >>= 8;
3558 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003559}
3560
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003561#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003562static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3563{
3564 bc_parse_push(p, BC_INST_JUMP);
3565 bc_parse_pushIndex(p, idx);
3566}
3567
3568static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3569{
3570 bc_parse_push(p, BC_INST_JUMP_ZERO);
3571 bc_parse_pushIndex(p, idx);
3572}
3573
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003574static BC_STATUS zbc_parse_pushSTR(BcParse *p)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003575{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003576 char *str = xstrdup(p->l.lex_buf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003577
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003578 bc_parse_push(p, XC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003579 bc_parse_pushIndex(p, p->func->strs.len);
3580 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003581
3582 RETURN_STATUS(zbc_lex_next(&p->l));
3583}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003584#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003585#endif
3586
3587static void bc_parse_pushNUM(BcParse *p)
3588{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003589 char *num = xstrdup(p->l.lex_buf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003590#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003591 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003592#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003593 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003594#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003595 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003596#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003597 bc_parse_push(p, XC_INST_NUM);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003598 bc_parse_pushIndex(p, idx);
3599}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003600
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003601static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003602{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003603 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003604
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003605 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003606}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003607#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003608
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003609// Called when parsing or execution detects a failure,
3610// resets execution structures.
3611static void bc_program_reset(void)
3612{
3613 BcFunc *f;
3614 BcInstPtr *ip;
3615
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003616 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003617 bc_vec_pop_all(&G.prog.results);
3618
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003619 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003620 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003621 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003622}
3623
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003624// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003625// resets parsing structures.
3626static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003627{
3628 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003629 bc_func_free(p->func);
3630 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003631
Denys Vlasenko65e10462018-12-19 15:13:14 +01003632 p->fidx = BC_PROG_MAIN;
3633 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003634 }
3635
3636 p->l.i = p->l.len;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003637 p->l.lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003638
Denys Vlasenko503faf92018-12-20 16:24:18 +01003639 IF_BC(bc_vec_pop_all(&p->exits);)
3640 IF_BC(bc_vec_pop_all(&p->conds);)
3641 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003642
Denys Vlasenkod38af482018-12-04 19:11:02 +01003643 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003644}
3645
3646static void bc_parse_free(BcParse *p)
3647{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003648 IF_BC(bc_vec_free(&p->exits);)
3649 IF_BC(bc_vec_free(&p->conds);)
3650 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003651 bc_lex_free(&p->l);
3652}
3653
Denys Vlasenkofa210792018-12-19 19:35:40 +01003654static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003655{
3656 memset(p, 0, sizeof(BcParse));
3657
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003658 bc_lex_init(&p->l);
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;
3664 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003665}
3666
Denys Vlasenko04715442018-12-21 00:10:26 +01003667static void bc_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 Vlasenko408b7d42018-12-19 19:43:03 +01003698 BcFunc *func = bc_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 Vlasenko44a99ca2018-12-20 20:34:09 +01003702 bc_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 Vlasenko4b72aeb2018-12-17 16:54:37 +01003714static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags);
3715
3716static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags)
3717{
3718 BcStatus s;
3719
3720 s = bc_parse_expr_empty_ok(p, flags);
3721 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
3727static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, 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
3730static BC_STATUS zbc_parse_stmt(BcParse *p)
3731{
3732 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3733}
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 Vlasenko94f72a32018-12-17 00:07:48 +01003736static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003737{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003738 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3739 // Same for "else", "while()", "for()".
3740 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3741 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003742 if (p->l.lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003743 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003744
3745 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003746}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003747#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003748
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003749static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3750 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003751{
Denys Vlasenko69560f42018-12-24 14:14:23 +01003752 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3753 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003754
3755 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003756 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003757 if (t == BC_LEX_LPAREN) break;
3758
Denys Vlasenko69560f42018-12-24 14:14:23 +01003759 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003760 if (l >= r && (l != r || !left)) break;
3761
Denys Vlasenko0154d782018-12-14 23:32:51 +01003762 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003763 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003764 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003765 }
3766
3767 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003768}
3769
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003770static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003771{
3772 BcLexType top;
3773
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003774 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003775 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003776 top = BC_PARSE_TOP_OP(p);
3777
3778 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003779 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003780
3781 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003782 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003783
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003784 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003785 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003786 top = BC_PARSE_TOP_OP(p);
3787 }
3788
3789 bc_vec_pop(&p->ops);
3790
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003791 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003792}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003793#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003794
Denys Vlasenko26819db2018-12-12 16:08:46 +01003795static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003796{
3797 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003798 size_t nparams;
3799
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003800 dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003801 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3802
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003803 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003804 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003805
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003806 nparams = 0;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003807 if (p->l.lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003808 for (;;) {
3809 s = zbc_parse_expr(p, flags);
3810 if (s) RETURN_STATUS(s);
3811 nparams++;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003812 if (p->l.lex != BC_LEX_COMMA) {
3813 if (p->l.lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003814 break;
3815 RETURN_STATUS(bc_error_bad_token());
3816 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003817 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003818 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003819 }
3820 }
3821
Gavin Howard01055ba2018-11-03 11:00:21 -06003822 bc_parse_push(p, BC_INST_CALL);
3823 bc_parse_pushIndex(p, nparams);
3824
Denys Vlasenko26819db2018-12-12 16:08:46 +01003825 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003826}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003827#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003828
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003829// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003830static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003831{
3832 BcStatus s;
3833 BcId entry, *entry_ptr;
3834 size_t idx;
3835
3836 entry.name = name;
3837
Denys Vlasenko26819db2018-12-12 16:08:46 +01003838 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003839 if (s) goto err;
3840
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003841 if (p->l.lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003842 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003843 goto err;
3844 }
3845
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003846 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003847
3848 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003849 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003850 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003851 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003852 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003853 free(name);
3854
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003855 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003856 bc_parse_pushIndex(p, entry_ptr->idx);
3857
Denys Vlasenko26819db2018-12-12 16:08:46 +01003858 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003859 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003860 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003861 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003862}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003863#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003864
Denys Vlasenko26819db2018-12-12 16:08:46 +01003865static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003866{
3867 BcStatus s;
3868 char *name;
3869
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003870 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003871 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003872 if (s) goto err;
3873
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003874 if (p->l.lex == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003875 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003876 if (s) goto err;
3877
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003878 if (p->l.lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003879 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003880 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003881 goto err;
3882 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003883 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003884 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003885 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003886 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003887 s = zbc_parse_expr(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003888 if (s) goto err;
3889 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003890 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003891 if (s) goto err;
3892 bc_parse_push(p, *type);
3893 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003894 free(name);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003895 } else if (p->l.lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003896 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003897 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003898 goto err;
3899 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003900 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003901 s = zbc_parse_call(p, name, flags);
3902 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003903 *type = XC_INST_VAR;
3904 bc_parse_push(p, XC_INST_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06003905 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003906 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003907 }
3908
Denys Vlasenko26819db2018-12-12 16:08:46 +01003909 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003910 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003911 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003912 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003913}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003914#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003915
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003916static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003917{
3918 BcStatus s;
3919
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003920 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003921 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003922 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003923
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003924 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003925 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003926 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003927
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003928 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003929
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003930 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003931}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003932#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003933
Denys Vlasenko26819db2018-12-12 16:08:46 +01003934static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003935 BcInst *prev)
3936{
3937 BcStatus s;
3938
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003939 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003940 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003941 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003942
3943 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3944
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003945 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003946 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003947
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003948 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003949 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003950
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003951 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003952
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003953 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Gavin Howard01055ba2018-11-03 11:00:21 -06003954 bc_parse_push(p, *prev);
3955
Denys Vlasenko26819db2018-12-12 16:08:46 +01003956 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003957}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003958#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003959
Denys Vlasenko26819db2018-12-12 16:08:46 +01003960static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003961{
3962 BcStatus s;
3963
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003964 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003965 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003966
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003967 if (p->l.lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003968 *type = XC_INST_SCALE;
3969 bc_parse_push(p, XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003970 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003971 }
3972
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003973 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003974 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3975
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003976 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003977 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003978
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003979 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003980 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003981 if (p->l.lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003982 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003983 bc_parse_push(p, XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003984
Denys Vlasenko26819db2018-12-12 16:08:46 +01003985 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003986}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003987#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003988
Denys Vlasenko26819db2018-12-12 16:08:46 +01003989static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003990 size_t *nexprs, uint8_t flags)
3991{
3992 BcStatus s;
3993 BcLexType type;
3994 char inst;
3995 BcInst etype = *prev;
3996
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003997 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
3998 || etype == XC_INST_SCALE || etype == BC_INST_LAST
3999 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004000 ) {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004001 *prev = inst = BC_INST_INC_POST + (p->l.lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004002 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004003 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004004 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004005 *prev = inst = BC_INST_INC_PRE + (p->l.lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004006 *paren_expr = true;
4007
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004008 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004009 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004010 type = p->l.lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004011
4012 // Because we parse the next part of the expression
4013 // right here, we need to increment this.
4014 *nexprs = *nexprs + 1;
4015
4016 switch (type) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004017 case XC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004018 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004019 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004020 case BC_LEX_KEY_IBASE:
4021 case BC_LEX_KEY_LAST:
4022 case BC_LEX_KEY_OBASE:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004023 bc_parse_push(p, type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004024 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004025 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004026 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004027 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004028 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004029 if (p->l.lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004030 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004031 else
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004032 bc_parse_push(p, XC_INST_SCALE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004033 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004034 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004035 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004036 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004037 }
4038
4039 if (!s) bc_parse_push(p, inst);
4040 }
4041
Denys Vlasenko26819db2018-12-12 16:08:46 +01004042 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004043}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004044#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004045
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004046#if 0
4047#define BC_PARSE_LEAF(p, rparen) \
4048 ((rparen) \
4049 || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \
4050 || (p) == BC_INST_INC_POST \
4051 || (p) == BC_INST_DEC_POST \
4052 )
4053#else
4054static int ok_in_expr(BcInst p)
4055{
4056 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4057 || p == BC_INST_INC_POST
4058 || p == BC_INST_DEC_POST
4059 ;
4060}
4061#define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p))
4062#endif
4063
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004064static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004065 bool rparen, size_t *nexprs)
4066{
4067 BcStatus s;
4068 BcLexType type;
4069 BcInst etype = *prev;
4070
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004071 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004072 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004073
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004074 type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004075 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004076
4077 // We can just push onto the op stack because this is the largest
4078 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004079 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004080 bc_vec_push(&p->ops, &type);
4081 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004082 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004083
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004084 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004085}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004086#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004087
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004088static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004089{
4090 BcStatus s;
4091 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004092
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004093 for (;;) {
4094 s = zbc_lex_next(&p->l);
4095 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004096 type = p->l.lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004097 if (type == XC_LEX_STR) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004098 s = zbc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004099 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004100 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004101 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004102 if (s) RETURN_STATUS(s);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004103 bc_parse_push(p, XC_INST_PRINT_POP);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004104 if (p->l.lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004105 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004106 }
4107
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004108 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004109}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004110#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004111
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004112static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004113{
4114 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 Vlasenko9a34e892018-12-12 13:58:55 +01004118 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004119 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004120
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004121 t = p->l.lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004122 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Gavin Howard01055ba2018-11-03 11:00:21 -06004123 bc_parse_push(p, BC_INST_RET0);
4124 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004125 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004126 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004127 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004128 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004129 s = zbc_lex_next(&p->l);
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 Vlasenkoa17d95d2018-12-24 22:55:54 +01004133 if (!paren || p->l.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 Vlasenkoa7732d12018-12-24 04:26:07 +01004138 bc_parse_push(p, 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 Vlasenko94f72a32018-12-17 00:07:48 +01004146static void rewrite_label_to_current(BcParse *p, size_t idx)
4147{
4148 size_t *label = bc_vec_item(&p->func->labels, idx);
4149 *label = p->func->code.len;
4150}
4151
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004152static BC_STATUS zbc_parse_if(BcParse *p)
4153{
4154 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004155 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004156
4157 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4158 s = zbc_lex_next(&p->l);
4159 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004160 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004161
4162 s = zbc_lex_next(&p->l);
4163 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004164 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004165 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004166 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004167
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004168 // Encode "if zero, jump to ..."
4169 // Pushed value (destination of the jump) is uninitialized,
4170 // will be rewritten to be address of "end of if()" or of "else".
4171 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004172 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004173
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004174 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004175 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004176
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004177 dbg_lex("%s:%d in if after stmt: p->l.lex:%d", __func__, __LINE__, p->l.lex);
4178 if (p->l.lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004179 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004180
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004181 // Encode "after then_stmt, jump to end of if()"
4182 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4183 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004184 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004185
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004186 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004187 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004188
Denys Vlasenko15850832018-12-16 21:40:54 +01004189 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004190
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004191 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004192 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004193 }
4194
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004195 dbg_lex("%s:%d rewriting label to jump after 'if' body-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004196 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004197
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004198 dbg_lex_done("%s:%d done", __func__, __LINE__);
4199 RETURN_STATUS(s);
4200}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004201#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004202
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004203static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004204{
4205 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004206 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004207 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004208
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004209 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004210 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004211 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004212 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004213 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004214
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004215 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004216 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004217 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004218
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004219 bc_vec_push(&p->exits, &ip_idx);
4220 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004221
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004222 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004223 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004224 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004225
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004226 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004227
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004228 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004229 if (s) RETURN_STATUS(s);
4230
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004231 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004232 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004233
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004234 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004235 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004236
4237 bc_vec_pop(&p->exits);
4238 bc_vec_pop(&p->conds);
4239
4240 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004241}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004242#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004243
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004244static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004245{
4246 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004247 size_t cond_idx, exit_idx, body_idx, update_idx;
4248
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004249 dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004250 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004251 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004252 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004253 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004254 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004255
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004256 if (p->l.lex != BC_LEX_SCOLON) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004257 s = zbc_parse_expr(p, 0);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004258 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004259 if (s) RETURN_STATUS(s);
4260 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004261 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4262 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004263 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004264
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004265 if (p->l.lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004266 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004267 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004268
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004269 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004270 update_idx = cond_idx + 1;
4271 body_idx = update_idx + 1;
4272 exit_idx = body_idx + 1;
4273
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004274 if (p->l.lex != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004275 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004276 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004277 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004278 // This is safe to set because the current token is a semicolon,
4279 // which has no string requirement.
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004280 bc_vec_string(&p->l.lex_buf, 1, "1");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004281 bc_parse_pushNUM(p);
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004282 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004283 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004284 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004285
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004286 if (p->l.lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004287
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004288 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004289 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004290
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004291 bc_parse_pushJUMP_ZERO(p, exit_idx);
4292 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004293
Gavin Howard01055ba2018-11-03 11:00:21 -06004294 bc_vec_push(&p->conds, &update_idx);
4295 bc_vec_push(&p->func->labels, &p->func->code.len);
4296
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004297 if (p->l.lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004298 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004299 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004300 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004301 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004302 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004303 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4304 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004305 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004306
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004307 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004308 bc_vec_push(&p->func->labels, &p->func->code.len);
4309
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004310 bc_vec_push(&p->exits, &exit_idx);
4311 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004312
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004313 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004314 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004315
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004316 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004317 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004318
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004319 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004320 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004321
4322 bc_vec_pop(&p->exits);
4323 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004324
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004325 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004326}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004327#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004328
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004329static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004330{
4331 BcStatus s;
4332 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004333
Gavin Howard01055ba2018-11-03 11:00:21 -06004334 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004335 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4336 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004337 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004338 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004339 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004340 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004341
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004342 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004343
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004344 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004345 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004346
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004347 if (p->l.lex != BC_LEX_SCOLON && p->l.lex != XC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004348 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004349
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004350 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004351}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004352#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004353
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004354static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4355{
4356 BcId *autoid;
4357 BcId a;
4358 size_t i;
4359
4360 autoid = (void*)f->autos.v;
4361 for (i = 0; i < f->autos.len; i++, autoid++) {
4362 if (strcmp(name, autoid->name) == 0)
4363 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
4364 }
4365
4366 a.idx = var;
4367 a.name = name;
4368
4369 bc_vec_push(&f->autos, &a);
4370
4371 RETURN_STATUS(BC_STATUS_SUCCESS);
4372}
4373#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4374
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004375static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004376{
4377 BcStatus s;
4378 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004379 char *name;
4380
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004381 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004382 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004383 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004384 if (p->l.lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004385 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004386
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004387 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004388 p->fidx = bc_program_addFunc(name);
4389 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004390
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004391 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004392 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004393 if (p->l.lex != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004394 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004395 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004396 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004397
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004398 while (p->l.lex != BC_LEX_RPAREN) {
4399 if (p->l.lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004400 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004401
4402 ++p->func->nparams;
4403
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004404 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004405 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004406 if (s) goto err;
4407
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004408 var = p->l.lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004409
4410 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004411 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004412 if (s) goto err;
4413
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004414 if (p->l.lex != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004415 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004416 goto err;
4417 }
4418
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004419 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004420 if (s) goto err;
4421 }
4422
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004423 comma = p->l.lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004424 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004425 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004426 if (s) goto err;
4427 }
4428
Denys Vlasenko29301232018-12-11 15:29:32 +01004429 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004430 if (s) goto err;
4431 }
4432
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004433 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004434
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004435 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004436 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004437
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004438 if (p->l.lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004439 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004440 if (s) RETURN_STATUS(s);
4441 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004442
Denys Vlasenko202dd192018-12-16 17:30:35 +01004443 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4444 s = zbc_lex_skip_if_at_NLINE(&p->l);
4445 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004446 //GNU bc requires a {} block even if function body has single stmt, enforce this?
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004447 if (p->l.lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004448 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004449
4450 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004451 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004452 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004453 if (s) RETURN_STATUS(s);
4454
4455 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004456
4457 // Subsequent code generation is into main program
4458 p->fidx = BC_PROG_MAIN;
4459 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004460
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004461 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004462 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004463 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004464 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004465 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004466 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004467}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004468#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004469
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004470static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004471{
4472 BcStatus s;
4473 bool comma, var, one;
4474 char *name;
4475
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004476 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004477 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004478 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004479
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004480 comma = false;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004481 one = p->l.lex == XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06004482
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004483 while (p->l.lex == XC_LEX_NAME) {
4484 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004485 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004486 if (s) goto err;
4487
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004488 var = p->l.lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004489 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004490 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004491 if (s) goto err;
4492
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004493 if (p->l.lex != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004494 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004495 goto err;
4496 }
4497
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004498 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004499 if (s) goto err;
4500 }
4501
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004502 comma = p->l.lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004503 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004504 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004505 if (s) goto err;
4506 }
4507
Denys Vlasenko29301232018-12-11 15:29:32 +01004508 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004509 if (s) goto err;
4510 }
4511
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004512 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4513 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004514
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004515 if (p->l.lex != XC_LEX_NLINE && p->l.lex != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004516 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004517
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004518 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004519 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004520 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004521 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004522 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004523 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004524}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004525#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004526
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004527#undef zbc_parse_stmt_possibly_auto
4528static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004529{
4530 BcStatus s = BC_STATUS_SUCCESS;
4531
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004532 dbg_lex_enter("%s:%d entered, p->l.lex:%d", __func__, __LINE__, p->l.lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004533
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004534 if (p->l.lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004535 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004536 RETURN_STATUS(zbc_lex_next(&p->l));
4537 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004538 if (p->l.lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004539 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4540 RETURN_STATUS(zbc_lex_next(&p->l));
4541 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004542
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004543 if (p->l.lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004544 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4545 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004546 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004547 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004548 } while (p->l.lex == XC_LEX_NLINE);
4549 if (auto_allowed && p->l.lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004550 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4551 s = zbc_parse_auto(p);
4552 if (s) RETURN_STATUS(s);
4553 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004554 while (p->l.lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004555 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4556 s = zbc_parse_stmt(p);
4557 if (s) RETURN_STATUS(s);
4558 }
4559 s = zbc_lex_next(&p->l);
4560 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4561 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004562 }
4563
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004564 dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex);
4565 switch (p->l.lex) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004566 case XC_LEX_OP_MINUS:
Gavin Howard01055ba2018-11-03 11:00:21 -06004567 case BC_LEX_OP_INC:
4568 case BC_LEX_OP_DEC:
Gavin Howard01055ba2018-11-03 11:00:21 -06004569 case BC_LEX_OP_BOOL_NOT:
4570 case BC_LEX_LPAREN:
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004571 case XC_LEX_NAME:
4572 case XC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06004573 case BC_LEX_KEY_IBASE:
4574 case BC_LEX_KEY_LAST:
4575 case BC_LEX_KEY_LENGTH:
4576 case BC_LEX_KEY_OBASE:
4577 case BC_LEX_KEY_READ:
4578 case BC_LEX_KEY_SCALE:
4579 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004580 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004581 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004582 case XC_LEX_STR:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004583 s = zbc_parse_pushSTR(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004584 bc_parse_push(p, XC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004585 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004586 case BC_LEX_KEY_BREAK:
4587 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004588 s = zbc_parse_break_or_continue(p, p->l.lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004589 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004590 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004591 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004592 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004593 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004594 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004595 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004596 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004597 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004598 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004599 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004600 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004601 // "limits" is a compile-time command,
4602 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004603 printf(
4604 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4605 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4606 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4607 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4608 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4609 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4610 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4611 "Number of vars = "BC_MAX_VARS_STR "\n"
4612 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004613 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004614 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004615 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004616 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004617 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004618 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004619 // "quit" is a compile-time command. For example,
4620 // "if (0 == 1) quit" terminates when parsing the statement,
4621 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004622 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004623 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004624 if (!p->in_funcdef)
4625 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004626 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004627 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004628 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004629 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004630 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004631 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004632 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004633 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004634 }
4635
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004636 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004637 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004638}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004639#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004640
Denys Vlasenko99b37622018-12-15 20:06:59 +01004641static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004642{
4643 BcStatus s;
4644
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004645 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004646 if (p->l.lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004647 s = bc_error("end of file");
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004648 else if (p->l.lex == BC_LEX_KEY_DEFINE) {
4649 dbg_lex("%s:%d p->l.lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004650 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004651 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004652 dbg_lex("%s:%d p->l.lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.lex);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004653 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004654 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004655
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004656 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004657 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004658}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004659#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004660
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004661// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004662static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004663{
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004664 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004665 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004666 unsigned nparens, nrelops;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004667 bool paren_first, paren_expr, rprn, assign, bin_last;
Gavin Howard01055ba2018-11-03 11:00:21 -06004668
Denys Vlasenko17df8822018-12-14 23:00:24 +01004669 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004670 paren_first = (p->l.lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004671 nparens = nrelops = 0;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004672 paren_expr = rprn = assign = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004673 bin_last = true;
4674
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004675 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004676 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004677 BcStatus s;
4678 BcLexType t = p->l.lex;
4679
4680 if (!lex_allowed_in_bc_expr(t))
4681 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004682
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004683 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004684 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004685 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004686 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004687 case BC_LEX_OP_INC:
4688 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004689 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004690 rprn = bin_last = false;
4691 //get_token = false; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06004692 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004693 case XC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004694 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004695 rprn = false;
4696 //get_token = false; - already is
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004697 bin_last = (prev == XC_INST_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004698 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004699 case BC_LEX_OP_ASSIGN_POWER:
4700 case BC_LEX_OP_ASSIGN_MULTIPLY:
4701 case BC_LEX_OP_ASSIGN_DIVIDE:
4702 case BC_LEX_OP_ASSIGN_MODULUS:
4703 case BC_LEX_OP_ASSIGN_PLUS:
4704 case BC_LEX_OP_ASSIGN_MINUS:
4705 case BC_LEX_OP_ASSIGN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004706 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4707 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4708 && prev != XC_INST_OBASE && prev != BC_INST_LAST
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004709 ) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004710 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004711 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004712 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004713 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004714 break;
4715 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004716 // Fallthrough.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004717 case XC_LEX_OP_POWER:
4718 case XC_LEX_OP_MULTIPLY:
4719 case XC_LEX_OP_DIVIDE:
4720 case XC_LEX_OP_MODULUS:
4721 case XC_LEX_OP_PLUS:
4722 case XC_LEX_OP_REL_EQ:
4723 case XC_LEX_OP_REL_LE:
4724 case XC_LEX_OP_REL_GE:
4725 case XC_LEX_OP_REL_NE:
4726 case XC_LEX_OP_REL_LT:
4727 case XC_LEX_OP_REL_GT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004728 case BC_LEX_OP_BOOL_NOT:
4729 case BC_LEX_OP_BOOL_OR:
4730 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004731 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004732 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004733 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004734 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004735 }
Denys Vlasenko69560f42018-12-24 14:14:23 +01004736 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
Denys Vlasenko0154d782018-12-14 23:32:51 +01004737 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004738 bc_parse_operator(p, t, ops_bgn, &nexprs);
4739 s = zbc_lex_next(&p->l);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004740 rprn = false;
4741 //get_token = false; - already is
Denys Vlasenko69560f42018-12-24 14:14:23 +01004742 bin_last = (t != BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004743 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004744 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004745 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004746 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004747 bc_vec_push(&p->ops, &t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004748 nparens++;
4749 get_token = true;
4750 paren_expr = false;
4751 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004752 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004753 case BC_LEX_RPAREN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004754 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004755 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004756 if (nparens == 0) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004757 goto exit_loop;
Gavin Howard01055ba2018-11-03 11:00:21 -06004758 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004759 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004760 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004761 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004762 }
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004763 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004764 nparens--;
Gavin Howard01055ba2018-11-03 11:00:21 -06004765 paren_expr = rprn = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004766 bin_last = false;
4767 //get_token = false; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06004768 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004769 case XC_LEX_NAME:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004770 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004771 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004772 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004773 paren_expr = true;
4774 rprn = bin_last = false;
4775 //get_token = false; - already is
Denys Vlasenkobb116032018-12-25 01:16:37 +01004776 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004777 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004778 case XC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004779 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004780 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004781 bc_parse_pushNUM(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004782 prev = XC_INST_NUM;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004783 get_token = true;
4784 paren_expr = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004785 rprn = bin_last = false;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004786 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004787 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004788 case BC_LEX_KEY_IBASE:
4789 case BC_LEX_KEY_LAST:
4790 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004791 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004792 return bc_error_bad_expression();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004793 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004794 bc_parse_push(p, (char) prev);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004795 get_token = true;
4796 paren_expr = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004797 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004798 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004799 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004800 case BC_LEX_KEY_LENGTH:
4801 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004802 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004803 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004804 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004805 paren_expr = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004806 rprn = bin_last = false;
4807 //get_token = false; - already is
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004808 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004809 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004810 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004811 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004812 return bc_error_bad_expression();
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004813 s = zbc_parse_read(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004814 prev = XC_INST_READ;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004815 paren_expr = true;
4816 rprn = bin_last = false;
4817 //get_token = false; - already is
4818 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004819 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004820 case BC_LEX_KEY_SCALE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004821 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004822 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004823 s = zbc_parse_scale(p, &prev, flags);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004824 prev = XC_INST_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06004825 paren_expr = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004826 rprn = bin_last = false;
4827 //get_token = false; - already is
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004828 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004829 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004830 default:
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004831 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004832 }
4833
Denys Vlasenkobb116032018-12-25 01:16:37 +01004834 if (s || G_interrupt) // error, or ^C: stop parsing
4835 return BC_STATUS_FAILURE;
4836 if (get_token) {
4837 s = zbc_lex_next(&p->l);
4838 if (s) return s;
4839 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004840 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004841 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004842
4843 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004844 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004845 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004846
4847 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004848 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004849
Denys Vlasenko0154d782018-12-14 23:32:51 +01004850 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004851
Denys Vlasenko69560f42018-12-24 14:14:23 +01004852 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004853 bc_vec_pop(&p->ops);
4854 }
4855
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004856 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004857 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004858
Gavin Howard01055ba2018-11-03 11:00:21 -06004859 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004860 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004861 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4862 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004863 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004864 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004865 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4866 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004867 }
4868
4869 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004870 if (paren_first || !assign)
4871 bc_parse_push(p, XC_INST_PRINT);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004872 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004873 }
4874
Denys Vlasenko17df8822018-12-14 23:00:24 +01004875 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004876 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004877}
4878
Gavin Howard01055ba2018-11-03 11:00:21 -06004879#endif // ENABLE_BC
4880
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004881#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004882
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004883static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004884{
4885 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004886
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004887 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004888 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004889 if (p->l.lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004890
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004891 bc_parse_pushName(p, p->l.lex_buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004892
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004893 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004894}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004895#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004896
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004897static void dc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004898{
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004899 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004900 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004901
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004902 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004903
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004904 str = xstrdup(p->l.lex_buf.v);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004905 bc_parse_push(p, XC_INST_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004906 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004907 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004908
4909 // Explanation needed here
4910 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004911 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004912
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004913 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004914}
4915
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004916static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004917{
4918 BcStatus s;
4919
4920 bc_parse_push(p, inst);
4921 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004922 s = zdc_parse_register(p);
4923 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004924 }
4925
4926 if (store) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004927 bc_parse_push(p, DC_INST_SWAP);
4928 bc_parse_push(p, XC_INST_ASSIGN);
4929 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004930 }
4931
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004932 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004933}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004934#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004935
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004936static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004937{
4938 BcStatus s;
4939
4940 bc_parse_push(p, inst);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004941 bc_parse_push(p, DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004942
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004943 s = zdc_parse_register(p);
4944 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004945
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004946 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004947 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004948
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004949 // Note that 'else' part can not be on the next line:
4950 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4951 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004952 if (p->l.lex == DC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004953 s = zdc_parse_register(p);
4954 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004955 s = zbc_lex_next(&p->l);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004956 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004957 bc_parse_push(p, BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004958 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004959
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004960 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004961}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004962#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004963
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004964static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004965{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004966 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004967 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004968 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004969
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004970 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004971 s = BC_STATUS_SUCCESS;
4972 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004973 switch (t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004974 case XC_LEX_OP_REL_EQ:
4975 case XC_LEX_OP_REL_LE:
4976 case XC_LEX_OP_REL_GE:
4977 case XC_LEX_OP_REL_NE:
4978 case XC_LEX_OP_REL_LT:
4979 case XC_LEX_OP_REL_GT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004980 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
Denys Vlasenko69560f42018-12-24 14:14:23 +01004981 s = zdc_parse_cond(p, t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004982 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004983 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004984 case DC_LEX_SCOLON:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004985 case DC_LEX_COLON:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004986 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004987 s = zdc_parse_mem(p, XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004988 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004989 case XC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004990 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004991 dc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004992 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004993 case XC_LEX_NEG:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004994 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
4995 s = zbc_lex_next(&p->l);
4996 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004997 if (p->l.lex != XC_LEX_NUMBER)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004998 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004999 bc_parse_pushNUM(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005000 bc_parse_push(p, XC_INST_NEG);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005001 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01005002 case XC_LEX_NUMBER:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005003 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
5004 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06005005 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005006 case DC_LEX_READ:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005007 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005008 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005009 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005010 case DC_LEX_OP_ASSIGN:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005011 case DC_LEX_STORE_PUSH:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005012 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005013 assign = (t == DC_LEX_OP_ASSIGN);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005014 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005015 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005016 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005017 case DC_LEX_LOAD:
5018 case DC_LEX_LOAD_POP:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005019 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005020 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005021 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005022 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005023 case DC_LEX_STORE_IBASE:
5024 case DC_LEX_STORE_SCALE:
5025 case DC_LEX_STORE_OBASE:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005026 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005027 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005028 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005029 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005030 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005031 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5032 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005033 }
5034
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005035 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005036
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005037 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005038 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005039}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005040#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005041
Denys Vlasenko39287e02018-12-22 02:23:08 +01005042static BC_STATUS zdc_parse_expr(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005043{
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005044 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005045
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01005046 i = (int)p->l.lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005047 if (i >= 0) {
5048 BcInst inst = dc_LEX_to_INST[i];
5049 if (inst != DC_INST_INVALID) {
5050 bc_parse_push(p, inst);
5051 RETURN_STATUS(zbc_lex_next(&p->l));
5052 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005053 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01005054 RETURN_STATUS(zdc_parse_token(p, p->l.lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005055}
5056#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5057
5058static BC_STATUS zdc_parse_exprs_until_eof(BcParse *p)
5059{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01005060 dbg_lex_enter("%s:%d entered, p->l.lex:%d", __func__, __LINE__, p->l.lex);
5061 while (p->l.lex != XC_LEX_EOF) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005062 BcStatus s = zdc_parse_expr(p);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005063 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005064 }
5065
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005066 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005067 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005068}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005069#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005070
Gavin Howard01055ba2018-11-03 11:00:21 -06005071#endif // ENABLE_DC
5072
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005073//
5074// Execution engine
5075//
5076
5077#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5078#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5079
Denys Vlasenkodf515392018-12-02 19:27:48 +01005080static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005081{
Gavin Howard01055ba2018-11-03 11:00:21 -06005082 BcId e, *ptr;
5083 BcVec *v, *map;
5084 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005085 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005086
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005087 v = var ? &G.prog.vars : &G.prog.arrs;
5088 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005089
5090 e.name = id;
5091 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005092 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005093
5094 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005095 BcVec v2;
5096 bc_array_init(&v2, var);
5097 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005098 }
5099
5100 ptr = bc_vec_item(map, i);
5101 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005102 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005103}
5104
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005105// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005106static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005107{
Gavin Howard01055ba2018-11-03 11:00:21 -06005108 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005109 case XC_RESULT_STR:
5110 case XC_RESULT_TEMP:
5111 case XC_RESULT_IBASE:
5112 case XC_RESULT_SCALE:
5113 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005114 *num = &r->d.n;
5115 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005116 case XC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005117 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005118 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005119 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005120 size_t len;
5121
5122 str = *bc_program_const(r->d.id.idx);
5123 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005124
5125 bc_num_init(&r->d.n, len);
5126
5127 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005128 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005129 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005130 if (s) {
5131 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005132 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005133 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005134 *num = &r->d.n;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005135 r->t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005136 break;
5137 }
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005138 case XC_RESULT_VAR:
5139 case XC_RESULT_ARRAY:
5140 case XC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005141 BcVec *v;
5142
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005143 v = bc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005144
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005145 if (r->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005146 v = bc_vec_top(v);
5147 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5148 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005149 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005150 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005151 break;
5152 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005153#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005154 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005155 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005156 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005157 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005158 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005159 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005160#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005161#if SANITY_CHECKS
Denys Vlasenko503faf92018-12-20 16:24:18 +01005162 default:
5163 // Testing the theory that dc does not reach LAST/ONE
5164 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005165#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005166 }
5167
Denys Vlasenko29301232018-12-11 15:29:32 +01005168 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005169}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005170#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005171
Denys Vlasenko29301232018-12-11 15:29:32 +01005172static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005173 BcResult **r, BcNum **rn, bool assign)
5174{
5175 BcStatus s;
5176 bool hex;
5177 BcResultType lt, rt;
5178
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005179 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005180 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005181
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005182 *r = bc_vec_item_rev(&G.prog.results, 0);
5183 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005184
5185 lt = (*l)->t;
5186 rt = (*r)->t;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005187 hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06005188
Denys Vlasenko29301232018-12-11 15:29:32 +01005189 s = zbc_program_num(*l, ln, false);
5190 if (s) RETURN_STATUS(s);
5191 s = zbc_program_num(*r, rn, hex);
5192 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005193
5194 // We run this again under these conditions in case any vector has been
5195 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005196 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005197 s = zbc_program_num(*l, ln, false);
5198 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005199 }
5200
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005201 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005202 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005203 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005204 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005205
Denys Vlasenko29301232018-12-11 15:29:32 +01005206 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005207}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005208#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005209
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005210static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005211{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005212 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005213 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005214 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005215}
5216
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005217// Note: *r and *n need not be initialized by caller
Denys Vlasenko29301232018-12-11 15:29:32 +01005218static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005219{
5220 BcStatus s;
5221
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005222 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005223 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005224 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005225
Denys Vlasenko29301232018-12-11 15:29:32 +01005226 s = zbc_program_num(*r, n, false);
5227 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005228
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005229 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005230 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005231
Denys Vlasenko29301232018-12-11 15:29:32 +01005232 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005233}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005234#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005235
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005236static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005237{
5238 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005239 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005240}
5241
Denys Vlasenko259137d2018-12-11 19:42:05 +01005242static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005243{
5244 BcStatus s;
5245 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005246 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005247
Denys Vlasenko29301232018-12-11 15:29:32 +01005248 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005249 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005250 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005251
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005252 s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005253 IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - XC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005254 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005255 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005256
Denys Vlasenko259137d2018-12-11 19:42:05 +01005257 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005258 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005259 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005260 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005261}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005262#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005263
Denys Vlasenko26819db2018-12-12 16:08:46 +01005264static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005265{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005266 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005267 BcStatus s;
5268 BcParse parse;
5269 BcVec buf;
5270 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005271 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005272
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005273 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005274 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005275
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005276 sv_file = G.prog.file;
5277 G.prog.file = NULL;
5278
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005279 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005280 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005281
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005282 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005283 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005284
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005285 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005286 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005287 if (IS_BC) {
5288 IF_BC(s = zbc_parse_expr(&parse, 0));
5289 } else {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005290 IF_DC(s = zdc_parse_exprs_until_eof(&parse));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005291 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005292 if (s) goto exec_err;
5293
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01005294 if (parse.l.lex != XC_LEX_NLINE && parse.l.lex != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005295 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005296 goto exec_err;
5297 }
5298
5299 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005300 ip.inst_idx = 0;
5301 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005302
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005303 bc_parse_push(&parse, XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005304 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005305 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005306 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005307 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005308 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005309 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005310}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005311#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005312
5313static size_t bc_program_index(char *code, size_t *bgn)
5314{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005315 unsigned char *bytes = (void*)(code + *bgn);
5316 unsigned amt;
5317 unsigned i;
5318 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005319
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005320 amt = *bytes++;
5321 *bgn += amt + 1;
5322
5323 amt *= 8;
5324 res = 0;
5325 for (i = 0; i < amt; i += 8)
5326 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005327
5328 return res;
5329}
5330
5331static char *bc_program_name(char *code, size_t *bgn)
5332{
5333 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005334 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005335
Denys Vlasenko694d2982018-12-18 19:17:11 +01005336 code += *bgn;
5337 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005338 i = 0;
5339 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005340 char c = *code++;
5341 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005342 break;
5343 s[i++] = c;
5344 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005345 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005346 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005347
5348 return s;
5349}
5350
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005351static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005352{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005353#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005354 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005355 // Example: echo '[]ap' | dc
5356 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005357 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005358 return;
5359 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005360#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005361 while (*str) {
5362 int c = *str++;
5363 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005364 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005365 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005366 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005367 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005368 case 'a':
5369 bb_putchar('\a');
5370 break;
5371 case 'b':
5372 bb_putchar('\b');
5373 break;
5374 case '\\':
5375 case 'e':
5376 bb_putchar('\\');
5377 break;
5378 case 'f':
5379 bb_putchar('\f');
5380 break;
5381 case 'n':
5382 bb_putchar('\n');
5383 G.prog.nchars = SIZE_MAX;
5384 break;
5385 case 'r':
5386 bb_putchar('\r');
5387 break;
5388 case 'q':
5389 bb_putchar('"');
5390 break;
5391 case 't':
5392 bb_putchar('\t');
5393 break;
5394 default:
5395 // Just print the backslash and following character.
5396 bb_putchar('\\');
5397 ++G.prog.nchars;
5398 bb_putchar(c);
5399 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005400 }
5401 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005402 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005403 }
5404}
5405
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005406static void bc_num_printNewline(void)
5407{
5408 if (G.prog.nchars == G.prog.len - 1) {
5409 bb_putchar('\\');
5410 bb_putchar('\n');
5411 G.prog.nchars = 0;
5412 }
5413}
5414
5415#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005416static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005417{
5418 (void) radix;
5419 bb_putchar((char) num);
5420 G.prog.nchars += width;
5421}
5422#endif
5423
5424static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5425{
5426 size_t exp, pow;
5427
5428 bc_num_printNewline();
5429 bb_putchar(radix ? '.' : ' ');
5430 ++G.prog.nchars;
5431
5432 bc_num_printNewline();
5433 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5434 continue;
5435
5436 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5437 size_t dig;
5438 bc_num_printNewline();
5439 dig = num / pow;
5440 num -= dig * pow;
5441 bb_putchar(((char) dig) + '0');
5442 }
5443}
5444
5445static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5446{
5447 if (radix) {
5448 bc_num_printNewline();
5449 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005450 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005451 }
5452
5453 bc_num_printNewline();
5454 bb_putchar(bb_hexdigits_upcase[num]);
5455 G.prog.nchars += width;
5456}
5457
5458static void bc_num_printDecimal(BcNum *n)
5459{
5460 size_t i, rdx = n->rdx - 1;
5461
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005462 if (n->neg) {
5463 bb_putchar('-');
5464 G.prog.nchars++;
5465 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005466
5467 for (i = n->len - 1; i < n->len; --i)
5468 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5469}
5470
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005471typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5472
Denys Vlasenkod3401432018-12-18 17:00:35 +01005473static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005474{
5475 BcStatus s;
5476 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005477 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005478 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005479 BcNum intp, fracp, digit, frac_len;
5480 unsigned long dig, *ptr;
5481 size_t i;
5482 bool radix;
5483
5484 if (n->len == 0) {
5485 print(0, width, false);
5486 RETURN_STATUS(BC_STATUS_SUCCESS);
5487 }
5488
5489 bc_vec_init(&stack, sizeof(long), NULL);
5490 bc_num_init(&intp, n->len);
5491 bc_num_init(&fracp, n->rdx);
5492 bc_num_init(&digit, width);
5493 bc_num_init(&frac_len, BC_NUM_INT(n));
5494 bc_num_copy(&intp, n);
5495 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005496 base.cap = ARRAY_SIZE(base_digs);
5497 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005498 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005499
5500 bc_num_truncate(&intp, intp.rdx);
5501 s = zbc_num_sub(n, &intp, &fracp, 0);
5502 if (s) goto err;
5503
5504 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005505 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005506 if (s) goto err;
5507 s = zbc_num_ulong(&digit, &dig);
5508 if (s) goto err;
5509 bc_vec_push(&stack, &dig);
5510 }
5511
5512 for (i = 0; i < stack.len; ++i) {
5513 ptr = bc_vec_item_rev(&stack, i);
5514 print(*ptr, width, false);
5515 }
5516
5517 if (!n->rdx) goto err;
5518
5519 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005520 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005521 if (s) goto err;
5522 s = zbc_num_ulong(&fracp, &dig);
5523 if (s) goto err;
5524 bc_num_ulong2num(&intp, dig);
5525 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5526 if (s) goto err;
5527 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005528 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005529 if (s) goto err;
5530 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005531 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005532 bc_num_free(&frac_len);
5533 bc_num_free(&digit);
5534 bc_num_free(&fracp);
5535 bc_num_free(&intp);
5536 bc_vec_free(&stack);
5537 RETURN_STATUS(s);
5538}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005539#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005540
5541static BC_STATUS zbc_num_printBase(BcNum *n)
5542{
5543 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005544 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005545 BcNumDigitOp print;
5546 bool neg = n->neg;
5547
5548 if (neg) {
5549 bb_putchar('-');
5550 G.prog.nchars++;
5551 }
5552
5553 n->neg = false;
5554
5555 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5556 width = 1;
5557 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005558 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005559 unsigned i = G.prog.ob_t - 1;
5560 width = 0;
5561 for (;;) {
5562 width++;
5563 i /= 10;
5564 if (i == 0)
5565 break;
5566 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005567 print = bc_num_printDigits;
5568 }
5569
Denys Vlasenkod3401432018-12-18 17:00:35 +01005570 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005571 n->neg = neg;
5572
5573 RETURN_STATUS(s);
5574}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005575#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005576
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005577static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5578{
5579 BcStatus s = BC_STATUS_SUCCESS;
5580
5581 bc_num_printNewline();
5582
5583 if (n->len == 0) {
5584 bb_putchar('0');
5585 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005586 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005587 bc_num_printDecimal(n);
5588 else
5589 s = zbc_num_printBase(n);
5590
5591 if (newline) {
5592 bb_putchar('\n');
5593 G.prog.nchars = 0;
5594 }
5595
5596 RETURN_STATUS(s);
5597}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005598#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005599
Denys Vlasenko29301232018-12-11 15:29:32 +01005600static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005601{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005602 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005603 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005604 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005605 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005606
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005607 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005608 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005609
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005610 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005611 s = zbc_program_num(r, &num, false);
5612 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005613
5614 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005615 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005616#if ENABLE_BC
5617 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5618#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005619 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005620 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005621
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005622 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005623 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005624
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005625 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005626 for (;;) {
5627 char c = *str++;
5628 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005629 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005630 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005631 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005632 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005633 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005634 bc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005635 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005636 }
5637 }
5638
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005639 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005640
Denys Vlasenko29301232018-12-11 15:29:32 +01005641 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005642}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005643#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005644
Denys Vlasenko29301232018-12-11 15:29:32 +01005645static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005646{
5647 BcStatus s;
5648 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005649 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005650
Denys Vlasenko29301232018-12-11 15:29:32 +01005651 s = zbc_program_prep(&ptr, &num);
5652 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005653
5654 bc_num_init(&res.d.n, num->len);
5655 bc_num_copy(&res.d.n, num);
5656 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5657
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005658 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005659
Denys Vlasenko29301232018-12-11 15:29:32 +01005660 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005661}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005662#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005663
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005664static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005665{
5666 BcStatus s;
5667 BcResult *opd1, *opd2, res;
5668 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005669 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005670
Denys Vlasenko29301232018-12-11 15:29:32 +01005671 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005672 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005673
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005674 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005675
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005676 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005677 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005678 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005679 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005680 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005681 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005682 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005683 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005684 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005685 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005686 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005687 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005688 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005689 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005690 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005691 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005692 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005693 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005694 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005695 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005696 cond = (cond > 0);
5697 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005698 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005699 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005700 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005701 }
5702 }
5703
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005704 if (cond) bc_num_one(&res.d.n);
5705 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005706
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005707 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005708
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005709 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005710}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005711#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005712
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005713#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005714static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005715{
5716 BcNum n2;
5717 BcResult res;
5718
5719 memset(&n2, 0, sizeof(BcNum));
5720 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005721 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005722
5723 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005724 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005725 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005726 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005727 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005728 }
5729
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005730 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005731 bc_vec_push(v, &n2);
5732
Denys Vlasenko29301232018-12-11 15:29:32 +01005733 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005734}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005735#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005736#endif // ENABLE_DC
5737
Denys Vlasenko29301232018-12-11 15:29:32 +01005738static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005739{
5740 BcStatus s;
5741 BcResult *ptr, r;
5742 BcVec *v;
5743 BcNum *n;
5744
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005745 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005746 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005747
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005748 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005749 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005750 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005751 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005752
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005753#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005754 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005755 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005756 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005757 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005758#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005759
Denys Vlasenko29301232018-12-11 15:29:32 +01005760 s = zbc_program_num(ptr, &n, false);
5761 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005762
5763 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005764 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005765
5766 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005767 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005768 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005769 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005770 bc_array_init(&r.d.v, true);
5771 bc_array_copy(&r.d.v, (BcVec *) n);
5772 }
5773
5774 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005775 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005776
Denys Vlasenko29301232018-12-11 15:29:32 +01005777 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005778}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005779#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005780
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005781static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005782{
5783 BcStatus s;
5784 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005785 BcNum *l, *r;
5786 bool assign = (inst == XC_INST_ASSIGN);
5787 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005788
Denys Vlasenko29301232018-12-11 15:29:32 +01005789 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005790 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005791
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005792 ib = left->t == XC_RESULT_IBASE;
5793 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005794
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005795#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005796 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005797 BcVec *v;
5798
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005799 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005800 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005801 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005802
Denys Vlasenkofa210792018-12-19 19:35:40 +01005803 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005804 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005805#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005806
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005807 if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005808 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005809 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005810 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005811 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005812
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005813#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005814 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005815 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005816
5817 if (assign)
5818 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005819 else {
5820 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005821 IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005822 }
5823 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005824#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005825 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005826#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005827
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005828 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005829 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005830 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5831 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5832 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005833 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005834 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005835 size_t max;
5836 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005837
Denys Vlasenko29301232018-12-11 15:29:32 +01005838 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005839 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005840 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005841 if (sc) {
5842 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005843 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005844 } else {
5845 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005846 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005847 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005848 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005849 }
5850
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005851 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005852 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005853
5854 *ptr = (size_t) val;
5855 s = BC_STATUS_SUCCESS;
5856 }
5857
5858 bc_num_init(&res.d.n, l->len);
5859 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005860 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005861
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005862 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005863}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005864#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005865
Denys Vlasenko416ce762018-12-02 20:57:17 +01005866#if !ENABLE_DC
5867#define bc_program_pushVar(code, bgn, pop, copy) \
5868 bc_program_pushVar(code, bgn)
5869// for bc, 'pop' and 'copy' are always false
5870#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005871static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005872 bool pop, bool copy)
5873{
Gavin Howard01055ba2018-11-03 11:00:21 -06005874 BcResult r;
5875 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005876
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005877 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005878 r.d.id.name = name;
5879
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005880#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005881 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005882 BcVec *v = bc_program_search(name, true);
5883 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005884
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005885 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005886 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005887 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005888 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005889
5890 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005891 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005892 bc_num_init_DEF_SIZE(&r.d.n);
5893 bc_num_copy(&r.d.n, num);
5894 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005895 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005896 r.d.id.idx = num->rdx;
5897 }
5898
5899 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005900 }
5901#endif // ENABLE_DC
5902
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005903 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005904
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005905 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005906}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005907#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005908
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005909static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005910{
5911 BcStatus s = BC_STATUS_SUCCESS;
5912 BcResult r;
5913 BcNum *num;
5914
5915 r.d.id.name = bc_program_name(code, bgn);
5916
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005917 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005918 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005919 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005920 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005921 BcResult *operand;
5922 unsigned long temp;
5923
Denys Vlasenko29301232018-12-11 15:29:32 +01005924 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005925 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005926 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005927 if (s) goto err;
5928
5929 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005930 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005931 goto err;
5932 }
5933
5934 r.d.id.idx = (size_t) temp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005935 bc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005936 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005937 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005938 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005939 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005940}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005941#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005942
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005943#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005944static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005945{
5946 BcStatus s;
5947 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005948 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005949 char inst2 = inst;
5950
Denys Vlasenko29301232018-12-11 15:29:32 +01005951 s = zbc_program_prep(&ptr, &num);
5952 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005953
5954 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005955 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005956 bc_num_init(&copy.d.n, num->len);
5957 bc_num_copy(&copy.d.n, num);
5958 }
5959
5960 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005961 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5962 ? BC_INST_ASSIGN_PLUS
5963 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005964
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005965 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005966 s = zbc_program_assign(inst);
5967 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005968
5969 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005970 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005971 }
5972
Denys Vlasenko29301232018-12-11 15:29:32 +01005973 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005974}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005975#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005976
Denys Vlasenko29301232018-12-11 15:29:32 +01005977static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005978{
Gavin Howard01055ba2018-11-03 11:00:21 -06005979 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005980 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005981 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005982 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005983 BcResult *arg;
5984
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005985 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005986 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005987 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005988 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005989
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005990 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005991 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005992 }
5993 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005994 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005995 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005996 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005997
5998 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005999 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006000
6001 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006002 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006003
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006004 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006005 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006006
Denys Vlasenko29301232018-12-11 15:29:32 +01006007 s = zbc_program_copyToVar(a->name, a->idx);
6008 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006009 }
6010
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006011 a = bc_vec_item(&func->autos, i);
6012 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006013 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006014
Denys Vlasenkodf515392018-12-02 19:27:48 +01006015 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006016 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006017 BcNum n2;
6018 bc_num_init_DEF_SIZE(&n2);
6019 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006020 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006021 BcVec v2;
6022 bc_array_init(&v2, true);
6023 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006024 }
6025 }
6026
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006027 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006028
Denys Vlasenko29301232018-12-11 15:29:32 +01006029 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006030}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006031#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006032
Denys Vlasenko29301232018-12-11 15:29:32 +01006033static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006034{
Gavin Howard01055ba2018-11-03 11:00:21 -06006035 BcResult res;
6036 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006037 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006038 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006039 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006040
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006041 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 +01006042 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006043
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006044 f = bc_program_func(ip->func);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006045 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006046
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006047 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006048 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006049 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006050 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006051
Denys Vlasenko29301232018-12-11 15:29:32 +01006052 s = zbc_program_num(operand, &num, false);
6053 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006054 bc_num_init(&res.d.n, num->len);
6055 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006056 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006057 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006058 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006059 }
6060
6061 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006062 a = (void*)f->autos.v;
6063 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006064 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006065 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006066 bc_vec_pop(v);
6067 }
6068
Denys Vlasenko24e41942018-12-21 23:01:26 +01006069 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006070 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006071 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006072
Denys Vlasenko29301232018-12-11 15:29:32 +01006073 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006074}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006075#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006076#endif // ENABLE_BC
6077
6078static unsigned long bc_program_scale(BcNum *n)
6079{
6080 return (unsigned long) n->rdx;
6081}
6082
6083static unsigned long bc_program_len(BcNum *n)
6084{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006085 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006086
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006087 if (n->rdx != len) return len;
6088 for (;;) {
6089 if (len == 0) break;
6090 len--;
6091 if (n->num[len] != 0) break;
6092 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006093 return len;
6094}
6095
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006096static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006097{
6098 BcStatus s;
6099 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006100 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006101 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006102 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006103
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006104 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006105 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006106 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006107
Denys Vlasenko29301232018-12-11 15:29:32 +01006108 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006109 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006110
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006111#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006112 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006113 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006114#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006115
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006116 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006117
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006118 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006119 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006120#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006121 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006122 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006123 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006124#endif
6125#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006126 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006127 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006128 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006129
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006130 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006131 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006132 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006133#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006134 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006135 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006136 }
6137
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006138 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006139
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006140 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006141}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006142#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006143
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006144#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006145static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006146{
6147 BcStatus s;
6148 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006149 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006150
Denys Vlasenko29301232018-12-11 15:29:32 +01006151 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006152 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006153
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006154 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006155 bc_num_init(&res2.d.n, n2->len);
6156
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006157 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006158 if (s) goto err;
6159
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006160 bc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006161 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006162 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006163
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006164 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006165 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006166 bc_num_free(&res2.d.n);
6167 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006168 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006169}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006170#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006171
Denys Vlasenkofa210792018-12-19 19:35:40 +01006172static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006173{
6174 BcStatus s;
6175 BcResult *r1, *r2, *r3, res;
6176 BcNum *n1, *n2, *n3;
6177
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006178 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006179 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006180 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006181 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006182
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006183 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006184 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006185 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006186 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006187 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006188
6189 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006190 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006191 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006192 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006193 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006194 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006195 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006196 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006197 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006198 }
6199 }
6200
6201 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006202 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203 if (s) goto err;
6204
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006205 bc_vec_pop(&G.prog.results);
6206 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006207
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006208 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006209 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006210 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006211 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006212}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006213#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006214
Denys Vlasenkofa210792018-12-19 19:35:40 +01006215static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006216{
Gavin Howard01055ba2018-11-03 11:00:21 -06006217 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006218 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006219
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006220 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006221
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006222 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006223 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006224 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006225}
6226
Denys Vlasenkofa210792018-12-19 19:35:40 +01006227static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006228{
6229 BcStatus s;
6230 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006231 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006232 char **strs;
6233 char *str;
6234 char c;
6235 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006236
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006237 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006238 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006239 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006240
Denys Vlasenko29301232018-12-11 15:29:32 +01006241 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006242 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006243
6244 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006245 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006246 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006247 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006248
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006249 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006250 bc_num_copy(&n, num);
6251 bc_num_truncate(&n, n.rdx);
6252
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006253 strmb.cap = ARRAY_SIZE(strmb_digs);
6254 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006255 bc_num_ulong2num(&strmb, 0x100);
6256 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006257
Gavin Howard01055ba2018-11-03 11:00:21 -06006258 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006259 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006260 if (s) goto num_err;
6261
6262 c = (char) val;
6263
6264 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006265 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006266 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006267 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006268 sp = *bc_program_str(idx);
6269 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006270 }
6271
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006272 strs = (void*)G.prog.strs.v;
6273 for (idx = 0; idx < G.prog.strs.len; idx++) {
6274 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6275 goto dup;
6276 }
6277 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006278 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006279 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006280 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006281 bc_vec_push(&G.prog.strs, &str);
6282 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006283 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006284 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006285 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006286
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006287 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006288 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006289 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006290 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006291}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006292#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006293
Denys Vlasenkofa210792018-12-19 19:35:40 +01006294static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006295{
6296 BcStatus s;
6297 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006298 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006299 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006300
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006301 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006302 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006303 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006304
Denys Vlasenko29301232018-12-11 15:29:32 +01006305 s = zbc_program_num(r, &n, false);
6306 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006307
Denys Vlasenko57734c92018-12-17 21:14:05 +01006308 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006309 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006310 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006311 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006312 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006313 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006314 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006315 }
6316
Denys Vlasenko29301232018-12-11 15:29:32 +01006317 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006318}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006319#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006320
Denys Vlasenkofa210792018-12-19 19:35:40 +01006321static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006322{
6323 BcStatus s;
6324 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006325 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006326 unsigned long val;
6327
Denys Vlasenko29301232018-12-11 15:29:32 +01006328 s = zbc_program_prep(&opnd, &num);
6329 if (s) RETURN_STATUS(s);
6330 s = zbc_num_ulong(num, &val);
6331 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006332
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006333 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006334
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006335 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006336 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006337 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006338 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006339 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006340
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006341 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006342
Denys Vlasenko29301232018-12-11 15:29:32 +01006343 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006344}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006345#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006346
Denys Vlasenkofa210792018-12-19 19:35:40 +01006347static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006348{
6349 BcStatus s = BC_STATUS_SUCCESS;
6350 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006351 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006352 BcInstPtr ip;
6353 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006354
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006355 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006356 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006357
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006358 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006359
6360 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006361 BcNum *n = n; // for compiler
6362 bool exec;
6363 char *name;
6364 char *then_name = bc_program_name(code, bgn);
6365 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006366
6367 if (code[*bgn] == BC_PARSE_STREND)
6368 (*bgn) += 1;
6369 else
6370 else_name = bc_program_name(code, bgn);
6371
6372 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006373 name = then_name;
6374 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006375 exec = true;
6376 name = else_name;
6377 }
6378
6379 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006380 BcVec *v;
6381 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006382 n = bc_vec_top(v);
6383 }
6384
6385 free(then_name);
6386 free(else_name);
6387
6388 if (!exec) goto exit;
6389 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006390 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006391 goto exit;
6392 }
6393
6394 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006395 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006396 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006397 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006398 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006399 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006400 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006401 if (s || !BC_PROG_STR(n)) goto exit;
6402 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006403 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006404 goto exit;
6405 }
6406
6407 fidx = sidx + BC_PROG_REQ_FUNCS;
6408
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006409 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006410
6411 if (f->code.len == 0) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006412 FILE *sv_input_fp;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006413 BcParse prs;
6414 char *str;
6415
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006416 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006417 str = *bc_program_str(sidx);
6418 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006419 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006420
6421 sv_input_fp = G.input_fp;
6422 G.input_fp = NULL; // "do not read from input file when <EOL> reached"
6423 s = zdc_parse_exprs_until_eof(&prs);
6424 G.input_fp = sv_input_fp;
6425
Gavin Howard01055ba2018-11-03 11:00:21 -06006426 if (s) goto err;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006427 if (prs.l.lex != XC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006428 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006429 err:
6430 bc_parse_free(&prs);
6431 bc_vec_pop_all(&f->code);
6432 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006433 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006434 bc_parse_push(&prs, DC_INST_POP_EXEC);
Gavin Howard01055ba2018-11-03 11:00:21 -06006435 bc_parse_free(&prs);
6436 }
6437
Denys Vlasenko24e41942018-12-21 23:01:26 +01006438 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006439 ip.func = fidx;
6440
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006441 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006442 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006443
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006444 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006445 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006446 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006447 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006448}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006449#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006450#endif // ENABLE_DC
6451
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006452static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006453{
Gavin Howard01055ba2018-11-03 11:00:21 -06006454 BcResult res;
6455 unsigned long val;
6456
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006457 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006458 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006459 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006460 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006461 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006462 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006463 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006464
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006465 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006466 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006467 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006468}
6469
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006470static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006471{
Gavin Howard01055ba2018-11-03 11:00:21 -06006472 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006473 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006474 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006475 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006476
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006477 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006478 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6479 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006480 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006481 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006482
Denys Vlasenko24e41942018-12-21 23:01:26 +01006483 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 -06006484 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006485#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006486 case BC_INST_JUMP_ZERO: {
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006487 BcNum *num;
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006488 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006489 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006490 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006491 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006492 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006493 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006494 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006495 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006496 break;
6497 }
6498 // else: fall through
6499 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006500 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006501 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006502 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006503 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006504 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006505 break;
6506 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006507 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006508 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006509 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006510 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006511 case BC_INST_INC_PRE:
6512 case BC_INST_DEC_PRE:
6513 case BC_INST_INC_POST:
6514 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006515 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006516 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006517 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006518 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006519 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006520 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006521 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006522 case XC_INST_RET:
Gavin Howard01055ba2018-11-03 11:00:21 -06006523 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006524 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006525 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006526 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006527 case XC_INST_BOOL_OR:
6528 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006529#endif // ENABLE_BC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006530 case XC_INST_REL_EQ:
6531 case XC_INST_REL_LE:
6532 case XC_INST_REL_GE:
6533 case XC_INST_REL_NE:
6534 case XC_INST_REL_LT:
6535 case XC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006536 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006537 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006538 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006539 case XC_INST_READ:
6540 dbg_exec("XC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006541 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006542 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006543 case XC_INST_VAR:
6544 dbg_exec("XC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006545 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006546 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006547 case XC_INST_ARRAY_ELEM:
6548 case XC_INST_ARRAY:
6549 dbg_exec("XC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006550 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006551 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006552#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006553 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006554 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006555 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006556 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006557 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006558#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006559 case XC_INST_IBASE:
6560 case XC_INST_OBASE:
6561 case XC_INST_SCALE:
6562 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006563 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006564 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006565 case XC_INST_SCALE_FUNC:
6566 case XC_INST_LENGTH:
6567 case XC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006568 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006569 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006570 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006571 case XC_INST_NUM:
6572 dbg_exec("XC_INST_NUM:");
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006573 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006574 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006575 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006576 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006577 case XC_INST_POP:
6578 dbg_exec("XC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006579 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006580 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006581 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006582 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006583 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006584 case XC_INST_PRINT:
6585 case XC_INST_PRINT_POP:
6586 case XC_INST_PRINT_STR:
6587 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006588 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006589 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006590 case XC_INST_STR:
6591 dbg_exec("XC_INST_STR:");
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006592 r.t = XC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006593 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006594 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006595 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006596 case XC_INST_POWER:
6597 case XC_INST_MULTIPLY:
6598 case XC_INST_DIVIDE:
6599 case XC_INST_MODULUS:
6600 case XC_INST_PLUS:
6601 case XC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006602 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006603 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006604 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006605 case XC_INST_BOOL_NOT: {
6606 BcNum *num;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006607 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006608 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006609 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006610 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006611 if (bc_num_cmp(num, &G.prog.zero) == 0)
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006612 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006613 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006614 bc_program_retire(&r, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006615 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006616 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006617 case XC_INST_NEG:
6618 dbg_exec("XC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006619 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006620 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006621#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006622 case BC_INST_ASSIGN_POWER:
6623 case BC_INST_ASSIGN_MULTIPLY:
6624 case BC_INST_ASSIGN_DIVIDE:
6625 case BC_INST_ASSIGN_MODULUS:
6626 case BC_INST_ASSIGN_PLUS:
6627 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006628#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006629 case XC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006630 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006631 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006632 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006633#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006634 case DC_INST_POP_EXEC:
6635 dbg_exec("DC_INST_POP_EXEC:");
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006636 bc_vec_pop(&G.prog.exestack);
6637 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006638 case DC_INST_MODEXP:
6639 dbg_exec("DC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006640 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006641 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006642 case DC_INST_DIVMOD:
6643 dbg_exec("DC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006644 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006645 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006646 case DC_INST_EXECUTE:
6647 case DC_INST_EXEC_COND:
6648 dbg_exec("DC_INST_EXEC[_COND]:");
6649 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006650 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006651 case DC_INST_PRINT_STACK: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006652 size_t idx;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006653 dbg_exec("DC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006654 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006655 s = zbc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006656 if (s) break;
6657 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006658 break;
6659 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006660 case DC_INST_CLEAR_STACK:
6661 dbg_exec("DC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006662 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006663 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006664 case DC_INST_STACK_LEN:
6665 dbg_exec("DC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006666 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006667 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006668 case DC_INST_DUPLICATE:
6669 dbg_exec("DC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006670 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006671 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006672 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006673 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006674 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006675 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006676 case DC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006677 BcResult *ptr2;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006678 dbg_exec("DC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006679 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006680 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006681 ptr = bc_vec_item_rev(&G.prog.results, 0);
6682 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006683 memcpy(&r, ptr, sizeof(BcResult));
6684 memcpy(ptr, ptr2, sizeof(BcResult));
6685 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006686 break;
6687 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006688 case DC_INST_ASCIIFY:
6689 dbg_exec("DC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006690 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006691 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006692 case DC_INST_PRINT_STREAM:
6693 dbg_exec("DC_INST_PRINT_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006694 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006695 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006696 case DC_INST_LOAD:
6697 case DC_INST_PUSH_VAR: {
6698 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006699 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006700 break;
6701 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006702 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006703 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006704 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006705 free(name);
6706 break;
6707 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006708 case DC_INST_QUIT:
6709 dbg_exec("DC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006710 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006711 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006712 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006713 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006714 case DC_INST_NQUIT:
6715 dbg_exec("DC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006716 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006717 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006718#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006719 read_updated_ip:
6720 // Instruction stack has changed, read new pointers
6721 ip = bc_vec_top(&G.prog.exestack);
6722 func = bc_program_func(ip->func);
6723 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006724 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006725 }
6726
Denys Vlasenkod38af482018-12-04 19:11:02 +01006727 if (s || G_interrupt) {
6728 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006729 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006730 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006731
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006732 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006733 }
6734
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006735 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006736}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006737#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006738
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006739static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006740{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006741 char *lenv;
6742 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006743
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006744 lenv = getenv(var);
6745 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006746 if (!lenv) return len;
6747
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006748 len = bb_strtou(lenv, NULL, 10) - 1;
6749 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006750 len = BC_NUM_PRINT_WIDTH;
6751
6752 return len;
6753}
6754
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006755static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006756{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006757 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006758
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006759 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006760 s = zbc_parse_text_init(&G.prs, text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006761 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006762
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006763 while (G.prs.l.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006764 BcInstPtr *ip;
6765 BcFunc *f;
6766
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006767 dbg_lex("%s:%d G.prs.l.lex:%d, parsing...", __func__, __LINE__, G.prs.l.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006768 if (IS_BC) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006769// FIXME: "eating" of stmt delimiters is coded inconsistently
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006770// (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't),
6771// which causes bugs such as "print 1 print 2" erroneously accepted,
6772// or "print 1 else 2" detecting parse error only after executing
6773// "print 1" part.
6774 IF_BC(s = zbc_parse_stmt_or_funcdef(&G.prs));
6775 } else {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006776 // Most of dc parsing assumes all whitespace,
6777 // including '\n', is eaten.
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006778 while (G.prs.l.lex == XC_LEX_NLINE) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006779 s = zbc_lex_next(&G.prs.l);
6780 if (s) goto err;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006781 if (G.prs.l.lex == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006782 goto done;
6783 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006784 IF_DC(s = zdc_parse_expr(&G.prs));
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006785 }
6786 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006787 err:
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006788 bc_parse_reset(&G.prs); // includes bc_program_reset()
6789 RETURN_STATUS(BC_STATUS_FAILURE);
6790 }
6791
Denys Vlasenko39287e02018-12-22 02:23:08 +01006792 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006793 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006794 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006795 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006796 break;
6797 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006798
6799 ip = (void*)G.prog.exestack.v;
6800#if SANITY_CHECKS
6801 if (G.prog.exestack.len != 1) // should have only main's IP
6802 bb_error_msg_and_die("BUG:call stack");
6803 if (ip->func != BC_PROG_MAIN)
6804 bb_error_msg_and_die("BUG:not MAIN");
6805#endif
6806 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006807 // bc discards strings, constants and code after each
6808 // top-level statement in the "main program".
6809 // This prevents "yes 1 | bc" from growing its memory
6810 // without bound. This can be done because data stack
6811 // is empty and thus can't hold any references to
6812 // strings or constants, there is no generated code
6813 // which can hold references (after we discard one
6814 // we just executed). Code of functions can have references,
6815 // but bc stores function strings/constants in per-function
6816 // storage.
6817 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006818#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006819 if (G.prog.results.len != 0) // should be empty
6820 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006821#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006822 IF_BC(bc_vec_pop_all(&f->strs);)
6823 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006824 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006825 if (G.prog.results.len == 0
6826 && G.prog.vars.len == 0
6827 ) {
6828 // If stack is empty and no registers exist (TODO: or they are all empty),
6829 // we can get rid of accumulated strings and constants.
6830 // In this example dc process should not grow
6831 // its memory consumption with time:
6832 // yes 1pc | dc
6833 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6834 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6835 }
6836 // The code is discarded always (below), thus this example
6837 // should also not grow its memory consumption with time,
6838 // even though its data stack is not empty:
6839 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006840 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006841 // We drop generated and executed code for both bc and dc:
6842 bc_vec_pop_all(&f->code);
6843 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006844 }
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006845 done:
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006846 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006847 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006848}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006849#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006850
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006851static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006852{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006853 // So far bc/dc have no way to include a file from another file,
6854 // therefore we know G.prog.file == NULL on entry
6855 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006856 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006857
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006858 G.prog.file = filename;
6859 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006860 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006861
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006862 do {
6863 s = zbc_vm_process("");
6864 // We do not stop looping on errors here if reading stdin.
6865 // Example: start interactive bc and enter "return".
6866 // It should say "'return' not in a function"
6867 // but should not exit.
6868 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006869 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006870 RETURN_STATUS(s);
6871}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006872#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006873
6874static BC_STATUS zbc_vm_file(const char *file)
6875{
6876 BcStatus s;
6877 FILE *fp;
6878
6879 fp = xfopen_for_read(file);
6880 s = zbc_vm_execute_FILE(fp, file);
6881 fclose(fp);
6882
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006883 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006884}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006885#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006886
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006887#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006888static void bc_vm_info(void)
6889{
6890 printf("%s "BB_VER"\n"
6891 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6892 , applet_name);
6893}
6894
6895static void bc_args(char **argv)
6896{
6897 unsigned opts;
6898 int i;
6899
6900 GETOPT_RESET();
6901#if ENABLE_FEATURE_BC_LONG_OPTIONS
6902 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6903 "warn\0" No_argument "w"
6904 "version\0" No_argument "v"
6905 "standard\0" No_argument "s"
6906 "quiet\0" No_argument "q"
6907 "mathlib\0" No_argument "l"
6908 "interactive\0" No_argument "i"
6909 );
6910#else
6911 opts = option_mask32 |= getopt32(argv, "wvsqli");
6912#endif
6913 if (getenv("POSIXLY_CORRECT"))
6914 option_mask32 |= BC_FLAG_S;
6915
6916 if (opts & BC_FLAG_V) {
6917 bc_vm_info();
6918 exit(0);
6919 }
6920
6921 for (i = optind; argv[i]; ++i)
6922 bc_vec_push(&G.files, argv + i);
6923}
6924
6925static void bc_vm_envArgs(void)
6926{
6927 BcVec v;
6928 char *buf;
6929 char *env_args = getenv("BC_ENV_ARGS");
6930
6931 if (!env_args) return;
6932
6933 G.env_args = xstrdup(env_args);
6934 buf = G.env_args;
6935
6936 bc_vec_init(&v, sizeof(char *), NULL);
6937
6938 while (*(buf = skip_whitespace(buf)) != '\0') {
6939 bc_vec_push(&v, &buf);
6940 buf = skip_non_whitespace(buf);
6941 if (!*buf)
6942 break;
6943 *buf++ = '\0';
6944 }
6945
6946 // NULL terminate, and pass argv[] so that first arg is argv[1]
6947 if (sizeof(int) == sizeof(char*)) {
6948 bc_vec_push(&v, &const_int_0);
6949 } else {
6950 static char *const nullptr = NULL;
6951 bc_vec_push(&v, &nullptr);
6952 }
6953 bc_args(((char **)v.v) - 1);
6954
6955 bc_vec_free(&v);
6956}
6957
6958static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006959 "scale=20"
6960"\n" "define e(x){"
6961"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006962////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6963//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6964//e(-.998896): GNU:.36828580434569428695
6965// above code:.36828580434569428696
6966// actual value:.3682858043456942869594...
6967// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006968"\n" "b=ibase"
6969"\n" "ibase=A"
6970"\n" "if(x<0){"
6971"\n" "n=1"
6972"\n" "x=-x"
6973"\n" "}"
6974"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006975"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006976"\n" "scale=scale(x)+1"
6977"\n" "while(x>1){"
6978"\n" "d+=1"
6979"\n" "x/=2"
6980"\n" "scale+=1"
6981"\n" "}"
6982"\n" "scale=r"
6983"\n" "r=x+1"
6984"\n" "p=x"
6985"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006986"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006987"\n" "p*=x"
6988"\n" "f*=i"
6989"\n" "v=p/f"
6990"\n" "r+=v"
6991"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006992"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006993"\n" "scale=s"
6994"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006995"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006996"\n" "return(r/1)"
6997"\n" "}"
6998"\n" "define l(x){"
6999"\n" "auto b,s,r,p,a,q,i,v"
7000"\n" "b=ibase"
7001"\n" "ibase=A"
7002"\n" "if(x<=0){"
7003"\n" "r=(1-10^scale)/1"
7004"\n" "ibase=b"
7005"\n" "return(r)"
7006"\n" "}"
7007"\n" "s=scale"
7008"\n" "scale+=6"
7009"\n" "p=2"
7010"\n" "while(x>=2){"
7011"\n" "p*=2"
7012"\n" "x=sqrt(x)"
7013"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007014"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007015"\n" "p*=2"
7016"\n" "x=sqrt(x)"
7017"\n" "}"
7018"\n" "r=a=(x-1)/(x+1)"
7019"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007020"\n" "v=1"
7021"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007022"\n" "a*=q"
7023"\n" "v=a/i"
7024"\n" "r+=v"
7025"\n" "}"
7026"\n" "r*=p"
7027"\n" "scale=s"
7028"\n" "ibase=b"
7029"\n" "return(r/1)"
7030"\n" "}"
7031"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007032"\n" "auto b,s,r,a,q,i"
7033"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007034"\n" "b=ibase"
7035"\n" "ibase=A"
7036"\n" "s=scale"
7037"\n" "scale=1.1*s+2"
7038"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007039"\n" "scale=0"
7040"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007041"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007042"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007043"\n" "scale=s+2"
7044"\n" "r=a=x"
7045"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007046"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007047"\n" "a*=q/(i*(i-1))"
7048"\n" "r+=a"
7049"\n" "}"
7050"\n" "scale=s"
7051"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007052"\n" "return(r/1)"
7053"\n" "}"
7054"\n" "define c(x){"
7055"\n" "auto b,s"
7056"\n" "b=ibase"
7057"\n" "ibase=A"
7058"\n" "s=scale"
7059"\n" "scale*=1.2"
7060"\n" "x=s(2*a(1)+x)"
7061"\n" "scale=s"
7062"\n" "ibase=b"
7063"\n" "return(x/1)"
7064"\n" "}"
7065"\n" "define a(x){"
7066"\n" "auto b,s,r,n,a,m,t,f,i,u"
7067"\n" "b=ibase"
7068"\n" "ibase=A"
7069"\n" "n=1"
7070"\n" "if(x<0){"
7071"\n" "n=-1"
7072"\n" "x=-x"
7073"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007074"\n" "if(scale<65){"
7075"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7076"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007077"\n" "}"
7078"\n" "s=scale"
7079"\n" "if(x>.2){"
7080"\n" "scale+=5"
7081"\n" "a=a(.2)"
7082"\n" "}"
7083"\n" "scale=s+3"
7084"\n" "while(x>.2){"
7085"\n" "m+=1"
7086"\n" "x=(x-.2)/(1+.2*x)"
7087"\n" "}"
7088"\n" "r=u=x"
7089"\n" "f=-x*x"
7090"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007091"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007092"\n" "u*=f"
7093"\n" "t=u/i"
7094"\n" "r+=t"
7095"\n" "}"
7096"\n" "scale=s"
7097"\n" "ibase=b"
7098"\n" "return((m*a+r)/n)"
7099"\n" "}"
7100"\n" "define j(n,x){"
7101"\n" "auto b,s,o,a,i,v,f"
7102"\n" "b=ibase"
7103"\n" "ibase=A"
7104"\n" "s=scale"
7105"\n" "scale=0"
7106"\n" "n/=1"
7107"\n" "if(n<0){"
7108"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007109"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007110"\n" "}"
7111"\n" "a=1"
7112"\n" "for(i=2;i<=n;++i)a*=i"
7113"\n" "scale=1.5*s"
7114"\n" "a=(x^n)/2^n/a"
7115"\n" "r=v=1"
7116"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007117"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007118"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007119"\n" "v=v*f/i/(n+i)"
7120"\n" "r+=v"
7121"\n" "}"
7122"\n" "scale=s"
7123"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007124"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007125"\n" "return(a*r/1)"
7126"\n" "}"
7127};
7128#endif // ENABLE_BC
7129
Denys Vlasenko19f11072018-12-12 22:48:19 +01007130static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007131{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007132 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007133 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007134 size_t i;
7135
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007136#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007137 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007138 // We know that internal library is not buggy,
7139 // thus error checking is normally disabled.
7140# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007141 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007142 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007143 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007144 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007145#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007146
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007147 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007148 fname = (void*)G.files.v;
7149 for (i = 0; i < G.files.len; i++) {
7150 s = zbc_vm_file(*fname++);
7151 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7152 // Debug config, non-interactive mode:
7153 // return all the way back to main.
7154 // Non-debug builds do not come here
7155 // in non-interactive mode, they exit.
7156 RETURN_STATUS(s);
7157 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007158 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007159
Denys Vlasenko91cde952018-12-10 20:56:08 +01007160 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007161 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007162
Denys Vlasenko19f11072018-12-12 22:48:19 +01007163 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007164}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007165#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007166
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007167#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007168static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007169{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007170 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007171 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007172 bc_vec_free(&G.prog.vars);
7173 bc_vec_free(&G.prog.var_map);
7174 bc_vec_free(&G.prog.arrs);
7175 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007176 IF_DC(bc_vec_free(&G.prog.strs);)
7177 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007178 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007179 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007180 IF_BC(bc_num_free(&G.prog.last);)
7181 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007182 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007183 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007184}
7185
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007186static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007187{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007188 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007189 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007190 bc_parse_free(&G.prs);
7191 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007192}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007193#endif
7194
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007195static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007196{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007197 BcInstPtr ip;
7198
Denys Vlasenko82269122018-12-14 16:30:56 +01007199 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007200 memset(&ip, 0, sizeof(BcInstPtr));
7201
Denys Vlasenko82269122018-12-14 16:30:56 +01007202 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007203 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007204 G.prog.ob_t = 10;
7205
Denys Vlasenko503faf92018-12-20 16:24:18 +01007206 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7207 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007208
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007209 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007210 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007211
Denys Vlasenko503faf92018-12-20 16:24:18 +01007212 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7213 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007214
7215 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007216 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007217
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007218 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007219 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007220 // a valid function name (and be short)
7221 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007222 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007223 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007224 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007225 bc_program_add_fn();
7226 bc_program_add_fn();
7227 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007228
7229 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007230 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007231
7232 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007233 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007234
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007235 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7236 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007237 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007238 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7239 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007240
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007241 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007242}
Gavin Howard01055ba2018-11-03 11:00:21 -06007243
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007244static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007245{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007246#if ENABLE_FEATURE_EDITING
7247 G.line_input_state = new_line_input_t(DO_HISTORY);
7248#endif
7249 G.prog.len = bc_vm_envLen(env_len);
7250
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007251 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007252 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007253 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007254 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007255
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007256//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7257//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007258 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007259#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007260 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007261 // With SA_RESTART, most system calls will restart
7262 // (IOW: they won't fail with EINTR).
7263 // In particular, this means ^C won't cause
7264 // stdout to get into "error state" if SIGINT hits
7265 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007266 //
7267 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007268 // will only be handled after [Enter] since read()
7269 // from stdin is not interrupted by ^C either,
7270 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007271 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007272 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7273
7274 // Without SA_RESTART, this exhibits a bug:
7275 // "while (1) print 1" and try ^C-ing it.
7276 // Intermittently, instead of returning to input line,
7277 // you'll get "output error: Interrupted system call"
7278 // and exit.
7279 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007280#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007281 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007282 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007283 return 0; // "not a tty"
7284}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007285
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007286static BcStatus bc_vm_run(void)
7287{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007288 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007289#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007290 if (G_exiting) // it was actually "halt" or "quit"
7291 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007292 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007293# if ENABLE_FEATURE_EDITING
7294 free_line_input_t(G.line_input_state);
7295# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007296 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007297#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007298 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007299 return st;
7300}
7301
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007302#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007303int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007304int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007305{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007306 int is_tty;
7307
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007308 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007309
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007310 is_tty = bc_vm_init("BC_LINE_LENGTH");
7311
7312 bc_args(argv);
7313
7314 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7315 bc_vm_info();
7316
7317 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007318}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007319#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007320
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007321#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007322int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007323int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007324{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007325 int noscript;
7326
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007327 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007328
7329 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7330 // 1 char wider than bc from the same package.
7331 // Both default width, and xC_LINE_LENGTH=N are wider:
7332 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007333 // |1234\ |
7334 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007335 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007336 // |123\ |
7337 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007338 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007339 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007340
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007341 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7342 noscript = BC_FLAG_I;
7343 for (;;) {
7344 int n = getopt(argc, argv, "e:f:x");
7345 if (n <= 0)
7346 break;
7347 switch (n) {
7348 case 'e':
7349 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007350 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007351 if (n) return n;
7352 break;
7353 case 'f':
7354 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007355 n = zbc_vm_file(optarg);
7356 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007357 break;
7358 case 'x':
7359 option_mask32 |= DC_FLAG_X;
7360 break;
7361 default:
7362 bb_show_usage();
7363 }
7364 }
7365 argv += optind;
7366
7367 while (*argv) {
7368 noscript = 0;
7369 bc_vec_push(&G.files, argv++);
7370 }
7371
7372 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7373
7374 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007375}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007376#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007377
7378#endif // not DC_SMALL