blob: 4fac2c99a092080b8cbb760aa70d4bf5f2f64be5 [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 {
Gavin Howard01055ba2018-11-03 11:00:21 -0600357 BC_RESULT_TEMP,
358
359 BC_RESULT_VAR,
360 BC_RESULT_ARRAY_ELEM,
361 BC_RESULT_ARRAY,
362
363 BC_RESULT_STR,
364
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100365 //code uses "inst - XC_INST_IBASE + BC_RESULT_IBASE" construct,
366 BC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
367 BC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
368 BC_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
Gavin Howard01055ba2018-11-03 11:00:21 -0600370 BC_RESULT_CONSTANT,
371 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};
594static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
595{
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;
717 bool newline;
Gavin Howard01055ba2018-11-03 11:00:21 -0600718 struct {
719 BcLexType t;
720 BcLexType last;
721 BcVec v;
722 } t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600723} BcLex;
724
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100725#define BC_PARSE_STREND (0xff)
Gavin Howard01055ba2018-11-03 11:00:21 -0600726
Denys Vlasenko39287e02018-12-22 02:23:08 +0100727#if ENABLE_BC
728# define BC_PARSE_REL (1 << 0)
729# define BC_PARSE_PRINT (1 << 1)
730# define BC_PARSE_ARRAY (1 << 2)
731# define BC_PARSE_NOCALL (1 << 3)
732#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600733
Gavin Howard01055ba2018-11-03 11:00:21 -0600734typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600735 BcLex l;
736
Denys Vlasenko503faf92018-12-20 16:24:18 +0100737 IF_BC(BcVec exits;)
738 IF_BC(BcVec conds;)
739 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600740
Gavin Howard01055ba2018-11-03 11:00:21 -0600741 BcFunc *func;
742 size_t fidx;
743
Denys Vlasenko503faf92018-12-20 16:24:18 +0100744 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600745} BcParse;
746
Gavin Howard01055ba2018-11-03 11:00:21 -0600747typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600748 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100749 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600750
Denys Vlasenko503faf92018-12-20 16:24:18 +0100751 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600752 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600753 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600754
Gavin Howard01055ba2018-11-03 11:00:21 -0600755 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100756 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600757
758 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100759 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600760
761 BcVec vars;
762 BcVec var_map;
763
764 BcVec arrs;
765 BcVec arr_map;
766
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100767 IF_DC(BcVec strs;)
768 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600769
770 const char *file;
771
Gavin Howard01055ba2018-11-03 11:00:21 -0600772 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100773 IF_BC(BcNum one;)
774 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600775} BcProgram;
776
Gavin Howard01055ba2018-11-03 11:00:21 -0600777#define BC_PROG_MAIN (0)
778#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100779#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600780#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100781#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600782
783#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
784#define BC_PROG_NUM(r, n) \
785 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
786
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100787#define BC_FLAG_W (1 << 0)
788#define BC_FLAG_V (1 << 1)
789#define BC_FLAG_S (1 << 2)
790#define BC_FLAG_Q (1 << 3)
791#define BC_FLAG_L (1 << 4)
792#define BC_FLAG_I (1 << 5)
793#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600794
795#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
796#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
797
Denys Vlasenko64074a12018-12-07 15:50:14 +0100798#define BC_MAX_OBASE ((unsigned) 999)
799#define BC_MAX_DIM ((unsigned) INT_MAX)
800#define BC_MAX_SCALE ((unsigned) UINT_MAX)
801#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
802#define BC_MAX_NUM BC_MAX_STRING
803// Unused apart from "limits" message. Just show a "biggish number" there.
804//#define BC_MAX_NAME BC_MAX_STRING
805//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
806//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
807#define BC_MAX_NAME_STR "999999999"
808#define BC_MAX_EXP_STR "999999999"
809#define BC_MAX_VARS_STR "999999999"
810
811#define BC_MAX_OBASE_STR "999"
812
813#if INT_MAX == 2147483647
814# define BC_MAX_DIM_STR "2147483647"
815#elif INT_MAX == 9223372036854775807
816# define BC_MAX_DIM_STR "9223372036854775807"
817#else
818# error Strange INT_MAX
819#endif
820
821#if UINT_MAX == 4294967295
822# define BC_MAX_SCALE_STR "4294967295"
823# define BC_MAX_STRING_STR "4294967294"
824#elif UINT_MAX == 18446744073709551615
825# define BC_MAX_SCALE_STR "18446744073709551615"
826# define BC_MAX_STRING_STR "18446744073709551614"
827#else
828# error Strange UINT_MAX
829#endif
830#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600831
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100832struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100833 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100834 IF_FEATURE_CLEAN_UP(smallint exiting;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600835
836 BcParse prs;
837 BcProgram prog;
838
Denys Vlasenko5318f812018-12-05 17:48:01 +0100839 // For error messages. Can be set to current parsed line,
840 // or [TODO] to current executing line (can be before last parsed one)
841 unsigned err_line;
842
Gavin Howard01055ba2018-11-03 11:00:21 -0600843 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100844 BcVec input_buffer;
845 FILE *input_fp;
Gavin Howard01055ba2018-11-03 11:00:21 -0600846
847 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100848
849#if ENABLE_FEATURE_EDITING
850 line_input_t *line_input_state;
851#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100852} FIX_ALIASING;
853#define G (*ptr_to_globals)
854#define INIT_G() do { \
855 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
856} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100857#define FREE_G() do { \
858 FREE_PTR_TO_GLOBALS(); \
859} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100860#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
861#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100862#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100863#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100864# define G_interrupt bb_got_signal
865# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100866#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100867# define G_interrupt 0
868# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100869#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100870#if ENABLE_FEATURE_CLEAN_UP
871# define G_exiting G.exiting
872#else
873# define G_exiting 0
874#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100875#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100876#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100877
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100878// In configurations where errors abort instead of propagating error
879// return code up the call chain, functions returning BC_STATUS
880// actually don't return anything, they always succeed and return "void".
881// A macro wrapper is provided, which makes this statement work:
882// s = zbc_func(...)
883// and makes it visible to the compiler that s is always zero,
884// allowing compiler to optimize dead code after the statement.
885//
886// To make code more readable, each such function has a "z"
887// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
888//
889#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100890# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100891# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100892# define IF_ERROR_RETURN_POSSIBLE(a) a
893# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100894# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100895# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100896#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100897# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100898# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100899# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
900# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100901# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100902# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100903#endif
904
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100905//
906// Utility routines
907//
Gavin Howard01055ba2018-11-03 11:00:21 -0600908
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100909static void fflush_and_check(void)
910{
911 fflush_all();
912 if (ferror(stdout) || ferror(stderr))
913 bb_perror_msg_and_die("output error");
914}
915
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100916#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100917#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100918do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100919 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100920 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100921 return BC_STATUS_FAILURE; \
922} while (0)
923#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100924#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100925#endif
926
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100927static void quit(void) NORETURN;
928static void quit(void)
929{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100930 if (ferror(stdin))
931 bb_perror_msg_and_die("input error");
932 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100933 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100934 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100935}
936
Denys Vlasenko5318f812018-12-05 17:48:01 +0100937static void bc_verror_msg(const char *fmt, va_list p)
938{
Denys Vlasenko82269122018-12-14 16:30:56 +0100939 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +0100940 if (G.prog.file) {
941 sv = applet_name;
942 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
943 }
944 bb_verror_msg(fmt, p, NULL);
945 if (G.prog.file) {
946 free((char*)applet_name);
947 applet_name = sv;
948 }
949}
950
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100951static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100952{
953 va_list p;
954
955 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100956 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100957 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100958
Denys Vlasenkoec603182018-12-17 10:34:02 +0100959 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
960 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
961 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100962}
963
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100964#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100965static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100966{
967 va_list p;
968
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100969 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100970 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100971 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100972
973 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100974 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100975 va_end(p);
976
977 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100978 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100979 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenkoec603182018-12-17 10:34:02 +0100980 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
981 return BC_STATUS_FAILURE;
982 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100983}
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 Vlasenkoa6f84e12018-12-06 11:10:11 +01001018static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001019{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001020 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001021}
Denys Vlasenko00646792018-12-05 18:12:27 +01001022static int bc_POSIX_does_not_allow(const char *msg)
1023{
1024 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1025}
1026static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1027{
1028 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1029}
1030static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1031{
1032 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1033}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001034#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001035
Gavin Howard01055ba2018-11-03 11:00:21 -06001036static void bc_vec_grow(BcVec *v, size_t n)
1037{
1038 size_t cap = v->cap * 2;
1039 while (cap < v->len + n) cap *= 2;
1040 v->v = xrealloc(v->v, v->size * cap);
1041 v->cap = cap;
1042}
1043
1044static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1045{
1046 v->size = esize;
1047 v->cap = BC_VEC_START_CAP;
1048 v->len = 0;
1049 v->dtor = dtor;
1050 v->v = xmalloc(esize * BC_VEC_START_CAP);
1051}
1052
Denys Vlasenko7d628012018-12-04 21:46:47 +01001053static void bc_char_vec_init(BcVec *v)
1054{
1055 bc_vec_init(v, sizeof(char), NULL);
1056}
1057
Gavin Howard01055ba2018-11-03 11:00:21 -06001058static void bc_vec_expand(BcVec *v, size_t req)
1059{
1060 if (v->cap < req) {
1061 v->v = xrealloc(v->v, v->size * req);
1062 v->cap = req;
1063 }
1064}
1065
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001066static void bc_vec_pop(BcVec *v)
1067{
1068 v->len--;
1069 if (v->dtor)
1070 v->dtor(v->v + (v->size * v->len));
1071}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001072
Gavin Howard01055ba2018-11-03 11:00:21 -06001073static void bc_vec_npop(BcVec *v, size_t n)
1074{
1075 if (!v->dtor)
1076 v->len -= n;
1077 else {
1078 size_t len = v->len - n;
1079 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1080 }
1081}
1082
Denys Vlasenko7d628012018-12-04 21:46:47 +01001083static void bc_vec_pop_all(BcVec *v)
1084{
1085 bc_vec_npop(v, v->len);
1086}
1087
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001088static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001089{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001090 size_t len = v->len;
1091 if (len >= v->cap) bc_vec_grow(v, 1);
1092 memmove(v->v + (v->size * len), data, v->size);
1093 v->len++;
1094 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001095}
1096
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001097// G.prog.results often needs "pop old operand, push result" idiom.
1098// Can do this without a few extra ops
1099static size_t bc_result_pop_and_push(const void *data)
1100{
1101 BcVec *v = &G.prog.results;
1102 char *last;
1103 size_t len = v->len - 1;
1104
1105 last = v->v + (v->size * len);
1106 if (v->dtor)
1107 v->dtor(last);
1108 memmove(last, data, v->size);
1109 return len;
1110}
1111
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001112static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001113{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001114 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001115}
1116
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001117static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001118{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001119 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001120 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001121 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001122}
1123
Gavin Howard01055ba2018-11-03 11:00:21 -06001124static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1125{
1126 if (idx == v->len)
1127 bc_vec_push(v, data);
1128 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001129 char *ptr;
1130
1131 if (v->len == v->cap) bc_vec_grow(v, 1);
1132
1133 ptr = v->v + v->size * idx;
1134
1135 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1136 memmove(ptr, data, v->size);
1137 }
1138}
1139
1140static void bc_vec_string(BcVec *v, size_t len, const char *str)
1141{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001142 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001143 bc_vec_expand(v, len + 1);
1144 memcpy(v->v, str, len);
1145 v->len = len;
1146
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001147 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001148}
1149
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001150#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001151static void bc_vec_concat(BcVec *v, const char *str)
1152{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001153 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001154
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001155 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001156
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001157 slen = strlen(str);
1158 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001159
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001160 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001161 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001162
1163 v->len = len;
1164}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001165#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001166
1167static void *bc_vec_item(const BcVec *v, size_t idx)
1168{
1169 return v->v + v->size * idx;
1170}
1171
1172static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1173{
1174 return v->v + v->size * (v->len - idx - 1);
1175}
1176
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001177static void *bc_vec_top(const BcVec *v)
1178{
1179 return v->v + v->size * (v->len - 1);
1180}
1181
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001182static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001183{
1184 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001185 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001186 free(v->v);
1187}
1188
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001189static BcFunc* bc_program_func(size_t idx)
1190{
1191 return bc_vec_item(&G.prog.fns, idx);
1192}
1193// BC_PROG_MAIN is zeroth element, so:
1194#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1195
1196#if ENABLE_BC
1197static BcFunc* bc_program_current_func(void)
1198{
1199 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1200 BcFunc *func = bc_program_func(ip->func);
1201 return func;
1202}
1203#endif
1204
1205static char** bc_program_str(size_t idx)
1206{
1207#if ENABLE_BC
1208 if (IS_BC) {
1209 BcFunc *func = bc_program_current_func();
1210 return bc_vec_item(&func->strs, idx);
1211 }
1212#endif
1213 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1214}
1215
1216static char** bc_program_const(size_t idx)
1217{
1218#if ENABLE_BC
1219 if (IS_BC) {
1220 BcFunc *func = bc_program_current_func();
1221 return bc_vec_item(&func->consts, idx);
1222 }
1223#endif
1224 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1225}
1226
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001227static int bc_id_cmp(const void *e1, const void *e2)
1228{
1229 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1230}
1231
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001232static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001233{
1234 free(((BcId *) id)->name);
1235}
1236
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001237static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001238{
1239 size_t low = 0, high = v->len;
1240
1241 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001242 size_t mid = (low + high) / 2;
1243 BcId *id = bc_vec_item(v, mid);
1244 int result = bc_id_cmp(ptr, id);
1245
1246 if (result == 0)
1247 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001248 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001249 high = mid;
1250 else
1251 low = mid + 1;
1252 }
1253
1254 return low;
1255}
1256
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001257static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001258{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001259 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001260
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001261 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001262 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001263 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1264 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001265 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001266 bc_vec_pushAt(v, ptr, n);
1267 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001268}
1269
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001270#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001271static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001272{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001273 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001274 if (i >= v->len) return BC_VEC_INVALID_IDX;
1275 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1276}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001277#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001278
Gavin Howard01055ba2018-11-03 11:00:21 -06001279static void bc_num_setToZero(BcNum *n, size_t scale)
1280{
1281 n->len = 0;
1282 n->neg = false;
1283 n->rdx = scale;
1284}
1285
1286static void bc_num_zero(BcNum *n)
1287{
1288 bc_num_setToZero(n, 0);
1289}
1290
1291static void bc_num_one(BcNum *n)
1292{
1293 bc_num_setToZero(n, 0);
1294 n->len = 1;
1295 n->num[0] = 1;
1296}
1297
Denys Vlasenko3129f702018-12-09 12:04:44 +01001298// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001299static void bc_num_init(BcNum *n, size_t req)
1300{
1301 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001302 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001303 n->num = xmalloc(req);
1304 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001305 n->rdx = 0;
1306 n->len = 0;
1307 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001308}
1309
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001310static void bc_num_init_DEF_SIZE(BcNum *n)
1311{
1312 bc_num_init(n, BC_NUM_DEF_SIZE);
1313}
1314
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001315static void bc_num_expand(BcNum *n, size_t req)
1316{
1317 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1318 if (req > n->cap) {
1319 n->num = xrealloc(n->num, req);
1320 n->cap = req;
1321 }
1322}
1323
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001324static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001325{
1326 free(((BcNum *) num)->num);
1327}
1328
1329static void bc_num_copy(BcNum *d, BcNum *s)
1330{
1331 if (d != s) {
1332 bc_num_expand(d, s->cap);
1333 d->len = s->len;
1334 d->neg = s->neg;
1335 d->rdx = s->rdx;
1336 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1337 }
1338}
1339
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001340static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001341{
1342 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001343 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001344
Denys Vlasenko1557b762018-12-22 21:37:46 +01001345 result = 0;
1346 i = n->len;
1347 while (i > n->rdx) {
1348 unsigned long prev = result;
1349 result = result * 10 + n->num[--i];
1350 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1351 // 0x1ff00000 * 10 is 0x13f600000,
1352 // or 0x3f600000 truncated to 32 bits. Which is larger.
1353 // However, (N*10)/8 < N check is always correct.
1354 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001355 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001356 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001357 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001358
Denys Vlasenko29301232018-12-11 15:29:32 +01001359 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001360}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001361#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1362
1363static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1364{
1365 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1366
1367 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1368}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001369#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001370
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001371#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1372# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1373#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1374# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1375#endif
1376// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1377// would not hit realloc() code path - not good if num[] is not malloced
1378
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001379static void bc_num_ulong2num(BcNum *n, unsigned long val)
1380{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001381 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001382
1383 bc_num_zero(n);
1384
1385 if (val == 0) return;
1386
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001387 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001388
1389 ptr = n->num;
1390 for (;;) {
1391 n->len++;
1392 *ptr++ = val % 10;
1393 val /= 10;
1394 if (val == 0) break;
1395 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001396}
1397
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001398static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001399{
1400 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001401 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001402 a[i] -= b[i];
1403 for (j = i; a[j] < 0;) {
1404 a[j++] += 10;
1405 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001406 }
1407 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001408}
1409
1410static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1411{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001412 size_t i = len;
1413 for (;;) {
1414 int c;
1415 if (i == 0)
1416 return 0;
1417 i--;
1418 c = a[i] - b[i];
1419 if (c != 0) {
1420 i++;
1421 if (c < 0)
1422 return -i;
1423 return i;
1424 }
1425 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001426}
1427
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001428#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1429#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1430#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1431//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1432static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1433{
1434 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1435}
1436//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1437static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1438{
1439 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1440}
1441
Gavin Howard01055ba2018-11-03 11:00:21 -06001442static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1443{
1444 size_t i, min, a_int, b_int, diff;
1445 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001446 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001447 ssize_t cmp;
1448
1449 if (a == b) return 0;
1450 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1451 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001452
1453 if (a->neg != b->neg) // signs of a and b differ
1454 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1455 return (int)b->neg - (int)a->neg;
1456 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001457
1458 a_int = BC_NUM_INT(a);
1459 b_int = BC_NUM_INT(b);
1460 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001461
1462 if (a_int != 0) return (ssize_t) a_int;
1463
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001464 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001465 if (a_max) {
1466 min = b->rdx;
1467 diff = a->rdx - b->rdx;
1468 max_num = a->num + diff;
1469 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001470 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1471 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001472 min = a->rdx;
1473 diff = b->rdx - a->rdx;
1474 max_num = b->num + diff;
1475 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001476 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001477 }
1478
1479 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001480 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001481
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001482 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001483 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001484 }
1485
1486 return 0;
1487}
1488
1489static void bc_num_truncate(BcNum *n, size_t places)
1490{
1491 if (places == 0) return;
1492
1493 n->rdx -= places;
1494
1495 if (n->len != 0) {
1496 n->len -= places;
1497 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1498 }
1499}
1500
1501static void bc_num_extend(BcNum *n, size_t places)
1502{
1503 size_t len = n->len + places;
1504
1505 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001506 if (n->cap < len) bc_num_expand(n, len);
1507
1508 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1509 memset(n->num, 0, sizeof(BcDig) * places);
1510
1511 n->len += places;
1512 n->rdx += places;
1513 }
1514}
1515
1516static void bc_num_clean(BcNum *n)
1517{
1518 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1519 if (n->len == 0)
1520 n->neg = false;
1521 else if (n->len < n->rdx)
1522 n->len = n->rdx;
1523}
1524
1525static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1526{
1527 if (n->rdx < scale)
1528 bc_num_extend(n, scale - n->rdx);
1529 else
1530 bc_num_truncate(n, n->rdx - scale);
1531
1532 bc_num_clean(n);
1533 if (n->len != 0) n->neg = !neg1 != !neg2;
1534}
1535
1536static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1537 BcNum *restrict b)
1538{
1539 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001540 b->len = n->len - idx;
1541 a->len = idx;
1542 a->rdx = b->rdx = 0;
1543
1544 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1545 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001546 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001547 bc_num_zero(b);
1548 bc_num_copy(a, n);
1549 }
1550
1551 bc_num_clean(a);
1552 bc_num_clean(b);
1553}
1554
Denys Vlasenko29301232018-12-11 15:29:32 +01001555static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001556{
Denys Vlasenko29301232018-12-11 15:29:32 +01001557 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001558
1559 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1560 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1561 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001562 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001563 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001564
1565 if (n->rdx >= places)
1566 n->rdx -= places;
1567 else {
1568 bc_num_extend(n, places - n->rdx);
1569 n->rdx = 0;
1570 }
1571
1572 bc_num_clean(n);
1573
Denys Vlasenko29301232018-12-11 15:29:32 +01001574 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001575}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001576#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001577
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001578typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1579
1580static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1581 BcNumBinaryOp op, size_t req)
1582{
1583 BcStatus s;
1584 BcNum num2, *ptr_a, *ptr_b;
1585 bool init = false;
1586
1587 if (c == a) {
1588 ptr_a = &num2;
1589 memcpy(ptr_a, c, sizeof(BcNum));
1590 init = true;
1591 } else
1592 ptr_a = a;
1593
1594 if (c == b) {
1595 ptr_b = &num2;
1596 if (c != a) {
1597 memcpy(ptr_b, c, sizeof(BcNum));
1598 init = true;
1599 }
1600 } else
1601 ptr_b = b;
1602
1603 if (init)
1604 bc_num_init(c, req);
1605 else
1606 bc_num_expand(c, req);
1607
1608 s = BC_STATUS_SUCCESS;
1609 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1610
1611 if (init) bc_num_free(&num2);
1612
1613 RETURN_STATUS(s);
1614}
1615#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1616
1617static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1618static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1619static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1620static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1621static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1622static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1623
1624static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1625{
1626 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1627 (void) scale;
1628 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1629}
1630
1631static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1632{
1633 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1634 (void) scale;
1635 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1636}
1637
1638static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1639{
1640 size_t req = BC_NUM_MREQ(a, b, scale);
1641 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1642}
1643
1644static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1645{
1646 size_t req = BC_NUM_MREQ(a, b, scale);
1647 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1648}
1649
1650static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1651{
1652 size_t req = BC_NUM_MREQ(a, b, scale);
1653 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1654}
1655
1656static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1657{
1658 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1659}
1660
1661static const BcNumBinaryOp zbc_program_ops[] = {
1662 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1663};
1664#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1665#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1666#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1667#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1668#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1669#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1670
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001671static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001672{
1673 BcNum one;
1674 BcDig num[2];
1675
1676 one.cap = 2;
1677 one.num = num;
1678 bc_num_one(&one);
1679
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001680 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001681}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001682#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001683
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001684static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001685{
1686 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1687 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001688 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001689
1690 // Because this function doesn't need to use scale (per the bc spec),
1691 // I am hijacking it to say whether it's doing an add or a subtract.
1692
1693 if (a->len == 0) {
1694 bc_num_copy(c, b);
1695 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001696 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001697 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001698 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001699 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001700 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001701 }
1702
1703 c->neg = a->neg;
1704 c->rdx = BC_MAX(a->rdx, b->rdx);
1705 min_rdx = BC_MIN(a->rdx, b->rdx);
1706 c->len = 0;
1707
1708 if (a->rdx > b->rdx) {
1709 diff = a->rdx - b->rdx;
1710 ptr = a->num;
1711 ptr_a = a->num + diff;
1712 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001713 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001714 diff = b->rdx - a->rdx;
1715 ptr = b->num;
1716 ptr_a = a->num;
1717 ptr_b = b->num + diff;
1718 }
1719
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001720 ptr_c = c->num;
1721 for (i = 0; i < diff; ++i, ++c->len)
1722 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001723
1724 ptr_c += diff;
1725 a_int = BC_NUM_INT(a);
1726 b_int = BC_NUM_INT(b);
1727
1728 if (a_int > b_int) {
1729 min_int = b_int;
1730 max = a_int;
1731 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001732 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001733 min_int = a_int;
1734 max = b_int;
1735 ptr = ptr_b;
1736 }
1737
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001738 carry = 0;
1739 for (i = 0; i < min_rdx + min_int; ++i) {
1740 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001741 carry = in / 10;
1742 ptr_c[i] = (BcDig)(in % 10);
1743 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001744 for (; i < max + min_rdx; ++i) {
1745 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001746 carry = in / 10;
1747 ptr_c[i] = (BcDig)(in % 10);
1748 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001749 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001750
1751 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1752
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001753 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001754}
1755
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001756static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001757{
Gavin Howard01055ba2018-11-03 11:00:21 -06001758 ssize_t cmp;
1759 BcNum *minuend, *subtrahend;
1760 size_t start;
1761 bool aneg, bneg, neg;
1762
1763 // Because this function doesn't need to use scale (per the bc spec),
1764 // I am hijacking it to say whether it's doing an add or a subtract.
1765
1766 if (a->len == 0) {
1767 bc_num_copy(c, b);
1768 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001769 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001770 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001771 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001772 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001773 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001774 }
1775
1776 aneg = a->neg;
1777 bneg = b->neg;
1778 a->neg = b->neg = false;
1779
1780 cmp = bc_num_cmp(a, b);
1781
1782 a->neg = aneg;
1783 b->neg = bneg;
1784
1785 if (cmp == 0) {
1786 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001787 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001788 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001789 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001790 neg = a->neg;
1791 minuend = a;
1792 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001793 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001794 neg = b->neg;
1795 if (sub) neg = !neg;
1796 minuend = b;
1797 subtrahend = a;
1798 }
1799
1800 bc_num_copy(c, minuend);
1801 c->neg = neg;
1802
1803 if (c->rdx < subtrahend->rdx) {
1804 bc_num_extend(c, subtrahend->rdx - c->rdx);
1805 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001806 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001807 start = c->rdx - subtrahend->rdx;
1808
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001809 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001810
1811 bc_num_clean(c);
1812
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001813 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001814}
1815
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001816static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001817 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001818#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001819{
1820 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001821 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001822 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001823 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001824
Gavin Howard01055ba2018-11-03 11:00:21 -06001825 if (a->len == 0 || b->len == 0) {
1826 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001827 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001828 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001829 aone = BC_NUM_ONE(a);
1830 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001831 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001832 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001833 }
1834
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001835 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1836 || a->len < BC_NUM_KARATSUBA_LEN
1837 || b->len < BC_NUM_KARATSUBA_LEN
1838 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001839 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001840
Gavin Howard01055ba2018-11-03 11:00:21 -06001841 bc_num_expand(c, a->len + b->len + 1);
1842
1843 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001844 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001845
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001846 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001847 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001848 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001849 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001850 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001851 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001852 carry = in / 10;
1853 c->num[i + j] = (BcDig)(in % 10);
1854 }
1855
1856 c->num[i + j] += (BcDig) carry;
1857 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001858
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001859#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001860 // a=2^1000000
1861 // a*a <- without check below, this will not be interruptible
1862 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001863#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001864 }
1865
1866 c->len = len;
1867
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001868 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001869 }
1870
1871 bc_num_init(&l1, max);
1872 bc_num_init(&h1, max);
1873 bc_num_init(&l2, max);
1874 bc_num_init(&h2, max);
1875 bc_num_init(&m1, max);
1876 bc_num_init(&m2, max);
1877 bc_num_init(&z0, max);
1878 bc_num_init(&z1, max);
1879 bc_num_init(&z2, max);
1880 bc_num_init(&temp, max + max);
1881
1882 bc_num_split(a, max2, &l1, &h1);
1883 bc_num_split(b, max2, &l2, &h2);
1884
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001885 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001886 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001887 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001888 if (s) goto err;
1889
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001890 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001892 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001893 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001894 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001895 if (s) goto err;
1896
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001897 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001898 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001899 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001900 if (s) goto err;
1901
Denys Vlasenko29301232018-12-11 15:29:32 +01001902 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001903 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001904 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001905 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001906 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001907 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001908 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001909 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001910 bc_num_free(&temp);
1911 bc_num_free(&z2);
1912 bc_num_free(&z1);
1913 bc_num_free(&z0);
1914 bc_num_free(&m2);
1915 bc_num_free(&m1);
1916 bc_num_free(&h2);
1917 bc_num_free(&l2);
1918 bc_num_free(&h1);
1919 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001920 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001921}
1922
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001923static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001924{
1925 BcStatus s;
1926 BcNum cpa, cpb;
1927 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1928
1929 scale = BC_MAX(scale, a->rdx);
1930 scale = BC_MAX(scale, b->rdx);
1931 scale = BC_MIN(a->rdx + b->rdx, scale);
1932 maxrdx = BC_MAX(maxrdx, scale);
1933
1934 bc_num_init(&cpa, a->len);
1935 bc_num_init(&cpb, b->len);
1936
1937 bc_num_copy(&cpa, a);
1938 bc_num_copy(&cpb, b);
1939 cpa.neg = cpb.neg = false;
1940
Denys Vlasenko29301232018-12-11 15:29:32 +01001941 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001942 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001943 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001944 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001945 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001946 if (s) goto err;
1947
1948 maxrdx += scale;
1949 bc_num_expand(c, c->len + maxrdx);
1950
1951 if (c->len < maxrdx) {
1952 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1953 c->len += maxrdx;
1954 }
1955
1956 c->rdx = maxrdx;
1957 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001958 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001959 bc_num_free(&cpb);
1960 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001961 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001962}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001963#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001964
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001965static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001966{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001967 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001968 size_t len, end, i;
1969 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001970
1971 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001972 RETURN_STATUS(bc_error("divide by zero"));
1973 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001974 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001975 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001976 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001977 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001978 bc_num_copy(c, a);
1979 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001980 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001981 }
1982
1983 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1984 bc_num_copy(&cp, a);
1985 len = b->len;
1986
1987 if (len > cp.len) {
1988 bc_num_expand(&cp, len + 2);
1989 bc_num_extend(&cp, len - cp.len);
1990 }
1991
1992 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1993 cp.rdx -= b->rdx;
1994 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1995
1996 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001997 for (;;) {
1998 if (len == 0) break;
1999 len--;
2000 if (b->num[len] != 0)
2001 break;
2002 }
2003 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002004 }
2005
2006 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2007
2008 // We want an extra zero in front to make things simpler.
2009 cp.num[cp.len++] = 0;
2010 end = cp.len - len;
2011
2012 bc_num_expand(c, cp.len);
2013
2014 bc_num_zero(c);
2015 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2016 c->rdx = cp.rdx;
2017 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002018
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002019 s = BC_STATUS_SUCCESS;
2020 for (i = end - 1; i < end; --i) {
2021 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002022 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002023 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2024 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002025 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002026#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002027 // a=2^100000
2028 // scale=40000
2029 // 1/a <- without check below, this will not be interruptible
2030 if (G_interrupt) {
2031 s = BC_STATUS_FAILURE;
2032 break;
2033 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002034#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002035 }
2036
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002037 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002038 bc_num_free(&cp);
2039
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002040 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002041}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002042#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002043
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002044static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002045 BcNum *restrict d, size_t scale, size_t ts)
2046{
2047 BcStatus s;
2048 BcNum temp;
2049 bool neg;
2050
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002051 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002052 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002053
2054 if (a->len == 0) {
2055 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002056 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002057 }
2058
2059 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002060 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002061 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002062
2063 if (scale != 0) scale = ts;
2064
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002065 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002066 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002067 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002068 if (s) goto err;
2069
2070 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2071
2072 neg = d->neg;
2073 bc_num_retireMul(d, ts, a->neg, b->neg);
2074 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002075 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002076 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002077 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002078}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002079#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002080
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002081static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002082{
2083 BcStatus s;
2084 BcNum c1;
2085 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2086
2087 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002088 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002089 bc_num_free(&c1);
2090
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002091 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002092}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002093#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002094
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002095static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002096{
2097 BcStatus s = BC_STATUS_SUCCESS;
2098 BcNum copy;
2099 unsigned long pow;
2100 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002101 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002102
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002103 // GNU bc does not allow 2^2.0 - we do
2104 for (i = 0; i < b->rdx; i++)
2105 if (b->num[i] != 0)
2106 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002107
2108 if (b->len == 0) {
2109 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002110 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002111 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002112 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002113 bc_num_setToZero(c, scale);
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 (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 if (!b->neg)
2118 bc_num_copy(c, a);
2119 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002120 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002121 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 }
2123
2124 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002125 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002126 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002127 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002128
2129 bc_num_init(&copy, a->len);
2130 bc_num_copy(&copy, a);
2131
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002132 if (!neg) {
2133 if (a->rdx > scale)
2134 scale = a->rdx;
2135 if (a->rdx * pow < scale)
2136 scale = a->rdx * pow;
2137 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002138
Gavin Howard01055ba2018-11-03 11:00:21 -06002139
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002140 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002141 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002142 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002143 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002144 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002145 //if (G_interrupt) {
2146 // s = BC_STATUS_FAILURE;
2147 // goto err;
2148 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002149 }
2150
Gavin Howard01055ba2018-11-03 11:00:21 -06002151 bc_num_copy(c, &copy);
2152
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002153 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002154 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002155 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002156 if (s) goto err;
2157
2158 if (pow & 1) {
2159 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002160 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002161 if (s) goto err;
2162 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002163 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002164 //if (G_interrupt) {
2165 // s = BC_STATUS_FAILURE;
2166 // goto err;
2167 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002168 }
2169
2170 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002171 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002172 if (s) goto err;
2173 }
2174
Gavin Howard01055ba2018-11-03 11:00:21 -06002175 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2176
2177 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002178 for (i = 0; i < c->len; ++i)
2179 if (c->num[i] != 0)
2180 goto skip;
2181 bc_num_setToZero(c, scale);
2182 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002183
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002184 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002185 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002186 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002187}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002188#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002189
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002190static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002191{
2192 BcStatus s;
2193 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2194 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2195 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2196
2197 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2198 bc_num_expand(b, req);
2199
2200 if (a->len == 0) {
2201 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002202 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002203 }
2204 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002205 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002206 }
2207 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002208 bc_num_one(b);
2209 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002210 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002211 }
2212
2213 scale = BC_MAX(scale, a->rdx) + 1;
2214 len = a->len + scale;
2215
2216 bc_num_init(&num1, len);
2217 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002218 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002219
2220 bc_num_one(&half);
2221 half.num[0] = 5;
2222 half.rdx = 1;
2223
2224 bc_num_init(&f, len);
2225 bc_num_init(&fprime, len);
2226
2227 x0 = &num1;
2228 x1 = &num2;
2229
2230 bc_num_one(x0);
2231 pow = BC_NUM_INT(a);
2232
2233 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002234 if (pow & 1)
2235 x0->num[0] = 2;
2236 else
2237 x0->num[0] = 6;
2238
2239 pow -= 2 - (pow & 1);
2240
2241 bc_num_extend(x0, pow);
2242
2243 // Make sure to move the radix back.
2244 x0->rdx -= pow;
2245 }
2246
2247 x0->rdx = digs = digs1 = 0;
2248 resrdx = scale + 2;
2249 len = BC_NUM_INT(x0) + resrdx - 1;
2250
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002251 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002252 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002253 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002254 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002255 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002256 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002257 if (s) goto err;
2258
2259 cmp = bc_num_cmp(x1, x0);
2260 digs = x1->len - (unsigned long long) llabs(cmp);
2261
2262 if (cmp == cmp2 && digs == digs1)
2263 times += 1;
2264 else
2265 times = 0;
2266
2267 resrdx += times > 4;
2268
2269 cmp2 = cmp1;
2270 cmp1 = cmp;
2271 digs1 = digs;
2272
2273 temp = x0;
2274 x0 = x1;
2275 x1 = temp;
2276 }
2277
Gavin Howard01055ba2018-11-03 11:00:21 -06002278 bc_num_copy(b, x0);
2279 scale -= 1;
2280 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002281 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002282 bc_num_free(&fprime);
2283 bc_num_free(&f);
2284 bc_num_free(&half);
2285 bc_num_free(&num2);
2286 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002287 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002288}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002289#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002290
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002291static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002292 size_t scale)
2293{
2294 BcStatus s;
2295 BcNum num2, *ptr_a;
2296 bool init = false;
2297 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2298
2299 if (c == a) {
2300 memcpy(&num2, c, sizeof(BcNum));
2301 ptr_a = &num2;
2302 bc_num_init(c, len);
2303 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002304 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002305 ptr_a = a;
2306 bc_num_expand(c, len);
2307 }
2308
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002309 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002310
2311 if (init) bc_num_free(&num2);
2312
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002313 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002314}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002315#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002316
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002317#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002318static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002319{
2320 BcStatus s;
2321 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002322 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002323
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002324 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002325 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002326 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002327 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002328 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002329 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002330
2331 bc_num_expand(d, c->len);
2332 bc_num_init(&base, c->len);
2333 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002334 bc_num_init(&temp, b->len);
2335
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002336 two.cap = ARRAY_SIZE(two_digs);
2337 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002338 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002339 two_digs[0] = 2;
2340
Gavin Howard01055ba2018-11-03 11:00:21 -06002341 bc_num_one(d);
2342
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002343 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002344 if (s) goto err;
2345 bc_num_copy(&exp, b);
2346
2347 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002348 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002349 if (s) goto err;
2350
2351 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002352 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002353 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002354 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002355 if (s) goto err;
2356 }
2357
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002358 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002359 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002360 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002361 if (s) goto err;
2362 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002363 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002364 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002365 bc_num_free(&exp);
2366 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002367 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002368}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002369#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002370#endif // ENABLE_DC
2371
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002372static FAST_FUNC void bc_string_free(void *string)
2373{
2374 free(*(char**)string);
2375}
2376
Gavin Howard01055ba2018-11-03 11:00:21 -06002377static void bc_func_init(BcFunc *f)
2378{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002379 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002380 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2381 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002382 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2383 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002384 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002385}
2386
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002387static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002388{
2389 BcFunc *f = (BcFunc *) func;
2390 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002391 IF_BC(bc_vec_free(&f->labels);)
2392 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002393 IF_BC(bc_vec_free(&f->strs);)
2394 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002395}
2396
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002397static void bc_array_expand(BcVec *a, size_t len);
2398
Gavin Howard01055ba2018-11-03 11:00:21 -06002399static void bc_array_init(BcVec *a, bool nums)
2400{
2401 if (nums)
2402 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2403 else
2404 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2405 bc_array_expand(a, 1);
2406}
2407
Gavin Howard01055ba2018-11-03 11:00:21 -06002408static void bc_array_expand(BcVec *a, size_t len)
2409{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002410 if (a->dtor == bc_num_free
2411 // && a->size == sizeof(BcNum) - always true
2412 ) {
2413 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002414 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002415 bc_num_init_DEF_SIZE(&n);
2416 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002417 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002418 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002419 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002420 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002421 bc_array_init(&v, true);
2422 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002423 }
2424 }
2425}
2426
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002427static void bc_array_copy(BcVec *d, const BcVec *s)
2428{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002429 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002430 size_t i;
2431
2432 bc_vec_pop_all(d);
2433 bc_vec_expand(d, s->cap);
2434 d->len = s->len;
2435
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002436 dnum = (void*)d->v;
2437 snum = (void*)s->v;
2438 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002439 bc_num_init(dnum, snum->len);
2440 bc_num_copy(dnum, snum);
2441 }
2442}
2443
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002444#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002445static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002446{
2447 d->t = src->t;
2448
2449 switch (d->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002450 case BC_RESULT_TEMP:
2451 case BC_RESULT_IBASE:
2452 case BC_RESULT_SCALE:
2453 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002454 bc_num_init(&d->d.n, src->d.n.len);
2455 bc_num_copy(&d->d.n, &src->d.n);
2456 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002457 case BC_RESULT_VAR:
2458 case BC_RESULT_ARRAY:
2459 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002460 d->d.id.name = xstrdup(src->d.id.name);
2461 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002462 case BC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002463 IF_BC(case BC_RESULT_LAST:)
Gavin Howard01055ba2018-11-03 11:00:21 -06002464 case BC_RESULT_ONE:
2465 case BC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002466 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2467 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002468 }
2469}
2470#endif // ENABLE_DC
2471
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002472static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002473{
2474 BcResult *r = (BcResult *) result;
2475
2476 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002477 case BC_RESULT_TEMP:
2478 case BC_RESULT_IBASE:
2479 case BC_RESULT_SCALE:
2480 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002481 bc_num_free(&r->d.n);
2482 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002483 case BC_RESULT_VAR:
2484 case BC_RESULT_ARRAY:
2485 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002486 free(r->d.id.name);
2487 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002488 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002489 // Do nothing.
2490 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002491 }
2492}
2493
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002494static int bad_input_byte(char c)
2495{
2496 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2497 || c > 0x7e
2498 ) {
2499 bc_error_fmt("illegal character 0x%02x", c);
2500 return 1;
2501 }
2502 return 0;
2503}
2504
2505// Note: it _appends_ data from fp to vec.
2506static void bc_read_line(BcVec *vec, FILE *fp)
2507{
2508 again:
2509 fflush_and_check();
2510
2511#if ENABLE_FEATURE_BC_SIGNALS
2512 if (G_interrupt) { // ^C was pressed
2513 intr:
2514 if (fp != stdin) {
2515 // ^C while running a script (bc SCRIPT): die.
2516 // We do not return to interactive prompt:
2517 // user might be running us from a shell,
2518 // and SCRIPT might be intended to terminate
2519 // (e.g. contain a "halt" stmt).
2520 // ^C dropping user into a bc prompt instead of
2521 // the shell would be unexpected.
2522 xfunc_die();
2523 }
2524 // ^C while interactive input
2525 G_interrupt = 0;
2526 // GNU bc says "interrupted execution."
2527 // GNU dc says "Interrupt!"
2528 fputs("\ninterrupted execution\n", stderr);
2529 }
2530
2531# if ENABLE_FEATURE_EDITING
2532 if (G_ttyin && fp == stdin) {
2533 int n, i;
2534# define line_buf bb_common_bufsiz1
2535 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2536 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2537 if (n == 0) // ^C
2538 goto intr;
2539 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2540 return;
2541 }
2542 i = 0;
2543 for (;;) {
2544 char c = line_buf[i++];
2545 if (!c) break;
2546 if (bad_input_byte(c)) goto again;
2547 }
2548 bc_vec_concat(vec, line_buf);
2549# undef line_buf
2550 } else
2551# endif
2552#endif
2553 {
2554 int c;
2555 bool bad_chars = 0;
2556 size_t len = vec->len;
2557
2558 do {
2559#if ENABLE_FEATURE_BC_SIGNALS
2560 if (G_interrupt) {
2561 // ^C was pressed: ignore entire line, get another one
2562 vec->len = len;
2563 goto intr;
2564 }
2565#endif
2566 do c = fgetc(fp); while (c == '\0');
2567 if (c == EOF) {
2568 if (ferror(fp))
2569 bb_perror_msg_and_die("input error");
2570 // Note: EOF does not append '\n'
2571 break;
2572 }
2573 bad_chars |= bad_input_byte(c);
2574 bc_vec_pushByte(vec, (char)c);
2575 } while (c != '\n');
2576
2577 if (bad_chars) {
2578 // Bad chars on this line
2579 if (!G.prog.file) { // stdin
2580 // ignore entire line, get another one
2581 vec->len = len;
2582 goto again;
2583 }
2584 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
2585 }
2586 bc_vec_pushZeroByte(vec);
2587 }
2588}
2589
2590//
2591// Parsing routines
2592//
2593
2594static bool bc_num_strValid(const char *val, size_t base)
2595{
2596 BcDig b;
2597 bool radix;
2598
2599 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2600 radix = false;
2601 for (;;) {
2602 BcDig c = *val++;
2603 if (c == '\0')
2604 break;
2605 if (c == '.') {
2606 if (radix) return false;
2607 radix = true;
2608 continue;
2609 }
2610 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2611 return false;
2612 }
2613 return true;
2614}
2615
2616// Note: n is already "bc_num_zero()"ed,
2617// leading zeroes in "val" are removed
2618static void bc_num_parseDecimal(BcNum *n, const char *val)
2619{
2620 size_t len, i;
2621 const char *ptr;
2622
2623 len = strlen(val);
2624 if (len == 0)
2625 return;
2626
2627 bc_num_expand(n, len);
2628
2629 ptr = strchr(val, '.');
2630
2631 n->rdx = 0;
2632 if (ptr != NULL)
2633 n->rdx = (size_t)((val + len) - (ptr + 1));
2634
2635 for (i = 0; val[i]; ++i) {
2636 if (val[i] != '0' && val[i] != '.') {
2637 // Not entirely zero value - convert it, and exit
2638 i = len - 1;
2639 for (;;) {
2640 n->num[n->len] = val[i] - '0';
2641 ++n->len;
2642 skip_dot:
2643 if (i == 0) break;
2644 if (val[--i] == '.') goto skip_dot;
2645 }
2646 break;
2647 }
2648 }
2649 // if for() exits without hitting if(), the value is entirely zero
2650}
2651
2652// Note: n is already "bc_num_zero()"ed,
2653// leading zeroes in "val" are removed
2654static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2655{
2656 BcStatus s;
2657 BcNum temp, mult, result;
2658 BcNum base;
2659 BcDig base_digs[ULONG_NUM_BUFSIZE];
2660 BcDig c = '\0';
2661 unsigned long v;
2662 size_t i, digits;
2663
2664 for (i = 0; ; ++i) {
2665 if (val[i] == '\0')
2666 return;
2667 if (val[i] != '.' && val[i] != '0')
2668 break;
2669 }
2670
2671 bc_num_init_DEF_SIZE(&temp);
2672 bc_num_init_DEF_SIZE(&mult);
2673 base.cap = ARRAY_SIZE(base_digs);
2674 base.num = base_digs;
2675 bc_num_ulong2num(&base, base_t);
2676
2677 for (;;) {
2678 c = *val++;
2679 if (c == '\0') goto int_err;
2680 if (c == '.') break;
2681
2682 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2683
2684 s = zbc_num_mul(n, &base, &mult, 0);
2685 if (s) goto int_err;
2686 bc_num_ulong2num(&temp, v);
2687 s = zbc_num_add(&mult, &temp, n, 0);
2688 if (s) goto int_err;
2689 }
2690
2691 bc_num_init(&result, base.len);
2692 //bc_num_zero(&result); - already is
2693 bc_num_one(&mult);
2694
2695 digits = 0;
2696 for (;;) {
2697 c = *val++;
2698 if (c == '\0') break;
2699 digits++;
2700
2701 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2702
2703 s = zbc_num_mul(&result, &base, &result, 0);
2704 if (s) goto err;
2705 bc_num_ulong2num(&temp, v);
2706 s = zbc_num_add(&result, &temp, &result, 0);
2707 if (s) goto err;
2708 s = zbc_num_mul(&mult, &base, &mult, 0);
2709 if (s) goto err;
2710 }
2711
2712 s = zbc_num_div(&result, &mult, &result, digits);
2713 if (s) goto err;
2714 s = zbc_num_add(n, &result, n, digits);
2715 if (s) goto err;
2716
2717 if (n->len != 0) {
2718 if (n->rdx < digits)
2719 bc_num_extend(n, digits - n->rdx);
2720 } else
2721 bc_num_zero(n);
2722 err:
2723 bc_num_free(&result);
2724 int_err:
2725 bc_num_free(&mult);
2726 bc_num_free(&temp);
2727}
2728
2729static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
2730{
2731 if (!bc_num_strValid(val, base_t))
2732 RETURN_STATUS(bc_error("bad number string"));
2733
2734 bc_num_zero(n);
2735 while (*val == '0') val++;
2736
2737 if (base_t == 10)
2738 bc_num_parseDecimal(n, val);
2739 else
2740 bc_num_parseBase(n, val, base_t);
2741
2742 RETURN_STATUS(BC_STATUS_SUCCESS);
2743}
2744#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
2745
Gavin Howard01055ba2018-11-03 11:00:21 -06002746static void bc_lex_lineComment(BcLex *l)
2747{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002748 // Try: echo -n '#foo' | bc
2749 size_t i;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002750 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002751 i = l->i;
2752 while (i < l->len && l->buf[i] != '\n')
2753 i++;
2754 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002755}
2756
2757static void bc_lex_whitespace(BcLex *l)
2758{
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002759 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002760 for (;;) {
2761 char c = l->buf[l->i];
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002762 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002763 break;
2764 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002765 break;
2766 l->i++;
2767 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002768}
2769
Denys Vlasenko29301232018-12-11 15:29:32 +01002770static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002771{
2772 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002773 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002774 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002775
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002776 pt = (start == '.');
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002777 l->t.t = XC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002778 ccnt = i = 0;
2779 for (;;) {
2780 char c = buf[i];
2781 if (c == '\0')
2782 break;
2783 if (c == '\\' && buf[i + 1] == '\n') {
2784 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002785 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002786 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002787 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002788 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2789 if (c != '.') break;
2790 // if '.' was already seen, stop on second one:
2791 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002792 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002793 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002794 // buf[i] is one of "0-9A-F."
2795 i++;
2796 if (c != '.')
2797 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002798 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002799 //ccnt is the number of chars in the number string, excluding possible
2800 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2801 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002802 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002803
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002804 // This might overestimate the size, if there are "\<NL>"'s
2805 // in the number. Subtracting number_of_backslashes*2 correctly
2806 // is not that easy: consider that in the case of "NNN.\<NL>"
2807 // loop above will count "\<NL>" before it realizes it is not
2808 // in fact *inside* the number:
2809 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002810
Denys Vlasenko64074a12018-12-07 15:50:14 +01002811 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2812 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2813 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002814 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002815 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002816
Denys Vlasenko7d628012018-12-04 21:46:47 +01002817 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002818 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002819 bc_vec_push(&l->t.v, &start);
2820
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002821 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002822 // If we have hit a backslash, skip it. We don't have
2823 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002824 if (*buf == '\\') {
2825 buf += 2;
2826 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002827 continue;
2828 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002829 bc_vec_push(&l->t.v, buf);
2830 buf++;
2831 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002832 }
2833
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002834 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002835
Denys Vlasenko29301232018-12-11 15:29:32 +01002836 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002837}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002838#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002839
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002840static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002841{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002842 size_t i;
2843 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002844
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002845 l->t.t = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06002846
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002847 i = 0;
2848 buf = l->buf + l->i - 1;
2849 for (;;) {
2850 char c = buf[i];
2851 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2852 i++;
2853 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002854
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002855#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002856 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2857 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2858 if (i > BC_MAX_STRING)
2859 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2860 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002861#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002862 bc_vec_string(&l->t.v, i, buf);
2863
2864 // Increment the index. We minus 1 because it has already been incremented.
2865 l->i += i - 1;
2866
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002867 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002868}
2869
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002870static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002871{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002872 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002873}
2874
2875static void bc_lex_free(BcLex *l)
2876{
2877 bc_vec_free(&l->t.v);
2878}
2879
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002880static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002881{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002882 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002883 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002884}
2885
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002886static bool bc_lex_more_input(BcLex *l)
2887{
2888 size_t str;
2889 bool comment;
2890
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002891 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002892
2893 // This loop is complex because the vm tries not to send any lines that end
2894 // with a backslash to the parser. The reason for that is because the parser
2895 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2896 // case, and for strings and comments, the parser will expect more stuff.
2897 comment = false;
2898 str = 0;
2899 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002900 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002901 char *string;
2902
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002903 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002904 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002905 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002906 break;
2907
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002908 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002909 while (*string) {
2910 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002911 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002912 if (IS_BC)
2913 str ^= (c == '"');
2914 else {
2915 if (c == ']')
2916 str -= 1;
2917 else if (c == '[')
2918 str += 1;
2919 }
2920 }
2921 string++;
2922 if (c == '/' && *string == '*') {
2923 comment = true;
2924 string++;
2925 continue;
2926 }
2927 if (c == '*' && *string == '/') {
2928 comment = false;
2929 string++;
2930 }
2931 }
2932 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002933 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002934 continue;
2935 }
2936
2937 // Check for backslash+newline.
2938 // we do not check that last char is '\n' -
2939 // if it is not, then it's EOF, and looping back
2940 // to bc_read_line() will detect it:
2941 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002942 if (string >= G.input_buffer.v && *string == '\\') {
2943 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002944 continue;
2945 }
2946
2947 break;
2948 }
2949
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002950 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002951 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002952// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2953 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002954
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002955 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002956}
2957
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002958IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2959IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002960#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2961#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002962
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002963static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002964{
2965 BcStatus s;
2966
2967 l->t.last = l->t.t;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01002968 if (l->t.last == XC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002969
2970 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002971 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002972 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002973
2974 // Loop until failure or we don't have whitespace. This
2975 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002976 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002977 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002978 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002979 if (l->i == l->len) {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01002980 l->t.t = XC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002981 if (!G.input_fp)
2982 RETURN_STATUS(BC_STATUS_SUCCESS);
2983 if (!bc_lex_more_input(l)) {
2984 G.input_fp = NULL;
2985 RETURN_STATUS(BC_STATUS_SUCCESS);
2986 }
2987 // here it's guaranteed that l->i is below l->len
2988 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002989 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002990 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2991 l->buf + l->i);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002992 if (IS_BC) {
2993 IF_BC(s = zbc_lex_token(l));
2994 } else {
2995 IF_DC(s = zdc_lex_token(l));
2996 }
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002997 } while (!s && l->t.t == XC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01002998 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002999
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003000 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003001}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003002#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003003
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003004#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01003005static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
3006{
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003007 if (l->t.t == XC_LEX_NLINE)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003008 RETURN_STATUS(zbc_lex_next(l));
3009 RETURN_STATUS(BC_STATUS_SUCCESS);
3010}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003011#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003012
3013static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
3014{
3015 BcStatus s;
3016 s = zbc_lex_next(l);
3017 if (s) RETURN_STATUS(s);
3018 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
3019 s = zbc_lex_skip_if_at_NLINE(l);
3020 RETURN_STATUS(s);
3021}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003022#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003023#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01003024
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003025static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003026{
3027 l->buf = text;
3028 l->i = 0;
3029 l->len = strlen(text);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003030 l->t.t = l->t.last = XC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003031 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003032}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003033#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003034
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003035#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003036static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003037{
3038 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003039 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003040 const char *buf = l->buf + l->i - 1;
3041
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003042 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3043 const char *keyword8 = bc_lex_kws[i].name8;
3044 unsigned j = 0;
3045 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3046 j++;
3047 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003048 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003049 if (keyword8[j] != '\0')
3050 continue;
3051 match:
3052 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003053 if (isalnum(buf[j]) || buf[j]=='_')
3054 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003055 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003056 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003057 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003058 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003059 }
3060
3061 // We minus 1 because the index has already been incremented.
3062 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003063 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003064 }
3065
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003066 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003067 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003068
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003069 if (l->t.v.len > 2) {
3070 // Prevent this:
3071 // >>> qwe=1
3072 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3073 // '
3074 unsigned len = strchrnul(buf, '\n') - buf;
3075 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3076 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003077
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003078 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003079}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003080#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003081
Denys Vlasenko26819db2018-12-12 16:08:46 +01003082static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003083{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003084 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003085
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003086 l->t.t = XC_LEX_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06003087
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003088 nls = 0;
3089 i = l->i;
3090 for (;;) {
3091 char c = l->buf[i];
3092 if (c == '\0') {
3093 l->i = i;
3094 RETURN_STATUS(bc_error("string end could not be found"));
3095 }
3096 if (c == '"')
3097 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003098 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003099 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003100 }
3101
3102 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003103 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3104 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3105 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003106 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003107 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003108 bc_vec_string(&l->t.v, len, l->buf + l->i);
3109
3110 l->i = i + 1;
3111 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003112 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003113
Denys Vlasenko26819db2018-12-12 16:08:46 +01003114 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003115}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003116#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003117
Denys Vlasenko0a238142018-12-14 16:48:34 +01003118static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003119{
3120 if (l->buf[l->i] == '=') {
3121 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003122 with_and_without >>= 8; // store "with" value
3123 } // else store "without" value
3124 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003125}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003126#define bc_lex_assign(l, with, without) \
3127 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003128
Denys Vlasenko29301232018-12-11 15:29:32 +01003129static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003130{
3131 size_t i, nls = 0;
3132 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003133
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003134 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003135 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003136 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003137 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003138 check_star:
3139 if (c == '*') {
3140 c = buf[++i];
3141 if (c == '/')
3142 break;
3143 goto check_star;
3144 }
3145 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003146 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003147 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003148 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003149 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 }
3151
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003152 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003154 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003155
Denys Vlasenko29301232018-12-11 15:29:32 +01003156 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003157}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003158#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003159
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003160#undef zbc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003161static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003162{
3163 BcStatus s = BC_STATUS_SUCCESS;
3164 char c = l->buf[l->i++], c2;
3165
3166 // This is the workhorse of the lexer.
3167 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003168// case '\0': // probably never reached
3169// l->i--;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003170// l->t.t = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003171// l->newline = true;
3172// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003173 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003174 l->t.t = XC_LEX_NLINE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003175 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003176 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003177 case '\t':
3178 case '\v':
3179 case '\f':
3180 case '\r':
3181 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003182 bc_lex_whitespace(l);
3183 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003184 case '!':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003185 bc_lex_assign(l, XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003186 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003187 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003188 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003189 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003190 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003191 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003192 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003193 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003194 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003195 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003196 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003198 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 case '%':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003200 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003201 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003202 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 c2 = l->buf[l->i];
3204 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003205 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003206 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003207 ++l->i;
3208 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003209 } else {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003210 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003211 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003212 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003214 case '(':
3215 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3217 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 case '*':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003219 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 c2 = l->buf[l->i];
3223 if (c2 == '+') {
3224 ++l->i;
3225 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003226 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003227 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003228 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003229 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003230 l->t.t = BC_LEX_COMMA;
3231 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003232 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003233 c2 = l->buf[l->i];
3234 if (c2 == '-') {
3235 ++l->i;
3236 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003237 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003238 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003239 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003240 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003241 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003242 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003243 else {
3244 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003245 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003246 }
3247 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003248 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003249 c2 = l->buf[l->i];
3250 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003251 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003253 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003254 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003255 case '0':
3256 case '1':
3257 case '2':
3258 case '3':
3259 case '4':
3260 case '5':
3261 case '6':
3262 case '7':
3263 case '8':
3264 case '9':
3265 case 'A':
3266 case 'B':
3267 case 'C':
3268 case 'D':
3269 case 'E':
3270 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003271 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003272 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003273 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003274 l->t.t = BC_LEX_SCOLON;
3275 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003276 case '<':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003277 bc_lex_assign(l, XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003278 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 case '=':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003280 bc_lex_assign(l, XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003281 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003282 case '>':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003283 bc_lex_assign(l, XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003284 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 case '[':
3286 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003287 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3288 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003289 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003290 if (l->buf[l->i] == '\n') {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003291 l->t.t = XC_LEX_WHITESPACE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003292 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003293 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003294 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003295 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003296 case '^':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003297 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
Gavin Howard01055ba2018-11-03 11:00:21 -06003298 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003299 case 'a':
3300 case 'b':
3301 case 'c':
3302 case 'd':
3303 case 'e':
3304 case 'f':
3305 case 'g':
3306 case 'h':
3307 case 'i':
3308 case 'j':
3309 case 'k':
3310 case 'l':
3311 case 'm':
3312 case 'n':
3313 case 'o':
3314 case 'p':
3315 case 'q':
3316 case 'r':
3317 case 's':
3318 case 't':
3319 case 'u':
3320 case 'v':
3321 case 'w':
3322 case 'x':
3323 case 'y':
3324 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003325 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003326 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003327 case '{':
3328 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003329 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3330 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003331 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003332 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003333 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003334 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003335 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003336 ++l->i;
3337 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003338 } else {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003339 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003340 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003341 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003342 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003343 default:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003344 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003345 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003346 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003347 }
3348
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003349 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003350}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003351#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003352#endif // ENABLE_BC
3353
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003354#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003355static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003356{
Denys Vlasenko81293c82018-12-24 01:53:55 +01003357 if (G_exreg && isspace(l->buf[l->i])) {
3358 bc_lex_whitespace(l); // eats whitespace (but not newline)
3359 l->i++; // bc_lex_name() expects this
Denys Vlasenko29301232018-12-11 15:29:32 +01003360 bc_lex_name(l);
Denys Vlasenko81293c82018-12-24 01:53:55 +01003361 } else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003362 bc_vec_pop_all(&l->t.v);
Denys Vlasenko81293c82018-12-24 01:53:55 +01003363 bc_vec_push(&l->t.v, &l->buf[l->i++]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003364 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003365 l->t.t = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003366 }
3367
Denys Vlasenko29301232018-12-11 15:29:32 +01003368 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003369}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003370#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003371
Denys Vlasenko29301232018-12-11 15:29:32 +01003372static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003373{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003374 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003375
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003376 l->t.t = XC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003377 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003378
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003379 nls = 0;
3380 depth = 1;
3381 i = l->i;
3382 for (;;) {
3383 char c = l->buf[i];
3384 if (c == '\0') {
3385 l->i = i;
3386 RETURN_STATUS(bc_error("string end could not be found"));
3387 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003388 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003389 if (i == l->i || l->buf[i - 1] != '\\') {
3390 if (c == '[') depth++;
3391 if (c == ']')
3392 if (--depth == 0)
3393 break;
3394 }
3395 bc_vec_push(&l->t.v, &l->buf[i]);
3396 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003397 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003398 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003399
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003400 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003401 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3402 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3403 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003404 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003405 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003406
3407 l->i = i;
3408 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003409 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003410
Denys Vlasenko29301232018-12-11 15:29:32 +01003411 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003412}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003413#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003414
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003415#undef zdc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003416static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003417{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003418 static const //BcLexType - should be this type, but narrower type saves size:
3419 uint8_t
3420 dc_lex_regs[] = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003421 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 +01003422 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3423 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003424 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003425 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003426
Denys Vlasenko81293c82018-12-24 01:53:55 +01003427 BcStatus s;
3428 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003429 size_t i;
3430
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003431 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3432 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003433 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003434 }
3435
Denys Vlasenko81293c82018-12-24 01:53:55 +01003436 s = BC_STATUS_SUCCESS;
3437 c = l->buf[l->i++];
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003438 if (c >= '%' && c <= '~'
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003439 && (l->t.t = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003440 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003441 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003442 }
3443
3444 // This is the workhorse of the lexer.
3445 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003446// case '\0': // probably never reached
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003447// l->t.t = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003448// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003449 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003450 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003451 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003452 // only to allow interactive dc have a way to exit
3453 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003454 // on <enter>, not on _next_ token (which would mean
3455 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003456 // IOW: typing "1p<enter>" should print "1" _at once_,
3457 // not after some more input.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003458 l->t.t = XC_LEX_NLINE;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003459 l->newline = true;
3460 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003461 case '\t':
3462 case '\v':
3463 case '\f':
3464 case '\r':
3465 case ' ':
Denys Vlasenko81293c82018-12-24 01:53:55 +01003466 l->newline = 0; // was (c == '\n')
Gavin Howard01055ba2018-11-03 11:00:21 -06003467 bc_lex_whitespace(l);
3468 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003469 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003470 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003471 if (c2 == '=')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003472 l->t.t = XC_LEX_OP_REL_NE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003473 else if (c2 == '<')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003474 l->t.t = XC_LEX_OP_REL_LE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003475 else if (c2 == '>')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003476 l->t.t = XC_LEX_OP_REL_GE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003477 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003478 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003479 ++l->i;
3480 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003481 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003482 bc_lex_lineComment(l);
3483 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003484 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003485 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003486 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003487 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003488 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003489 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003490 case '0':
3491 case '1':
3492 case '2':
3493 case '3':
3494 case '4':
3495 case '5':
3496 case '6':
3497 case '7':
3498 case '8':
3499 case '9':
3500 case 'A':
3501 case 'B':
3502 case 'C':
3503 case 'D':
3504 case 'E':
3505 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003506 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003507 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003508 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003509 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003510 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003511 default:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003512 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003513 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003514 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003515 }
3516
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003517 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003518}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003519#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003520#endif // ENABLE_DC
3521
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003522static void bc_parse_push(BcParse *p, char i)
3523{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003524 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003525 bc_vec_pushByte(&p->func->code, i);
3526}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003527
Gavin Howard01055ba2018-11-03 11:00:21 -06003528static void bc_parse_pushName(BcParse *p, char *name)
3529{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003530 while (*name)
3531 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003532 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003533}
3534
3535static void bc_parse_pushIndex(BcParse *p, size_t idx)
3536{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003537 size_t mask;
3538 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003539
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003540 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003541 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3542 amt = sizeof(idx);
3543 do {
3544 if (idx & mask) break;
3545 mask >>= 8;
3546 amt--;
3547 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003548
3549 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003550
3551 while (idx != 0) {
3552 bc_parse_push(p, (unsigned char)idx);
3553 idx >>= 8;
3554 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003555}
3556
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003557#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003558static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3559{
3560 bc_parse_push(p, BC_INST_JUMP);
3561 bc_parse_pushIndex(p, idx);
3562}
3563
3564static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3565{
3566 bc_parse_push(p, BC_INST_JUMP_ZERO);
3567 bc_parse_pushIndex(p, idx);
3568}
3569
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003570static BC_STATUS zbc_parse_pushSTR(BcParse *p)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003571{
3572 char *str = xstrdup(p->l.t.v.v);
3573
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003574 bc_parse_push(p, XC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003575 bc_parse_pushIndex(p, p->func->strs.len);
3576 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003577
3578 RETURN_STATUS(zbc_lex_next(&p->l));
3579}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003580#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003581#endif
3582
3583static void bc_parse_pushNUM(BcParse *p)
3584{
3585 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003586#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003587 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003588#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003589 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003590#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003591 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003592#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003593 bc_parse_push(p, XC_INST_NUM);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003594 bc_parse_pushIndex(p, idx);
3595}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003596
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003597static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003598{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003599 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003600
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003601 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003602}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003603#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003604
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003605// Called when parsing or execution detects a failure,
3606// resets execution structures.
3607static void bc_program_reset(void)
3608{
3609 BcFunc *f;
3610 BcInstPtr *ip;
3611
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003612 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003613 bc_vec_pop_all(&G.prog.results);
3614
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003615 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003616 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003617 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003618}
3619
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003620// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003621// resets parsing structures.
3622static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003623{
3624 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003625 bc_func_free(p->func);
3626 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003627
Denys Vlasenko65e10462018-12-19 15:13:14 +01003628 p->fidx = BC_PROG_MAIN;
3629 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003630 }
3631
3632 p->l.i = p->l.len;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003633 p->l.t.t = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003634
Denys Vlasenko503faf92018-12-20 16:24:18 +01003635 IF_BC(bc_vec_pop_all(&p->exits);)
3636 IF_BC(bc_vec_pop_all(&p->conds);)
3637 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003638
Denys Vlasenkod38af482018-12-04 19:11:02 +01003639 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003640}
3641
3642static void bc_parse_free(BcParse *p)
3643{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003644 IF_BC(bc_vec_free(&p->exits);)
3645 IF_BC(bc_vec_free(&p->conds);)
3646 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003647 bc_lex_free(&p->l);
3648}
3649
Denys Vlasenkofa210792018-12-19 19:35:40 +01003650static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003651{
3652 memset(p, 0, sizeof(BcParse));
3653
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003654 bc_lex_init(&p->l);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003655 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3656 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3657 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003658
Denys Vlasenkofa210792018-12-19 19:35:40 +01003659 p->fidx = fidx;
3660 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003661}
3662
Denys Vlasenko04715442018-12-21 00:10:26 +01003663static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003664{
Denys Vlasenko04715442018-12-21 00:10:26 +01003665 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003666 BcFunc f;
3667 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003668 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003669 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003670 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003671}
3672
3673#if ENABLE_BC
3674
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003675// Note: takes ownership of 'name' (must be malloced)
3676static size_t bc_program_addFunc(char *name)
3677{
3678 size_t idx;
3679 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003680 int inserted;
3681
3682 entry.name = name;
3683 entry.idx = G.prog.fns.len;
3684
3685 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3686 if (!inserted) free(name);
3687
3688 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3689 idx = entry_ptr->idx;
3690
3691 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003692 // There is already a function with this name.
3693 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003694 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003695 bc_func_free(func);
3696 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003697 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003698 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003699 }
3700
3701 return idx;
3702}
3703
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003704#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3705#define BC_PARSE_LEAF(p, rparen) \
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003706 (((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) || (rparen) || \
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003707 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3708
3709// We can calculate the conversion between tokens and exprs by subtracting the
3710// position of the first operator in the lex enum and adding the position of the
3711// first in the expr enum. Note: This only works for binary operators.
Denys 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 Vlasenko23ea0732018-12-24 15:05:49 +01003742 if (p->l.t.t == 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 Vlasenko2ea53a42018-12-14 17:51:17 +01003800 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
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;
3807 if (p->l.t.t != BC_LEX_RPAREN) {
3808 for (;;) {
3809 s = zbc_parse_expr(p, flags);
3810 if (s) RETURN_STATUS(s);
3811 nparams++;
3812 if (p->l.t.t != BC_LEX_COMMA) {
3813 if (p->l.t.t == BC_LEX_RPAREN)
3814 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
3841 if (p->l.t.t != 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
3870 name = xstrdup(p->l.t.v.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
3874 if (p->l.t.t == 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
3878 if (p->l.t.t == 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 Vlasenko7f2d59c2018-12-18 16:24:07 +01003895 } else if (p->l.t.t == 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);
3922 if (p->l.t.t != 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);
3926 if (p->l.t.t != 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);
3941 if (p->l.t.t != 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 Vlasenko26819db2018-12-12 16:08:46 +01003951 if (p->l.t.t != 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
3967 if (p->l.t.t != 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);
3981 if (p->l.t.t != BC_LEX_RPAREN)
3982 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 ) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004001 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
4002 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 {
Gavin Howard01055ba2018-11-03 11:00:21 -06004005 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
4006 *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);
Gavin Howard01055ba2018-11-03 11:00:21 -06004010 type = p->l.t.t;
4011
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);
Gavin Howard01055ba2018-11-03 11:00:21 -06004029 if (p->l.t.t == 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 Vlasenko8cd468f2018-12-12 14:54:38 +01004046static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004047 bool rparen, size_t *nexprs)
4048{
4049 BcStatus s;
4050 BcLexType type;
4051 BcInst etype = *prev;
4052
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004053 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004054 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004055
4056 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004057 (etype >= XC_INST_NUM && etype <= XC_INST_SQRT) ?
Denys Vlasenko69560f42018-12-24 14:14:23 +01004058 XC_LEX_OP_MINUS :
4059 XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004060 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004061
4062 // We can just push onto the op stack because this is the largest
4063 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004064 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004065 bc_vec_push(&p->ops, &type);
4066 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004067 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004068
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004069 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004070}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004071#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004072
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004073static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004074{
4075 BcStatus s;
4076 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004077
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004078 for (;;) {
4079 s = zbc_lex_next(&p->l);
4080 if (s) RETURN_STATUS(s);
4081 type = p->l.t.t;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004082 if (type == XC_LEX_STR) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004083 s = zbc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004084 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004085 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004086 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004087 if (s) RETURN_STATUS(s);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004088 bc_parse_push(p, XC_INST_PRINT_POP);
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004089 if (p->l.t.t != BC_LEX_COMMA)
4090 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004091 }
4092
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004093 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004094}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004095#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004096
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004097static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004098{
4099 BcStatus s;
4100 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004101
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004102 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004103 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004104 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004105
4106 t = p->l.t.t;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004107 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Gavin Howard01055ba2018-11-03 11:00:21 -06004108 bc_parse_push(p, BC_INST_RET0);
4109 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004110 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004111 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004112 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004113 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004114 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004115 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004116 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004117
4118 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004119 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004120 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06004121 }
4122
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004123 bc_parse_push(p, XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004124 }
4125
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004126 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004127 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004128}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004129#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004130
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004131static void rewrite_label_to_current(BcParse *p, size_t idx)
4132{
4133 size_t *label = bc_vec_item(&p->func->labels, idx);
4134 *label = p->func->code.len;
4135}
4136
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004137static BC_STATUS zbc_parse_if(BcParse *p)
4138{
4139 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004140 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004141
4142 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4143 s = zbc_lex_next(&p->l);
4144 if (s) RETURN_STATUS(s);
4145 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
4146
4147 s = zbc_lex_next(&p->l);
4148 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004149 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004150 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004151 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004152
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004153 // Encode "if zero, jump to ..."
4154 // Pushed value (destination of the jump) is uninitialized,
4155 // will be rewritten to be address of "end of if()" or of "else".
4156 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004157 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004158
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004159 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004160 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004161
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004162 dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4163 if (p->l.t.t == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004164 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004165
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004166 // Encode "after then_stmt, jump to end of if()"
4167 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4168 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004169 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004170
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004171 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 +01004172 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004173
Denys Vlasenko15850832018-12-16 21:40:54 +01004174 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004175
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004176 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004177 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004178 }
4179
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004180 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 +01004181 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004182
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004183 dbg_lex_done("%s:%d done", __func__, __LINE__);
4184 RETURN_STATUS(s);
4185}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004186#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004187
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004188static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004189{
4190 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004191 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004192 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004193
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004194 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004195 if (s) RETURN_STATUS(s);
4196 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004197 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004198 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004199
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004200 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004201 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004202 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004203
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004204 bc_vec_push(&p->exits, &ip_idx);
4205 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004206
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004207 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004208 if (s) RETURN_STATUS(s);
4209 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004210
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004211 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004212
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004213 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004214 if (s) RETURN_STATUS(s);
4215
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004216 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004217 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004218
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004219 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004220 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004221
4222 bc_vec_pop(&p->exits);
4223 bc_vec_pop(&p->conds);
4224
4225 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004226}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004227#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004228
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004229static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004230{
4231 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004232 size_t cond_idx, exit_idx, body_idx, update_idx;
4233
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004234 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004235 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004236 if (s) RETURN_STATUS(s);
4237 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004238 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004239 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004240
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004241 if (p->l.t.t != BC_LEX_SCOLON) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004242 s = zbc_parse_expr(p, 0);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004243 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004244 if (s) RETURN_STATUS(s);
4245 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004246 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004247 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4248 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004249
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004250 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004251 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004252 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004253
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004254 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004255 update_idx = cond_idx + 1;
4256 body_idx = update_idx + 1;
4257 exit_idx = body_idx + 1;
4258
Gavin Howard01055ba2018-11-03 11:00:21 -06004259 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004260 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004261 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004262 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004263 // This is safe to set because the current token is a semicolon,
4264 // which has no string requirement.
4265 bc_vec_string(&p->l.t.v, 1, "1");
4266 bc_parse_pushNUM(p);
Denys Vlasenko00646792018-12-05 18:12:27 +01004267 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004268 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004269 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004270
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004271 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004272
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004273 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004274 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004275
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004276 bc_parse_pushJUMP_ZERO(p, exit_idx);
4277 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004278
Gavin Howard01055ba2018-11-03 11:00:21 -06004279 bc_vec_push(&p->conds, &update_idx);
4280 bc_vec_push(&p->func->labels, &p->func->code.len);
4281
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004282 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004283 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004284 if (s) RETURN_STATUS(s);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004285 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004286 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004287 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004288 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004289 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4290 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004291
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004292 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004293 bc_vec_push(&p->func->labels, &p->func->code.len);
4294
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004295 bc_vec_push(&p->exits, &exit_idx);
4296 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004297
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004298 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004299 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004300
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004301 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004302 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004303
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004304 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004305 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004306
4307 bc_vec_pop(&p->exits);
4308 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004309
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004310 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004311}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004312#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004313
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004314static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004315{
4316 BcStatus s;
4317 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004318
Gavin Howard01055ba2018-11-03 11:00:21 -06004319 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004320 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4321 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004322 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004323 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004324 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004325 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004326
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004327 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004328
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004329 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004330 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004331
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004332 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != XC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004333 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004334
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004335 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004336}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004337#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004338
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004339static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4340{
4341 BcId *autoid;
4342 BcId a;
4343 size_t i;
4344
4345 autoid = (void*)f->autos.v;
4346 for (i = 0; i < f->autos.len; i++, autoid++) {
4347 if (strcmp(name, autoid->name) == 0)
4348 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
4349 }
4350
4351 a.idx = var;
4352 a.name = name;
4353
4354 bc_vec_push(&f->autos, &a);
4355
4356 RETURN_STATUS(BC_STATUS_SUCCESS);
4357}
4358#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4359
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004360static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004361{
4362 BcStatus s;
4363 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004364 char *name;
4365
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004366 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004367 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004368 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004369 if (p->l.t.t != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004370 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004371
4372 name = xstrdup(p->l.t.v.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004373 p->fidx = bc_program_addFunc(name);
4374 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004375
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004376 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004377 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004378 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004379 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004380 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004381 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004382
4383 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004384 if (p->l.t.t != 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
4387 ++p->func->nparams;
4388
4389 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004390 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004391 if (s) goto err;
4392
4393 var = p->l.t.t != BC_LEX_LBRACKET;
4394
4395 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004396 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004397 if (s) goto err;
4398
4399 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004400 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004401 goto err;
4402 }
4403
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004404 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004405 if (s) goto err;
4406 }
4407
4408 comma = p->l.t.t == BC_LEX_COMMA;
4409 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004410 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004411 if (s) goto err;
4412 }
4413
Denys Vlasenko29301232018-12-11 15:29:32 +01004414 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004415 if (s) goto err;
4416 }
4417
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004418 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004419
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004420 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004421 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004422
4423 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004424 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004425
Denys Vlasenko202dd192018-12-16 17:30:35 +01004426 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4427 s = zbc_lex_skip_if_at_NLINE(&p->l);
4428 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004429 //GNU bc requires a {} block even if function body has single stmt, enforce this?
4430 if (p->l.t.t != BC_LEX_LBRACE)
4431 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004432
4433 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004434 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004435 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004436 if (s) RETURN_STATUS(s);
4437
4438 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004439
4440 // Subsequent code generation is into main program
4441 p->fidx = BC_PROG_MAIN;
4442 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004443
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004444 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004445 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004446 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004447 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004448 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004449 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004450}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004451#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004452
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004453static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004454{
4455 BcStatus s;
4456 bool comma, var, one;
4457 char *name;
4458
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004459 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004460 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004461 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004462
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004463 comma = false;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004464 one = p->l.t.t == XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06004465
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004466 while (p->l.t.t == XC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004467 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004468 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004469 if (s) goto err;
4470
4471 var = p->l.t.t != BC_LEX_LBRACKET;
4472 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004473 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004474 if (s) goto err;
4475
4476 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004477 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004478 goto err;
4479 }
4480
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004481 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004482 if (s) goto err;
4483 }
4484
4485 comma = p->l.t.t == BC_LEX_COMMA;
4486 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004487 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004488 if (s) goto err;
4489 }
4490
Denys Vlasenko29301232018-12-11 15:29:32 +01004491 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004492 if (s) goto err;
4493 }
4494
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004495 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4496 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004497
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004498 if (p->l.t.t != XC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004499 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004500
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004501 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004502 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004503 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004504 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004505 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004506 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004507}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004508#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004509
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004510#undef zbc_parse_stmt_possibly_auto
4511static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004512{
4513 BcStatus s = BC_STATUS_SUCCESS;
4514
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004515 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004516
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004517 if (p->l.t.t == XC_LEX_NLINE) {
4518 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004519 RETURN_STATUS(zbc_lex_next(&p->l));
4520 }
4521 if (p->l.t.t == BC_LEX_SCOLON) {
4522 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4523 RETURN_STATUS(zbc_lex_next(&p->l));
4524 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004525
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004526 if (p->l.t.t == BC_LEX_LBRACE) {
4527 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4528 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004529 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004530 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004531 } while (p->l.t.t == XC_LEX_NLINE);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004532 if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) {
4533 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4534 s = zbc_parse_auto(p);
4535 if (s) RETURN_STATUS(s);
4536 }
4537 while (p->l.t.t != BC_LEX_RBRACE) {
4538 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4539 s = zbc_parse_stmt(p);
4540 if (s) RETURN_STATUS(s);
4541 }
4542 s = zbc_lex_next(&p->l);
4543 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4544 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004545 }
4546
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004547 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004548 switch (p->l.t.t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004549 case XC_LEX_OP_MINUS:
Gavin Howard01055ba2018-11-03 11:00:21 -06004550 case BC_LEX_OP_INC:
4551 case BC_LEX_OP_DEC:
Gavin Howard01055ba2018-11-03 11:00:21 -06004552 case BC_LEX_OP_BOOL_NOT:
4553 case BC_LEX_LPAREN:
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004554 case XC_LEX_NAME:
4555 case XC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06004556 case BC_LEX_KEY_IBASE:
4557 case BC_LEX_KEY_LAST:
4558 case BC_LEX_KEY_LENGTH:
4559 case BC_LEX_KEY_OBASE:
4560 case BC_LEX_KEY_READ:
4561 case BC_LEX_KEY_SCALE:
4562 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004563 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004564 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004565 case XC_LEX_STR:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004566 s = zbc_parse_pushSTR(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004567 bc_parse_push(p, XC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004568 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004569 case BC_LEX_KEY_BREAK:
4570 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004571 s = zbc_parse_break_or_continue(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004572 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004573 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004574 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004577 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004578 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004579 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004580 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004581 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004582 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004584 // "limits" is a compile-time command,
4585 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004586 printf(
4587 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4588 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4589 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4590 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4591 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4592 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4593 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4594 "Number of vars = "BC_MAX_VARS_STR "\n"
4595 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004596 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004597 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004598 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004599 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004600 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004601 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004602 // "quit" is a compile-time command. For example,
4603 // "if (0 == 1) quit" terminates when parsing the statement,
4604 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004605 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004606 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004607 if (!p->in_funcdef)
4608 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004609 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004610 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004611 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004612 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004613 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004614 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004615 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004616 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004617 }
4618
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004619 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004620 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004621}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004622#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004623
Denys Vlasenko99b37622018-12-15 20:06:59 +01004624static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004625{
4626 BcStatus s;
4627
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004628 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004629 if (p->l.t.t == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004630 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004631 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004632 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004633 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004634 } else {
4635 dbg_lex("%s:%d p->l.t.t:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.t.t);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004636 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004637 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004638
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004639 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004640 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004641}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004642#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004643
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004644// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004645static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004646{
4647 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004648 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004649 BcLexType top, t = p->l.t.t;
4650 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004651 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004652 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4653
Denys Vlasenko17df8822018-12-14 23:00:24 +01004654 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004655 paren_first = (p->l.t.t == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004656 nparens = nrelops = 0;
4657 paren_expr = rprn = done = get_token = assign = false;
4658 bin_last = true;
4659
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004660 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004661 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004662 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004663 case BC_LEX_OP_INC:
4664 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004665 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004666 rprn = get_token = bin_last = false;
4667 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004668 case XC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004669 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004670 rprn = get_token = false;
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004671 bin_last = (prev == XC_INST_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004672 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004673 case BC_LEX_OP_ASSIGN_POWER:
4674 case BC_LEX_OP_ASSIGN_MULTIPLY:
4675 case BC_LEX_OP_ASSIGN_DIVIDE:
4676 case BC_LEX_OP_ASSIGN_MODULUS:
4677 case BC_LEX_OP_ASSIGN_PLUS:
4678 case BC_LEX_OP_ASSIGN_MINUS:
4679 case BC_LEX_OP_ASSIGN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004680 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4681 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4682 && prev != XC_INST_OBASE && prev != BC_INST_LAST
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004683 ) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004684 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004685 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004686 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004687 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004688 break;
4689 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004690 // Fallthrough.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004691 case XC_LEX_OP_POWER:
4692 case XC_LEX_OP_MULTIPLY:
4693 case XC_LEX_OP_DIVIDE:
4694 case XC_LEX_OP_MODULUS:
4695 case XC_LEX_OP_PLUS:
4696 case XC_LEX_OP_REL_EQ:
4697 case XC_LEX_OP_REL_LE:
4698 case XC_LEX_OP_REL_GE:
4699 case XC_LEX_OP_REL_NE:
4700 case XC_LEX_OP_REL_LT:
4701 case XC_LEX_OP_REL_GT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004702 case BC_LEX_OP_BOOL_NOT:
4703 case BC_LEX_OP_BOOL_OR:
4704 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004705 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004706 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004707 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004708 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004709 }
Denys Vlasenko69560f42018-12-24 14:14:23 +01004710 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
Denys Vlasenko0154d782018-12-14 23:32:51 +01004711 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004712 bc_parse_operator(p, t, ops_bgn, &nexprs);
4713 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004714 rprn = get_token = false;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004715 bin_last = (t != BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004716 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004717 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004718 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004719 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004720 ++nparens;
4721 paren_expr = rprn = bin_last = false;
4722 get_token = true;
4723 bc_vec_push(&p->ops, &t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004724 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004725 case BC_LEX_RPAREN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004726 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004727 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004728 if (nparens == 0) {
4729 s = BC_STATUS_SUCCESS;
4730 done = true;
4731 get_token = false;
4732 break;
4733 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004734 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004735 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004736 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004737 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004738 --nparens;
4739 paren_expr = rprn = true;
4740 get_token = bin_last = false;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004741 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004742 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004743 case XC_LEX_NAME:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004744 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004745 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004746 paren_expr = true;
4747 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004748 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004749 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004750 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004751 case XC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004752 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004753 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004754 bc_parse_pushNUM(p);
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004755 nexprs++;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004756 prev = XC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004757 paren_expr = get_token = true;
4758 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004759 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004760 case BC_LEX_KEY_IBASE:
4761 case BC_LEX_KEY_LAST:
4762 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004763 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004764 return bc_error_bad_expression();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004765 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004766 bc_parse_push(p, (char) prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004767 paren_expr = get_token = true;
4768 rprn = bin_last = false;
4769 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004770 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004771 case BC_LEX_KEY_LENGTH:
4772 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004773 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004774 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004775 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004776 paren_expr = true;
4777 rprn = get_token = bin_last = false;
4778 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004779 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004780 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004781 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004782 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004783 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004784 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004785 paren_expr = true;
4786 rprn = get_token = bin_last = false;
4787 ++nexprs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004788 prev = XC_INST_READ;
Gavin Howard01055ba2018-11-03 11:00:21 -06004789 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004790 case BC_LEX_KEY_SCALE:
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 Vlasenko26819db2018-12-12 16:08:46 +01004793 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004794 paren_expr = true;
4795 rprn = get_token = bin_last = false;
4796 ++nexprs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004797 prev = XC_INST_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06004798 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004799 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004800 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004801 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004802 }
4803
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004804 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004805 }
4806
4807 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004808 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004809
4810 while (p->ops.len > ops_bgn) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004811 top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004812 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004813
4814 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004815 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004816
Denys Vlasenko0154d782018-12-14 23:32:51 +01004817 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004818
Denys Vlasenko69560f42018-12-24 14:14:23 +01004819 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004820 bc_vec_pop(&p->ops);
4821 }
4822
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004823 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004824 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004825
Gavin Howard01055ba2018-11-03 11:00:21 -06004826 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004827 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004828 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004829 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004830 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004831 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004832 }
4833
4834 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004835 if (paren_first || !assign) bc_parse_push(p, XC_INST_PRINT);
4836 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004837 }
4838
Denys Vlasenko17df8822018-12-14 23:00:24 +01004839 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004840 return s;
4841}
4842
Gavin Howard01055ba2018-11-03 11:00:21 -06004843#endif // ENABLE_BC
4844
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004845#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004846
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004847static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004848{
4849 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004850
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004851 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004852 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004853 if (p->l.t.t != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004854
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01004855 bc_parse_pushName(p, p->l.t.v.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004856
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004857 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004858}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004859#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004860
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004861static void dc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004862{
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004863 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004864 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004865
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004866 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004867
4868 str = xstrdup(p->l.t.v.v);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004869 bc_parse_push(p, XC_INST_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004870 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004871 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004872
4873 // Explanation needed here
4874 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004875 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004876
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004877 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004878}
4879
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004880static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004881{
4882 BcStatus s;
4883
4884 bc_parse_push(p, inst);
4885 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004886 s = zdc_parse_register(p);
4887 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004888 }
4889
4890 if (store) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004891 bc_parse_push(p, DC_INST_SWAP);
4892 bc_parse_push(p, XC_INST_ASSIGN);
4893 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004894 }
4895
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004896 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004897}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004898#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004899
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004900static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004901{
4902 BcStatus s;
4903
4904 bc_parse_push(p, inst);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004905 bc_parse_push(p, DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004906
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004907 s = zdc_parse_register(p);
4908 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004909
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004910 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004911 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004912
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004913 // Note that 'else' part can not be on the next line:
4914 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4915 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004916 if (p->l.t.t == DC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004917 s = zdc_parse_register(p);
4918 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004919 s = zbc_lex_next(&p->l);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004920 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004921 bc_parse_push(p, BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004922 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004923
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004924 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004925}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004926#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004927
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004928static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004929{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004930 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004931 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004932 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004933
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004934 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004935 s = BC_STATUS_SUCCESS;
4936 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004937 switch (t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004938 case XC_LEX_OP_REL_EQ:
4939 case XC_LEX_OP_REL_LE:
4940 case XC_LEX_OP_REL_GE:
4941 case XC_LEX_OP_REL_NE:
4942 case XC_LEX_OP_REL_LT:
4943 case XC_LEX_OP_REL_GT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004944 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
Denys Vlasenko69560f42018-12-24 14:14:23 +01004945 s = zdc_parse_cond(p, t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004946 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004947 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004948 case DC_LEX_SCOLON:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004949 case DC_LEX_COLON:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004950 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004951 s = zdc_parse_mem(p, XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004952 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004953 case XC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004954 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004955 dc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004956 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004957 case XC_LEX_NEG:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004958 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
4959 s = zbc_lex_next(&p->l);
4960 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004961 if (p->l.t.t != XC_LEX_NUMBER)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004962 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004963 bc_parse_pushNUM(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004964 bc_parse_push(p, XC_INST_NEG);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004965 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004966 case XC_LEX_NUMBER:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004967 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4968 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004969 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004970 case DC_LEX_READ:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004971 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004972 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004973 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004974 case DC_LEX_OP_ASSIGN:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004975 case DC_LEX_STORE_PUSH:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004976 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004977 assign = (t == DC_LEX_OP_ASSIGN);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004978 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004979 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004980 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004981 case DC_LEX_LOAD:
4982 case DC_LEX_LOAD_POP:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004983 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004984 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004985 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004986 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004987 case DC_LEX_STORE_IBASE:
4988 case DC_LEX_STORE_SCALE:
4989 case DC_LEX_STORE_OBASE:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004990 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004991 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004992 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004993 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004994 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004995 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4996 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004997 }
4998
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004999 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005000
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005001 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005002 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005003}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005004#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005005
Denys Vlasenko39287e02018-12-22 02:23:08 +01005006static BC_STATUS zdc_parse_expr(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005007{
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005008 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005009
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005010 i = (int)p->l.t.t - (int)XC_LEX_OP_POWER;
5011 if (i >= 0) {
5012 BcInst inst = dc_LEX_to_INST[i];
5013 if (inst != DC_INST_INVALID) {
5014 bc_parse_push(p, inst);
5015 RETURN_STATUS(zbc_lex_next(&p->l));
5016 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005017 }
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005018 RETURN_STATUS(zdc_parse_token(p, p->l.t.t));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005019}
5020#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5021
5022static BC_STATUS zdc_parse_exprs_until_eof(BcParse *p)
5023{
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01005024 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005025 while (p->l.t.t != XC_LEX_EOF) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005026 BcStatus s = zdc_parse_expr(p);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005027 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005028 }
5029
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005030 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005031 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005032}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005033#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005034
Gavin Howard01055ba2018-11-03 11:00:21 -06005035#endif // ENABLE_DC
5036
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005037//
5038// Execution engine
5039//
5040
5041#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5042#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5043
Denys Vlasenkodf515392018-12-02 19:27:48 +01005044static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005045{
Gavin Howard01055ba2018-11-03 11:00:21 -06005046 BcId e, *ptr;
5047 BcVec *v, *map;
5048 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005049 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005050
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005051 v = var ? &G.prog.vars : &G.prog.arrs;
5052 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005053
5054 e.name = id;
5055 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005056 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005057
5058 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005059 BcVec v2;
5060 bc_array_init(&v2, var);
5061 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005062 }
5063
5064 ptr = bc_vec_item(map, i);
5065 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005066 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005067}
5068
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005069// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005070static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005071{
Gavin Howard01055ba2018-11-03 11:00:21 -06005072 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005073 case BC_RESULT_STR:
5074 case BC_RESULT_TEMP:
5075 case BC_RESULT_IBASE:
5076 case BC_RESULT_SCALE:
5077 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005078 *num = &r->d.n;
5079 break;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005080 case BC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005081 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005082 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005083 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005084 size_t len;
5085
5086 str = *bc_program_const(r->d.id.idx);
5087 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005088
5089 bc_num_init(&r->d.n, len);
5090
5091 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005092 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005093 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005094 if (s) {
5095 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005096 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005097 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005098 *num = &r->d.n;
5099 r->t = BC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005100 break;
5101 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005102 case BC_RESULT_VAR:
5103 case BC_RESULT_ARRAY:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005104 case BC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005105 BcVec *v;
5106
Denys Vlasenkodf515392018-12-02 19:27:48 +01005107 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005108
5109 if (r->t == BC_RESULT_ARRAY_ELEM) {
5110 v = bc_vec_top(v);
5111 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5112 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005113 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005114 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005115 break;
5116 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005117#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005118 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005119 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005120 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005121 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005122 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005123 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005124#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005125#if SANITY_CHECKS
Denys Vlasenko503faf92018-12-20 16:24:18 +01005126 default:
5127 // Testing the theory that dc does not reach LAST/ONE
5128 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005129#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005130 }
5131
Denys Vlasenko29301232018-12-11 15:29:32 +01005132 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005133}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005134#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005135
Denys Vlasenko29301232018-12-11 15:29:32 +01005136static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005137 BcResult **r, BcNum **rn, bool assign)
5138{
5139 BcStatus s;
5140 bool hex;
5141 BcResultType lt, rt;
5142
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005143 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005144 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005145
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005146 *r = bc_vec_item_rev(&G.prog.results, 0);
5147 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005148
5149 lt = (*l)->t;
5150 rt = (*r)->t;
5151 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5152
Denys Vlasenko29301232018-12-11 15:29:32 +01005153 s = zbc_program_num(*l, ln, false);
5154 if (s) RETURN_STATUS(s);
5155 s = zbc_program_num(*r, rn, hex);
5156 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005157
5158 // We run this again under these conditions in case any vector has been
5159 // reallocated out from under the BcNums or arrays we had.
5160 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005161 s = zbc_program_num(*l, ln, false);
5162 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005163 }
5164
5165 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005166 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005167 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005168 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005169
Denys Vlasenko29301232018-12-11 15:29:32 +01005170 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005171}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005172#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005173
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005174static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005175{
5176 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005177 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005178 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005179}
5180
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005181// Note: *r and *n need not be initialized by caller
Denys Vlasenko29301232018-12-11 15:29:32 +01005182static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005183{
5184 BcStatus s;
5185
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005186 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005187 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005188 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005189
Denys Vlasenko29301232018-12-11 15:29:32 +01005190 s = zbc_program_num(*r, n, false);
5191 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005192
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005193 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005194 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005195
Denys Vlasenko29301232018-12-11 15:29:32 +01005196 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005197}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005198#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005199
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005200static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005201{
5202 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005203 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005204}
5205
Denys Vlasenko259137d2018-12-11 19:42:05 +01005206static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005207{
5208 BcStatus s;
5209 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005210 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005211
Denys Vlasenko29301232018-12-11 15:29:32 +01005212 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005213 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005214 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005215
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005216 s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005217 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 -06005218 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005219 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005220
Denys Vlasenko259137d2018-12-11 19:42:05 +01005221 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005222 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005223 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005224 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005225}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005226#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005227
Denys Vlasenko26819db2018-12-12 16:08:46 +01005228static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005229{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005230 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005231 BcStatus s;
5232 BcParse parse;
5233 BcVec buf;
5234 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005235 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005236
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005237 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005238 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005239
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005240 sv_file = G.prog.file;
5241 G.prog.file = NULL;
5242
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005243 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005244 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005245
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005246 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005247 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005248
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005249 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005250 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005251 if (IS_BC) {
5252 IF_BC(s = zbc_parse_expr(&parse, 0));
5253 } else {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005254 IF_DC(s = zdc_parse_exprs_until_eof(&parse));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005255 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005256 if (s) goto exec_err;
5257
Denys Vlasenko23ea0732018-12-24 15:05:49 +01005258 if (parse.l.t.t != XC_LEX_NLINE && parse.l.t.t != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005259 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005260 goto exec_err;
5261 }
5262
5263 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005264 ip.inst_idx = 0;
5265 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005266
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005267 bc_parse_push(&parse, XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005268 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005269 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005270 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005271 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005272 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005273 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005274}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005275#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005276
5277static size_t bc_program_index(char *code, size_t *bgn)
5278{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005279 unsigned char *bytes = (void*)(code + *bgn);
5280 unsigned amt;
5281 unsigned i;
5282 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005283
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005284 amt = *bytes++;
5285 *bgn += amt + 1;
5286
5287 amt *= 8;
5288 res = 0;
5289 for (i = 0; i < amt; i += 8)
5290 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005291
5292 return res;
5293}
5294
5295static char *bc_program_name(char *code, size_t *bgn)
5296{
5297 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005298 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005299
Denys Vlasenko694d2982018-12-18 19:17:11 +01005300 code += *bgn;
5301 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005302 i = 0;
5303 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005304 char c = *code++;
5305 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005306 break;
5307 s[i++] = c;
5308 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005309 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005310 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005311
5312 return s;
5313}
5314
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005315static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005316{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005317#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005318 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005319 // Example: echo '[]ap' | dc
5320 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005321 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005322 return;
5323 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005324#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005325 while (*str) {
5326 int c = *str++;
5327 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005328 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005329 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005330 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005331 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005332 case 'a':
5333 bb_putchar('\a');
5334 break;
5335 case 'b':
5336 bb_putchar('\b');
5337 break;
5338 case '\\':
5339 case 'e':
5340 bb_putchar('\\');
5341 break;
5342 case 'f':
5343 bb_putchar('\f');
5344 break;
5345 case 'n':
5346 bb_putchar('\n');
5347 G.prog.nchars = SIZE_MAX;
5348 break;
5349 case 'r':
5350 bb_putchar('\r');
5351 break;
5352 case 'q':
5353 bb_putchar('"');
5354 break;
5355 case 't':
5356 bb_putchar('\t');
5357 break;
5358 default:
5359 // Just print the backslash and following character.
5360 bb_putchar('\\');
5361 ++G.prog.nchars;
5362 bb_putchar(c);
5363 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005364 }
5365 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005366 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005367 }
5368}
5369
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005370static void bc_num_printNewline(void)
5371{
5372 if (G.prog.nchars == G.prog.len - 1) {
5373 bb_putchar('\\');
5374 bb_putchar('\n');
5375 G.prog.nchars = 0;
5376 }
5377}
5378
5379#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005380static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005381{
5382 (void) radix;
5383 bb_putchar((char) num);
5384 G.prog.nchars += width;
5385}
5386#endif
5387
5388static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5389{
5390 size_t exp, pow;
5391
5392 bc_num_printNewline();
5393 bb_putchar(radix ? '.' : ' ');
5394 ++G.prog.nchars;
5395
5396 bc_num_printNewline();
5397 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5398 continue;
5399
5400 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5401 size_t dig;
5402 bc_num_printNewline();
5403 dig = num / pow;
5404 num -= dig * pow;
5405 bb_putchar(((char) dig) + '0');
5406 }
5407}
5408
5409static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5410{
5411 if (radix) {
5412 bc_num_printNewline();
5413 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005414 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005415 }
5416
5417 bc_num_printNewline();
5418 bb_putchar(bb_hexdigits_upcase[num]);
5419 G.prog.nchars += width;
5420}
5421
5422static void bc_num_printDecimal(BcNum *n)
5423{
5424 size_t i, rdx = n->rdx - 1;
5425
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005426 if (n->neg) {
5427 bb_putchar('-');
5428 G.prog.nchars++;
5429 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005430
5431 for (i = n->len - 1; i < n->len; --i)
5432 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5433}
5434
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005435typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5436
Denys Vlasenkod3401432018-12-18 17:00:35 +01005437static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005438{
5439 BcStatus s;
5440 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005441 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005442 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005443 BcNum intp, fracp, digit, frac_len;
5444 unsigned long dig, *ptr;
5445 size_t i;
5446 bool radix;
5447
5448 if (n->len == 0) {
5449 print(0, width, false);
5450 RETURN_STATUS(BC_STATUS_SUCCESS);
5451 }
5452
5453 bc_vec_init(&stack, sizeof(long), NULL);
5454 bc_num_init(&intp, n->len);
5455 bc_num_init(&fracp, n->rdx);
5456 bc_num_init(&digit, width);
5457 bc_num_init(&frac_len, BC_NUM_INT(n));
5458 bc_num_copy(&intp, n);
5459 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005460 base.cap = ARRAY_SIZE(base_digs);
5461 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005462 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005463
5464 bc_num_truncate(&intp, intp.rdx);
5465 s = zbc_num_sub(n, &intp, &fracp, 0);
5466 if (s) goto err;
5467
5468 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005469 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005470 if (s) goto err;
5471 s = zbc_num_ulong(&digit, &dig);
5472 if (s) goto err;
5473 bc_vec_push(&stack, &dig);
5474 }
5475
5476 for (i = 0; i < stack.len; ++i) {
5477 ptr = bc_vec_item_rev(&stack, i);
5478 print(*ptr, width, false);
5479 }
5480
5481 if (!n->rdx) goto err;
5482
5483 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005484 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005485 if (s) goto err;
5486 s = zbc_num_ulong(&fracp, &dig);
5487 if (s) goto err;
5488 bc_num_ulong2num(&intp, dig);
5489 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5490 if (s) goto err;
5491 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005492 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005493 if (s) goto err;
5494 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005495 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005496 bc_num_free(&frac_len);
5497 bc_num_free(&digit);
5498 bc_num_free(&fracp);
5499 bc_num_free(&intp);
5500 bc_vec_free(&stack);
5501 RETURN_STATUS(s);
5502}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005503#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005504
5505static BC_STATUS zbc_num_printBase(BcNum *n)
5506{
5507 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005508 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005509 BcNumDigitOp print;
5510 bool neg = n->neg;
5511
5512 if (neg) {
5513 bb_putchar('-');
5514 G.prog.nchars++;
5515 }
5516
5517 n->neg = false;
5518
5519 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5520 width = 1;
5521 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005522 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005523 unsigned i = G.prog.ob_t - 1;
5524 width = 0;
5525 for (;;) {
5526 width++;
5527 i /= 10;
5528 if (i == 0)
5529 break;
5530 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005531 print = bc_num_printDigits;
5532 }
5533
Denys Vlasenkod3401432018-12-18 17:00:35 +01005534 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005535 n->neg = neg;
5536
5537 RETURN_STATUS(s);
5538}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005539#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005540
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005541static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5542{
5543 BcStatus s = BC_STATUS_SUCCESS;
5544
5545 bc_num_printNewline();
5546
5547 if (n->len == 0) {
5548 bb_putchar('0');
5549 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005550 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005551 bc_num_printDecimal(n);
5552 else
5553 s = zbc_num_printBase(n);
5554
5555 if (newline) {
5556 bb_putchar('\n');
5557 G.prog.nchars = 0;
5558 }
5559
5560 RETURN_STATUS(s);
5561}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005562#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005563
Denys Vlasenko29301232018-12-11 15:29:32 +01005564static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005565{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005566 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005567 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005568 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005569 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005570
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005571 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005572 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005573
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005574 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005575 s = zbc_program_num(r, &num, false);
5576 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005577
5578 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005579 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005580#if ENABLE_BC
5581 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5582#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005583 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005584 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005585
5586 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005587 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005588
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005589 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005590 for (;;) {
5591 char c = *str++;
5592 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005593 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005594 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005595 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005596 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005597 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005598 bc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005599 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005600 }
5601 }
5602
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005603 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005604
Denys Vlasenko29301232018-12-11 15:29:32 +01005605 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005606}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005607#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005608
Denys Vlasenko29301232018-12-11 15:29:32 +01005609static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005610{
5611 BcStatus s;
5612 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005613 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005614
Denys Vlasenko29301232018-12-11 15:29:32 +01005615 s = zbc_program_prep(&ptr, &num);
5616 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005617
5618 bc_num_init(&res.d.n, num->len);
5619 bc_num_copy(&res.d.n, num);
5620 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5621
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005622 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005623
Denys Vlasenko29301232018-12-11 15:29:32 +01005624 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005625}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005626#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005627
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005628static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005629{
5630 BcStatus s;
5631 BcResult *opd1, *opd2, res;
5632 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005633 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005634
Denys Vlasenko29301232018-12-11 15:29:32 +01005635 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005636 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005637
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005638 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005639
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005640 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005641 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005642 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005643 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005644 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005645 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005646 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005647 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005648 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005649 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005650 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005651 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005652 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005653 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005654 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005655 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005656 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005657 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005658 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005659 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005660 cond = (cond > 0);
5661 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005662 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005663 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005664 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005665 }
5666 }
5667
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005668 if (cond) bc_num_one(&res.d.n);
5669 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005670
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005671 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005672
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005673 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005674}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005675#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005676
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005677#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005678static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005679{
5680 BcNum n2;
5681 BcResult res;
5682
5683 memset(&n2, 0, sizeof(BcNum));
5684 n2.rdx = res.d.id.idx = r->d.id.idx;
5685 res.t = BC_RESULT_STR;
5686
5687 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005688 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005689 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005690 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005691 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005692 }
5693
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005694 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005695 bc_vec_push(v, &n2);
5696
Denys Vlasenko29301232018-12-11 15:29:32 +01005697 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005698}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005699#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005700#endif // ENABLE_DC
5701
Denys Vlasenko29301232018-12-11 15:29:32 +01005702static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005703{
5704 BcStatus s;
5705 BcResult *ptr, r;
5706 BcVec *v;
5707 BcNum *n;
5708
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005709 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005710 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005711
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005712 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005713 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005714 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005715 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005716
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005717#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005718 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005719 RETURN_STATUS(bc_error_variable_is_wrong_type());
5720 if (ptr->t == BC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005721 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005722#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005723
Denys Vlasenko29301232018-12-11 15:29:32 +01005724 s = zbc_program_num(ptr, &n, false);
5725 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005726
5727 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005728 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005729
5730 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005731 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005732 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005733 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005734 bc_array_init(&r.d.v, true);
5735 bc_array_copy(&r.d.v, (BcVec *) n);
5736 }
5737
5738 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005739 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005740
Denys Vlasenko29301232018-12-11 15:29:32 +01005741 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005742}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005743#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005744
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005745static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005746{
5747 BcStatus s;
5748 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005749 BcNum *l, *r;
5750 bool assign = (inst == XC_INST_ASSIGN);
5751 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005752
Denys Vlasenko29301232018-12-11 15:29:32 +01005753 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005754 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005755
5756 ib = left->t == BC_RESULT_IBASE;
5757 sc = left->t == BC_RESULT_SCALE;
5758
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005759#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005760 if (right->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005761 BcVec *v;
5762
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005763 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005764 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005765 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005766
Denys Vlasenkofa210792018-12-19 19:35:40 +01005767 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005768 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005769#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005770
5771 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005772 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005773 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005774 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005775 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005776
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005777#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005778 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005779 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005780
5781 if (assign)
5782 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005783 else {
5784 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005785 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 +01005786 }
5787 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005788#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005789 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005790#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005791
5792 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005793 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005794 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
Denys Vlasenko64074a12018-12-07 15:50:14 +01005795 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005796 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005797 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005798 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005799 size_t max;
5800 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005801
Denys Vlasenko29301232018-12-11 15:29:32 +01005802 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005803 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005804 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005805 if (sc) {
5806 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005807 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005808 } else {
5809 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005810 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005811 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005812 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005813 }
5814
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005815 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005816 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005817
5818 *ptr = (size_t) val;
5819 s = BC_STATUS_SUCCESS;
5820 }
5821
5822 bc_num_init(&res.d.n, l->len);
5823 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005824 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005825
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005826 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005827}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005828#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005829
Denys Vlasenko416ce762018-12-02 20:57:17 +01005830#if !ENABLE_DC
5831#define bc_program_pushVar(code, bgn, pop, copy) \
5832 bc_program_pushVar(code, bgn)
5833// for bc, 'pop' and 'copy' are always false
5834#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005835static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005836 bool pop, bool copy)
5837{
Gavin Howard01055ba2018-11-03 11:00:21 -06005838 BcResult r;
5839 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005840
5841 r.t = BC_RESULT_VAR;
5842 r.d.id.name = name;
5843
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005844#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005845 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005846 BcVec *v = bc_program_search(name, true);
5847 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005848
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005849 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005850 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005851 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005852 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005853
5854 if (!BC_PROG_STR(num)) {
5855 r.t = BC_RESULT_TEMP;
5856 bc_num_init_DEF_SIZE(&r.d.n);
5857 bc_num_copy(&r.d.n, num);
5858 } else {
5859 r.t = BC_RESULT_STR;
5860 r.d.id.idx = num->rdx;
5861 }
5862
5863 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005864 }
5865#endif // ENABLE_DC
5866
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005867 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005868
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005869 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005870}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005871#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005872
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005873static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005874{
5875 BcStatus s = BC_STATUS_SUCCESS;
5876 BcResult r;
5877 BcNum *num;
5878
5879 r.d.id.name = bc_program_name(code, bgn);
5880
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005881 if (inst == XC_INST_ARRAY) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005882 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005883 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005884 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005885 BcResult *operand;
5886 unsigned long temp;
5887
Denys Vlasenko29301232018-12-11 15:29:32 +01005888 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005889 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005890 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005891 if (s) goto err;
5892
5893 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005894 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005895 goto err;
5896 }
5897
5898 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005899 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005900 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005901 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005902 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005903 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005904}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005905#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005906
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005907#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005908static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005909{
5910 BcStatus s;
5911 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005912 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005913 char inst2 = inst;
5914
Denys Vlasenko29301232018-12-11 15:29:32 +01005915 s = zbc_program_prep(&ptr, &num);
5916 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005917
5918 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5919 copy.t = BC_RESULT_TEMP;
5920 bc_num_init(&copy.d.n, num->len);
5921 bc_num_copy(&copy.d.n, num);
5922 }
5923
5924 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005925 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5926 ? BC_INST_ASSIGN_PLUS
5927 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005928
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005929 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005930 s = zbc_program_assign(inst);
5931 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005932
5933 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005934 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005935 }
5936
Denys Vlasenko29301232018-12-11 15:29:32 +01005937 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005938}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005939#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005940
Denys Vlasenko29301232018-12-11 15:29:32 +01005941static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005942{
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005944 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005945 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005946 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005947 BcResult *arg;
5948
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005949 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005950 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005951 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005952 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005953
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005954 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005955 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005956 }
5957 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005958 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005959 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005960 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005961
5962 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005963 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005964
5965 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005966 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005967
5968 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005969 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005970
Denys Vlasenko29301232018-12-11 15:29:32 +01005971 s = zbc_program_copyToVar(a->name, a->idx);
5972 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005973 }
5974
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005975 a = bc_vec_item(&func->autos, i);
5976 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005977 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005978
Denys Vlasenkodf515392018-12-02 19:27:48 +01005979 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005980 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005981 BcNum n2;
5982 bc_num_init_DEF_SIZE(&n2);
5983 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005984 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005985 BcVec v2;
5986 bc_array_init(&v2, true);
5987 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005988 }
5989 }
5990
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005991 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005992
Denys Vlasenko29301232018-12-11 15:29:32 +01005993 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005994}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005995#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005996
Denys Vlasenko29301232018-12-11 15:29:32 +01005997static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005998{
Gavin Howard01055ba2018-11-03 11:00:21 -06005999 BcResult res;
6000 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006001 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006002 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006003 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006004
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006005 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 +01006006 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006007
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006008 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006009 res.t = BC_RESULT_TEMP;
6010
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006011 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006012 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006013 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006014 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
Denys Vlasenko29301232018-12-11 15:29:32 +01006016 s = zbc_program_num(operand, &num, false);
6017 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006018 bc_num_init(&res.d.n, num->len);
6019 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006020 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006021 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006022 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006023 }
6024
6025 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006026 a = (void*)f->autos.v;
6027 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006028 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006029 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006030 bc_vec_pop(v);
6031 }
6032
Denys Vlasenko24e41942018-12-21 23:01:26 +01006033 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006034 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006035 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006036
Denys Vlasenko29301232018-12-11 15:29:32 +01006037 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006038}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006039#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006040#endif // ENABLE_BC
6041
6042static unsigned long bc_program_scale(BcNum *n)
6043{
6044 return (unsigned long) n->rdx;
6045}
6046
6047static unsigned long bc_program_len(BcNum *n)
6048{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006049 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006050
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006051 if (n->rdx != len) return len;
6052 for (;;) {
6053 if (len == 0) break;
6054 len--;
6055 if (n->num[len] != 0) break;
6056 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006057 return len;
6058}
6059
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006060static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006061{
6062 BcStatus s;
6063 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006064 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006065 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006066 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006067
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006068 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006069 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006070 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006071
Denys Vlasenko29301232018-12-11 15:29:32 +01006072 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006073 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006074
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006075#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006076 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006077 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006078#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006079
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006080 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006081
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006082 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006083 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006084#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006085 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006086 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006087 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006088#endif
6089#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006090 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006091 char **str;
6092 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6093
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006094 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006095 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006097#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006098 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006099 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006100 }
6101
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006102 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006103
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006104 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006105}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006106#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006107
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006108#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006109static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006110{
6111 BcStatus s;
6112 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006113 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006114
Denys Vlasenko29301232018-12-11 15:29:32 +01006115 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006116 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006117
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006118 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006119 bc_num_init(&res2.d.n, n2->len);
6120
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006121 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006122 if (s) goto err;
6123
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006124 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006125 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006126 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006127
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006128 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006129 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006130 bc_num_free(&res2.d.n);
6131 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006132 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006133}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006134#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006135
Denys Vlasenkofa210792018-12-19 19:35:40 +01006136static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006137{
6138 BcStatus s;
6139 BcResult *r1, *r2, *r3, res;
6140 BcNum *n1, *n2, *n3;
6141
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006142 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006143 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006144 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006145 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006146
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006147 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006148 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006149 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006150 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006151 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006152
6153 // Make sure that the values have their pointers updated, if necessary.
6154 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006155 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006156 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006157 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006158 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006159 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006160 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006161 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006162 }
6163 }
6164
6165 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006166 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006167 if (s) goto err;
6168
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006169 bc_vec_pop(&G.prog.results);
6170 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006171
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006172 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006173 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006174 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006175 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006176}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006177#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006178
Denys Vlasenkofa210792018-12-19 19:35:40 +01006179static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006180{
Gavin Howard01055ba2018-11-03 11:00:21 -06006181 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006182 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006183
6184 res.t = BC_RESULT_TEMP;
6185
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006186 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006187 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006188 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006189}
6190
Denys Vlasenkofa210792018-12-19 19:35:40 +01006191static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006192{
6193 BcStatus s;
6194 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006195 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006196 char **strs;
6197 char *str;
6198 char c;
6199 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006200
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006201 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006202 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006203 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006204
Denys Vlasenko29301232018-12-11 15:29:32 +01006205 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006206 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006207
6208 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006209 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006210 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006211 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006212
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006213 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006214 bc_num_copy(&n, num);
6215 bc_num_truncate(&n, n.rdx);
6216
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006217 strmb.cap = ARRAY_SIZE(strmb_digs);
6218 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006219 bc_num_ulong2num(&strmb, 0x100);
6220 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006221
Gavin Howard01055ba2018-11-03 11:00:21 -06006222 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006223 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006224 if (s) goto num_err;
6225
6226 c = (char) val;
6227
6228 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006229 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006230 char *sp;
Gavin Howard01055ba2018-11-03 11:00:21 -06006231 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006232 sp = *bc_program_str(idx);
6233 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006234 }
6235
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006236 strs = (void*)G.prog.strs.v;
6237 for (idx = 0; idx < G.prog.strs.len; idx++) {
6238 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6239 goto dup;
6240 }
6241 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006242 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006243 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006244 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006245 bc_vec_push(&G.prog.strs, &str);
6246 dup:
Gavin Howard01055ba2018-11-03 11:00:21 -06006247 res.t = BC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006248 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006249 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006250
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006251 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006252 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006253 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006254 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006255}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006256#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006257
Denys Vlasenkofa210792018-12-19 19:35:40 +01006258static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006259{
6260 BcStatus s;
6261 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006262 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006263 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006264
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006265 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006266 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006267 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006268
Denys Vlasenko29301232018-12-11 15:29:32 +01006269 s = zbc_program_num(r, &n, false);
6270 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006271
Denys Vlasenko57734c92018-12-17 21:14:05 +01006272 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006273 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006274 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006275 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06006276 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006277 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006278 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006279 }
6280
Denys Vlasenko29301232018-12-11 15:29:32 +01006281 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006282}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006283#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006284
Denys Vlasenkofa210792018-12-19 19:35:40 +01006285static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006286{
6287 BcStatus s;
6288 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006289 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006290 unsigned long val;
6291
Denys Vlasenko29301232018-12-11 15:29:32 +01006292 s = zbc_program_prep(&opnd, &num);
6293 if (s) RETURN_STATUS(s);
6294 s = zbc_num_ulong(num, &val);
6295 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006296
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006297 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006298
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006299 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006300 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006301 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006302 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006303 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006304
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006305 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006306
Denys Vlasenko29301232018-12-11 15:29:32 +01006307 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006308}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006309#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006310
Denys Vlasenkofa210792018-12-19 19:35:40 +01006311static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006312{
6313 BcStatus s = BC_STATUS_SUCCESS;
6314 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006315 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006316 BcInstPtr ip;
6317 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006318
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006319 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006320 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006321
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006322 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006323
6324 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006325 BcNum *n = n; // for compiler
6326 bool exec;
6327 char *name;
6328 char *then_name = bc_program_name(code, bgn);
6329 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006330
6331 if (code[*bgn] == BC_PARSE_STREND)
6332 (*bgn) += 1;
6333 else
6334 else_name = bc_program_name(code, bgn);
6335
6336 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006337 name = then_name;
6338 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006339 exec = true;
6340 name = else_name;
6341 }
6342
6343 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006344 BcVec *v;
6345 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006346 n = bc_vec_top(v);
6347 }
6348
6349 free(then_name);
6350 free(else_name);
6351
6352 if (!exec) goto exit;
6353 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006354 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006355 goto exit;
6356 }
6357
6358 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006359 } else {
6360 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006361 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006362 } else if (r->t == BC_RESULT_VAR) {
6363 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006364 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006365 if (s || !BC_PROG_STR(n)) goto exit;
6366 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006367 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006368 goto exit;
6369 }
6370
6371 fidx = sidx + BC_PROG_REQ_FUNCS;
6372
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006373 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006374
6375 if (f->code.len == 0) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006376 FILE *sv_input_fp;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006377 BcParse prs;
6378 char *str;
6379
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006380 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006381 str = *bc_program_str(sidx);
6382 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006383 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006384
6385 sv_input_fp = G.input_fp;
6386 G.input_fp = NULL; // "do not read from input file when <EOL> reached"
6387 s = zdc_parse_exprs_until_eof(&prs);
6388 G.input_fp = sv_input_fp;
6389
Gavin Howard01055ba2018-11-03 11:00:21 -06006390 if (s) goto err;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006391 if (prs.l.t.t != XC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006392 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006393 err:
6394 bc_parse_free(&prs);
6395 bc_vec_pop_all(&f->code);
6396 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006397 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006398 bc_parse_push(&prs, DC_INST_POP_EXEC);
Gavin Howard01055ba2018-11-03 11:00:21 -06006399 bc_parse_free(&prs);
6400 }
6401
Denys Vlasenko24e41942018-12-21 23:01:26 +01006402 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 ip.func = fidx;
6404
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006405 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006406 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006407
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006408 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006409 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006410 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006411 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006412}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006413#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006414#endif // ENABLE_DC
6415
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006416static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006417{
Gavin Howard01055ba2018-11-03 11:00:21 -06006418 BcResult res;
6419 unsigned long val;
6420
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006421 res.t = inst - XC_INST_IBASE + BC_RESULT_IBASE;
6422 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006423 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006424 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006425 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006426 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006427 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006428
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006429 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006430 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006431 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006432}
6433
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006434static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006435{
Gavin Howard01055ba2018-11-03 11:00:21 -06006436 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006437 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006438 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006439 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006440
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006441 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006442 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6443 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006444 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006445 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006446
Denys Vlasenko24e41942018-12-21 23:01:26 +01006447 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 -06006448 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006449#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006450 case BC_INST_JUMP_ZERO: {
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006451 BcNum *num;
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006452 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006453 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006454 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006455 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006456 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006457 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006458 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006459 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006460 break;
6461 }
6462 // else: fall through
6463 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006464 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006465 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006466 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006467 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006468 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006469 break;
6470 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006471 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006472 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006473 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006474 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006475 case BC_INST_INC_PRE:
6476 case BC_INST_DEC_PRE:
6477 case BC_INST_INC_POST:
6478 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006479 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006480 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006481 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006482 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006483 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006484 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006485 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006486 case XC_INST_RET:
Gavin Howard01055ba2018-11-03 11:00:21 -06006487 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006488 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006489 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006490 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006491 case XC_INST_BOOL_OR:
6492 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006493#endif // ENABLE_BC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006494 case XC_INST_REL_EQ:
6495 case XC_INST_REL_LE:
6496 case XC_INST_REL_GE:
6497 case XC_INST_REL_NE:
6498 case XC_INST_REL_LT:
6499 case XC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006500 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006501 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006502 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006503 case XC_INST_READ:
6504 dbg_exec("XC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006505 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006506 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006507 case XC_INST_VAR:
6508 dbg_exec("XC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006509 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006510 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006511 case XC_INST_ARRAY_ELEM:
6512 case XC_INST_ARRAY:
6513 dbg_exec("XC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006514 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006515 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006516#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006517 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006518 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006519 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006520 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006521 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006522#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006523 case XC_INST_IBASE:
6524 case XC_INST_OBASE:
6525 case XC_INST_SCALE:
6526 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006527 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006528 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006529 case XC_INST_SCALE_FUNC:
6530 case XC_INST_LENGTH:
6531 case XC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006532 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006533 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006534 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006535 case XC_INST_NUM:
6536 dbg_exec("XC_INST_NUM:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006537 r.t = BC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006538 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006539 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006540 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006541 case XC_INST_POP:
6542 dbg_exec("XC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006543 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006544 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006545 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006546 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006547 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006548 case XC_INST_PRINT:
6549 case XC_INST_PRINT_POP:
6550 case XC_INST_PRINT_STR:
6551 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006552 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006553 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006554 case XC_INST_STR:
6555 dbg_exec("XC_INST_STR:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006556 r.t = BC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006557 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006558 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006559 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006560 case XC_INST_POWER:
6561 case XC_INST_MULTIPLY:
6562 case XC_INST_DIVIDE:
6563 case XC_INST_MODULUS:
6564 case XC_INST_PLUS:
6565 case XC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006566 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006567 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006568 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006569 case XC_INST_BOOL_NOT: {
6570 BcNum *num;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006571 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006572 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006573 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006574 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006575 if (bc_num_cmp(num, &G.prog.zero) == 0)
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006576 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006577 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006578 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006579 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006580 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006581 case XC_INST_NEG:
6582 dbg_exec("XC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006583 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006584 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006585#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006586 case BC_INST_ASSIGN_POWER:
6587 case BC_INST_ASSIGN_MULTIPLY:
6588 case BC_INST_ASSIGN_DIVIDE:
6589 case BC_INST_ASSIGN_MODULUS:
6590 case BC_INST_ASSIGN_PLUS:
6591 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006592#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006593 case XC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006594 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006595 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006596 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006597#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006598 case DC_INST_POP_EXEC:
6599 dbg_exec("DC_INST_POP_EXEC:");
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006600 bc_vec_pop(&G.prog.exestack);
6601 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006602 case DC_INST_MODEXP:
6603 dbg_exec("DC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006604 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006605 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006606 case DC_INST_DIVMOD:
6607 dbg_exec("DC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006608 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006609 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006610 case DC_INST_EXECUTE:
6611 case DC_INST_EXEC_COND:
6612 dbg_exec("DC_INST_EXEC[_COND]:");
6613 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006614 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006615 case DC_INST_PRINT_STACK: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006616 size_t idx;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006617 dbg_exec("DC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006618 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006619 s = zbc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006620 if (s) break;
6621 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006622 break;
6623 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006624 case DC_INST_CLEAR_STACK:
6625 dbg_exec("DC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006626 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006627 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006628 case DC_INST_STACK_LEN:
6629 dbg_exec("DC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006630 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006631 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006632 case DC_INST_DUPLICATE:
6633 dbg_exec("DC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006634 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006635 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006636 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006637 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006638 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006639 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006640 case DC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006641 BcResult *ptr2;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006642 dbg_exec("DC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006643 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006644 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006645 ptr = bc_vec_item_rev(&G.prog.results, 0);
6646 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006647 memcpy(&r, ptr, sizeof(BcResult));
6648 memcpy(ptr, ptr2, sizeof(BcResult));
6649 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006650 break;
6651 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006652 case DC_INST_ASCIIFY:
6653 dbg_exec("DC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006654 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006655 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006656 case DC_INST_PRINT_STREAM:
6657 dbg_exec("DC_INST_PRINT_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006658 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006659 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006660 case DC_INST_LOAD:
6661 case DC_INST_PUSH_VAR: {
6662 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006663 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006664 break;
6665 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006666 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006667 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006668 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006669 free(name);
6670 break;
6671 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006672 case DC_INST_QUIT:
6673 dbg_exec("DC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006674 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006675 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006676 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006677 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006678 case DC_INST_NQUIT:
6679 dbg_exec("DC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006680 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006681 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006682#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006683 read_updated_ip:
6684 // Instruction stack has changed, read new pointers
6685 ip = bc_vec_top(&G.prog.exestack);
6686 func = bc_program_func(ip->func);
6687 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006688 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006689 }
6690
Denys Vlasenkod38af482018-12-04 19:11:02 +01006691 if (s || G_interrupt) {
6692 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006693 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006694 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006695
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006696 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006697 }
6698
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006699 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006700}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006701#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006702
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006703static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006704{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006705 char *lenv;
6706 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006707
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006708 lenv = getenv(var);
6709 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006710 if (!lenv) return len;
6711
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006712 len = bb_strtou(lenv, NULL, 10) - 1;
6713 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006714 len = BC_NUM_PRINT_WIDTH;
6715
6716 return len;
6717}
6718
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006719static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006720{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006721 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006722
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006723 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006724 s = zbc_parse_text_init(&G.prs, text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006725 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006726
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006727 while (G.prs.l.t.t != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006728 BcInstPtr *ip;
6729 BcFunc *f;
6730
Denys Vlasenko39287e02018-12-22 02:23:08 +01006731 dbg_lex("%s:%d G.prs.l.t.t:%d, parsing...", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006732 if (IS_BC) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006733// FIXME: "eating" of stmt delimiters is coded inconsistently
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006734// (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't),
6735// which causes bugs such as "print 1 print 2" erroneously accepted,
6736// or "print 1 else 2" detecting parse error only after executing
6737// "print 1" part.
6738 IF_BC(s = zbc_parse_stmt_or_funcdef(&G.prs));
6739 } else {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006740 // Most of dc parsing assumes all whitespace,
6741 // including '\n', is eaten.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01006742 while (G.prs.l.t.t == XC_LEX_NLINE) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006743 s = zbc_lex_next(&G.prs.l);
6744 if (s) goto err;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006745 if (G.prs.l.t.t == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006746 goto done;
6747 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006748 IF_DC(s = zdc_parse_expr(&G.prs));
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006749 }
6750 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006751 err:
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006752 bc_parse_reset(&G.prs); // includes bc_program_reset()
6753 RETURN_STATUS(BC_STATUS_FAILURE);
6754 }
6755
Denys Vlasenko39287e02018-12-22 02:23:08 +01006756 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006757 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006758 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006759 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006760 break;
6761 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006762
6763 ip = (void*)G.prog.exestack.v;
6764#if SANITY_CHECKS
6765 if (G.prog.exestack.len != 1) // should have only main's IP
6766 bb_error_msg_and_die("BUG:call stack");
6767 if (ip->func != BC_PROG_MAIN)
6768 bb_error_msg_and_die("BUG:not MAIN");
6769#endif
6770 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006771 // bc discards strings, constants and code after each
6772 // top-level statement in the "main program".
6773 // This prevents "yes 1 | bc" from growing its memory
6774 // without bound. This can be done because data stack
6775 // is empty and thus can't hold any references to
6776 // strings or constants, there is no generated code
6777 // which can hold references (after we discard one
6778 // we just executed). Code of functions can have references,
6779 // but bc stores function strings/constants in per-function
6780 // storage.
6781 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006782#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006783 if (G.prog.results.len != 0) // should be empty
6784 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006785#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006786 IF_BC(bc_vec_pop_all(&f->strs);)
6787 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006788 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006789 if (G.prog.results.len == 0
6790 && G.prog.vars.len == 0
6791 ) {
6792 // If stack is empty and no registers exist (TODO: or they are all empty),
6793 // we can get rid of accumulated strings and constants.
6794 // In this example dc process should not grow
6795 // its memory consumption with time:
6796 // yes 1pc | dc
6797 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6798 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6799 }
6800 // The code is discarded always (below), thus this example
6801 // should also not grow its memory consumption with time,
6802 // even though its data stack is not empty:
6803 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006804 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006805 // We drop generated and executed code for both bc and dc:
6806 bc_vec_pop_all(&f->code);
6807 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006808 }
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006809 done:
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006810 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006811 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006812}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006813#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006814
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006815static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006816{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006817 // So far bc/dc have no way to include a file from another file,
6818 // therefore we know G.prog.file == NULL on entry
6819 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006820 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006821
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006822 G.prog.file = filename;
6823 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006824 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006825
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006826 do {
6827 s = zbc_vm_process("");
6828 // We do not stop looping on errors here if reading stdin.
6829 // Example: start interactive bc and enter "return".
6830 // It should say "'return' not in a function"
6831 // but should not exit.
6832 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006833 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006834 RETURN_STATUS(s);
6835}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006836#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006837
6838static BC_STATUS zbc_vm_file(const char *file)
6839{
6840 BcStatus s;
6841 FILE *fp;
6842
6843 fp = xfopen_for_read(file);
6844 s = zbc_vm_execute_FILE(fp, file);
6845 fclose(fp);
6846
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_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006850
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006851#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006852static void bc_vm_info(void)
6853{
6854 printf("%s "BB_VER"\n"
6855 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6856 , applet_name);
6857}
6858
6859static void bc_args(char **argv)
6860{
6861 unsigned opts;
6862 int i;
6863
6864 GETOPT_RESET();
6865#if ENABLE_FEATURE_BC_LONG_OPTIONS
6866 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6867 "warn\0" No_argument "w"
6868 "version\0" No_argument "v"
6869 "standard\0" No_argument "s"
6870 "quiet\0" No_argument "q"
6871 "mathlib\0" No_argument "l"
6872 "interactive\0" No_argument "i"
6873 );
6874#else
6875 opts = option_mask32 |= getopt32(argv, "wvsqli");
6876#endif
6877 if (getenv("POSIXLY_CORRECT"))
6878 option_mask32 |= BC_FLAG_S;
6879
6880 if (opts & BC_FLAG_V) {
6881 bc_vm_info();
6882 exit(0);
6883 }
6884
6885 for (i = optind; argv[i]; ++i)
6886 bc_vec_push(&G.files, argv + i);
6887}
6888
6889static void bc_vm_envArgs(void)
6890{
6891 BcVec v;
6892 char *buf;
6893 char *env_args = getenv("BC_ENV_ARGS");
6894
6895 if (!env_args) return;
6896
6897 G.env_args = xstrdup(env_args);
6898 buf = G.env_args;
6899
6900 bc_vec_init(&v, sizeof(char *), NULL);
6901
6902 while (*(buf = skip_whitespace(buf)) != '\0') {
6903 bc_vec_push(&v, &buf);
6904 buf = skip_non_whitespace(buf);
6905 if (!*buf)
6906 break;
6907 *buf++ = '\0';
6908 }
6909
6910 // NULL terminate, and pass argv[] so that first arg is argv[1]
6911 if (sizeof(int) == sizeof(char*)) {
6912 bc_vec_push(&v, &const_int_0);
6913 } else {
6914 static char *const nullptr = NULL;
6915 bc_vec_push(&v, &nullptr);
6916 }
6917 bc_args(((char **)v.v) - 1);
6918
6919 bc_vec_free(&v);
6920}
6921
6922static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006923 "scale=20"
6924"\n" "define e(x){"
6925"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006926////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6927//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6928//e(-.998896): GNU:.36828580434569428695
6929// above code:.36828580434569428696
6930// actual value:.3682858043456942869594...
6931// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006932"\n" "b=ibase"
6933"\n" "ibase=A"
6934"\n" "if(x<0){"
6935"\n" "n=1"
6936"\n" "x=-x"
6937"\n" "}"
6938"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006939"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006940"\n" "scale=scale(x)+1"
6941"\n" "while(x>1){"
6942"\n" "d+=1"
6943"\n" "x/=2"
6944"\n" "scale+=1"
6945"\n" "}"
6946"\n" "scale=r"
6947"\n" "r=x+1"
6948"\n" "p=x"
6949"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006950"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006951"\n" "p*=x"
6952"\n" "f*=i"
6953"\n" "v=p/f"
6954"\n" "r+=v"
6955"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006956"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006957"\n" "scale=s"
6958"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006959"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006960"\n" "return(r/1)"
6961"\n" "}"
6962"\n" "define l(x){"
6963"\n" "auto b,s,r,p,a,q,i,v"
6964"\n" "b=ibase"
6965"\n" "ibase=A"
6966"\n" "if(x<=0){"
6967"\n" "r=(1-10^scale)/1"
6968"\n" "ibase=b"
6969"\n" "return(r)"
6970"\n" "}"
6971"\n" "s=scale"
6972"\n" "scale+=6"
6973"\n" "p=2"
6974"\n" "while(x>=2){"
6975"\n" "p*=2"
6976"\n" "x=sqrt(x)"
6977"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006978"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006979"\n" "p*=2"
6980"\n" "x=sqrt(x)"
6981"\n" "}"
6982"\n" "r=a=(x-1)/(x+1)"
6983"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006984"\n" "v=1"
6985"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006986"\n" "a*=q"
6987"\n" "v=a/i"
6988"\n" "r+=v"
6989"\n" "}"
6990"\n" "r*=p"
6991"\n" "scale=s"
6992"\n" "ibase=b"
6993"\n" "return(r/1)"
6994"\n" "}"
6995"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006996"\n" "auto b,s,r,a,q,i"
6997"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006998"\n" "b=ibase"
6999"\n" "ibase=A"
7000"\n" "s=scale"
7001"\n" "scale=1.1*s+2"
7002"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007003"\n" "scale=0"
7004"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007005"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007006"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007007"\n" "scale=s+2"
7008"\n" "r=a=x"
7009"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007010"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007011"\n" "a*=q/(i*(i-1))"
7012"\n" "r+=a"
7013"\n" "}"
7014"\n" "scale=s"
7015"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007016"\n" "return(r/1)"
7017"\n" "}"
7018"\n" "define c(x){"
7019"\n" "auto b,s"
7020"\n" "b=ibase"
7021"\n" "ibase=A"
7022"\n" "s=scale"
7023"\n" "scale*=1.2"
7024"\n" "x=s(2*a(1)+x)"
7025"\n" "scale=s"
7026"\n" "ibase=b"
7027"\n" "return(x/1)"
7028"\n" "}"
7029"\n" "define a(x){"
7030"\n" "auto b,s,r,n,a,m,t,f,i,u"
7031"\n" "b=ibase"
7032"\n" "ibase=A"
7033"\n" "n=1"
7034"\n" "if(x<0){"
7035"\n" "n=-1"
7036"\n" "x=-x"
7037"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007038"\n" "if(scale<65){"
7039"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7040"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007041"\n" "}"
7042"\n" "s=scale"
7043"\n" "if(x>.2){"
7044"\n" "scale+=5"
7045"\n" "a=a(.2)"
7046"\n" "}"
7047"\n" "scale=s+3"
7048"\n" "while(x>.2){"
7049"\n" "m+=1"
7050"\n" "x=(x-.2)/(1+.2*x)"
7051"\n" "}"
7052"\n" "r=u=x"
7053"\n" "f=-x*x"
7054"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007055"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007056"\n" "u*=f"
7057"\n" "t=u/i"
7058"\n" "r+=t"
7059"\n" "}"
7060"\n" "scale=s"
7061"\n" "ibase=b"
7062"\n" "return((m*a+r)/n)"
7063"\n" "}"
7064"\n" "define j(n,x){"
7065"\n" "auto b,s,o,a,i,v,f"
7066"\n" "b=ibase"
7067"\n" "ibase=A"
7068"\n" "s=scale"
7069"\n" "scale=0"
7070"\n" "n/=1"
7071"\n" "if(n<0){"
7072"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007073"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007074"\n" "}"
7075"\n" "a=1"
7076"\n" "for(i=2;i<=n;++i)a*=i"
7077"\n" "scale=1.5*s"
7078"\n" "a=(x^n)/2^n/a"
7079"\n" "r=v=1"
7080"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007081"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007082"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007083"\n" "v=v*f/i/(n+i)"
7084"\n" "r+=v"
7085"\n" "}"
7086"\n" "scale=s"
7087"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007088"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007089"\n" "return(a*r/1)"
7090"\n" "}"
7091};
7092#endif // ENABLE_BC
7093
Denys Vlasenko19f11072018-12-12 22:48:19 +01007094static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007095{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007096 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007097 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007098 size_t i;
7099
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007100#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007101 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007102 // We know that internal library is not buggy,
7103 // thus error checking is normally disabled.
7104# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007105 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007106 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007107 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007108 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007109#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007110
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007111 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007112 fname = (void*)G.files.v;
7113 for (i = 0; i < G.files.len; i++) {
7114 s = zbc_vm_file(*fname++);
7115 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7116 // Debug config, non-interactive mode:
7117 // return all the way back to main.
7118 // Non-debug builds do not come here
7119 // in non-interactive mode, they exit.
7120 RETURN_STATUS(s);
7121 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007122 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007123
Denys Vlasenko91cde952018-12-10 20:56:08 +01007124 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007125 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007126
Denys Vlasenko19f11072018-12-12 22:48:19 +01007127 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007128}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007129#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007130
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007131#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007132static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007133{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007134 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007135 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007136 bc_vec_free(&G.prog.vars);
7137 bc_vec_free(&G.prog.var_map);
7138 bc_vec_free(&G.prog.arrs);
7139 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007140 IF_DC(bc_vec_free(&G.prog.strs);)
7141 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007142 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007143 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007144 IF_BC(bc_num_free(&G.prog.last);)
7145 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007146 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007147 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007148}
7149
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007150static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007151{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007152 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007153 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007154 bc_parse_free(&G.prs);
7155 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007156}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007157#endif
7158
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007159static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007160{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007161 BcInstPtr ip;
7162
Denys Vlasenko82269122018-12-14 16:30:56 +01007163 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007164 memset(&ip, 0, sizeof(BcInstPtr));
7165
Denys Vlasenko82269122018-12-14 16:30:56 +01007166 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007167 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007168 G.prog.ob_t = 10;
7169
Denys Vlasenko503faf92018-12-20 16:24:18 +01007170 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7171 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007172
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007173 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007174 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007175
Denys Vlasenko503faf92018-12-20 16:24:18 +01007176 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7177 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007178
7179 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007180 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007181
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007182 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007183 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007184 // a valid function name (and be short)
7185 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007186 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007187 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007188 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007189 bc_program_add_fn();
7190 bc_program_add_fn();
7191 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007192
7193 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007194 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007195
7196 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007197 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007198
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007199 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7200 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007201 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007202 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7203 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007204
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007205 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007206}
Gavin Howard01055ba2018-11-03 11:00:21 -06007207
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007208static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007209{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007210#if ENABLE_FEATURE_EDITING
7211 G.line_input_state = new_line_input_t(DO_HISTORY);
7212#endif
7213 G.prog.len = bc_vm_envLen(env_len);
7214
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007215 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007216 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007217 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007218 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007219
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007220//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7221//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007222 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007223#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007224 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007225 // With SA_RESTART, most system calls will restart
7226 // (IOW: they won't fail with EINTR).
7227 // In particular, this means ^C won't cause
7228 // stdout to get into "error state" if SIGINT hits
7229 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007230 //
7231 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007232 // will only be handled after [Enter] since read()
7233 // from stdin is not interrupted by ^C either,
7234 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007235 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007236 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7237
7238 // Without SA_RESTART, this exhibits a bug:
7239 // "while (1) print 1" and try ^C-ing it.
7240 // Intermittently, instead of returning to input line,
7241 // you'll get "output error: Interrupted system call"
7242 // and exit.
7243 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007244#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007245 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007246 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007247 return 0; // "not a tty"
7248}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007249
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007250static BcStatus bc_vm_run(void)
7251{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007252 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007253#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007254 if (G_exiting) // it was actually "halt" or "quit"
7255 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007256 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007257# if ENABLE_FEATURE_EDITING
7258 free_line_input_t(G.line_input_state);
7259# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007260 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007261#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007262 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007263 return st;
7264}
7265
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007266#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007267int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007268int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007269{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007270 int is_tty;
7271
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007272 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007273
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007274 is_tty = bc_vm_init("BC_LINE_LENGTH");
7275
7276 bc_args(argv);
7277
7278 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7279 bc_vm_info();
7280
7281 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007282}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007283#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007284
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007285#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007286int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007287int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007288{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007289 int noscript;
7290
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007291 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007292
7293 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7294 // 1 char wider than bc from the same package.
7295 // Both default width, and xC_LINE_LENGTH=N are wider:
7296 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007297 // |1234\ |
7298 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007299 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007300 // |123\ |
7301 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007302 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007303 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007304
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007305 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7306 noscript = BC_FLAG_I;
7307 for (;;) {
7308 int n = getopt(argc, argv, "e:f:x");
7309 if (n <= 0)
7310 break;
7311 switch (n) {
7312 case 'e':
7313 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007314 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007315 if (n) return n;
7316 break;
7317 case 'f':
7318 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007319 n = zbc_vm_file(optarg);
7320 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007321 break;
7322 case 'x':
7323 option_mask32 |= DC_FLAG_X;
7324 break;
7325 default:
7326 bb_show_usage();
7327 }
7328 }
7329 argv += optind;
7330
7331 while (*argv) {
7332 noscript = 0;
7333 bc_vec_push(&G.files, argv++);
7334 }
7335
7336 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7337
7338 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007339}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007340#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007341
7342#endif // not DC_SMALL