blob: 8b44d44254f2cd4e453cfbb875305ab822df1049 [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 * Copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06005 */
6//config:config BC
7//config: bool "bc (45 kb; 49 kb when combined with dc)"
8//config: default y
9//config: help
10//config: bc is a command-line, arbitrary-precision calculator with a
11//config: Turing-complete language. See the GNU bc manual
12//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010013//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060014//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010015//config: This bc has five differences to the GNU bc:
16//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060017//config: 2) Arrays are copied before being passed as arguments to
18//config: functions. This behavior is required by the bc spec.
19//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010020//config: the number of elements in the array. This prints "1":
21//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060022//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010023//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060024//config: allows POSIX-compliant scripts to work while somewhat
25//config: preserving expected behavior (versus C) and making parsing
26//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010027//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060028//config:
29//config: Options:
Gavin Howard01055ba2018-11-03 11:00:21 -060030//config: -i --interactive force interactive mode
Denys Vlasenko89e785a2018-12-13 16:35:52 +010031//config: -q --quiet don't print version and copyright
32//config: -s --standard error if any non-POSIX extensions are used
33//config: -w --warn warn if any non-POSIX extensions are used
Gavin Howard01055ba2018-11-03 11:00:21 -060034//config: -l --mathlib use predefined math routines:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010035//config: s(expr) sine in radians
36//config: c(expr) cosine in radians
37//config: a(expr) arctangent, returning radians
38//config: l(expr) natural log
39//config: e(expr) raises e to the power of expr
40//config: j(n, x) Bessel function of integer order n of x
Gavin Howard01055ba2018-11-03 11:00:21 -060041//config:
42//config:config DC
43//config: bool "dc (38 kb; 49 kb when combined with bc)"
44//config: default y
45//config: help
46//config: dc is a reverse-polish notation command-line calculator which
47//config: supports unlimited precision arithmetic. See the FreeBSD man page
48//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010049//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060050//config:
51//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060053//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010054//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060055//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010056//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060057//config: 4) Like the FreeBSD dc, this dc supports extended registers.
58//config: However, they are implemented differently. When it encounters
59//config: whitespace where a register should be, it skips the whitespace.
60//config: If the character following is not a lowercase letter, an error
61//config: is issued. Otherwise, the register name is parsed by the
62//config: following regex:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010063//config: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060064//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010065//config: whitespace. Examples:
66//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060067//config: Also note that, like the FreeBSD dc, extended registers are not
68//config: allowed unless the "-x" option is given.
69//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010070//config:config FEATURE_DC_SMALL
71//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
72//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010073//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010074//config:
75//config:config FEATURE_DC_LIBM
76//config: bool "Enable power and exp functions (requires libm)"
77//config: default y
78//config: depends on FEATURE_DC_SMALL
79//config: help
80//config: Enable power and exp functions.
81//config: NOTE: This will require libm to be present for linking.
82//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060083//config:config FEATURE_BC_SIGNALS
Denys Vlasenko89e785a2018-12-13 16:35:52 +010084//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060085//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010086//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -060087//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010088//config: Enable interactive mode: when started on a tty,
89//config: ^C interrupts execution and returns to command line,
90//config: errors also return to command line instead of exiting,
91//config: line editing with history is available.
92//config:
93//config: With this option off, input can still be taken from tty,
94//config: but all errors are fatal, ^C is fatal,
95//config: tty is treated exactly the same as any other
96//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060097//config:
98//config:config FEATURE_BC_LONG_OPTIONS
99//config: bool "Enable bc/dc long options"
100//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100101//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600102//config: help
103//config: Enable long options for bc and dc.
104
105//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
106//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
107
108//kbuild:lib-$(CONFIG_BC) += bc.o
109//kbuild:lib-$(CONFIG_DC) += bc.o
110
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100111//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600112//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100113//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600114//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100115//usage:#define bc_full_usage "\n"
116//usage: "\nArbitrary precision calculator"
117//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100118///////: "\n -i Interactive" - has no effect for now
119//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100120//usage: "\n -l Load standard math library"
121//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100122//usage: "\n -w Warn if extensions are used"
123///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100124//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100125//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600126//usage:
127//usage:#define bc_example_usage
128//usage: "3 + 4.129\n"
129//usage: "1903 - 2893\n"
130//usage: "-129 * 213.28935\n"
131//usage: "12 / -1932\n"
132//usage: "12 % 12\n"
133//usage: "34 ^ 189\n"
134//usage: "scale = 13\n"
135//usage: "ibase = 2\n"
136//usage: "obase = A\n"
137//usage:
138//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100139//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600140//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100141//usage:#define dc_full_usage "\n"
142//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100143//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100144//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100145//usage: "\nf - print entire stack"
146//usage: "\nk - pop the value and set the precision"
147//usage: "\ni - pop the value and set input radix"
148//usage: "\no - pop the value and set output radix"
149//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600150//usage:
151//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100152//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600153//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100154//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600155//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100156//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600157//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100158//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600159//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600161//usage: "64\n"
162
163#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100164#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600165
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100166#if ENABLE_FEATURE_DC_SMALL
167# include "dc.c"
168#else
169
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100170#define DEBUG_LEXER 0
171#define DEBUG_COMPILE 0
172#define DEBUG_EXEC 0
Denys Vlasenko19eee8e2018-12-21 20:29:34 +0100173// This can be left enabled for production as well:
174#define SANITY_CHECKS 1
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100175
176#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100177static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100178#define dbg_lex(...) \
179 do { \
180 fprintf(stderr, "%*s", lex_indent, ""); \
181 bb_error_msg(__VA_ARGS__); \
182 } while (0)
183#define dbg_lex_enter(...) \
184 do { \
185 dbg_lex(__VA_ARGS__); \
186 lex_indent++; \
187 } while (0)
188#define dbg_lex_done(...) \
189 do { \
190 lex_indent--; \
191 dbg_lex(__VA_ARGS__); \
192 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100193#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100194# define dbg_lex(...) ((void)0)
195# define dbg_lex_enter(...) ((void)0)
196# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100197#endif
198
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100199#if DEBUG_COMPILE
200# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
201#else
202# define dbg_compile(...) ((void)0)
203#endif
204
Denys Vlasenko99b37622018-12-15 20:06:59 +0100205#if DEBUG_EXEC
206# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
207#else
208# define dbg_exec(...) ((void)0)
209#endif
210
Gavin Howard01055ba2018-11-03 11:00:21 -0600211typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100212 BC_STATUS_SUCCESS = 0,
213 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100214 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600215} BcStatus;
216
Gavin Howard01055ba2018-11-03 11:00:21 -0600217#define BC_VEC_INVALID_IDX ((size_t) -1)
218#define BC_VEC_START_CAP (1 << 5)
219
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100220typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600221
222typedef struct BcVec {
223 char *v;
224 size_t len;
225 size_t cap;
226 size_t size;
227 BcVecFree dtor;
228} BcVec;
229
Gavin Howard01055ba2018-11-03 11:00:21 -0600230typedef signed char BcDig;
231
232typedef struct BcNum {
233 BcDig *restrict num;
234 size_t rdx;
235 size_t len;
236 size_t cap;
237 bool neg;
238} BcNum;
239
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100240#define BC_NUM_MAX_IBASE ((unsigned long) 16)
241// larger value might speed up BIGNUM calculations a bit:
242#define BC_NUM_DEF_SIZE (16)
243#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600244
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100245#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600246
Gavin Howard01055ba2018-11-03 11:00:21 -0600247typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100248#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600249 BC_INST_INC_PRE,
250 BC_INST_DEC_PRE,
251 BC_INST_INC_POST,
252 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100253#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100254 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600255
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100256 XC_INST_REL_EQ, // should
257 XC_INST_REL_LE, // match
258 XC_INST_REL_GE, // LEX
259 XC_INST_REL_NE, // constants
260 XC_INST_REL_LT, // for
261 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600262
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100263 XC_INST_POWER, // operations
264 XC_INST_MULTIPLY, // |
265 XC_INST_DIVIDE, // |
266 XC_INST_MODULUS, // |
267 XC_INST_PLUS, // |
268 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600269
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100270 XC_INST_BOOL_NOT, // |
271 XC_INST_BOOL_OR, // |
272 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100273#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100274 BC_INST_ASSIGN_POWER, // |
275 BC_INST_ASSIGN_MULTIPLY,// |
276 BC_INST_ASSIGN_DIVIDE, // |
277 BC_INST_ASSIGN_MODULUS, // |
278 BC_INST_ASSIGN_PLUS, // |
279 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100280#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100281 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600282
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100283 XC_INST_NUM,
284 XC_INST_VAR,
285 XC_INST_ARRAY_ELEM,
286 XC_INST_ARRAY,
287 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100288
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100289 XC_INST_IBASE, // order of these constans should match other enums
290 XC_INST_OBASE, // order of these constans should match other enums
291 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100292 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100293 XC_INST_LENGTH,
294 XC_INST_READ,
295 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600296
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100297 XC_INST_PRINT,
298 XC_INST_PRINT_POP,
299 XC_INST_STR,
300 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600301
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100302#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100303 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600304 BC_INST_JUMP,
305 BC_INST_JUMP_ZERO,
306
307 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600308 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100309#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100310 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600311
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100312 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100313#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100314 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600315
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100316 DC_INST_MODEXP,
317 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600318
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100319 DC_INST_EXECUTE,
320 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600321
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100322 DC_INST_ASCIIFY,
323 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600324
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100325 DC_INST_PRINT_STACK,
326 DC_INST_CLEAR_STACK,
327 DC_INST_STACK_LEN,
328 DC_INST_DUPLICATE,
329 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600330
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100331 DC_INST_LOAD,
332 DC_INST_PUSH_VAR,
333 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600334
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100335 DC_INST_QUIT,
336 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600337
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100338 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100339#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600340} BcInst;
341
342typedef struct BcId {
343 char *name;
344 size_t idx;
345} BcId;
346
347typedef struct BcFunc {
348 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100349 IF_BC(BcVec labels;)
350 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100351 IF_BC(BcVec strs;)
352 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100353 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600354} BcFunc;
355
356typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100357 XC_RESULT_TEMP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600358
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100359 XC_RESULT_VAR,
360 XC_RESULT_ARRAY_ELEM,
361 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600362
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100363 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600364
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100365 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
366 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
367 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
368 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100369 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100370 XC_RESULT_CONSTANT,
371 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600372} BcResultType;
373
374typedef union BcResultData {
375 BcNum n;
376 BcVec v;
377 BcId id;
378} BcResultData;
379
380typedef struct BcResult {
381 BcResultType t;
382 BcResultData d;
383} BcResult;
384
385typedef struct BcInstPtr {
386 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100387 size_t inst_idx;
388 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600389} BcInstPtr;
390
Gavin Howard01055ba2018-11-03 11:00:21 -0600391typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100392 XC_LEX_EOF,
393 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600394
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100395 XC_LEX_NLINE,
396 XC_LEX_WHITESPACE,
397 XC_LEX_STR,
398 XC_LEX_NAME,
399 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100400
Denys Vlasenko69560f42018-12-24 14:14:23 +0100401 XC_LEX_1st_op,
402 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100403
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100404 XC_LEX_OP_REL_EQ, // should
405 XC_LEX_OP_REL_LE, // match
406 XC_LEX_OP_REL_GE, // INST
407 XC_LEX_OP_REL_NE, // constants
408 XC_LEX_OP_REL_LT, // for
409 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100410
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100411 XC_LEX_OP_POWER, // operations
412 XC_LEX_OP_MULTIPLY, // |
413 XC_LEX_OP_DIVIDE, // |
414 XC_LEX_OP_MODULUS, // |
415 XC_LEX_OP_PLUS, // |
416 XC_LEX_OP_MINUS, // |
417 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100418#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100419 BC_LEX_OP_BOOL_NOT, // |
420 BC_LEX_OP_BOOL_OR, // |
421 BC_LEX_OP_BOOL_AND, // |
422
423 BC_LEX_OP_ASSIGN_POWER, // |
424 BC_LEX_OP_ASSIGN_MULTIPLY, // |
425 BC_LEX_OP_ASSIGN_DIVIDE, // |
426 BC_LEX_OP_ASSIGN_MODULUS, // |
427 BC_LEX_OP_ASSIGN_PLUS, // |
428 BC_LEX_OP_ASSIGN_MINUS, // |
429
430 BC_LEX_OP_ASSIGN, // V
431
Gavin Howard01055ba2018-11-03 11:00:21 -0600432 BC_LEX_OP_INC,
433 BC_LEX_OP_DEC,
434
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100435 BC_LEX_LPAREN, // () are 0x28 and 0x29
436 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600437
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100438 BC_LEX_LBRACKET, // [] are 0x5B and 5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600439 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100440 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600441
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100442 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600443 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100444 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600445
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100446 BC_LEX_KEY_1st_keyword,
447 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600448 BC_LEX_KEY_BREAK,
449 BC_LEX_KEY_CONTINUE,
450 BC_LEX_KEY_DEFINE,
451 BC_LEX_KEY_ELSE,
452 BC_LEX_KEY_FOR,
453 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100454 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
455 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
456 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600457 BC_LEX_KEY_IF,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100458 IF_BC(BC_LEX_KEY_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600459 BC_LEX_KEY_LENGTH,
460 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600461 BC_LEX_KEY_PRINT,
462 BC_LEX_KEY_QUIT,
463 BC_LEX_KEY_READ,
464 BC_LEX_KEY_RETURN,
465 BC_LEX_KEY_SCALE,
466 BC_LEX_KEY_SQRT,
467 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100468#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600469
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100470#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100471 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
472 DC_LEX_OP_ASSIGN,
473
474 DC_LEX_LPAREN,
475 DC_LEX_SCOLON,
476 DC_LEX_READ,
477 DC_LEX_IBASE,
478 DC_LEX_SCALE,
479 DC_LEX_OBASE,
480 DC_LEX_LENGTH,
481 DC_LEX_PRINT,
482 DC_LEX_QUIT,
483 DC_LEX_SQRT,
484 DC_LEX_LBRACE,
485
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100486 DC_LEX_EQ_NO_REG,
487 DC_LEX_OP_MODEXP,
488 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600489
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100490 DC_LEX_COLON,
491 DC_LEX_ELSE,
492 DC_LEX_EXECUTE,
493 DC_LEX_PRINT_STACK,
494 DC_LEX_CLEAR_STACK,
495 DC_LEX_STACK_LEVEL,
496 DC_LEX_DUPLICATE,
497 DC_LEX_SWAP,
498 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600499
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100500 DC_LEX_ASCIIFY,
501 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600502
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100503 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
504 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
505 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
506 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
507 DC_LEX_LOAD,
508 DC_LEX_LOAD_POP,
509 DC_LEX_STORE_PUSH,
510 DC_LEX_PRINT_POP,
511 DC_LEX_NQUIT,
512 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100513#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600514} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100515// must match order of BC_LEX_KEY_foo etc above
516#if ENABLE_BC
517struct BcLexKeyword {
518 char name8[8];
519};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100520#define BC_LEX_KW_ENTRY(a, b) \
521 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100522static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100523 BC_LEX_KW_ENTRY("auto" , 1), // 0
524 BC_LEX_KW_ENTRY("break" , 1), // 1
525 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
526 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100527 BC_LEX_KW_ENTRY("else" , 0), // 4
528 BC_LEX_KW_ENTRY("for" , 1), // 5
529 BC_LEX_KW_ENTRY("halt" , 0), // 6
530 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100531 BC_LEX_KW_ENTRY("obase" , 1), // 8
532 BC_LEX_KW_ENTRY("if" , 1), // 9
533 BC_LEX_KW_ENTRY("last" , 0), // 10
534 BC_LEX_KW_ENTRY("length" , 1), // 11
535 BC_LEX_KW_ENTRY("limits" , 0), // 12
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100536 BC_LEX_KW_ENTRY("print" , 0), // 13
537 BC_LEX_KW_ENTRY("quit" , 1), // 14
538 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100539 BC_LEX_KW_ENTRY("return" , 1), // 16
540 BC_LEX_KW_ENTRY("scale" , 1), // 17
541 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
542 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100543};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100544#undef BC_LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100545#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100546#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100547#define STRING_if (bc_lex_kws[9].name8)
548#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100549enum {
550 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100551 | (1 << 0) // 0
552 | (1 << 1) // 1
553 | (0 << 2) // 2
554 | (1 << 3) // 3
555 | (0 << 4) // 4
556 | (1 << 5) // 5
557 | (0 << 6) // 6
558 | (1 << 7) // 7
559 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100560 | (1 << 9) // 9
561 | (0 << 10) // 10
562 | (1 << 11) // 11
563 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100564 | (0 << 13) // 13
565 | (1 << 14) // 14
566 | (0 << 15) // 15
567 | (1 << 16) // 16
568 | (1 << 17) // 17
569 | (1 << 18) // 18
570 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100571};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100572#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100573
574// This is a bit array that corresponds to token types. An entry is
575// true if the token is valid in an expression, false otherwise.
576// Used to figure out when expr parsing should stop *without error message*
577// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100578// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100579// by later processing.
580enum {
581#define EXBITS(a,b,c,d,e,f,g,h) \
582 ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7)))
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100583 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100584 + (EXBITS(0,0,0,0,0,1,1,1) << (0*8)) // 0: EOF INVAL NL WS STR NAME NUM -
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100585 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
586 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100587 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
588 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100589 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
590 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
591 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100592#undef EXBITS
593};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100594static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100595{
596#if ULONG_MAX > 0xffffffff
597 // 64-bit version (will not work correctly for 32-bit longs!)
598 return BC_PARSE_EXPRS_BITS & (1UL << i);
599#else
600 // 32-bit version
601 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
602 if (i >= 32) {
603 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
604 i &= 31;
605 }
606 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100607#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100608}
609
610// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100611// [XC_LEX_1st_op...] token types.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100612static const uint8_t bc_parse_ops[] = {
613#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100614 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100615 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100616 OP(2, false), // pow
617 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
618 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100619 OP(1, false), // not
620 OP(7, true ), OP( 7, true ), // or and
621 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
622 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100623 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100624#undef OP
625};
626#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
627#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
628#endif // ENABLE_BC
629
630#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100631static const //BcLexType - should be this type
632uint8_t
633dc_char_to_LEX[] = {
634 /* %&'( */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100635 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100636 /* )*+, */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100637 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100638 /* -./ */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100639 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100640 /* 0123456789 */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100641 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
642 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
643 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100644 /* :;<=>?@ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100645 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
646 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100647 /* ABCDEFGH */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100648 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
649 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100650 /* IJKLMNOP */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100651 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
652 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100653 /* QRSTUVWXY */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100654 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
655 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100656 /* Z[\] */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100657 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100658 /* ^_` */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100659 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100660 /* abcdefgh */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100661 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
662 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100663 /* ijklmnop */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100664 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100665 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100666 /* qrstuvwx */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100667 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
668 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100669 /* yz */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100670 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100671 /* {|}~ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100672 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100673};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100674static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100675int8_t
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100676dc_LEX_to_INST[] = { // starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
677 XC_INST_POWER, XC_INST_MULTIPLY, // OP_POWER OP_MULTIPLY
678 XC_INST_DIVIDE, XC_INST_MODULUS, // OP_DIVIDE OP_MODULUS
679 XC_INST_PLUS, XC_INST_MINUS, // OP_PLUS OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100680 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
681 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
682 XC_INST_REL_GT, // DC_LEX_LPAREN
683 DC_INST_INVALID, // DC_LEX_SCOLON
684 DC_INST_INVALID, // DC_LEX_READ
685 XC_INST_IBASE, // DC_LEX_IBASE
686 XC_INST_SCALE, // DC_LEX_SCALE
687 XC_INST_OBASE, // DC_LEX_OBASE
688 XC_INST_LENGTH, // DC_LEX_LENGTH
689 XC_INST_PRINT, // DC_LEX_PRINT
690 DC_INST_QUIT, // DC_LEX_QUIT
691 XC_INST_SQRT, // DC_LEX_SQRT
692 XC_INST_REL_GE, // DC_LEX_LBRACE
693 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
694 DC_INST_MODEXP, DC_INST_DIVMOD, // OP_MODEXP OP_DIVMOD
695 DC_INST_INVALID, DC_INST_INVALID, // COLON ELSE
696 DC_INST_EXECUTE, // EXECUTE
697 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // PRINT_STACK CLEAR_STACK
698 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // STACK_LEVEL DUPLICATE
699 DC_INST_SWAP, XC_INST_POP, // SWAP POP
700 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // ASCIIFY PRINT_STREAM
701 DC_INST_INVALID, DC_INST_INVALID, // STORE_IBASE STORE_OBASE
702 DC_INST_INVALID, DC_INST_INVALID, // STORE_SCALE LOAD
703 DC_INST_INVALID, DC_INST_INVALID, // LOAD_POP STORE_PUSH
704 XC_INST_PRINT, DC_INST_NQUIT, // PRINT_POP NQUIT
705 XC_INST_SCALE_FUNC, // SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100706 // DC_INST_INVALID in this table either means that corresponding LEX
707 // is not possible for dc, or that it does not compile one-to-one
708 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100709};
710#endif // ENABLE_DC
711
Gavin Howard01055ba2018-11-03 11:00:21 -0600712typedef struct BcLex {
Gavin Howard01055ba2018-11-03 11:00:21 -0600713 const char *buf;
Denys Vlasenko26384542018-12-25 18:37:52 +0100714 const char *lex_next_at; // last lex_next() was called at this string
Gavin Howard01055ba2018-11-03 11:00:21 -0600715 size_t i;
716 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600717 size_t len;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +0100718 bool newline;
719 smallint lex; // was BcLexType
720 smallint lex_last; // was BcLexType
721 BcVec lex_buf;
Gavin Howard01055ba2018-11-03 11:00:21 -0600722} BcLex;
723
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100724#define BC_PARSE_STREND (0xff)
Gavin Howard01055ba2018-11-03 11:00:21 -0600725
Denys Vlasenko39287e02018-12-22 02:23:08 +0100726#if ENABLE_BC
727# define BC_PARSE_REL (1 << 0)
728# define BC_PARSE_PRINT (1 << 1)
729# define BC_PARSE_ARRAY (1 << 2)
730# define BC_PARSE_NOCALL (1 << 3)
731#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600732
Gavin Howard01055ba2018-11-03 11:00:21 -0600733typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600734 BcLex l;
735
Denys Vlasenko503faf92018-12-20 16:24:18 +0100736 IF_BC(BcVec exits;)
737 IF_BC(BcVec conds;)
738 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600739
Gavin Howard01055ba2018-11-03 11:00:21 -0600740 BcFunc *func;
741 size_t fidx;
742
Denys Vlasenko503faf92018-12-20 16:24:18 +0100743 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600744} BcParse;
745
Gavin Howard01055ba2018-11-03 11:00:21 -0600746typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600747 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100748 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600749
Denys Vlasenko503faf92018-12-20 16:24:18 +0100750 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600751 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600752 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600753
Gavin Howard01055ba2018-11-03 11:00:21 -0600754 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100755 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600756
757 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100758 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600759
760 BcVec vars;
761 BcVec var_map;
762
763 BcVec arrs;
764 BcVec arr_map;
765
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100766 IF_DC(BcVec strs;)
767 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600768
769 const char *file;
770
Gavin Howard01055ba2018-11-03 11:00:21 -0600771 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100772 IF_BC(BcNum one;)
773 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600774} BcProgram;
775
Gavin Howard01055ba2018-11-03 11:00:21 -0600776#define BC_PROG_MAIN (0)
777#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100778#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600779#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100780#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600781
782#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
783#define BC_PROG_NUM(r, n) \
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100784 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
Gavin Howard01055ba2018-11-03 11:00:21 -0600785
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100786#define BC_FLAG_W (1 << 0)
787#define BC_FLAG_V (1 << 1)
788#define BC_FLAG_S (1 << 2)
789#define BC_FLAG_Q (1 << 3)
790#define BC_FLAG_L (1 << 4)
791#define BC_FLAG_I (1 << 5)
792#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600793
794#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
795#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
796
Denys Vlasenko64074a12018-12-07 15:50:14 +0100797#define BC_MAX_OBASE ((unsigned) 999)
798#define BC_MAX_DIM ((unsigned) INT_MAX)
799#define BC_MAX_SCALE ((unsigned) UINT_MAX)
800#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
801#define BC_MAX_NUM BC_MAX_STRING
802// Unused apart from "limits" message. Just show a "biggish number" there.
803//#define BC_MAX_NAME BC_MAX_STRING
804//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
805//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
806#define BC_MAX_NAME_STR "999999999"
807#define BC_MAX_EXP_STR "999999999"
808#define BC_MAX_VARS_STR "999999999"
809
810#define BC_MAX_OBASE_STR "999"
811
812#if INT_MAX == 2147483647
813# define BC_MAX_DIM_STR "2147483647"
814#elif INT_MAX == 9223372036854775807
815# define BC_MAX_DIM_STR "9223372036854775807"
816#else
817# error Strange INT_MAX
818#endif
819
820#if UINT_MAX == 4294967295
821# define BC_MAX_SCALE_STR "4294967295"
822# define BC_MAX_STRING_STR "4294967294"
823#elif UINT_MAX == 18446744073709551615
824# define BC_MAX_SCALE_STR "18446744073709551615"
825# define BC_MAX_STRING_STR "18446744073709551614"
826#else
827# error Strange UINT_MAX
828#endif
829#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600830
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100831struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100832 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100833 IF_FEATURE_CLEAN_UP(smallint exiting;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600834
835 BcParse prs;
836 BcProgram prog;
837
Denys Vlasenko5318f812018-12-05 17:48:01 +0100838 // For error messages. Can be set to current parsed line,
839 // or [TODO] to current executing line (can be before last parsed one)
840 unsigned err_line;
841
Gavin Howard01055ba2018-11-03 11:00:21 -0600842 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100843 BcVec input_buffer;
844 FILE *input_fp;
Gavin Howard01055ba2018-11-03 11:00:21 -0600845
846 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100847
848#if ENABLE_FEATURE_EDITING
849 line_input_t *line_input_state;
850#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100851} FIX_ALIASING;
852#define G (*ptr_to_globals)
853#define INIT_G() do { \
854 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
855} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100856#define FREE_G() do { \
857 FREE_PTR_TO_GLOBALS(); \
858} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100859#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
860#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100861#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100862#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100863# define G_interrupt bb_got_signal
864# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100865#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100866# define G_interrupt 0
867# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100868#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100869#if ENABLE_FEATURE_CLEAN_UP
870# define G_exiting G.exiting
871#else
872# define G_exiting 0
873#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100874#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100875#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100876
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100877// In configurations where errors abort instead of propagating error
878// return code up the call chain, functions returning BC_STATUS
879// actually don't return anything, they always succeed and return "void".
880// A macro wrapper is provided, which makes this statement work:
881// s = zbc_func(...)
882// and makes it visible to the compiler that s is always zero,
883// allowing compiler to optimize dead code after the statement.
884//
885// To make code more readable, each such function has a "z"
886// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
887//
888#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100889# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100890# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100891# define IF_ERROR_RETURN_POSSIBLE(a) a
892# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100893# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100894# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100895#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100896# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100897# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100898# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
899# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100900# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100901# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100902#endif
903
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100904//
905// Utility routines
906//
Gavin Howard01055ba2018-11-03 11:00:21 -0600907
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100908static void fflush_and_check(void)
909{
910 fflush_all();
911 if (ferror(stdout) || ferror(stderr))
912 bb_perror_msg_and_die("output error");
913}
914
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100915#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100916#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100917do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100918 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100919 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100920 return BC_STATUS_FAILURE; \
921} while (0)
922#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100923#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100924#endif
925
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100926static void quit(void) NORETURN;
927static void quit(void)
928{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100929 if (ferror(stdin))
930 bb_perror_msg_and_die("input error");
931 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100932 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100933 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100934}
935
Denys Vlasenko5318f812018-12-05 17:48:01 +0100936static void bc_verror_msg(const char *fmt, va_list p)
937{
Denys Vlasenko82269122018-12-14 16:30:56 +0100938 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +0100939 if (G.prog.file) {
940 sv = applet_name;
941 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
942 }
943 bb_verror_msg(fmt, p, NULL);
944 if (G.prog.file) {
945 free((char*)applet_name);
946 applet_name = sv;
947 }
948}
949
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100950static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100951{
952 va_list p;
953
954 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100955 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100956 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100957
Denys Vlasenkoec603182018-12-17 10:34:02 +0100958 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
959 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
960 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100961}
962
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100963#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100964static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100965{
966 va_list p;
967
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100968 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100969 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100970 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100971
972 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100973 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100974 va_end(p);
975
976 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100977 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100978 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
979
Denys Vlasenkoec603182018-12-17 10:34:02 +0100980 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100981 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100982 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100983}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100984#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100985#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100986
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100987// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
988// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100989// function must not have caller-cleaned parameters on stack.
990// Unfortunately, vararg function API does exactly that on most arches.
991// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100992static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100993{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100994 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100995}
996static ERRORFUNC int bc_error_bad_character(char c)
997{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100998 if (!c)
999 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +01001000 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001001}
1002static ERRORFUNC int bc_error_bad_expression(void)
1003{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001004 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001005}
1006static ERRORFUNC int bc_error_bad_token(void)
1007{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001008 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001009}
1010static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1011{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001012 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001013}
1014static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1015{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001016 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001017}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001018#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001019static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001020{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001021 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001022}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001023#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1024static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001025{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001026 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001027}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001028#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1029static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001030{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001031 RETURN_STATUS(zbc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001032}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001033#define zbc_POSIX_does_not_allow_bool_ops_this_is_bad(...) (zbc_POSIX_does_not_allow_bool_ops_this_is_bad(__VA_ARGS__) COMMA_SUCCESS)
1034static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001035{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001036 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001037}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001038#define zbc_POSIX_does_not_allow_empty_X_expression_in_for(...) (zbc_POSIX_does_not_allow_empty_X_expression_in_for(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001039#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001040
Gavin Howard01055ba2018-11-03 11:00:21 -06001041static void bc_vec_grow(BcVec *v, size_t n)
1042{
1043 size_t cap = v->cap * 2;
1044 while (cap < v->len + n) cap *= 2;
1045 v->v = xrealloc(v->v, v->size * cap);
1046 v->cap = cap;
1047}
1048
1049static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1050{
1051 v->size = esize;
1052 v->cap = BC_VEC_START_CAP;
1053 v->len = 0;
1054 v->dtor = dtor;
1055 v->v = xmalloc(esize * BC_VEC_START_CAP);
1056}
1057
Denys Vlasenko7d628012018-12-04 21:46:47 +01001058static void bc_char_vec_init(BcVec *v)
1059{
1060 bc_vec_init(v, sizeof(char), NULL);
1061}
1062
Gavin Howard01055ba2018-11-03 11:00:21 -06001063static void bc_vec_expand(BcVec *v, size_t req)
1064{
1065 if (v->cap < req) {
1066 v->v = xrealloc(v->v, v->size * req);
1067 v->cap = req;
1068 }
1069}
1070
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001071static void bc_vec_pop(BcVec *v)
1072{
1073 v->len--;
1074 if (v->dtor)
1075 v->dtor(v->v + (v->size * v->len));
1076}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001077
Gavin Howard01055ba2018-11-03 11:00:21 -06001078static void bc_vec_npop(BcVec *v, size_t n)
1079{
1080 if (!v->dtor)
1081 v->len -= n;
1082 else {
1083 size_t len = v->len - n;
1084 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1085 }
1086}
1087
Denys Vlasenko7d628012018-12-04 21:46:47 +01001088static void bc_vec_pop_all(BcVec *v)
1089{
1090 bc_vec_npop(v, v->len);
1091}
1092
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001093static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001094{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001095 size_t len = v->len;
1096 if (len >= v->cap) bc_vec_grow(v, 1);
1097 memmove(v->v + (v->size * len), data, v->size);
1098 v->len++;
1099 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001100}
1101
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001102// G.prog.results often needs "pop old operand, push result" idiom.
1103// Can do this without a few extra ops
1104static size_t bc_result_pop_and_push(const void *data)
1105{
1106 BcVec *v = &G.prog.results;
1107 char *last;
1108 size_t len = v->len - 1;
1109
1110 last = v->v + (v->size * len);
1111 if (v->dtor)
1112 v->dtor(last);
1113 memmove(last, data, v->size);
1114 return len;
1115}
1116
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001117static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001118{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001119 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001120}
1121
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001122static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001123{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001124 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001125 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001126 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001127}
1128
Gavin Howard01055ba2018-11-03 11:00:21 -06001129static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1130{
1131 if (idx == v->len)
1132 bc_vec_push(v, data);
1133 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001134 char *ptr;
1135
1136 if (v->len == v->cap) bc_vec_grow(v, 1);
1137
1138 ptr = v->v + v->size * idx;
1139
1140 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1141 memmove(ptr, data, v->size);
1142 }
1143}
1144
1145static void bc_vec_string(BcVec *v, size_t len, const char *str)
1146{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001147 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001148 bc_vec_expand(v, len + 1);
1149 memcpy(v->v, str, len);
1150 v->len = len;
1151
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001152 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001153}
1154
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001155#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001156static void bc_vec_concat(BcVec *v, const char *str)
1157{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001158 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001159
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001160 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001161
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001162 slen = strlen(str);
1163 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001164
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001165 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001166 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001167
1168 v->len = len;
1169}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001170#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001171
1172static void *bc_vec_item(const BcVec *v, size_t idx)
1173{
1174 return v->v + v->size * idx;
1175}
1176
1177static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1178{
1179 return v->v + v->size * (v->len - idx - 1);
1180}
1181
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001182static void *bc_vec_top(const BcVec *v)
1183{
1184 return v->v + v->size * (v->len - 1);
1185}
1186
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001187static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001188{
1189 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001190 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001191 free(v->v);
1192}
1193
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001194static BcFunc* bc_program_func(size_t idx)
1195{
1196 return bc_vec_item(&G.prog.fns, idx);
1197}
1198// BC_PROG_MAIN is zeroth element, so:
1199#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1200
1201#if ENABLE_BC
1202static BcFunc* bc_program_current_func(void)
1203{
1204 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1205 BcFunc *func = bc_program_func(ip->func);
1206 return func;
1207}
1208#endif
1209
1210static char** bc_program_str(size_t idx)
1211{
1212#if ENABLE_BC
1213 if (IS_BC) {
1214 BcFunc *func = bc_program_current_func();
1215 return bc_vec_item(&func->strs, idx);
1216 }
1217#endif
1218 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1219}
1220
1221static char** bc_program_const(size_t idx)
1222{
1223#if ENABLE_BC
1224 if (IS_BC) {
1225 BcFunc *func = bc_program_current_func();
1226 return bc_vec_item(&func->consts, idx);
1227 }
1228#endif
1229 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1230}
1231
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001232static int bc_id_cmp(const void *e1, const void *e2)
1233{
1234 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1235}
1236
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001237static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001238{
1239 free(((BcId *) id)->name);
1240}
1241
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001242static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001243{
1244 size_t low = 0, high = v->len;
1245
1246 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001247 size_t mid = (low + high) / 2;
1248 BcId *id = bc_vec_item(v, mid);
1249 int result = bc_id_cmp(ptr, id);
1250
1251 if (result == 0)
1252 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001253 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001254 high = mid;
1255 else
1256 low = mid + 1;
1257 }
1258
1259 return low;
1260}
1261
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001262static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001263{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001264 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001265
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001266 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001267 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001268 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1269 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001270 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001271 bc_vec_pushAt(v, ptr, n);
1272 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001273}
1274
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001275#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001276static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001277{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001278 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001279 if (i >= v->len) return BC_VEC_INVALID_IDX;
1280 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1281}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001282#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001283
Gavin Howard01055ba2018-11-03 11:00:21 -06001284static void bc_num_setToZero(BcNum *n, size_t scale)
1285{
1286 n->len = 0;
1287 n->neg = false;
1288 n->rdx = scale;
1289}
1290
1291static void bc_num_zero(BcNum *n)
1292{
1293 bc_num_setToZero(n, 0);
1294}
1295
1296static void bc_num_one(BcNum *n)
1297{
1298 bc_num_setToZero(n, 0);
1299 n->len = 1;
1300 n->num[0] = 1;
1301}
1302
Denys Vlasenko3129f702018-12-09 12:04:44 +01001303// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001304static void bc_num_init(BcNum *n, size_t req)
1305{
1306 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001307 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001308 n->num = xmalloc(req);
1309 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001310 n->rdx = 0;
1311 n->len = 0;
1312 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001313}
1314
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001315static void bc_num_init_DEF_SIZE(BcNum *n)
1316{
1317 bc_num_init(n, BC_NUM_DEF_SIZE);
1318}
1319
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001320static void bc_num_expand(BcNum *n, size_t req)
1321{
1322 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1323 if (req > n->cap) {
1324 n->num = xrealloc(n->num, req);
1325 n->cap = req;
1326 }
1327}
1328
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001329static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001330{
1331 free(((BcNum *) num)->num);
1332}
1333
1334static void bc_num_copy(BcNum *d, BcNum *s)
1335{
1336 if (d != s) {
1337 bc_num_expand(d, s->cap);
1338 d->len = s->len;
1339 d->neg = s->neg;
1340 d->rdx = s->rdx;
1341 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1342 }
1343}
1344
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001345static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001346{
1347 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001348 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001349
Denys Vlasenko1557b762018-12-22 21:37:46 +01001350 result = 0;
1351 i = n->len;
1352 while (i > n->rdx) {
1353 unsigned long prev = result;
1354 result = result * 10 + n->num[--i];
1355 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1356 // 0x1ff00000 * 10 is 0x13f600000,
1357 // or 0x3f600000 truncated to 32 bits. Which is larger.
1358 // However, (N*10)/8 < N check is always correct.
1359 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001360 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001361 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001362 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001363
Denys Vlasenko29301232018-12-11 15:29:32 +01001364 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001365}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001366#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1367
1368static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1369{
1370 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1371
1372 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1373}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001374#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001375
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001376#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1377# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1378#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1379# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1380#endif
1381// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1382// would not hit realloc() code path - not good if num[] is not malloced
1383
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001384static void bc_num_ulong2num(BcNum *n, unsigned long val)
1385{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001386 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001387
1388 bc_num_zero(n);
1389
1390 if (val == 0) return;
1391
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001392 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001393
1394 ptr = n->num;
1395 for (;;) {
1396 n->len++;
1397 *ptr++ = val % 10;
1398 val /= 10;
1399 if (val == 0) break;
1400 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001401}
1402
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001403static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001404{
1405 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001406 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001407 a[i] -= b[i];
1408 for (j = i; a[j] < 0;) {
1409 a[j++] += 10;
1410 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001411 }
1412 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001413}
1414
1415static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1416{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001417 size_t i = len;
1418 for (;;) {
1419 int c;
1420 if (i == 0)
1421 return 0;
1422 i--;
1423 c = a[i] - b[i];
1424 if (c != 0) {
1425 i++;
1426 if (c < 0)
1427 return -i;
1428 return i;
1429 }
1430 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001431}
1432
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001433#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1434#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1435#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1436//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1437static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1438{
1439 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1440}
1441//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1442static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1443{
1444 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1445}
1446
Gavin Howard01055ba2018-11-03 11:00:21 -06001447static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1448{
1449 size_t i, min, a_int, b_int, diff;
1450 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001451 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001452 ssize_t cmp;
1453
1454 if (a == b) return 0;
1455 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1456 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001457
1458 if (a->neg != b->neg) // signs of a and b differ
1459 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1460 return (int)b->neg - (int)a->neg;
1461 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001462
1463 a_int = BC_NUM_INT(a);
1464 b_int = BC_NUM_INT(b);
1465 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001466
1467 if (a_int != 0) return (ssize_t) a_int;
1468
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001469 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001470 if (a_max) {
1471 min = b->rdx;
1472 diff = a->rdx - b->rdx;
1473 max_num = a->num + diff;
1474 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001475 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1476 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001477 min = a->rdx;
1478 diff = b->rdx - a->rdx;
1479 max_num = b->num + diff;
1480 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001481 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001482 }
1483
1484 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001485 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001486
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001487 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001488 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001489 }
1490
1491 return 0;
1492}
1493
1494static void bc_num_truncate(BcNum *n, size_t places)
1495{
1496 if (places == 0) return;
1497
1498 n->rdx -= places;
1499
1500 if (n->len != 0) {
1501 n->len -= places;
1502 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1503 }
1504}
1505
1506static void bc_num_extend(BcNum *n, size_t places)
1507{
1508 size_t len = n->len + places;
1509
1510 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001511 if (n->cap < len) bc_num_expand(n, len);
1512
1513 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1514 memset(n->num, 0, sizeof(BcDig) * places);
1515
1516 n->len += places;
1517 n->rdx += places;
1518 }
1519}
1520
1521static void bc_num_clean(BcNum *n)
1522{
1523 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1524 if (n->len == 0)
1525 n->neg = false;
1526 else if (n->len < n->rdx)
1527 n->len = n->rdx;
1528}
1529
1530static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1531{
1532 if (n->rdx < scale)
1533 bc_num_extend(n, scale - n->rdx);
1534 else
1535 bc_num_truncate(n, n->rdx - scale);
1536
1537 bc_num_clean(n);
1538 if (n->len != 0) n->neg = !neg1 != !neg2;
1539}
1540
1541static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1542 BcNum *restrict b)
1543{
1544 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001545 b->len = n->len - idx;
1546 a->len = idx;
1547 a->rdx = b->rdx = 0;
1548
1549 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1550 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001551 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001552 bc_num_zero(b);
1553 bc_num_copy(a, n);
1554 }
1555
1556 bc_num_clean(a);
1557 bc_num_clean(b);
1558}
1559
Denys Vlasenko29301232018-12-11 15:29:32 +01001560static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001561{
Denys Vlasenko29301232018-12-11 15:29:32 +01001562 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001563
1564 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1565 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1566 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001567 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001568 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001569
1570 if (n->rdx >= places)
1571 n->rdx -= places;
1572 else {
1573 bc_num_extend(n, places - n->rdx);
1574 n->rdx = 0;
1575 }
1576
1577 bc_num_clean(n);
1578
Denys Vlasenko29301232018-12-11 15:29:32 +01001579 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001580}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001581#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001582
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001583typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1584
1585static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1586 BcNumBinaryOp op, size_t req)
1587{
1588 BcStatus s;
1589 BcNum num2, *ptr_a, *ptr_b;
1590 bool init = false;
1591
1592 if (c == a) {
1593 ptr_a = &num2;
1594 memcpy(ptr_a, c, sizeof(BcNum));
1595 init = true;
1596 } else
1597 ptr_a = a;
1598
1599 if (c == b) {
1600 ptr_b = &num2;
1601 if (c != a) {
1602 memcpy(ptr_b, c, sizeof(BcNum));
1603 init = true;
1604 }
1605 } else
1606 ptr_b = b;
1607
1608 if (init)
1609 bc_num_init(c, req);
1610 else
1611 bc_num_expand(c, req);
1612
1613 s = BC_STATUS_SUCCESS;
1614 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1615
1616 if (init) bc_num_free(&num2);
1617
1618 RETURN_STATUS(s);
1619}
1620#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1621
1622static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1623static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1624static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1625static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1626static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1627static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1628
1629static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1630{
1631 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1632 (void) scale;
1633 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1634}
1635
1636static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1637{
1638 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1639 (void) scale;
1640 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1641}
1642
1643static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1644{
1645 size_t req = BC_NUM_MREQ(a, b, scale);
1646 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1647}
1648
1649static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1650{
1651 size_t req = BC_NUM_MREQ(a, b, scale);
1652 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1653}
1654
1655static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1656{
1657 size_t req = BC_NUM_MREQ(a, b, scale);
1658 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1659}
1660
1661static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1662{
1663 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1664}
1665
1666static const BcNumBinaryOp zbc_program_ops[] = {
1667 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1668};
1669#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1670#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1671#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1672#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1673#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1674#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1675
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001676static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001677{
1678 BcNum one;
1679 BcDig num[2];
1680
1681 one.cap = 2;
1682 one.num = num;
1683 bc_num_one(&one);
1684
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001685 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001686}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001687#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001688
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001689static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001690{
1691 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1692 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001693 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001694
1695 // Because this function doesn't need to use scale (per the bc spec),
1696 // I am hijacking it to say whether it's doing an add or a subtract.
1697
1698 if (a->len == 0) {
1699 bc_num_copy(c, b);
1700 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001701 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001702 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001703 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001704 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001705 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001706 }
1707
1708 c->neg = a->neg;
1709 c->rdx = BC_MAX(a->rdx, b->rdx);
1710 min_rdx = BC_MIN(a->rdx, b->rdx);
1711 c->len = 0;
1712
1713 if (a->rdx > b->rdx) {
1714 diff = a->rdx - b->rdx;
1715 ptr = a->num;
1716 ptr_a = a->num + diff;
1717 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001718 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001719 diff = b->rdx - a->rdx;
1720 ptr = b->num;
1721 ptr_a = a->num;
1722 ptr_b = b->num + diff;
1723 }
1724
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001725 ptr_c = c->num;
1726 for (i = 0; i < diff; ++i, ++c->len)
1727 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001728
1729 ptr_c += diff;
1730 a_int = BC_NUM_INT(a);
1731 b_int = BC_NUM_INT(b);
1732
1733 if (a_int > b_int) {
1734 min_int = b_int;
1735 max = a_int;
1736 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001737 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001738 min_int = a_int;
1739 max = b_int;
1740 ptr = ptr_b;
1741 }
1742
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001743 carry = 0;
1744 for (i = 0; i < min_rdx + min_int; ++i) {
1745 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[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 for (; i < max + min_rdx; ++i) {
1750 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001751 carry = in / 10;
1752 ptr_c[i] = (BcDig)(in % 10);
1753 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001754 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001755
1756 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1757
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001758 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001759}
1760
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001761static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001762{
Gavin Howard01055ba2018-11-03 11:00:21 -06001763 ssize_t cmp;
1764 BcNum *minuend, *subtrahend;
1765 size_t start;
1766 bool aneg, bneg, neg;
1767
1768 // Because this function doesn't need to use scale (per the bc spec),
1769 // I am hijacking it to say whether it's doing an add or a subtract.
1770
1771 if (a->len == 0) {
1772 bc_num_copy(c, b);
1773 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001774 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001775 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001776 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001777 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001778 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001779 }
1780
1781 aneg = a->neg;
1782 bneg = b->neg;
1783 a->neg = b->neg = false;
1784
1785 cmp = bc_num_cmp(a, b);
1786
1787 a->neg = aneg;
1788 b->neg = bneg;
1789
1790 if (cmp == 0) {
1791 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001792 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001793 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001794 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001795 neg = a->neg;
1796 minuend = a;
1797 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001798 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001799 neg = b->neg;
1800 if (sub) neg = !neg;
1801 minuend = b;
1802 subtrahend = a;
1803 }
1804
1805 bc_num_copy(c, minuend);
1806 c->neg = neg;
1807
1808 if (c->rdx < subtrahend->rdx) {
1809 bc_num_extend(c, subtrahend->rdx - c->rdx);
1810 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001811 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001812 start = c->rdx - subtrahend->rdx;
1813
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001814 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001815
1816 bc_num_clean(c);
1817
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001818 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001819}
1820
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001821static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001822 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001823#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001824{
1825 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001826 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001827 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001828 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001829
Gavin Howard01055ba2018-11-03 11:00:21 -06001830 if (a->len == 0 || b->len == 0) {
1831 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001832 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001833 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001834 aone = BC_NUM_ONE(a);
1835 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001836 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001837 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001838 }
1839
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001840 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1841 || a->len < BC_NUM_KARATSUBA_LEN
1842 || b->len < BC_NUM_KARATSUBA_LEN
1843 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001844 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001845
Gavin Howard01055ba2018-11-03 11:00:21 -06001846 bc_num_expand(c, a->len + b->len + 1);
1847
1848 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001849 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001850
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001851 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001852 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001853 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001854 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001855 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001856 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001857 carry = in / 10;
1858 c->num[i + j] = (BcDig)(in % 10);
1859 }
1860
1861 c->num[i + j] += (BcDig) carry;
1862 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001863
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001864#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001865 // a=2^1000000
1866 // a*a <- without check below, this will not be interruptible
1867 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001868#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001869 }
1870
1871 c->len = len;
1872
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001873 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001874 }
1875
1876 bc_num_init(&l1, max);
1877 bc_num_init(&h1, max);
1878 bc_num_init(&l2, max);
1879 bc_num_init(&h2, max);
1880 bc_num_init(&m1, max);
1881 bc_num_init(&m2, max);
1882 bc_num_init(&z0, max);
1883 bc_num_init(&z1, max);
1884 bc_num_init(&z2, max);
1885 bc_num_init(&temp, max + max);
1886
1887 bc_num_split(a, max2, &l1, &h1);
1888 bc_num_split(b, max2, &l2, &h2);
1889
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001890 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001892 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001893 if (s) goto err;
1894
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001895 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001896 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001897 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001898 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001899 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001900 if (s) goto err;
1901
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001902 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001903 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001904 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001905 if (s) goto err;
1906
Denys Vlasenko29301232018-12-11 15:29:32 +01001907 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001908 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001909 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001910 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001911 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001912 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001913 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001914 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001915 bc_num_free(&temp);
1916 bc_num_free(&z2);
1917 bc_num_free(&z1);
1918 bc_num_free(&z0);
1919 bc_num_free(&m2);
1920 bc_num_free(&m1);
1921 bc_num_free(&h2);
1922 bc_num_free(&l2);
1923 bc_num_free(&h1);
1924 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001925 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001926}
1927
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001928static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001929{
1930 BcStatus s;
1931 BcNum cpa, cpb;
1932 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1933
1934 scale = BC_MAX(scale, a->rdx);
1935 scale = BC_MAX(scale, b->rdx);
1936 scale = BC_MIN(a->rdx + b->rdx, scale);
1937 maxrdx = BC_MAX(maxrdx, scale);
1938
1939 bc_num_init(&cpa, a->len);
1940 bc_num_init(&cpb, b->len);
1941
1942 bc_num_copy(&cpa, a);
1943 bc_num_copy(&cpb, b);
1944 cpa.neg = cpb.neg = false;
1945
Denys Vlasenko29301232018-12-11 15:29:32 +01001946 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001947 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001948 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001949 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001950 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001951 if (s) goto err;
1952
1953 maxrdx += scale;
1954 bc_num_expand(c, c->len + maxrdx);
1955
1956 if (c->len < maxrdx) {
1957 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1958 c->len += maxrdx;
1959 }
1960
1961 c->rdx = maxrdx;
1962 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001963 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001964 bc_num_free(&cpb);
1965 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001966 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001967}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001968#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001969
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001970static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001971{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001972 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001973 size_t len, end, i;
1974 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001975
1976 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001977 RETURN_STATUS(bc_error("divide by zero"));
1978 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001979 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001980 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001981 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001982 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001983 bc_num_copy(c, a);
1984 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001985 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001986 }
1987
1988 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1989 bc_num_copy(&cp, a);
1990 len = b->len;
1991
1992 if (len > cp.len) {
1993 bc_num_expand(&cp, len + 2);
1994 bc_num_extend(&cp, len - cp.len);
1995 }
1996
1997 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1998 cp.rdx -= b->rdx;
1999 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2000
2001 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01002002 for (;;) {
2003 if (len == 0) break;
2004 len--;
2005 if (b->num[len] != 0)
2006 break;
2007 }
2008 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002009 }
2010
2011 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2012
2013 // We want an extra zero in front to make things simpler.
2014 cp.num[cp.len++] = 0;
2015 end = cp.len - len;
2016
2017 bc_num_expand(c, cp.len);
2018
2019 bc_num_zero(c);
2020 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2021 c->rdx = cp.rdx;
2022 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002023
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002024 s = BC_STATUS_SUCCESS;
2025 for (i = end - 1; i < end; --i) {
2026 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002027 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002028 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2029 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002030 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002031#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002032 // a=2^100000
2033 // scale=40000
2034 // 1/a <- without check below, this will not be interruptible
2035 if (G_interrupt) {
2036 s = BC_STATUS_FAILURE;
2037 break;
2038 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002039#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002040 }
2041
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002042 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002043 bc_num_free(&cp);
2044
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002045 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002046}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002047#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002048
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002049static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002050 BcNum *restrict d, size_t scale, size_t ts)
2051{
2052 BcStatus s;
2053 BcNum temp;
2054 bool neg;
2055
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002056 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002057 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002058
2059 if (a->len == 0) {
2060 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002061 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002062 }
2063
2064 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002065 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002066 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002067
2068 if (scale != 0) scale = ts;
2069
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002070 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002071 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002072 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002073 if (s) goto err;
2074
2075 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2076
2077 neg = d->neg;
2078 bc_num_retireMul(d, ts, a->neg, b->neg);
2079 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002080 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002081 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002082 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002083}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002084#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002085
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002086static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002087{
2088 BcStatus s;
2089 BcNum c1;
2090 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2091
2092 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002093 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002094 bc_num_free(&c1);
2095
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002096 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002097}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002098#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002099
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002100static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002101{
2102 BcStatus s = BC_STATUS_SUCCESS;
2103 BcNum copy;
2104 unsigned long pow;
2105 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002106 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002107
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002108 // GNU bc does not allow 2^2.0 - we do
2109 for (i = 0; i < b->rdx; i++)
2110 if (b->num[i] != 0)
2111 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002112
2113 if (b->len == 0) {
2114 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002115 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002116 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002117 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002118 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002119 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002120 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002121 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 if (!b->neg)
2123 bc_num_copy(c, a);
2124 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002125 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002126 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002127 }
2128
2129 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002130 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002131 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002132 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002133
2134 bc_num_init(&copy, a->len);
2135 bc_num_copy(&copy, a);
2136
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002137 if (!neg) {
2138 if (a->rdx > scale)
2139 scale = a->rdx;
2140 if (a->rdx * pow < scale)
2141 scale = a->rdx * pow;
2142 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002143
Gavin Howard01055ba2018-11-03 11:00:21 -06002144
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002145 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002146 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002147 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002148 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002149 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002150 //if (G_interrupt) {
2151 // s = BC_STATUS_FAILURE;
2152 // goto err;
2153 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002154 }
2155
Gavin Howard01055ba2018-11-03 11:00:21 -06002156 bc_num_copy(c, &copy);
2157
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002158 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002159 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002160 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002161 if (s) goto err;
2162
2163 if (pow & 1) {
2164 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002165 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002166 if (s) goto err;
2167 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002168 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002169 //if (G_interrupt) {
2170 // s = BC_STATUS_FAILURE;
2171 // goto err;
2172 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002173 }
2174
2175 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002176 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002177 if (s) goto err;
2178 }
2179
Gavin Howard01055ba2018-11-03 11:00:21 -06002180 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2181
2182 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002183 for (i = 0; i < c->len; ++i)
2184 if (c->num[i] != 0)
2185 goto skip;
2186 bc_num_setToZero(c, scale);
2187 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002188
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002189 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002190 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002191 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002192}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002193#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002194
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002195static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002196{
2197 BcStatus s;
2198 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2199 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2200 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2201
2202 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2203 bc_num_expand(b, req);
2204
2205 if (a->len == 0) {
2206 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002207 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002208 }
2209 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002210 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002211 }
2212 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002213 bc_num_one(b);
2214 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002215 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002216 }
2217
2218 scale = BC_MAX(scale, a->rdx) + 1;
2219 len = a->len + scale;
2220
2221 bc_num_init(&num1, len);
2222 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002223 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002224
2225 bc_num_one(&half);
2226 half.num[0] = 5;
2227 half.rdx = 1;
2228
2229 bc_num_init(&f, len);
2230 bc_num_init(&fprime, len);
2231
2232 x0 = &num1;
2233 x1 = &num2;
2234
2235 bc_num_one(x0);
2236 pow = BC_NUM_INT(a);
2237
2238 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002239 if (pow & 1)
2240 x0->num[0] = 2;
2241 else
2242 x0->num[0] = 6;
2243
2244 pow -= 2 - (pow & 1);
2245
2246 bc_num_extend(x0, pow);
2247
2248 // Make sure to move the radix back.
2249 x0->rdx -= pow;
2250 }
2251
2252 x0->rdx = digs = digs1 = 0;
2253 resrdx = scale + 2;
2254 len = BC_NUM_INT(x0) + resrdx - 1;
2255
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002256 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002257 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002258 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002259 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002260 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002261 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002262 if (s) goto err;
2263
2264 cmp = bc_num_cmp(x1, x0);
2265 digs = x1->len - (unsigned long long) llabs(cmp);
2266
2267 if (cmp == cmp2 && digs == digs1)
2268 times += 1;
2269 else
2270 times = 0;
2271
2272 resrdx += times > 4;
2273
2274 cmp2 = cmp1;
2275 cmp1 = cmp;
2276 digs1 = digs;
2277
2278 temp = x0;
2279 x0 = x1;
2280 x1 = temp;
2281 }
2282
Gavin Howard01055ba2018-11-03 11:00:21 -06002283 bc_num_copy(b, x0);
2284 scale -= 1;
2285 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002286 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002287 bc_num_free(&fprime);
2288 bc_num_free(&f);
2289 bc_num_free(&half);
2290 bc_num_free(&num2);
2291 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002292 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002293}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002294#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002295
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002296static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002297 size_t scale)
2298{
2299 BcStatus s;
2300 BcNum num2, *ptr_a;
2301 bool init = false;
2302 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2303
2304 if (c == a) {
2305 memcpy(&num2, c, sizeof(BcNum));
2306 ptr_a = &num2;
2307 bc_num_init(c, len);
2308 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002309 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002310 ptr_a = a;
2311 bc_num_expand(c, len);
2312 }
2313
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002314 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002315
2316 if (init) bc_num_free(&num2);
2317
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002318 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002319}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002320#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002321
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002322#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002323static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002324{
2325 BcStatus s;
2326 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002327 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002328
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002329 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002330 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002331 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002332 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002333 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002334 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002335
2336 bc_num_expand(d, c->len);
2337 bc_num_init(&base, c->len);
2338 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002339 bc_num_init(&temp, b->len);
2340
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002341 two.cap = ARRAY_SIZE(two_digs);
2342 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002343 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002344 two_digs[0] = 2;
2345
Gavin Howard01055ba2018-11-03 11:00:21 -06002346 bc_num_one(d);
2347
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002348 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002349 if (s) goto err;
2350 bc_num_copy(&exp, b);
2351
2352 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002353 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002354 if (s) goto err;
2355
2356 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002357 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002358 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002359 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002360 if (s) goto err;
2361 }
2362
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002363 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002364 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002365 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002366 if (s) goto err;
2367 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002368 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002369 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002370 bc_num_free(&exp);
2371 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002372 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002373}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002374#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002375#endif // ENABLE_DC
2376
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002377static FAST_FUNC void bc_string_free(void *string)
2378{
2379 free(*(char**)string);
2380}
2381
Gavin Howard01055ba2018-11-03 11:00:21 -06002382static void bc_func_init(BcFunc *f)
2383{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002384 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002385 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2386 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002387 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2388 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002389 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002390}
2391
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002392static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002393{
2394 BcFunc *f = (BcFunc *) func;
2395 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002396 IF_BC(bc_vec_free(&f->labels);)
2397 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002398 IF_BC(bc_vec_free(&f->strs);)
2399 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002400}
2401
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002402static void bc_array_expand(BcVec *a, size_t len);
2403
Gavin Howard01055ba2018-11-03 11:00:21 -06002404static void bc_array_init(BcVec *a, bool nums)
2405{
2406 if (nums)
2407 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2408 else
2409 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2410 bc_array_expand(a, 1);
2411}
2412
Gavin Howard01055ba2018-11-03 11:00:21 -06002413static void bc_array_expand(BcVec *a, size_t len)
2414{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002415 if (a->dtor == bc_num_free
2416 // && a->size == sizeof(BcNum) - always true
2417 ) {
2418 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002419 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002420 bc_num_init_DEF_SIZE(&n);
2421 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002422 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002423 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002424 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002425 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002426 bc_array_init(&v, true);
2427 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002428 }
2429 }
2430}
2431
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002432static void bc_array_copy(BcVec *d, const BcVec *s)
2433{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002434 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002435 size_t i;
2436
2437 bc_vec_pop_all(d);
2438 bc_vec_expand(d, s->cap);
2439 d->len = s->len;
2440
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002441 dnum = (void*)d->v;
2442 snum = (void*)s->v;
2443 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002444 bc_num_init(dnum, snum->len);
2445 bc_num_copy(dnum, snum);
2446 }
2447}
2448
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002449#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002450static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002451{
2452 d->t = src->t;
2453
2454 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002455 case XC_RESULT_TEMP:
2456 case XC_RESULT_IBASE:
2457 case XC_RESULT_SCALE:
2458 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002459 bc_num_init(&d->d.n, src->d.n.len);
2460 bc_num_copy(&d->d.n, &src->d.n);
2461 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002462 case XC_RESULT_VAR:
2463 case XC_RESULT_ARRAY:
2464 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002465 d->d.id.name = xstrdup(src->d.id.name);
2466 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002467 case XC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002468 IF_BC(case BC_RESULT_LAST:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002469 IF_BC(case BC_RESULT_ONE:)
2470 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002471 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2472 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002473 }
2474}
2475#endif // ENABLE_DC
2476
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002477static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002478{
2479 BcResult *r = (BcResult *) result;
2480
2481 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002482 case XC_RESULT_TEMP:
2483 case XC_RESULT_IBASE:
2484 case XC_RESULT_SCALE:
2485 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002486 bc_num_free(&r->d.n);
2487 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002488 case XC_RESULT_VAR:
2489 case XC_RESULT_ARRAY:
2490 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002491 free(r->d.id.name);
2492 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002493 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002494 // Do nothing.
2495 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002496 }
2497}
2498
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002499static int bad_input_byte(char c)
2500{
2501 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2502 || c > 0x7e
2503 ) {
2504 bc_error_fmt("illegal character 0x%02x", c);
2505 return 1;
2506 }
2507 return 0;
2508}
2509
2510// Note: it _appends_ data from fp to vec.
2511static void bc_read_line(BcVec *vec, FILE *fp)
2512{
2513 again:
2514 fflush_and_check();
2515
2516#if ENABLE_FEATURE_BC_SIGNALS
2517 if (G_interrupt) { // ^C was pressed
2518 intr:
2519 if (fp != stdin) {
2520 // ^C while running a script (bc SCRIPT): die.
2521 // We do not return to interactive prompt:
2522 // user might be running us from a shell,
2523 // and SCRIPT might be intended to terminate
2524 // (e.g. contain a "halt" stmt).
2525 // ^C dropping user into a bc prompt instead of
2526 // the shell would be unexpected.
2527 xfunc_die();
2528 }
2529 // ^C while interactive input
2530 G_interrupt = 0;
2531 // GNU bc says "interrupted execution."
2532 // GNU dc says "Interrupt!"
2533 fputs("\ninterrupted execution\n", stderr);
2534 }
2535
2536# if ENABLE_FEATURE_EDITING
2537 if (G_ttyin && fp == stdin) {
2538 int n, i;
2539# define line_buf bb_common_bufsiz1
2540 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2541 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2542 if (n == 0) // ^C
2543 goto intr;
2544 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2545 return;
2546 }
2547 i = 0;
2548 for (;;) {
2549 char c = line_buf[i++];
2550 if (!c) break;
2551 if (bad_input_byte(c)) goto again;
2552 }
2553 bc_vec_concat(vec, line_buf);
2554# undef line_buf
2555 } else
2556# endif
2557#endif
2558 {
2559 int c;
2560 bool bad_chars = 0;
2561 size_t len = vec->len;
2562
2563 do {
2564#if ENABLE_FEATURE_BC_SIGNALS
2565 if (G_interrupt) {
2566 // ^C was pressed: ignore entire line, get another one
2567 vec->len = len;
2568 goto intr;
2569 }
2570#endif
2571 do c = fgetc(fp); while (c == '\0');
2572 if (c == EOF) {
2573 if (ferror(fp))
2574 bb_perror_msg_and_die("input error");
2575 // Note: EOF does not append '\n'
2576 break;
2577 }
2578 bad_chars |= bad_input_byte(c);
2579 bc_vec_pushByte(vec, (char)c);
2580 } while (c != '\n');
2581
2582 if (bad_chars) {
2583 // Bad chars on this line
2584 if (!G.prog.file) { // stdin
2585 // ignore entire line, get another one
2586 vec->len = len;
2587 goto again;
2588 }
2589 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
2590 }
2591 bc_vec_pushZeroByte(vec);
2592 }
2593}
2594
2595//
2596// Parsing routines
2597//
2598
2599static bool bc_num_strValid(const char *val, size_t base)
2600{
2601 BcDig b;
2602 bool radix;
2603
2604 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2605 radix = false;
2606 for (;;) {
2607 BcDig c = *val++;
2608 if (c == '\0')
2609 break;
2610 if (c == '.') {
2611 if (radix) return false;
2612 radix = true;
2613 continue;
2614 }
2615 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2616 return false;
2617 }
2618 return true;
2619}
2620
2621// Note: n is already "bc_num_zero()"ed,
2622// leading zeroes in "val" are removed
2623static void bc_num_parseDecimal(BcNum *n, const char *val)
2624{
2625 size_t len, i;
2626 const char *ptr;
2627
2628 len = strlen(val);
2629 if (len == 0)
2630 return;
2631
2632 bc_num_expand(n, len);
2633
2634 ptr = strchr(val, '.');
2635
2636 n->rdx = 0;
2637 if (ptr != NULL)
2638 n->rdx = (size_t)((val + len) - (ptr + 1));
2639
2640 for (i = 0; val[i]; ++i) {
2641 if (val[i] != '0' && val[i] != '.') {
2642 // Not entirely zero value - convert it, and exit
2643 i = len - 1;
2644 for (;;) {
2645 n->num[n->len] = val[i] - '0';
2646 ++n->len;
2647 skip_dot:
2648 if (i == 0) break;
2649 if (val[--i] == '.') goto skip_dot;
2650 }
2651 break;
2652 }
2653 }
2654 // if for() exits without hitting if(), the value is entirely zero
2655}
2656
2657// Note: n is already "bc_num_zero()"ed,
2658// leading zeroes in "val" are removed
2659static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2660{
2661 BcStatus s;
2662 BcNum temp, mult, result;
2663 BcNum base;
2664 BcDig base_digs[ULONG_NUM_BUFSIZE];
2665 BcDig c = '\0';
2666 unsigned long v;
2667 size_t i, digits;
2668
2669 for (i = 0; ; ++i) {
2670 if (val[i] == '\0')
2671 return;
2672 if (val[i] != '.' && val[i] != '0')
2673 break;
2674 }
2675
2676 bc_num_init_DEF_SIZE(&temp);
2677 bc_num_init_DEF_SIZE(&mult);
2678 base.cap = ARRAY_SIZE(base_digs);
2679 base.num = base_digs;
2680 bc_num_ulong2num(&base, base_t);
2681
2682 for (;;) {
2683 c = *val++;
2684 if (c == '\0') goto int_err;
2685 if (c == '.') break;
2686
2687 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2688
2689 s = zbc_num_mul(n, &base, &mult, 0);
2690 if (s) goto int_err;
2691 bc_num_ulong2num(&temp, v);
2692 s = zbc_num_add(&mult, &temp, n, 0);
2693 if (s) goto int_err;
2694 }
2695
2696 bc_num_init(&result, base.len);
2697 //bc_num_zero(&result); - already is
2698 bc_num_one(&mult);
2699
2700 digits = 0;
2701 for (;;) {
2702 c = *val++;
2703 if (c == '\0') break;
2704 digits++;
2705
2706 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2707
2708 s = zbc_num_mul(&result, &base, &result, 0);
2709 if (s) goto err;
2710 bc_num_ulong2num(&temp, v);
2711 s = zbc_num_add(&result, &temp, &result, 0);
2712 if (s) goto err;
2713 s = zbc_num_mul(&mult, &base, &mult, 0);
2714 if (s) goto err;
2715 }
2716
2717 s = zbc_num_div(&result, &mult, &result, digits);
2718 if (s) goto err;
2719 s = zbc_num_add(n, &result, n, digits);
2720 if (s) goto err;
2721
2722 if (n->len != 0) {
2723 if (n->rdx < digits)
2724 bc_num_extend(n, digits - n->rdx);
2725 } else
2726 bc_num_zero(n);
2727 err:
2728 bc_num_free(&result);
2729 int_err:
2730 bc_num_free(&mult);
2731 bc_num_free(&temp);
2732}
2733
2734static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
2735{
2736 if (!bc_num_strValid(val, base_t))
2737 RETURN_STATUS(bc_error("bad number string"));
2738
2739 bc_num_zero(n);
2740 while (*val == '0') val++;
2741
2742 if (base_t == 10)
2743 bc_num_parseDecimal(n, val);
2744 else
2745 bc_num_parseBase(n, val, base_t);
2746
2747 RETURN_STATUS(BC_STATUS_SUCCESS);
2748}
2749#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
2750
Gavin Howard01055ba2018-11-03 11:00:21 -06002751static void bc_lex_lineComment(BcLex *l)
2752{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002753 // Try: echo -n '#foo' | bc
2754 size_t i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002755 l->lex = XC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002756 i = l->i;
2757 while (i < l->len && l->buf[i] != '\n')
2758 i++;
2759 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002760}
2761
2762static void bc_lex_whitespace(BcLex *l)
2763{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002764 l->lex = XC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002765 for (;;) {
2766 char c = l->buf[l->i];
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002767 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002768 break;
2769 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002770 break;
2771 l->i++;
2772 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002773}
2774
Denys Vlasenko29301232018-12-11 15:29:32 +01002775static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002776{
2777 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002778 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002779 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002780
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002781 pt = (start == '.');
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002782 l->lex = XC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002783 ccnt = i = 0;
2784 for (;;) {
2785 char c = buf[i];
2786 if (c == '\0')
2787 break;
2788 if (c == '\\' && buf[i + 1] == '\n') {
2789 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002790 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002791 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002792 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002793 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2794 if (c != '.') break;
2795 // if '.' was already seen, stop on second one:
2796 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002797 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002798 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002799 // buf[i] is one of "0-9A-F."
2800 i++;
2801 if (c != '.')
2802 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002803 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002804 //ccnt is the number of chars in the number string, excluding possible
2805 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2806 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002807 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002808
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002809 // This might overestimate the size, if there are "\<NL>"'s
2810 // in the number. Subtracting number_of_backslashes*2 correctly
2811 // is not that easy: consider that in the case of "NNN.\<NL>"
2812 // loop above will count "\<NL>" before it realizes it is not
2813 // in fact *inside* the number:
2814 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002815
Denys Vlasenko64074a12018-12-07 15:50:14 +01002816 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2817 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2818 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002819 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002820 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002821
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002822 bc_vec_pop_all(&l->lex_buf);
2823 bc_vec_expand(&l->lex_buf, 1 + len);
2824 bc_vec_push(&l->lex_buf, &start);
Gavin Howard01055ba2018-11-03 11:00:21 -06002825
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002826 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002827 // If we have hit a backslash, skip it. We don't have
2828 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002829 if (*buf == '\\') {
2830 buf += 2;
2831 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002832 continue;
2833 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002834 bc_vec_push(&l->lex_buf, buf);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002835 buf++;
2836 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002837 }
2838
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002839 bc_vec_pushZeroByte(&l->lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002840
Denys Vlasenko29301232018-12-11 15:29:32 +01002841 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002842}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002843#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002844
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002845static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002846{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002847 size_t i;
2848 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002849
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002850 l->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06002851
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002852 i = 0;
2853 buf = l->buf + l->i - 1;
2854 for (;;) {
2855 char c = buf[i];
2856 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2857 i++;
2858 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002859
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002860#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002861 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2862 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2863 if (i > BC_MAX_STRING)
2864 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2865 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002866#endif
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002867 bc_vec_string(&l->lex_buf, i, buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002868
2869 // Increment the index. We minus 1 because it has already been incremented.
2870 l->i += i - 1;
2871
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002872 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002873}
2874
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002875static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002876{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002877 bc_char_vec_init(&l->lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002878}
2879
2880static void bc_lex_free(BcLex *l)
2881{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002882 bc_vec_free(&l->lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06002883}
2884
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002885static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002886{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002887 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002888 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002889}
2890
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002891static bool bc_lex_more_input(BcLex *l)
2892{
2893 size_t str;
2894 bool comment;
2895
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002896 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002897
2898 // This loop is complex because the vm tries not to send any lines that end
2899 // with a backslash to the parser. The reason for that is because the parser
2900 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2901 // case, and for strings and comments, the parser will expect more stuff.
2902 comment = false;
2903 str = 0;
2904 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002905 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002906 char *string;
2907
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002908 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002909 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002910 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002911 break;
2912
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002913 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002914 while (*string) {
2915 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002916 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002917 if (IS_BC)
2918 str ^= (c == '"');
2919 else {
2920 if (c == ']')
2921 str -= 1;
2922 else if (c == '[')
2923 str += 1;
2924 }
2925 }
2926 string++;
2927 if (c == '/' && *string == '*') {
2928 comment = true;
2929 string++;
2930 continue;
2931 }
2932 if (c == '*' && *string == '/') {
2933 comment = false;
2934 string++;
2935 }
2936 }
2937 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002938 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002939 continue;
2940 }
2941
2942 // Check for backslash+newline.
2943 // we do not check that last char is '\n' -
2944 // if it is not, then it's EOF, and looping back
2945 // to bc_read_line() will detect it:
2946 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002947 if (string >= G.input_buffer.v && *string == '\\') {
2948 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002949 continue;
2950 }
2951
2952 break;
2953 }
2954
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002955 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002956 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002957// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2958 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002959
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002960 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002961}
2962
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002963IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2964IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002965#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2966#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002967
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002968static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002969{
2970 BcStatus s;
2971
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002972 l->lex_last = l->lex;
2973 if (l->lex_last == XC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002974
2975 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002976 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002977 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002978
2979 // Loop until failure or we don't have whitespace. This
2980 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002981 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002982 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002983 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002984 if (l->i == l->len) {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01002985 l->lex = XC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002986 if (!G.input_fp)
2987 RETURN_STATUS(BC_STATUS_SUCCESS);
2988 if (!bc_lex_more_input(l)) {
2989 G.input_fp = NULL;
2990 RETURN_STATUS(BC_STATUS_SUCCESS);
2991 }
2992 // here it's guaranteed that l->i is below l->len
2993 }
Denys Vlasenko26384542018-12-25 18:37:52 +01002994 l->lex_next_at = l->buf + l->i;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002995 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko26384542018-12-25 18:37:52 +01002996 (int)(strchrnul(l->lex_next_at, '\n') - l->lex_next_at),
2997 l->lex_next_at
2998 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002999 if (IS_BC) {
3000 IF_BC(s = zbc_lex_token(l));
3001 } else {
3002 IF_DC(s = zdc_lex_token(l));
3003 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003004 } while (!s && l->lex == XC_LEX_WHITESPACE);
3005 dbg_lex("l->lex from string:%d", l->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06003006
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003007 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003008}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003009#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003010
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003011#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01003012static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
3013{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003014 if (l->lex == XC_LEX_NLINE)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003015 RETURN_STATUS(zbc_lex_next(l));
3016 RETURN_STATUS(BC_STATUS_SUCCESS);
3017}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003018#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003019
3020static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
3021{
3022 BcStatus s;
3023 s = zbc_lex_next(l);
3024 if (s) RETURN_STATUS(s);
3025 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
3026 s = zbc_lex_skip_if_at_NLINE(l);
3027 RETURN_STATUS(s);
3028}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003029#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003030#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01003031
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003032static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003033{
3034 l->buf = text;
3035 l->i = 0;
3036 l->len = strlen(text);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003037 l->lex = l->lex_last = XC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003038 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003039}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003040#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003041
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003042#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003043static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003044{
3045 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003046 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003047 const char *buf = l->buf + l->i - 1;
3048
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003049 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3050 const char *keyword8 = bc_lex_kws[i].name8;
3051 unsigned j = 0;
3052 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3053 j++;
3054 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003055 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003056 if (keyword8[j] != '\0')
3057 continue;
3058 match:
3059 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003060 if (isalnum(buf[j]) || buf[j]=='_')
3061 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003062 l->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003063 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003064 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3065 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003066 }
3067
3068 // We minus 1 because the index has already been incremented.
3069 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003070 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003071 }
3072
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003073 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003074 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003075
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003076 if (l->lex_buf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003077 // Prevent this:
3078 // >>> qwe=1
3079 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3080 // '
3081 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003082 s = zbc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003083 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003084
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003085 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003086}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003087#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003088
Denys Vlasenko26819db2018-12-12 16:08:46 +01003089static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003090{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003091 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003092
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003093 l->lex = XC_LEX_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06003094
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003095 nls = 0;
3096 i = l->i;
3097 for (;;) {
3098 char c = l->buf[i];
3099 if (c == '\0') {
3100 l->i = i;
3101 RETURN_STATUS(bc_error("string end could not be found"));
3102 }
3103 if (c == '"')
3104 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003105 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003106 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003107 }
3108
3109 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003110 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3111 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3112 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003113 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003114 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003115 bc_vec_string(&l->lex_buf, len, l->buf + l->i);
Gavin Howard01055ba2018-11-03 11:00:21 -06003116
3117 l->i = i + 1;
3118 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003119 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003120
Denys Vlasenko26819db2018-12-12 16:08:46 +01003121 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003122}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003123#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003124
Denys Vlasenko0a238142018-12-14 16:48:34 +01003125static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003126{
3127 if (l->buf[l->i] == '=') {
3128 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003129 with_and_without >>= 8; // store "with" value
3130 } // else store "without" value
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003131 l->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003132}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003133#define bc_lex_assign(l, with, without) \
3134 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003135
Denys Vlasenko29301232018-12-11 15:29:32 +01003136static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003137{
3138 size_t i, nls = 0;
3139 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003140
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003141 l->lex = XC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003142 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003143 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003144 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003145 check_star:
3146 if (c == '*') {
3147 c = buf[++i];
3148 if (c == '/')
3149 break;
3150 goto check_star;
3151 }
3152 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003154 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003155 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003156 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003157 }
3158
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003159 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003160 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003161 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003162
Denys Vlasenko29301232018-12-11 15:29:32 +01003163 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003164}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003165#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003166
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003167#undef zbc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003168static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003169{
3170 BcStatus s = BC_STATUS_SUCCESS;
3171 char c = l->buf[l->i++], c2;
3172
3173 // This is the workhorse of the lexer.
3174 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003175// case '\0': // probably never reached
3176// l->i--;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003177// l->lex = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003178// l->newline = true;
3179// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003180 case '\n':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003181 l->lex = XC_LEX_NLINE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003182 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003183 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003184 case '\t':
3185 case '\v':
3186 case '\f':
3187 case '\r':
3188 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003189 bc_lex_whitespace(l);
3190 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003191 case '!':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003192 bc_lex_assign(l, XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003193 if (l->lex == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003194 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
3195 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003198 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003199 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003201 case '#':
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003202 s = zbc_POSIX_does_not_allow("'#' script comments");
3203 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003204 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 case '%':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003207 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003209 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003210 c2 = l->buf[l->i];
3211 if (c2 == '&') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003212 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3213 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003214 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003215 l->lex = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003216 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003217 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003218 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 case '(':
3222 case ')':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003223 l->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003224 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003225 case '*':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003226 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
Gavin Howard01055ba2018-11-03 11:00:21 -06003227 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003228 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003229 c2 = l->buf[l->i];
3230 if (c2 == '+') {
3231 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003232 l->lex = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003233 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003234 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003235 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003236 case ',':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003237 l->lex = BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06003238 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003239 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003240 c2 = l->buf[l->i];
3241 if (c2 == '-') {
3242 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003243 l->lex = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003244 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003245 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003246 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003247 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003248 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003249 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003251 l->lex = BC_LEX_KEY_LAST;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003252 s = zbc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003253 }
3254 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003255 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003256 c2 = l->buf[l->i];
3257 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003258 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003259 else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003260 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003261 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003262 case '0':
3263 case '1':
3264 case '2':
3265 case '3':
3266 case '4':
3267 case '5':
3268 case '6':
3269 case '7':
3270 case '8':
3271 case '9':
3272 case 'A':
3273 case 'B':
3274 case 'C':
3275 case 'D':
3276 case 'E':
3277 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003278 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003280 case ';':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003281 l->lex = BC_LEX_SCOLON;
Gavin Howard01055ba2018-11-03 11:00:21 -06003282 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003283 case '<':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003284 bc_lex_assign(l, XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003286 case '=':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003287 bc_lex_assign(l, XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003288 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003289 case '>':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003290 bc_lex_assign(l, XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003291 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003292 case '[':
3293 case ']':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003294 l->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06003295 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003296 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003297 if (l->buf[l->i] == '\n') {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003298 l->lex = XC_LEX_WHITESPACE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003299 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003300 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003301 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003302 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003303 case '^':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003304 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
Gavin Howard01055ba2018-11-03 11:00:21 -06003305 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003306 case 'a':
3307 case 'b':
3308 case 'c':
3309 case 'd':
3310 case 'e':
3311 case 'f':
3312 case 'g':
3313 case 'h':
3314 case 'i':
3315 case 'j':
3316 case 'k':
3317 case 'l':
3318 case 'm':
3319 case 'n':
3320 case 'o':
3321 case 'p':
3322 case 'q':
3323 case 'r':
3324 case 's':
3325 case 't':
3326 case 'u':
3327 case 'v':
3328 case 'w':
3329 case 'x':
3330 case 'y':
3331 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003332 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003333 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003334 case '{':
3335 case '}':
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003336 l->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003337 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003338 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003339 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003340 if (c2 == '|') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003341 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3342 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003343 ++l->i;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003344 l->lex = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003345 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003346 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003347 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003348 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003349 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003350 default:
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003351 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003352 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003353 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003354 }
3355
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003356 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003357}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003358#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003359#endif // ENABLE_BC
3360
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003361#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003362static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003363{
Denys Vlasenko81293c82018-12-24 01:53:55 +01003364 if (G_exreg && isspace(l->buf[l->i])) {
3365 bc_lex_whitespace(l); // eats whitespace (but not newline)
3366 l->i++; // bc_lex_name() expects this
Denys Vlasenko29301232018-12-11 15:29:32 +01003367 bc_lex_name(l);
Denys Vlasenko81293c82018-12-24 01:53:55 +01003368 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003369 bc_vec_pop_all(&l->lex_buf);
3370 bc_vec_push(&l->lex_buf, &l->buf[l->i++]);
3371 bc_vec_pushZeroByte(&l->lex_buf);
3372 l->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003373 }
3374
Denys Vlasenko29301232018-12-11 15:29:32 +01003375 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003376}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003377#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003378
Denys Vlasenko29301232018-12-11 15:29:32 +01003379static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003380{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003381 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003382
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003383 l->lex = XC_LEX_STR;
3384 bc_vec_pop_all(&l->lex_buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003385
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003386 nls = 0;
3387 depth = 1;
3388 i = l->i;
3389 for (;;) {
3390 char c = l->buf[i];
3391 if (c == '\0') {
3392 l->i = i;
3393 RETURN_STATUS(bc_error("string end could not be found"));
3394 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003395 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003396 if (i == l->i || l->buf[i - 1] != '\\') {
3397 if (c == '[') depth++;
3398 if (c == ']')
3399 if (--depth == 0)
3400 break;
3401 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003402 bc_vec_push(&l->lex_buf, &l->buf[i]);
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003403 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003404 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003405 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003406
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003407 bc_vec_pushZeroByte(&l->lex_buf);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003408 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3409 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3410 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003411 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003412 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003413
3414 l->i = i;
3415 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003416 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003417
Denys Vlasenko29301232018-12-11 15:29:32 +01003418 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003419}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003420#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003421
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003422#undef zdc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003423static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003424{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003425 static const //BcLexType - should be this type, but narrower type saves size:
3426 uint8_t
3427 dc_lex_regs[] = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003428 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 +01003429 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3430 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003431 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003432 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003433
Denys Vlasenko81293c82018-12-24 01:53:55 +01003434 BcStatus s;
3435 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003436 size_t i;
3437
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003438 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003439 if (l->lex_last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003440 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003441 }
3442
Denys Vlasenko81293c82018-12-24 01:53:55 +01003443 s = BC_STATUS_SUCCESS;
3444 c = l->buf[l->i++];
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003445 if (c >= '%' && c <= '~'
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003446 && (l->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003447 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003448 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003449 }
3450
3451 // This is the workhorse of the lexer.
3452 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003453// case '\0': // probably never reached
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003454// l->lex = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003455// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003456 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003457 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003458 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003459 // only to allow interactive dc have a way to exit
3460 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003461 // on <enter>, not on _next_ token (which would mean
3462 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003463 // IOW: typing "1p<enter>" should print "1" _at once_,
3464 // not after some more input.
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003465 l->lex = XC_LEX_NLINE;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003466 l->newline = true;
3467 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003468 case '\t':
3469 case '\v':
3470 case '\f':
3471 case '\r':
3472 case ' ':
Denys Vlasenko81293c82018-12-24 01:53:55 +01003473 l->newline = 0; // was (c == '\n')
Gavin Howard01055ba2018-11-03 11:00:21 -06003474 bc_lex_whitespace(l);
3475 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003476 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003477 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003478 if (c2 == '=')
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003479 l->lex = XC_LEX_OP_REL_NE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003480 else if (c2 == '<')
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003481 l->lex = XC_LEX_OP_REL_LE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003482 else if (c2 == '>')
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003483 l->lex = XC_LEX_OP_REL_GE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003484 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003485 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003486 ++l->i;
3487 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003488 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003489 bc_lex_lineComment(l);
3490 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003491 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003492 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003493 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003494 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003495 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003496 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003497 case '0':
3498 case '1':
3499 case '2':
3500 case '3':
3501 case '4':
3502 case '5':
3503 case '6':
3504 case '7':
3505 case '8':
3506 case '9':
3507 case 'A':
3508 case 'B':
3509 case 'C':
3510 case 'D':
3511 case 'E':
3512 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003513 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003514 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003515 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003516 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003517 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003518 default:
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003519 l->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003520 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003521 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003522 }
3523
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003524 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003525}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003526#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003527#endif // ENABLE_DC
3528
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003529static void bc_parse_push(BcParse *p, char i)
3530{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003531 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003532 bc_vec_pushByte(&p->func->code, i);
3533}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003534
Gavin Howard01055ba2018-11-03 11:00:21 -06003535static void bc_parse_pushName(BcParse *p, char *name)
3536{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003537 while (*name)
3538 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003539 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003540}
3541
3542static void bc_parse_pushIndex(BcParse *p, size_t idx)
3543{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003544 size_t mask;
3545 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003546
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003547 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003548 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3549 amt = sizeof(idx);
3550 do {
3551 if (idx & mask) break;
3552 mask >>= 8;
3553 amt--;
3554 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003555
3556 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003557
3558 while (idx != 0) {
3559 bc_parse_push(p, (unsigned char)idx);
3560 idx >>= 8;
3561 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003562}
3563
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003564#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003565static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3566{
3567 bc_parse_push(p, BC_INST_JUMP);
3568 bc_parse_pushIndex(p, idx);
3569}
3570
3571static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3572{
3573 bc_parse_push(p, BC_INST_JUMP_ZERO);
3574 bc_parse_pushIndex(p, idx);
3575}
3576
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003577static BC_STATUS zbc_parse_pushSTR(BcParse *p)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003578{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003579 char *str = xstrdup(p->l.lex_buf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003580
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003581 bc_parse_push(p, XC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003582 bc_parse_pushIndex(p, p->func->strs.len);
3583 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003584
3585 RETURN_STATUS(zbc_lex_next(&p->l));
3586}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003587#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003588#endif
3589
3590static void bc_parse_pushNUM(BcParse *p)
3591{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003592 char *num = xstrdup(p->l.lex_buf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003593#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003594 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003595#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003596 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003597#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003598 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003599#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003600 bc_parse_push(p, XC_INST_NUM);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003601 bc_parse_pushIndex(p, idx);
3602}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003603
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003604static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003605{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003606 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003607
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003608 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003609}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003610#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003611
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003612// Called when parsing or execution detects a failure,
3613// resets execution structures.
3614static void bc_program_reset(void)
3615{
3616 BcFunc *f;
3617 BcInstPtr *ip;
3618
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003619 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003620 bc_vec_pop_all(&G.prog.results);
3621
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003622 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003623 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003624 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003625}
3626
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003627// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003628// resets parsing structures.
3629static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003630{
3631 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003632 bc_func_free(p->func);
3633 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003634
Denys Vlasenko65e10462018-12-19 15:13:14 +01003635 p->fidx = BC_PROG_MAIN;
3636 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003637 }
3638
3639 p->l.i = p->l.len;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003640 p->l.lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003641
Denys Vlasenko503faf92018-12-20 16:24:18 +01003642 IF_BC(bc_vec_pop_all(&p->exits);)
3643 IF_BC(bc_vec_pop_all(&p->conds);)
3644 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003645
Denys Vlasenkod38af482018-12-04 19:11:02 +01003646 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003647}
3648
3649static void bc_parse_free(BcParse *p)
3650{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003651 IF_BC(bc_vec_free(&p->exits);)
3652 IF_BC(bc_vec_free(&p->conds);)
3653 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003654 bc_lex_free(&p->l);
3655}
3656
Denys Vlasenkofa210792018-12-19 19:35:40 +01003657static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003658{
3659 memset(p, 0, sizeof(BcParse));
3660
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003661 bc_lex_init(&p->l);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003662 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3663 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3664 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003665
Denys Vlasenkofa210792018-12-19 19:35:40 +01003666 p->fidx = fidx;
3667 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003668}
3669
Denys Vlasenko04715442018-12-21 00:10:26 +01003670static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003671{
Denys Vlasenko04715442018-12-21 00:10:26 +01003672 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003673 BcFunc f;
3674 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003675 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003676 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003677 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003678}
3679
3680#if ENABLE_BC
3681
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003682// Note: takes ownership of 'name' (must be malloced)
3683static size_t bc_program_addFunc(char *name)
3684{
3685 size_t idx;
3686 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003687 int inserted;
3688
3689 entry.name = name;
3690 entry.idx = G.prog.fns.len;
3691
3692 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3693 if (!inserted) free(name);
3694
3695 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3696 idx = entry_ptr->idx;
3697
3698 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003699 // There is already a function with this name.
3700 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003701 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003702 bc_func_free(func);
3703 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003704 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003705 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003706 }
3707
3708 return idx;
3709}
3710
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003711#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003712// We can calculate the conversion between tokens and exprs by subtracting the
3713// position of the first operator in the lex enum and adding the position of the
3714// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003715#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003716
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003717static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags);
3718
3719static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags)
3720{
3721 BcStatus s;
3722
3723 s = bc_parse_expr_empty_ok(p, flags);
3724 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3725 RETURN_STATUS(bc_error("empty expression"));
3726 RETURN_STATUS(s);
3727}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003728#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003729
3730static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003731#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003732
3733static BC_STATUS zbc_parse_stmt(BcParse *p)
3734{
3735 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3736}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003737#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003738
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003739static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003740{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003741 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3742 // Same for "else", "while()", "for()".
3743 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3744 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003745 if (p->l.lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003746 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003747
3748 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003749}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003750#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003751
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003752static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3753 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003754{
Denys Vlasenko69560f42018-12-24 14:14:23 +01003755 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3756 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003757
3758 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003759 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003760 if (t == BC_LEX_LPAREN) break;
3761
Denys Vlasenko69560f42018-12-24 14:14:23 +01003762 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003763 if (l >= r && (l != r || !left)) break;
3764
Denys Vlasenko0154d782018-12-14 23:32:51 +01003765 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003766 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003767 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003768 }
3769
3770 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003771}
3772
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003773static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003774{
3775 BcLexType top;
3776
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003777 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003778 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003779 top = BC_PARSE_TOP_OP(p);
3780
3781 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003782 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003783
3784 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003785 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003786
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003787 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003788 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003789 top = BC_PARSE_TOP_OP(p);
3790 }
3791
3792 bc_vec_pop(&p->ops);
3793
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003794 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003795}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003796#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003797
Denys Vlasenko26819db2018-12-12 16:08:46 +01003798static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003799{
3800 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003801 size_t nparams;
3802
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003803 dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003804 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3805
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003806 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003807 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003808
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003809 nparams = 0;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003810 if (p->l.lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003811 for (;;) {
3812 s = zbc_parse_expr(p, flags);
3813 if (s) RETURN_STATUS(s);
3814 nparams++;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003815 if (p->l.lex != BC_LEX_COMMA) {
3816 if (p->l.lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003817 break;
3818 RETURN_STATUS(bc_error_bad_token());
3819 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003820 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003821 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003822 }
3823 }
3824
Gavin Howard01055ba2018-11-03 11:00:21 -06003825 bc_parse_push(p, BC_INST_CALL);
3826 bc_parse_pushIndex(p, nparams);
3827
Denys Vlasenko26819db2018-12-12 16:08:46 +01003828 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003829}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003830#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003831
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003832// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003833static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003834{
3835 BcStatus s;
3836 BcId entry, *entry_ptr;
3837 size_t idx;
3838
3839 entry.name = name;
3840
Denys Vlasenko26819db2018-12-12 16:08:46 +01003841 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003842 if (s) goto err;
3843
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003844 if (p->l.lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003845 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003846 goto err;
3847 }
3848
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003849 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003850
3851 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003852 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003853 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003854 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003855 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003856 free(name);
3857
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003858 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003859 bc_parse_pushIndex(p, entry_ptr->idx);
3860
Denys Vlasenko26819db2018-12-12 16:08:46 +01003861 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003862 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003863 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003864 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003865}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003866#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003867
Denys Vlasenko26819db2018-12-12 16:08:46 +01003868static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003869{
3870 BcStatus s;
3871 char *name;
3872
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003873 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003874 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003875 if (s) goto err;
3876
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003877 if (p->l.lex == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003878 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003879 if (s) goto err;
3880
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003881 if (p->l.lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003882 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003883 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003884 goto err;
3885 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003886 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003887 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003888 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003889 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003890 s = zbc_parse_expr(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003891 if (s) goto err;
3892 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003893 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 if (s) goto err;
3895 bc_parse_push(p, *type);
3896 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003897 free(name);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003898 } else if (p->l.lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003899 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003900 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003901 goto err;
3902 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003903 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003904 s = zbc_parse_call(p, name, flags);
3905 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003906 *type = XC_INST_VAR;
3907 bc_parse_push(p, XC_INST_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06003908 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003909 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003910 }
3911
Denys Vlasenko26819db2018-12-12 16:08:46 +01003912 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003913 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003914 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003915 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003916}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003917#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003918
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003919static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003920{
3921 BcStatus s;
3922
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003923 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003924 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003925 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003926
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003927 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003928 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003929 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003930
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003931 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003932
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003933 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003934}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003935#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003936
Denys Vlasenko26819db2018-12-12 16:08:46 +01003937static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003938 BcInst *prev)
3939{
3940 BcStatus s;
3941
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003942 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003943 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003944 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003945
3946 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3947
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003948 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003949 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003950
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003951 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003952 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003953
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003954 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003955
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003956 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Gavin Howard01055ba2018-11-03 11:00:21 -06003957 bc_parse_push(p, *prev);
3958
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003959 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003960}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003961#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003962
Denys Vlasenko26819db2018-12-12 16:08:46 +01003963static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003964{
3965 BcStatus s;
3966
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003967 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003968 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003969
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003970 if (p->l.lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003971 *type = XC_INST_SCALE;
3972 bc_parse_push(p, XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003973 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003974 }
3975
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003976 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003977 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3978
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003979 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003980 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003981
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003982 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003983 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01003984 if (p->l.lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003985 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003986 bc_parse_push(p, XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003987
Denys Vlasenko26819db2018-12-12 16:08:46 +01003988 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003989}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003990#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003991
Denys Vlasenko26819db2018-12-12 16:08:46 +01003992static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003993 size_t *nexprs, uint8_t flags)
3994{
3995 BcStatus s;
3996 BcLexType type;
3997 char inst;
3998 BcInst etype = *prev;
3999
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004000 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4001 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4002 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004003 ) {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004004 *prev = inst = BC_INST_INC_POST + (p->l.lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004005 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004006 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004007 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004008 *prev = inst = BC_INST_INC_PRE + (p->l.lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06004009 *paren_expr = true;
4010
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004011 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004012 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004013 type = p->l.lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06004014
4015 // Because we parse the next part of the expression
4016 // right here, we need to increment this.
4017 *nexprs = *nexprs + 1;
4018
4019 switch (type) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004020 case XC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004021 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004022 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 case BC_LEX_KEY_IBASE:
4024 case BC_LEX_KEY_LAST:
4025 case BC_LEX_KEY_OBASE:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004026 bc_parse_push(p, type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004027 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004028 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004029 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004030 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004031 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004032 if (p->l.lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004033 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004034 else
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004035 bc_parse_push(p, XC_INST_SCALE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004036 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004037 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004038 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004039 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004040 }
4041
4042 if (!s) bc_parse_push(p, inst);
4043 }
4044
Denys Vlasenko26819db2018-12-12 16:08:46 +01004045 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004046}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004047#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004048
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004049#if 0
4050#define BC_PARSE_LEAF(p, rparen) \
4051 ((rparen) \
4052 || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \
4053 || (p) == BC_INST_INC_POST \
4054 || (p) == BC_INST_DEC_POST \
4055 )
4056#else
4057static int ok_in_expr(BcInst p)
4058{
4059 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
4060 || p == BC_INST_INC_POST
4061 || p == BC_INST_DEC_POST
4062 ;
4063}
4064#define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p))
4065#endif
4066
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004067static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004068 bool rparen, size_t *nexprs)
4069{
4070 BcStatus s;
4071 BcLexType type;
4072 BcInst etype = *prev;
4073
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004074 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004075 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004076
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004077 type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004078 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004079
4080 // We can just push onto the op stack because this is the largest
4081 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004082 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004083 bc_vec_push(&p->ops, &type);
4084 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004085 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004086
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004087 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004088}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004089#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004090
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004091static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004092{
4093 BcStatus s;
4094 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004095
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004096 for (;;) {
4097 s = zbc_lex_next(&p->l);
4098 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004099 type = p->l.lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004100 if (type == XC_LEX_STR) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004101 s = zbc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004102 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004103 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004104 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004105 if (s) RETURN_STATUS(s);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004106 bc_parse_push(p, XC_INST_PRINT_POP);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004107 if (p->l.lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004108 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004109 }
4110
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004111 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004112}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004113#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004114
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004115static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004116{
4117 BcStatus s;
4118 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004119
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004120 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004121 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004122 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004123
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004124 t = p->l.lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004125 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Gavin Howard01055ba2018-11-03 11:00:21 -06004126 bc_parse_push(p, BC_INST_RET0);
4127 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004128 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004129 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004130 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004131 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004132 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004133 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004134 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004135
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004136 if (!paren || p->l.lex_last != BC_LEX_RPAREN) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004137 s = zbc_POSIX_requires("parentheses around return expressions");
4138 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004139 }
4140
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004141 bc_parse_push(p, XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004142 }
4143
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004144 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004145 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004146}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004147#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004148
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004149static void rewrite_label_to_current(BcParse *p, size_t idx)
4150{
4151 size_t *label = bc_vec_item(&p->func->labels, idx);
4152 *label = p->func->code.len;
4153}
4154
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004155static BC_STATUS zbc_parse_if(BcParse *p)
4156{
4157 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004158 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004159
4160 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4161 s = zbc_lex_next(&p->l);
4162 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004163 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004164
4165 s = zbc_lex_next(&p->l);
4166 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004167 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004168 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004169 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004170
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004171 // Encode "if zero, jump to ..."
4172 // Pushed value (destination of the jump) is uninitialized,
4173 // will be rewritten to be address of "end of if()" or of "else".
4174 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004175 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004176
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004177 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004178 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004179
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004180 dbg_lex("%s:%d in if after stmt: p->l.lex:%d", __func__, __LINE__, p->l.lex);
4181 if (p->l.lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004182 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004183
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004184 // Encode "after then_stmt, jump to end of if()"
4185 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4186 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004187 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004188
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004189 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 +01004190 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004191
Denys Vlasenko15850832018-12-16 21:40:54 +01004192 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004193
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004194 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004195 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004196 }
4197
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004198 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 +01004199 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004200
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004201 dbg_lex_done("%s:%d done", __func__, __LINE__);
4202 RETURN_STATUS(s);
4203}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004204#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004205
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004206static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004207{
4208 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004209 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004210 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004211
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004212 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004213 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004214 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004215 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004216 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004217
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004218 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004219 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004220 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004221
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004222 bc_vec_push(&p->exits, &ip_idx);
4223 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004224
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004225 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004226 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004227 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004228
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004229 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004230
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004231 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004232 if (s) RETURN_STATUS(s);
4233
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004234 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004235 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004236
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004237 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004238 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004239
4240 bc_vec_pop(&p->exits);
4241 bc_vec_pop(&p->conds);
4242
4243 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004244}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004245#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004246
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004247static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004248{
4249 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004250 size_t cond_idx, exit_idx, body_idx, update_idx;
4251
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004252 dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004253 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004254 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004255 if (p->l.lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004256 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004257 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004258
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004259 if (p->l.lex != BC_LEX_SCOLON) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004260 s = zbc_parse_expr(p, 0);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004261 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004262 if (s) RETURN_STATUS(s);
4263 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004264 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4265 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004266 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004267
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004268 if (p->l.lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004269 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004270 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004271
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004272 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004273 update_idx = cond_idx + 1;
4274 body_idx = update_idx + 1;
4275 exit_idx = body_idx + 1;
4276
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004277 if (p->l.lex != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004278 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004279 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004280 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004281 // This is safe to set because the current token is a semicolon,
4282 // which has no string requirement.
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004283 bc_vec_string(&p->l.lex_buf, 1, "1");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004284 bc_parse_pushNUM(p);
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004285 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004286 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004287 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004288
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004289 if (p->l.lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004290
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004291 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004292 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004293
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004294 bc_parse_pushJUMP_ZERO(p, exit_idx);
4295 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004296
Gavin Howard01055ba2018-11-03 11:00:21 -06004297 bc_vec_push(&p->conds, &update_idx);
4298 bc_vec_push(&p->func->labels, &p->func->code.len);
4299
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004300 if (p->l.lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004301 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004302 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004303 if (p->l.lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004304 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004305 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004306 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4307 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004308 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004309
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004310 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004311 bc_vec_push(&p->func->labels, &p->func->code.len);
4312
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004313 bc_vec_push(&p->exits, &exit_idx);
4314 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004315
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004316 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004317 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004318
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004319 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004320 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004321
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004322 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004323 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004324
4325 bc_vec_pop(&p->exits);
4326 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004327
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004328 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004329}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004330#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004331
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004332static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004333{
Gavin Howard01055ba2018-11-03 11:00:21 -06004334 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004335
Gavin Howard01055ba2018-11-03 11:00:21 -06004336 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004337 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4338 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004339 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004340 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004341 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004342 }
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004343 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004344
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004345 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004346}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004347#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004348
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004349static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4350{
4351 BcId *autoid;
4352 BcId a;
4353 size_t i;
4354
4355 autoid = (void*)f->autos.v;
4356 for (i = 0; i < f->autos.len; i++, autoid++) {
4357 if (strcmp(name, autoid->name) == 0)
4358 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
4359 }
4360
4361 a.idx = var;
4362 a.name = name;
4363
4364 bc_vec_push(&f->autos, &a);
4365
4366 RETURN_STATUS(BC_STATUS_SUCCESS);
4367}
4368#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4369
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004370static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004371{
4372 BcStatus s;
4373 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004374 char *name;
4375
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004376 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004377 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004378 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004379 if (p->l.lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004380 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004381
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004382 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004383 p->fidx = bc_program_addFunc(name);
4384 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004385
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004386 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004387 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004388 if (p->l.lex != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004389 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004390 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004391 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004392
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004393 while (p->l.lex != BC_LEX_RPAREN) {
4394 if (p->l.lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004395 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004396
4397 ++p->func->nparams;
4398
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004399 name = xstrdup(p->l.lex_buf.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004400 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004401 if (s) goto err;
4402
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004403 var = p->l.lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004404
4405 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004406 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004407 if (s) goto err;
4408
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004409 if (p->l.lex != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004410 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004411 goto err;
4412 }
4413
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004414 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004415 if (s) goto err;
4416 }
4417
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004418 comma = p->l.lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004419 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004420 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004421 if (s) goto err;
4422 }
4423
Denys Vlasenko29301232018-12-11 15:29:32 +01004424 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004425 if (s) goto err;
4426 }
4427
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004428 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004429
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004430 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004431 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004432
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004433 if (p->l.lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004434 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004435 if (s) RETURN_STATUS(s);
4436 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004437
Denys Vlasenko202dd192018-12-16 17:30:35 +01004438 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4439 s = zbc_lex_skip_if_at_NLINE(&p->l);
4440 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004441 //GNU bc requires a {} block even if function body has single stmt, enforce this?
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004442 if (p->l.lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004443 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004444
4445 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004446 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004447 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004448 if (s) RETURN_STATUS(s);
4449
4450 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004451
4452 // Subsequent code generation is into main program
4453 p->fidx = BC_PROG_MAIN;
4454 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004455
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004456 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004457 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004458 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004459 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004460 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004461 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004462}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004463#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004464
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004465static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004466{
4467 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004468 char *name;
4469
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004470 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004471 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004472 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004473
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004474 for (;;) {
4475 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004476
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004477 if (p->l.lex != XC_LEX_NAME)
4478 RETURN_STATUS(bc_error("bad 'auto' syntax"));
4479
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004480 name = xstrdup(p->l.lex_buf.v);
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
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004484 var = (p->l.lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004485 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004486 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004487 if (s) goto err;
4488
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004489 if (p->l.lex != BC_LEX_RBRACKET) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004490 s = bc_error("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004491 goto err;
4492 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004493 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004494 if (s) goto err;
4495 }
4496
Denys Vlasenko29301232018-12-11 15:29:32 +01004497 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004498 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004499
4500 if (p->l.lex == XC_LEX_NLINE
4501 || p->l.lex == BC_LEX_SCOLON
4502 //|| p->l.lex == BC_LEX_RBRACE // allow "define f() {auto a}"
4503 ) {
4504 break;
4505 }
4506 if (p->l.lex != BC_LEX_COMMA)
4507 RETURN_STATUS(bc_error("bad 'auto' syntax"));
4508 s = zbc_lex_next(&p->l); // skip comma
4509 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004510 }
4511
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004512 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004513 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004514 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004515 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004516 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004517 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004518}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004519#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004520
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004521#undef zbc_parse_stmt_possibly_auto
4522static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004523{
4524 BcStatus s = BC_STATUS_SUCCESS;
4525
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004526 dbg_lex_enter("%s:%d entered, p->l.lex:%d", __func__, __LINE__, p->l.lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004527
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004528 if (p->l.lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004529 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004530 RETURN_STATUS(zbc_lex_next(&p->l));
4531 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004532 if (p->l.lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004533 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4534 RETURN_STATUS(zbc_lex_next(&p->l));
4535 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004536
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004537 if (p->l.lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004538 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4539 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004540 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004541 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004542 } while (p->l.lex == XC_LEX_NLINE);
4543 if (auto_allowed && p->l.lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004544 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4545 s = zbc_parse_auto(p);
4546 if (s) RETURN_STATUS(s);
4547 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004548 while (p->l.lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004549 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4550 s = zbc_parse_stmt(p);
4551 if (s) RETURN_STATUS(s);
4552 }
4553 s = zbc_lex_next(&p->l);
4554 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4555 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004556 }
4557
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004558 dbg_lex("%s:%d p->l.lex:%d", __func__, __LINE__, p->l.lex);
4559 switch (p->l.lex) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004560 case XC_LEX_OP_MINUS:
Gavin Howard01055ba2018-11-03 11:00:21 -06004561 case BC_LEX_OP_INC:
4562 case BC_LEX_OP_DEC:
Gavin Howard01055ba2018-11-03 11:00:21 -06004563 case BC_LEX_OP_BOOL_NOT:
4564 case BC_LEX_LPAREN:
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004565 case XC_LEX_NAME:
4566 case XC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06004567 case BC_LEX_KEY_IBASE:
4568 case BC_LEX_KEY_LAST:
4569 case BC_LEX_KEY_LENGTH:
4570 case BC_LEX_KEY_OBASE:
4571 case BC_LEX_KEY_READ:
4572 case BC_LEX_KEY_SCALE:
4573 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004574 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004576 case XC_LEX_STR:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004577 s = zbc_parse_pushSTR(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004578 bc_parse_push(p, XC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004579 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004580 case BC_LEX_KEY_BREAK:
4581 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004582 s = zbc_parse_break_or_continue(p, p->l.lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004584 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004585 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004586 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004587 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004588 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004589 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004590 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004591 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004592 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004593 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004594 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004595 // "limits" is a compile-time command,
4596 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004597 printf(
4598 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4599 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4600 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4601 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4602 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4603 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4604 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4605 "Number of vars = "BC_MAX_VARS_STR "\n"
4606 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004607 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004608 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004609 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004610 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004611 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004612 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004613 // "quit" is a compile-time command. For example,
4614 // "if (0 == 1) quit" terminates when parsing the statement,
4615 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004616 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004617 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004618 if (!p->in_funcdef)
4619 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004620 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004621 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004622 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004623 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004624 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004625 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004626 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004627 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004628 }
4629
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004630 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004631 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004632}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004633#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004634
Denys Vlasenko99b37622018-12-15 20:06:59 +01004635static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004636{
4637 BcStatus s;
4638
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004639 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004640 if (p->l.lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004641 s = bc_error("end of file");
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004642 else if (p->l.lex == BC_LEX_KEY_DEFINE) {
4643 dbg_lex("%s:%d p->l.lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004644 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004645 } else {
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004646 dbg_lex("%s:%d p->l.lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.lex);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004647 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004648 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004649
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004650 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004651 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004652}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004653#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004654
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004655// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004656static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004657{
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004658 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004659 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004660 unsigned nparens, nrelops;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004661 bool paren_first, paren_expr, rprn, assign, bin_last;
Gavin Howard01055ba2018-11-03 11:00:21 -06004662
Denys Vlasenko17df8822018-12-14 23:00:24 +01004663 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004664 paren_first = (p->l.lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004665 nparens = nrelops = 0;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004666 paren_expr = rprn = assign = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004667 bin_last = true;
4668
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004669 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004670 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004671 BcStatus s;
4672 BcLexType t = p->l.lex;
4673
4674 if (!lex_allowed_in_bc_expr(t))
4675 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004676
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004677 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004678 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004679 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004680 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004681 case BC_LEX_OP_INC:
4682 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004683 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004684 rprn = bin_last = false;
4685 //get_token = false; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06004686 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004687 case XC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004688 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004689 rprn = false;
4690 //get_token = false; - already is
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004691 bin_last = (prev == XC_INST_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004692 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004693 case BC_LEX_OP_ASSIGN_POWER:
4694 case BC_LEX_OP_ASSIGN_MULTIPLY:
4695 case BC_LEX_OP_ASSIGN_DIVIDE:
4696 case BC_LEX_OP_ASSIGN_MODULUS:
4697 case BC_LEX_OP_ASSIGN_PLUS:
4698 case BC_LEX_OP_ASSIGN_MINUS:
4699 case BC_LEX_OP_ASSIGN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004700 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4701 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4702 && prev != XC_INST_OBASE && prev != BC_INST_LAST
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004703 ) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004704 return bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004705 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004706 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004707 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004708 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004709 // Fallthrough.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004710 case XC_LEX_OP_POWER:
4711 case XC_LEX_OP_MULTIPLY:
4712 case XC_LEX_OP_DIVIDE:
4713 case XC_LEX_OP_MODULUS:
4714 case XC_LEX_OP_PLUS:
4715 case XC_LEX_OP_REL_EQ:
4716 case XC_LEX_OP_REL_LE:
4717 case XC_LEX_OP_REL_GE:
4718 case XC_LEX_OP_REL_NE:
4719 case XC_LEX_OP_REL_LT:
4720 case XC_LEX_OP_REL_GT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004721 case BC_LEX_OP_BOOL_NOT:
4722 case BC_LEX_OP_BOOL_OR:
4723 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004724 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004725 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004726 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004727 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004728 }
Denys Vlasenko69560f42018-12-24 14:14:23 +01004729 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
Denys Vlasenko0154d782018-12-14 23:32:51 +01004730 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004731 bc_parse_operator(p, t, ops_bgn, &nexprs);
4732 s = zbc_lex_next(&p->l);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004733 rprn = false;
4734 //get_token = false; - already is
Denys Vlasenko69560f42018-12-24 14:14:23 +01004735 bin_last = (t != BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004736 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004737 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004738 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004739 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004740 bc_vec_push(&p->ops, &t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004741 nparens++;
4742 get_token = true;
4743 paren_expr = false;
4744 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004745 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004746 case BC_LEX_RPAREN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004747 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004748 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004749 if (nparens == 0) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004750 goto exit_loop;
Gavin Howard01055ba2018-11-03 11:00:21 -06004751 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004752 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004753 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004754 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004755 }
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004756 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004757 nparens--;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004758 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004759 paren_expr = rprn = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004760 bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004761 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004762 case XC_LEX_NAME:
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 Vlasenko26819db2018-12-12 16:08:46 +01004765 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004766 paren_expr = true;
4767 rprn = bin_last = false;
4768 //get_token = false; - already is
Denys Vlasenkobb116032018-12-25 01:16:37 +01004769 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004770 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004771 case XC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004772 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004773 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004774 bc_parse_pushNUM(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004775 prev = XC_INST_NUM;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004776 get_token = true;
4777 paren_expr = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004778 rprn = bin_last = false;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004779 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004780 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004781 case BC_LEX_KEY_IBASE:
4782 case BC_LEX_KEY_LAST:
4783 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004784 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004785 return bc_error_bad_expression();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004786 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004787 bc_parse_push(p, (char) prev);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004788 get_token = true;
4789 paren_expr = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004790 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004791 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004792 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004793 case BC_LEX_KEY_LENGTH:
4794 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004795 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004796 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004797 s = zbc_parse_builtin(p, t, flags, &prev);
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004798 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004799 paren_expr = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004800 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004801 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004802 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004803 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004804 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004805 return bc_error_bad_expression();
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004806 s = zbc_parse_read(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004807 prev = XC_INST_READ;
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004808 get_token = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004809 paren_expr = true;
4810 rprn = bin_last = false;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004811 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004812 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004813 case BC_LEX_KEY_SCALE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004814 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004815 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004816 s = zbc_parse_scale(p, &prev, flags);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004817 prev = XC_INST_SCALE;
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01004818 //get_token = false; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06004819 paren_expr = true;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004820 rprn = bin_last = false;
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01004821 nexprs++;
Gavin Howard01055ba2018-11-03 11:00:21 -06004822 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004823 default:
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004824 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004825 }
4826
Denys Vlasenkobb116032018-12-25 01:16:37 +01004827 if (s || G_interrupt) // error, or ^C: stop parsing
4828 return BC_STATUS_FAILURE;
4829 if (get_token) {
4830 s = zbc_lex_next(&p->l);
4831 if (s) return s;
4832 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004833 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004834 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004835
4836 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004837 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004838 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004839
4840 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004841 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004842
Denys Vlasenko0154d782018-12-14 23:32:51 +01004843 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004844
Denys Vlasenko69560f42018-12-24 14:14:23 +01004845 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004846 bc_vec_pop(&p->ops);
4847 }
4848
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004849 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004850 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004851
Gavin Howard01055ba2018-11-03 11:00:21 -06004852 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004853 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004854 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4855 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004856 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004857 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004858 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4859 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004860 }
4861
4862 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004863 if (paren_first || !assign)
4864 bc_parse_push(p, XC_INST_PRINT);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004865 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004866 }
4867
Denys Vlasenko17df8822018-12-14 23:00:24 +01004868 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004869 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004870}
4871
Gavin Howard01055ba2018-11-03 11:00:21 -06004872#endif // ENABLE_BC
4873
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004874#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004875
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004876static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004877{
4878 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004879
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004880 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004881 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004882 if (p->l.lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004883
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004884 bc_parse_pushName(p, p->l.lex_buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004885
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004886 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004887}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004888#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004889
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004890static void dc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004891{
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004892 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004893 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004894
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004895 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004896
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004897 str = xstrdup(p->l.lex_buf.v);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004898 bc_parse_push(p, XC_INST_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004899 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004900 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004901
4902 // Explanation needed here
4903 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004904 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004905
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004906 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004907}
4908
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004909static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004910{
4911 BcStatus s;
4912
4913 bc_parse_push(p, inst);
4914 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004915 s = zdc_parse_register(p);
4916 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004917 }
4918
4919 if (store) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004920 bc_parse_push(p, DC_INST_SWAP);
4921 bc_parse_push(p, XC_INST_ASSIGN);
4922 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004923 }
4924
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004925 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004926}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004927#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004928
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004929static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004930{
4931 BcStatus s;
4932
4933 bc_parse_push(p, inst);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004934 bc_parse_push(p, DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004935
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004936 s = zdc_parse_register(p);
4937 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004938
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004939 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004940 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004941
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004942 // Note that 'else' part can not be on the next line:
4943 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4944 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004945 if (p->l.lex == DC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004946 s = zdc_parse_register(p);
4947 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004948 s = zbc_lex_next(&p->l);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004949 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004950 bc_parse_push(p, BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004951 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004952
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004953 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004954}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004955#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004956
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004957static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004958{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004959 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004960 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004961 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004962
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004963 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004964 s = BC_STATUS_SUCCESS;
4965 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004966 switch (t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004967 case XC_LEX_OP_REL_EQ:
4968 case XC_LEX_OP_REL_LE:
4969 case XC_LEX_OP_REL_GE:
4970 case XC_LEX_OP_REL_NE:
4971 case XC_LEX_OP_REL_LT:
4972 case XC_LEX_OP_REL_GT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004973 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
Denys Vlasenko69560f42018-12-24 14:14:23 +01004974 s = zdc_parse_cond(p, t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004975 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004976 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004977 case DC_LEX_SCOLON:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004978 case DC_LEX_COLON:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004979 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004980 s = zdc_parse_mem(p, XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004981 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004982 case XC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004983 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004984 dc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004985 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004986 case XC_LEX_NEG:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004987 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
4988 s = zbc_lex_next(&p->l);
4989 if (s) RETURN_STATUS(s);
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01004990 if (p->l.lex != XC_LEX_NUMBER)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004991 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004992 bc_parse_pushNUM(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004993 bc_parse_push(p, XC_INST_NEG);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004994 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004995 case XC_LEX_NUMBER:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004996 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4997 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004998 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004999 case DC_LEX_READ:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005000 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005001 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005002 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005003 case DC_LEX_OP_ASSIGN:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005004 case DC_LEX_STORE_PUSH:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005005 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01005006 assign = (t == DC_LEX_OP_ASSIGN);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005007 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005008 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005009 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005010 case DC_LEX_LOAD:
5011 case DC_LEX_LOAD_POP:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005012 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005013 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005014 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005015 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005016 case DC_LEX_STORE_IBASE:
5017 case DC_LEX_STORE_SCALE:
5018 case DC_LEX_STORE_OBASE:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005019 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005020 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005021 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005022 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005023 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005024 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5025 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005026 }
5027
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005028 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005029
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005030 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005031 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005032}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005033#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005034
Denys Vlasenko39287e02018-12-22 02:23:08 +01005035static BC_STATUS zdc_parse_expr(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005036{
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005037 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005038
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01005039 i = (int)p->l.lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005040 if (i >= 0) {
5041 BcInst inst = dc_LEX_to_INST[i];
5042 if (inst != DC_INST_INVALID) {
5043 bc_parse_push(p, inst);
5044 RETURN_STATUS(zbc_lex_next(&p->l));
5045 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005046 }
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01005047 RETURN_STATUS(zdc_parse_token(p, p->l.lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005048}
5049#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5050
5051static BC_STATUS zdc_parse_exprs_until_eof(BcParse *p)
5052{
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01005053 dbg_lex_enter("%s:%d entered, p->l.lex:%d", __func__, __LINE__, p->l.lex);
5054 while (p->l.lex != XC_LEX_EOF) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005055 BcStatus s = zdc_parse_expr(p);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005056 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005057 }
5058
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005059 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005060 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005061}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005062#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005063
Gavin Howard01055ba2018-11-03 11:00:21 -06005064#endif // ENABLE_DC
5065
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005066//
5067// Execution engine
5068//
5069
5070#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5071#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5072
Denys Vlasenkodf515392018-12-02 19:27:48 +01005073static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005074{
Gavin Howard01055ba2018-11-03 11:00:21 -06005075 BcId e, *ptr;
5076 BcVec *v, *map;
5077 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005078 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005079
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005080 v = var ? &G.prog.vars : &G.prog.arrs;
5081 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005082
5083 e.name = id;
5084 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005085 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005086
5087 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005088 BcVec v2;
5089 bc_array_init(&v2, var);
5090 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005091 }
5092
5093 ptr = bc_vec_item(map, i);
5094 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005095 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005096}
5097
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005098// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005099static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005100{
Gavin Howard01055ba2018-11-03 11:00:21 -06005101 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005102 case XC_RESULT_STR:
5103 case XC_RESULT_TEMP:
5104 case XC_RESULT_IBASE:
5105 case XC_RESULT_SCALE:
5106 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005107 *num = &r->d.n;
5108 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005109 case XC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005110 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005111 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005112 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005113 size_t len;
5114
5115 str = *bc_program_const(r->d.id.idx);
5116 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005117
5118 bc_num_init(&r->d.n, len);
5119
5120 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005121 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005122 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005123 if (s) {
5124 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005125 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005126 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005127 *num = &r->d.n;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005128 r->t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005129 break;
5130 }
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005131 case XC_RESULT_VAR:
5132 case XC_RESULT_ARRAY:
5133 case XC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005134 BcVec *v;
5135
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005136 v = bc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005137
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005138 if (r->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005139 v = bc_vec_top(v);
5140 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5141 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005142 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005143 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005144 break;
5145 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005146#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005147 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005148 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005149 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005150 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005151 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005152 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005153#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005154#if SANITY_CHECKS
Denys Vlasenko503faf92018-12-20 16:24:18 +01005155 default:
5156 // Testing the theory that dc does not reach LAST/ONE
5157 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005158#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005159 }
5160
Denys Vlasenko29301232018-12-11 15:29:32 +01005161 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005162}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005163#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005164
Denys Vlasenko29301232018-12-11 15:29:32 +01005165static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005166 BcResult **r, BcNum **rn, bool assign)
5167{
5168 BcStatus s;
5169 bool hex;
5170 BcResultType lt, rt;
5171
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005172 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005173 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005174
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005175 *r = bc_vec_item_rev(&G.prog.results, 0);
5176 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005177
5178 lt = (*l)->t;
5179 rt = (*r)->t;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005180 hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06005181
Denys Vlasenko29301232018-12-11 15:29:32 +01005182 s = zbc_program_num(*l, ln, false);
5183 if (s) RETURN_STATUS(s);
5184 s = zbc_program_num(*r, rn, hex);
5185 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005186
5187 // We run this again under these conditions in case any vector has been
5188 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005189 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005190 s = zbc_program_num(*l, ln, false);
5191 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005192 }
5193
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005194 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005195 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005196 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005197 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005198
Denys Vlasenko29301232018-12-11 15:29:32 +01005199 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005200}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005201#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005202
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005203static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005204{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005205 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005206 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005207 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005208}
5209
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005210// Note: *r and *n need not be initialized by caller
Denys Vlasenko29301232018-12-11 15:29:32 +01005211static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005212{
5213 BcStatus s;
5214
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005215 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005216 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005217 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005218
Denys Vlasenko29301232018-12-11 15:29:32 +01005219 s = zbc_program_num(*r, n, false);
5220 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005221
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005222 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005223 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005224
Denys Vlasenko29301232018-12-11 15:29:32 +01005225 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005226}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005227#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005228
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005229static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005230{
5231 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005232 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005233}
5234
Denys Vlasenko259137d2018-12-11 19:42:05 +01005235static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005236{
5237 BcStatus s;
5238 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005239 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005240
Denys Vlasenko29301232018-12-11 15:29:32 +01005241 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005242 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005243 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005244
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005245 s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005246 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 -06005247 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005248 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005249
Denys Vlasenko259137d2018-12-11 19:42:05 +01005250 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005251 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005252 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005253 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005254}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005255#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005256
Denys Vlasenko26819db2018-12-12 16:08:46 +01005257static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005258{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005259 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005260 BcStatus s;
5261 BcParse parse;
5262 BcVec buf;
5263 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005264 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005265
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005266 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005267 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005268
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005269 sv_file = G.prog.file;
5270 G.prog.file = NULL;
5271
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005272 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005273 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005274
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005275 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005276 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005277
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005278 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005279 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005280 if (IS_BC) {
5281 IF_BC(s = zbc_parse_expr(&parse, 0));
5282 } else {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005283 IF_DC(s = zdc_parse_exprs_until_eof(&parse));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005284 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005285 if (s) goto exec_err;
5286
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01005287 if (parse.l.lex != XC_LEX_NLINE && parse.l.lex != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005288 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005289 goto exec_err;
5290 }
5291
5292 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005293 ip.inst_idx = 0;
5294 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005295
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005296 bc_parse_push(&parse, XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005297 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005298 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005299 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005300 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005301 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005302 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005303}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005304#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005305
5306static size_t bc_program_index(char *code, size_t *bgn)
5307{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005308 unsigned char *bytes = (void*)(code + *bgn);
5309 unsigned amt;
5310 unsigned i;
5311 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005312
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005313 amt = *bytes++;
5314 *bgn += amt + 1;
5315
5316 amt *= 8;
5317 res = 0;
5318 for (i = 0; i < amt; i += 8)
5319 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005320
5321 return res;
5322}
5323
5324static char *bc_program_name(char *code, size_t *bgn)
5325{
5326 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005327 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005328
Denys Vlasenko694d2982018-12-18 19:17:11 +01005329 code += *bgn;
5330 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005331 i = 0;
5332 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005333 char c = *code++;
5334 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005335 break;
5336 s[i++] = c;
5337 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005338 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005339 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005340
5341 return s;
5342}
5343
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005344static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005345{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005346#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005347 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005348 // Example: echo '[]ap' | dc
5349 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005350 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005351 return;
5352 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005353#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005354 while (*str) {
5355 int c = *str++;
5356 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005357 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005358 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005359 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005360 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005361 case 'a':
5362 bb_putchar('\a');
5363 break;
5364 case 'b':
5365 bb_putchar('\b');
5366 break;
5367 case '\\':
5368 case 'e':
5369 bb_putchar('\\');
5370 break;
5371 case 'f':
5372 bb_putchar('\f');
5373 break;
5374 case 'n':
5375 bb_putchar('\n');
5376 G.prog.nchars = SIZE_MAX;
5377 break;
5378 case 'r':
5379 bb_putchar('\r');
5380 break;
5381 case 'q':
5382 bb_putchar('"');
5383 break;
5384 case 't':
5385 bb_putchar('\t');
5386 break;
5387 default:
5388 // Just print the backslash and following character.
5389 bb_putchar('\\');
5390 ++G.prog.nchars;
5391 bb_putchar(c);
5392 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005393 }
5394 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005395 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005396 }
5397}
5398
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005399static void bc_num_printNewline(void)
5400{
5401 if (G.prog.nchars == G.prog.len - 1) {
5402 bb_putchar('\\');
5403 bb_putchar('\n');
5404 G.prog.nchars = 0;
5405 }
5406}
5407
5408#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005409static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005410{
5411 (void) radix;
5412 bb_putchar((char) num);
5413 G.prog.nchars += width;
5414}
5415#endif
5416
5417static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5418{
5419 size_t exp, pow;
5420
5421 bc_num_printNewline();
5422 bb_putchar(radix ? '.' : ' ');
5423 ++G.prog.nchars;
5424
5425 bc_num_printNewline();
5426 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5427 continue;
5428
5429 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5430 size_t dig;
5431 bc_num_printNewline();
5432 dig = num / pow;
5433 num -= dig * pow;
5434 bb_putchar(((char) dig) + '0');
5435 }
5436}
5437
5438static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5439{
5440 if (radix) {
5441 bc_num_printNewline();
5442 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005443 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005444 }
5445
5446 bc_num_printNewline();
5447 bb_putchar(bb_hexdigits_upcase[num]);
5448 G.prog.nchars += width;
5449}
5450
5451static void bc_num_printDecimal(BcNum *n)
5452{
5453 size_t i, rdx = n->rdx - 1;
5454
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005455 if (n->neg) {
5456 bb_putchar('-');
5457 G.prog.nchars++;
5458 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005459
5460 for (i = n->len - 1; i < n->len; --i)
5461 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5462}
5463
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005464typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5465
Denys Vlasenkod3401432018-12-18 17:00:35 +01005466static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005467{
5468 BcStatus s;
5469 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005470 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005471 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005472 BcNum intp, fracp, digit, frac_len;
5473 unsigned long dig, *ptr;
5474 size_t i;
5475 bool radix;
5476
5477 if (n->len == 0) {
5478 print(0, width, false);
5479 RETURN_STATUS(BC_STATUS_SUCCESS);
5480 }
5481
5482 bc_vec_init(&stack, sizeof(long), NULL);
5483 bc_num_init(&intp, n->len);
5484 bc_num_init(&fracp, n->rdx);
5485 bc_num_init(&digit, width);
5486 bc_num_init(&frac_len, BC_NUM_INT(n));
5487 bc_num_copy(&intp, n);
5488 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005489 base.cap = ARRAY_SIZE(base_digs);
5490 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005491 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005492
5493 bc_num_truncate(&intp, intp.rdx);
5494 s = zbc_num_sub(n, &intp, &fracp, 0);
5495 if (s) goto err;
5496
5497 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005498 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005499 if (s) goto err;
5500 s = zbc_num_ulong(&digit, &dig);
5501 if (s) goto err;
5502 bc_vec_push(&stack, &dig);
5503 }
5504
5505 for (i = 0; i < stack.len; ++i) {
5506 ptr = bc_vec_item_rev(&stack, i);
5507 print(*ptr, width, false);
5508 }
5509
5510 if (!n->rdx) goto err;
5511
5512 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005513 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005514 if (s) goto err;
5515 s = zbc_num_ulong(&fracp, &dig);
5516 if (s) goto err;
5517 bc_num_ulong2num(&intp, dig);
5518 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5519 if (s) goto err;
5520 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005521 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005522 if (s) goto err;
5523 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005524 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005525 bc_num_free(&frac_len);
5526 bc_num_free(&digit);
5527 bc_num_free(&fracp);
5528 bc_num_free(&intp);
5529 bc_vec_free(&stack);
5530 RETURN_STATUS(s);
5531}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005532#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005533
5534static BC_STATUS zbc_num_printBase(BcNum *n)
5535{
5536 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005537 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005538 BcNumDigitOp print;
5539 bool neg = n->neg;
5540
5541 if (neg) {
5542 bb_putchar('-');
5543 G.prog.nchars++;
5544 }
5545
5546 n->neg = false;
5547
5548 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5549 width = 1;
5550 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005551 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005552 unsigned i = G.prog.ob_t - 1;
5553 width = 0;
5554 for (;;) {
5555 width++;
5556 i /= 10;
5557 if (i == 0)
5558 break;
5559 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005560 print = bc_num_printDigits;
5561 }
5562
Denys Vlasenkod3401432018-12-18 17:00:35 +01005563 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005564 n->neg = neg;
5565
5566 RETURN_STATUS(s);
5567}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005568#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005569
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005570static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5571{
5572 BcStatus s = BC_STATUS_SUCCESS;
5573
5574 bc_num_printNewline();
5575
5576 if (n->len == 0) {
5577 bb_putchar('0');
5578 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005579 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005580 bc_num_printDecimal(n);
5581 else
5582 s = zbc_num_printBase(n);
5583
5584 if (newline) {
5585 bb_putchar('\n');
5586 G.prog.nchars = 0;
5587 }
5588
5589 RETURN_STATUS(s);
5590}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005591#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005592
Denys Vlasenko29301232018-12-11 15:29:32 +01005593static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005594{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005595 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005596 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005597 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005598 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005599
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005600 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005601 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005602
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005603 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005604 s = zbc_program_num(r, &num, false);
5605 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005606
5607 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005608 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005609#if ENABLE_BC
5610 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5611#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005612 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005613 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005614
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005615 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005616 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005617
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005618 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005619 for (;;) {
5620 char c = *str++;
5621 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005622 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005623 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005624 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005625 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005626 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005627 bc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005628 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005629 }
5630 }
5631
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005632 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005633
Denys Vlasenko29301232018-12-11 15:29:32 +01005634 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005635}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005636#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005637
Denys Vlasenko29301232018-12-11 15:29:32 +01005638static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005639{
5640 BcStatus s;
5641 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005642 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005643
Denys Vlasenko29301232018-12-11 15:29:32 +01005644 s = zbc_program_prep(&ptr, &num);
5645 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005646
5647 bc_num_init(&res.d.n, num->len);
5648 bc_num_copy(&res.d.n, num);
5649 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5650
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005651 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005652
Denys Vlasenko29301232018-12-11 15:29:32 +01005653 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005654}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005655#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005656
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005657static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005658{
5659 BcStatus s;
5660 BcResult *opd1, *opd2, res;
5661 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005662 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005663
Denys Vlasenko29301232018-12-11 15:29:32 +01005664 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005665 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005666
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005667 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005668
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005669 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005670 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005671 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005672 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005673 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005674 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005675 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005676 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005677 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005678 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005679 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005680 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005681 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005682 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005683 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005684 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005685 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005686 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005687 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005688 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005689 cond = (cond > 0);
5690 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005691 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005692 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005693 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005694 }
5695 }
5696
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005697 if (cond) bc_num_one(&res.d.n);
5698 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005699
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005700 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005701
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005702 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005703}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005704#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005705
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005706#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005707static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005708{
5709 BcNum n2;
5710 BcResult res;
5711
5712 memset(&n2, 0, sizeof(BcNum));
5713 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005714 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005715
5716 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005717 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005718 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005719 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005720 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005721 }
5722
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005723 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005724 bc_vec_push(v, &n2);
5725
Denys Vlasenko29301232018-12-11 15:29:32 +01005726 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005727}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005728#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005729#endif // ENABLE_DC
5730
Denys Vlasenko29301232018-12-11 15:29:32 +01005731static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005732{
5733 BcStatus s;
5734 BcResult *ptr, r;
5735 BcVec *v;
5736 BcNum *n;
5737
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005738 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005739 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005740
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005741 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005742 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005743 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005744 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005745
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005746#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005747 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005748 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005749 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005750 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005751#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005752
Denys Vlasenko29301232018-12-11 15:29:32 +01005753 s = zbc_program_num(ptr, &n, false);
5754 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005755
5756 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005757 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005758
5759 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005760 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005761 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005762 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005763 bc_array_init(&r.d.v, true);
5764 bc_array_copy(&r.d.v, (BcVec *) n);
5765 }
5766
5767 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005768 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005769
Denys Vlasenko29301232018-12-11 15:29:32 +01005770 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005771}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005772#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005773
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005774static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005775{
5776 BcStatus s;
5777 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005778 BcNum *l, *r;
5779 bool assign = (inst == XC_INST_ASSIGN);
5780 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005781
Denys Vlasenko29301232018-12-11 15:29:32 +01005782 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005783 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005784
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005785 ib = left->t == XC_RESULT_IBASE;
5786 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005787
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005788#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005789 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005790 BcVec *v;
5791
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005792 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005793 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005794 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005795
Denys Vlasenkofa210792018-12-19 19:35:40 +01005796 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005797 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005798#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005799
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005800 if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005801 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005802 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005803 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005804 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005805
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005806#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005807 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005808 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005809
5810 if (assign)
5811 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005812 else {
5813 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005814 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 +01005815 }
5816 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005817#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005818 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005819#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005820
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005821 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005822 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005823 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5824 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5825 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005826 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005827 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005828 size_t max;
5829 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005830
Denys Vlasenko29301232018-12-11 15:29:32 +01005831 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005832 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005833 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005834 if (sc) {
5835 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005836 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005837 } else {
5838 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005839 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005840 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005841 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005842 }
5843
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005844 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005845 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005846
5847 *ptr = (size_t) val;
5848 s = BC_STATUS_SUCCESS;
5849 }
5850
5851 bc_num_init(&res.d.n, l->len);
5852 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005853 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005854
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005855 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005856}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005857#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005858
Denys Vlasenko416ce762018-12-02 20:57:17 +01005859#if !ENABLE_DC
5860#define bc_program_pushVar(code, bgn, pop, copy) \
5861 bc_program_pushVar(code, bgn)
5862// for bc, 'pop' and 'copy' are always false
5863#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005864static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005865 bool pop, bool copy)
5866{
Gavin Howard01055ba2018-11-03 11:00:21 -06005867 BcResult r;
5868 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005869
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005870 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005871 r.d.id.name = name;
5872
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005873#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005874 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005875 BcVec *v = bc_program_search(name, true);
5876 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005877
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005878 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005879 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005880 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005881 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005882
5883 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005884 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005885 bc_num_init_DEF_SIZE(&r.d.n);
5886 bc_num_copy(&r.d.n, num);
5887 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005888 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005889 r.d.id.idx = num->rdx;
5890 }
5891
5892 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005893 }
5894#endif // ENABLE_DC
5895
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005896 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005897
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005898 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005899}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005900#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005901
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005902static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005903{
5904 BcStatus s = BC_STATUS_SUCCESS;
5905 BcResult r;
5906 BcNum *num;
5907
5908 r.d.id.name = bc_program_name(code, bgn);
5909
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005910 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005911 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005912 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005913 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005914 BcResult *operand;
5915 unsigned long temp;
5916
Denys Vlasenko29301232018-12-11 15:29:32 +01005917 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005918 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005919 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005920 if (s) goto err;
5921
5922 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005923 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005924 goto err;
5925 }
5926
5927 r.d.id.idx = (size_t) temp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005928 bc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005929 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005930 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005931 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005932 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005933}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005934#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005935
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005936#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005937static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005938{
5939 BcStatus s;
5940 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005941 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005942 char inst2 = inst;
5943
Denys Vlasenko29301232018-12-11 15:29:32 +01005944 s = zbc_program_prep(&ptr, &num);
5945 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005946
5947 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005948 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005949 bc_num_init(&copy.d.n, num->len);
5950 bc_num_copy(&copy.d.n, num);
5951 }
5952
5953 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005954 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5955 ? BC_INST_ASSIGN_PLUS
5956 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005957
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005958 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005959 s = zbc_program_assign(inst);
5960 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005961
5962 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005963 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005964 }
5965
Denys Vlasenko29301232018-12-11 15:29:32 +01005966 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005967}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005968#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005969
Denys Vlasenko29301232018-12-11 15:29:32 +01005970static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005971{
Gavin Howard01055ba2018-11-03 11:00:21 -06005972 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005973 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005974 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005975 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005976 BcResult *arg;
5977
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005978 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005979 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005980 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005981 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005982
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005983 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005984 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005985 }
5986 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005987 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005988 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005989 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005990
5991 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005992 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005993
5994 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005995 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005996
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005997 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005998 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005999
Denys Vlasenko29301232018-12-11 15:29:32 +01006000 s = zbc_program_copyToVar(a->name, a->idx);
6001 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006002 }
6003
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006004 a = bc_vec_item(&func->autos, i);
6005 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006006 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006007
Denys Vlasenkodf515392018-12-02 19:27:48 +01006008 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006009 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006010 BcNum n2;
6011 bc_num_init_DEF_SIZE(&n2);
6012 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006013 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01006014 BcVec v2;
6015 bc_array_init(&v2, true);
6016 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006017 }
6018 }
6019
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006020 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006021
Denys Vlasenko29301232018-12-11 15:29:32 +01006022 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006023}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006024#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006025
Denys Vlasenko29301232018-12-11 15:29:32 +01006026static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006027{
Gavin Howard01055ba2018-11-03 11:00:21 -06006028 BcResult res;
6029 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006030 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006031 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006032 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006033
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006034 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 +01006035 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006036
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006037 f = bc_program_func(ip->func);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006038 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006039
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006040 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006041 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006042 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006043 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006044
Denys Vlasenko29301232018-12-11 15:29:32 +01006045 s = zbc_program_num(operand, &num, false);
6046 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006047 bc_num_init(&res.d.n, num->len);
6048 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006049 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006050 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006051 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006052 }
6053
6054 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006055 a = (void*)f->autos.v;
6056 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006057 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006058 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006059 bc_vec_pop(v);
6060 }
6061
Denys Vlasenko24e41942018-12-21 23:01:26 +01006062 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006063 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006064 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006065
Denys Vlasenko29301232018-12-11 15:29:32 +01006066 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006067}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006068#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006069#endif // ENABLE_BC
6070
6071static unsigned long bc_program_scale(BcNum *n)
6072{
6073 return (unsigned long) n->rdx;
6074}
6075
6076static unsigned long bc_program_len(BcNum *n)
6077{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006078 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006079
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006080 if (n->rdx != len) return len;
6081 for (;;) {
6082 if (len == 0) break;
6083 len--;
6084 if (n->num[len] != 0) break;
6085 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006086 return len;
6087}
6088
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006089static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006090{
6091 BcStatus s;
6092 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006093 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006094 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006095 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006096
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006097 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006098 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006099 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006100
Denys Vlasenko29301232018-12-11 15:29:32 +01006101 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006102 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006103
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006104#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006105 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006106 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006107#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006108
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006109 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006110
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006111 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006112 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006113#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006114 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006115 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006116 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006117#endif
6118#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006119 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006120 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006121 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006122
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006123 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006124 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006125 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006126#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006127 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006128 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006129 }
6130
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006131 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006132
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006133 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006134}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006135#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006136
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006137#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006138static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006139{
6140 BcStatus s;
6141 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006142 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006143
Denys Vlasenko29301232018-12-11 15:29:32 +01006144 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006145 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006146
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006147 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006148 bc_num_init(&res2.d.n, n2->len);
6149
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006150 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006151 if (s) goto err;
6152
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006153 bc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006154 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006155 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006156
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006157 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006158 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006159 bc_num_free(&res2.d.n);
6160 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006161 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006162}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006163#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006164
Denys Vlasenkofa210792018-12-19 19:35:40 +01006165static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006166{
6167 BcStatus s;
6168 BcResult *r1, *r2, *r3, res;
6169 BcNum *n1, *n2, *n3;
6170
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006171 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006172 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006173 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006174 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006175
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006176 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006177 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006178 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006179 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006180 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006181
6182 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006183 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006184 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006185 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006186 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006187 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006188 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006189 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006190 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006191 }
6192 }
6193
6194 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006195 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006196 if (s) goto err;
6197
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006198 bc_vec_pop(&G.prog.results);
6199 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006200
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006201 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006202 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006203 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006204 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006205}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006206#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006207
Denys Vlasenkofa210792018-12-19 19:35:40 +01006208static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006209{
Gavin Howard01055ba2018-11-03 11:00:21 -06006210 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006211 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006212
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006213 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006214
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006215 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006216 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006217 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006218}
6219
Denys Vlasenkofa210792018-12-19 19:35:40 +01006220static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006221{
6222 BcStatus s;
6223 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006224 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006225 char **strs;
6226 char *str;
6227 char c;
6228 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006229
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006230 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006231 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006232 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006233
Denys Vlasenko29301232018-12-11 15:29:32 +01006234 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006235 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006236
6237 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006238 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006239 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006240 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006241
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006242 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006243 bc_num_copy(&n, num);
6244 bc_num_truncate(&n, n.rdx);
6245
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006246 strmb.cap = ARRAY_SIZE(strmb_digs);
6247 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006248 bc_num_ulong2num(&strmb, 0x100);
6249 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006250
Gavin Howard01055ba2018-11-03 11:00:21 -06006251 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006252 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006253 if (s) goto num_err;
6254
6255 c = (char) val;
6256
6257 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006258 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006259 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006260 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006261 sp = *bc_program_str(idx);
6262 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006263 }
6264
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006265 strs = (void*)G.prog.strs.v;
6266 for (idx = 0; idx < G.prog.strs.len; idx++) {
6267 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6268 goto dup;
6269 }
6270 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006271 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006272 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006273 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006274 bc_vec_push(&G.prog.strs, &str);
6275 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006276 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006277 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006278 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006279
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006280 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006281 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006282 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006283 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006284}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006285#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006286
Denys Vlasenkofa210792018-12-19 19:35:40 +01006287static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006288{
6289 BcStatus s;
6290 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006291 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006292 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006293
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006294 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006295 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006296 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006297
Denys Vlasenko29301232018-12-11 15:29:32 +01006298 s = zbc_program_num(r, &n, false);
6299 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006300
Denys Vlasenko57734c92018-12-17 21:14:05 +01006301 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006302 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006303 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006304 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006305 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006306 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006307 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006308 }
6309
Denys Vlasenko29301232018-12-11 15:29:32 +01006310 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006311}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006312#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006313
Denys Vlasenkofa210792018-12-19 19:35:40 +01006314static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006315{
6316 BcStatus s;
6317 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006318 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006319 unsigned long val;
6320
Denys Vlasenko29301232018-12-11 15:29:32 +01006321 s = zbc_program_prep(&opnd, &num);
6322 if (s) RETURN_STATUS(s);
6323 s = zbc_num_ulong(num, &val);
6324 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006325
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006326 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006327
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006328 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006329 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006330 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006331 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006332 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006333
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006334 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006335
Denys Vlasenko29301232018-12-11 15:29:32 +01006336 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006337}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006338#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006339
Denys Vlasenkofa210792018-12-19 19:35:40 +01006340static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006341{
6342 BcStatus s = BC_STATUS_SUCCESS;
6343 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006344 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006345 BcInstPtr ip;
6346 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006347
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006348 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006349 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006350
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006351 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006352
6353 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006354 BcNum *n = n; // for compiler
6355 bool exec;
6356 char *name;
6357 char *then_name = bc_program_name(code, bgn);
6358 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006359
6360 if (code[*bgn] == BC_PARSE_STREND)
6361 (*bgn) += 1;
6362 else
6363 else_name = bc_program_name(code, bgn);
6364
6365 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006366 name = then_name;
6367 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006368 exec = true;
6369 name = else_name;
6370 }
6371
6372 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006373 BcVec *v;
6374 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006375 n = bc_vec_top(v);
6376 }
6377
6378 free(then_name);
6379 free(else_name);
6380
6381 if (!exec) goto exit;
6382 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006383 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006384 goto exit;
6385 }
6386
6387 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006388 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006389 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006390 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006391 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006392 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006393 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006394 if (s || !BC_PROG_STR(n)) goto exit;
6395 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006396 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006397 goto exit;
6398 }
6399
6400 fidx = sidx + BC_PROG_REQ_FUNCS;
6401
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006402 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006403
6404 if (f->code.len == 0) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006405 FILE *sv_input_fp;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006406 BcParse prs;
6407 char *str;
6408
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006409 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006410 str = *bc_program_str(sidx);
6411 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006412 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006413
6414 sv_input_fp = G.input_fp;
6415 G.input_fp = NULL; // "do not read from input file when <EOL> reached"
6416 s = zdc_parse_exprs_until_eof(&prs);
6417 G.input_fp = sv_input_fp;
6418
Gavin Howard01055ba2018-11-03 11:00:21 -06006419 if (s) goto err;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006420 if (prs.l.lex != XC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006421 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006422 err:
6423 bc_parse_free(&prs);
6424 bc_vec_pop_all(&f->code);
6425 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006426 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006427 bc_parse_push(&prs, DC_INST_POP_EXEC);
Gavin Howard01055ba2018-11-03 11:00:21 -06006428 bc_parse_free(&prs);
6429 }
6430
Denys Vlasenko24e41942018-12-21 23:01:26 +01006431 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006432 ip.func = fidx;
6433
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006434 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006435 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006436
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006437 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006438 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006439 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006440 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006441}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006442#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006443#endif // ENABLE_DC
6444
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006445static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006446{
Gavin Howard01055ba2018-11-03 11:00:21 -06006447 BcResult res;
6448 unsigned long val;
6449
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006450 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006451 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006452 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006453 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006454 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006455 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006456 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006457
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006458 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006459 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006460 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006461}
6462
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006463static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006464{
Gavin Howard01055ba2018-11-03 11:00:21 -06006465 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006466 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006467 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006468 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006469
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006470 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006471 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6472 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006473 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006474 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006475
Denys Vlasenko24e41942018-12-21 23:01:26 +01006476 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 -06006477 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006478#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006479 case BC_INST_JUMP_ZERO: {
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006480 BcNum *num;
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006481 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006482 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006483 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006484 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006485 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006486 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006487 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006488 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006489 break;
6490 }
6491 // else: fall through
6492 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006493 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006494 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006495 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006496 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006497 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006498 break;
6499 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006500 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006501 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006502 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006503 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006504 case BC_INST_INC_PRE:
6505 case BC_INST_DEC_PRE:
6506 case BC_INST_INC_POST:
6507 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006508 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006509 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006510 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006511 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006512 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006513 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006514 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006515 case XC_INST_RET:
Gavin Howard01055ba2018-11-03 11:00:21 -06006516 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006517 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006518 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006519 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006520 case XC_INST_BOOL_OR:
6521 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006522#endif // ENABLE_BC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006523 case XC_INST_REL_EQ:
6524 case XC_INST_REL_LE:
6525 case XC_INST_REL_GE:
6526 case XC_INST_REL_NE:
6527 case XC_INST_REL_LT:
6528 case XC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006529 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006530 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006531 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006532 case XC_INST_READ:
6533 dbg_exec("XC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006534 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006535 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006536 case XC_INST_VAR:
6537 dbg_exec("XC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006538 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006539 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006540 case XC_INST_ARRAY_ELEM:
6541 case XC_INST_ARRAY:
6542 dbg_exec("XC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006543 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006544 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006545#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006546 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006547 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006548 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006549 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006550 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006551#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006552 case XC_INST_IBASE:
6553 case XC_INST_OBASE:
6554 case XC_INST_SCALE:
6555 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006556 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006557 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006558 case XC_INST_SCALE_FUNC:
6559 case XC_INST_LENGTH:
6560 case XC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006561 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006562 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006563 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006564 case XC_INST_NUM:
6565 dbg_exec("XC_INST_NUM:");
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006566 r.t = XC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006567 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006568 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006569 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006570 case XC_INST_POP:
6571 dbg_exec("XC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006572 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006573 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006574 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006575 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006576 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006577 case XC_INST_PRINT:
6578 case XC_INST_PRINT_POP:
6579 case XC_INST_PRINT_STR:
6580 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006581 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006582 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006583 case XC_INST_STR:
6584 dbg_exec("XC_INST_STR:");
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006585 r.t = XC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006586 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006587 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006588 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006589 case XC_INST_POWER:
6590 case XC_INST_MULTIPLY:
6591 case XC_INST_DIVIDE:
6592 case XC_INST_MODULUS:
6593 case XC_INST_PLUS:
6594 case XC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006595 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006596 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006597 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006598 case XC_INST_BOOL_NOT: {
6599 BcNum *num;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006600 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006601 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006602 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006603 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006604 if (bc_num_cmp(num, &G.prog.zero) == 0)
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006605 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006606 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006607 bc_program_retire(&r, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006608 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006609 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006610 case XC_INST_NEG:
6611 dbg_exec("XC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006612 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006613 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006614#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006615 case BC_INST_ASSIGN_POWER:
6616 case BC_INST_ASSIGN_MULTIPLY:
6617 case BC_INST_ASSIGN_DIVIDE:
6618 case BC_INST_ASSIGN_MODULUS:
6619 case BC_INST_ASSIGN_PLUS:
6620 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006621#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006622 case XC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006623 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006624 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006625 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006626#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006627 case DC_INST_POP_EXEC:
6628 dbg_exec("DC_INST_POP_EXEC:");
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006629 bc_vec_pop(&G.prog.exestack);
6630 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006631 case DC_INST_MODEXP:
6632 dbg_exec("DC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006633 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006634 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006635 case DC_INST_DIVMOD:
6636 dbg_exec("DC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006637 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006638 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006639 case DC_INST_EXECUTE:
6640 case DC_INST_EXEC_COND:
6641 dbg_exec("DC_INST_EXEC[_COND]:");
6642 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006643 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006644 case DC_INST_PRINT_STACK: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006645 size_t idx;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006646 dbg_exec("DC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006647 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006648 s = zbc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006649 if (s) break;
6650 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006651 break;
6652 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006653 case DC_INST_CLEAR_STACK:
6654 dbg_exec("DC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006655 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006656 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006657 case DC_INST_STACK_LEN:
6658 dbg_exec("DC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006659 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006660 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006661 case DC_INST_DUPLICATE:
6662 dbg_exec("DC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006663 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006664 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006665 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006666 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006667 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006668 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006669 case DC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006670 BcResult *ptr2;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006671 dbg_exec("DC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006672 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006673 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006674 ptr = bc_vec_item_rev(&G.prog.results, 0);
6675 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006676 memcpy(&r, ptr, sizeof(BcResult));
6677 memcpy(ptr, ptr2, sizeof(BcResult));
6678 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006679 break;
6680 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006681 case DC_INST_ASCIIFY:
6682 dbg_exec("DC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006683 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006684 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006685 case DC_INST_PRINT_STREAM:
6686 dbg_exec("DC_INST_PRINT_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006687 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006688 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006689 case DC_INST_LOAD:
6690 case DC_INST_PUSH_VAR: {
6691 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006692 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006693 break;
6694 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006695 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006696 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006697 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006698 free(name);
6699 break;
6700 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006701 case DC_INST_QUIT:
6702 dbg_exec("DC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006703 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006704 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006705 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006706 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006707 case DC_INST_NQUIT:
6708 dbg_exec("DC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006709 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006710 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006711#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006712 read_updated_ip:
6713 // Instruction stack has changed, read new pointers
6714 ip = bc_vec_top(&G.prog.exestack);
6715 func = bc_program_func(ip->func);
6716 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006717 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006718 }
6719
Denys Vlasenkod38af482018-12-04 19:11:02 +01006720 if (s || G_interrupt) {
6721 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006722 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006723 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006724
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006725 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006726 }
6727
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006728 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006729}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006730#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006731
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006732static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006733{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006734 char *lenv;
6735 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006736
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006737 lenv = getenv(var);
6738 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006739 if (!lenv) return len;
6740
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006741 len = bb_strtou(lenv, NULL, 10) - 1;
6742 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006743 len = BC_NUM_PRINT_WIDTH;
6744
6745 return len;
6746}
6747
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006748static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006749{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006750 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006751
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006752 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006753 s = zbc_parse_text_init(&G.prs, text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006754 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006755
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006756 while (G.prs.l.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006757 BcInstPtr *ip;
6758 BcFunc *f;
6759
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006760 dbg_lex("%s:%d G.prs.l.lex:%d, parsing...", __func__, __LINE__, G.prs.l.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006761 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006762#if ENABLE_BC
6763 s = zbc_parse_stmt_or_funcdef(&G.prs);
6764 if (s) goto err;
6765
6766 // Check that next token is not bogus, and skip over
6767 // stmt delimiter(s) - newlines and semicolons
6768 s = 1; // s == 1 on first iteration only
6769 for (;;) {
6770 if (G.prs.l.lex == XC_LEX_EOF)
6771 goto execute; // this goto avoids resetting 's' to zero
6772 if (G.prs.l.lex != BC_LEX_SCOLON
6773 && G.prs.l.lex != XC_LEX_NLINE
6774 ) {
6775 const char *err_at;
6776 // Not newline and not semicolon
6777 if (s == 0) // saw at least one NL/semicolon before it?
6778 break; // yes, good
6779//TODO: commolalize for other parse errors:
6780 err_at = G.prs.l.lex_next_at ? G.prs.l.lex_next_at : "UNKNOWN";
6781 bc_error_fmt("bad statement terminator at '%.*s'",
6782 (int)(strchrnul(err_at, '\n') - err_at),
6783 err_at
6784 );
6785 goto err;
6786 }
6787 // NL or semicolon: skip it, set s = 0, repeat
6788 s = zbc_lex_next(&G.prs.l);
6789 if (s) goto err;
6790 }
6791#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006792 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006793#if ENABLE_DC
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006794 // Most of dc parsing assumes all whitespace,
6795 // including '\n', is eaten.
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006796 while (G.prs.l.lex == XC_LEX_NLINE) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006797 s = zbc_lex_next(&G.prs.l);
6798 if (s) goto err;
Denys Vlasenkoa17d95d2018-12-24 22:55:54 +01006799 if (G.prs.l.lex == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006800 goto done;
6801 }
Denys Vlasenko26384542018-12-25 18:37:52 +01006802 s = zdc_parse_expr(&G.prs);
6803#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006804 }
6805 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006806 err:
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006807 bc_parse_reset(&G.prs); // includes bc_program_reset()
6808 RETURN_STATUS(BC_STATUS_FAILURE);
6809 }
Denys Vlasenko26384542018-12-25 18:37:52 +01006810 IF_BC(execute:)
Denys Vlasenko39287e02018-12-22 02:23:08 +01006811 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006812 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006813 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006814 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006815 break;
6816 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006817
6818 ip = (void*)G.prog.exestack.v;
6819#if SANITY_CHECKS
6820 if (G.prog.exestack.len != 1) // should have only main's IP
6821 bb_error_msg_and_die("BUG:call stack");
6822 if (ip->func != BC_PROG_MAIN)
6823 bb_error_msg_and_die("BUG:not MAIN");
6824#endif
6825 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006826 // bc discards strings, constants and code after each
6827 // top-level statement in the "main program".
6828 // This prevents "yes 1 | bc" from growing its memory
6829 // without bound. This can be done because data stack
6830 // is empty and thus can't hold any references to
6831 // strings or constants, there is no generated code
6832 // which can hold references (after we discard one
6833 // we just executed). Code of functions can have references,
6834 // but bc stores function strings/constants in per-function
6835 // storage.
6836 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006837#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006838 if (G.prog.results.len != 0) // should be empty
6839 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006840#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006841 IF_BC(bc_vec_pop_all(&f->strs);)
6842 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006843 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006844 if (G.prog.results.len == 0
6845 && G.prog.vars.len == 0
6846 ) {
6847 // If stack is empty and no registers exist (TODO: or they are all empty),
6848 // we can get rid of accumulated strings and constants.
6849 // In this example dc process should not grow
6850 // its memory consumption with time:
6851 // yes 1pc | dc
6852 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6853 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6854 }
6855 // The code is discarded always (below), thus this example
6856 // should also not grow its memory consumption with time,
6857 // even though its data stack is not empty:
6858 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006859 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006860 // We drop generated and executed code for both bc and dc:
6861 bc_vec_pop_all(&f->code);
6862 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006863 }
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006864 done:
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006865 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006866 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006867}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006868#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006869
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006870static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006871{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006872 // So far bc/dc have no way to include a file from another file,
6873 // therefore we know G.prog.file == NULL on entry
6874 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006875 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006876
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006877 G.prog.file = filename;
6878 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006879 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006880
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006881 do {
6882 s = zbc_vm_process("");
6883 // We do not stop looping on errors here if reading stdin.
6884 // Example: start interactive bc and enter "return".
6885 // It should say "'return' not in a function"
6886 // but should not exit.
6887 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006888 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006889 RETURN_STATUS(s);
6890}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006891#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006892
6893static BC_STATUS zbc_vm_file(const char *file)
6894{
6895 BcStatus s;
6896 FILE *fp;
6897
6898 fp = xfopen_for_read(file);
6899 s = zbc_vm_execute_FILE(fp, file);
6900 fclose(fp);
6901
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006902 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006903}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006904#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006905
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006906#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006907static void bc_vm_info(void)
6908{
6909 printf("%s "BB_VER"\n"
6910 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6911 , applet_name);
6912}
6913
6914static void bc_args(char **argv)
6915{
6916 unsigned opts;
6917 int i;
6918
6919 GETOPT_RESET();
6920#if ENABLE_FEATURE_BC_LONG_OPTIONS
6921 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6922 "warn\0" No_argument "w"
6923 "version\0" No_argument "v"
6924 "standard\0" No_argument "s"
6925 "quiet\0" No_argument "q"
6926 "mathlib\0" No_argument "l"
6927 "interactive\0" No_argument "i"
6928 );
6929#else
6930 opts = option_mask32 |= getopt32(argv, "wvsqli");
6931#endif
6932 if (getenv("POSIXLY_CORRECT"))
6933 option_mask32 |= BC_FLAG_S;
6934
6935 if (opts & BC_FLAG_V) {
6936 bc_vm_info();
6937 exit(0);
6938 }
6939
6940 for (i = optind; argv[i]; ++i)
6941 bc_vec_push(&G.files, argv + i);
6942}
6943
6944static void bc_vm_envArgs(void)
6945{
6946 BcVec v;
6947 char *buf;
6948 char *env_args = getenv("BC_ENV_ARGS");
6949
6950 if (!env_args) return;
6951
6952 G.env_args = xstrdup(env_args);
6953 buf = G.env_args;
6954
6955 bc_vec_init(&v, sizeof(char *), NULL);
6956
6957 while (*(buf = skip_whitespace(buf)) != '\0') {
6958 bc_vec_push(&v, &buf);
6959 buf = skip_non_whitespace(buf);
6960 if (!*buf)
6961 break;
6962 *buf++ = '\0';
6963 }
6964
6965 // NULL terminate, and pass argv[] so that first arg is argv[1]
6966 if (sizeof(int) == sizeof(char*)) {
6967 bc_vec_push(&v, &const_int_0);
6968 } else {
6969 static char *const nullptr = NULL;
6970 bc_vec_push(&v, &nullptr);
6971 }
6972 bc_args(((char **)v.v) - 1);
6973
6974 bc_vec_free(&v);
6975}
6976
6977static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006978 "scale=20"
6979"\n" "define e(x){"
6980"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006981////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6982//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6983//e(-.998896): GNU:.36828580434569428695
6984// above code:.36828580434569428696
6985// actual value:.3682858043456942869594...
6986// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006987"\n" "b=ibase"
6988"\n" "ibase=A"
6989"\n" "if(x<0){"
6990"\n" "n=1"
6991"\n" "x=-x"
6992"\n" "}"
6993"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006994"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006995"\n" "scale=scale(x)+1"
6996"\n" "while(x>1){"
6997"\n" "d+=1"
6998"\n" "x/=2"
6999"\n" "scale+=1"
7000"\n" "}"
7001"\n" "scale=r"
7002"\n" "r=x+1"
7003"\n" "p=x"
7004"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007005"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007006"\n" "p*=x"
7007"\n" "f*=i"
7008"\n" "v=p/f"
7009"\n" "r+=v"
7010"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007011"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007012"\n" "scale=s"
7013"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007014"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007015"\n" "return(r/1)"
7016"\n" "}"
7017"\n" "define l(x){"
7018"\n" "auto b,s,r,p,a,q,i,v"
7019"\n" "b=ibase"
7020"\n" "ibase=A"
7021"\n" "if(x<=0){"
7022"\n" "r=(1-10^scale)/1"
7023"\n" "ibase=b"
7024"\n" "return(r)"
7025"\n" "}"
7026"\n" "s=scale"
7027"\n" "scale+=6"
7028"\n" "p=2"
7029"\n" "while(x>=2){"
7030"\n" "p*=2"
7031"\n" "x=sqrt(x)"
7032"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01007033"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007034"\n" "p*=2"
7035"\n" "x=sqrt(x)"
7036"\n" "}"
7037"\n" "r=a=(x-1)/(x+1)"
7038"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007039"\n" "v=1"
7040"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007041"\n" "a*=q"
7042"\n" "v=a/i"
7043"\n" "r+=v"
7044"\n" "}"
7045"\n" "r*=p"
7046"\n" "scale=s"
7047"\n" "ibase=b"
7048"\n" "return(r/1)"
7049"\n" "}"
7050"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007051"\n" "auto b,s,r,a,q,i"
7052"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007053"\n" "b=ibase"
7054"\n" "ibase=A"
7055"\n" "s=scale"
7056"\n" "scale=1.1*s+2"
7057"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007058"\n" "scale=0"
7059"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007060"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007061"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007062"\n" "scale=s+2"
7063"\n" "r=a=x"
7064"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007065"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007066"\n" "a*=q/(i*(i-1))"
7067"\n" "r+=a"
7068"\n" "}"
7069"\n" "scale=s"
7070"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007071"\n" "return(r/1)"
7072"\n" "}"
7073"\n" "define c(x){"
7074"\n" "auto b,s"
7075"\n" "b=ibase"
7076"\n" "ibase=A"
7077"\n" "s=scale"
7078"\n" "scale*=1.2"
7079"\n" "x=s(2*a(1)+x)"
7080"\n" "scale=s"
7081"\n" "ibase=b"
7082"\n" "return(x/1)"
7083"\n" "}"
7084"\n" "define a(x){"
7085"\n" "auto b,s,r,n,a,m,t,f,i,u"
7086"\n" "b=ibase"
7087"\n" "ibase=A"
7088"\n" "n=1"
7089"\n" "if(x<0){"
7090"\n" "n=-1"
7091"\n" "x=-x"
7092"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007093"\n" "if(scale<65){"
7094"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7095"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007096"\n" "}"
7097"\n" "s=scale"
7098"\n" "if(x>.2){"
7099"\n" "scale+=5"
7100"\n" "a=a(.2)"
7101"\n" "}"
7102"\n" "scale=s+3"
7103"\n" "while(x>.2){"
7104"\n" "m+=1"
7105"\n" "x=(x-.2)/(1+.2*x)"
7106"\n" "}"
7107"\n" "r=u=x"
7108"\n" "f=-x*x"
7109"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007110"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007111"\n" "u*=f"
7112"\n" "t=u/i"
7113"\n" "r+=t"
7114"\n" "}"
7115"\n" "scale=s"
7116"\n" "ibase=b"
7117"\n" "return((m*a+r)/n)"
7118"\n" "}"
7119"\n" "define j(n,x){"
7120"\n" "auto b,s,o,a,i,v,f"
7121"\n" "b=ibase"
7122"\n" "ibase=A"
7123"\n" "s=scale"
7124"\n" "scale=0"
7125"\n" "n/=1"
7126"\n" "if(n<0){"
7127"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007128"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007129"\n" "}"
7130"\n" "a=1"
7131"\n" "for(i=2;i<=n;++i)a*=i"
7132"\n" "scale=1.5*s"
7133"\n" "a=(x^n)/2^n/a"
7134"\n" "r=v=1"
7135"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007136"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007137"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007138"\n" "v=v*f/i/(n+i)"
7139"\n" "r+=v"
7140"\n" "}"
7141"\n" "scale=s"
7142"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007143"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007144"\n" "return(a*r/1)"
7145"\n" "}"
7146};
7147#endif // ENABLE_BC
7148
Denys Vlasenko19f11072018-12-12 22:48:19 +01007149static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007150{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007151 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007152 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007153 size_t i;
7154
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007155#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007156 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007157 // We know that internal library is not buggy,
7158 // thus error checking is normally disabled.
7159# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007160 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007161 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007162 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007163 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007164#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007165
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007166 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007167 fname = (void*)G.files.v;
7168 for (i = 0; i < G.files.len; i++) {
7169 s = zbc_vm_file(*fname++);
7170 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7171 // Debug config, non-interactive mode:
7172 // return all the way back to main.
7173 // Non-debug builds do not come here
7174 // in non-interactive mode, they exit.
7175 RETURN_STATUS(s);
7176 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007177 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007178
Denys Vlasenko91cde952018-12-10 20:56:08 +01007179 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007180 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007181
Denys Vlasenko19f11072018-12-12 22:48:19 +01007182 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007183}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007184#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007185
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007186#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007187static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007188{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007189 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007190 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007191 bc_vec_free(&G.prog.vars);
7192 bc_vec_free(&G.prog.var_map);
7193 bc_vec_free(&G.prog.arrs);
7194 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007195 IF_DC(bc_vec_free(&G.prog.strs);)
7196 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007197 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007198 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007199 IF_BC(bc_num_free(&G.prog.last);)
7200 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007201 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007202 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007203}
7204
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007205static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007206{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007207 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007208 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007209 bc_parse_free(&G.prs);
7210 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007211}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007212#endif
7213
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007214static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007215{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007216 BcInstPtr ip;
7217
Denys Vlasenko82269122018-12-14 16:30:56 +01007218 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007219 memset(&ip, 0, sizeof(BcInstPtr));
7220
Denys Vlasenko82269122018-12-14 16:30:56 +01007221 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007222 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007223 G.prog.ob_t = 10;
7224
Denys Vlasenko503faf92018-12-20 16:24:18 +01007225 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7226 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007227
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007228 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007229 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007230
Denys Vlasenko503faf92018-12-20 16:24:18 +01007231 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7232 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007233
7234 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007235 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007236
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007237 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007238 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007239 // a valid function name (and be short)
7240 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007241 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007242 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007243 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007244 bc_program_add_fn();
7245 bc_program_add_fn();
7246 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007247
7248 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007249 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007250
7251 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007252 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007253
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007254 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7255 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007256 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007257 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7258 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007259
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007260 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007261}
Gavin Howard01055ba2018-11-03 11:00:21 -06007262
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007263static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007264{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007265#if ENABLE_FEATURE_EDITING
7266 G.line_input_state = new_line_input_t(DO_HISTORY);
7267#endif
7268 G.prog.len = bc_vm_envLen(env_len);
7269
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007270 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007271 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007272 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007273 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007274
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007275//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7276//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007277 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007278#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007279 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007280 // With SA_RESTART, most system calls will restart
7281 // (IOW: they won't fail with EINTR).
7282 // In particular, this means ^C won't cause
7283 // stdout to get into "error state" if SIGINT hits
7284 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007285 //
7286 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007287 // will only be handled after [Enter] since read()
7288 // from stdin is not interrupted by ^C either,
7289 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007290 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007291 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7292
7293 // Without SA_RESTART, this exhibits a bug:
7294 // "while (1) print 1" and try ^C-ing it.
7295 // Intermittently, instead of returning to input line,
7296 // you'll get "output error: Interrupted system call"
7297 // and exit.
7298 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007299#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007300 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007301 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007302 return 0; // "not a tty"
7303}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007304
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007305static BcStatus bc_vm_run(void)
7306{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007307 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007308#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007309 if (G_exiting) // it was actually "halt" or "quit"
7310 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007311 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007312# if ENABLE_FEATURE_EDITING
7313 free_line_input_t(G.line_input_state);
7314# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007315 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007316#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007317 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007318 return st;
7319}
7320
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007321#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007322int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007323int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007324{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007325 int is_tty;
7326
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007327 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007328
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007329 is_tty = bc_vm_init("BC_LINE_LENGTH");
7330
7331 bc_args(argv);
7332
7333 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7334 bc_vm_info();
7335
7336 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007337}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007338#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007339
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007340#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007341int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007342int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007343{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007344 int noscript;
7345
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007346 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007347
7348 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7349 // 1 char wider than bc from the same package.
7350 // Both default width, and xC_LINE_LENGTH=N are wider:
7351 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007352 // |1234\ |
7353 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007354 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007355 // |123\ |
7356 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007357 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007358 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007359
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007360 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7361 noscript = BC_FLAG_I;
7362 for (;;) {
7363 int n = getopt(argc, argv, "e:f:x");
7364 if (n <= 0)
7365 break;
7366 switch (n) {
7367 case 'e':
7368 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007369 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007370 if (n) return n;
7371 break;
7372 case 'f':
7373 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007374 n = zbc_vm_file(optarg);
7375 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007376 break;
7377 case 'x':
7378 option_mask32 |= DC_FLAG_X;
7379 break;
7380 default:
7381 bb_show_usage();
7382 }
7383 }
7384 argv += optind;
7385
7386 while (*argv) {
7387 noscript = 0;
7388 bc_vec_push(&G.files, argv++);
7389 }
7390
7391 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7392
7393 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007394}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007395#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007396
7397#endif // not DC_SMALL