blob: 12bc80b3ee1c0122970338dd9f7da44f2f559041 [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 * Copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06005 */
6//config:config BC
7//config: bool "bc (45 kb; 49 kb when combined with dc)"
8//config: default y
9//config: help
10//config: bc is a command-line, arbitrary-precision calculator with a
11//config: Turing-complete language. See the GNU bc manual
12//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010013//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060014//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010015//config: This bc has five differences to the GNU bc:
16//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060017//config: 2) Arrays are copied before being passed as arguments to
18//config: functions. This behavior is required by the bc spec.
19//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010020//config: the number of elements in the array. This prints "1":
21//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060022//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010023//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060024//config: allows POSIX-compliant scripts to work while somewhat
25//config: preserving expected behavior (versus C) and making parsing
26//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010027//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060028//config:
29//config: Options:
Gavin Howard01055ba2018-11-03 11:00:21 -060030//config: -i --interactive force interactive mode
Denys Vlasenko89e785a2018-12-13 16:35:52 +010031//config: -q --quiet don't print version and copyright
32//config: -s --standard error if any non-POSIX extensions are used
33//config: -w --warn warn if any non-POSIX extensions are used
Gavin Howard01055ba2018-11-03 11:00:21 -060034//config: -l --mathlib use predefined math routines:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010035//config: s(expr) sine in radians
36//config: c(expr) cosine in radians
37//config: a(expr) arctangent, returning radians
38//config: l(expr) natural log
39//config: e(expr) raises e to the power of expr
40//config: j(n, x) Bessel function of integer order n of x
Gavin Howard01055ba2018-11-03 11:00:21 -060041//config:
42//config:config DC
43//config: bool "dc (38 kb; 49 kb when combined with bc)"
44//config: default y
45//config: help
46//config: dc is a reverse-polish notation command-line calculator which
47//config: supports unlimited precision arithmetic. See the FreeBSD man page
48//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010049//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060050//config:
51//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060053//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010054//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060055//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010056//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060057//config: 4) Like the FreeBSD dc, this dc supports extended registers.
58//config: However, they are implemented differently. When it encounters
59//config: whitespace where a register should be, it skips the whitespace.
60//config: If the character following is not a lowercase letter, an error
61//config: is issued. Otherwise, the register name is parsed by the
62//config: following regex:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010063//config: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060064//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010065//config: whitespace. Examples:
66//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060067//config: Also note that, like the FreeBSD dc, extended registers are not
68//config: allowed unless the "-x" option is given.
69//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010070//config:config FEATURE_DC_SMALL
71//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
72//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010073//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010074//config:
75//config:config FEATURE_DC_LIBM
76//config: bool "Enable power and exp functions (requires libm)"
77//config: default y
78//config: depends on FEATURE_DC_SMALL
79//config: help
80//config: Enable power and exp functions.
81//config: NOTE: This will require libm to be present for linking.
82//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060083//config:config FEATURE_BC_SIGNALS
Denys Vlasenko89e785a2018-12-13 16:35:52 +010084//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060085//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010086//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -060087//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010088//config: Enable interactive mode: when started on a tty,
89//config: ^C interrupts execution and returns to command line,
90//config: errors also return to command line instead of exiting,
91//config: line editing with history is available.
92//config:
93//config: With this option off, input can still be taken from tty,
94//config: but all errors are fatal, ^C is fatal,
95//config: tty is treated exactly the same as any other
96//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060097//config:
98//config:config FEATURE_BC_LONG_OPTIONS
99//config: bool "Enable bc/dc long options"
100//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100101//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600102//config: help
103//config: Enable long options for bc and dc.
104
105//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
106//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
107
108//kbuild:lib-$(CONFIG_BC) += bc.o
109//kbuild:lib-$(CONFIG_DC) += bc.o
110
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100111//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600112//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100113//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600114//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100115//usage:#define bc_full_usage "\n"
116//usage: "\nArbitrary precision calculator"
117//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100118///////: "\n -i Interactive" - has no effect for now
119//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100120//usage: "\n -l Load standard math library"
121//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100122//usage: "\n -w Warn if extensions are used"
123///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100124//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100125//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600126//usage:
127//usage:#define bc_example_usage
128//usage: "3 + 4.129\n"
129//usage: "1903 - 2893\n"
130//usage: "-129 * 213.28935\n"
131//usage: "12 / -1932\n"
132//usage: "12 % 12\n"
133//usage: "34 ^ 189\n"
134//usage: "scale = 13\n"
135//usage: "ibase = 2\n"
136//usage: "obase = A\n"
137//usage:
138//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100139//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600140//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100141//usage:#define dc_full_usage "\n"
142//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100143//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100144//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100145//usage: "\nf - print entire stack"
146//usage: "\nk - pop the value and set the precision"
147//usage: "\ni - pop the value and set input radix"
148//usage: "\no - pop the value and set output radix"
149//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600150//usage:
151//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100152//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600153//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100154//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600155//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100156//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600157//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100158//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600159//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600161//usage: "64\n"
162
163#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100164#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600165
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100166#if ENABLE_FEATURE_DC_SMALL
167# include "dc.c"
168#else
169
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100170#define DEBUG_LEXER 0
171#define DEBUG_COMPILE 0
172#define DEBUG_EXEC 0
Denys Vlasenko19eee8e2018-12-21 20:29:34 +0100173// This can be left enabled for production as well:
174#define SANITY_CHECKS 1
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100175
176#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100177static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100178#define dbg_lex(...) \
179 do { \
180 fprintf(stderr, "%*s", lex_indent, ""); \
181 bb_error_msg(__VA_ARGS__); \
182 } while (0)
183#define dbg_lex_enter(...) \
184 do { \
185 dbg_lex(__VA_ARGS__); \
186 lex_indent++; \
187 } while (0)
188#define dbg_lex_done(...) \
189 do { \
190 lex_indent--; \
191 dbg_lex(__VA_ARGS__); \
192 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100193#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100194# define dbg_lex(...) ((void)0)
195# define dbg_lex_enter(...) ((void)0)
196# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100197#endif
198
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100199#if DEBUG_COMPILE
200# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
201#else
202# define dbg_compile(...) ((void)0)
203#endif
204
Denys Vlasenko99b37622018-12-15 20:06:59 +0100205#if DEBUG_EXEC
206# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
207#else
208# define dbg_exec(...) ((void)0)
209#endif
210
Gavin Howard01055ba2018-11-03 11:00:21 -0600211typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100212 BC_STATUS_SUCCESS = 0,
213 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100214 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600215} BcStatus;
216
Gavin Howard01055ba2018-11-03 11:00:21 -0600217#define BC_VEC_INVALID_IDX ((size_t) -1)
218#define BC_VEC_START_CAP (1 << 5)
219
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100220typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600221
222typedef struct BcVec {
223 char *v;
224 size_t len;
225 size_t cap;
226 size_t size;
227 BcVecFree dtor;
228} BcVec;
229
Gavin Howard01055ba2018-11-03 11:00:21 -0600230typedef signed char BcDig;
231
232typedef struct BcNum {
233 BcDig *restrict num;
234 size_t rdx;
235 size_t len;
236 size_t cap;
237 bool neg;
238} BcNum;
239
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100240#define BC_NUM_MAX_IBASE ((unsigned long) 16)
241// larger value might speed up BIGNUM calculations a bit:
242#define BC_NUM_DEF_SIZE (16)
243#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600244
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100245#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600246
Gavin Howard01055ba2018-11-03 11:00:21 -0600247typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100248#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600249 BC_INST_INC_PRE,
250 BC_INST_DEC_PRE,
251 BC_INST_INC_POST,
252 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100253#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100254 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600255
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100256 XC_INST_REL_EQ, // should
257 XC_INST_REL_LE, // match
258 XC_INST_REL_GE, // LEX
259 XC_INST_REL_NE, // constants
260 XC_INST_REL_LT, // for
261 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600262
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100263 XC_INST_POWER, // operations
264 XC_INST_MULTIPLY, // |
265 XC_INST_DIVIDE, // |
266 XC_INST_MODULUS, // |
267 XC_INST_PLUS, // |
268 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600269
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100270 XC_INST_BOOL_NOT, // |
271 XC_INST_BOOL_OR, // |
272 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100273#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100274 BC_INST_ASSIGN_POWER, // |
275 BC_INST_ASSIGN_MULTIPLY,// |
276 BC_INST_ASSIGN_DIVIDE, // |
277 BC_INST_ASSIGN_MODULUS, // |
278 BC_INST_ASSIGN_PLUS, // |
279 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100280#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100281 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600282
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100283 XC_INST_NUM,
284 XC_INST_VAR,
285 XC_INST_ARRAY_ELEM,
286 XC_INST_ARRAY,
287 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100288
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100289 XC_INST_IBASE, // order of these constans should match other enums
290 XC_INST_OBASE, // order of these constans should match other enums
291 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100292 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100293 XC_INST_LENGTH,
294 XC_INST_READ,
295 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600296
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100297 XC_INST_PRINT,
298 XC_INST_PRINT_POP,
299 XC_INST_STR,
300 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600301
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100302#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100303 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600304 BC_INST_JUMP,
305 BC_INST_JUMP_ZERO,
306
307 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600308 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100309#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100310 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600311
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100312 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100313#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100314 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600315
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100316 DC_INST_MODEXP,
317 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600318
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100319 DC_INST_EXECUTE,
320 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600321
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100322 DC_INST_ASCIIFY,
323 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600324
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100325 DC_INST_PRINT_STACK,
326 DC_INST_CLEAR_STACK,
327 DC_INST_STACK_LEN,
328 DC_INST_DUPLICATE,
329 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600330
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100331 DC_INST_LOAD,
332 DC_INST_PUSH_VAR,
333 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600334
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100335 DC_INST_QUIT,
336 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600337
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100338 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100339#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600340} BcInst;
341
342typedef struct BcId {
343 char *name;
344 size_t idx;
345} BcId;
346
347typedef struct BcFunc {
348 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100349 IF_BC(BcVec labels;)
350 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100351 IF_BC(BcVec strs;)
352 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100353 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600354} BcFunc;
355
356typedef enum BcResultType {
Gavin Howard01055ba2018-11-03 11:00:21 -0600357 BC_RESULT_TEMP,
358
359 BC_RESULT_VAR,
360 BC_RESULT_ARRAY_ELEM,
361 BC_RESULT_ARRAY,
362
363 BC_RESULT_STR,
364
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100365 //code uses "inst - XC_INST_IBASE + BC_RESULT_IBASE" construct,
366 BC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
367 BC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
368 BC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100369 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600370 BC_RESULT_CONSTANT,
371 BC_RESULT_ONE,
Gavin Howard01055ba2018-11-03 11:00:21 -0600372} BcResultType;
373
374typedef union BcResultData {
375 BcNum n;
376 BcVec v;
377 BcId id;
378} BcResultData;
379
380typedef struct BcResult {
381 BcResultType t;
382 BcResultData d;
383} BcResult;
384
385typedef struct BcInstPtr {
386 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100387 size_t inst_idx;
388 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600389} BcInstPtr;
390
Gavin Howard01055ba2018-11-03 11:00:21 -0600391typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100392 XC_LEX_EOF,
393 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600394
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100395 XC_LEX_NLINE,
396 XC_LEX_WHITESPACE,
397 XC_LEX_STR,
398 XC_LEX_NAME,
399 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100400
Denys Vlasenko69560f42018-12-24 14:14:23 +0100401 XC_LEX_1st_op,
402 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100403
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100404 XC_LEX_OP_REL_EQ, // should
405 XC_LEX_OP_REL_LE, // match
406 XC_LEX_OP_REL_GE, // INST
407 XC_LEX_OP_REL_NE, // constants
408 XC_LEX_OP_REL_LT, // for
409 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100410
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100411 XC_LEX_OP_POWER, // operations
412 XC_LEX_OP_MULTIPLY, // |
413 XC_LEX_OP_DIVIDE, // |
414 XC_LEX_OP_MODULUS, // |
415 XC_LEX_OP_PLUS, // |
416 XC_LEX_OP_MINUS, // |
417 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100418#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100419 BC_LEX_OP_BOOL_NOT, // |
420 BC_LEX_OP_BOOL_OR, // |
421 BC_LEX_OP_BOOL_AND, // |
422
423 BC_LEX_OP_ASSIGN_POWER, // |
424 BC_LEX_OP_ASSIGN_MULTIPLY, // |
425 BC_LEX_OP_ASSIGN_DIVIDE, // |
426 BC_LEX_OP_ASSIGN_MODULUS, // |
427 BC_LEX_OP_ASSIGN_PLUS, // |
428 BC_LEX_OP_ASSIGN_MINUS, // |
429
430 BC_LEX_OP_ASSIGN, // V
431
Gavin Howard01055ba2018-11-03 11:00:21 -0600432 BC_LEX_OP_INC,
433 BC_LEX_OP_DEC,
434
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100435 BC_LEX_LPAREN, // () are 0x28 and 0x29
436 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600437
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100438 BC_LEX_LBRACKET, // [] are 0x5B and 5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600439 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100440 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600441
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100442 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600443 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100444 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600445
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100446 BC_LEX_KEY_1st_keyword,
447 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600448 BC_LEX_KEY_BREAK,
449 BC_LEX_KEY_CONTINUE,
450 BC_LEX_KEY_DEFINE,
451 BC_LEX_KEY_ELSE,
452 BC_LEX_KEY_FOR,
453 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100454 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
455 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
456 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600457 BC_LEX_KEY_IF,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100458 IF_BC(BC_LEX_KEY_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600459 BC_LEX_KEY_LENGTH,
460 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600461 BC_LEX_KEY_PRINT,
462 BC_LEX_KEY_QUIT,
463 BC_LEX_KEY_READ,
464 BC_LEX_KEY_RETURN,
465 BC_LEX_KEY_SCALE,
466 BC_LEX_KEY_SQRT,
467 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100468#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600469
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100470#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100471 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
472 DC_LEX_OP_ASSIGN,
473
474 DC_LEX_LPAREN,
475 DC_LEX_SCOLON,
476 DC_LEX_READ,
477 DC_LEX_IBASE,
478 DC_LEX_SCALE,
479 DC_LEX_OBASE,
480 DC_LEX_LENGTH,
481 DC_LEX_PRINT,
482 DC_LEX_QUIT,
483 DC_LEX_SQRT,
484 DC_LEX_LBRACE,
485
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100486 DC_LEX_EQ_NO_REG,
487 DC_LEX_OP_MODEXP,
488 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600489
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100490 DC_LEX_COLON,
491 DC_LEX_ELSE,
492 DC_LEX_EXECUTE,
493 DC_LEX_PRINT_STACK,
494 DC_LEX_CLEAR_STACK,
495 DC_LEX_STACK_LEVEL,
496 DC_LEX_DUPLICATE,
497 DC_LEX_SWAP,
498 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600499
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100500 DC_LEX_ASCIIFY,
501 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600502
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100503 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
504 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
505 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
506 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
507 DC_LEX_LOAD,
508 DC_LEX_LOAD_POP,
509 DC_LEX_STORE_PUSH,
510 DC_LEX_PRINT_POP,
511 DC_LEX_NQUIT,
512 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100513#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600514} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100515// must match order of BC_LEX_KEY_foo etc above
516#if ENABLE_BC
517struct BcLexKeyword {
518 char name8[8];
519};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100520#define BC_LEX_KW_ENTRY(a, b) \
521 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100522static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100523 BC_LEX_KW_ENTRY("auto" , 1), // 0
524 BC_LEX_KW_ENTRY("break" , 1), // 1
525 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
526 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100527 BC_LEX_KW_ENTRY("else" , 0), // 4
528 BC_LEX_KW_ENTRY("for" , 1), // 5
529 BC_LEX_KW_ENTRY("halt" , 0), // 6
530 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100531 BC_LEX_KW_ENTRY("obase" , 1), // 8
532 BC_LEX_KW_ENTRY("if" , 1), // 9
533 BC_LEX_KW_ENTRY("last" , 0), // 10
534 BC_LEX_KW_ENTRY("length" , 1), // 11
535 BC_LEX_KW_ENTRY("limits" , 0), // 12
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100536 BC_LEX_KW_ENTRY("print" , 0), // 13
537 BC_LEX_KW_ENTRY("quit" , 1), // 14
538 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100539 BC_LEX_KW_ENTRY("return" , 1), // 16
540 BC_LEX_KW_ENTRY("scale" , 1), // 17
541 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
542 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100543};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100544#undef BC_LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100545#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100546#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100547#define STRING_if (bc_lex_kws[9].name8)
548#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100549enum {
550 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100551 | (1 << 0) // 0
552 | (1 << 1) // 1
553 | (0 << 2) // 2
554 | (1 << 3) // 3
555 | (0 << 4) // 4
556 | (1 << 5) // 5
557 | (0 << 6) // 6
558 | (1 << 7) // 7
559 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100560 | (1 << 9) // 9
561 | (0 << 10) // 10
562 | (1 << 11) // 11
563 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100564 | (0 << 13) // 13
565 | (1 << 14) // 14
566 | (0 << 15) // 15
567 | (1 << 16) // 16
568 | (1 << 17) // 17
569 | (1 << 18) // 18
570 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100571};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100572#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100573
574// This is a bit array that corresponds to token types. An entry is
575// true if the token is valid in an expression, false otherwise.
576// Used to figure out when expr parsing should stop *without error message*
577// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100578// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100579// by later processing.
580enum {
581#define EXBITS(a,b,c,d,e,f,g,h) \
582 ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7)))
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100583 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100584 + (EXBITS(0,0,0,0,0,1,1,1) << (0*8)) // 0: EOF INVAL NL WS STR NAME NUM -
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100585 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
586 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100587 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
588 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100589 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
590 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
591 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100592#undef EXBITS
593};
594static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
595{
596#if ULONG_MAX > 0xffffffff
597 // 64-bit version (will not work correctly for 32-bit longs!)
598 return BC_PARSE_EXPRS_BITS & (1UL << i);
599#else
600 // 32-bit version
601 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
602 if (i >= 32) {
603 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
604 i &= 31;
605 }
606 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100607#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100608}
609
610// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100611// [XC_LEX_1st_op...] token types.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100612static const uint8_t bc_parse_ops[] = {
613#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100614 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100615 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100616 OP(2, false), // pow
617 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
618 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100619 OP(1, false), // not
620 OP(7, true ), OP( 7, true ), // or and
621 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
622 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100623 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100624#undef OP
625};
626#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
627#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
628#endif // ENABLE_BC
629
630#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100631static const //BcLexType - should be this type
632uint8_t
633dc_char_to_LEX[] = {
634 /* %&'( */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100635 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100636 /* )*+, */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100637 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100638 /* -./ */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100639 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100640 /* 0123456789 */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100641 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
642 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
643 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100644 /* :;<=>?@ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100645 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
646 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100647 /* ABCDEFGH */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100648 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
649 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100650 /* IJKLMNOP */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100651 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
652 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100653 /* QRSTUVWXY */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100654 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
655 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100656 /* Z[\] */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100657 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100658 /* ^_` */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100659 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100660 /* abcdefgh */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100661 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
662 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100663 /* ijklmnop */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100664 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100665 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100666 /* qrstuvwx */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100667 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
668 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100669 /* yz */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100670 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100671 /* {|}~ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100672 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100673};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100674static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100675int8_t
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100676dc_LEX_to_INST[] = { // starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
677 XC_INST_POWER, XC_INST_MULTIPLY, // OP_POWER OP_MULTIPLY
678 XC_INST_DIVIDE, XC_INST_MODULUS, // OP_DIVIDE OP_MODULUS
679 XC_INST_PLUS, XC_INST_MINUS, // OP_PLUS OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100680 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
681 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
682 XC_INST_REL_GT, // DC_LEX_LPAREN
683 DC_INST_INVALID, // DC_LEX_SCOLON
684 DC_INST_INVALID, // DC_LEX_READ
685 XC_INST_IBASE, // DC_LEX_IBASE
686 XC_INST_SCALE, // DC_LEX_SCALE
687 XC_INST_OBASE, // DC_LEX_OBASE
688 XC_INST_LENGTH, // DC_LEX_LENGTH
689 XC_INST_PRINT, // DC_LEX_PRINT
690 DC_INST_QUIT, // DC_LEX_QUIT
691 XC_INST_SQRT, // DC_LEX_SQRT
692 XC_INST_REL_GE, // DC_LEX_LBRACE
693 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
694 DC_INST_MODEXP, DC_INST_DIVMOD, // OP_MODEXP OP_DIVMOD
695 DC_INST_INVALID, DC_INST_INVALID, // COLON ELSE
696 DC_INST_EXECUTE, // EXECUTE
697 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // PRINT_STACK CLEAR_STACK
698 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // STACK_LEVEL DUPLICATE
699 DC_INST_SWAP, XC_INST_POP, // SWAP POP
700 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // ASCIIFY PRINT_STREAM
701 DC_INST_INVALID, DC_INST_INVALID, // STORE_IBASE STORE_OBASE
702 DC_INST_INVALID, DC_INST_INVALID, // STORE_SCALE LOAD
703 DC_INST_INVALID, DC_INST_INVALID, // LOAD_POP STORE_PUSH
704 XC_INST_PRINT, DC_INST_NQUIT, // PRINT_POP NQUIT
705 XC_INST_SCALE_FUNC, // SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100706 // DC_INST_INVALID in this table either means that corresponding LEX
707 // is not possible for dc, or that it does not compile one-to-one
708 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100709};
710#endif // ENABLE_DC
711
Gavin Howard01055ba2018-11-03 11:00:21 -0600712typedef struct BcLex {
Gavin Howard01055ba2018-11-03 11:00:21 -0600713 const char *buf;
714 size_t i;
715 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600716 size_t len;
717 bool newline;
Gavin Howard01055ba2018-11-03 11:00:21 -0600718 struct {
719 BcLexType t;
720 BcLexType last;
721 BcVec v;
722 } t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600723} BcLex;
724
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100725#define BC_PARSE_STREND (0xff)
Gavin Howard01055ba2018-11-03 11:00:21 -0600726
Denys Vlasenko39287e02018-12-22 02:23:08 +0100727#if ENABLE_BC
728# define BC_PARSE_REL (1 << 0)
729# define BC_PARSE_PRINT (1 << 1)
730# define BC_PARSE_ARRAY (1 << 2)
731# define BC_PARSE_NOCALL (1 << 3)
732#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600733
Gavin Howard01055ba2018-11-03 11:00:21 -0600734typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600735 BcLex l;
736
Denys Vlasenko503faf92018-12-20 16:24:18 +0100737 IF_BC(BcVec exits;)
738 IF_BC(BcVec conds;)
739 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600740
Gavin Howard01055ba2018-11-03 11:00:21 -0600741 BcFunc *func;
742 size_t fidx;
743
Denys Vlasenko503faf92018-12-20 16:24:18 +0100744 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600745} BcParse;
746
Gavin Howard01055ba2018-11-03 11:00:21 -0600747typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600748 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100749 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600750
Denys Vlasenko503faf92018-12-20 16:24:18 +0100751 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600752 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600753 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600754
Gavin Howard01055ba2018-11-03 11:00:21 -0600755 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100756 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600757
758 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100759 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600760
761 BcVec vars;
762 BcVec var_map;
763
764 BcVec arrs;
765 BcVec arr_map;
766
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100767 IF_DC(BcVec strs;)
768 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600769
770 const char *file;
771
Gavin Howard01055ba2018-11-03 11:00:21 -0600772 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100773 IF_BC(BcNum one;)
774 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600775} BcProgram;
776
Gavin Howard01055ba2018-11-03 11:00:21 -0600777#define BC_PROG_MAIN (0)
778#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100779#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600780#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100781#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600782
783#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
784#define BC_PROG_NUM(r, n) \
785 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
786
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100787#define BC_FLAG_W (1 << 0)
788#define BC_FLAG_V (1 << 1)
789#define BC_FLAG_S (1 << 2)
790#define BC_FLAG_Q (1 << 3)
791#define BC_FLAG_L (1 << 4)
792#define BC_FLAG_I (1 << 5)
793#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600794
795#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
796#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
797
Denys Vlasenko64074a12018-12-07 15:50:14 +0100798#define BC_MAX_OBASE ((unsigned) 999)
799#define BC_MAX_DIM ((unsigned) INT_MAX)
800#define BC_MAX_SCALE ((unsigned) UINT_MAX)
801#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
802#define BC_MAX_NUM BC_MAX_STRING
803// Unused apart from "limits" message. Just show a "biggish number" there.
804//#define BC_MAX_NAME BC_MAX_STRING
805//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
806//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
807#define BC_MAX_NAME_STR "999999999"
808#define BC_MAX_EXP_STR "999999999"
809#define BC_MAX_VARS_STR "999999999"
810
811#define BC_MAX_OBASE_STR "999"
812
813#if INT_MAX == 2147483647
814# define BC_MAX_DIM_STR "2147483647"
815#elif INT_MAX == 9223372036854775807
816# define BC_MAX_DIM_STR "9223372036854775807"
817#else
818# error Strange INT_MAX
819#endif
820
821#if UINT_MAX == 4294967295
822# define BC_MAX_SCALE_STR "4294967295"
823# define BC_MAX_STRING_STR "4294967294"
824#elif UINT_MAX == 18446744073709551615
825# define BC_MAX_SCALE_STR "18446744073709551615"
826# define BC_MAX_STRING_STR "18446744073709551614"
827#else
828# error Strange UINT_MAX
829#endif
830#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600831
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100832struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100833 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100834 IF_FEATURE_CLEAN_UP(smallint exiting;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600835
836 BcParse prs;
837 BcProgram prog;
838
Denys Vlasenko5318f812018-12-05 17:48:01 +0100839 // For error messages. Can be set to current parsed line,
840 // or [TODO] to current executing line (can be before last parsed one)
841 unsigned err_line;
842
Gavin Howard01055ba2018-11-03 11:00:21 -0600843 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100844 BcVec input_buffer;
845 FILE *input_fp;
Gavin Howard01055ba2018-11-03 11:00:21 -0600846
847 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100848
849#if ENABLE_FEATURE_EDITING
850 line_input_t *line_input_state;
851#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100852} FIX_ALIASING;
853#define G (*ptr_to_globals)
854#define INIT_G() do { \
855 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
856} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100857#define FREE_G() do { \
858 FREE_PTR_TO_GLOBALS(); \
859} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100860#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
861#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100862#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100863#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100864# define G_interrupt bb_got_signal
865# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100866#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100867# define G_interrupt 0
868# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100869#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100870#if ENABLE_FEATURE_CLEAN_UP
871# define G_exiting G.exiting
872#else
873# define G_exiting 0
874#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100875#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100876#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100877
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100878// In configurations where errors abort instead of propagating error
879// return code up the call chain, functions returning BC_STATUS
880// actually don't return anything, they always succeed and return "void".
881// A macro wrapper is provided, which makes this statement work:
882// s = zbc_func(...)
883// and makes it visible to the compiler that s is always zero,
884// allowing compiler to optimize dead code after the statement.
885//
886// To make code more readable, each such function has a "z"
887// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
888//
889#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100890# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100891# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100892# define IF_ERROR_RETURN_POSSIBLE(a) a
893# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100894# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100895# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100896#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100897# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100898# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100899# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
900# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100901# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100902# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100903#endif
904
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100905//
906// Utility routines
907//
Gavin Howard01055ba2018-11-03 11:00:21 -0600908
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100909static void fflush_and_check(void)
910{
911 fflush_all();
912 if (ferror(stdout) || ferror(stderr))
913 bb_perror_msg_and_die("output error");
914}
915
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100916#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100917#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100918do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100919 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100920 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100921 return BC_STATUS_FAILURE; \
922} while (0)
923#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100924#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100925#endif
926
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100927static void quit(void) NORETURN;
928static void quit(void)
929{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100930 if (ferror(stdin))
931 bb_perror_msg_and_die("input error");
932 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100933 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100934 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100935}
936
Denys Vlasenko5318f812018-12-05 17:48:01 +0100937static void bc_verror_msg(const char *fmt, va_list p)
938{
Denys Vlasenko82269122018-12-14 16:30:56 +0100939 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +0100940 if (G.prog.file) {
941 sv = applet_name;
942 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
943 }
944 bb_verror_msg(fmt, p, NULL);
945 if (G.prog.file) {
946 free((char*)applet_name);
947 applet_name = sv;
948 }
949}
950
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100951static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100952{
953 va_list p;
954
955 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100956 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100957 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100958
Denys Vlasenkoec603182018-12-17 10:34:02 +0100959 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
960 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
961 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100962}
963
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100964#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100965static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100966{
967 va_list p;
968
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100969 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100970 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100971 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100972
973 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100974 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100975 va_end(p);
976
977 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100978 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100979 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
980
Denys Vlasenkoec603182018-12-17 10:34:02 +0100981 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100982 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100983 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100984}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100985#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100986#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100987
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100988// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
989// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100990// function must not have caller-cleaned parameters on stack.
991// Unfortunately, vararg function API does exactly that on most arches.
992// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100993static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100994{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100995 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100996}
997static ERRORFUNC int bc_error_bad_character(char c)
998{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100999 if (!c)
1000 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +01001001 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001002}
1003static ERRORFUNC int bc_error_bad_expression(void)
1004{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001005 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001006}
1007static ERRORFUNC int bc_error_bad_token(void)
1008{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001009 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001010}
1011static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1012{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001013 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001014}
1015static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1016{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001017 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001018}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001019#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001020static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001021{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001022 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001023}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001024#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1025static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001026{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001027 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001028}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001029#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1030static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001031{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001032 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 +01001033}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001034#define zbc_POSIX_does_not_allow_bool_ops_this_is_bad(...) (zbc_POSIX_does_not_allow_bool_ops_this_is_bad(__VA_ARGS__) COMMA_SUCCESS)
1035static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001036{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001037 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 +01001038}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001039#define zbc_POSIX_does_not_allow_empty_X_expression_in_for(...) (zbc_POSIX_does_not_allow_empty_X_expression_in_for(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001040#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001041
Gavin Howard01055ba2018-11-03 11:00:21 -06001042static void bc_vec_grow(BcVec *v, size_t n)
1043{
1044 size_t cap = v->cap * 2;
1045 while (cap < v->len + n) cap *= 2;
1046 v->v = xrealloc(v->v, v->size * cap);
1047 v->cap = cap;
1048}
1049
1050static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1051{
1052 v->size = esize;
1053 v->cap = BC_VEC_START_CAP;
1054 v->len = 0;
1055 v->dtor = dtor;
1056 v->v = xmalloc(esize * BC_VEC_START_CAP);
1057}
1058
Denys Vlasenko7d628012018-12-04 21:46:47 +01001059static void bc_char_vec_init(BcVec *v)
1060{
1061 bc_vec_init(v, sizeof(char), NULL);
1062}
1063
Gavin Howard01055ba2018-11-03 11:00:21 -06001064static void bc_vec_expand(BcVec *v, size_t req)
1065{
1066 if (v->cap < req) {
1067 v->v = xrealloc(v->v, v->size * req);
1068 v->cap = req;
1069 }
1070}
1071
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001072static void bc_vec_pop(BcVec *v)
1073{
1074 v->len--;
1075 if (v->dtor)
1076 v->dtor(v->v + (v->size * v->len));
1077}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001078
Gavin Howard01055ba2018-11-03 11:00:21 -06001079static void bc_vec_npop(BcVec *v, size_t n)
1080{
1081 if (!v->dtor)
1082 v->len -= n;
1083 else {
1084 size_t len = v->len - n;
1085 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1086 }
1087}
1088
Denys Vlasenko7d628012018-12-04 21:46:47 +01001089static void bc_vec_pop_all(BcVec *v)
1090{
1091 bc_vec_npop(v, v->len);
1092}
1093
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001094static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001095{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001096 size_t len = v->len;
1097 if (len >= v->cap) bc_vec_grow(v, 1);
1098 memmove(v->v + (v->size * len), data, v->size);
1099 v->len++;
1100 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001101}
1102
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001103// G.prog.results often needs "pop old operand, push result" idiom.
1104// Can do this without a few extra ops
1105static size_t bc_result_pop_and_push(const void *data)
1106{
1107 BcVec *v = &G.prog.results;
1108 char *last;
1109 size_t len = v->len - 1;
1110
1111 last = v->v + (v->size * len);
1112 if (v->dtor)
1113 v->dtor(last);
1114 memmove(last, data, v->size);
1115 return len;
1116}
1117
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001118static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001119{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001120 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001121}
1122
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001123static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001124{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001125 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001126 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001127 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001128}
1129
Gavin Howard01055ba2018-11-03 11:00:21 -06001130static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1131{
1132 if (idx == v->len)
1133 bc_vec_push(v, data);
1134 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001135 char *ptr;
1136
1137 if (v->len == v->cap) bc_vec_grow(v, 1);
1138
1139 ptr = v->v + v->size * idx;
1140
1141 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1142 memmove(ptr, data, v->size);
1143 }
1144}
1145
1146static void bc_vec_string(BcVec *v, size_t len, const char *str)
1147{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001148 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001149 bc_vec_expand(v, len + 1);
1150 memcpy(v->v, str, len);
1151 v->len = len;
1152
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001153 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001154}
1155
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001156#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001157static void bc_vec_concat(BcVec *v, const char *str)
1158{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001159 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001160
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001161 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001162
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001163 slen = strlen(str);
1164 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001165
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001166 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001167 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001168
1169 v->len = len;
1170}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001171#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001172
1173static void *bc_vec_item(const BcVec *v, size_t idx)
1174{
1175 return v->v + v->size * idx;
1176}
1177
1178static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1179{
1180 return v->v + v->size * (v->len - idx - 1);
1181}
1182
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001183static void *bc_vec_top(const BcVec *v)
1184{
1185 return v->v + v->size * (v->len - 1);
1186}
1187
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001188static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001189{
1190 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001191 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001192 free(v->v);
1193}
1194
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001195static BcFunc* bc_program_func(size_t idx)
1196{
1197 return bc_vec_item(&G.prog.fns, idx);
1198}
1199// BC_PROG_MAIN is zeroth element, so:
1200#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1201
1202#if ENABLE_BC
1203static BcFunc* bc_program_current_func(void)
1204{
1205 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1206 BcFunc *func = bc_program_func(ip->func);
1207 return func;
1208}
1209#endif
1210
1211static char** bc_program_str(size_t idx)
1212{
1213#if ENABLE_BC
1214 if (IS_BC) {
1215 BcFunc *func = bc_program_current_func();
1216 return bc_vec_item(&func->strs, idx);
1217 }
1218#endif
1219 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1220}
1221
1222static char** bc_program_const(size_t idx)
1223{
1224#if ENABLE_BC
1225 if (IS_BC) {
1226 BcFunc *func = bc_program_current_func();
1227 return bc_vec_item(&func->consts, idx);
1228 }
1229#endif
1230 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1231}
1232
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001233static int bc_id_cmp(const void *e1, const void *e2)
1234{
1235 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1236}
1237
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001238static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001239{
1240 free(((BcId *) id)->name);
1241}
1242
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001243static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001244{
1245 size_t low = 0, high = v->len;
1246
1247 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001248 size_t mid = (low + high) / 2;
1249 BcId *id = bc_vec_item(v, mid);
1250 int result = bc_id_cmp(ptr, id);
1251
1252 if (result == 0)
1253 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001254 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001255 high = mid;
1256 else
1257 low = mid + 1;
1258 }
1259
1260 return low;
1261}
1262
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001263static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001264{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001265 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001266
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001267 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001268 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001269 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1270 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001271 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001272 bc_vec_pushAt(v, ptr, n);
1273 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001274}
1275
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001276#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001277static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001278{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001279 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001280 if (i >= v->len) return BC_VEC_INVALID_IDX;
1281 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1282}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001283#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001284
Gavin Howard01055ba2018-11-03 11:00:21 -06001285static void bc_num_setToZero(BcNum *n, size_t scale)
1286{
1287 n->len = 0;
1288 n->neg = false;
1289 n->rdx = scale;
1290}
1291
1292static void bc_num_zero(BcNum *n)
1293{
1294 bc_num_setToZero(n, 0);
1295}
1296
1297static void bc_num_one(BcNum *n)
1298{
1299 bc_num_setToZero(n, 0);
1300 n->len = 1;
1301 n->num[0] = 1;
1302}
1303
Denys Vlasenko3129f702018-12-09 12:04:44 +01001304// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001305static void bc_num_init(BcNum *n, size_t req)
1306{
1307 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001308 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001309 n->num = xmalloc(req);
1310 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001311 n->rdx = 0;
1312 n->len = 0;
1313 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001314}
1315
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001316static void bc_num_init_DEF_SIZE(BcNum *n)
1317{
1318 bc_num_init(n, BC_NUM_DEF_SIZE);
1319}
1320
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001321static void bc_num_expand(BcNum *n, size_t req)
1322{
1323 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1324 if (req > n->cap) {
1325 n->num = xrealloc(n->num, req);
1326 n->cap = req;
1327 }
1328}
1329
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001330static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001331{
1332 free(((BcNum *) num)->num);
1333}
1334
1335static void bc_num_copy(BcNum *d, BcNum *s)
1336{
1337 if (d != s) {
1338 bc_num_expand(d, s->cap);
1339 d->len = s->len;
1340 d->neg = s->neg;
1341 d->rdx = s->rdx;
1342 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1343 }
1344}
1345
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001346static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001347{
1348 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001349 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001350
Denys Vlasenko1557b762018-12-22 21:37:46 +01001351 result = 0;
1352 i = n->len;
1353 while (i > n->rdx) {
1354 unsigned long prev = result;
1355 result = result * 10 + n->num[--i];
1356 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1357 // 0x1ff00000 * 10 is 0x13f600000,
1358 // or 0x3f600000 truncated to 32 bits. Which is larger.
1359 // However, (N*10)/8 < N check is always correct.
1360 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001361 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001362 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001363 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001364
Denys Vlasenko29301232018-12-11 15:29:32 +01001365 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001366}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001367#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1368
1369static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1370{
1371 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1372
1373 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1374}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001375#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001376
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001377#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1378# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1379#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1380# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1381#endif
1382// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1383// would not hit realloc() code path - not good if num[] is not malloced
1384
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001385static void bc_num_ulong2num(BcNum *n, unsigned long val)
1386{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001387 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001388
1389 bc_num_zero(n);
1390
1391 if (val == 0) return;
1392
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001393 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001394
1395 ptr = n->num;
1396 for (;;) {
1397 n->len++;
1398 *ptr++ = val % 10;
1399 val /= 10;
1400 if (val == 0) break;
1401 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001402}
1403
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001404static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001405{
1406 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001407 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001408 a[i] -= b[i];
1409 for (j = i; a[j] < 0;) {
1410 a[j++] += 10;
1411 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001412 }
1413 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001414}
1415
1416static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1417{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001418 size_t i = len;
1419 for (;;) {
1420 int c;
1421 if (i == 0)
1422 return 0;
1423 i--;
1424 c = a[i] - b[i];
1425 if (c != 0) {
1426 i++;
1427 if (c < 0)
1428 return -i;
1429 return i;
1430 }
1431 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001432}
1433
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001434#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1435#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1436#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1437//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1438static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1439{
1440 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1441}
1442//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1443static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1444{
1445 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1446}
1447
Gavin Howard01055ba2018-11-03 11:00:21 -06001448static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1449{
1450 size_t i, min, a_int, b_int, diff;
1451 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001452 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001453 ssize_t cmp;
1454
1455 if (a == b) return 0;
1456 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1457 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001458
1459 if (a->neg != b->neg) // signs of a and b differ
1460 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1461 return (int)b->neg - (int)a->neg;
1462 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001463
1464 a_int = BC_NUM_INT(a);
1465 b_int = BC_NUM_INT(b);
1466 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001467
1468 if (a_int != 0) return (ssize_t) a_int;
1469
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001470 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001471 if (a_max) {
1472 min = b->rdx;
1473 diff = a->rdx - b->rdx;
1474 max_num = a->num + diff;
1475 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001476 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1477 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001478 min = a->rdx;
1479 diff = b->rdx - a->rdx;
1480 max_num = b->num + diff;
1481 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001482 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001483 }
1484
1485 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001486 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001487
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001488 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001489 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001490 }
1491
1492 return 0;
1493}
1494
1495static void bc_num_truncate(BcNum *n, size_t places)
1496{
1497 if (places == 0) return;
1498
1499 n->rdx -= places;
1500
1501 if (n->len != 0) {
1502 n->len -= places;
1503 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1504 }
1505}
1506
1507static void bc_num_extend(BcNum *n, size_t places)
1508{
1509 size_t len = n->len + places;
1510
1511 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001512 if (n->cap < len) bc_num_expand(n, len);
1513
1514 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1515 memset(n->num, 0, sizeof(BcDig) * places);
1516
1517 n->len += places;
1518 n->rdx += places;
1519 }
1520}
1521
1522static void bc_num_clean(BcNum *n)
1523{
1524 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1525 if (n->len == 0)
1526 n->neg = false;
1527 else if (n->len < n->rdx)
1528 n->len = n->rdx;
1529}
1530
1531static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1532{
1533 if (n->rdx < scale)
1534 bc_num_extend(n, scale - n->rdx);
1535 else
1536 bc_num_truncate(n, n->rdx - scale);
1537
1538 bc_num_clean(n);
1539 if (n->len != 0) n->neg = !neg1 != !neg2;
1540}
1541
1542static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1543 BcNum *restrict b)
1544{
1545 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001546 b->len = n->len - idx;
1547 a->len = idx;
1548 a->rdx = b->rdx = 0;
1549
1550 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1551 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001552 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001553 bc_num_zero(b);
1554 bc_num_copy(a, n);
1555 }
1556
1557 bc_num_clean(a);
1558 bc_num_clean(b);
1559}
1560
Denys Vlasenko29301232018-12-11 15:29:32 +01001561static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001562{
Denys Vlasenko29301232018-12-11 15:29:32 +01001563 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001564
1565 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1566 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1567 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001568 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001569 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001570
1571 if (n->rdx >= places)
1572 n->rdx -= places;
1573 else {
1574 bc_num_extend(n, places - n->rdx);
1575 n->rdx = 0;
1576 }
1577
1578 bc_num_clean(n);
1579
Denys Vlasenko29301232018-12-11 15:29:32 +01001580 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001581}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001582#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001583
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001584typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1585
1586static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1587 BcNumBinaryOp op, size_t req)
1588{
1589 BcStatus s;
1590 BcNum num2, *ptr_a, *ptr_b;
1591 bool init = false;
1592
1593 if (c == a) {
1594 ptr_a = &num2;
1595 memcpy(ptr_a, c, sizeof(BcNum));
1596 init = true;
1597 } else
1598 ptr_a = a;
1599
1600 if (c == b) {
1601 ptr_b = &num2;
1602 if (c != a) {
1603 memcpy(ptr_b, c, sizeof(BcNum));
1604 init = true;
1605 }
1606 } else
1607 ptr_b = b;
1608
1609 if (init)
1610 bc_num_init(c, req);
1611 else
1612 bc_num_expand(c, req);
1613
1614 s = BC_STATUS_SUCCESS;
1615 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1616
1617 if (init) bc_num_free(&num2);
1618
1619 RETURN_STATUS(s);
1620}
1621#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1622
1623static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1624static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1625static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1626static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1627static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1628static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1629
1630static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1631{
1632 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1633 (void) scale;
1634 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1635}
1636
1637static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1638{
1639 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1640 (void) scale;
1641 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1642}
1643
1644static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1645{
1646 size_t req = BC_NUM_MREQ(a, b, scale);
1647 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1648}
1649
1650static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1651{
1652 size_t req = BC_NUM_MREQ(a, b, scale);
1653 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1654}
1655
1656static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1657{
1658 size_t req = BC_NUM_MREQ(a, b, scale);
1659 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1660}
1661
1662static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1663{
1664 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1665}
1666
1667static const BcNumBinaryOp zbc_program_ops[] = {
1668 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1669};
1670#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1671#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1672#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1673#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1674#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1675#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1676
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001677static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001678{
1679 BcNum one;
1680 BcDig num[2];
1681
1682 one.cap = 2;
1683 one.num = num;
1684 bc_num_one(&one);
1685
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001686 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001687}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001688#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001689
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001690static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001691{
1692 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1693 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001694 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001695
1696 // Because this function doesn't need to use scale (per the bc spec),
1697 // I am hijacking it to say whether it's doing an add or a subtract.
1698
1699 if (a->len == 0) {
1700 bc_num_copy(c, b);
1701 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001702 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001703 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001704 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001705 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001706 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001707 }
1708
1709 c->neg = a->neg;
1710 c->rdx = BC_MAX(a->rdx, b->rdx);
1711 min_rdx = BC_MIN(a->rdx, b->rdx);
1712 c->len = 0;
1713
1714 if (a->rdx > b->rdx) {
1715 diff = a->rdx - b->rdx;
1716 ptr = a->num;
1717 ptr_a = a->num + diff;
1718 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001719 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001720 diff = b->rdx - a->rdx;
1721 ptr = b->num;
1722 ptr_a = a->num;
1723 ptr_b = b->num + diff;
1724 }
1725
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001726 ptr_c = c->num;
1727 for (i = 0; i < diff; ++i, ++c->len)
1728 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001729
1730 ptr_c += diff;
1731 a_int = BC_NUM_INT(a);
1732 b_int = BC_NUM_INT(b);
1733
1734 if (a_int > b_int) {
1735 min_int = b_int;
1736 max = a_int;
1737 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001738 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001739 min_int = a_int;
1740 max = b_int;
1741 ptr = ptr_b;
1742 }
1743
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001744 carry = 0;
1745 for (i = 0; i < min_rdx + min_int; ++i) {
1746 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001747 carry = in / 10;
1748 ptr_c[i] = (BcDig)(in % 10);
1749 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001750 for (; i < max + min_rdx; ++i) {
1751 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001752 carry = in / 10;
1753 ptr_c[i] = (BcDig)(in % 10);
1754 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001755 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001756
1757 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1758
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001759 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001760}
1761
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001763{
Gavin Howard01055ba2018-11-03 11:00:21 -06001764 ssize_t cmp;
1765 BcNum *minuend, *subtrahend;
1766 size_t start;
1767 bool aneg, bneg, neg;
1768
1769 // Because this function doesn't need to use scale (per the bc spec),
1770 // I am hijacking it to say whether it's doing an add or a subtract.
1771
1772 if (a->len == 0) {
1773 bc_num_copy(c, b);
1774 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001775 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001776 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001777 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001778 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001779 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001780 }
1781
1782 aneg = a->neg;
1783 bneg = b->neg;
1784 a->neg = b->neg = false;
1785
1786 cmp = bc_num_cmp(a, b);
1787
1788 a->neg = aneg;
1789 b->neg = bneg;
1790
1791 if (cmp == 0) {
1792 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001793 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001794 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001795 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001796 neg = a->neg;
1797 minuend = a;
1798 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001799 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001800 neg = b->neg;
1801 if (sub) neg = !neg;
1802 minuend = b;
1803 subtrahend = a;
1804 }
1805
1806 bc_num_copy(c, minuend);
1807 c->neg = neg;
1808
1809 if (c->rdx < subtrahend->rdx) {
1810 bc_num_extend(c, subtrahend->rdx - c->rdx);
1811 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001812 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001813 start = c->rdx - subtrahend->rdx;
1814
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001815 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001816
1817 bc_num_clean(c);
1818
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001819 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001820}
1821
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001822static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001823 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001824#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001825{
1826 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001827 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001828 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001829 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001830
Gavin Howard01055ba2018-11-03 11:00:21 -06001831 if (a->len == 0 || b->len == 0) {
1832 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001833 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001834 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001835 aone = BC_NUM_ONE(a);
1836 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001837 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001838 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001839 }
1840
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001841 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1842 || a->len < BC_NUM_KARATSUBA_LEN
1843 || b->len < BC_NUM_KARATSUBA_LEN
1844 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001845 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001846
Gavin Howard01055ba2018-11-03 11:00:21 -06001847 bc_num_expand(c, a->len + b->len + 1);
1848
1849 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001850 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001851
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001852 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001853 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001854 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001855 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001856 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001857 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001858 carry = in / 10;
1859 c->num[i + j] = (BcDig)(in % 10);
1860 }
1861
1862 c->num[i + j] += (BcDig) carry;
1863 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001864
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001865#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001866 // a=2^1000000
1867 // a*a <- without check below, this will not be interruptible
1868 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001869#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001870 }
1871
1872 c->len = len;
1873
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001874 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001875 }
1876
1877 bc_num_init(&l1, max);
1878 bc_num_init(&h1, max);
1879 bc_num_init(&l2, max);
1880 bc_num_init(&h2, max);
1881 bc_num_init(&m1, max);
1882 bc_num_init(&m2, max);
1883 bc_num_init(&z0, max);
1884 bc_num_init(&z1, max);
1885 bc_num_init(&z2, max);
1886 bc_num_init(&temp, max + max);
1887
1888 bc_num_split(a, max2, &l1, &h1);
1889 bc_num_split(b, max2, &l2, &h2);
1890
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001891 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001892 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001893 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001894 if (s) goto err;
1895
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001896 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001897 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001898 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001899 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001900 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001901 if (s) goto err;
1902
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001903 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001904 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001905 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001906 if (s) goto err;
1907
Denys Vlasenko29301232018-12-11 15:29:32 +01001908 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001909 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001910 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001911 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001912 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001913 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001914 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001915 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001916 bc_num_free(&temp);
1917 bc_num_free(&z2);
1918 bc_num_free(&z1);
1919 bc_num_free(&z0);
1920 bc_num_free(&m2);
1921 bc_num_free(&m1);
1922 bc_num_free(&h2);
1923 bc_num_free(&l2);
1924 bc_num_free(&h1);
1925 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001926 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001927}
1928
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001929static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001930{
1931 BcStatus s;
1932 BcNum cpa, cpb;
1933 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1934
1935 scale = BC_MAX(scale, a->rdx);
1936 scale = BC_MAX(scale, b->rdx);
1937 scale = BC_MIN(a->rdx + b->rdx, scale);
1938 maxrdx = BC_MAX(maxrdx, scale);
1939
1940 bc_num_init(&cpa, a->len);
1941 bc_num_init(&cpb, b->len);
1942
1943 bc_num_copy(&cpa, a);
1944 bc_num_copy(&cpb, b);
1945 cpa.neg = cpb.neg = false;
1946
Denys Vlasenko29301232018-12-11 15:29:32 +01001947 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001948 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001949 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001950 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001951 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001952 if (s) goto err;
1953
1954 maxrdx += scale;
1955 bc_num_expand(c, c->len + maxrdx);
1956
1957 if (c->len < maxrdx) {
1958 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1959 c->len += maxrdx;
1960 }
1961
1962 c->rdx = maxrdx;
1963 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001964 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001965 bc_num_free(&cpb);
1966 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001967 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001968}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001969#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001970
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001971static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001972{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001973 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001974 size_t len, end, i;
1975 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001976
1977 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001978 RETURN_STATUS(bc_error("divide by zero"));
1979 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001980 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001981 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001982 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001983 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001984 bc_num_copy(c, a);
1985 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001986 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001987 }
1988
1989 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1990 bc_num_copy(&cp, a);
1991 len = b->len;
1992
1993 if (len > cp.len) {
1994 bc_num_expand(&cp, len + 2);
1995 bc_num_extend(&cp, len - cp.len);
1996 }
1997
1998 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1999 cp.rdx -= b->rdx;
2000 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2001
2002 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01002003 for (;;) {
2004 if (len == 0) break;
2005 len--;
2006 if (b->num[len] != 0)
2007 break;
2008 }
2009 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06002010 }
2011
2012 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2013
2014 // We want an extra zero in front to make things simpler.
2015 cp.num[cp.len++] = 0;
2016 end = cp.len - len;
2017
2018 bc_num_expand(c, cp.len);
2019
2020 bc_num_zero(c);
2021 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2022 c->rdx = cp.rdx;
2023 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002024
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002025 s = BC_STATUS_SUCCESS;
2026 for (i = end - 1; i < end; --i) {
2027 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002028 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002029 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2030 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002031 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002032#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002033 // a=2^100000
2034 // scale=40000
2035 // 1/a <- without check below, this will not be interruptible
2036 if (G_interrupt) {
2037 s = BC_STATUS_FAILURE;
2038 break;
2039 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002040#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002041 }
2042
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002043 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002044 bc_num_free(&cp);
2045
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002046 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002047}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002048#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002049
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002050static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002051 BcNum *restrict d, size_t scale, size_t ts)
2052{
2053 BcStatus s;
2054 BcNum temp;
2055 bool neg;
2056
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002057 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002058 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002059
2060 if (a->len == 0) {
2061 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002062 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002063 }
2064
2065 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002066 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002067 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002068
2069 if (scale != 0) scale = ts;
2070
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002071 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002072 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002073 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002074 if (s) goto err;
2075
2076 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2077
2078 neg = d->neg;
2079 bc_num_retireMul(d, ts, a->neg, b->neg);
2080 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002081 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002082 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002083 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002084}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002085#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002086
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002087static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002088{
2089 BcStatus s;
2090 BcNum c1;
2091 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2092
2093 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002094 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002095 bc_num_free(&c1);
2096
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002097 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002098}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002099#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002100
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002101static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002102{
2103 BcStatus s = BC_STATUS_SUCCESS;
2104 BcNum copy;
2105 unsigned long pow;
2106 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002107 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002108
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002109 // GNU bc does not allow 2^2.0 - we do
2110 for (i = 0; i < b->rdx; i++)
2111 if (b->num[i] != 0)
2112 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002113
2114 if (b->len == 0) {
2115 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002116 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002118 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002119 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002120 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002121 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002122 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002123 if (!b->neg)
2124 bc_num_copy(c, a);
2125 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002126 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002127 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002128 }
2129
2130 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002131 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002132 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002133 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002134
2135 bc_num_init(&copy, a->len);
2136 bc_num_copy(&copy, a);
2137
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002138 if (!neg) {
2139 if (a->rdx > scale)
2140 scale = a->rdx;
2141 if (a->rdx * pow < scale)
2142 scale = a->rdx * pow;
2143 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002144
Gavin Howard01055ba2018-11-03 11:00:21 -06002145
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002146 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002147 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002148 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002149 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002150 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002151 //if (G_interrupt) {
2152 // s = BC_STATUS_FAILURE;
2153 // goto err;
2154 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002155 }
2156
Gavin Howard01055ba2018-11-03 11:00:21 -06002157 bc_num_copy(c, &copy);
2158
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002159 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002160 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002161 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002162 if (s) goto err;
2163
2164 if (pow & 1) {
2165 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002166 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002167 if (s) goto err;
2168 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002169 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002170 //if (G_interrupt) {
2171 // s = BC_STATUS_FAILURE;
2172 // goto err;
2173 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002174 }
2175
2176 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002177 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002178 if (s) goto err;
2179 }
2180
Gavin Howard01055ba2018-11-03 11:00:21 -06002181 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2182
2183 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002184 for (i = 0; i < c->len; ++i)
2185 if (c->num[i] != 0)
2186 goto skip;
2187 bc_num_setToZero(c, scale);
2188 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002189
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002190 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002191 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002192 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002193}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002194#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002195
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002196static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002197{
2198 BcStatus s;
2199 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2200 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2201 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2202
2203 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2204 bc_num_expand(b, req);
2205
2206 if (a->len == 0) {
2207 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002208 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002209 }
2210 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002211 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002212 }
2213 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002214 bc_num_one(b);
2215 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002216 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002217 }
2218
2219 scale = BC_MAX(scale, a->rdx) + 1;
2220 len = a->len + scale;
2221
2222 bc_num_init(&num1, len);
2223 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002224 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002225
2226 bc_num_one(&half);
2227 half.num[0] = 5;
2228 half.rdx = 1;
2229
2230 bc_num_init(&f, len);
2231 bc_num_init(&fprime, len);
2232
2233 x0 = &num1;
2234 x1 = &num2;
2235
2236 bc_num_one(x0);
2237 pow = BC_NUM_INT(a);
2238
2239 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002240 if (pow & 1)
2241 x0->num[0] = 2;
2242 else
2243 x0->num[0] = 6;
2244
2245 pow -= 2 - (pow & 1);
2246
2247 bc_num_extend(x0, pow);
2248
2249 // Make sure to move the radix back.
2250 x0->rdx -= pow;
2251 }
2252
2253 x0->rdx = digs = digs1 = 0;
2254 resrdx = scale + 2;
2255 len = BC_NUM_INT(x0) + resrdx - 1;
2256
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002257 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002258 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002259 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002260 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002261 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002262 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002263 if (s) goto err;
2264
2265 cmp = bc_num_cmp(x1, x0);
2266 digs = x1->len - (unsigned long long) llabs(cmp);
2267
2268 if (cmp == cmp2 && digs == digs1)
2269 times += 1;
2270 else
2271 times = 0;
2272
2273 resrdx += times > 4;
2274
2275 cmp2 = cmp1;
2276 cmp1 = cmp;
2277 digs1 = digs;
2278
2279 temp = x0;
2280 x0 = x1;
2281 x1 = temp;
2282 }
2283
Gavin Howard01055ba2018-11-03 11:00:21 -06002284 bc_num_copy(b, x0);
2285 scale -= 1;
2286 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002287 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002288 bc_num_free(&fprime);
2289 bc_num_free(&f);
2290 bc_num_free(&half);
2291 bc_num_free(&num2);
2292 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002293 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002294}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002295#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002296
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002297static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002298 size_t scale)
2299{
2300 BcStatus s;
2301 BcNum num2, *ptr_a;
2302 bool init = false;
2303 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2304
2305 if (c == a) {
2306 memcpy(&num2, c, sizeof(BcNum));
2307 ptr_a = &num2;
2308 bc_num_init(c, len);
2309 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002310 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002311 ptr_a = a;
2312 bc_num_expand(c, len);
2313 }
2314
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002315 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002316
2317 if (init) bc_num_free(&num2);
2318
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002319 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002320}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002321#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002322
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002323#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002324static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002325{
2326 BcStatus s;
2327 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002328 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002329
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002330 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002331 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002332 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002333 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002334 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002335 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002336
2337 bc_num_expand(d, c->len);
2338 bc_num_init(&base, c->len);
2339 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002340 bc_num_init(&temp, b->len);
2341
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002342 two.cap = ARRAY_SIZE(two_digs);
2343 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002344 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002345 two_digs[0] = 2;
2346
Gavin Howard01055ba2018-11-03 11:00:21 -06002347 bc_num_one(d);
2348
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002349 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002350 if (s) goto err;
2351 bc_num_copy(&exp, b);
2352
2353 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002354 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002355 if (s) goto err;
2356
2357 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002358 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002359 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002360 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002361 if (s) goto err;
2362 }
2363
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002364 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002365 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002366 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002367 if (s) goto err;
2368 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002369 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002370 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002371 bc_num_free(&exp);
2372 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002373 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002374}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002375#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002376#endif // ENABLE_DC
2377
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002378static FAST_FUNC void bc_string_free(void *string)
2379{
2380 free(*(char**)string);
2381}
2382
Gavin Howard01055ba2018-11-03 11:00:21 -06002383static void bc_func_init(BcFunc *f)
2384{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002385 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002386 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2387 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002388 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2389 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002390 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002391}
2392
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002393static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002394{
2395 BcFunc *f = (BcFunc *) func;
2396 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002397 IF_BC(bc_vec_free(&f->labels);)
2398 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002399 IF_BC(bc_vec_free(&f->strs);)
2400 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002401}
2402
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002403static void bc_array_expand(BcVec *a, size_t len);
2404
Gavin Howard01055ba2018-11-03 11:00:21 -06002405static void bc_array_init(BcVec *a, bool nums)
2406{
2407 if (nums)
2408 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2409 else
2410 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2411 bc_array_expand(a, 1);
2412}
2413
Gavin Howard01055ba2018-11-03 11:00:21 -06002414static void bc_array_expand(BcVec *a, size_t len)
2415{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002416 if (a->dtor == bc_num_free
2417 // && a->size == sizeof(BcNum) - always true
2418 ) {
2419 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002420 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002421 bc_num_init_DEF_SIZE(&n);
2422 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002423 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002424 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002425 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002426 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002427 bc_array_init(&v, true);
2428 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002429 }
2430 }
2431}
2432
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002433static void bc_array_copy(BcVec *d, const BcVec *s)
2434{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002435 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002436 size_t i;
2437
2438 bc_vec_pop_all(d);
2439 bc_vec_expand(d, s->cap);
2440 d->len = s->len;
2441
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002442 dnum = (void*)d->v;
2443 snum = (void*)s->v;
2444 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002445 bc_num_init(dnum, snum->len);
2446 bc_num_copy(dnum, snum);
2447 }
2448}
2449
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002450#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002451static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002452{
2453 d->t = src->t;
2454
2455 switch (d->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002456 case BC_RESULT_TEMP:
2457 case BC_RESULT_IBASE:
2458 case BC_RESULT_SCALE:
2459 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002460 bc_num_init(&d->d.n, src->d.n.len);
2461 bc_num_copy(&d->d.n, &src->d.n);
2462 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002463 case BC_RESULT_VAR:
2464 case BC_RESULT_ARRAY:
2465 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002466 d->d.id.name = xstrdup(src->d.id.name);
2467 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002468 case BC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002469 IF_BC(case BC_RESULT_LAST:)
Gavin Howard01055ba2018-11-03 11:00:21 -06002470 case BC_RESULT_ONE:
2471 case BC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002472 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2473 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002474 }
2475}
2476#endif // ENABLE_DC
2477
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002478static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002479{
2480 BcResult *r = (BcResult *) result;
2481
2482 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002483 case BC_RESULT_TEMP:
2484 case BC_RESULT_IBASE:
2485 case BC_RESULT_SCALE:
2486 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002487 bc_num_free(&r->d.n);
2488 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002489 case BC_RESULT_VAR:
2490 case BC_RESULT_ARRAY:
2491 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002492 free(r->d.id.name);
2493 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002494 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002495 // Do nothing.
2496 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002497 }
2498}
2499
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002500static int bad_input_byte(char c)
2501{
2502 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2503 || c > 0x7e
2504 ) {
2505 bc_error_fmt("illegal character 0x%02x", c);
2506 return 1;
2507 }
2508 return 0;
2509}
2510
2511// Note: it _appends_ data from fp to vec.
2512static void bc_read_line(BcVec *vec, FILE *fp)
2513{
2514 again:
2515 fflush_and_check();
2516
2517#if ENABLE_FEATURE_BC_SIGNALS
2518 if (G_interrupt) { // ^C was pressed
2519 intr:
2520 if (fp != stdin) {
2521 // ^C while running a script (bc SCRIPT): die.
2522 // We do not return to interactive prompt:
2523 // user might be running us from a shell,
2524 // and SCRIPT might be intended to terminate
2525 // (e.g. contain a "halt" stmt).
2526 // ^C dropping user into a bc prompt instead of
2527 // the shell would be unexpected.
2528 xfunc_die();
2529 }
2530 // ^C while interactive input
2531 G_interrupt = 0;
2532 // GNU bc says "interrupted execution."
2533 // GNU dc says "Interrupt!"
2534 fputs("\ninterrupted execution\n", stderr);
2535 }
2536
2537# if ENABLE_FEATURE_EDITING
2538 if (G_ttyin && fp == stdin) {
2539 int n, i;
2540# define line_buf bb_common_bufsiz1
2541 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2542 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2543 if (n == 0) // ^C
2544 goto intr;
2545 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2546 return;
2547 }
2548 i = 0;
2549 for (;;) {
2550 char c = line_buf[i++];
2551 if (!c) break;
2552 if (bad_input_byte(c)) goto again;
2553 }
2554 bc_vec_concat(vec, line_buf);
2555# undef line_buf
2556 } else
2557# endif
2558#endif
2559 {
2560 int c;
2561 bool bad_chars = 0;
2562 size_t len = vec->len;
2563
2564 do {
2565#if ENABLE_FEATURE_BC_SIGNALS
2566 if (G_interrupt) {
2567 // ^C was pressed: ignore entire line, get another one
2568 vec->len = len;
2569 goto intr;
2570 }
2571#endif
2572 do c = fgetc(fp); while (c == '\0');
2573 if (c == EOF) {
2574 if (ferror(fp))
2575 bb_perror_msg_and_die("input error");
2576 // Note: EOF does not append '\n'
2577 break;
2578 }
2579 bad_chars |= bad_input_byte(c);
2580 bc_vec_pushByte(vec, (char)c);
2581 } while (c != '\n');
2582
2583 if (bad_chars) {
2584 // Bad chars on this line
2585 if (!G.prog.file) { // stdin
2586 // ignore entire line, get another one
2587 vec->len = len;
2588 goto again;
2589 }
2590 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
2591 }
2592 bc_vec_pushZeroByte(vec);
2593 }
2594}
2595
2596//
2597// Parsing routines
2598//
2599
2600static bool bc_num_strValid(const char *val, size_t base)
2601{
2602 BcDig b;
2603 bool radix;
2604
2605 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2606 radix = false;
2607 for (;;) {
2608 BcDig c = *val++;
2609 if (c == '\0')
2610 break;
2611 if (c == '.') {
2612 if (radix) return false;
2613 radix = true;
2614 continue;
2615 }
2616 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2617 return false;
2618 }
2619 return true;
2620}
2621
2622// Note: n is already "bc_num_zero()"ed,
2623// leading zeroes in "val" are removed
2624static void bc_num_parseDecimal(BcNum *n, const char *val)
2625{
2626 size_t len, i;
2627 const char *ptr;
2628
2629 len = strlen(val);
2630 if (len == 0)
2631 return;
2632
2633 bc_num_expand(n, len);
2634
2635 ptr = strchr(val, '.');
2636
2637 n->rdx = 0;
2638 if (ptr != NULL)
2639 n->rdx = (size_t)((val + len) - (ptr + 1));
2640
2641 for (i = 0; val[i]; ++i) {
2642 if (val[i] != '0' && val[i] != '.') {
2643 // Not entirely zero value - convert it, and exit
2644 i = len - 1;
2645 for (;;) {
2646 n->num[n->len] = val[i] - '0';
2647 ++n->len;
2648 skip_dot:
2649 if (i == 0) break;
2650 if (val[--i] == '.') goto skip_dot;
2651 }
2652 break;
2653 }
2654 }
2655 // if for() exits without hitting if(), the value is entirely zero
2656}
2657
2658// Note: n is already "bc_num_zero()"ed,
2659// leading zeroes in "val" are removed
2660static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2661{
2662 BcStatus s;
2663 BcNum temp, mult, result;
2664 BcNum base;
2665 BcDig base_digs[ULONG_NUM_BUFSIZE];
2666 BcDig c = '\0';
2667 unsigned long v;
2668 size_t i, digits;
2669
2670 for (i = 0; ; ++i) {
2671 if (val[i] == '\0')
2672 return;
2673 if (val[i] != '.' && val[i] != '0')
2674 break;
2675 }
2676
2677 bc_num_init_DEF_SIZE(&temp);
2678 bc_num_init_DEF_SIZE(&mult);
2679 base.cap = ARRAY_SIZE(base_digs);
2680 base.num = base_digs;
2681 bc_num_ulong2num(&base, base_t);
2682
2683 for (;;) {
2684 c = *val++;
2685 if (c == '\0') goto int_err;
2686 if (c == '.') break;
2687
2688 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2689
2690 s = zbc_num_mul(n, &base, &mult, 0);
2691 if (s) goto int_err;
2692 bc_num_ulong2num(&temp, v);
2693 s = zbc_num_add(&mult, &temp, n, 0);
2694 if (s) goto int_err;
2695 }
2696
2697 bc_num_init(&result, base.len);
2698 //bc_num_zero(&result); - already is
2699 bc_num_one(&mult);
2700
2701 digits = 0;
2702 for (;;) {
2703 c = *val++;
2704 if (c == '\0') break;
2705 digits++;
2706
2707 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2708
2709 s = zbc_num_mul(&result, &base, &result, 0);
2710 if (s) goto err;
2711 bc_num_ulong2num(&temp, v);
2712 s = zbc_num_add(&result, &temp, &result, 0);
2713 if (s) goto err;
2714 s = zbc_num_mul(&mult, &base, &mult, 0);
2715 if (s) goto err;
2716 }
2717
2718 s = zbc_num_div(&result, &mult, &result, digits);
2719 if (s) goto err;
2720 s = zbc_num_add(n, &result, n, digits);
2721 if (s) goto err;
2722
2723 if (n->len != 0) {
2724 if (n->rdx < digits)
2725 bc_num_extend(n, digits - n->rdx);
2726 } else
2727 bc_num_zero(n);
2728 err:
2729 bc_num_free(&result);
2730 int_err:
2731 bc_num_free(&mult);
2732 bc_num_free(&temp);
2733}
2734
2735static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
2736{
2737 if (!bc_num_strValid(val, base_t))
2738 RETURN_STATUS(bc_error("bad number string"));
2739
2740 bc_num_zero(n);
2741 while (*val == '0') val++;
2742
2743 if (base_t == 10)
2744 bc_num_parseDecimal(n, val);
2745 else
2746 bc_num_parseBase(n, val, base_t);
2747
2748 RETURN_STATUS(BC_STATUS_SUCCESS);
2749}
2750#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
2751
Gavin Howard01055ba2018-11-03 11:00:21 -06002752static void bc_lex_lineComment(BcLex *l)
2753{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002754 // Try: echo -n '#foo' | bc
2755 size_t i;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002756 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002757 i = l->i;
2758 while (i < l->len && l->buf[i] != '\n')
2759 i++;
2760 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002761}
2762
2763static void bc_lex_whitespace(BcLex *l)
2764{
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002765 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002766 for (;;) {
2767 char c = l->buf[l->i];
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002768 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002769 break;
2770 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002771 break;
2772 l->i++;
2773 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002774}
2775
Denys Vlasenko29301232018-12-11 15:29:32 +01002776static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002777{
2778 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002779 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002780 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002781
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002782 pt = (start == '.');
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002783 l->t.t = XC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002784 ccnt = i = 0;
2785 for (;;) {
2786 char c = buf[i];
2787 if (c == '\0')
2788 break;
2789 if (c == '\\' && buf[i + 1] == '\n') {
2790 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002791 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002792 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002793 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002794 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2795 if (c != '.') break;
2796 // if '.' was already seen, stop on second one:
2797 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002798 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002799 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002800 // buf[i] is one of "0-9A-F."
2801 i++;
2802 if (c != '.')
2803 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002804 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002805 //ccnt is the number of chars in the number string, excluding possible
2806 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2807 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002808 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002809
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002810 // This might overestimate the size, if there are "\<NL>"'s
2811 // in the number. Subtracting number_of_backslashes*2 correctly
2812 // is not that easy: consider that in the case of "NNN.\<NL>"
2813 // loop above will count "\<NL>" before it realizes it is not
2814 // in fact *inside* the number:
2815 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002816
Denys Vlasenko64074a12018-12-07 15:50:14 +01002817 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2818 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2819 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002820 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002821 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002822
Denys Vlasenko7d628012018-12-04 21:46:47 +01002823 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002824 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002825 bc_vec_push(&l->t.v, &start);
2826
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002827 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002828 // If we have hit a backslash, skip it. We don't have
2829 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002830 if (*buf == '\\') {
2831 buf += 2;
2832 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002833 continue;
2834 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002835 bc_vec_push(&l->t.v, buf);
2836 buf++;
2837 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002838 }
2839
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002840 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002841
Denys Vlasenko29301232018-12-11 15:29:32 +01002842 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002843}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002844#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002845
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002846static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002847{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002848 size_t i;
2849 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002850
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002851 l->t.t = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06002852
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002853 i = 0;
2854 buf = l->buf + l->i - 1;
2855 for (;;) {
2856 char c = buf[i];
2857 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2858 i++;
2859 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002860
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002861#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002862 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2863 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2864 if (i > BC_MAX_STRING)
2865 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2866 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002867#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002868 bc_vec_string(&l->t.v, i, buf);
2869
2870 // Increment the index. We minus 1 because it has already been incremented.
2871 l->i += i - 1;
2872
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002873 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002874}
2875
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002876static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002877{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002878 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002879}
2880
2881static void bc_lex_free(BcLex *l)
2882{
2883 bc_vec_free(&l->t.v);
2884}
2885
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002886static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002887{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002888 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002889 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002890}
2891
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002892static bool bc_lex_more_input(BcLex *l)
2893{
2894 size_t str;
2895 bool comment;
2896
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002897 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002898
2899 // This loop is complex because the vm tries not to send any lines that end
2900 // with a backslash to the parser. The reason for that is because the parser
2901 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2902 // case, and for strings and comments, the parser will expect more stuff.
2903 comment = false;
2904 str = 0;
2905 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002906 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002907 char *string;
2908
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002909 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002910 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002911 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002912 break;
2913
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002914 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002915 while (*string) {
2916 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002917 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002918 if (IS_BC)
2919 str ^= (c == '"');
2920 else {
2921 if (c == ']')
2922 str -= 1;
2923 else if (c == '[')
2924 str += 1;
2925 }
2926 }
2927 string++;
2928 if (c == '/' && *string == '*') {
2929 comment = true;
2930 string++;
2931 continue;
2932 }
2933 if (c == '*' && *string == '/') {
2934 comment = false;
2935 string++;
2936 }
2937 }
2938 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002939 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002940 continue;
2941 }
2942
2943 // Check for backslash+newline.
2944 // we do not check that last char is '\n' -
2945 // if it is not, then it's EOF, and looping back
2946 // to bc_read_line() will detect it:
2947 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002948 if (string >= G.input_buffer.v && *string == '\\') {
2949 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002950 continue;
2951 }
2952
2953 break;
2954 }
2955
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002956 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002957 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002958// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2959 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002960
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002961 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002962}
2963
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002964IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2965IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002966#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2967#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002968
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002969static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002970{
2971 BcStatus s;
2972
2973 l->t.last = l->t.t;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01002974 if (l->t.last == XC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002975
2976 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002977 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002978 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002979
2980 // Loop until failure or we don't have whitespace. This
2981 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002982 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002983 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002984 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002985 if (l->i == l->len) {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01002986 l->t.t = XC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002987 if (!G.input_fp)
2988 RETURN_STATUS(BC_STATUS_SUCCESS);
2989 if (!bc_lex_more_input(l)) {
2990 G.input_fp = NULL;
2991 RETURN_STATUS(BC_STATUS_SUCCESS);
2992 }
2993 // here it's guaranteed that l->i is below l->len
2994 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002995 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002996 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2997 l->buf + l->i);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002998 if (IS_BC) {
2999 IF_BC(s = zbc_lex_token(l));
3000 } else {
3001 IF_DC(s = zdc_lex_token(l));
3002 }
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003003 } while (!s && l->t.t == XC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01003004 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06003005
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003006 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003007}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003008#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003009
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003010#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01003011static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
3012{
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003013 if (l->t.t == XC_LEX_NLINE)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003014 RETURN_STATUS(zbc_lex_next(l));
3015 RETURN_STATUS(BC_STATUS_SUCCESS);
3016}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003017#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01003018
3019static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
3020{
3021 BcStatus s;
3022 s = zbc_lex_next(l);
3023 if (s) RETURN_STATUS(s);
3024 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
3025 s = zbc_lex_skip_if_at_NLINE(l);
3026 RETURN_STATUS(s);
3027}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003028#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003029#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01003030
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003031static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003032{
3033 l->buf = text;
3034 l->i = 0;
3035 l->len = strlen(text);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003036 l->t.t = l->t.last = XC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003037 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003038}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003039#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003040
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003041#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003042static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003043{
3044 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003045 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003046 const char *buf = l->buf + l->i - 1;
3047
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003048 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3049 const char *keyword8 = bc_lex_kws[i].name8;
3050 unsigned j = 0;
3051 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3052 j++;
3053 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003054 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003055 if (keyword8[j] != '\0')
3056 continue;
3057 match:
3058 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003059 if (isalnum(buf[j]) || buf[j]=='_')
3060 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003061 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003062 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003063 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
3064 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003065 }
3066
3067 // We minus 1 because the index has already been incremented.
3068 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003069 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003070 }
3071
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003072 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003073 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003074
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003075 if (l->t.v.len > 2) {
3076 // Prevent this:
3077 // >>> qwe=1
3078 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3079 // '
3080 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003081 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 +01003082 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003083
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003084 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003085}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003086#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003087
Denys Vlasenko26819db2018-12-12 16:08:46 +01003088static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003089{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003090 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003091
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003092 l->t.t = XC_LEX_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06003093
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003094 nls = 0;
3095 i = l->i;
3096 for (;;) {
3097 char c = l->buf[i];
3098 if (c == '\0') {
3099 l->i = i;
3100 RETURN_STATUS(bc_error("string end could not be found"));
3101 }
3102 if (c == '"')
3103 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003104 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003105 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003106 }
3107
3108 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003109 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3110 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3111 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003112 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003113 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003114 bc_vec_string(&l->t.v, len, l->buf + l->i);
3115
3116 l->i = i + 1;
3117 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003118 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003119
Denys Vlasenko26819db2018-12-12 16:08:46 +01003120 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003121}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003122#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003123
Denys Vlasenko0a238142018-12-14 16:48:34 +01003124static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003125{
3126 if (l->buf[l->i] == '=') {
3127 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003128 with_and_without >>= 8; // store "with" value
3129 } // else store "without" value
3130 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003131}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003132#define bc_lex_assign(l, with, without) \
3133 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003134
Denys Vlasenko29301232018-12-11 15:29:32 +01003135static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003136{
3137 size_t i, nls = 0;
3138 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003139
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003140 l->t.t = XC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003141 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003142 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003143 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003144 check_star:
3145 if (c == '*') {
3146 c = buf[++i];
3147 if (c == '/')
3148 break;
3149 goto check_star;
3150 }
3151 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003152 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003153 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003154 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003155 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 }
3157
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003158 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003159 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003160 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003161
Denys Vlasenko29301232018-12-11 15:29:32 +01003162 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003163}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003164#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003165
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003166#undef zbc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003167static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003168{
3169 BcStatus s = BC_STATUS_SUCCESS;
3170 char c = l->buf[l->i++], c2;
3171
3172 // This is the workhorse of the lexer.
3173 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003174// case '\0': // probably never reached
3175// l->i--;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003176// l->t.t = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003177// l->newline = true;
3178// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003179 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003180 l->t.t = XC_LEX_NLINE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003181 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003182 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003183 case '\t':
3184 case '\v':
3185 case '\f':
3186 case '\r':
3187 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003188 bc_lex_whitespace(l);
3189 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003190 case '!':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003191 bc_lex_assign(l, XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003192 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003193 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
3194 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003195 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003198 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 case '#':
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003201 s = zbc_POSIX_does_not_allow("'#' script comments");
3202 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003204 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 case '%':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003206 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003207 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003209 c2 = l->buf[l->i];
3210 if (c2 == '&') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003211 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3212 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 ++l->i;
3214 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003215 } else {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003216 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003217 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 case '(':
3221 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3223 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003224 case '*':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003225 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
Gavin Howard01055ba2018-11-03 11:00:21 -06003226 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003227 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003228 c2 = l->buf[l->i];
3229 if (c2 == '+') {
3230 ++l->i;
3231 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003232 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003233 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003234 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003235 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003236 l->t.t = BC_LEX_COMMA;
3237 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003238 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003239 c2 = l->buf[l->i];
3240 if (c2 == '-') {
3241 ++l->i;
3242 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003243 } else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003244 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003245 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003246 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003247 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003248 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003249 else {
3250 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003251 s = zbc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 }
3253 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003254 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003255 c2 = l->buf[l->i];
3256 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003257 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003258 else
Denys Vlasenko69560f42018-12-24 14:14:23 +01003259 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
Gavin Howard01055ba2018-11-03 11:00:21 -06003260 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003261 case '0':
3262 case '1':
3263 case '2':
3264 case '3':
3265 case '4':
3266 case '5':
3267 case '6':
3268 case '7':
3269 case '8':
3270 case '9':
3271 case 'A':
3272 case 'B':
3273 case 'C':
3274 case 'D':
3275 case 'E':
3276 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003277 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003278 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003280 l->t.t = BC_LEX_SCOLON;
3281 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003282 case '<':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003283 bc_lex_assign(l, XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003284 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 case '=':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003286 bc_lex_assign(l, XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06003287 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003288 case '>':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003289 bc_lex_assign(l, XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003290 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003291 case '[':
3292 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003293 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3294 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003295 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003296 if (l->buf[l->i] == '\n') {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003297 l->t.t = XC_LEX_WHITESPACE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003298 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003299 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003300 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003301 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003302 case '^':
Denys Vlasenko69560f42018-12-24 14:14:23 +01003303 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
Gavin Howard01055ba2018-11-03 11:00:21 -06003304 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003305 case 'a':
3306 case 'b':
3307 case 'c':
3308 case 'd':
3309 case 'e':
3310 case 'f':
3311 case 'g':
3312 case 'h':
3313 case 'i':
3314 case 'j':
3315 case 'k':
3316 case 'l':
3317 case 'm':
3318 case 'n':
3319 case 'o':
3320 case 'p':
3321 case 'q':
3322 case 'r':
3323 case 's':
3324 case 't':
3325 case 'u':
3326 case 'v':
3327 case 'w':
3328 case 'x':
3329 case 'y':
3330 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003331 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003332 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003333 case '{':
3334 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003335 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3336 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003337 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003338 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003339 if (c2 == '|') {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003340 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3341 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003342 ++l->i;
3343 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003344 } else {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003345 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003346 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003347 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003348 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003349 default:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003350 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003351 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003352 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003353 }
3354
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003355 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003356}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003357#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003358#endif // ENABLE_BC
3359
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003360#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003361static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003362{
Denys Vlasenko81293c82018-12-24 01:53:55 +01003363 if (G_exreg && isspace(l->buf[l->i])) {
3364 bc_lex_whitespace(l); // eats whitespace (but not newline)
3365 l->i++; // bc_lex_name() expects this
Denys Vlasenko29301232018-12-11 15:29:32 +01003366 bc_lex_name(l);
Denys Vlasenko81293c82018-12-24 01:53:55 +01003367 } else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003368 bc_vec_pop_all(&l->t.v);
Denys Vlasenko81293c82018-12-24 01:53:55 +01003369 bc_vec_push(&l->t.v, &l->buf[l->i++]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003370 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003371 l->t.t = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003372 }
3373
Denys Vlasenko29301232018-12-11 15:29:32 +01003374 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003375}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003376#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003377
Denys Vlasenko29301232018-12-11 15:29:32 +01003378static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003379{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003380 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003381
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003382 l->t.t = XC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003383 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003384
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003385 nls = 0;
3386 depth = 1;
3387 i = l->i;
3388 for (;;) {
3389 char c = l->buf[i];
3390 if (c == '\0') {
3391 l->i = i;
3392 RETURN_STATUS(bc_error("string end could not be found"));
3393 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003394 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003395 if (i == l->i || l->buf[i - 1] != '\\') {
3396 if (c == '[') depth++;
3397 if (c == ']')
3398 if (--depth == 0)
3399 break;
3400 }
3401 bc_vec_push(&l->t.v, &l->buf[i]);
3402 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003403 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003404 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003405
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003406 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003407 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3408 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3409 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003410 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003411 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003412
3413 l->i = i;
3414 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003415 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003416
Denys Vlasenko29301232018-12-11 15:29:32 +01003417 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003418}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003419#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003420
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003421#undef zdc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003422static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003423{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003424 static const //BcLexType - should be this type, but narrower type saves size:
3425 uint8_t
3426 dc_lex_regs[] = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003427 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 +01003428 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3429 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003430 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003431 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003432
Denys Vlasenko81293c82018-12-24 01:53:55 +01003433 BcStatus s;
3434 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003435 size_t i;
3436
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003437 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3438 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003439 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003440 }
3441
Denys Vlasenko81293c82018-12-24 01:53:55 +01003442 s = BC_STATUS_SUCCESS;
3443 c = l->buf[l->i++];
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003444 if (c >= '%' && c <= '~'
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003445 && (l->t.t = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003446 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003447 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003448 }
3449
3450 // This is the workhorse of the lexer.
3451 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003452// case '\0': // probably never reached
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003453// l->t.t = XC_LEX_EOF;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003454// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003455 case '\n':
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003456 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003457 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003458 // only to allow interactive dc have a way to exit
3459 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003460 // on <enter>, not on _next_ token (which would mean
3461 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003462 // IOW: typing "1p<enter>" should print "1" _at once_,
3463 // not after some more input.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003464 l->t.t = XC_LEX_NLINE;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003465 l->newline = true;
3466 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003467 case '\t':
3468 case '\v':
3469 case '\f':
3470 case '\r':
3471 case ' ':
Denys Vlasenko81293c82018-12-24 01:53:55 +01003472 l->newline = 0; // was (c == '\n')
Gavin Howard01055ba2018-11-03 11:00:21 -06003473 bc_lex_whitespace(l);
3474 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003475 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003476 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003477 if (c2 == '=')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003478 l->t.t = XC_LEX_OP_REL_NE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003479 else if (c2 == '<')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003480 l->t.t = XC_LEX_OP_REL_LE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003481 else if (c2 == '>')
Denys Vlasenko69560f42018-12-24 14:14:23 +01003482 l->t.t = XC_LEX_OP_REL_GE;
Gavin Howard01055ba2018-11-03 11:00:21 -06003483 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003484 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003485 ++l->i;
3486 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003487 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003488 bc_lex_lineComment(l);
3489 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003490 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003491 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003492 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003493 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003494 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003495 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003496 case '0':
3497 case '1':
3498 case '2':
3499 case '3':
3500 case '4':
3501 case '5':
3502 case '6':
3503 case '7':
3504 case '8':
3505 case '9':
3506 case 'A':
3507 case 'B':
3508 case 'C':
3509 case 'D':
3510 case 'E':
3511 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003512 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003513 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003514 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003515 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003516 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003517 default:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003518 l->t.t = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003519 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003520 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003521 }
3522
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003523 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003524}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003525#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003526#endif // ENABLE_DC
3527
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003528static void bc_parse_push(BcParse *p, char i)
3529{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003530 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003531 bc_vec_pushByte(&p->func->code, i);
3532}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003533
Gavin Howard01055ba2018-11-03 11:00:21 -06003534static void bc_parse_pushName(BcParse *p, char *name)
3535{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003536 while (*name)
3537 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003538 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003539}
3540
3541static void bc_parse_pushIndex(BcParse *p, size_t idx)
3542{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003543 size_t mask;
3544 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003545
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003546 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003547 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3548 amt = sizeof(idx);
3549 do {
3550 if (idx & mask) break;
3551 mask >>= 8;
3552 amt--;
3553 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003554
3555 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003556
3557 while (idx != 0) {
3558 bc_parse_push(p, (unsigned char)idx);
3559 idx >>= 8;
3560 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003561}
3562
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003563#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003564static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3565{
3566 bc_parse_push(p, BC_INST_JUMP);
3567 bc_parse_pushIndex(p, idx);
3568}
3569
3570static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3571{
3572 bc_parse_push(p, BC_INST_JUMP_ZERO);
3573 bc_parse_pushIndex(p, idx);
3574}
3575
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003576static BC_STATUS zbc_parse_pushSTR(BcParse *p)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003577{
3578 char *str = xstrdup(p->l.t.v.v);
3579
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003580 bc_parse_push(p, XC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003581 bc_parse_pushIndex(p, p->func->strs.len);
3582 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003583
3584 RETURN_STATUS(zbc_lex_next(&p->l));
3585}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003586#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003587#endif
3588
3589static void bc_parse_pushNUM(BcParse *p)
3590{
3591 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003592#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003593 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003594#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003595 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003596#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003597 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003598#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003599 bc_parse_push(p, XC_INST_NUM);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003600 bc_parse_pushIndex(p, idx);
3601}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003602
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003603static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003604{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003605 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003606
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003607 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003608}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003609#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003610
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003611// Called when parsing or execution detects a failure,
3612// resets execution structures.
3613static void bc_program_reset(void)
3614{
3615 BcFunc *f;
3616 BcInstPtr *ip;
3617
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003618 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003619 bc_vec_pop_all(&G.prog.results);
3620
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003621 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003622 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003623 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003624}
3625
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003626// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003627// resets parsing structures.
3628static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003629{
3630 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003631 bc_func_free(p->func);
3632 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003633
Denys Vlasenko65e10462018-12-19 15:13:14 +01003634 p->fidx = BC_PROG_MAIN;
3635 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003636 }
3637
3638 p->l.i = p->l.len;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003639 p->l.t.t = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003640
Denys Vlasenko503faf92018-12-20 16:24:18 +01003641 IF_BC(bc_vec_pop_all(&p->exits);)
3642 IF_BC(bc_vec_pop_all(&p->conds);)
3643 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003644
Denys Vlasenkod38af482018-12-04 19:11:02 +01003645 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003646}
3647
3648static void bc_parse_free(BcParse *p)
3649{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003650 IF_BC(bc_vec_free(&p->exits);)
3651 IF_BC(bc_vec_free(&p->conds);)
3652 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003653 bc_lex_free(&p->l);
3654}
3655
Denys Vlasenkofa210792018-12-19 19:35:40 +01003656static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003657{
3658 memset(p, 0, sizeof(BcParse));
3659
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003660 bc_lex_init(&p->l);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003661 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3662 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3663 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003664
Denys Vlasenkofa210792018-12-19 19:35:40 +01003665 p->fidx = fidx;
3666 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003667}
3668
Denys Vlasenko04715442018-12-21 00:10:26 +01003669static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003670{
Denys Vlasenko04715442018-12-21 00:10:26 +01003671 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003672 BcFunc f;
3673 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003674 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003675 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003676 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003677}
3678
3679#if ENABLE_BC
3680
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003681// Note: takes ownership of 'name' (must be malloced)
3682static size_t bc_program_addFunc(char *name)
3683{
3684 size_t idx;
3685 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003686 int inserted;
3687
3688 entry.name = name;
3689 entry.idx = G.prog.fns.len;
3690
3691 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3692 if (!inserted) free(name);
3693
3694 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3695 idx = entry_ptr->idx;
3696
3697 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003698 // There is already a function with this name.
3699 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003700 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003701 bc_func_free(func);
3702 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003703 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003704 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003705 }
3706
3707 return idx;
3708}
3709
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003710#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3711#define BC_PARSE_LEAF(p, rparen) \
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003712 (((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) || (rparen) || \
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003713 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3714
3715// We can calculate the conversion between tokens and exprs by subtracting the
3716// position of the first operator in the lex enum and adding the position of the
3717// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003718#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003719
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003720static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags);
3721
3722static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags)
3723{
3724 BcStatus s;
3725
3726 s = bc_parse_expr_empty_ok(p, flags);
3727 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3728 RETURN_STATUS(bc_error("empty expression"));
3729 RETURN_STATUS(s);
3730}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003731#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003732
3733static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003734#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003735
3736static BC_STATUS zbc_parse_stmt(BcParse *p)
3737{
3738 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3739}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003740#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003741
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003742static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003743{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003744 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3745 // Same for "else", "while()", "for()".
3746 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3747 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01003748 if (p->l.t.t == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003749 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003750
3751 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003752}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003753#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003754
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003755static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3756 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003757{
Denys Vlasenko69560f42018-12-24 14:14:23 +01003758 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3759 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003760
3761 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003762 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003763 if (t == BC_LEX_LPAREN) break;
3764
Denys Vlasenko69560f42018-12-24 14:14:23 +01003765 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003766 if (l >= r && (l != r || !left)) break;
3767
Denys Vlasenko0154d782018-12-14 23:32:51 +01003768 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003769 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003770 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003771 }
3772
3773 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003774}
3775
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003776static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003777{
3778 BcLexType top;
3779
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003780 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003781 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003782 top = BC_PARSE_TOP_OP(p);
3783
3784 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003785 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003786
3787 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003788 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003789
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003790 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003791 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003792 top = BC_PARSE_TOP_OP(p);
3793 }
3794
3795 bc_vec_pop(&p->ops);
3796
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003797 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003798}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003799#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003800
Denys Vlasenko26819db2018-12-12 16:08:46 +01003801static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003802{
3803 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003804 size_t nparams;
3805
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003806 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003807 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3808
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003809 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003810 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003811
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003812 nparams = 0;
3813 if (p->l.t.t != BC_LEX_RPAREN) {
3814 for (;;) {
3815 s = zbc_parse_expr(p, flags);
3816 if (s) RETURN_STATUS(s);
3817 nparams++;
3818 if (p->l.t.t != BC_LEX_COMMA) {
3819 if (p->l.t.t == BC_LEX_RPAREN)
3820 break;
3821 RETURN_STATUS(bc_error_bad_token());
3822 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003823 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003824 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003825 }
3826 }
3827
Gavin Howard01055ba2018-11-03 11:00:21 -06003828 bc_parse_push(p, BC_INST_CALL);
3829 bc_parse_pushIndex(p, nparams);
3830
Denys Vlasenko26819db2018-12-12 16:08:46 +01003831 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003832}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003833#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003834
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003835// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003836static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003837{
3838 BcStatus s;
3839 BcId entry, *entry_ptr;
3840 size_t idx;
3841
3842 entry.name = name;
3843
Denys Vlasenko26819db2018-12-12 16:08:46 +01003844 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003845 if (s) goto err;
3846
3847 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003848 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003849 goto err;
3850 }
3851
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003852 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003853
3854 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003855 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003856 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003857 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003858 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003859 free(name);
3860
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003861 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003862 bc_parse_pushIndex(p, entry_ptr->idx);
3863
Denys Vlasenko26819db2018-12-12 16:08:46 +01003864 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003865 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003866 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003867 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003868}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003869#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003870
Denys Vlasenko26819db2018-12-12 16:08:46 +01003871static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003872{
3873 BcStatus s;
3874 char *name;
3875
3876 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003877 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003878 if (s) goto err;
3879
3880 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003881 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003882 if (s) goto err;
3883
3884 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003886 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003887 goto err;
3888 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003889 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003890 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003891 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003892 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003893 s = zbc_parse_expr(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 if (s) goto err;
3895 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003896 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 if (s) goto err;
3898 bc_parse_push(p, *type);
3899 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003900 free(name);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003901 } else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003902 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003903 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003904 goto err;
3905 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003906 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003907 s = zbc_parse_call(p, name, flags);
3908 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003909 *type = XC_INST_VAR;
3910 bc_parse_push(p, XC_INST_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06003911 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003912 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003913 }
3914
Denys Vlasenko26819db2018-12-12 16:08:46 +01003915 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003916 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003917 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003918 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003919}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003920#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003921
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003922static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003923{
3924 BcStatus s;
3925
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003926 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003927 if (s) RETURN_STATUS(s);
3928 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003929
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003930 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003931 if (s) RETURN_STATUS(s);
3932 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003933
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003934 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003935
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003936 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003937}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003938#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003939
Denys Vlasenko26819db2018-12-12 16:08:46 +01003940static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003941 BcInst *prev)
3942{
3943 BcStatus s;
3944
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003945 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003946 if (s) RETURN_STATUS(s);
3947 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003948
3949 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3950
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003951 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003952 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003953
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003954 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003955 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003956
Denys Vlasenko26819db2018-12-12 16:08:46 +01003957 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003958
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003959 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Gavin Howard01055ba2018-11-03 11:00:21 -06003960 bc_parse_push(p, *prev);
3961
Denys Vlasenko26819db2018-12-12 16:08:46 +01003962 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003963}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003964#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003965
Denys Vlasenko26819db2018-12-12 16:08:46 +01003966static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003967{
3968 BcStatus s;
3969
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003970 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003971 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003972
3973 if (p->l.t.t != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003974 *type = XC_INST_SCALE;
3975 bc_parse_push(p, XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003976 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003977 }
3978
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003979 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003980 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3981
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003982 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003983 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003984
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003985 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003986 if (s) RETURN_STATUS(s);
3987 if (p->l.t.t != BC_LEX_RPAREN)
3988 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003989 bc_parse_push(p, XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003990
Denys Vlasenko26819db2018-12-12 16:08:46 +01003991 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003992}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003993#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003994
Denys Vlasenko26819db2018-12-12 16:08:46 +01003995static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003996 size_t *nexprs, uint8_t flags)
3997{
3998 BcStatus s;
3999 BcLexType type;
4000 char inst;
4001 BcInst etype = *prev;
4002
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004003 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
4004 || etype == XC_INST_SCALE || etype == BC_INST_LAST
4005 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004006 ) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004007 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
4008 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004009 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004010 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004011 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
4012 *paren_expr = true;
4013
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004014 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004015 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004016 type = p->l.t.t;
4017
4018 // Because we parse the next part of the expression
4019 // right here, we need to increment this.
4020 *nexprs = *nexprs + 1;
4021
4022 switch (type) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004023 case XC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004024 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004025 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004026 case BC_LEX_KEY_IBASE:
4027 case BC_LEX_KEY_LAST:
4028 case BC_LEX_KEY_OBASE:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004029 bc_parse_push(p, type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004030 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004031 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004032 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004033 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004034 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004035 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004036 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004037 else
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004038 bc_parse_push(p, XC_INST_SCALE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004039 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004040 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004041 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004042 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004043 }
4044
4045 if (!s) bc_parse_push(p, inst);
4046 }
4047
Denys Vlasenko26819db2018-12-12 16:08:46 +01004048 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004049}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004050#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004051
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004052static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004053 bool rparen, size_t *nexprs)
4054{
4055 BcStatus s;
4056 BcLexType type;
4057 BcInst etype = *prev;
4058
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004059 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004060 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004061
4062 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004063 (etype >= XC_INST_NUM && etype <= XC_INST_SQRT) ?
Denys Vlasenko69560f42018-12-24 14:14:23 +01004064 XC_LEX_OP_MINUS :
4065 XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004066 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004067
4068 // We can just push onto the op stack because this is the largest
4069 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004070 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004071 bc_vec_push(&p->ops, &type);
4072 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004073 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004074
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004075 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004076}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004077#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004078
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004079static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004080{
4081 BcStatus s;
4082 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004083
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004084 for (;;) {
4085 s = zbc_lex_next(&p->l);
4086 if (s) RETURN_STATUS(s);
4087 type = p->l.t.t;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004088 if (type == XC_LEX_STR) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004089 s = zbc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004090 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004091 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004092 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004093 if (s) RETURN_STATUS(s);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004094 bc_parse_push(p, XC_INST_PRINT_POP);
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004095 if (p->l.t.t != BC_LEX_COMMA)
4096 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004097 }
4098
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004099 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004100}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004101#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004102
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004103static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004104{
4105 BcStatus s;
4106 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004107
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004108 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004109 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004110 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004111
4112 t = p->l.t.t;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004113 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Gavin Howard01055ba2018-11-03 11:00:21 -06004114 bc_parse_push(p, BC_INST_RET0);
4115 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004116 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004117 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004118 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004119 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004120 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004121 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004122 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004123
4124 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004125 s = zbc_POSIX_requires("parentheses around return expressions");
4126 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004127 }
4128
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004129 bc_parse_push(p, XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004130 }
4131
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004132 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004133 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004134}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004135#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004136
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004137static void rewrite_label_to_current(BcParse *p, size_t idx)
4138{
4139 size_t *label = bc_vec_item(&p->func->labels, idx);
4140 *label = p->func->code.len;
4141}
4142
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004143static BC_STATUS zbc_parse_if(BcParse *p)
4144{
4145 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004146 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004147
4148 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4149 s = zbc_lex_next(&p->l);
4150 if (s) RETURN_STATUS(s);
4151 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
4152
4153 s = zbc_lex_next(&p->l);
4154 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004155 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004156 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004157 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004158
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004159 // Encode "if zero, jump to ..."
4160 // Pushed value (destination of the jump) is uninitialized,
4161 // will be rewritten to be address of "end of if()" or of "else".
4162 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004163 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004164
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004165 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004166 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004167
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004168 dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4169 if (p->l.t.t == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004170 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004171
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004172 // Encode "after then_stmt, jump to end of if()"
4173 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4174 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004175 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004176
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004177 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 +01004178 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004179
Denys Vlasenko15850832018-12-16 21:40:54 +01004180 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004181
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004182 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004183 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004184 }
4185
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004186 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 +01004187 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004188
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004189 dbg_lex_done("%s:%d done", __func__, __LINE__);
4190 RETURN_STATUS(s);
4191}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004192#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004193
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004194static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004195{
4196 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004197 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004198 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004199
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004200 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004201 if (s) RETURN_STATUS(s);
4202 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004203 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004204 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004205
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004206 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004207 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004208 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004209
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004210 bc_vec_push(&p->exits, &ip_idx);
4211 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004212
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004213 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004214 if (s) RETURN_STATUS(s);
4215 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004216
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004217 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004218
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004219 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004220 if (s) RETURN_STATUS(s);
4221
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004222 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004223 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004224
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004225 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004226 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004227
4228 bc_vec_pop(&p->exits);
4229 bc_vec_pop(&p->conds);
4230
4231 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004232}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004233#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004234
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004235static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004236{
4237 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004238 size_t cond_idx, exit_idx, body_idx, update_idx;
4239
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004240 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004241 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004242 if (s) RETURN_STATUS(s);
4243 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004244 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004245 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004246
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004247 if (p->l.t.t != BC_LEX_SCOLON) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004248 s = zbc_parse_expr(p, 0);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004249 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004250 if (s) RETURN_STATUS(s);
4251 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004252 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4253 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004254 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004255
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004256 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004257 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004258 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004259
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004260 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004261 update_idx = cond_idx + 1;
4262 body_idx = update_idx + 1;
4263 exit_idx = body_idx + 1;
4264
Gavin Howard01055ba2018-11-03 11:00:21 -06004265 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004266 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004267 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004268 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004269 // This is safe to set because the current token is a semicolon,
4270 // which has no string requirement.
4271 bc_vec_string(&p->l.t.v, 1, "1");
4272 bc_parse_pushNUM(p);
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004273 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004274 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004275 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004276
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004277 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004278
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004279 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004280 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004281
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004282 bc_parse_pushJUMP_ZERO(p, exit_idx);
4283 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004284
Gavin Howard01055ba2018-11-03 11:00:21 -06004285 bc_vec_push(&p->conds, &update_idx);
4286 bc_vec_push(&p->func->labels, &p->func->code.len);
4287
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004288 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004289 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004290 if (s) RETURN_STATUS(s);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004291 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004292 bc_parse_push(p, XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004293 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004294 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4295 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004296 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004297
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004298 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004299 bc_vec_push(&p->func->labels, &p->func->code.len);
4300
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004301 bc_vec_push(&p->exits, &exit_idx);
4302 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004303
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004304 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004305 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004306
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004307 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004308 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004309
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004310 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004311 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004312
4313 bc_vec_pop(&p->exits);
4314 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004315
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004316 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004317}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004318#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004319
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004320static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004321{
4322 BcStatus s;
4323 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004324
Gavin Howard01055ba2018-11-03 11:00:21 -06004325 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004326 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4327 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004328 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004329 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004330 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004331 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004332
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004333 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004334
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004335 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004336 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004337
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004338 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != XC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004339 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004340
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004341 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004342}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004343#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004344
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004345static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4346{
4347 BcId *autoid;
4348 BcId a;
4349 size_t i;
4350
4351 autoid = (void*)f->autos.v;
4352 for (i = 0; i < f->autos.len; i++, autoid++) {
4353 if (strcmp(name, autoid->name) == 0)
4354 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
4355 }
4356
4357 a.idx = var;
4358 a.name = name;
4359
4360 bc_vec_push(&f->autos, &a);
4361
4362 RETURN_STATUS(BC_STATUS_SUCCESS);
4363}
4364#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4365
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004366static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004367{
4368 BcStatus s;
4369 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004370 char *name;
4371
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004372 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004373 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004374 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004375 if (p->l.t.t != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004376 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004377
4378 name = xstrdup(p->l.t.v.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004379 p->fidx = bc_program_addFunc(name);
4380 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004381
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004382 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004383 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004384 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004385 RETURN_STATUS(bc_error("bad function definition"));
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);
Gavin Howard01055ba2018-11-03 11:00:21 -06004388
4389 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004390 if (p->l.t.t != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004391 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004392
4393 ++p->func->nparams;
4394
4395 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004396 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004397 if (s) goto err;
4398
4399 var = p->l.t.t != BC_LEX_LBRACKET;
4400
4401 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004402 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004403 if (s) goto err;
4404
4405 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004406 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004407 goto err;
4408 }
4409
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004410 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004411 if (s) goto err;
4412 }
4413
4414 comma = p->l.t.t == BC_LEX_COMMA;
4415 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004416 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004417 if (s) goto err;
4418 }
4419
Denys Vlasenko29301232018-12-11 15:29:32 +01004420 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004421 if (s) goto err;
4422 }
4423
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004424 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004425
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004426 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004427 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004428
4429 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004430 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004431
Denys Vlasenko202dd192018-12-16 17:30:35 +01004432 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4433 s = zbc_lex_skip_if_at_NLINE(&p->l);
4434 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004435 //GNU bc requires a {} block even if function body has single stmt, enforce this?
4436 if (p->l.t.t != BC_LEX_LBRACE)
4437 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004438
4439 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004440 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004441 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004442 if (s) RETURN_STATUS(s);
4443
4444 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004445
4446 // Subsequent code generation is into main program
4447 p->fidx = BC_PROG_MAIN;
4448 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004449
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004450 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004451 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004452 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004453 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004454 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004455 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004456}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004457#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004458
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004459static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004460{
4461 BcStatus s;
4462 bool comma, var, one;
4463 char *name;
4464
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004465 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004466 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004467 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004468
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004469 comma = false;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004470 one = p->l.t.t == XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06004471
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004472 while (p->l.t.t == XC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004473 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004474 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004475 if (s) goto err;
4476
4477 var = p->l.t.t != BC_LEX_LBRACKET;
4478 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004479 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004480 if (s) goto err;
4481
4482 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004483 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004484 goto err;
4485 }
4486
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004487 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004488 if (s) goto err;
4489 }
4490
4491 comma = p->l.t.t == BC_LEX_COMMA;
4492 if (comma) {
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;
4499 }
4500
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004501 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4502 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004503
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004504 if (p->l.t.t != XC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004505 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004506
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004507 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004508 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004509 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004510 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004511 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004512 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004513}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004514#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004515
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004516#undef zbc_parse_stmt_possibly_auto
4517static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004518{
4519 BcStatus s = BC_STATUS_SUCCESS;
4520
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004521 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004522
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004523 if (p->l.t.t == XC_LEX_NLINE) {
4524 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004525 RETURN_STATUS(zbc_lex_next(&p->l));
4526 }
4527 if (p->l.t.t == BC_LEX_SCOLON) {
4528 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4529 RETURN_STATUS(zbc_lex_next(&p->l));
4530 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004531
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004532 if (p->l.t.t == BC_LEX_LBRACE) {
4533 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4534 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004535 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004536 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004537 } while (p->l.t.t == XC_LEX_NLINE);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004538 if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) {
4539 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4540 s = zbc_parse_auto(p);
4541 if (s) RETURN_STATUS(s);
4542 }
4543 while (p->l.t.t != BC_LEX_RBRACE) {
4544 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4545 s = zbc_parse_stmt(p);
4546 if (s) RETURN_STATUS(s);
4547 }
4548 s = zbc_lex_next(&p->l);
4549 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4550 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004551 }
4552
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004553 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004554 switch (p->l.t.t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004555 case XC_LEX_OP_MINUS:
Gavin Howard01055ba2018-11-03 11:00:21 -06004556 case BC_LEX_OP_INC:
4557 case BC_LEX_OP_DEC:
Gavin Howard01055ba2018-11-03 11:00:21 -06004558 case BC_LEX_OP_BOOL_NOT:
4559 case BC_LEX_LPAREN:
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004560 case XC_LEX_NAME:
4561 case XC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06004562 case BC_LEX_KEY_IBASE:
4563 case BC_LEX_KEY_LAST:
4564 case BC_LEX_KEY_LENGTH:
4565 case BC_LEX_KEY_OBASE:
4566 case BC_LEX_KEY_READ:
4567 case BC_LEX_KEY_SCALE:
4568 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004569 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004570 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004571 case XC_LEX_STR:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004572 s = zbc_parse_pushSTR(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004573 bc_parse_push(p, XC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004574 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 case BC_LEX_KEY_BREAK:
4576 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004577 s = zbc_parse_break_or_continue(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004578 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004579 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004580 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004581 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004582 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004584 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004585 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004586 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004587 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004588 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004589 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004590 // "limits" is a compile-time command,
4591 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004592 printf(
4593 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4594 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4595 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4596 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4597 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4598 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4599 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4600 "Number of vars = "BC_MAX_VARS_STR "\n"
4601 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004602 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004603 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004604 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004605 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004606 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004607 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004608 // "quit" is a compile-time command. For example,
4609 // "if (0 == 1) quit" terminates when parsing the statement,
4610 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004611 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004612 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004613 if (!p->in_funcdef)
4614 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004615 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004616 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004617 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004618 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004619 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004620 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004621 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004622 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004623 }
4624
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004625 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004626 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004627}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004628#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004629
Denys Vlasenko99b37622018-12-15 20:06:59 +01004630static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004631{
4632 BcStatus s;
4633
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004634 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004635 if (p->l.t.t == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004636 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004637 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004638 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004639 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004640 } else {
4641 dbg_lex("%s:%d p->l.t.t:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.t.t);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004642 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004643 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004644
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004645 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004646 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004647}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004648#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004649
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004650// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004651static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004652{
4653 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004654 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004655 BcLexType top, t = p->l.t.t;
4656 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004657 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004658 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4659
Denys Vlasenko17df8822018-12-14 23:00:24 +01004660 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004661 paren_first = (p->l.t.t == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004662 nparens = nrelops = 0;
4663 paren_expr = rprn = done = get_token = assign = false;
4664 bin_last = true;
4665
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004666 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004667 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004668 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004669 case BC_LEX_OP_INC:
4670 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004671 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004672 rprn = get_token = bin_last = false;
4673 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004674 case XC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004675 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004676 rprn = get_token = false;
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004677 bin_last = (prev == XC_INST_MINUS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004678 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004679 case BC_LEX_OP_ASSIGN_POWER:
4680 case BC_LEX_OP_ASSIGN_MULTIPLY:
4681 case BC_LEX_OP_ASSIGN_DIVIDE:
4682 case BC_LEX_OP_ASSIGN_MODULUS:
4683 case BC_LEX_OP_ASSIGN_PLUS:
4684 case BC_LEX_OP_ASSIGN_MINUS:
4685 case BC_LEX_OP_ASSIGN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004686 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4687 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4688 && prev != XC_INST_OBASE && prev != BC_INST_LAST
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004689 ) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004690 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004691 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004692 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004693 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004694 break;
4695 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004696 // Fallthrough.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004697 case XC_LEX_OP_POWER:
4698 case XC_LEX_OP_MULTIPLY:
4699 case XC_LEX_OP_DIVIDE:
4700 case XC_LEX_OP_MODULUS:
4701 case XC_LEX_OP_PLUS:
4702 case XC_LEX_OP_REL_EQ:
4703 case XC_LEX_OP_REL_LE:
4704 case XC_LEX_OP_REL_GE:
4705 case XC_LEX_OP_REL_NE:
4706 case XC_LEX_OP_REL_LT:
4707 case XC_LEX_OP_REL_GT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004708 case BC_LEX_OP_BOOL_NOT:
4709 case BC_LEX_OP_BOOL_OR:
4710 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004711 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004712 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004713 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004714 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004715 }
Denys Vlasenko69560f42018-12-24 14:14:23 +01004716 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
Denys Vlasenko0154d782018-12-14 23:32:51 +01004717 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004718 bc_parse_operator(p, t, ops_bgn, &nexprs);
4719 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004720 rprn = get_token = false;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004721 bin_last = (t != BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004722 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004723 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004724 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004725 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004726 ++nparens;
4727 paren_expr = rprn = bin_last = false;
4728 get_token = true;
4729 bc_vec_push(&p->ops, &t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004730 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004731 case BC_LEX_RPAREN:
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004732 if (bin_last || prev == XC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004733 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004734 if (nparens == 0) {
4735 s = BC_STATUS_SUCCESS;
4736 done = true;
4737 get_token = false;
4738 break;
4739 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004740 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004741 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004742 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004743 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004744 --nparens;
4745 paren_expr = rprn = true;
4746 get_token = bin_last = false;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004747 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004748 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004749 case XC_LEX_NAME:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004750 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004751 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004752 paren_expr = true;
4753 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004754 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004755 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004756 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004757 case XC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004758 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004759 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004760 bc_parse_pushNUM(p);
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004761 nexprs++;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004762 prev = XC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004763 paren_expr = get_token = true;
4764 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004765 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004766 case BC_LEX_KEY_IBASE:
4767 case BC_LEX_KEY_LAST:
4768 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004769 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004770 return bc_error_bad_expression();
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004771 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06004772 bc_parse_push(p, (char) prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004773 paren_expr = get_token = true;
4774 rprn = bin_last = false;
4775 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004776 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004777 case BC_LEX_KEY_LENGTH:
4778 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004779 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004780 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004781 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004782 paren_expr = true;
4783 rprn = get_token = bin_last = false;
4784 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004785 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004786 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004787 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004788 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004789 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004790 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004791 paren_expr = true;
4792 rprn = get_token = bin_last = false;
4793 ++nexprs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004794 prev = XC_INST_READ;
Gavin Howard01055ba2018-11-03 11:00:21 -06004795 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004796 case BC_LEX_KEY_SCALE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004797 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004798 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004799 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004800 paren_expr = true;
4801 rprn = get_token = bin_last = false;
4802 ++nexprs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004803 prev = XC_INST_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06004804 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004805 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004806 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004807 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004808 }
4809
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004810 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004811 }
4812
4813 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004814 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004815
4816 while (p->ops.len > ops_bgn) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004817 top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004818 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004819
4820 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004821 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004822
Denys Vlasenko0154d782018-12-14 23:32:51 +01004823 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004824
Denys Vlasenko69560f42018-12-24 14:14:23 +01004825 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004826 bc_vec_pop(&p->ops);
4827 }
4828
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004829 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004830 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004831
Gavin Howard01055ba2018-11-03 11:00:21 -06004832 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004833 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4834 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004835 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004836 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4837 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004838 }
4839
4840 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004841 if (paren_first || !assign) bc_parse_push(p, XC_INST_PRINT);
4842 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004843 }
4844
Denys Vlasenko17df8822018-12-14 23:00:24 +01004845 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004846 return s;
4847}
4848
Gavin Howard01055ba2018-11-03 11:00:21 -06004849#endif // ENABLE_BC
4850
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004851#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004852
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004853static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004854{
4855 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004856
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004857 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004858 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004859 if (p->l.t.t != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004860
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01004861 bc_parse_pushName(p, p->l.t.v.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004862
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004863 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004864}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004865#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004866
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004867static void dc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004868{
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004869 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004870 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004871
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004872 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004873
4874 str = xstrdup(p->l.t.v.v);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004875 bc_parse_push(p, XC_INST_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004876 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004877 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004878
4879 // Explanation needed here
4880 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004881 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004882
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004883 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004884}
4885
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004886static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004887{
4888 BcStatus s;
4889
4890 bc_parse_push(p, inst);
4891 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004892 s = zdc_parse_register(p);
4893 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004894 }
4895
4896 if (store) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004897 bc_parse_push(p, DC_INST_SWAP);
4898 bc_parse_push(p, XC_INST_ASSIGN);
4899 bc_parse_push(p, XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004900 }
4901
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004902 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004903}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004904#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004905
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004906static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004907{
4908 BcStatus s;
4909
4910 bc_parse_push(p, inst);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004911 bc_parse_push(p, DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004912
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004913 s = zdc_parse_register(p);
4914 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004915
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004916 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004917 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004918
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004919 // Note that 'else' part can not be on the next line:
4920 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4921 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004922 if (p->l.t.t == DC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004923 s = zdc_parse_register(p);
4924 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004925 s = zbc_lex_next(&p->l);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004926 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004927 bc_parse_push(p, BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004928 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004929
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004930 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004931}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004932#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004933
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004934static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004935{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004936 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004937 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004938 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004939
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004940 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004941 s = BC_STATUS_SUCCESS;
4942 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004943 switch (t) {
Denys Vlasenko69560f42018-12-24 14:14:23 +01004944 case XC_LEX_OP_REL_EQ:
4945 case XC_LEX_OP_REL_LE:
4946 case XC_LEX_OP_REL_GE:
4947 case XC_LEX_OP_REL_NE:
4948 case XC_LEX_OP_REL_LT:
4949 case XC_LEX_OP_REL_GT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004950 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
Denys Vlasenko69560f42018-12-24 14:14:23 +01004951 s = zdc_parse_cond(p, t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004952 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004953 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004954 case DC_LEX_SCOLON:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004955 case DC_LEX_COLON:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004956 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004957 s = zdc_parse_mem(p, XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004958 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004959 case XC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004960 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004961 dc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004962 break;
Denys Vlasenko69560f42018-12-24 14:14:23 +01004963 case XC_LEX_NEG:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004964 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
4965 s = zbc_lex_next(&p->l);
4966 if (s) RETURN_STATUS(s);
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004967 if (p->l.t.t != XC_LEX_NUMBER)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004968 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004969 bc_parse_pushNUM(p);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004970 bc_parse_push(p, XC_INST_NEG);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004971 break;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004972 case XC_LEX_NUMBER:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004973 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4974 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004975 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004976 case DC_LEX_READ:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004977 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004978 bc_parse_push(p, XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004979 break;
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004980 case DC_LEX_OP_ASSIGN:
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004981 case DC_LEX_STORE_PUSH:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004982 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +01004983 assign = (t == DC_LEX_OP_ASSIGN);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004984 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004985 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004986 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004987 case DC_LEX_LOAD:
4988 case DC_LEX_LOAD_POP:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004989 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004990 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004991 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004992 break;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004993 case DC_LEX_STORE_IBASE:
4994 case DC_LEX_STORE_SCALE:
4995 case DC_LEX_STORE_OBASE:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01004996 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01004997 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004998 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004999 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005000 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01005001 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
5002 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005003 }
5004
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005005 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005006
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005007 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005008 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005009}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005010#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005011
Denys Vlasenko39287e02018-12-22 02:23:08 +01005012static BC_STATUS zdc_parse_expr(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005013{
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005014 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005015
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005016 i = (int)p->l.t.t - (int)XC_LEX_OP_POWER;
5017 if (i >= 0) {
5018 BcInst inst = dc_LEX_to_INST[i];
5019 if (inst != DC_INST_INVALID) {
5020 bc_parse_push(p, inst);
5021 RETURN_STATUS(zbc_lex_next(&p->l));
5022 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005023 }
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01005024 RETURN_STATUS(zdc_parse_token(p, p->l.t.t));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005025}
5026#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
5027
5028static BC_STATUS zdc_parse_exprs_until_eof(BcParse *p)
5029{
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01005030 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01005031 while (p->l.t.t != XC_LEX_EOF) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005032 BcStatus s = zdc_parse_expr(p);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005033 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005034 }
5035
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005036 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005037 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005038}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005039#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005040
Gavin Howard01055ba2018-11-03 11:00:21 -06005041#endif // ENABLE_DC
5042
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005043//
5044// Execution engine
5045//
5046
5047#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5048#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5049
Denys Vlasenkodf515392018-12-02 19:27:48 +01005050static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005051{
Gavin Howard01055ba2018-11-03 11:00:21 -06005052 BcId e, *ptr;
5053 BcVec *v, *map;
5054 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005055 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005056
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005057 v = var ? &G.prog.vars : &G.prog.arrs;
5058 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005059
5060 e.name = id;
5061 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005062 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005063
5064 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005065 BcVec v2;
5066 bc_array_init(&v2, var);
5067 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005068 }
5069
5070 ptr = bc_vec_item(map, i);
5071 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005072 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005073}
5074
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005075// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005076static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005077{
Gavin Howard01055ba2018-11-03 11:00:21 -06005078 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005079 case BC_RESULT_STR:
5080 case BC_RESULT_TEMP:
5081 case BC_RESULT_IBASE:
5082 case BC_RESULT_SCALE:
5083 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005084 *num = &r->d.n;
5085 break;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005086 case BC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005087 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005088 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005089 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005090 size_t len;
5091
5092 str = *bc_program_const(r->d.id.idx);
5093 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005094
5095 bc_num_init(&r->d.n, len);
5096
5097 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005098 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005099 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005100 if (s) {
5101 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005102 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005103 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005104 *num = &r->d.n;
5105 r->t = BC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005106 break;
5107 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005108 case BC_RESULT_VAR:
5109 case BC_RESULT_ARRAY:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005110 case BC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005111 BcVec *v;
5112
Denys Vlasenkodf515392018-12-02 19:27:48 +01005113 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005114
5115 if (r->t == BC_RESULT_ARRAY_ELEM) {
5116 v = bc_vec_top(v);
5117 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5118 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005119 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005120 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005121 break;
5122 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005123#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005124 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005125 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005126 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005127 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005128 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005129 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005130#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005131#if SANITY_CHECKS
Denys Vlasenko503faf92018-12-20 16:24:18 +01005132 default:
5133 // Testing the theory that dc does not reach LAST/ONE
5134 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005135#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005136 }
5137
Denys Vlasenko29301232018-12-11 15:29:32 +01005138 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005139}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005140#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005141
Denys Vlasenko29301232018-12-11 15:29:32 +01005142static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005143 BcResult **r, BcNum **rn, bool assign)
5144{
5145 BcStatus s;
5146 bool hex;
5147 BcResultType lt, rt;
5148
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005149 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005150 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005151
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005152 *r = bc_vec_item_rev(&G.prog.results, 0);
5153 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005154
5155 lt = (*l)->t;
5156 rt = (*r)->t;
5157 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5158
Denys Vlasenko29301232018-12-11 15:29:32 +01005159 s = zbc_program_num(*l, ln, false);
5160 if (s) RETURN_STATUS(s);
5161 s = zbc_program_num(*r, rn, hex);
5162 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005163
5164 // We run this again under these conditions in case any vector has been
5165 // reallocated out from under the BcNums or arrays we had.
5166 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005167 s = zbc_program_num(*l, ln, false);
5168 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005169 }
5170
5171 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005172 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005173 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005174 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005175
Denys Vlasenko29301232018-12-11 15:29:32 +01005176 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005177}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005178#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005179
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005180static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005181{
5182 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005183 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005184 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005185}
5186
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005187// Note: *r and *n need not be initialized by caller
Denys Vlasenko29301232018-12-11 15:29:32 +01005188static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005189{
5190 BcStatus s;
5191
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005192 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005193 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005194 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005195
Denys Vlasenko29301232018-12-11 15:29:32 +01005196 s = zbc_program_num(*r, n, false);
5197 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005198
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005199 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005200 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005201
Denys Vlasenko29301232018-12-11 15:29:32 +01005202 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005203}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005204#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005205
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005206static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005207{
5208 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005209 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005210}
5211
Denys Vlasenko259137d2018-12-11 19:42:05 +01005212static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005213{
5214 BcStatus s;
5215 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005216 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005217
Denys Vlasenko29301232018-12-11 15:29:32 +01005218 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005219 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005220 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005221
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005222 s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005223 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 -06005224 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005225 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005226
Denys Vlasenko259137d2018-12-11 19:42:05 +01005227 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005228 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005229 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005230 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005231}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005232#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005233
Denys Vlasenko26819db2018-12-12 16:08:46 +01005234static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005235{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005236 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005237 BcStatus s;
5238 BcParse parse;
5239 BcVec buf;
5240 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005241 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005242
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005243 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005244 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005245
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005246 sv_file = G.prog.file;
5247 G.prog.file = NULL;
5248
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005249 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005250 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005251
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005252 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005253 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005254
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005255 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005256 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005257 if (IS_BC) {
5258 IF_BC(s = zbc_parse_expr(&parse, 0));
5259 } else {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005260 IF_DC(s = zdc_parse_exprs_until_eof(&parse));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005261 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005262 if (s) goto exec_err;
5263
Denys Vlasenko23ea0732018-12-24 15:05:49 +01005264 if (parse.l.t.t != XC_LEX_NLINE && parse.l.t.t != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005265 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005266 goto exec_err;
5267 }
5268
5269 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005270 ip.inst_idx = 0;
5271 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005272
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005273 bc_parse_push(&parse, XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005274 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005275 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005276 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005277 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005278 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005279 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005280}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005281#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005282
5283static size_t bc_program_index(char *code, size_t *bgn)
5284{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005285 unsigned char *bytes = (void*)(code + *bgn);
5286 unsigned amt;
5287 unsigned i;
5288 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005289
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005290 amt = *bytes++;
5291 *bgn += amt + 1;
5292
5293 amt *= 8;
5294 res = 0;
5295 for (i = 0; i < amt; i += 8)
5296 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005297
5298 return res;
5299}
5300
5301static char *bc_program_name(char *code, size_t *bgn)
5302{
5303 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005304 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005305
Denys Vlasenko694d2982018-12-18 19:17:11 +01005306 code += *bgn;
5307 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005308 i = 0;
5309 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005310 char c = *code++;
5311 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005312 break;
5313 s[i++] = c;
5314 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005315 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005316 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005317
5318 return s;
5319}
5320
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005321static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005322{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005323#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005324 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005325 // Example: echo '[]ap' | dc
5326 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005327 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005328 return;
5329 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005330#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005331 while (*str) {
5332 int c = *str++;
5333 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005334 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005335 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005336 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005337 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005338 case 'a':
5339 bb_putchar('\a');
5340 break;
5341 case 'b':
5342 bb_putchar('\b');
5343 break;
5344 case '\\':
5345 case 'e':
5346 bb_putchar('\\');
5347 break;
5348 case 'f':
5349 bb_putchar('\f');
5350 break;
5351 case 'n':
5352 bb_putchar('\n');
5353 G.prog.nchars = SIZE_MAX;
5354 break;
5355 case 'r':
5356 bb_putchar('\r');
5357 break;
5358 case 'q':
5359 bb_putchar('"');
5360 break;
5361 case 't':
5362 bb_putchar('\t');
5363 break;
5364 default:
5365 // Just print the backslash and following character.
5366 bb_putchar('\\');
5367 ++G.prog.nchars;
5368 bb_putchar(c);
5369 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005370 }
5371 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005372 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005373 }
5374}
5375
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005376static void bc_num_printNewline(void)
5377{
5378 if (G.prog.nchars == G.prog.len - 1) {
5379 bb_putchar('\\');
5380 bb_putchar('\n');
5381 G.prog.nchars = 0;
5382 }
5383}
5384
5385#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005386static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005387{
5388 (void) radix;
5389 bb_putchar((char) num);
5390 G.prog.nchars += width;
5391}
5392#endif
5393
5394static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5395{
5396 size_t exp, pow;
5397
5398 bc_num_printNewline();
5399 bb_putchar(radix ? '.' : ' ');
5400 ++G.prog.nchars;
5401
5402 bc_num_printNewline();
5403 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5404 continue;
5405
5406 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5407 size_t dig;
5408 bc_num_printNewline();
5409 dig = num / pow;
5410 num -= dig * pow;
5411 bb_putchar(((char) dig) + '0');
5412 }
5413}
5414
5415static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5416{
5417 if (radix) {
5418 bc_num_printNewline();
5419 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005420 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005421 }
5422
5423 bc_num_printNewline();
5424 bb_putchar(bb_hexdigits_upcase[num]);
5425 G.prog.nchars += width;
5426}
5427
5428static void bc_num_printDecimal(BcNum *n)
5429{
5430 size_t i, rdx = n->rdx - 1;
5431
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005432 if (n->neg) {
5433 bb_putchar('-');
5434 G.prog.nchars++;
5435 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005436
5437 for (i = n->len - 1; i < n->len; --i)
5438 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5439}
5440
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005441typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5442
Denys Vlasenkod3401432018-12-18 17:00:35 +01005443static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005444{
5445 BcStatus s;
5446 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005447 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005448 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005449 BcNum intp, fracp, digit, frac_len;
5450 unsigned long dig, *ptr;
5451 size_t i;
5452 bool radix;
5453
5454 if (n->len == 0) {
5455 print(0, width, false);
5456 RETURN_STATUS(BC_STATUS_SUCCESS);
5457 }
5458
5459 bc_vec_init(&stack, sizeof(long), NULL);
5460 bc_num_init(&intp, n->len);
5461 bc_num_init(&fracp, n->rdx);
5462 bc_num_init(&digit, width);
5463 bc_num_init(&frac_len, BC_NUM_INT(n));
5464 bc_num_copy(&intp, n);
5465 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005466 base.cap = ARRAY_SIZE(base_digs);
5467 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005468 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005469
5470 bc_num_truncate(&intp, intp.rdx);
5471 s = zbc_num_sub(n, &intp, &fracp, 0);
5472 if (s) goto err;
5473
5474 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005475 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005476 if (s) goto err;
5477 s = zbc_num_ulong(&digit, &dig);
5478 if (s) goto err;
5479 bc_vec_push(&stack, &dig);
5480 }
5481
5482 for (i = 0; i < stack.len; ++i) {
5483 ptr = bc_vec_item_rev(&stack, i);
5484 print(*ptr, width, false);
5485 }
5486
5487 if (!n->rdx) goto err;
5488
5489 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005490 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005491 if (s) goto err;
5492 s = zbc_num_ulong(&fracp, &dig);
5493 if (s) goto err;
5494 bc_num_ulong2num(&intp, dig);
5495 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5496 if (s) goto err;
5497 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005498 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005499 if (s) goto err;
5500 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005501 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005502 bc_num_free(&frac_len);
5503 bc_num_free(&digit);
5504 bc_num_free(&fracp);
5505 bc_num_free(&intp);
5506 bc_vec_free(&stack);
5507 RETURN_STATUS(s);
5508}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005509#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005510
5511static BC_STATUS zbc_num_printBase(BcNum *n)
5512{
5513 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005514 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005515 BcNumDigitOp print;
5516 bool neg = n->neg;
5517
5518 if (neg) {
5519 bb_putchar('-');
5520 G.prog.nchars++;
5521 }
5522
5523 n->neg = false;
5524
5525 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5526 width = 1;
5527 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005528 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005529 unsigned i = G.prog.ob_t - 1;
5530 width = 0;
5531 for (;;) {
5532 width++;
5533 i /= 10;
5534 if (i == 0)
5535 break;
5536 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005537 print = bc_num_printDigits;
5538 }
5539
Denys Vlasenkod3401432018-12-18 17:00:35 +01005540 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005541 n->neg = neg;
5542
5543 RETURN_STATUS(s);
5544}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005545#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005546
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005547static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5548{
5549 BcStatus s = BC_STATUS_SUCCESS;
5550
5551 bc_num_printNewline();
5552
5553 if (n->len == 0) {
5554 bb_putchar('0');
5555 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005556 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005557 bc_num_printDecimal(n);
5558 else
5559 s = zbc_num_printBase(n);
5560
5561 if (newline) {
5562 bb_putchar('\n');
5563 G.prog.nchars = 0;
5564 }
5565
5566 RETURN_STATUS(s);
5567}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005568#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005569
Denys Vlasenko29301232018-12-11 15:29:32 +01005570static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005571{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005572 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005573 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005574 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005575 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005576
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005577 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005578 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005579
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005580 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005581 s = zbc_program_num(r, &num, false);
5582 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005583
5584 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005585 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005586#if ENABLE_BC
5587 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5588#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005589 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005590 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005591
5592 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005593 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005594
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005595 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005596 for (;;) {
5597 char c = *str++;
5598 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005599 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005600 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005601 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005602 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005603 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005604 bc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005605 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005606 }
5607 }
5608
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005609 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005610
Denys Vlasenko29301232018-12-11 15:29:32 +01005611 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005612}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005613#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005614
Denys Vlasenko29301232018-12-11 15:29:32 +01005615static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005616{
5617 BcStatus s;
5618 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005619 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005620
Denys Vlasenko29301232018-12-11 15:29:32 +01005621 s = zbc_program_prep(&ptr, &num);
5622 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005623
5624 bc_num_init(&res.d.n, num->len);
5625 bc_num_copy(&res.d.n, num);
5626 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5627
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005628 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005629
Denys Vlasenko29301232018-12-11 15:29:32 +01005630 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005631}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005632#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005633
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005634static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005635{
5636 BcStatus s;
5637 BcResult *opd1, *opd2, res;
5638 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005639 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005640
Denys Vlasenko29301232018-12-11 15:29:32 +01005641 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005642 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005643
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005644 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005645
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005646 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005647 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005648 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005649 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005650 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005651 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005652 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005653 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005654 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005655 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005656 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005657 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005658 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005659 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005660 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005661 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005662 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005663 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005664 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005665 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005666 cond = (cond > 0);
5667 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005668 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005669 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005670 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005671 }
5672 }
5673
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005674 if (cond) bc_num_one(&res.d.n);
5675 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005676
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005677 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005678
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005679 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005680}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005681#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005682
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005683#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005684static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005685{
5686 BcNum n2;
5687 BcResult res;
5688
5689 memset(&n2, 0, sizeof(BcNum));
5690 n2.rdx = res.d.id.idx = r->d.id.idx;
5691 res.t = BC_RESULT_STR;
5692
5693 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005694 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005695 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005696 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005697 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005698 }
5699
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005700 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005701 bc_vec_push(v, &n2);
5702
Denys Vlasenko29301232018-12-11 15:29:32 +01005703 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005704}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005705#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005706#endif // ENABLE_DC
5707
Denys Vlasenko29301232018-12-11 15:29:32 +01005708static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005709{
5710 BcStatus s;
5711 BcResult *ptr, r;
5712 BcVec *v;
5713 BcNum *n;
5714
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005715 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005716 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005717
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005718 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005719 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005720 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005721 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005722
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005723#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005724 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005725 RETURN_STATUS(bc_error_variable_is_wrong_type());
5726 if (ptr->t == BC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005727 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005728#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005729
Denys Vlasenko29301232018-12-11 15:29:32 +01005730 s = zbc_program_num(ptr, &n, false);
5731 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005732
5733 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005734 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005735
5736 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005737 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005738 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005739 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005740 bc_array_init(&r.d.v, true);
5741 bc_array_copy(&r.d.v, (BcVec *) n);
5742 }
5743
5744 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005745 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005746
Denys Vlasenko29301232018-12-11 15:29:32 +01005747 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005748}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005749#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005750
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005751static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005752{
5753 BcStatus s;
5754 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005755 BcNum *l, *r;
5756 bool assign = (inst == XC_INST_ASSIGN);
5757 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005758
Denys Vlasenko29301232018-12-11 15:29:32 +01005759 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005760 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005761
5762 ib = left->t == BC_RESULT_IBASE;
5763 sc = left->t == BC_RESULT_SCALE;
5764
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005765#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005766 if (right->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005767 BcVec *v;
5768
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005769 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005770 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005771 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005772
Denys Vlasenkofa210792018-12-19 19:35:40 +01005773 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005774 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005775#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005776
5777 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005778 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005779 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005780 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005781 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005782
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005783#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005784 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005785 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005786
5787 if (assign)
5788 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005789 else {
5790 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005791 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 +01005792 }
5793 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005794#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005795 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005796#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005797
5798 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005799 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005800 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
Denys Vlasenko64074a12018-12-07 15:50:14 +01005801 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005802 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005803 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005804 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005805 size_t max;
5806 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005807
Denys Vlasenko29301232018-12-11 15:29:32 +01005808 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005809 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005810 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005811 if (sc) {
5812 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005813 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005814 } else {
5815 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005816 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005817 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005818 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005819 }
5820
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005821 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005822 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005823
5824 *ptr = (size_t) val;
5825 s = BC_STATUS_SUCCESS;
5826 }
5827
5828 bc_num_init(&res.d.n, l->len);
5829 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005830 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005831
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005832 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005833}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005834#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005835
Denys Vlasenko416ce762018-12-02 20:57:17 +01005836#if !ENABLE_DC
5837#define bc_program_pushVar(code, bgn, pop, copy) \
5838 bc_program_pushVar(code, bgn)
5839// for bc, 'pop' and 'copy' are always false
5840#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005841static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005842 bool pop, bool copy)
5843{
Gavin Howard01055ba2018-11-03 11:00:21 -06005844 BcResult r;
5845 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005846
5847 r.t = BC_RESULT_VAR;
5848 r.d.id.name = name;
5849
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005850#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005851 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005852 BcVec *v = bc_program_search(name, true);
5853 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005854
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005855 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005856 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005857 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005858 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005859
5860 if (!BC_PROG_STR(num)) {
5861 r.t = BC_RESULT_TEMP;
5862 bc_num_init_DEF_SIZE(&r.d.n);
5863 bc_num_copy(&r.d.n, num);
5864 } else {
5865 r.t = BC_RESULT_STR;
5866 r.d.id.idx = num->rdx;
5867 }
5868
5869 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005870 }
5871#endif // ENABLE_DC
5872
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005873 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005874
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005875 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005876}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005877#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005878
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005879static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005880{
5881 BcStatus s = BC_STATUS_SUCCESS;
5882 BcResult r;
5883 BcNum *num;
5884
5885 r.d.id.name = bc_program_name(code, bgn);
5886
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005887 if (inst == XC_INST_ARRAY) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005888 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005889 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005890 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005891 BcResult *operand;
5892 unsigned long temp;
5893
Denys Vlasenko29301232018-12-11 15:29:32 +01005894 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005895 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005896 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005897 if (s) goto err;
5898
5899 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005900 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005901 goto err;
5902 }
5903
5904 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005905 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005906 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005907 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005908 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005909 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005910}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005911#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005912
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005913#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005914static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005915{
5916 BcStatus s;
5917 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005918 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005919 char inst2 = inst;
5920
Denys Vlasenko29301232018-12-11 15:29:32 +01005921 s = zbc_program_prep(&ptr, &num);
5922 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005923
5924 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5925 copy.t = BC_RESULT_TEMP;
5926 bc_num_init(&copy.d.n, num->len);
5927 bc_num_copy(&copy.d.n, num);
5928 }
5929
5930 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005931 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5932 ? BC_INST_ASSIGN_PLUS
5933 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005934
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005935 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005936 s = zbc_program_assign(inst);
5937 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005938
5939 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005940 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005941 }
5942
Denys Vlasenko29301232018-12-11 15:29:32 +01005943 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005944}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005945#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005946
Denys Vlasenko29301232018-12-11 15:29:32 +01005947static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005948{
Gavin Howard01055ba2018-11-03 11:00:21 -06005949 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005950 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005951 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005952 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005953 BcResult *arg;
5954
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005955 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005956 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005957 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005958 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005959
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005960 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005961 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005962 }
5963 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005964 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005965 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005966 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005967
5968 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005969 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005970
5971 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005972 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005973
5974 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005975 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005976
Denys Vlasenko29301232018-12-11 15:29:32 +01005977 s = zbc_program_copyToVar(a->name, a->idx);
5978 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005979 }
5980
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005981 a = bc_vec_item(&func->autos, i);
5982 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005983 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005984
Denys Vlasenkodf515392018-12-02 19:27:48 +01005985 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005986 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005987 BcNum n2;
5988 bc_num_init_DEF_SIZE(&n2);
5989 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005990 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005991 BcVec v2;
5992 bc_array_init(&v2, true);
5993 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005994 }
5995 }
5996
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005997 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005998
Denys Vlasenko29301232018-12-11 15:29:32 +01005999 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006000}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006001#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006002
Denys Vlasenko29301232018-12-11 15:29:32 +01006003static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006004{
Gavin Howard01055ba2018-11-03 11:00:21 -06006005 BcResult res;
6006 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006007 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006008 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006009 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006010
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006011 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 +01006012 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006013
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006014 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006015 res.t = BC_RESULT_TEMP;
6016
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006017 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006018 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006019 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006020 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006021
Denys Vlasenko29301232018-12-11 15:29:32 +01006022 s = zbc_program_num(operand, &num, false);
6023 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006024 bc_num_init(&res.d.n, num->len);
6025 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006026 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006027 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006028 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006029 }
6030
6031 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006032 a = (void*)f->autos.v;
6033 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006034 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006035 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006036 bc_vec_pop(v);
6037 }
6038
Denys Vlasenko24e41942018-12-21 23:01:26 +01006039 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006040 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006041 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006042
Denys Vlasenko29301232018-12-11 15:29:32 +01006043 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006044}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006045#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006046#endif // ENABLE_BC
6047
6048static unsigned long bc_program_scale(BcNum *n)
6049{
6050 return (unsigned long) n->rdx;
6051}
6052
6053static unsigned long bc_program_len(BcNum *n)
6054{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006055 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006056
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006057 if (n->rdx != len) return len;
6058 for (;;) {
6059 if (len == 0) break;
6060 len--;
6061 if (n->num[len] != 0) break;
6062 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006063 return len;
6064}
6065
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006066static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006067{
6068 BcStatus s;
6069 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006070 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006071 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006072 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006073
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006074 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006075 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006076 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006077
Denys Vlasenko29301232018-12-11 15:29:32 +01006078 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006079 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006080
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006081#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006082 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006083 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006084#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006085
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006086 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006087
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006088 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006089 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006090#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006091 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006092 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006093 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006094#endif
6095#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006097 char **str;
6098 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6099
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006100 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006101 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006102 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006103#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006104 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006105 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006106 }
6107
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006108 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006109
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006110 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006111}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006112#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006113
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006114#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006115static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006116{
6117 BcStatus s;
6118 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006119 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006120
Denys Vlasenko29301232018-12-11 15:29:32 +01006121 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006122 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006123
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006124 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006125 bc_num_init(&res2.d.n, n2->len);
6126
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006127 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006128 if (s) goto err;
6129
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006130 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006131 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006132 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006133
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006134 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006135 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006136 bc_num_free(&res2.d.n);
6137 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006138 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006139}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006140#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006141
Denys Vlasenkofa210792018-12-19 19:35:40 +01006142static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006143{
6144 BcStatus s;
6145 BcResult *r1, *r2, *r3, res;
6146 BcNum *n1, *n2, *n3;
6147
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006148 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006149 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006150 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006151 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006152
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006153 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006154 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006155 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006156 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006157 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006158
6159 // Make sure that the values have their pointers updated, if necessary.
6160 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006161 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006162 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006163 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006164 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006165 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006166 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006167 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006168 }
6169 }
6170
6171 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006172 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006173 if (s) goto err;
6174
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006175 bc_vec_pop(&G.prog.results);
6176 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006177
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006178 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006179 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006180 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006181 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006182}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006183#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006184
Denys Vlasenkofa210792018-12-19 19:35:40 +01006185static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006186{
Gavin Howard01055ba2018-11-03 11:00:21 -06006187 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006188 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006189
6190 res.t = BC_RESULT_TEMP;
6191
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006192 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006193 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006194 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006195}
6196
Denys Vlasenkofa210792018-12-19 19:35:40 +01006197static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006198{
6199 BcStatus s;
6200 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006201 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006202 char **strs;
6203 char *str;
6204 char c;
6205 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006206
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006207 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006208 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006209 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006210
Denys Vlasenko29301232018-12-11 15:29:32 +01006211 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006212 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006213
6214 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006215 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006216 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006217 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006218
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006219 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006220 bc_num_copy(&n, num);
6221 bc_num_truncate(&n, n.rdx);
6222
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006223 strmb.cap = ARRAY_SIZE(strmb_digs);
6224 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006225 bc_num_ulong2num(&strmb, 0x100);
6226 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006227
Gavin Howard01055ba2018-11-03 11:00:21 -06006228 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006229 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006230 if (s) goto num_err;
6231
6232 c = (char) val;
6233
6234 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006235 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006236 char *sp;
Gavin Howard01055ba2018-11-03 11:00:21 -06006237 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006238 sp = *bc_program_str(idx);
6239 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006240 }
6241
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006242 strs = (void*)G.prog.strs.v;
6243 for (idx = 0; idx < G.prog.strs.len; idx++) {
6244 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6245 goto dup;
6246 }
6247 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006248 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006249 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006250 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006251 bc_vec_push(&G.prog.strs, &str);
6252 dup:
Gavin Howard01055ba2018-11-03 11:00:21 -06006253 res.t = BC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006254 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006255 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006256
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006257 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006258 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006259 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006260 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006261}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006262#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006263
Denys Vlasenkofa210792018-12-19 19:35:40 +01006264static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006265{
6266 BcStatus s;
6267 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006268 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006269 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006270
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006271 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006272 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006273 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006274
Denys Vlasenko29301232018-12-11 15:29:32 +01006275 s = zbc_program_num(r, &n, false);
6276 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006277
Denys Vlasenko57734c92018-12-17 21:14:05 +01006278 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006279 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006280 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006281 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06006282 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006283 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006284 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006285 }
6286
Denys Vlasenko29301232018-12-11 15:29:32 +01006287 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006288}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006289#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006290
Denys Vlasenkofa210792018-12-19 19:35:40 +01006291static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006292{
6293 BcStatus s;
6294 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006295 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006296 unsigned long val;
6297
Denys Vlasenko29301232018-12-11 15:29:32 +01006298 s = zbc_program_prep(&opnd, &num);
6299 if (s) RETURN_STATUS(s);
6300 s = zbc_num_ulong(num, &val);
6301 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006302
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006303 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006304
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006305 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006306 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006307 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006308 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006309 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006310
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006311 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006312
Denys Vlasenko29301232018-12-11 15:29:32 +01006313 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006314}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006315#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006316
Denys Vlasenkofa210792018-12-19 19:35:40 +01006317static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006318{
6319 BcStatus s = BC_STATUS_SUCCESS;
6320 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006321 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006322 BcInstPtr ip;
6323 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006324
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006325 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006326 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006327
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006328 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006329
6330 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006331 BcNum *n = n; // for compiler
6332 bool exec;
6333 char *name;
6334 char *then_name = bc_program_name(code, bgn);
6335 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006336
6337 if (code[*bgn] == BC_PARSE_STREND)
6338 (*bgn) += 1;
6339 else
6340 else_name = bc_program_name(code, bgn);
6341
6342 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006343 name = then_name;
6344 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006345 exec = true;
6346 name = else_name;
6347 }
6348
6349 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006350 BcVec *v;
6351 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006352 n = bc_vec_top(v);
6353 }
6354
6355 free(then_name);
6356 free(else_name);
6357
6358 if (!exec) goto exit;
6359 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006360 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006361 goto exit;
6362 }
6363
6364 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006365 } else {
6366 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006367 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006368 } else if (r->t == BC_RESULT_VAR) {
6369 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006370 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006371 if (s || !BC_PROG_STR(n)) goto exit;
6372 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006373 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006374 goto exit;
6375 }
6376
6377 fidx = sidx + BC_PROG_REQ_FUNCS;
6378
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006379 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006380
6381 if (f->code.len == 0) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006382 FILE *sv_input_fp;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006383 BcParse prs;
6384 char *str;
6385
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006386 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006387 str = *bc_program_str(sidx);
6388 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006389 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006390
6391 sv_input_fp = G.input_fp;
6392 G.input_fp = NULL; // "do not read from input file when <EOL> reached"
6393 s = zdc_parse_exprs_until_eof(&prs);
6394 G.input_fp = sv_input_fp;
6395
Gavin Howard01055ba2018-11-03 11:00:21 -06006396 if (s) goto err;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006397 if (prs.l.t.t != XC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006398 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006399 err:
6400 bc_parse_free(&prs);
6401 bc_vec_pop_all(&f->code);
6402 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006404 bc_parse_push(&prs, DC_INST_POP_EXEC);
Gavin Howard01055ba2018-11-03 11:00:21 -06006405 bc_parse_free(&prs);
6406 }
6407
Denys Vlasenko24e41942018-12-21 23:01:26 +01006408 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006409 ip.func = fidx;
6410
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006411 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006412 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006413
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006414 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006415 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006416 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006417 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006418}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006419#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006420#endif // ENABLE_DC
6421
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006422static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006423{
Gavin Howard01055ba2018-11-03 11:00:21 -06006424 BcResult res;
6425 unsigned long val;
6426
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006427 res.t = inst - XC_INST_IBASE + BC_RESULT_IBASE;
6428 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006429 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006430 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006431 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006432 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006433 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006434
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006435 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006436 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006437 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006438}
6439
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006440static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006441{
Gavin Howard01055ba2018-11-03 11:00:21 -06006442 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006443 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006444 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006445 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006446
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006447 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006448 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6449 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006450 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006451 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006452
Denys Vlasenko24e41942018-12-21 23:01:26 +01006453 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 -06006454 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006455#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006456 case BC_INST_JUMP_ZERO: {
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006457 BcNum *num;
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006458 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006459 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006460 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006461 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006462 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006463 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006464 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006465 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006466 break;
6467 }
6468 // else: fall through
6469 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006470 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006471 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006472 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006473 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006474 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006475 break;
6476 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006477 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006478 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006479 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006480 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006481 case BC_INST_INC_PRE:
6482 case BC_INST_DEC_PRE:
6483 case BC_INST_INC_POST:
6484 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006485 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006486 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006487 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006488 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006489 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006490 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006491 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006492 case XC_INST_RET:
Gavin Howard01055ba2018-11-03 11:00:21 -06006493 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006494 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006495 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006496 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006497 case XC_INST_BOOL_OR:
6498 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006499#endif // ENABLE_BC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006500 case XC_INST_REL_EQ:
6501 case XC_INST_REL_LE:
6502 case XC_INST_REL_GE:
6503 case XC_INST_REL_NE:
6504 case XC_INST_REL_LT:
6505 case XC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006506 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006507 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006508 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006509 case XC_INST_READ:
6510 dbg_exec("XC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006511 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006512 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006513 case XC_INST_VAR:
6514 dbg_exec("XC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006515 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006516 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006517 case XC_INST_ARRAY_ELEM:
6518 case XC_INST_ARRAY:
6519 dbg_exec("XC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006520 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006521 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006522#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006523 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006524 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006525 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006526 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006527 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006528#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006529 case XC_INST_IBASE:
6530 case XC_INST_OBASE:
6531 case XC_INST_SCALE:
6532 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006533 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006534 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006535 case XC_INST_SCALE_FUNC:
6536 case XC_INST_LENGTH:
6537 case XC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006538 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006539 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006540 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006541 case XC_INST_NUM:
6542 dbg_exec("XC_INST_NUM:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006543 r.t = BC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006544 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006545 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006546 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006547 case XC_INST_POP:
6548 dbg_exec("XC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006549 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006550 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006551 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006552 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006553 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006554 case XC_INST_PRINT:
6555 case XC_INST_PRINT_POP:
6556 case XC_INST_PRINT_STR:
6557 dbg_exec("XC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006558 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006559 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006560 case XC_INST_STR:
6561 dbg_exec("XC_INST_STR:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006562 r.t = BC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006563 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006564 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006565 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006566 case XC_INST_POWER:
6567 case XC_INST_MULTIPLY:
6568 case XC_INST_DIVIDE:
6569 case XC_INST_MODULUS:
6570 case XC_INST_PLUS:
6571 case XC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006572 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006573 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006574 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006575 case XC_INST_BOOL_NOT: {
6576 BcNum *num;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006577 dbg_exec("XC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006578 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006579 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006580 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006581 if (bc_num_cmp(num, &G.prog.zero) == 0)
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006582 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006583 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006584 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006585 break;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006586 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006587 case XC_INST_NEG:
6588 dbg_exec("XC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006589 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006590 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006591#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006592 case BC_INST_ASSIGN_POWER:
6593 case BC_INST_ASSIGN_MULTIPLY:
6594 case BC_INST_ASSIGN_DIVIDE:
6595 case BC_INST_ASSIGN_MODULUS:
6596 case BC_INST_ASSIGN_PLUS:
6597 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006598#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006599 case XC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006600 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006601 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006602 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006603#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006604 case DC_INST_POP_EXEC:
6605 dbg_exec("DC_INST_POP_EXEC:");
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006606 bc_vec_pop(&G.prog.exestack);
6607 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006608 case DC_INST_MODEXP:
6609 dbg_exec("DC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006610 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006611 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006612 case DC_INST_DIVMOD:
6613 dbg_exec("DC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006614 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006615 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006616 case DC_INST_EXECUTE:
6617 case DC_INST_EXEC_COND:
6618 dbg_exec("DC_INST_EXEC[_COND]:");
6619 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006620 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006621 case DC_INST_PRINT_STACK: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006622 size_t idx;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006623 dbg_exec("DC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006624 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006625 s = zbc_program_print(XC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006626 if (s) break;
6627 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006628 break;
6629 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006630 case DC_INST_CLEAR_STACK:
6631 dbg_exec("DC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006632 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006633 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006634 case DC_INST_STACK_LEN:
6635 dbg_exec("DC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006636 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006637 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006638 case DC_INST_DUPLICATE:
6639 dbg_exec("DC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006640 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006641 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006642 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006643 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006644 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006645 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006646 case DC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006647 BcResult *ptr2;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006648 dbg_exec("DC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006649 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006650 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006651 ptr = bc_vec_item_rev(&G.prog.results, 0);
6652 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006653 memcpy(&r, ptr, sizeof(BcResult));
6654 memcpy(ptr, ptr2, sizeof(BcResult));
6655 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006656 break;
6657 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006658 case DC_INST_ASCIIFY:
6659 dbg_exec("DC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006660 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006661 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006662 case DC_INST_PRINT_STREAM:
6663 dbg_exec("DC_INST_PRINT_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006664 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006665 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006666 case DC_INST_LOAD:
6667 case DC_INST_PUSH_VAR: {
6668 bool copy = inst == DC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006669 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006670 break;
6671 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006672 case DC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006673 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006674 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006675 free(name);
6676 break;
6677 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006678 case DC_INST_QUIT:
6679 dbg_exec("DC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006680 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006681 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006682 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006683 goto read_updated_ip;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006684 case DC_INST_NQUIT:
6685 dbg_exec("DC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006686 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006687 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006688#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006689 read_updated_ip:
6690 // Instruction stack has changed, read new pointers
6691 ip = bc_vec_top(&G.prog.exestack);
6692 func = bc_program_func(ip->func);
6693 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006694 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006695 }
6696
Denys Vlasenkod38af482018-12-04 19:11:02 +01006697 if (s || G_interrupt) {
6698 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006699 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006700 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006701
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006702 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006703 }
6704
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006705 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006706}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006707#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006708
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006709static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006710{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006711 char *lenv;
6712 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006713
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006714 lenv = getenv(var);
6715 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006716 if (!lenv) return len;
6717
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006718 len = bb_strtou(lenv, NULL, 10) - 1;
6719 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006720 len = BC_NUM_PRINT_WIDTH;
6721
6722 return len;
6723}
6724
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006725static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006726{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006727 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006728
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006729 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006730 s = zbc_parse_text_init(&G.prs, text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006731 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006732
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006733 while (G.prs.l.t.t != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006734 BcInstPtr *ip;
6735 BcFunc *f;
6736
Denys Vlasenko39287e02018-12-22 02:23:08 +01006737 dbg_lex("%s:%d G.prs.l.t.t:%d, parsing...", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006738 if (IS_BC) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006739// FIXME: "eating" of stmt delimiters is coded inconsistently
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006740// (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't),
6741// which causes bugs such as "print 1 print 2" erroneously accepted,
6742// or "print 1 else 2" detecting parse error only after executing
6743// "print 1" part.
6744 IF_BC(s = zbc_parse_stmt_or_funcdef(&G.prs));
6745 } else {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006746 // Most of dc parsing assumes all whitespace,
6747 // including '\n', is eaten.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01006748 while (G.prs.l.t.t == XC_LEX_NLINE) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006749 s = zbc_lex_next(&G.prs.l);
6750 if (s) goto err;
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01006751 if (G.prs.l.t.t == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006752 goto done;
6753 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006754 IF_DC(s = zdc_parse_expr(&G.prs));
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006755 }
6756 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006757 err:
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006758 bc_parse_reset(&G.prs); // includes bc_program_reset()
6759 RETURN_STATUS(BC_STATUS_FAILURE);
6760 }
6761
Denys Vlasenko39287e02018-12-22 02:23:08 +01006762 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006763 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006764 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006765 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006766 break;
6767 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006768
6769 ip = (void*)G.prog.exestack.v;
6770#if SANITY_CHECKS
6771 if (G.prog.exestack.len != 1) // should have only main's IP
6772 bb_error_msg_and_die("BUG:call stack");
6773 if (ip->func != BC_PROG_MAIN)
6774 bb_error_msg_and_die("BUG:not MAIN");
6775#endif
6776 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006777 // bc discards strings, constants and code after each
6778 // top-level statement in the "main program".
6779 // This prevents "yes 1 | bc" from growing its memory
6780 // without bound. This can be done because data stack
6781 // is empty and thus can't hold any references to
6782 // strings or constants, there is no generated code
6783 // which can hold references (after we discard one
6784 // we just executed). Code of functions can have references,
6785 // but bc stores function strings/constants in per-function
6786 // storage.
6787 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006788#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006789 if (G.prog.results.len != 0) // should be empty
6790 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006791#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006792 IF_BC(bc_vec_pop_all(&f->strs);)
6793 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006794 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006795 if (G.prog.results.len == 0
6796 && G.prog.vars.len == 0
6797 ) {
6798 // If stack is empty and no registers exist (TODO: or they are all empty),
6799 // we can get rid of accumulated strings and constants.
6800 // In this example dc process should not grow
6801 // its memory consumption with time:
6802 // yes 1pc | dc
6803 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6804 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6805 }
6806 // The code is discarded always (below), thus this example
6807 // should also not grow its memory consumption with time,
6808 // even though its data stack is not empty:
6809 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006810 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006811 // We drop generated and executed code for both bc and dc:
6812 bc_vec_pop_all(&f->code);
6813 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006814 }
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006815 done:
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006816 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006817 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006818}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006819#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006820
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006821static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006822{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006823 // So far bc/dc have no way to include a file from another file,
6824 // therefore we know G.prog.file == NULL on entry
6825 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006826 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006827
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006828 G.prog.file = filename;
6829 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006830 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006831
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006832 do {
6833 s = zbc_vm_process("");
6834 // We do not stop looping on errors here if reading stdin.
6835 // Example: start interactive bc and enter "return".
6836 // It should say "'return' not in a function"
6837 // but should not exit.
6838 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006839 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006840 RETURN_STATUS(s);
6841}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006842#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006843
6844static BC_STATUS zbc_vm_file(const char *file)
6845{
6846 BcStatus s;
6847 FILE *fp;
6848
6849 fp = xfopen_for_read(file);
6850 s = zbc_vm_execute_FILE(fp, file);
6851 fclose(fp);
6852
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006853 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006854}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006855#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006856
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006857#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006858static void bc_vm_info(void)
6859{
6860 printf("%s "BB_VER"\n"
6861 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6862 , applet_name);
6863}
6864
6865static void bc_args(char **argv)
6866{
6867 unsigned opts;
6868 int i;
6869
6870 GETOPT_RESET();
6871#if ENABLE_FEATURE_BC_LONG_OPTIONS
6872 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6873 "warn\0" No_argument "w"
6874 "version\0" No_argument "v"
6875 "standard\0" No_argument "s"
6876 "quiet\0" No_argument "q"
6877 "mathlib\0" No_argument "l"
6878 "interactive\0" No_argument "i"
6879 );
6880#else
6881 opts = option_mask32 |= getopt32(argv, "wvsqli");
6882#endif
6883 if (getenv("POSIXLY_CORRECT"))
6884 option_mask32 |= BC_FLAG_S;
6885
6886 if (opts & BC_FLAG_V) {
6887 bc_vm_info();
6888 exit(0);
6889 }
6890
6891 for (i = optind; argv[i]; ++i)
6892 bc_vec_push(&G.files, argv + i);
6893}
6894
6895static void bc_vm_envArgs(void)
6896{
6897 BcVec v;
6898 char *buf;
6899 char *env_args = getenv("BC_ENV_ARGS");
6900
6901 if (!env_args) return;
6902
6903 G.env_args = xstrdup(env_args);
6904 buf = G.env_args;
6905
6906 bc_vec_init(&v, sizeof(char *), NULL);
6907
6908 while (*(buf = skip_whitespace(buf)) != '\0') {
6909 bc_vec_push(&v, &buf);
6910 buf = skip_non_whitespace(buf);
6911 if (!*buf)
6912 break;
6913 *buf++ = '\0';
6914 }
6915
6916 // NULL terminate, and pass argv[] so that first arg is argv[1]
6917 if (sizeof(int) == sizeof(char*)) {
6918 bc_vec_push(&v, &const_int_0);
6919 } else {
6920 static char *const nullptr = NULL;
6921 bc_vec_push(&v, &nullptr);
6922 }
6923 bc_args(((char **)v.v) - 1);
6924
6925 bc_vec_free(&v);
6926}
6927
6928static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006929 "scale=20"
6930"\n" "define e(x){"
6931"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006932////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6933//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6934//e(-.998896): GNU:.36828580434569428695
6935// above code:.36828580434569428696
6936// actual value:.3682858043456942869594...
6937// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006938"\n" "b=ibase"
6939"\n" "ibase=A"
6940"\n" "if(x<0){"
6941"\n" "n=1"
6942"\n" "x=-x"
6943"\n" "}"
6944"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006945"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006946"\n" "scale=scale(x)+1"
6947"\n" "while(x>1){"
6948"\n" "d+=1"
6949"\n" "x/=2"
6950"\n" "scale+=1"
6951"\n" "}"
6952"\n" "scale=r"
6953"\n" "r=x+1"
6954"\n" "p=x"
6955"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006956"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006957"\n" "p*=x"
6958"\n" "f*=i"
6959"\n" "v=p/f"
6960"\n" "r+=v"
6961"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006962"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006963"\n" "scale=s"
6964"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006965"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006966"\n" "return(r/1)"
6967"\n" "}"
6968"\n" "define l(x){"
6969"\n" "auto b,s,r,p,a,q,i,v"
6970"\n" "b=ibase"
6971"\n" "ibase=A"
6972"\n" "if(x<=0){"
6973"\n" "r=(1-10^scale)/1"
6974"\n" "ibase=b"
6975"\n" "return(r)"
6976"\n" "}"
6977"\n" "s=scale"
6978"\n" "scale+=6"
6979"\n" "p=2"
6980"\n" "while(x>=2){"
6981"\n" "p*=2"
6982"\n" "x=sqrt(x)"
6983"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006984"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006985"\n" "p*=2"
6986"\n" "x=sqrt(x)"
6987"\n" "}"
6988"\n" "r=a=(x-1)/(x+1)"
6989"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006990"\n" "v=1"
6991"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006992"\n" "a*=q"
6993"\n" "v=a/i"
6994"\n" "r+=v"
6995"\n" "}"
6996"\n" "r*=p"
6997"\n" "scale=s"
6998"\n" "ibase=b"
6999"\n" "return(r/1)"
7000"\n" "}"
7001"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01007002"\n" "auto b,s,r,a,q,i"
7003"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007004"\n" "b=ibase"
7005"\n" "ibase=A"
7006"\n" "s=scale"
7007"\n" "scale=1.1*s+2"
7008"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007009"\n" "scale=0"
7010"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007011"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007012"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007013"\n" "scale=s+2"
7014"\n" "r=a=x"
7015"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007016"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007017"\n" "a*=q/(i*(i-1))"
7018"\n" "r+=a"
7019"\n" "}"
7020"\n" "scale=s"
7021"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007022"\n" "return(r/1)"
7023"\n" "}"
7024"\n" "define c(x){"
7025"\n" "auto b,s"
7026"\n" "b=ibase"
7027"\n" "ibase=A"
7028"\n" "s=scale"
7029"\n" "scale*=1.2"
7030"\n" "x=s(2*a(1)+x)"
7031"\n" "scale=s"
7032"\n" "ibase=b"
7033"\n" "return(x/1)"
7034"\n" "}"
7035"\n" "define a(x){"
7036"\n" "auto b,s,r,n,a,m,t,f,i,u"
7037"\n" "b=ibase"
7038"\n" "ibase=A"
7039"\n" "n=1"
7040"\n" "if(x<0){"
7041"\n" "n=-1"
7042"\n" "x=-x"
7043"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007044"\n" "if(scale<65){"
7045"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7046"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007047"\n" "}"
7048"\n" "s=scale"
7049"\n" "if(x>.2){"
7050"\n" "scale+=5"
7051"\n" "a=a(.2)"
7052"\n" "}"
7053"\n" "scale=s+3"
7054"\n" "while(x>.2){"
7055"\n" "m+=1"
7056"\n" "x=(x-.2)/(1+.2*x)"
7057"\n" "}"
7058"\n" "r=u=x"
7059"\n" "f=-x*x"
7060"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007061"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007062"\n" "u*=f"
7063"\n" "t=u/i"
7064"\n" "r+=t"
7065"\n" "}"
7066"\n" "scale=s"
7067"\n" "ibase=b"
7068"\n" "return((m*a+r)/n)"
7069"\n" "}"
7070"\n" "define j(n,x){"
7071"\n" "auto b,s,o,a,i,v,f"
7072"\n" "b=ibase"
7073"\n" "ibase=A"
7074"\n" "s=scale"
7075"\n" "scale=0"
7076"\n" "n/=1"
7077"\n" "if(n<0){"
7078"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007079"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007080"\n" "}"
7081"\n" "a=1"
7082"\n" "for(i=2;i<=n;++i)a*=i"
7083"\n" "scale=1.5*s"
7084"\n" "a=(x^n)/2^n/a"
7085"\n" "r=v=1"
7086"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007087"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007088"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007089"\n" "v=v*f/i/(n+i)"
7090"\n" "r+=v"
7091"\n" "}"
7092"\n" "scale=s"
7093"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007094"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007095"\n" "return(a*r/1)"
7096"\n" "}"
7097};
7098#endif // ENABLE_BC
7099
Denys Vlasenko19f11072018-12-12 22:48:19 +01007100static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007101{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007102 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007103 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007104 size_t i;
7105
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007106#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007107 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007108 // We know that internal library is not buggy,
7109 // thus error checking is normally disabled.
7110# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007111 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007112 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007113 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007114 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007115#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007116
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007117 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007118 fname = (void*)G.files.v;
7119 for (i = 0; i < G.files.len; i++) {
7120 s = zbc_vm_file(*fname++);
7121 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7122 // Debug config, non-interactive mode:
7123 // return all the way back to main.
7124 // Non-debug builds do not come here
7125 // in non-interactive mode, they exit.
7126 RETURN_STATUS(s);
7127 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007128 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007129
Denys Vlasenko91cde952018-12-10 20:56:08 +01007130 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007131 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007132
Denys Vlasenko19f11072018-12-12 22:48:19 +01007133 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007134}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007135#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007136
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007137#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007138static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007139{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007140 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007141 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007142 bc_vec_free(&G.prog.vars);
7143 bc_vec_free(&G.prog.var_map);
7144 bc_vec_free(&G.prog.arrs);
7145 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007146 IF_DC(bc_vec_free(&G.prog.strs);)
7147 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007148 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007149 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007150 IF_BC(bc_num_free(&G.prog.last);)
7151 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007152 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007153 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007154}
7155
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007156static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007157{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007158 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007159 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007160 bc_parse_free(&G.prs);
7161 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007162}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007163#endif
7164
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007165static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007166{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007167 BcInstPtr ip;
7168
Denys Vlasenko82269122018-12-14 16:30:56 +01007169 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007170 memset(&ip, 0, sizeof(BcInstPtr));
7171
Denys Vlasenko82269122018-12-14 16:30:56 +01007172 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007173 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007174 G.prog.ob_t = 10;
7175
Denys Vlasenko503faf92018-12-20 16:24:18 +01007176 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7177 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007178
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007179 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007180 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007181
Denys Vlasenko503faf92018-12-20 16:24:18 +01007182 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7183 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007184
7185 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007186 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007187
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007188 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007189 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007190 // a valid function name (and be short)
7191 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007192 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007193 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007194 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007195 bc_program_add_fn();
7196 bc_program_add_fn();
7197 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007198
7199 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007200 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007201
7202 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007203 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007204
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007205 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7206 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007207 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007208 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7209 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007210
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007211 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007212}
Gavin Howard01055ba2018-11-03 11:00:21 -06007213
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007214static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007215{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007216#if ENABLE_FEATURE_EDITING
7217 G.line_input_state = new_line_input_t(DO_HISTORY);
7218#endif
7219 G.prog.len = bc_vm_envLen(env_len);
7220
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007221 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007222 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007223 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007224 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007225
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007226//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7227//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007228 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007229#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007230 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007231 // With SA_RESTART, most system calls will restart
7232 // (IOW: they won't fail with EINTR).
7233 // In particular, this means ^C won't cause
7234 // stdout to get into "error state" if SIGINT hits
7235 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007236 //
7237 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007238 // will only be handled after [Enter] since read()
7239 // from stdin is not interrupted by ^C either,
7240 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007241 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007242 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7243
7244 // Without SA_RESTART, this exhibits a bug:
7245 // "while (1) print 1" and try ^C-ing it.
7246 // Intermittently, instead of returning to input line,
7247 // you'll get "output error: Interrupted system call"
7248 // and exit.
7249 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007250#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007251 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007252 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007253 return 0; // "not a tty"
7254}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007255
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007256static BcStatus bc_vm_run(void)
7257{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007258 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007259#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007260 if (G_exiting) // it was actually "halt" or "quit"
7261 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007262 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007263# if ENABLE_FEATURE_EDITING
7264 free_line_input_t(G.line_input_state);
7265# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007266 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007267#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007268 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007269 return st;
7270}
7271
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007272#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007273int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007274int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007275{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007276 int is_tty;
7277
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007278 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007279
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007280 is_tty = bc_vm_init("BC_LINE_LENGTH");
7281
7282 bc_args(argv);
7283
7284 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7285 bc_vm_info();
7286
7287 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007288}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007289#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007290
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007291#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007292int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007293int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007294{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007295 int noscript;
7296
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007297 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007298
7299 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7300 // 1 char wider than bc from the same package.
7301 // Both default width, and xC_LINE_LENGTH=N are wider:
7302 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007303 // |1234\ |
7304 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007305 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007306 // |123\ |
7307 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007308 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007309 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007310
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007311 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7312 noscript = BC_FLAG_I;
7313 for (;;) {
7314 int n = getopt(argc, argv, "e:f:x");
7315 if (n <= 0)
7316 break;
7317 switch (n) {
7318 case 'e':
7319 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007320 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007321 if (n) return n;
7322 break;
7323 case 'f':
7324 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007325 n = zbc_vm_file(optarg);
7326 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007327 break;
7328 case 'x':
7329 option_mask32 |= DC_FLAG_X;
7330 break;
7331 default:
7332 bb_show_usage();
7333 }
7334 }
7335 argv += optind;
7336
7337 while (*argv) {
7338 noscript = 0;
7339 bc_vec_push(&G.files, argv++);
7340 }
7341
7342 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7343
7344 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007345}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007346#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007347
7348#endif // not DC_SMALL