blob: 5b79b2db1e0e4ba48a73ac9f6d99f5369d10dbf5 [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Gavin Howardfa495ce2018-12-18 10:03:14 -07004 * Adapted from https://github.com/gavinhoward/bc
5 * Original code copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06006 */
7//config:config BC
8//config: bool "bc (45 kb; 49 kb when combined with dc)"
9//config: default y
10//config: help
11//config: bc is a command-line, arbitrary-precision calculator with a
12//config: Turing-complete language. See the GNU bc manual
13//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010014//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060015//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010016//config: This bc has five differences to the GNU bc:
17//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060018//config: 2) Arrays are copied before being passed as arguments to
19//config: functions. This behavior is required by the bc spec.
20//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010021//config: the number of elements in the array. This prints "1":
22//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060023//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010024//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060025//config: allows POSIX-compliant scripts to work while somewhat
26//config: preserving expected behavior (versus C) and making parsing
27//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010028//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060029//config:
30//config: Options:
Gavin Howard01055ba2018-11-03 11:00:21 -060031//config: -i --interactive force interactive mode
Denys Vlasenko89e785a2018-12-13 16:35:52 +010032//config: -q --quiet don't print version and copyright
33//config: -s --standard error if any non-POSIX extensions are used
34//config: -w --warn warn if any non-POSIX extensions are used
Gavin Howard01055ba2018-11-03 11:00:21 -060035//config: -l --mathlib use predefined math routines:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010036//config: s(expr) sine in radians
37//config: c(expr) cosine in radians
38//config: a(expr) arctangent, returning radians
39//config: l(expr) natural log
40//config: e(expr) raises e to the power of expr
41//config: j(n, x) Bessel function of integer order n of x
Gavin Howard01055ba2018-11-03 11:00:21 -060042//config:
43//config:config DC
44//config: bool "dc (38 kb; 49 kb when combined with bc)"
45//config: default y
46//config: help
47//config: dc is a reverse-polish notation command-line calculator which
48//config: supports unlimited precision arithmetic. See the FreeBSD man page
49//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010050//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060051//config:
52//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010053//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060054//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010055//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060056//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010057//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060058//config: 4) Like the FreeBSD dc, this dc supports extended registers.
59//config: However, they are implemented differently. When it encounters
60//config: whitespace where a register should be, it skips the whitespace.
61//config: If the character following is not a lowercase letter, an error
62//config: is issued. Otherwise, the register name is parsed by the
63//config: following regex:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010064//config: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060065//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010066//config: whitespace. Examples:
67//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060068//config: Also note that, like the FreeBSD dc, extended registers are not
69//config: allowed unless the "-x" option is given.
70//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010071//config:config FEATURE_DC_SMALL
72//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
73//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010074//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010075//config:
76//config:config FEATURE_DC_LIBM
77//config: bool "Enable power and exp functions (requires libm)"
78//config: default y
79//config: depends on FEATURE_DC_SMALL
80//config: help
81//config: Enable power and exp functions.
82//config: NOTE: This will require libm to be present for linking.
83//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060084//config:config FEATURE_BC_SIGNALS
Denys Vlasenko89e785a2018-12-13 16:35:52 +010085//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060086//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010087//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -060088//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010089//config: Enable interactive mode: when started on a tty,
90//config: ^C interrupts execution and returns to command line,
91//config: errors also return to command line instead of exiting,
92//config: line editing with history is available.
93//config:
94//config: With this option off, input can still be taken from tty,
95//config: but all errors are fatal, ^C is fatal,
96//config: tty is treated exactly the same as any other
97//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060098//config:
99//config:config FEATURE_BC_LONG_OPTIONS
100//config: bool "Enable bc/dc long options"
101//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100102//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600103//config: help
104//config: Enable long options for bc and dc.
105
106//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
107//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
108
109//kbuild:lib-$(CONFIG_BC) += bc.o
110//kbuild:lib-$(CONFIG_DC) += bc.o
111
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100112//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600113//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100114//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600115//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100116//usage:#define bc_full_usage "\n"
117//usage: "\nArbitrary precision calculator"
118//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100119///////: "\n -i Interactive" - has no effect for now
120//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100121//usage: "\n -l Load standard math library"
122//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100123//usage: "\n -w Warn if extensions are used"
124///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100125//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100126//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600127//usage:
128//usage:#define bc_example_usage
129//usage: "3 + 4.129\n"
130//usage: "1903 - 2893\n"
131//usage: "-129 * 213.28935\n"
132//usage: "12 / -1932\n"
133//usage: "12 % 12\n"
134//usage: "34 ^ 189\n"
135//usage: "scale = 13\n"
136//usage: "ibase = 2\n"
137//usage: "obase = A\n"
138//usage:
139//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100140//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600141//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100142//usage:#define dc_full_usage "\n"
143//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100144//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100145//usage: "\np - print top of the stack without popping"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100146//usage: "\nf - print entire stack"
147//usage: "\nk - pop the value and set the precision"
148//usage: "\ni - pop the value and set input radix"
149//usage: "\no - pop the value and set output radix"
150//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600151//usage:
152//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100153//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600154//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100155//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600156//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100157//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600158//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100159//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600160//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100161//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600162//usage: "64\n"
163
164#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100165#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600166
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100167#if ENABLE_FEATURE_DC_SMALL
168# include "dc.c"
169#else
170
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100171#define DEBUG_LEXER 0
172#define DEBUG_COMPILE 0
173#define DEBUG_EXEC 0
Denys Vlasenko19eee8e2018-12-21 20:29:34 +0100174// This can be left enabled for production as well:
175#define SANITY_CHECKS 1
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100176
177#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100178static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100179#define dbg_lex(...) \
180 do { \
181 fprintf(stderr, "%*s", lex_indent, ""); \
182 bb_error_msg(__VA_ARGS__); \
183 } while (0)
184#define dbg_lex_enter(...) \
185 do { \
186 dbg_lex(__VA_ARGS__); \
187 lex_indent++; \
188 } while (0)
189#define dbg_lex_done(...) \
190 do { \
191 lex_indent--; \
192 dbg_lex(__VA_ARGS__); \
193 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100194#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100195# define dbg_lex(...) ((void)0)
196# define dbg_lex_enter(...) ((void)0)
197# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100198#endif
199
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100200#if DEBUG_COMPILE
201# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
202#else
203# define dbg_compile(...) ((void)0)
204#endif
205
Denys Vlasenko99b37622018-12-15 20:06:59 +0100206#if DEBUG_EXEC
207# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
208#else
209# define dbg_exec(...) ((void)0)
210#endif
211
Gavin Howard01055ba2018-11-03 11:00:21 -0600212typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100213 BC_STATUS_SUCCESS = 0,
214 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100215 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600216} BcStatus;
217
Gavin Howard01055ba2018-11-03 11:00:21 -0600218#define BC_VEC_INVALID_IDX ((size_t) -1)
219#define BC_VEC_START_CAP (1 << 5)
220
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100221typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600222
223typedef struct BcVec {
224 char *v;
225 size_t len;
226 size_t cap;
227 size_t size;
228 BcVecFree dtor;
229} BcVec;
230
Gavin Howard01055ba2018-11-03 11:00:21 -0600231typedef signed char BcDig;
232
233typedef struct BcNum {
234 BcDig *restrict num;
235 size_t rdx;
236 size_t len;
237 size_t cap;
238 bool neg;
239} BcNum;
240
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100241#define BC_NUM_MAX_IBASE ((unsigned long) 16)
242// larger value might speed up BIGNUM calculations a bit:
243#define BC_NUM_DEF_SIZE (16)
244#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600245
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100246#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600247
Gavin Howard01055ba2018-11-03 11:00:21 -0600248typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100249#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600250 BC_INST_INC_PRE,
251 BC_INST_DEC_PRE,
252 BC_INST_INC_POST,
253 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100254#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100255 XC_INST_NEG, // order
Gavin Howard01055ba2018-11-03 11:00:21 -0600256
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100257 XC_INST_REL_EQ, // should
258 XC_INST_REL_LE, // match
259 XC_INST_REL_GE, // LEX
260 XC_INST_REL_NE, // constants
261 XC_INST_REL_LT, // for
262 XC_INST_REL_GT, // these
Gavin Howard01055ba2018-11-03 11:00:21 -0600263
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100264 XC_INST_POWER, // operations
265 XC_INST_MULTIPLY, // |
266 XC_INST_DIVIDE, // |
267 XC_INST_MODULUS, // |
268 XC_INST_PLUS, // |
269 XC_INST_MINUS, // |
Gavin Howard01055ba2018-11-03 11:00:21 -0600270
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100271 XC_INST_BOOL_NOT, // |
272 XC_INST_BOOL_OR, // |
273 XC_INST_BOOL_AND, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100274#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100275 BC_INST_ASSIGN_POWER, // |
276 BC_INST_ASSIGN_MULTIPLY,// |
277 BC_INST_ASSIGN_DIVIDE, // |
278 BC_INST_ASSIGN_MODULUS, // |
279 BC_INST_ASSIGN_PLUS, // |
280 BC_INST_ASSIGN_MINUS, // |
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100281#endif
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100282 XC_INST_ASSIGN, // V
Gavin Howard01055ba2018-11-03 11:00:21 -0600283
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100284 XC_INST_NUM,
285 XC_INST_VAR,
286 XC_INST_ARRAY_ELEM,
287 XC_INST_ARRAY,
288 XC_INST_SCALE_FUNC,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100289
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100290 XC_INST_IBASE, // order of these constans should match other enums
291 XC_INST_OBASE, // order of these constans should match other enums
292 XC_INST_SCALE, // order of these constans should match other enums
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100293 IF_BC(BC_INST_LAST,) // order of these constans should match other enums
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100294 XC_INST_LENGTH,
295 XC_INST_READ,
296 XC_INST_SQRT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600297
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100298 XC_INST_PRINT,
299 XC_INST_PRINT_POP,
300 XC_INST_STR,
301 XC_INST_PRINT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600302
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100303#if ENABLE_BC
Denys Vlasenko39287e02018-12-22 02:23:08 +0100304 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600305 BC_INST_JUMP,
306 BC_INST_JUMP_ZERO,
307
308 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600309 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100310#endif
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100311 XC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600312
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100313 XC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100314#if ENABLE_DC
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100315 DC_INST_POP_EXEC,
Gavin Howard01055ba2018-11-03 11:00:21 -0600316
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100317 DC_INST_MODEXP,
318 DC_INST_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600319
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100320 DC_INST_EXECUTE,
321 DC_INST_EXEC_COND,
Gavin Howard01055ba2018-11-03 11:00:21 -0600322
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100323 DC_INST_ASCIIFY,
324 DC_INST_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600325
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100326 DC_INST_PRINT_STACK,
327 DC_INST_CLEAR_STACK,
328 DC_INST_STACK_LEN,
329 DC_INST_DUPLICATE,
330 DC_INST_SWAP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600331
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100332 DC_INST_LOAD,
333 DC_INST_PUSH_VAR,
334 DC_INST_PUSH_TO_VAR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600335
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100336 DC_INST_QUIT,
337 DC_INST_NQUIT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600338
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100339 DC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100340#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600341} BcInst;
342
343typedef struct BcId {
344 char *name;
345 size_t idx;
346} BcId;
347
348typedef struct BcFunc {
349 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100350 IF_BC(BcVec labels;)
351 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100352 IF_BC(BcVec strs;)
353 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100354 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600355} BcFunc;
356
357typedef enum BcResultType {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100358 XC_RESULT_TEMP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600359
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100360 XC_RESULT_VAR,
361 XC_RESULT_ARRAY_ELEM,
362 XC_RESULT_ARRAY,
Gavin Howard01055ba2018-11-03 11:00:21 -0600363
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100364 XC_RESULT_STR,
Gavin Howard01055ba2018-11-03 11:00:21 -0600365
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100366 //code uses "inst - XC_INST_IBASE + XC_RESULT_IBASE" construct,
367 XC_RESULT_IBASE, // relative order should match for: XC_INST_IBASE
368 XC_RESULT_OBASE, // relative order should match for: XC_INST_OBASE
369 XC_RESULT_SCALE, // relative order should match for: XC_INST_SCALE
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100370 IF_BC(BC_RESULT_LAST,) // relative order should match for: BC_INST_LAST
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100371 XC_RESULT_CONSTANT,
372 IF_BC(BC_RESULT_ONE,)
Gavin Howard01055ba2018-11-03 11:00:21 -0600373} BcResultType;
374
375typedef union BcResultData {
376 BcNum n;
377 BcVec v;
378 BcId id;
379} BcResultData;
380
381typedef struct BcResult {
382 BcResultType t;
383 BcResultData d;
384} BcResult;
385
386typedef struct BcInstPtr {
387 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100388 size_t inst_idx;
389 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600390} BcInstPtr;
391
Gavin Howard01055ba2018-11-03 11:00:21 -0600392typedef enum BcLexType {
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100393 XC_LEX_EOF,
394 XC_LEX_INVALID,
Gavin Howard01055ba2018-11-03 11:00:21 -0600395
Denys Vlasenko23ea0732018-12-24 15:05:49 +0100396 XC_LEX_NLINE,
397 XC_LEX_WHITESPACE,
398 XC_LEX_STR,
399 XC_LEX_NAME,
400 XC_LEX_NUMBER,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100401
Denys Vlasenko69560f42018-12-24 14:14:23 +0100402 XC_LEX_1st_op,
403 XC_LEX_NEG = XC_LEX_1st_op, // order
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100404
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100405 XC_LEX_OP_REL_EQ, // should
406 XC_LEX_OP_REL_LE, // match
407 XC_LEX_OP_REL_GE, // INST
408 XC_LEX_OP_REL_NE, // constants
409 XC_LEX_OP_REL_LT, // for
410 XC_LEX_OP_REL_GT, // these
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100411
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100412 XC_LEX_OP_POWER, // operations
413 XC_LEX_OP_MULTIPLY, // |
414 XC_LEX_OP_DIVIDE, // |
415 XC_LEX_OP_MODULUS, // |
416 XC_LEX_OP_PLUS, // |
417 XC_LEX_OP_MINUS, // |
418 XC_LEX_OP_last = XC_LEX_OP_MINUS,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100419#if ENABLE_BC
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100420 BC_LEX_OP_BOOL_NOT, // |
421 BC_LEX_OP_BOOL_OR, // |
422 BC_LEX_OP_BOOL_AND, // |
423
424 BC_LEX_OP_ASSIGN_POWER, // |
425 BC_LEX_OP_ASSIGN_MULTIPLY, // |
426 BC_LEX_OP_ASSIGN_DIVIDE, // |
427 BC_LEX_OP_ASSIGN_MODULUS, // |
428 BC_LEX_OP_ASSIGN_PLUS, // |
429 BC_LEX_OP_ASSIGN_MINUS, // |
430
431 BC_LEX_OP_ASSIGN, // V
432
Gavin Howard01055ba2018-11-03 11:00:21 -0600433 BC_LEX_OP_INC,
434 BC_LEX_OP_DEC,
435
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100436 BC_LEX_LPAREN, // () are 0x28 and 0x29
437 BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN)
Gavin Howard01055ba2018-11-03 11:00:21 -0600438
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100439 BC_LEX_LBRACKET, // [] are 0x5B and 5D
Gavin Howard01055ba2018-11-03 11:00:21 -0600440 BC_LEX_COMMA,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100441 BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET)
Gavin Howard01055ba2018-11-03 11:00:21 -0600442
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100443 BC_LEX_LBRACE, // {} are 0x7B and 0x7D
Gavin Howard01055ba2018-11-03 11:00:21 -0600444 BC_LEX_SCOLON,
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100445 BC_LEX_RBRACE, // must be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600446
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100447 BC_LEX_KEY_1st_keyword,
448 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600449 BC_LEX_KEY_BREAK,
450 BC_LEX_KEY_CONTINUE,
451 BC_LEX_KEY_DEFINE,
452 BC_LEX_KEY_ELSE,
453 BC_LEX_KEY_FOR,
454 BC_LEX_KEY_HALT,
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100455 // code uses "type - BC_LEX_KEY_IBASE + XC_INST_IBASE" construct,
456 BC_LEX_KEY_IBASE, // relative order should match for: XC_INST_IBASE
457 BC_LEX_KEY_OBASE, // relative order should match for: XC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600458 BC_LEX_KEY_IF,
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100459 IF_BC(BC_LEX_KEY_LAST,) // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600460 BC_LEX_KEY_LENGTH,
461 BC_LEX_KEY_LIMITS,
Gavin Howard01055ba2018-11-03 11:00:21 -0600462 BC_LEX_KEY_PRINT,
463 BC_LEX_KEY_QUIT,
464 BC_LEX_KEY_READ,
465 BC_LEX_KEY_RETURN,
466 BC_LEX_KEY_SCALE,
467 BC_LEX_KEY_SQRT,
468 BC_LEX_KEY_WHILE,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100469#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600470
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100471#if ENABLE_DC
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100472 DC_LEX_OP_BOOL_NOT = XC_LEX_OP_last + 1,
473 DC_LEX_OP_ASSIGN,
474
475 DC_LEX_LPAREN,
476 DC_LEX_SCOLON,
477 DC_LEX_READ,
478 DC_LEX_IBASE,
479 DC_LEX_SCALE,
480 DC_LEX_OBASE,
481 DC_LEX_LENGTH,
482 DC_LEX_PRINT,
483 DC_LEX_QUIT,
484 DC_LEX_SQRT,
485 DC_LEX_LBRACE,
486
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100487 DC_LEX_EQ_NO_REG,
488 DC_LEX_OP_MODEXP,
489 DC_LEX_OP_DIVMOD,
Gavin Howard01055ba2018-11-03 11:00:21 -0600490
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100491 DC_LEX_COLON,
492 DC_LEX_ELSE,
493 DC_LEX_EXECUTE,
494 DC_LEX_PRINT_STACK,
495 DC_LEX_CLEAR_STACK,
496 DC_LEX_STACK_LEVEL,
497 DC_LEX_DUPLICATE,
498 DC_LEX_SWAP,
499 DC_LEX_POP,
Gavin Howard01055ba2018-11-03 11:00:21 -0600500
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100501 DC_LEX_ASCIIFY,
502 DC_LEX_PRINT_STREAM,
Gavin Howard01055ba2018-11-03 11:00:21 -0600503
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100504 // code uses "t - DC_LEX_STORE_IBASE + XC_INST_IBASE" construct,
505 DC_LEX_STORE_IBASE, // relative order should match for: XC_INST_IBASE
506 DC_LEX_STORE_OBASE, // relative order should match for: XC_INST_OBASE
507 DC_LEX_STORE_SCALE, // relative order should match for: XC_INST_SCALE
508 DC_LEX_LOAD,
509 DC_LEX_LOAD_POP,
510 DC_LEX_STORE_PUSH,
511 DC_LEX_PRINT_POP,
512 DC_LEX_NQUIT,
513 DC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100514#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600515} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100516// must match order of BC_LEX_KEY_foo etc above
517#if ENABLE_BC
518struct BcLexKeyword {
519 char name8[8];
520};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100521#define LEX_KW_ENTRY(a, b) \
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100522 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100523static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100524 LEX_KW_ENTRY("auto" , 1), // 0
525 LEX_KW_ENTRY("break" , 1), // 1
526 LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
527 LEX_KW_ENTRY("define" , 1), // 3
528 LEX_KW_ENTRY("else" , 0), // 4
529 LEX_KW_ENTRY("for" , 1), // 5
530 LEX_KW_ENTRY("halt" , 0), // 6
531 LEX_KW_ENTRY("ibase" , 1), // 7
532 LEX_KW_ENTRY("obase" , 1), // 8
533 LEX_KW_ENTRY("if" , 1), // 9
534 LEX_KW_ENTRY("last" , 0), // 10
535 LEX_KW_ENTRY("length" , 1), // 11
536 LEX_KW_ENTRY("limits" , 0), // 12
537 LEX_KW_ENTRY("print" , 0), // 13
538 LEX_KW_ENTRY("quit" , 1), // 14
539 LEX_KW_ENTRY("read" , 0), // 15
540 LEX_KW_ENTRY("return" , 1), // 16
541 LEX_KW_ENTRY("scale" , 1), // 17
542 LEX_KW_ENTRY("sqrt" , 1), // 18
543 LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100544};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100545#undef LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100546#define STRING_else (bc_lex_kws[4].name8)
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100547#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100548#define STRING_if (bc_lex_kws[9].name8)
549#define STRING_while (bc_lex_kws[19].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100550enum {
551 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100552 | (1 << 0) // 0
553 | (1 << 1) // 1
554 | (0 << 2) // 2
555 | (1 << 3) // 3
556 | (0 << 4) // 4
557 | (1 << 5) // 5
558 | (0 << 6) // 6
559 | (1 << 7) // 7
560 | (1 << 8) // 8
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100561 | (1 << 9) // 9
562 | (0 << 10) // 10
563 | (1 << 11) // 11
564 | (0 << 12) // 12
Denys Vlasenko82269122018-12-14 16:30:56 +0100565 | (0 << 13) // 13
566 | (1 << 14) // 14
567 | (0 << 15) // 15
568 | (1 << 16) // 16
569 | (1 << 17) // 17
570 | (1 << 18) // 18
571 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100572};
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100573#define keyword_is_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100574
575// This is a bit array that corresponds to token types. An entry is
576// true if the token is valid in an expression, false otherwise.
577// Used to figure out when expr parsing should stop *without error message*
578// - 0 element indicates this condition. 1 means "this token is to be eaten
Denys Vlasenkoad0bd382018-12-24 00:50:32 +0100579// as part of the expression", it can then still be determined to be invalid
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100580// by later processing.
581enum {
582#define EXBITS(a,b,c,d,e,f,g,h) \
583 ((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 +0100584 BC_PARSE_EXPRS_BITS = 0 // corresponding BC_LEX_xyz:
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100585 + (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 +0100586 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: == <= >= != < > ^ *
587 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: / % + - ! || && ^=
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100588 + (EXBITS(1,1,1,1,1,1,1,1) << (3*8)) // 24: *= /= %= += -= = ++ --
589 + (EXBITS(1,1,0,0,0,0,0,0) << (4*8)) // 32: ( ) [ , ] { ; }
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100590 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
591 + (EXBITS(1,0,1,1,0,0,0,1) << (6*8)) // 48: obase if last length limits print quit read
592 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100593#undef EXBITS
594};
Denys Vlasenkod0238d82018-12-25 01:21:16 +0100595static ALWAYS_INLINE long lex_allowed_in_bc_expr(unsigned i)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100596{
597#if ULONG_MAX > 0xffffffff
598 // 64-bit version (will not work correctly for 32-bit longs!)
599 return BC_PARSE_EXPRS_BITS & (1UL << i);
600#else
601 // 32-bit version
602 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
603 if (i >= 32) {
604 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
605 i &= 31;
606 }
607 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100608#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100609}
610
611// This is an array of data for operators that correspond to
Denys Vlasenko69560f42018-12-24 14:14:23 +0100612// [XC_LEX_1st_op...] token types.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100613static const uint8_t bc_parse_ops[] = {
614#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100615 OP(1, false), // neg
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +0100616 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 +0100617 OP(2, false), // pow
618 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
619 OP(4, true ), OP( 4, true ), // + -
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100620 OP(1, false), // not
621 OP(7, true ), OP( 7, true ), // or and
622 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
623 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenkoabf6cf62018-12-24 13:20:57 +0100624 OP(0, false), OP( 0, false ), // inc dec
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100625#undef OP
626};
627#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
628#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
629#endif // ENABLE_BC
630
631#if ENABLE_DC
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100632static const //BcLexType - should be this type
633uint8_t
634dc_char_to_LEX[] = {
635 /* %&'( */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100636 XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100637 /* )*+, */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100638 XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100639 /* -./ */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100640 XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100641 /* 0123456789 */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100642 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
643 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
644 XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100645 /* :;<=>?@ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100646 DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ,
647 XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100648 /* ABCDEFGH */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100649 XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
650 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100651 /* IJKLMNOP */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100652 DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP,
653 XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100654 /* QRSTUVWXY */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100655 DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID,
656 XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100657 /* Z[\] */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100658 DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100659 /* ^_` */
Denys Vlasenko69560f42018-12-24 14:14:23 +0100660 XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100661 /* abcdefgh */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100662 DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE,
663 DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100664 /* ijklmnop */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100665 DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD,
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100666 XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100667 /* qrstuvwx */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100668 DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID,
669 XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100670 /* yz */
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +0100671 XC_LEX_INVALID, DC_LEX_STACK_LEVEL,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100672 /* {|}~ */
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100673 DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD,
Denys Vlasenko73b2c602018-12-24 01:02:59 +0100674};
Denys Vlasenkoa7732d12018-12-24 04:26:07 +0100675static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100676int8_t
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100677dc_LEX_to_INST[] = { // starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz:
678 XC_INST_POWER, XC_INST_MULTIPLY, // OP_POWER OP_MULTIPLY
679 XC_INST_DIVIDE, XC_INST_MODULUS, // OP_DIVIDE OP_MODULUS
680 XC_INST_PLUS, XC_INST_MINUS, // OP_PLUS OP_MINUS
Denys Vlasenko4accb6b2018-12-24 15:29:08 +0100681 XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT
682 DC_INST_INVALID, // DC_LEX_OP_ASSIGN
683 XC_INST_REL_GT, // DC_LEX_LPAREN
684 DC_INST_INVALID, // DC_LEX_SCOLON
685 DC_INST_INVALID, // DC_LEX_READ
686 XC_INST_IBASE, // DC_LEX_IBASE
687 XC_INST_SCALE, // DC_LEX_SCALE
688 XC_INST_OBASE, // DC_LEX_OBASE
689 XC_INST_LENGTH, // DC_LEX_LENGTH
690 XC_INST_PRINT, // DC_LEX_PRINT
691 DC_INST_QUIT, // DC_LEX_QUIT
692 XC_INST_SQRT, // DC_LEX_SQRT
693 XC_INST_REL_GE, // DC_LEX_LBRACE
694 XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG
695 DC_INST_MODEXP, DC_INST_DIVMOD, // OP_MODEXP OP_DIVMOD
696 DC_INST_INVALID, DC_INST_INVALID, // COLON ELSE
697 DC_INST_EXECUTE, // EXECUTE
698 DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // PRINT_STACK CLEAR_STACK
699 DC_INST_STACK_LEN, DC_INST_DUPLICATE, // STACK_LEVEL DUPLICATE
700 DC_INST_SWAP, XC_INST_POP, // SWAP POP
701 DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // ASCIIFY PRINT_STREAM
702 DC_INST_INVALID, DC_INST_INVALID, // STORE_IBASE STORE_OBASE
703 DC_INST_INVALID, DC_INST_INVALID, // STORE_SCALE LOAD
704 DC_INST_INVALID, DC_INST_INVALID, // LOAD_POP STORE_PUSH
705 XC_INST_PRINT, DC_INST_NQUIT, // PRINT_POP NQUIT
706 XC_INST_SCALE_FUNC, // SCALE_FACTOR
Denys Vlasenko9d9c97e2018-12-24 15:00:56 +0100707 // DC_INST_INVALID in this table either means that corresponding LEX
708 // is not possible for dc, or that it does not compile one-to-one
709 // to a single INST.
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100710};
711#endif // ENABLE_DC
712
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100713typedef struct BcParse {
Denys Vlasenko6e618232018-12-25 22:20:14 +0100714 smallint lex; // was BcLexType // first member is most used
715 smallint lex_last; // was BcLexType
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100716 size_t lex_line;
Denys Vlasenko6e618232018-12-25 22:20:14 +0100717 const char *lex_inbuf;
718 const char *lex_next_at; // last lex_next() was called at this string
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100719 const char *lex_filename;
720 FILE *lex_input_fp;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +0100721 BcVec lex_strnumbuf;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +0100722
Gavin Howard01055ba2018-11-03 11:00:21 -0600723 BcFunc *func;
724 size_t fidx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100725 IF_BC(size_t in_funcdef;)
Denys Vlasenko6e618232018-12-25 22:20:14 +0100726 IF_BC(BcVec exits;)
727 IF_BC(BcVec conds;)
728 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600729} BcParse;
730
Gavin Howard01055ba2018-11-03 11:00:21 -0600731typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600732 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100733 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600734
Denys Vlasenko503faf92018-12-20 16:24:18 +0100735 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600736 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600737 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600738
Gavin Howard01055ba2018-11-03 11:00:21 -0600739 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100740 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600741
742 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100743 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600744
745 BcVec vars;
746 BcVec var_map;
747
748 BcVec arrs;
749 BcVec arr_map;
750
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100751 IF_DC(BcVec strs;)
752 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600753
Gavin Howard01055ba2018-11-03 11:00:21 -0600754 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100755 IF_BC(BcNum one;)
756 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600757} BcProgram;
758
Denys Vlasenko6e618232018-12-25 22:20:14 +0100759struct globals {
760 BcParse prs; // first member is most used
761
762 // For error messages. Can be set to current parsed line,
763 // or [TODO] to current executing line (can be before last parsed one)
764 unsigned err_line;
765
766 BcVec input_buffer;
767
768 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
769 IF_FEATURE_CLEAN_UP(smallint exiting;)
770
771 BcProgram prog;
772
773 BcVec files;
774
775 char *env_args;
776
777#if ENABLE_FEATURE_EDITING
778 line_input_t *line_input_state;
779#endif
780} FIX_ALIASING;
781#define G (*ptr_to_globals)
782#define INIT_G() do { \
783 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
784} while (0)
785#define FREE_G() do { \
786 FREE_PTR_TO_GLOBALS(); \
787} while (0)
788#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
789#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
790#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
791#if ENABLE_FEATURE_BC_SIGNALS
792# define G_interrupt bb_got_signal
793# define G_ttyin G.ttyin
794#else
795# define G_interrupt 0
796# define G_ttyin 0
797#endif
798#if ENABLE_FEATURE_CLEAN_UP
799# define G_exiting G.exiting
800#else
801# define G_exiting 0
802#endif
803#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
804#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
805
806#define BC_PARSE_STREND (0xff)
807
808#if ENABLE_BC
809# define BC_PARSE_REL (1 << 0)
810# define BC_PARSE_PRINT (1 << 1)
811# define BC_PARSE_ARRAY (1 << 2)
812# define BC_PARSE_NOCALL (1 << 3)
813#endif
814
Gavin Howard01055ba2018-11-03 11:00:21 -0600815#define BC_PROG_MAIN (0)
816#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100817#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600818#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100819#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600820
821#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
822#define BC_PROG_NUM(r, n) \
Denys Vlasenkod897c9a2018-12-24 23:41:31 +0100823 ((r)->t != XC_RESULT_ARRAY && (r)->t != XC_RESULT_STR && !BC_PROG_STR(n))
Gavin Howard01055ba2018-11-03 11:00:21 -0600824
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100825#define BC_FLAG_W (1 << 0)
826#define BC_FLAG_V (1 << 1)
827#define BC_FLAG_S (1 << 2)
828#define BC_FLAG_Q (1 << 3)
829#define BC_FLAG_L (1 << 4)
830#define BC_FLAG_I (1 << 5)
831#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600832
833#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
834#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
835
Denys Vlasenko64074a12018-12-07 15:50:14 +0100836#define BC_MAX_OBASE ((unsigned) 999)
837#define BC_MAX_DIM ((unsigned) INT_MAX)
838#define BC_MAX_SCALE ((unsigned) UINT_MAX)
839#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
840#define BC_MAX_NUM BC_MAX_STRING
841// Unused apart from "limits" message. Just show a "biggish number" there.
842//#define BC_MAX_NAME BC_MAX_STRING
843//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
844//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
845#define BC_MAX_NAME_STR "999999999"
846#define BC_MAX_EXP_STR "999999999"
847#define BC_MAX_VARS_STR "999999999"
848
849#define BC_MAX_OBASE_STR "999"
850
851#if INT_MAX == 2147483647
852# define BC_MAX_DIM_STR "2147483647"
853#elif INT_MAX == 9223372036854775807
854# define BC_MAX_DIM_STR "9223372036854775807"
855#else
856# error Strange INT_MAX
857#endif
858
859#if UINT_MAX == 4294967295
860# define BC_MAX_SCALE_STR "4294967295"
861# define BC_MAX_STRING_STR "4294967294"
862#elif UINT_MAX == 18446744073709551615
863# define BC_MAX_SCALE_STR "18446744073709551615"
864# define BC_MAX_STRING_STR "18446744073709551614"
865#else
866# error Strange UINT_MAX
867#endif
868#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600869
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100870// In configurations where errors abort instead of propagating error
871// return code up the call chain, functions returning BC_STATUS
872// actually don't return anything, they always succeed and return "void".
873// A macro wrapper is provided, which makes this statement work:
874// s = zbc_func(...)
875// and makes it visible to the compiler that s is always zero,
876// allowing compiler to optimize dead code after the statement.
877//
878// To make code more readable, each such function has a "z"
879// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
880//
881#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100882# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100883# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100884# define IF_ERROR_RETURN_POSSIBLE(a) a
885# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100886# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100887# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100888#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100889# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100890# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100891# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
892# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100893# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100894# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100895#endif
896
Denys Vlasenko2097ac82018-12-24 05:00:36 +0100897//
898// Utility routines
899//
Gavin Howard01055ba2018-11-03 11:00:21 -0600900
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100901static void fflush_and_check(void)
902{
903 fflush_all();
904 if (ferror(stdout) || ferror(stderr))
905 bb_perror_msg_and_die("output error");
906}
907
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100908#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100909#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100910do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100911 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100912 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100913 return BC_STATUS_FAILURE; \
914} while (0)
915#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100916#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100917#endif
918
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100919static void quit(void) NORETURN;
920static void quit(void)
921{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100922 if (ferror(stdin))
923 bb_perror_msg_and_die("input error");
924 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100925 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100926 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100927}
928
Denys Vlasenko5318f812018-12-05 17:48:01 +0100929static void bc_verror_msg(const char *fmt, va_list p)
930{
Denys Vlasenko82269122018-12-14 16:30:56 +0100931 const char *sv = sv; // for compiler
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100932 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100933 sv = applet_name;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100934 applet_name = xasprintf("%s: %s:%u", applet_name, G.prs.lex_filename, G.err_line);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100935 }
936 bb_verror_msg(fmt, p, NULL);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +0100937 if (G.prs.lex_filename) {
Denys Vlasenko5318f812018-12-05 17:48:01 +0100938 free((char*)applet_name);
939 applet_name = sv;
940 }
941}
942
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100943static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100944{
945 va_list p;
946
947 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100948 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100949 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100950
Denys Vlasenkoec603182018-12-17 10:34:02 +0100951 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
952 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
953 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100954}
955
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100956#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100957static NOINLINE BC_STATUS zbc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100958{
959 va_list p;
960
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100961 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100962 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100963 RETURN_STATUS(BC_STATUS_SUCCESS); // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100964
965 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100966 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100967 va_end(p);
968
969 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100970 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100971 RETURN_STATUS(BC_STATUS_SUCCESS); // no, it's a warning
972
Denys Vlasenkoec603182018-12-17 10:34:02 +0100973 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100974 RETURN_STATUS(BC_STATUS_FAILURE);
Denys Vlasenkoec603182018-12-17 10:34:02 +0100975 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100976}
Denys Vlasenko79587cb2018-12-24 18:11:41 +0100977#define zbc_posix_error_fmt(...) (zbc_posix_error_fmt(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100978#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100979
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100980// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
981// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100982// function must not have caller-cleaned parameters on stack.
983// Unfortunately, vararg function API does exactly that on most arches.
984// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100985static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100986{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100987 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100988}
989static ERRORFUNC int bc_error_bad_character(char c)
990{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +0100991 if (!c)
992 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +0100993 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100994}
995static ERRORFUNC int bc_error_bad_expression(void)
996{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100997 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100998}
999static ERRORFUNC int bc_error_bad_token(void)
1000{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001001 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001002}
1003static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1004{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001005 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001006}
1007static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1008{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001009 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001010}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001011#if ENABLE_BC
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001012static BC_STATUS zbc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001013{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001014 RETURN_STATUS(zbc_posix_error_fmt("POSIX requires %s", msg));
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001015}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001016#define zbc_POSIX_requires(...) (zbc_POSIX_requires(__VA_ARGS__) COMMA_SUCCESS)
1017static BC_STATUS zbc_POSIX_does_not_allow(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001018{
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001019 RETURN_STATUS(zbc_posix_error_fmt("%s%s", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001020}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001021#define zbc_POSIX_does_not_allow(...) (zbc_POSIX_does_not_allow(__VA_ARGS__) COMMA_SUCCESS)
1022static BC_STATUS zbc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001023{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001024 RETURN_STATUS(zbc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; this is bad:", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001025}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001026#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)
1027static BC_STATUS zbc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
Denys Vlasenko00646792018-12-05 18:12:27 +01001028{
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01001029 RETURN_STATUS(zbc_posix_error_fmt("%san empty %s expression in 'for()'", "POSIX does not allow ", msg));
Denys Vlasenko00646792018-12-05 18:12:27 +01001030}
Denys Vlasenko79587cb2018-12-24 18:11:41 +01001031#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 +01001032#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001033
Gavin Howard01055ba2018-11-03 11:00:21 -06001034static void bc_vec_grow(BcVec *v, size_t n)
1035{
1036 size_t cap = v->cap * 2;
1037 while (cap < v->len + n) cap *= 2;
1038 v->v = xrealloc(v->v, v->size * cap);
1039 v->cap = cap;
1040}
1041
1042static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1043{
1044 v->size = esize;
1045 v->cap = BC_VEC_START_CAP;
1046 v->len = 0;
1047 v->dtor = dtor;
1048 v->v = xmalloc(esize * BC_VEC_START_CAP);
1049}
1050
Denys Vlasenko7d628012018-12-04 21:46:47 +01001051static void bc_char_vec_init(BcVec *v)
1052{
1053 bc_vec_init(v, sizeof(char), NULL);
1054}
1055
Gavin Howard01055ba2018-11-03 11:00:21 -06001056static void bc_vec_expand(BcVec *v, size_t req)
1057{
1058 if (v->cap < req) {
1059 v->v = xrealloc(v->v, v->size * req);
1060 v->cap = req;
1061 }
1062}
1063
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001064static void bc_vec_pop(BcVec *v)
1065{
1066 v->len--;
1067 if (v->dtor)
1068 v->dtor(v->v + (v->size * v->len));
1069}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001070
Gavin Howard01055ba2018-11-03 11:00:21 -06001071static void bc_vec_npop(BcVec *v, size_t n)
1072{
1073 if (!v->dtor)
1074 v->len -= n;
1075 else {
1076 size_t len = v->len - n;
1077 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1078 }
1079}
1080
Denys Vlasenko7d628012018-12-04 21:46:47 +01001081static void bc_vec_pop_all(BcVec *v)
1082{
1083 bc_vec_npop(v, v->len);
1084}
1085
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001086static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001087{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001088 size_t len = v->len;
1089 if (len >= v->cap) bc_vec_grow(v, 1);
1090 memmove(v->v + (v->size * len), data, v->size);
1091 v->len++;
1092 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001093}
1094
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001095// G.prog.results often needs "pop old operand, push result" idiom.
1096// Can do this without a few extra ops
1097static size_t bc_result_pop_and_push(const void *data)
1098{
1099 BcVec *v = &G.prog.results;
1100 char *last;
1101 size_t len = v->len - 1;
1102
1103 last = v->v + (v->size * len);
1104 if (v->dtor)
1105 v->dtor(last);
1106 memmove(last, data, v->size);
1107 return len;
1108}
1109
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001110static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001111{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001112 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001113}
1114
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001115static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001116{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001117 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001118 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001119 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001120}
1121
Gavin Howard01055ba2018-11-03 11:00:21 -06001122static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1123{
1124 if (idx == v->len)
1125 bc_vec_push(v, data);
1126 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001127 char *ptr;
1128
1129 if (v->len == v->cap) bc_vec_grow(v, 1);
1130
1131 ptr = v->v + v->size * idx;
1132
1133 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1134 memmove(ptr, data, v->size);
1135 }
1136}
1137
1138static void bc_vec_string(BcVec *v, size_t len, const char *str)
1139{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001140 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001141 bc_vec_expand(v, len + 1);
1142 memcpy(v->v, str, len);
1143 v->len = len;
1144
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001145 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001146}
1147
Gavin Howard01055ba2018-11-03 11:00:21 -06001148static void *bc_vec_item(const BcVec *v, size_t idx)
1149{
1150 return v->v + v->size * idx;
1151}
1152
1153static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1154{
1155 return v->v + v->size * (v->len - idx - 1);
1156}
1157
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001158static void *bc_vec_top(const BcVec *v)
1159{
1160 return v->v + v->size * (v->len - 1);
1161}
1162
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001163static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001164{
1165 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001166 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001167 free(v->v);
1168}
1169
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001170static BcFunc* bc_program_func(size_t idx)
1171{
1172 return bc_vec_item(&G.prog.fns, idx);
1173}
1174// BC_PROG_MAIN is zeroth element, so:
1175#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1176
1177#if ENABLE_BC
1178static BcFunc* bc_program_current_func(void)
1179{
1180 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1181 BcFunc *func = bc_program_func(ip->func);
1182 return func;
1183}
1184#endif
1185
1186static char** bc_program_str(size_t idx)
1187{
1188#if ENABLE_BC
1189 if (IS_BC) {
1190 BcFunc *func = bc_program_current_func();
1191 return bc_vec_item(&func->strs, idx);
1192 }
1193#endif
1194 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1195}
1196
1197static char** bc_program_const(size_t idx)
1198{
1199#if ENABLE_BC
1200 if (IS_BC) {
1201 BcFunc *func = bc_program_current_func();
1202 return bc_vec_item(&func->consts, idx);
1203 }
1204#endif
1205 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1206}
1207
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001208static int bc_id_cmp(const void *e1, const void *e2)
1209{
1210 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1211}
1212
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001213static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001214{
1215 free(((BcId *) id)->name);
1216}
1217
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001218static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001219{
1220 size_t low = 0, high = v->len;
1221
1222 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001223 size_t mid = (low + high) / 2;
1224 BcId *id = bc_vec_item(v, mid);
1225 int result = bc_id_cmp(ptr, id);
1226
1227 if (result == 0)
1228 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001229 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001230 high = mid;
1231 else
1232 low = mid + 1;
1233 }
1234
1235 return low;
1236}
1237
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001238static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001239{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001240 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001241
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001242 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001243 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001244 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1245 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001246 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001247 bc_vec_pushAt(v, ptr, n);
1248 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001249}
1250
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001251#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001252static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001253{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001254 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001255 if (i >= v->len) return BC_VEC_INVALID_IDX;
1256 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1257}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001258#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001259
Gavin Howard01055ba2018-11-03 11:00:21 -06001260static void bc_num_setToZero(BcNum *n, size_t scale)
1261{
1262 n->len = 0;
1263 n->neg = false;
1264 n->rdx = scale;
1265}
1266
1267static void bc_num_zero(BcNum *n)
1268{
1269 bc_num_setToZero(n, 0);
1270}
1271
1272static void bc_num_one(BcNum *n)
1273{
1274 bc_num_setToZero(n, 0);
1275 n->len = 1;
1276 n->num[0] = 1;
1277}
1278
Denys Vlasenko3129f702018-12-09 12:04:44 +01001279// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001280static void bc_num_init(BcNum *n, size_t req)
1281{
1282 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001283 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001284 n->num = xmalloc(req);
1285 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001286 n->rdx = 0;
1287 n->len = 0;
1288 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001289}
1290
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001291static void bc_num_init_DEF_SIZE(BcNum *n)
1292{
1293 bc_num_init(n, BC_NUM_DEF_SIZE);
1294}
1295
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001296static void bc_num_expand(BcNum *n, size_t req)
1297{
1298 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1299 if (req > n->cap) {
1300 n->num = xrealloc(n->num, req);
1301 n->cap = req;
1302 }
1303}
1304
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001305static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001306{
1307 free(((BcNum *) num)->num);
1308}
1309
1310static void bc_num_copy(BcNum *d, BcNum *s)
1311{
1312 if (d != s) {
1313 bc_num_expand(d, s->cap);
1314 d->len = s->len;
1315 d->neg = s->neg;
1316 d->rdx = s->rdx;
1317 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1318 }
1319}
1320
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001321static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001322{
1323 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001324 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001325
Denys Vlasenko1557b762018-12-22 21:37:46 +01001326 result = 0;
1327 i = n->len;
1328 while (i > n->rdx) {
1329 unsigned long prev = result;
1330 result = result * 10 + n->num[--i];
1331 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1332 // 0x1ff00000 * 10 is 0x13f600000,
1333 // or 0x3f600000 truncated to 32 bits. Which is larger.
1334 // However, (N*10)/8 < N check is always correct.
1335 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001336 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001337 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001338 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001339
Denys Vlasenko29301232018-12-11 15:29:32 +01001340 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001341}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001342#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1343
1344static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1345{
1346 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1347
1348 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1349}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001350#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001351
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001352#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1353# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1354#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1355# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1356#endif
1357// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1358// would not hit realloc() code path - not good if num[] is not malloced
1359
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001360static void bc_num_ulong2num(BcNum *n, unsigned long val)
1361{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001362 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001363
1364 bc_num_zero(n);
1365
1366 if (val == 0) return;
1367
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001368 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001369
1370 ptr = n->num;
1371 for (;;) {
1372 n->len++;
1373 *ptr++ = val % 10;
1374 val /= 10;
1375 if (val == 0) break;
1376 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001377}
1378
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001379static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001380{
1381 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001382 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001383 a[i] -= b[i];
1384 for (j = i; a[j] < 0;) {
1385 a[j++] += 10;
1386 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001387 }
1388 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001389}
1390
1391static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1392{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001393 size_t i = len;
1394 for (;;) {
1395 int c;
1396 if (i == 0)
1397 return 0;
1398 i--;
1399 c = a[i] - b[i];
1400 if (c != 0) {
1401 i++;
1402 if (c < 0)
1403 return -i;
1404 return i;
1405 }
1406 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001407}
1408
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001409#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1410#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1411#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1412//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1413static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1414{
1415 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1416}
1417//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1418static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1419{
1420 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1421}
1422
Gavin Howard01055ba2018-11-03 11:00:21 -06001423static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1424{
1425 size_t i, min, a_int, b_int, diff;
1426 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001427 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001428 ssize_t cmp;
1429
1430 if (a == b) return 0;
1431 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1432 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001433
1434 if (a->neg != b->neg) // signs of a and b differ
1435 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1436 return (int)b->neg - (int)a->neg;
1437 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001438
1439 a_int = BC_NUM_INT(a);
1440 b_int = BC_NUM_INT(b);
1441 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001442
1443 if (a_int != 0) return (ssize_t) a_int;
1444
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001445 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001446 if (a_max) {
1447 min = b->rdx;
1448 diff = a->rdx - b->rdx;
1449 max_num = a->num + diff;
1450 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001451 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1452 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001453 min = a->rdx;
1454 diff = b->rdx - a->rdx;
1455 max_num = b->num + diff;
1456 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001457 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001458 }
1459
1460 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001461 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001462
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001463 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001464 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001465 }
1466
1467 return 0;
1468}
1469
1470static void bc_num_truncate(BcNum *n, size_t places)
1471{
1472 if (places == 0) return;
1473
1474 n->rdx -= places;
1475
1476 if (n->len != 0) {
1477 n->len -= places;
1478 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1479 }
1480}
1481
1482static void bc_num_extend(BcNum *n, size_t places)
1483{
1484 size_t len = n->len + places;
1485
1486 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001487 if (n->cap < len) bc_num_expand(n, len);
1488
1489 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1490 memset(n->num, 0, sizeof(BcDig) * places);
1491
1492 n->len += places;
1493 n->rdx += places;
1494 }
1495}
1496
1497static void bc_num_clean(BcNum *n)
1498{
1499 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1500 if (n->len == 0)
1501 n->neg = false;
1502 else if (n->len < n->rdx)
1503 n->len = n->rdx;
1504}
1505
1506static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1507{
1508 if (n->rdx < scale)
1509 bc_num_extend(n, scale - n->rdx);
1510 else
1511 bc_num_truncate(n, n->rdx - scale);
1512
1513 bc_num_clean(n);
1514 if (n->len != 0) n->neg = !neg1 != !neg2;
1515}
1516
1517static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1518 BcNum *restrict b)
1519{
1520 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001521 b->len = n->len - idx;
1522 a->len = idx;
1523 a->rdx = b->rdx = 0;
1524
1525 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1526 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001527 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001528 bc_num_zero(b);
1529 bc_num_copy(a, n);
1530 }
1531
1532 bc_num_clean(a);
1533 bc_num_clean(b);
1534}
1535
Denys Vlasenko29301232018-12-11 15:29:32 +01001536static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001537{
Denys Vlasenko29301232018-12-11 15:29:32 +01001538 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001539
1540 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1541 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1542 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001543 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001544 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001545
1546 if (n->rdx >= places)
1547 n->rdx -= places;
1548 else {
1549 bc_num_extend(n, places - n->rdx);
1550 n->rdx = 0;
1551 }
1552
1553 bc_num_clean(n);
1554
Denys Vlasenko29301232018-12-11 15:29:32 +01001555 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001556}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001557#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001558
Denys Vlasenko2097ac82018-12-24 05:00:36 +01001559typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
1560
1561static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1562 BcNumBinaryOp op, size_t req)
1563{
1564 BcStatus s;
1565 BcNum num2, *ptr_a, *ptr_b;
1566 bool init = false;
1567
1568 if (c == a) {
1569 ptr_a = &num2;
1570 memcpy(ptr_a, c, sizeof(BcNum));
1571 init = true;
1572 } else
1573 ptr_a = a;
1574
1575 if (c == b) {
1576 ptr_b = &num2;
1577 if (c != a) {
1578 memcpy(ptr_b, c, sizeof(BcNum));
1579 init = true;
1580 }
1581 } else
1582 ptr_b = b;
1583
1584 if (init)
1585 bc_num_init(c, req);
1586 else
1587 bc_num_expand(c, req);
1588
1589 s = BC_STATUS_SUCCESS;
1590 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
1591
1592 if (init) bc_num_free(&num2);
1593
1594 RETURN_STATUS(s);
1595}
1596#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
1597
1598static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1599static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1600static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1601static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1602static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1603static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
1604
1605static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1606{
1607 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
1608 (void) scale;
1609 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
1610}
1611
1612static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1613{
1614 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
1615 (void) scale;
1616 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
1617}
1618
1619static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1620{
1621 size_t req = BC_NUM_MREQ(a, b, scale);
1622 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
1623}
1624
1625static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1626{
1627 size_t req = BC_NUM_MREQ(a, b, scale);
1628 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1629}
1630
1631static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1632{
1633 size_t req = BC_NUM_MREQ(a, b, scale);
1634 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1635}
1636
1637static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1638{
1639 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1640}
1641
1642static const BcNumBinaryOp zbc_program_ops[] = {
1643 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
1644};
1645#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
1646#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
1647#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
1648#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
1649#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
1650#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
1651
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001652static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001653{
1654 BcNum one;
1655 BcDig num[2];
1656
1657 one.cap = 2;
1658 one.num = num;
1659 bc_num_one(&one);
1660
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001661 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001662}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001663#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001664
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001665static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001666{
1667 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1668 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001669 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001670
1671 // Because this function doesn't need to use scale (per the bc spec),
1672 // I am hijacking it to say whether it's doing an add or a subtract.
1673
1674 if (a->len == 0) {
1675 bc_num_copy(c, b);
1676 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001677 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001678 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001679 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001680 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001681 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001682 }
1683
1684 c->neg = a->neg;
1685 c->rdx = BC_MAX(a->rdx, b->rdx);
1686 min_rdx = BC_MIN(a->rdx, b->rdx);
1687 c->len = 0;
1688
1689 if (a->rdx > b->rdx) {
1690 diff = a->rdx - b->rdx;
1691 ptr = a->num;
1692 ptr_a = a->num + diff;
1693 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001694 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001695 diff = b->rdx - a->rdx;
1696 ptr = b->num;
1697 ptr_a = a->num;
1698 ptr_b = b->num + diff;
1699 }
1700
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001701 ptr_c = c->num;
1702 for (i = 0; i < diff; ++i, ++c->len)
1703 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001704
1705 ptr_c += diff;
1706 a_int = BC_NUM_INT(a);
1707 b_int = BC_NUM_INT(b);
1708
1709 if (a_int > b_int) {
1710 min_int = b_int;
1711 max = a_int;
1712 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001713 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001714 min_int = a_int;
1715 max = b_int;
1716 ptr = ptr_b;
1717 }
1718
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001719 carry = 0;
1720 for (i = 0; i < min_rdx + min_int; ++i) {
1721 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001722 carry = in / 10;
1723 ptr_c[i] = (BcDig)(in % 10);
1724 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001725 for (; i < max + min_rdx; ++i) {
1726 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001727 carry = in / 10;
1728 ptr_c[i] = (BcDig)(in % 10);
1729 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001730 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001731
1732 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1733
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001734 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001735}
1736
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001737static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001738{
Gavin Howard01055ba2018-11-03 11:00:21 -06001739 ssize_t cmp;
1740 BcNum *minuend, *subtrahend;
1741 size_t start;
1742 bool aneg, bneg, neg;
1743
1744 // Because this function doesn't need to use scale (per the bc spec),
1745 // I am hijacking it to say whether it's doing an add or a subtract.
1746
1747 if (a->len == 0) {
1748 bc_num_copy(c, b);
1749 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001750 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001751 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001752 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001753 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001754 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001755 }
1756
1757 aneg = a->neg;
1758 bneg = b->neg;
1759 a->neg = b->neg = false;
1760
1761 cmp = bc_num_cmp(a, b);
1762
1763 a->neg = aneg;
1764 b->neg = bneg;
1765
1766 if (cmp == 0) {
1767 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001768 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001769 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001770 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001771 neg = a->neg;
1772 minuend = a;
1773 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001774 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001775 neg = b->neg;
1776 if (sub) neg = !neg;
1777 minuend = b;
1778 subtrahend = a;
1779 }
1780
1781 bc_num_copy(c, minuend);
1782 c->neg = neg;
1783
1784 if (c->rdx < subtrahend->rdx) {
1785 bc_num_extend(c, subtrahend->rdx - c->rdx);
1786 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001787 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001788 start = c->rdx - subtrahend->rdx;
1789
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001790 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001791
1792 bc_num_clean(c);
1793
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001794 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001795}
1796
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001797static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001798 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001799#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001800{
1801 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001802 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001803 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001804 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001805
Gavin Howard01055ba2018-11-03 11:00:21 -06001806 if (a->len == 0 || b->len == 0) {
1807 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001808 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001809 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001810 aone = BC_NUM_ONE(a);
1811 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001812 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001813 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001814 }
1815
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001816 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1817 || a->len < BC_NUM_KARATSUBA_LEN
1818 || b->len < BC_NUM_KARATSUBA_LEN
1819 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001820 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001821
Gavin Howard01055ba2018-11-03 11:00:21 -06001822 bc_num_expand(c, a->len + b->len + 1);
1823
1824 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001825 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001826
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001827 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001828 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001829 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001830 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001831 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001832 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001833 carry = in / 10;
1834 c->num[i + j] = (BcDig)(in % 10);
1835 }
1836
1837 c->num[i + j] += (BcDig) carry;
1838 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001839
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001840#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001841 // a=2^1000000
1842 // a*a <- without check below, this will not be interruptible
1843 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001844#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001845 }
1846
1847 c->len = len;
1848
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001849 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001850 }
1851
1852 bc_num_init(&l1, max);
1853 bc_num_init(&h1, max);
1854 bc_num_init(&l2, max);
1855 bc_num_init(&h2, max);
1856 bc_num_init(&m1, max);
1857 bc_num_init(&m2, max);
1858 bc_num_init(&z0, max);
1859 bc_num_init(&z1, max);
1860 bc_num_init(&z2, max);
1861 bc_num_init(&temp, max + max);
1862
1863 bc_num_split(a, max2, &l1, &h1);
1864 bc_num_split(b, max2, &l2, &h2);
1865
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001866 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001867 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001868 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001869 if (s) goto err;
1870
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001871 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001872 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001873 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001874 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001875 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001876 if (s) goto err;
1877
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001878 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001879 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001880 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001881 if (s) goto err;
1882
Denys Vlasenko29301232018-12-11 15:29:32 +01001883 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001884 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001885 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001886 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001887 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001888 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001889 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001890 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 bc_num_free(&temp);
1892 bc_num_free(&z2);
1893 bc_num_free(&z1);
1894 bc_num_free(&z0);
1895 bc_num_free(&m2);
1896 bc_num_free(&m1);
1897 bc_num_free(&h2);
1898 bc_num_free(&l2);
1899 bc_num_free(&h1);
1900 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001901 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001902}
1903
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001904static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001905{
1906 BcStatus s;
1907 BcNum cpa, cpb;
1908 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1909
1910 scale = BC_MAX(scale, a->rdx);
1911 scale = BC_MAX(scale, b->rdx);
1912 scale = BC_MIN(a->rdx + b->rdx, scale);
1913 maxrdx = BC_MAX(maxrdx, scale);
1914
1915 bc_num_init(&cpa, a->len);
1916 bc_num_init(&cpb, b->len);
1917
1918 bc_num_copy(&cpa, a);
1919 bc_num_copy(&cpb, b);
1920 cpa.neg = cpb.neg = false;
1921
Denys Vlasenko29301232018-12-11 15:29:32 +01001922 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001923 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001924 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001925 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001926 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001927 if (s) goto err;
1928
1929 maxrdx += scale;
1930 bc_num_expand(c, c->len + maxrdx);
1931
1932 if (c->len < maxrdx) {
1933 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1934 c->len += maxrdx;
1935 }
1936
1937 c->rdx = maxrdx;
1938 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001939 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001940 bc_num_free(&cpb);
1941 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001942 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001943}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001944#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001945
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001946static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001947{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001948 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001949 size_t len, end, i;
1950 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001951
1952 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001953 RETURN_STATUS(bc_error("divide by zero"));
1954 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001955 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001956 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001957 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001958 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001959 bc_num_copy(c, a);
1960 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001961 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001962 }
1963
1964 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1965 bc_num_copy(&cp, a);
1966 len = b->len;
1967
1968 if (len > cp.len) {
1969 bc_num_expand(&cp, len + 2);
1970 bc_num_extend(&cp, len - cp.len);
1971 }
1972
1973 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1974 cp.rdx -= b->rdx;
1975 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1976
1977 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001978 for (;;) {
1979 if (len == 0) break;
1980 len--;
1981 if (b->num[len] != 0)
1982 break;
1983 }
1984 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001985 }
1986
1987 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1988
1989 // We want an extra zero in front to make things simpler.
1990 cp.num[cp.len++] = 0;
1991 end = cp.len - len;
1992
1993 bc_num_expand(c, cp.len);
1994
1995 bc_num_zero(c);
1996 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
1997 c->rdx = cp.rdx;
1998 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001999
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002000 s = BC_STATUS_SUCCESS;
2001 for (i = end - 1; i < end; --i) {
2002 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002003 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002004 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2005 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002006 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002007#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002008 // a=2^100000
2009 // scale=40000
2010 // 1/a <- without check below, this will not be interruptible
2011 if (G_interrupt) {
2012 s = BC_STATUS_FAILURE;
2013 break;
2014 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002015#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002016 }
2017
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002018 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002019 bc_num_free(&cp);
2020
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002021 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002022}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002023#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002024
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002025static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002026 BcNum *restrict d, size_t scale, size_t ts)
2027{
2028 BcStatus s;
2029 BcNum temp;
2030 bool neg;
2031
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002032 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002033 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002034
2035 if (a->len == 0) {
2036 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002037 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002038 }
2039
2040 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002041 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002042 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002043
2044 if (scale != 0) scale = ts;
2045
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002046 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002047 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002048 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002049 if (s) goto err;
2050
2051 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2052
2053 neg = d->neg;
2054 bc_num_retireMul(d, ts, a->neg, b->neg);
2055 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002056 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002057 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002058 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002059}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002060#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002061
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002062static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002063{
2064 BcStatus s;
2065 BcNum c1;
2066 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2067
2068 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002069 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002070 bc_num_free(&c1);
2071
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002072 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002073}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002074#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002075
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002076static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002077{
2078 BcStatus s = BC_STATUS_SUCCESS;
2079 BcNum copy;
2080 unsigned long pow;
2081 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002082 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002083
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002084 // GNU bc does not allow 2^2.0 - we do
2085 for (i = 0; i < b->rdx; i++)
2086 if (b->num[i] != 0)
2087 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002088
2089 if (b->len == 0) {
2090 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002091 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002092 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002093 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002094 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002095 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002096 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002097 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002098 if (!b->neg)
2099 bc_num_copy(c, a);
2100 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002101 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002102 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002103 }
2104
2105 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002106 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002107 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002108 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002109
2110 bc_num_init(&copy, a->len);
2111 bc_num_copy(&copy, a);
2112
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002113 if (!neg) {
2114 if (a->rdx > scale)
2115 scale = a->rdx;
2116 if (a->rdx * pow < scale)
2117 scale = a->rdx * pow;
2118 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002119
Gavin Howard01055ba2018-11-03 11:00:21 -06002120
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002121 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002123 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002124 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002125 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002126 //if (G_interrupt) {
2127 // s = BC_STATUS_FAILURE;
2128 // goto err;
2129 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002130 }
2131
Gavin Howard01055ba2018-11-03 11:00:21 -06002132 bc_num_copy(c, &copy);
2133
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002134 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002135 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002136 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002137 if (s) goto err;
2138
2139 if (pow & 1) {
2140 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002141 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002142 if (s) goto err;
2143 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002144 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002145 //if (G_interrupt) {
2146 // s = BC_STATUS_FAILURE;
2147 // goto err;
2148 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002149 }
2150
2151 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002152 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002153 if (s) goto err;
2154 }
2155
Gavin Howard01055ba2018-11-03 11:00:21 -06002156 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2157
2158 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002159 for (i = 0; i < c->len; ++i)
2160 if (c->num[i] != 0)
2161 goto skip;
2162 bc_num_setToZero(c, scale);
2163 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002164
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002165 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002166 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002167 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002168}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002169#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002170
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002171static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002172{
2173 BcStatus s;
2174 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2175 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2176 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2177
2178 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2179 bc_num_expand(b, req);
2180
2181 if (a->len == 0) {
2182 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002183 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002184 }
2185 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002186 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002187 }
2188 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002189 bc_num_one(b);
2190 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002191 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002192 }
2193
2194 scale = BC_MAX(scale, a->rdx) + 1;
2195 len = a->len + scale;
2196
2197 bc_num_init(&num1, len);
2198 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002199 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002200
2201 bc_num_one(&half);
2202 half.num[0] = 5;
2203 half.rdx = 1;
2204
2205 bc_num_init(&f, len);
2206 bc_num_init(&fprime, len);
2207
2208 x0 = &num1;
2209 x1 = &num2;
2210
2211 bc_num_one(x0);
2212 pow = BC_NUM_INT(a);
2213
2214 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002215 if (pow & 1)
2216 x0->num[0] = 2;
2217 else
2218 x0->num[0] = 6;
2219
2220 pow -= 2 - (pow & 1);
2221
2222 bc_num_extend(x0, pow);
2223
2224 // Make sure to move the radix back.
2225 x0->rdx -= pow;
2226 }
2227
2228 x0->rdx = digs = digs1 = 0;
2229 resrdx = scale + 2;
2230 len = BC_NUM_INT(x0) + resrdx - 1;
2231
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002232 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002233 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002234 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002235 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002236 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002237 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002238 if (s) goto err;
2239
2240 cmp = bc_num_cmp(x1, x0);
2241 digs = x1->len - (unsigned long long) llabs(cmp);
2242
2243 if (cmp == cmp2 && digs == digs1)
2244 times += 1;
2245 else
2246 times = 0;
2247
2248 resrdx += times > 4;
2249
2250 cmp2 = cmp1;
2251 cmp1 = cmp;
2252 digs1 = digs;
2253
2254 temp = x0;
2255 x0 = x1;
2256 x1 = temp;
2257 }
2258
Gavin Howard01055ba2018-11-03 11:00:21 -06002259 bc_num_copy(b, x0);
2260 scale -= 1;
2261 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002262 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002263 bc_num_free(&fprime);
2264 bc_num_free(&f);
2265 bc_num_free(&half);
2266 bc_num_free(&num2);
2267 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002268 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002269}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002270#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002271
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002272static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002273 size_t scale)
2274{
2275 BcStatus s;
2276 BcNum num2, *ptr_a;
2277 bool init = false;
2278 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2279
2280 if (c == a) {
2281 memcpy(&num2, c, sizeof(BcNum));
2282 ptr_a = &num2;
2283 bc_num_init(c, len);
2284 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002285 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002286 ptr_a = a;
2287 bc_num_expand(c, len);
2288 }
2289
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002290 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002291
2292 if (init) bc_num_free(&num2);
2293
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002294 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002295}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002296#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002297
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002298#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002299static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002300{
2301 BcStatus s;
2302 BcNum base, exp, two, temp;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002303 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002304
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002305 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002306 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002307 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002308 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002309 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002310 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002311
2312 bc_num_expand(d, c->len);
2313 bc_num_init(&base, c->len);
2314 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002315 bc_num_init(&temp, b->len);
2316
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002317 two.cap = ARRAY_SIZE(two_digs);
2318 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002319 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002320 two_digs[0] = 2;
2321
Gavin Howard01055ba2018-11-03 11:00:21 -06002322 bc_num_one(d);
2323
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002324 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002325 if (s) goto err;
2326 bc_num_copy(&exp, b);
2327
2328 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002329 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002330 if (s) goto err;
2331
2332 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002333 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002334 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002335 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002336 if (s) goto err;
2337 }
2338
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002339 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002340 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002341 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002342 if (s) goto err;
2343 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002344 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002345 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002346 bc_num_free(&exp);
2347 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002348 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002349}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002350#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002351#endif // ENABLE_DC
2352
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002353static FAST_FUNC void bc_string_free(void *string)
2354{
2355 free(*(char**)string);
2356}
2357
Gavin Howard01055ba2018-11-03 11:00:21 -06002358static void bc_func_init(BcFunc *f)
2359{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002360 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002361 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2362 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002363 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2364 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002365 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002366}
2367
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002368static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002369{
2370 BcFunc *f = (BcFunc *) func;
2371 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002372 IF_BC(bc_vec_free(&f->labels);)
2373 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002374 IF_BC(bc_vec_free(&f->strs);)
2375 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002376}
2377
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002378static void bc_array_expand(BcVec *a, size_t len);
2379
Gavin Howard01055ba2018-11-03 11:00:21 -06002380static void bc_array_init(BcVec *a, bool nums)
2381{
2382 if (nums)
2383 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2384 else
2385 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2386 bc_array_expand(a, 1);
2387}
2388
Gavin Howard01055ba2018-11-03 11:00:21 -06002389static void bc_array_expand(BcVec *a, size_t len)
2390{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002391 if (a->dtor == bc_num_free
2392 // && a->size == sizeof(BcNum) - always true
2393 ) {
2394 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002395 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002396 bc_num_init_DEF_SIZE(&n);
2397 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002398 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002399 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002400 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002401 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002402 bc_array_init(&v, true);
2403 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002404 }
2405 }
2406}
2407
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002408static void bc_array_copy(BcVec *d, const BcVec *s)
2409{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002410 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002411 size_t i;
2412
2413 bc_vec_pop_all(d);
2414 bc_vec_expand(d, s->cap);
2415 d->len = s->len;
2416
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002417 dnum = (void*)d->v;
2418 snum = (void*)s->v;
2419 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002420 bc_num_init(dnum, snum->len);
2421 bc_num_copy(dnum, snum);
2422 }
2423}
2424
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002425#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002426static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002427{
2428 d->t = src->t;
2429
2430 switch (d->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002431 case XC_RESULT_TEMP:
2432 case XC_RESULT_IBASE:
2433 case XC_RESULT_SCALE:
2434 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002435 bc_num_init(&d->d.n, src->d.n.len);
2436 bc_num_copy(&d->d.n, &src->d.n);
2437 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002438 case XC_RESULT_VAR:
2439 case XC_RESULT_ARRAY:
2440 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002441 d->d.id.name = xstrdup(src->d.id.name);
2442 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002443 case XC_RESULT_CONSTANT:
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01002444 IF_BC(case BC_RESULT_LAST:)
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002445 IF_BC(case BC_RESULT_ONE:)
2446 case XC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002447 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2448 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002449 }
2450}
2451#endif // ENABLE_DC
2452
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002453static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002454{
2455 BcResult *r = (BcResult *) result;
2456
2457 switch (r->t) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002458 case XC_RESULT_TEMP:
2459 case XC_RESULT_IBASE:
2460 case XC_RESULT_SCALE:
2461 case XC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002462 bc_num_free(&r->d.n);
2463 break;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01002464 case XC_RESULT_VAR:
2465 case XC_RESULT_ARRAY:
2466 case XC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002467 free(r->d.id.name);
2468 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002469 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002470 // Do nothing.
2471 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002472 }
2473}
2474
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002475static int bad_input_byte(char c)
2476{
2477 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
2478 || c > 0x7e
2479 ) {
2480 bc_error_fmt("illegal character 0x%02x", c);
2481 return 1;
2482 }
2483 return 0;
2484}
2485
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002486static void bc_read_line(BcVec *vec, FILE *fp)
2487{
2488 again:
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002489 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002490 fflush_and_check();
2491
2492#if ENABLE_FEATURE_BC_SIGNALS
2493 if (G_interrupt) { // ^C was pressed
2494 intr:
2495 if (fp != stdin) {
2496 // ^C while running a script (bc SCRIPT): die.
2497 // We do not return to interactive prompt:
2498 // user might be running us from a shell,
2499 // and SCRIPT might be intended to terminate
2500 // (e.g. contain a "halt" stmt).
2501 // ^C dropping user into a bc prompt instead of
2502 // the shell would be unexpected.
2503 xfunc_die();
2504 }
2505 // ^C while interactive input
2506 G_interrupt = 0;
2507 // GNU bc says "interrupted execution."
2508 // GNU dc says "Interrupt!"
2509 fputs("\ninterrupted execution\n", stderr);
2510 }
2511
2512# if ENABLE_FEATURE_EDITING
2513 if (G_ttyin && fp == stdin) {
2514 int n, i;
2515# define line_buf bb_common_bufsiz1
2516 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
2517 if (n <= 0) { // read errors or EOF, or ^D, or ^C
2518 if (n == 0) // ^C
2519 goto intr;
2520 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
2521 return;
2522 }
2523 i = 0;
2524 for (;;) {
2525 char c = line_buf[i++];
2526 if (!c) break;
2527 if (bad_input_byte(c)) goto again;
2528 }
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002529 bc_vec_string(vec, n, line_buf);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002530# undef line_buf
2531 } else
2532# endif
2533#endif
2534 {
2535 int c;
2536 bool bad_chars = 0;
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002537
2538 do {
2539#if ENABLE_FEATURE_BC_SIGNALS
2540 if (G_interrupt) {
2541 // ^C was pressed: ignore entire line, get another one
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002542 bc_vec_pop_all(vec);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002543 goto intr;
2544 }
2545#endif
2546 do c = fgetc(fp); while (c == '\0');
2547 if (c == EOF) {
2548 if (ferror(fp))
2549 bb_perror_msg_and_die("input error");
2550 // Note: EOF does not append '\n'
2551 break;
2552 }
2553 bad_chars |= bad_input_byte(c);
2554 bc_vec_pushByte(vec, (char)c);
2555 } while (c != '\n');
2556
2557 if (bad_chars) {
2558 // Bad chars on this line
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002559 if (!G.prs.lex_filename) { // stdin
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002560 // ignore entire line, get another one
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002561 goto again;
2562 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002563 bb_perror_msg_and_die("file '%s' is not text", G.prs.lex_filename);
Denys Vlasenko2097ac82018-12-24 05:00:36 +01002564 }
2565 bc_vec_pushZeroByte(vec);
2566 }
2567}
2568
2569//
2570// Parsing routines
2571//
2572
2573static bool bc_num_strValid(const char *val, size_t base)
2574{
2575 BcDig b;
2576 bool radix;
2577
2578 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2579 radix = false;
2580 for (;;) {
2581 BcDig c = *val++;
2582 if (c == '\0')
2583 break;
2584 if (c == '.') {
2585 if (radix) return false;
2586 radix = true;
2587 continue;
2588 }
2589 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2590 return false;
2591 }
2592 return true;
2593}
2594
2595// Note: n is already "bc_num_zero()"ed,
2596// leading zeroes in "val" are removed
2597static void bc_num_parseDecimal(BcNum *n, const char *val)
2598{
2599 size_t len, i;
2600 const char *ptr;
2601
2602 len = strlen(val);
2603 if (len == 0)
2604 return;
2605
2606 bc_num_expand(n, len);
2607
2608 ptr = strchr(val, '.');
2609
2610 n->rdx = 0;
2611 if (ptr != NULL)
2612 n->rdx = (size_t)((val + len) - (ptr + 1));
2613
2614 for (i = 0; val[i]; ++i) {
2615 if (val[i] != '0' && val[i] != '.') {
2616 // Not entirely zero value - convert it, and exit
2617 i = len - 1;
2618 for (;;) {
2619 n->num[n->len] = val[i] - '0';
2620 ++n->len;
2621 skip_dot:
2622 if (i == 0) break;
2623 if (val[--i] == '.') goto skip_dot;
2624 }
2625 break;
2626 }
2627 }
2628 // if for() exits without hitting if(), the value is entirely zero
2629}
2630
2631// Note: n is already "bc_num_zero()"ed,
2632// leading zeroes in "val" are removed
2633static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2634{
2635 BcStatus s;
2636 BcNum temp, mult, result;
2637 BcNum base;
2638 BcDig base_digs[ULONG_NUM_BUFSIZE];
2639 BcDig c = '\0';
2640 unsigned long v;
2641 size_t i, digits;
2642
2643 for (i = 0; ; ++i) {
2644 if (val[i] == '\0')
2645 return;
2646 if (val[i] != '.' && val[i] != '0')
2647 break;
2648 }
2649
2650 bc_num_init_DEF_SIZE(&temp);
2651 bc_num_init_DEF_SIZE(&mult);
2652 base.cap = ARRAY_SIZE(base_digs);
2653 base.num = base_digs;
2654 bc_num_ulong2num(&base, base_t);
2655
2656 for (;;) {
2657 c = *val++;
2658 if (c == '\0') goto int_err;
2659 if (c == '.') break;
2660
2661 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2662
2663 s = zbc_num_mul(n, &base, &mult, 0);
2664 if (s) goto int_err;
2665 bc_num_ulong2num(&temp, v);
2666 s = zbc_num_add(&mult, &temp, n, 0);
2667 if (s) goto int_err;
2668 }
2669
2670 bc_num_init(&result, base.len);
2671 //bc_num_zero(&result); - already is
2672 bc_num_one(&mult);
2673
2674 digits = 0;
2675 for (;;) {
2676 c = *val++;
2677 if (c == '\0') break;
2678 digits++;
2679
2680 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2681
2682 s = zbc_num_mul(&result, &base, &result, 0);
2683 if (s) goto err;
2684 bc_num_ulong2num(&temp, v);
2685 s = zbc_num_add(&result, &temp, &result, 0);
2686 if (s) goto err;
2687 s = zbc_num_mul(&mult, &base, &mult, 0);
2688 if (s) goto err;
2689 }
2690
2691 s = zbc_num_div(&result, &mult, &result, digits);
2692 if (s) goto err;
2693 s = zbc_num_add(n, &result, n, digits);
2694 if (s) goto err;
2695
2696 if (n->len != 0) {
2697 if (n->rdx < digits)
2698 bc_num_extend(n, digits - n->rdx);
2699 } else
2700 bc_num_zero(n);
2701 err:
2702 bc_num_free(&result);
2703 int_err:
2704 bc_num_free(&mult);
2705 bc_num_free(&temp);
2706}
2707
2708static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
2709{
2710 if (!bc_num_strValid(val, base_t))
2711 RETURN_STATUS(bc_error("bad number string"));
2712
2713 bc_num_zero(n);
2714 while (*val == '0') val++;
2715
2716 if (base_t == 10)
2717 bc_num_parseDecimal(n, val);
2718 else
2719 bc_num_parseBase(n, val, base_t);
2720
2721 RETURN_STATUS(BC_STATUS_SUCCESS);
2722}
2723#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
2724
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002725static bool bc_lex_more_input(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002726{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002727 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002728
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002729 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002730
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002731 bc_read_line(&G.input_buffer, G.prs.lex_input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002732
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002733 p->lex_inbuf = G.input_buffer.v;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002734// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002735
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002736 return G.input_buffer.len > 1;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002737}
2738
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002739// p->lex_inbuf points to the current string to be parsed.
2740// if p->lex_inbuf points to '\0', it's either EOF or it points after
2741// last processed line's terminating '\n' (and more reading needs to be done
2742// to get next character).
2743//
2744// If you are in a situation where that is a possibility, call peek_inbuf().
2745// If necessary, it performs more reading and changes p->lex_inbuf,
2746// then it returns *p->lex_inbuf (which will be '\0' only if it's EOF).
2747// After it, just referencing *p->lex_inbuf is valid, and if it wasn't '\0',
2748// it's ok to do p->lex_inbuf++ once without end-of-buffer checking.
2749//
2750// eat_inbuf() is equvalent to "peek_inbuf(); if (c) p->lex_inbuf++":
2751// it returns current char and advances the pointer (if not EOF).
2752// After eat_inbuf(), referencing p->lex_inbuf[-1] and *p->lex_inbuf is valid.
2753//
2754// In many cases, you can use fast *p->lex_inbuf instead of peek_inbuf():
2755// unless prev char might have been '\n', *p->lex_inbuf is '\0' ONLY
2756// on real EOF, not end-of-buffer.
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002757//
2758// bc cases to test interactively:
2759// 1 #comment\ - prints "1<newline>" at once (comment is not continued)
2760// 1 #comment/* - prints "1<newline>" at once
2761// 1 #comment" - prints "1<newline>" at once
2762// 1\#comment - error at once (\ is not a line continuation)
2763// 1 + /*"*/2 - prints "3<newline>" at once
2764// 1 + /*#*/2 - prints "3<newline>" at once
2765// "str\" - prints "str\" at once
2766// "str#" - prints "str#" at once
2767// "str/*" - prints "str/*" at once
2768// "str#\ - waits for second line
2769// end" - ...prints "str#\<newline>end"
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002770static char peek_inbuf(void)
2771{
Denys Vlasenkob1b79962018-12-26 18:46:03 +01002772 if (*G.prs.lex_inbuf == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002773 if (G.prs.lex_input_fp)
2774 if (!bc_lex_more_input())
2775 G.prs.lex_input_fp = NULL;
2776 }
2777 return *G.prs.lex_inbuf;
2778}
2779static char eat_inbuf(void)
2780{
2781 char c = peek_inbuf();
2782 if (c) G.prs.lex_inbuf++;
2783 return c;
2784}
2785
2786static void bc_lex_lineComment(void)
2787{
2788 BcParse *p = &G.prs;
2789 char c;
2790
2791 // Try: echo -n '#foo' | bc
2792 p->lex = XC_LEX_WHITESPACE;
2793
2794 // We depend here on input being done in whole lines:
2795 // '\0' which isn't the EOF can only be seen after '\n'.
2796 while ((c = *p->lex_inbuf) != '\n' && c != '\0')
2797 p->lex_inbuf++;
2798}
2799
2800static void bc_lex_whitespace(void)
2801{
2802 BcParse *p = &G.prs;
2803
2804 p->lex = XC_LEX_WHITESPACE;
2805 for (;;) {
2806 // We depend here on input being done in whole lines:
2807 // '\0' which isn't the EOF can only be seen after '\n'.
2808 char c = *p->lex_inbuf;
2809 if (c == '\n') // this is XC_LEX_NLINE, not XC_LEX_WHITESPACE
2810 break;
2811 if (!isspace(c))
2812 break;
2813 p->lex_inbuf++;
2814 }
2815}
2816
2817static BC_STATUS zbc_lex_number(char last)
2818{
2819 BcParse *p = &G.prs;
2820 bool pt;
2821
2822 bc_vec_pop_all(&p->lex_strnumbuf);
2823 bc_vec_pushByte(&p->lex_strnumbuf, last);
2824
2825 pt = (last == '.');
2826 p->lex = XC_LEX_NUMBER;
2827 for (;;) {
2828 // We depend here on input being done in whole lines:
2829 // '\0' which isn't the EOF can only be seen after '\n'.
2830 char c = *p->lex_inbuf;
2831 check_c:
2832 if (c == '\0')
2833 break;
2834 if (c == '\\' && p->lex_inbuf[1] == '\n') {
2835 p->lex_inbuf += 2;
2836 p->lex_line++;
2837 c = peek_inbuf(); // force next line to be read
2838 goto check_c;
2839 }
2840 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2841 if (c != '.') break;
2842 // if '.' was already seen, stop on second one:
2843 if (pt) break;
2844 pt = true;
2845 }
2846 // c is one of "0-9A-F."
2847 last = c;
2848 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
2849 p->lex_inbuf++;
2850 }
2851 if (last == '.') // remove trailing '.' if any
2852 bc_vec_pop(&p->lex_strnumbuf);
2853 bc_vec_pushZeroByte(&p->lex_strnumbuf);
2854
2855 G.err_line = G.prs.lex_line;
2856 RETURN_STATUS(BC_STATUS_SUCCESS);
2857}
2858#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
2859
2860static void bc_lex_name(void)
2861{
2862 BcParse *p = &G.prs;
2863 size_t i;
2864 const char *buf;
2865
2866 p->lex = XC_LEX_NAME;
2867
2868 // Since names can't cross lines with \<newline>,
2869 // we depend on the fact that whole line is in the buffer
2870 i = 0;
2871 buf = p->lex_inbuf - 1;
2872 for (;;) {
2873 char c = buf[i];
2874 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2875 i++;
2876 }
2877
2878#if 0 // We do not protect against people with gigabyte-long names
2879 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2880 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2881 if (i > BC_MAX_STRING)
2882 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2883 }
2884#endif
2885 bc_vec_string(&p->lex_strnumbuf, i, buf);
2886
2887 // Increment the index. We minus 1 because it has already been incremented.
2888 p->lex_inbuf += i - 1;
2889
2890 //return BC_STATUS_SUCCESS;
2891}
2892
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002893IF_BC(static BC_STATUS zbc_lex_token(void);)
2894IF_DC(static BC_STATUS zdc_lex_token(void);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002895#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2896#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002897
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002898static BC_STATUS zbc_lex_next(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002899{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002900 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002901 BcStatus s;
2902
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002903 p->lex_last = p->lex;
Denys Vlasenko2f7352b2018-12-26 18:59:42 +01002904//why?
2905// if (p->lex_last == XC_LEX_EOF)
2906// RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002907
2908 // Loop until failure or we don't have whitespace. This
2909 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko23ea0732018-12-24 15:05:49 +01002910 // Comments are also XC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002911 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002912 do {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002913 if (*p->lex_inbuf == '\0') {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002914 p->lex = XC_LEX_EOF;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01002915 if (peek_inbuf() == '\0')
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002916 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002917 }
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002918 p->lex_next_at = p->lex_inbuf;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002919 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002920 (int)(strchrnul(p->lex_next_at, '\n') - p->lex_next_at),
2921 p->lex_next_at
Denys Vlasenko26384542018-12-25 18:37:52 +01002922 );
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002923 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002924 IF_BC(s = zbc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002925 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002926 IF_DC(s = zdc_lex_token());
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002927 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002928 } while (!s && p->lex == XC_LEX_WHITESPACE);
2929 dbg_lex("p->lex from string:%d", p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06002930
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002931 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002932}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002933#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002934
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002935#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002936static BC_STATUS zbc_lex_skip_if_at_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002937{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002938 if (G.prs.lex == XC_LEX_NLINE)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002939 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko202dd192018-12-16 17:30:35 +01002940 RETURN_STATUS(BC_STATUS_SUCCESS);
2941}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002942#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002943
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002944static BC_STATUS zbc_lex_next_and_skip_NLINE(void)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002945{
2946 BcStatus s;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002947 s = zbc_lex_next();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002948 if (s) RETURN_STATUS(s);
2949 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002950 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01002951 RETURN_STATUS(s);
2952}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002953#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002954#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01002955
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002956static BC_STATUS zbc_lex_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002957{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002958 G.prs.lex_inbuf = text;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002959 G.prs.lex = G.prs.lex_last = XC_LEX_INVALID;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002960 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06002961}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002962#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002963
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002964#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002965static BC_STATUS zbc_lex_identifier(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002966{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002967 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002968 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002969 unsigned i;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002970 const char *buf = p->lex_inbuf - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002971
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002972 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2973 const char *keyword8 = bc_lex_kws[i].name8;
2974 unsigned j = 0;
2975 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2976 j++;
2977 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002978 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002979 if (keyword8[j] != '\0')
2980 continue;
2981 match:
2982 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01002983 if (isalnum(buf[j]) || buf[j]=='_')
2984 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002985 p->lex = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko0b0e8d02018-12-25 21:44:10 +01002986 if (!keyword_is_POSIX(i)) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01002987 s = zbc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
2988 if (s) RETURN_STATUS(s);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002989 }
2990
2991 // We minus 1 because the index has already been incremented.
Denys Vlasenkoc192b042018-12-25 23:15:59 +01002992 p->lex_inbuf += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002993 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002994 }
2995
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01002996 bc_lex_name();
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002997 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002998
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01002999 if (p->lex_strnumbuf.len > 2) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003000 // Prevent this:
3001 // >>> qwe=1
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003002 // bc: POSIX only allows one character names; this is bad: 'qwe=1
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003003 // '
3004 unsigned len = strchrnul(buf, '\n') - buf;
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003005 s = zbc_posix_error_fmt("POSIX only allows one character names; this is bad: '%.*s'", len, buf);
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003006 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003007
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003008 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003009}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003010#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003011
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003012static BC_STATUS zbc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003013{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003014 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003015
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003016 p->lex = XC_LEX_STR;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003017 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003018 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003019 char c = peek_inbuf(); // strings can cross lines
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003020 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003021 RETURN_STATUS(bc_error("unterminated string1"));
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003022 }
3023 if (c == '"')
3024 break;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003025 if (c == '\n')
3026 p->lex_line++;
3027 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3028 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003029 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003030 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3031 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003032
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003033 G.err_line = p->lex_line;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003034 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003035}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003036#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003037
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003038static void parse_lex_by_checking_eq_sign(unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003039{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003040 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003041 if (*p->lex_inbuf == '=') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003042 // ^^^ not using peek_inbuf() since '==' etc can't be split across lines
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003043 p->lex_inbuf++;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003044 with_and_without >>= 8; // store "with" value
3045 } // else store "without" value
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003046 p->lex = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003047}
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003048#define parse_lex_by_checking_eq_sign(with, without) \
3049 parse_lex_by_checking_eq_sign(((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003050
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003051static BC_STATUS zbc_lex_comment(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003052{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003053 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003054
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003055 p->lex = XC_LEX_WHITESPACE;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003056 // here lex_inbuf is at '*' of opening comment delimiter
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003057 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003058 char c;
3059
3060 p->lex_inbuf++;
3061 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003062 check_star:
3063 if (c == '*') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003064 p->lex_inbuf++;
3065 c = peek_inbuf();
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003066 if (c == '/')
3067 break;
3068 goto check_star;
3069 }
3070 if (c == '\0') {
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01003071 RETURN_STATUS(bc_error("unterminated comment"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003072 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003073 if (c == '\n')
3074 p->lex_line++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003075 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003076 p->lex_inbuf++; // skip trailing '/'
Gavin Howard01055ba2018-11-03 11:00:21 -06003077
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003078 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003079 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003080}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003081#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003082
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003083#undef zbc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003084static BC_STATUS zbc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003085{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003086 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003087 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003088 char c = eat_inbuf();
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003089 char c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003090
3091 // This is the workhorse of the lexer.
3092 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003093// case '\0': // probably never reached
3094// p->lex_inbuf--;
3095// p->lex = XC_LEX_EOF;
3096// break;
3097 case '\n':
3098 p->lex_line++;
3099 p->lex = XC_LEX_NLINE;
3100 break;
3101 case '\t':
3102 case '\v':
3103 case '\f':
3104 case '\r':
3105 case ' ':
3106 bc_lex_whitespace();
3107 break;
3108 case '!':
3109 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3110 if (p->lex == BC_LEX_OP_BOOL_NOT) {
3111 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko79587cb2018-12-24 18:11:41 +01003112 if (s) RETURN_STATUS(s);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003113 }
3114 break;
3115 case '"':
3116 s = zbc_lex_string();
3117 break;
3118 case '#':
3119 s = zbc_POSIX_does_not_allow("'#' script comments");
3120 if (s) RETURN_STATUS(s);
3121 bc_lex_lineComment();
3122 break;
3123 case '%':
3124 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS);
3125 break;
3126 case '&':
3127 c2 = *p->lex_inbuf;
3128 if (c2 == '&') {
3129 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
3130 if (s) RETURN_STATUS(s);
3131 p->lex_inbuf++;
3132 p->lex = BC_LEX_OP_BOOL_AND;
3133 } else {
3134 p->lex = XC_LEX_INVALID;
3135 s = bc_error_bad_character('&');
3136 }
3137 break;
3138 case '(':
3139 case ')':
3140 p->lex = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3141 break;
3142 case '*':
3143 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MULTIPLY, XC_LEX_OP_MULTIPLY);
3144 break;
3145 case '+':
3146 c2 = *p->lex_inbuf;
3147 if (c2 == '+') {
3148 p->lex_inbuf++;
3149 p->lex = BC_LEX_OP_INC;
3150 } else
3151 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_PLUS, XC_LEX_OP_PLUS);
3152 break;
3153 case ',':
3154 p->lex = BC_LEX_COMMA;
3155 break;
3156 case '-':
3157 c2 = *p->lex_inbuf;
3158 if (c2 == '-') {
3159 p->lex_inbuf++;
3160 p->lex = BC_LEX_OP_DEC;
3161 } else
3162 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MINUS, XC_LEX_OP_MINUS);
3163 break;
3164 case '.':
3165 if (isdigit(*p->lex_inbuf))
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003166 s = zbc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003167 else {
3168 p->lex = BC_LEX_KEY_LAST;
3169 s = zbc_POSIX_does_not_allow("'.' as 'last'");
3170 }
3171 break;
3172 case '/':
3173 c2 = *p->lex_inbuf;
3174 if (c2 == '*')
3175 s = zbc_lex_comment();
3176 else
3177 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_DIVIDE, XC_LEX_OP_DIVIDE);
3178 break;
3179 case '0':
3180 case '1':
3181 case '2':
3182 case '3':
3183 case '4':
3184 case '5':
3185 case '6':
3186 case '7':
3187 case '8':
3188 case '9':
3189 case 'A':
3190 case 'B':
3191 case 'C':
3192 case 'D':
3193 case 'E':
3194 case 'F':
3195 s = zbc_lex_number(c);
3196 break;
3197 case ';':
3198 p->lex = BC_LEX_SCOLON;
3199 break;
3200 case '<':
3201 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_LE, XC_LEX_OP_REL_LT);
3202 break;
3203 case '=':
3204 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3205 break;
3206 case '>':
3207 parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_GE, XC_LEX_OP_REL_GT);
3208 break;
3209 case '[':
3210 case ']':
3211 p->lex = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3212 break;
3213 case '\\':
3214 if (*p->lex_inbuf == '\n') {
3215 p->lex = XC_LEX_WHITESPACE;
3216 p->lex_inbuf++;
3217 } else
3218 s = bc_error_bad_character(c);
3219 break;
3220 case '^':
3221 parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_POWER, XC_LEX_OP_POWER);
3222 break;
3223 case 'a':
3224 case 'b':
3225 case 'c':
3226 case 'd':
3227 case 'e':
3228 case 'f':
3229 case 'g':
3230 case 'h':
3231 case 'i':
3232 case 'j':
3233 case 'k':
3234 case 'l':
3235 case 'm':
3236 case 'n':
3237 case 'o':
3238 case 'p':
3239 case 'q':
3240 case 'r':
3241 case 's':
3242 case 't':
3243 case 'u':
3244 case 'v':
3245 case 'w':
3246 case 'x':
3247 case 'y':
3248 case 'z':
3249 s = zbc_lex_identifier();
3250 break;
3251 case '{':
3252 case '}':
3253 p->lex = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3254 break;
3255 case '|':
3256 c2 = *p->lex_inbuf;
3257 if (c2 == '|') {
3258 s = zbc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
3259 if (s) RETURN_STATUS(s);
3260 p->lex_inbuf++;
3261 p->lex = BC_LEX_OP_BOOL_OR;
3262 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003263 p->lex = XC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003264 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003265 }
3266 break;
3267 default:
3268 p->lex = XC_LEX_INVALID;
3269 s = bc_error_bad_character(c);
3270 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003271 }
3272
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003273 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003274}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003275#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003276#endif // ENABLE_BC
3277
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003278#if ENABLE_DC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003279static BC_STATUS zdc_lex_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003280{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003281 BcParse *p = &G.prs;
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003282 if (G_exreg && isspace(*p->lex_inbuf)) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003283 bc_lex_whitespace(); // eats whitespace (but not newline)
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003284 p->lex_inbuf++; // bc_lex_name() expects this
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003285 bc_lex_name();
Denys Vlasenko81293c82018-12-24 01:53:55 +01003286 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003287 bc_vec_pop_all(&p->lex_strnumbuf);
Denys Vlasenkoc192b042018-12-25 23:15:59 +01003288 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003289 bc_vec_pushZeroByte(&p->lex_strnumbuf);
3290 p->lex = XC_LEX_NAME;
Gavin Howard01055ba2018-11-03 11:00:21 -06003291 }
3292
Denys Vlasenko29301232018-12-11 15:29:32 +01003293 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003294}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003295#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003296
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003297static BC_STATUS zdc_lex_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003298{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003299 BcParse *p = &G.prs;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003300 size_t depth;
Gavin Howard01055ba2018-11-03 11:00:21 -06003301
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003302 p->lex = XC_LEX_STR;
3303 bc_vec_pop_all(&p->lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003304
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003305 depth = 1;
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003306 for (;;) {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003307 char c = peek_inbuf();
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003308 if (c == '\0') {
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003309 RETURN_STATUS(bc_error("unterminated string"));
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003310 }
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003311 if (c == '[') depth++;
3312 if (c == ']')
3313 if (--depth == 0)
3314 break;
3315 if (c == '\n')
3316 p->lex_line++;
3317 bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf);
3318 p->lex_inbuf++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003319 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003320 bc_vec_pushZeroByte(&p->lex_strnumbuf);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003321 p->lex_inbuf++; // skip trailing ']'
Gavin Howard01055ba2018-11-03 11:00:21 -06003322
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003323 G.err_line = p->lex_line;
Denys Vlasenko29301232018-12-11 15:29:32 +01003324 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003325}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003326#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003327
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003328#undef zdc_lex_token
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003329static BC_STATUS zdc_lex_token(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003330{
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003331 BcParse *p = &G.prs;
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003332 static const //BcLexType - should be this type, but narrower type saves size:
3333 uint8_t
3334 dc_lex_regs[] = {
Denys Vlasenko69560f42018-12-24 14:14:23 +01003335 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 +01003336 XC_LEX_OP_REL_LT, XC_LEX_OP_REL_GT, DC_LEX_SCOLON, DC_LEX_COLON,
3337 DC_LEX_ELSE, DC_LEX_LOAD, DC_LEX_LOAD_POP, DC_LEX_OP_ASSIGN,
Denys Vlasenko7d9be0b2018-12-24 12:25:20 +01003338 DC_LEX_STORE_PUSH,
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003339 };
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003340
Denys Vlasenko81293c82018-12-24 01:53:55 +01003341 BcStatus s;
3342 char c, c2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003343 size_t i;
3344
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003345 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003346 if (p->lex_last == dc_lex_regs[i])
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003347 RETURN_STATUS(zdc_lex_register());
Gavin Howard01055ba2018-11-03 11:00:21 -06003348 }
3349
Denys Vlasenko81293c82018-12-24 01:53:55 +01003350 s = BC_STATUS_SUCCESS;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003351 c = eat_inbuf();
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003352 if (c >= '%' && c <= '~'
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003353 && (p->lex = dc_char_to_LEX[c - '%']) != XC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003354 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003355 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003356 }
3357
3358 // This is the workhorse of the lexer.
3359 switch (c) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003360// case '\0': // probably never reached
3361// p->lex = XC_LEX_EOF;
3362// break;
3363 case '\n':
3364 // '\n' is XC_LEX_NLINE, not XC_LEX_WHITESPACE
3365 // (and "case '\n':" is not just empty here)
3366 // only to allow interactive dc have a way to exit
3367 // "parse" stage of "parse,execute" loop
3368 // on <enter>, not on _next_ token (which would mean
3369 // commands are not executed on pressing <enter>).
3370 // IOW: typing "1p<enter>" should print "1" _at once_,
3371 // not after some more input.
3372 p->lex_line++;
3373 p->lex = XC_LEX_NLINE;
3374 break;
3375 case '\t':
3376 case '\v':
3377 case '\f':
3378 case '\r':
3379 case ' ':
3380 bc_lex_whitespace();
3381 break;
3382 case '!':
3383 c2 = *p->lex_inbuf;
3384 if (c2 == '=')
3385 p->lex = XC_LEX_OP_REL_NE;
3386 else if (c2 == '<')
3387 p->lex = XC_LEX_OP_REL_LE;
3388 else if (c2 == '>')
3389 p->lex = XC_LEX_OP_REL_GE;
3390 else
3391 RETURN_STATUS(bc_error_bad_character(c));
3392 p->lex_inbuf++;
3393 break;
3394 case '#':
3395 bc_lex_lineComment();
3396 break;
3397 case '.':
3398 if (isdigit(*p->lex_inbuf))
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003399 s = zbc_lex_number(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003400 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003401 s = bc_error_bad_character(c);
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003402 break;
3403 case '0':
3404 case '1':
3405 case '2':
3406 case '3':
3407 case '4':
3408 case '5':
3409 case '6':
3410 case '7':
3411 case '8':
3412 case '9':
3413 case 'A':
3414 case 'B':
3415 case 'C':
3416 case 'D':
3417 case 'E':
3418 case 'F':
3419 s = zbc_lex_number(c);
3420 break;
3421 case '[':
3422 s = zdc_lex_string();
3423 break;
3424 default:
3425 p->lex = XC_LEX_INVALID;
3426 s = bc_error_bad_character(c);
3427 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003428 }
3429
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003430 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003431}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003432#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003433#endif // ENABLE_DC
3434
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003435static void bc_parse_push(char i)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003436{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003437 BcParse *p = &G.prs;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003438 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003439 bc_vec_pushByte(&p->func->code, i);
3440}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003441
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003442static void bc_parse_pushName(char *name)
Gavin Howard01055ba2018-11-03 11:00:21 -06003443{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003444 while (*name)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003445 bc_parse_push(*name++);
3446 bc_parse_push(BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003447}
3448
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003449static void bc_parse_pushIndex(size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003450{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003451 size_t mask;
3452 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003453
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003454 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003455 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3456 amt = sizeof(idx);
3457 do {
3458 if (idx & mask) break;
3459 mask >>= 8;
3460 amt--;
3461 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003462
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003463 bc_parse_push(amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003464
3465 while (idx != 0) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003466 bc_parse_push((unsigned char)idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003467 idx >>= 8;
3468 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003469}
3470
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003471#if ENABLE_BC
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003472static void bc_parse_pushJUMP(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003473{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003474 bc_parse_push(BC_INST_JUMP);
3475 bc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003476}
3477
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003478static void bc_parse_pushJUMP_ZERO(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003479{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003480 bc_parse_push(BC_INST_JUMP_ZERO);
3481 bc_parse_pushIndex(idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003482}
3483
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003484static BC_STATUS zbc_parse_pushSTR(void)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003485{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003486 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003487 char *str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003488
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003489 bc_parse_push(XC_INST_STR);
3490 bc_parse_pushIndex(p->func->strs.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003491 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003492
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003493 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003494}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003495#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003496#endif
3497
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003498static void bc_parse_pushNUM(void)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003499{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003500 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003501 char *num = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003502#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003503 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003504#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003505 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003506#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003507 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003508#endif
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003509 bc_parse_push(XC_INST_NUM);
3510 bc_parse_pushIndex(idx);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003511}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003512
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003513static BC_STATUS zbc_parse_text_init(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003514{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003515 BcParse *p = &G.prs;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003516 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003517
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003518 RETURN_STATUS(zbc_lex_text_init(text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003519}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003520#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003521
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003522// Called when parsing or execution detects a failure,
3523// resets execution structures.
3524static void bc_program_reset(void)
3525{
3526 BcFunc *f;
3527 BcInstPtr *ip;
3528
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003529 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003530 bc_vec_pop_all(&G.prog.results);
3531
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003532 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003533 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003534 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003535}
3536
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003537// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003538// resets parsing structures.
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003539static void bc_parse_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003540{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003541 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003542 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003543 bc_func_free(p->func);
3544 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003545
Denys Vlasenko65e10462018-12-19 15:13:14 +01003546 p->fidx = BC_PROG_MAIN;
3547 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003548 }
3549
Denys Vlasenkob1b79962018-12-26 18:46:03 +01003550 p->lex_inbuf += strlen(p->lex_inbuf);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003551 p->lex = XC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003552
Denys Vlasenko503faf92018-12-20 16:24:18 +01003553 IF_BC(bc_vec_pop_all(&p->exits);)
3554 IF_BC(bc_vec_pop_all(&p->conds);)
3555 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003556
Denys Vlasenkod38af482018-12-04 19:11:02 +01003557 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003558}
3559
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003560static void bc_parse_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003561{
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003562 IF_BC(bc_vec_free(&G.prs.exits);)
3563 IF_BC(bc_vec_free(&G.prs.conds);)
3564 IF_BC(bc_vec_free(&G.prs.ops);)
Denys Vlasenko7d32e252018-12-26 18:32:43 +01003565 bc_vec_free(&G.prs.lex_strnumbuf);
Gavin Howard01055ba2018-11-03 11:00:21 -06003566}
3567
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003568static void bc_parse_create(size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003569{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003570 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003571 memset(p, 0, sizeof(BcParse));
3572
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003573 bc_char_vec_init(&p->lex_strnumbuf);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003574 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3575 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3576 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003577
Denys Vlasenkofa210792018-12-19 19:35:40 +01003578 p->fidx = fidx;
3579 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003580}
3581
Denys Vlasenko04715442018-12-21 00:10:26 +01003582static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003583{
Denys Vlasenko04715442018-12-21 00:10:26 +01003584 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003585 BcFunc f;
3586 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003587 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003588 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003589 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003590}
3591
3592#if ENABLE_BC
3593
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003594// Note: takes ownership of 'name' (must be malloced)
3595static size_t bc_program_addFunc(char *name)
3596{
3597 size_t idx;
3598 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003599 int inserted;
3600
3601 entry.name = name;
3602 entry.idx = G.prog.fns.len;
3603
3604 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3605 if (!inserted) free(name);
3606
3607 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3608 idx = entry_ptr->idx;
3609
3610 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003611 // There is already a function with this name.
3612 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003613 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003614 bc_func_free(func);
3615 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003616 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003617 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003618 }
3619
3620 return idx;
3621}
3622
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003623#define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003624// We can calculate the conversion between tokens and exprs by subtracting the
3625// position of the first operator in the lex enum and adding the position of the
3626// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko69560f42018-12-24 14:14:23 +01003627#define BC_TOKEN_2_INST(t) ((char) ((t) - XC_LEX_OP_POWER + XC_INST_POWER))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003628
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003629static BcStatus bc_parse_expr_empty_ok(uint8_t flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003630
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003631static BC_STATUS zbc_parse_expr(uint8_t flags)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003632{
3633 BcStatus s;
3634
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003635 s = bc_parse_expr_empty_ok(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003636 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3637 RETURN_STATUS(bc_error("empty expression"));
3638 RETURN_STATUS(s);
3639}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003640#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003641
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003642static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003643#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003644
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003645static BC_STATUS zbc_parse_stmt(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003646{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003647 RETURN_STATUS(zbc_parse_stmt_possibly_auto(false));
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003648}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003649#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003650
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003651static BC_STATUS zbc_parse_stmt_allow_NLINE_before(const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003652{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003653 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003654 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3655 // Same for "else", "while()", "for()".
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003656 BcStatus s = zbc_lex_next_and_skip_NLINE();
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003657 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003658 if (p->lex == XC_LEX_NLINE)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003659 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003660
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003661 RETURN_STATUS(zbc_parse_stmt());
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003662}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003663#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003664
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003665static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003666{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003667 BcParse *p = &G.prs;
Denys Vlasenko69560f42018-12-24 14:14:23 +01003668 char l, r = bc_parse_op_PREC(type - XC_LEX_1st_op);
3669 bool left = bc_parse_op_LEFT(type - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003670
3671 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003672 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003673 if (t == BC_LEX_LPAREN) break;
3674
Denys Vlasenko69560f42018-12-24 14:14:23 +01003675 l = bc_parse_op_PREC(t - XC_LEX_1st_op);
Gavin Howard01055ba2018-11-03 11:00:21 -06003676 if (l >= r && (l != r || !left)) break;
3677
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003678 bc_parse_push(BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003679 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003680 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003681 }
3682
3683 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003684}
3685
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003686static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003687{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003688 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003689 BcLexType top;
3690
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003691 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003692 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003693 top = BC_PARSE_TOP_OP(p);
3694
3695 while (top != BC_LEX_LPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003696 bc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003697
3698 bc_vec_pop(&p->ops);
Denys Vlasenko69560f42018-12-24 14:14:23 +01003699 *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003700
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003701 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003702 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003703 top = BC_PARSE_TOP_OP(p);
3704 }
3705
3706 bc_vec_pop(&p->ops);
3707
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01003708 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003709}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003710#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003711
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003712static BC_STATUS zbc_parse_params(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003713{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003714 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003715 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003716 size_t nparams;
3717
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003718 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003719 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3720
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003721 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003722 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003723
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003724 nparams = 0;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003725 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003726 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003727 s = zbc_parse_expr(flags);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003728 if (s) RETURN_STATUS(s);
3729 nparams++;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003730 if (p->lex != BC_LEX_COMMA) {
3731 if (p->lex == BC_LEX_RPAREN)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003732 break;
3733 RETURN_STATUS(bc_error_bad_token());
3734 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003735 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003736 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003737 }
3738 }
3739
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003740 bc_parse_push(BC_INST_CALL);
3741 bc_parse_pushIndex(nparams);
Gavin Howard01055ba2018-11-03 11:00:21 -06003742
Denys Vlasenko26819db2018-12-12 16:08:46 +01003743 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003744}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003745#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003746
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003747// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003748static BC_STATUS zbc_parse_call(char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003749{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003750 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003751 BcStatus s;
3752 BcId entry, *entry_ptr;
3753 size_t idx;
3754
3755 entry.name = name;
3756
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003757 s = zbc_parse_params(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003758 if (s) goto err;
3759
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003760 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003761 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003762 goto err;
3763 }
3764
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003765 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003766
3767 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003768 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003769 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003770 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003771 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003772 free(name);
3773
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003774 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003775 bc_parse_pushIndex(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003776
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003777 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003778 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003779 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003780 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003781}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003782#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003783
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003784static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003785{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003786 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003787 BcStatus s;
3788 char *name;
3789
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003790 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003791 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003792 if (s) goto err;
3793
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003794 if (p->lex == BC_LEX_LBRACKET) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003795 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003796 if (s) goto err;
3797
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003798 if (p->lex == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003799 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003800 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003801 goto err;
3802 }
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003803 *type = XC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003804 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003805 *type = XC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003806 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003807 s = zbc_parse_expr(flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003808 if (s) goto err;
3809 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003810 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06003811 if (s) goto err;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003812 bc_parse_push(*type);
3813 bc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003814 free(name);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003815 } else if (p->lex == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003816 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003817 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003818 goto err;
3819 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003820 *type = BC_INST_CALL;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003821 s = zbc_parse_call(name, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003822 } else {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003823 *type = XC_INST_VAR;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003824 bc_parse_push(XC_INST_VAR);
3825 bc_parse_pushName(name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003826 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003827 }
3828
Denys Vlasenko26819db2018-12-12 16:08:46 +01003829 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003830 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003831 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003832 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003833}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003834#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003835
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003836static BC_STATUS zbc_parse_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06003837{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003838 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003839 BcStatus s;
3840
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003841 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003842 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003843 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003844
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003845 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003846 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003847 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003848
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003849 bc_parse_push(XC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06003850
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003851 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003852}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003853#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003854
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003855static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev)
Gavin Howard01055ba2018-11-03 11:00:21 -06003856{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003857 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003858 BcStatus s;
3859
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003860 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003861 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003862 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003863
3864 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3865
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003866 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003867 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003868
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003869 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003870 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003871
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003872 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003873
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003874 *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003875 bc_parse_push(*prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06003876
Denys Vlasenko5fa74b92018-12-25 17:07:51 +01003877 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003878}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003879#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003880
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003881static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003882{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003883 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003884 BcStatus s;
3885
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003886 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003887 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003888
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003889 if (p->lex != BC_LEX_LPAREN) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003890 *type = XC_INST_SCALE;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003891 bc_parse_push(XC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003892 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003893 }
3894
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003895 *type = XC_INST_SCALE_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -06003896 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3897
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003898 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003899 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003900
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003901 s = zbc_parse_expr(flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003902 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003903 if (p->lex != BC_LEX_RPAREN)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003904 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003905 bc_parse_push(XC_INST_SCALE_FUNC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003906
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003907 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06003908}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003909#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003910
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003911static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr,
3912 size_t *nexprs, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003913{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003914 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003915 BcStatus s;
3916 BcLexType type;
3917 char inst;
3918 BcInst etype = *prev;
3919
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01003920 if (etype == XC_INST_VAR || etype == XC_INST_ARRAY_ELEM
3921 || etype == XC_INST_SCALE || etype == BC_INST_LAST
3922 || etype == XC_INST_IBASE || etype == XC_INST_OBASE
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003923 ) {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003924 *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003925 bc_parse_push(inst);
3926 s = zbc_lex_next();
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003927 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003928 *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003929 *paren_expr = true;
3930
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003931 s = zbc_lex_next();
Denys Vlasenko26819db2018-12-12 16:08:46 +01003932 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01003933 type = p->lex;
Gavin Howard01055ba2018-11-03 11:00:21 -06003934
3935 // Because we parse the next part of the expression
3936 // right here, we need to increment this.
3937 *nexprs = *nexprs + 1;
3938
3939 switch (type) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003940 case XC_LEX_NAME:
3941 s = zbc_parse_name(prev, flags | BC_PARSE_NOCALL);
3942 break;
3943 case BC_LEX_KEY_IBASE:
3944 case BC_LEX_KEY_LAST:
3945 case BC_LEX_KEY_OBASE:
3946 bc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE);
3947 s = zbc_lex_next();
3948 break;
3949 case BC_LEX_KEY_SCALE:
3950 s = zbc_lex_next();
3951 if (s) RETURN_STATUS(s);
3952 if (p->lex == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003953 s = bc_error_bad_token();
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01003954 else
3955 bc_parse_push(XC_INST_SCALE);
3956 break;
3957 default:
3958 s = bc_error_bad_token();
3959 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003960 }
3961
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003962 if (!s) bc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06003963 }
3964
Denys Vlasenko26819db2018-12-12 16:08:46 +01003965 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003966}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003967#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003968
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003969#if 0
3970#define BC_PARSE_LEAF(p, rparen) \
3971 ((rparen) \
3972 || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \
3973 || (p) == BC_INST_INC_POST \
3974 || (p) == BC_INST_DEC_POST \
3975 )
3976#else
3977static int ok_in_expr(BcInst p)
3978{
3979 return (p >= XC_INST_NUM && p <= XC_INST_SQRT)
3980 || p == BC_INST_INC_POST
3981 || p == BC_INST_DEC_POST
3982 ;
3983}
3984#define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p))
3985#endif
3986
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003987static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn,
3988 bool rparen, size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003989{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003990 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06003991 BcStatus s;
3992 BcLexType type;
3993 BcInst etype = *prev;
3994
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01003995 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003996 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003997
Denys Vlasenko0c45bb222018-12-24 23:22:40 +01003998 type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01003999 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004000
4001 // We can just push onto the op stack because this is the largest
4002 // precedence operator that gets pushed. Inc/dec does not.
Denys Vlasenko69560f42018-12-24 14:14:23 +01004003 if (type != XC_LEX_OP_MINUS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004004 bc_vec_push(&p->ops, &type);
4005 else
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004006 bc_parse_operator(type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004007
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004008 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004009}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004010#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004011
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004012static BC_STATUS zbc_parse_print(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004013{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004014 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004015 BcStatus s;
4016 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004017
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004018 for (;;) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004019 s = zbc_lex_next();
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004020 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004021 type = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004022 if (type == XC_LEX_STR) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004023 s = zbc_parse_pushSTR();
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004024 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004025 s = zbc_parse_expr(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004026 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004027 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004028 bc_parse_push(XC_INST_PRINT_POP);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004029 if (p->lex != BC_LEX_COMMA)
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004030 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004031 }
4032
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004033 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004034}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004035#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004036
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004037static BC_STATUS zbc_parse_return(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004038{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004039 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004040 BcStatus s;
4041 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004042
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004043 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004044 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004045 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004046
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004047 t = p->lex;
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004048 if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004049 bc_parse_push(BC_INST_RET0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004050 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004051 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004052 s = bc_parse_expr_empty_ok(0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004053 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004054 bc_parse_push(BC_INST_RET0);
4055 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004056 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004057 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004058
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004059 if (!paren || p->lex_last != BC_LEX_RPAREN) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004060 s = zbc_POSIX_requires("parentheses around return expressions");
4061 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004062 }
4063
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004064 bc_parse_push(XC_INST_RET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004065 }
4066
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004067 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004068 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004069}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004070#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004071
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004072static void rewrite_label_to_current(size_t idx)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004073{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004074 BcParse *p = &G.prs;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004075 size_t *label = bc_vec_item(&p->func->labels, idx);
4076 *label = p->func->code.len;
4077}
4078
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004079static BC_STATUS zbc_parse_if(void)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004080{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004081 BcParse *p = &G.prs;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004082 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004083 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004084
4085 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004086 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004087 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004088 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004089
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004090 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004091 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004092 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004093 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004094 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004095
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004096 // Encode "if zero, jump to ..."
4097 // Pushed value (destination of the jump) is uninitialized,
4098 // will be rewritten to be address of "end of if()" or of "else".
4099 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004100 bc_parse_pushJUMP_ZERO(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004101
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004102 s = zbc_parse_stmt_allow_NLINE_before(STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004103 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004104
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004105 dbg_lex("%s:%d in if after stmt: p->lex:%d", __func__, __LINE__, p->lex);
4106 if (p->lex == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004107 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004108
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004109 // Encode "after then_stmt, jump to end of if()"
4110 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4111 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004112 bc_parse_pushJUMP(ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004113
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004114 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004115 rewrite_label_to_current(ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004116
Denys Vlasenko15850832018-12-16 21:40:54 +01004117 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004118
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004119 s = zbc_parse_stmt_allow_NLINE_before(STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004120 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004121 }
4122
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004123 dbg_lex("%s:%d rewriting label to jump after 'if' body-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004124 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004125
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004126 dbg_lex_done("%s:%d done", __func__, __LINE__);
4127 RETURN_STATUS(s);
4128}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004129#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004130
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004131static BC_STATUS zbc_parse_while(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004132{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004133 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004134 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004135 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004136 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004137
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004138 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004139 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004140 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004141 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004142 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004143
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004144 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004145 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004146 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004147
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004148 bc_vec_push(&p->exits, &ip_idx);
4149 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004150
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004151 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004152 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004153 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004154
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004155 bc_parse_pushJUMP_ZERO(ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004156
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004157 s = zbc_parse_stmt_allow_NLINE_before(STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004158 if (s) RETURN_STATUS(s);
4159
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004160 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004161 bc_parse_pushJUMP(cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004162
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004163 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004164 rewrite_label_to_current(ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004165
4166 bc_vec_pop(&p->exits);
4167 bc_vec_pop(&p->conds);
4168
4169 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004170}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004171#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004172
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004173static BC_STATUS zbc_parse_for(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004174{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004175 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004176 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004177 size_t cond_idx, exit_idx, body_idx, update_idx;
4178
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004179 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004180 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004181 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004182 if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004183 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004184 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004185
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004186 if (p->lex != BC_LEX_SCOLON) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004187 s = zbc_parse_expr(0);
4188 bc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004189 if (s) RETURN_STATUS(s);
4190 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004191 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init");
4192 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004193 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004194
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004195 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004196 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004197 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004198
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004199 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004200 update_idx = cond_idx + 1;
4201 body_idx = update_idx + 1;
4202 exit_idx = body_idx + 1;
4203
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004204 if (p->lex != BC_LEX_SCOLON)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004205 s = zbc_parse_expr(BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004206 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004207 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004208 // This is safe to set because the current token is a semicolon,
4209 // which has no string requirement.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004210 bc_vec_string(&p->lex_strnumbuf, 1, "1");
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004211 bc_parse_pushNUM();
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004212 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004213 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004214 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004215
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004216 if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004217
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004218 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004219 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004220
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004221 bc_parse_pushJUMP_ZERO(exit_idx);
4222 bc_parse_pushJUMP(body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004223
Gavin Howard01055ba2018-11-03 11:00:21 -06004224 bc_vec_push(&p->conds, &update_idx);
4225 bc_vec_push(&p->func->labels, &p->func->code.len);
4226
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004227 if (p->lex != BC_LEX_RPAREN) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004228 s = zbc_parse_expr(0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004229 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004230 if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004231 bc_parse_push(XC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004232 } else {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004233 s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update");
4234 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004235 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004236
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004237 bc_parse_pushJUMP(cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004238 bc_vec_push(&p->func->labels, &p->func->code.len);
4239
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004240 bc_vec_push(&p->exits, &exit_idx);
4241 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004242
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004243 s = zbc_parse_stmt_allow_NLINE_before(STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004244 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004245
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004246 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004247 bc_parse_pushJUMP(update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004248
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004249 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004250 rewrite_label_to_current(exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004251
4252 bc_vec_pop(&p->exits);
4253 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004254
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004255 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004256}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004257#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004258
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004259static BC_STATUS zbc_parse_break_or_continue(BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004260{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004261 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004262 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004263
Gavin Howard01055ba2018-11-03 11:00:21 -06004264 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004265 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4266 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004267 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004268 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004269 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004270 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004271 bc_parse_pushJUMP(i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004272
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004273 RETURN_STATUS(zbc_lex_next());
Gavin Howard01055ba2018-11-03 11:00:21 -06004274}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004275#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004276
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004277static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
4278{
4279 BcId *autoid;
4280 BcId a;
4281 size_t i;
4282
4283 autoid = (void*)f->autos.v;
4284 for (i = 0; i < f->autos.len; i++, autoid++) {
4285 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko3f8752c2018-12-25 21:28:25 +01004286 RETURN_STATUS(bc_error("duplicate function parameter or auto name"));
Denys Vlasenko2097ac82018-12-24 05:00:36 +01004287 }
4288
4289 a.idx = var;
4290 a.name = name;
4291
4292 bc_vec_push(&f->autos, &a);
4293
4294 RETURN_STATUS(BC_STATUS_SUCCESS);
4295}
4296#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
4297
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004298static BC_STATUS zbc_parse_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004299{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004300 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004301 BcStatus s;
4302 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004303 char *name;
4304
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004305 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004306 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004307 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004308 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004309 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004310
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004311 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004312 p->fidx = bc_program_addFunc(name);
4313 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004314
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004315 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004316 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004317 if (p->lex != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004318 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004319 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004320 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004321
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004322 while (p->lex != BC_LEX_RPAREN) {
4323 if (p->lex != XC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004324 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004325
4326 ++p->func->nparams;
4327
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004328 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004329 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004330 if (s) goto err;
4331
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004332 var = p->lex != BC_LEX_LBRACKET;
Gavin Howard01055ba2018-11-03 11:00:21 -06004333
4334 if (!var) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004335 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004336 if (s) goto err;
4337
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004338 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004339 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004340 goto err;
4341 }
4342
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004343 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004344 if (s) goto err;
4345 }
4346
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004347 comma = p->lex == BC_LEX_COMMA;
Gavin Howard01055ba2018-11-03 11:00:21 -06004348 if (comma) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004349 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004350 if (s) goto err;
4351 }
4352
Denys Vlasenko29301232018-12-11 15:29:32 +01004353 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004354 if (s) goto err;
4355 }
4356
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004357 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004358
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004359 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004360 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004361
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004362 if (p->lex != BC_LEX_LBRACE) {
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004363 s = zbc_POSIX_requires("the left brace be on the same line as the function header");
Denys Vlasenkod279d802018-12-24 18:28:56 +01004364 if (s) RETURN_STATUS(s);
4365 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004366
Denys Vlasenko202dd192018-12-16 17:30:35 +01004367 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004368 s = zbc_lex_skip_if_at_NLINE();
Denys Vlasenko202dd192018-12-16 17:30:35 +01004369 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004370 //GNU bc requires a {} block even if function body has single stmt, enforce this?
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004371 if (p->lex != BC_LEX_LBRACE)
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004372 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004373
4374 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004375 s = zbc_parse_stmt_possibly_auto(true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004376 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004377 if (s) RETURN_STATUS(s);
4378
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004379 bc_parse_push(BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004380
4381 // Subsequent code generation is into main program
4382 p->fidx = BC_PROG_MAIN;
4383 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004384
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004385 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004386 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004387 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004388 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004389 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004390 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004391}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004392#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004393
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004394static BC_STATUS zbc_parse_auto(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004395{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004396 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004397 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004398 char *name;
4399
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004400 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004401 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004402 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004403
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004404 for (;;) {
4405 bool var;
Gavin Howard01055ba2018-11-03 11:00:21 -06004406
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004407 if (p->lex != XC_LEX_NAME)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004408 RETURN_STATUS(bc_error("bad 'auto' syntax"));
4409
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004410 name = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004411 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004412 if (s) goto err;
4413
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004414 var = (p->lex != BC_LEX_LBRACKET);
Gavin Howard01055ba2018-11-03 11:00:21 -06004415 if (!var) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004416 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004417 if (s) goto err;
4418
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004419 if (p->lex != BC_LEX_RBRACKET) {
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004420 s = bc_error("bad 'auto' syntax");
Gavin Howard01055ba2018-11-03 11:00:21 -06004421 goto err;
4422 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004423 s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004424 if (s) goto err;
4425 }
4426
Denys Vlasenko29301232018-12-11 15:29:32 +01004427 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004428 if (s) goto err;
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004429
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004430 if (p->lex == XC_LEX_NLINE
4431 || p->lex == BC_LEX_SCOLON
4432 //|| p->lex == BC_LEX_RBRACE // allow "define f() {auto a}"
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004433 ) {
4434 break;
4435 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004436 if (p->lex != BC_LEX_COMMA)
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004437 RETURN_STATUS(bc_error("bad 'auto' syntax"));
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004438 s = zbc_lex_next(); // skip comma
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004439 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004440 }
4441
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004442 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod4b721c2018-12-25 16:39:01 +01004443 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004444 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004445 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004446 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004447 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004448}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004449#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004450
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004451#undef zbc_parse_stmt_possibly_auto
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004452static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004453{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004454 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004455 BcStatus s = BC_STATUS_SUCCESS;
4456
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004457 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
Gavin Howard01055ba2018-11-03 11:00:21 -06004458
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004459 if (p->lex == XC_LEX_NLINE) {
Denys Vlasenko23ea0732018-12-24 15:05:49 +01004460 dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004461 RETURN_STATUS(zbc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004462 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004463 if (p->lex == BC_LEX_SCOLON) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004464 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004465 RETURN_STATUS(zbc_lex_next());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004466 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004467
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004468 if (p->lex == BC_LEX_LBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004469 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4470 do {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004471 s = zbc_lex_next();
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004472 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004473 } while (p->lex == XC_LEX_NLINE);
4474 if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004475 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004476 s = zbc_parse_auto();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004477 if (s) RETURN_STATUS(s);
4478 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004479 while (p->lex != BC_LEX_RBRACE) {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004480 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004481 s = zbc_parse_stmt();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004482 if (s) RETURN_STATUS(s);
4483 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004484 s = zbc_lex_next();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004485 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4486 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004487 }
4488
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004489 dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex);
4490 switch (p->lex) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004491 case XC_LEX_OP_MINUS:
4492 case BC_LEX_OP_INC:
4493 case BC_LEX_OP_DEC:
4494 case BC_LEX_OP_BOOL_NOT:
4495 case BC_LEX_LPAREN:
4496 case XC_LEX_NAME:
4497 case XC_LEX_NUMBER:
4498 case BC_LEX_KEY_IBASE:
4499 case BC_LEX_KEY_LAST:
4500 case BC_LEX_KEY_LENGTH:
4501 case BC_LEX_KEY_OBASE:
4502 case BC_LEX_KEY_READ:
4503 case BC_LEX_KEY_SCALE:
4504 case BC_LEX_KEY_SQRT:
4505 s = zbc_parse_expr(BC_PARSE_PRINT);
4506 break;
4507 case XC_LEX_STR:
4508 s = zbc_parse_pushSTR();
4509 bc_parse_push(XC_INST_PRINT_STR);
4510 break;
4511 case BC_LEX_KEY_BREAK:
4512 case BC_LEX_KEY_CONTINUE:
4513 s = zbc_parse_break_or_continue(p->lex);
4514 break;
4515 case BC_LEX_KEY_FOR:
4516 s = zbc_parse_for();
4517 break;
4518 case BC_LEX_KEY_HALT:
4519 bc_parse_push(BC_INST_HALT);
4520 s = zbc_lex_next();
4521 break;
4522 case BC_LEX_KEY_IF:
4523 s = zbc_parse_if();
4524 break;
4525 case BC_LEX_KEY_LIMITS:
4526 // "limits" is a compile-time command,
4527 // the output is produced at _parse time_.
4528 printf(
4529 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4530 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4531 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4532 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4533 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4534 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4535 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4536 "Number of vars = "BC_MAX_VARS_STR "\n"
4537 );
4538 s = zbc_lex_next();
4539 break;
4540 case BC_LEX_KEY_PRINT:
4541 s = zbc_parse_print();
4542 break;
4543 case BC_LEX_KEY_QUIT:
4544 // "quit" is a compile-time command. For example,
4545 // "if (0 == 1) quit" terminates when parsing the statement,
4546 // not when it is executed
4547 QUIT_OR_RETURN_TO_MAIN;
4548 case BC_LEX_KEY_RETURN:
4549 if (!p->in_funcdef)
4550 RETURN_STATUS(bc_error("'return' not in a function"));
4551 s = zbc_parse_return();
4552 break;
4553 case BC_LEX_KEY_WHILE:
4554 s = zbc_parse_while();
4555 break;
4556 default:
4557 s = bc_error_bad_token();
4558 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004559 }
4560
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004561 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004562 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004563}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004564#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004565
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004566static BC_STATUS zbc_parse_stmt_or_funcdef(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004567{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004568 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004569 BcStatus s;
4570
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004571 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004572 if (p->lex == XC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004573 s = bc_error("end of file");
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004574 else if (p->lex == BC_LEX_KEY_DEFINE) {
4575 dbg_lex("%s:%d p->lex:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004576 s = zbc_parse_funcdef();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004577 } else {
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004578 dbg_lex("%s:%d p->lex:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->lex);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004579 s = zbc_parse_stmt();
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004580 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004581
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004582 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004583 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004584}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004585#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004586
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004587// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004588static BcStatus bc_parse_expr_empty_ok(uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004589{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004590 BcParse *p = &G.prs;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004591 BcInst prev = XC_INST_PRINT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004592 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004593 unsigned nparens, nrelops;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004594 bool paren_first, paren_expr, rprn, assign, bin_last;
Gavin Howard01055ba2018-11-03 11:00:21 -06004595
Denys Vlasenko17df8822018-12-14 23:00:24 +01004596 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004597 paren_first = (p->lex == BC_LEX_LPAREN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004598 nparens = nrelops = 0;
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004599 paren_expr = rprn = assign = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004600 bin_last = true;
4601
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004602 for (;;) {
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004603 bool get_token;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004604 BcStatus s;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004605 BcLexType t = p->lex;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004606
4607 if (!lex_allowed_in_bc_expr(t))
4608 break;
Denys Vlasenkobb116032018-12-25 01:16:37 +01004609
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004610 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Denys Vlasenko73b3ebc2018-12-25 01:43:52 +01004611 get_token = false;
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004612 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004613 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004614 case BC_LEX_OP_INC:
4615 case BC_LEX_OP_DEC:
4616 dbg_lex("%s:%d LEX_OP_INC/DEC", __func__, __LINE__);
4617 s = zbc_parse_incdec(&prev, &paren_expr, &nexprs, flags);
4618 rprn = bin_last = false;
4619 //get_token = false; - already is
4620 break;
4621 case XC_LEX_OP_MINUS:
4622 dbg_lex("%s:%d LEX_OP_MINUS", __func__, __LINE__);
4623 s = zbc_parse_minus(&prev, ops_bgn, rprn, &nexprs);
4624 rprn = false;
4625 //get_token = false; - already is
4626 bin_last = (prev == XC_INST_MINUS);
4627 break;
4628 case BC_LEX_OP_ASSIGN_POWER:
4629 case BC_LEX_OP_ASSIGN_MULTIPLY:
4630 case BC_LEX_OP_ASSIGN_DIVIDE:
4631 case BC_LEX_OP_ASSIGN_MODULUS:
4632 case BC_LEX_OP_ASSIGN_PLUS:
4633 case BC_LEX_OP_ASSIGN_MINUS:
4634 case BC_LEX_OP_ASSIGN:
4635 dbg_lex("%s:%d LEX_ASSIGNxyz", __func__, __LINE__);
4636 if (prev != XC_INST_VAR && prev != XC_INST_ARRAY_ELEM
4637 && prev != XC_INST_SCALE && prev != XC_INST_IBASE
4638 && prev != XC_INST_OBASE && prev != BC_INST_LAST
4639 ) {
4640 return bc_error("bad assignment:"
4641 " left side must be variable"
4642 " or array element"
4643 ); // note: shared string
4644 }
4645 // Fallthrough.
4646 case XC_LEX_OP_POWER:
4647 case XC_LEX_OP_MULTIPLY:
4648 case XC_LEX_OP_DIVIDE:
4649 case XC_LEX_OP_MODULUS:
4650 case XC_LEX_OP_PLUS:
4651 case XC_LEX_OP_REL_EQ:
4652 case XC_LEX_OP_REL_LE:
4653 case XC_LEX_OP_REL_GE:
4654 case XC_LEX_OP_REL_NE:
4655 case XC_LEX_OP_REL_LT:
4656 case XC_LEX_OP_REL_GT:
4657 case BC_LEX_OP_BOOL_NOT:
4658 case BC_LEX_OP_BOOL_OR:
4659 case BC_LEX_OP_BOOL_AND:
4660 dbg_lex("%s:%d LEX_OP_xyz", __func__, __LINE__);
4661 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4662 || (t != BC_LEX_OP_BOOL_NOT && prev == XC_INST_BOOL_NOT)
4663 ) {
4664 return bc_error_bad_expression();
4665 }
4666 nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT);
4667 prev = BC_TOKEN_2_INST(t);
4668 bc_parse_operator(t, ops_bgn, &nexprs);
4669 s = zbc_lex_next();
4670 rprn = false;
4671 //get_token = false; - already is
4672 bin_last = (t != BC_LEX_OP_BOOL_NOT);
4673 break;
4674 case BC_LEX_LPAREN:
4675 dbg_lex("%s:%d LEX_LPAREN", __func__, __LINE__);
4676 if (BC_PARSE_LEAF(prev, rprn))
4677 return bc_error_bad_expression();
4678 bc_vec_push(&p->ops, &t);
4679 nparens++;
4680 get_token = true;
4681 paren_expr = false;
4682 rprn = bin_last = false;
4683 break;
4684 case BC_LEX_RPAREN:
4685 dbg_lex("%s:%d LEX_RPAREN", __func__, __LINE__);
4686 if (bin_last || prev == XC_INST_BOOL_NOT)
4687 return bc_error_bad_expression();
4688 if (nparens == 0) {
4689 goto exit_loop;
4690 }
4691 if (!paren_expr) {
4692 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
4693 return BC_STATUS_PARSE_EMPTY_EXP;
4694 }
4695 s = zbc_parse_rightParen(ops_bgn, &nexprs);
4696 nparens--;
4697 get_token = true;
4698 paren_expr = rprn = true;
4699 bin_last = false;
4700 break;
4701 case XC_LEX_NAME:
4702 dbg_lex("%s:%d LEX_NAME", __func__, __LINE__);
4703 if (BC_PARSE_LEAF(prev, rprn))
4704 return bc_error_bad_expression();
4705 s = zbc_parse_name(&prev, flags & ~BC_PARSE_NOCALL);
4706 paren_expr = true;
4707 rprn = bin_last = false;
4708 //get_token = false; - already is
4709 nexprs++;
4710 break;
4711 case XC_LEX_NUMBER:
4712 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4713 if (BC_PARSE_LEAF(prev, rprn))
4714 return bc_error_bad_expression();
4715 bc_parse_pushNUM();
4716 prev = XC_INST_NUM;
4717 get_token = true;
4718 paren_expr = true;
4719 rprn = bin_last = false;
4720 nexprs++;
4721 break;
4722 case BC_LEX_KEY_IBASE:
4723 case BC_LEX_KEY_LAST:
4724 case BC_LEX_KEY_OBASE:
4725 dbg_lex("%s:%d LEX_IBASE/LAST/OBASE", __func__, __LINE__);
4726 if (BC_PARSE_LEAF(prev, rprn))
4727 return bc_error_bad_expression();
4728 prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE);
4729 bc_parse_push((char) prev);
4730 get_token = true;
4731 paren_expr = true;
4732 rprn = bin_last = false;
4733 nexprs++;
4734 break;
4735 case BC_LEX_KEY_LENGTH:
4736 case BC_LEX_KEY_SQRT:
4737 dbg_lex("%s:%d LEX_LEN/SQRT", __func__, __LINE__);
4738 if (BC_PARSE_LEAF(prev, rprn))
4739 return bc_error_bad_expression();
4740 s = zbc_parse_builtin(t, flags, &prev);
4741 get_token = true;
4742 paren_expr = true;
4743 rprn = bin_last = false;
4744 nexprs++;
4745 break;
4746 case BC_LEX_KEY_READ:
4747 dbg_lex("%s:%d LEX_READ", __func__, __LINE__);
4748 if (BC_PARSE_LEAF(prev, rprn))
4749 return bc_error_bad_expression();
4750 s = zbc_parse_read();
4751 prev = XC_INST_READ;
4752 get_token = true;
4753 paren_expr = true;
4754 rprn = bin_last = false;
4755 nexprs++;
4756 break;
4757 case BC_LEX_KEY_SCALE:
4758 dbg_lex("%s:%d LEX_SCALE", __func__, __LINE__);
4759 if (BC_PARSE_LEAF(prev, rprn))
4760 return bc_error_bad_expression();
4761 s = zbc_parse_scale(&prev, flags);
4762 prev = XC_INST_SCALE;
4763 //get_token = false; - already is
4764 paren_expr = true;
4765 rprn = bin_last = false;
4766 nexprs++;
4767 break;
4768 default:
4769 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004770 }
4771
Denys Vlasenkobb116032018-12-25 01:16:37 +01004772 if (s || G_interrupt) // error, or ^C: stop parsing
4773 return BC_STATUS_FAILURE;
4774 if (get_token) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004775 s = zbc_lex_next();
Denys Vlasenkobb116032018-12-25 01:16:37 +01004776 if (s) return s;
4777 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004778 }
Denys Vlasenkobb116032018-12-25 01:16:37 +01004779 exit_loop:
Gavin Howard01055ba2018-11-03 11:00:21 -06004780
4781 while (p->ops.len > ops_bgn) {
Denys Vlasenkod0238d82018-12-25 01:21:16 +01004782 BcLexType top = BC_PARSE_TOP_OP(p);
Denys Vlasenkoa5bf53e2018-12-24 17:06:37 +01004783 assign = (top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN);
Gavin Howard01055ba2018-11-03 11:00:21 -06004784
4785 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004786 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004787
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004788 bc_parse_push(BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004789
Denys Vlasenko69560f42018-12-24 14:14:23 +01004790 nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004791 bc_vec_pop(&p->ops);
4792 }
4793
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01004794 if (prev == XC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004795 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004796
Gavin Howard01055ba2018-11-03 11:00:21 -06004797 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004798 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004799 s = zbc_POSIX_does_not_allow("comparison operators outside if or loops");
4800 if (s) return s;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004801 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004802 BcStatus s;
Denys Vlasenko79587cb2018-12-24 18:11:41 +01004803 s = zbc_POSIX_requires("exactly one comparison operator per condition");
4804 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004805 }
4806
4807 if (flags & BC_PARSE_PRINT) {
Denys Vlasenkobb116032018-12-25 01:16:37 +01004808 if (paren_first || !assign)
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004809 bc_parse_push(XC_INST_PRINT);
4810 bc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004811 }
4812
Denys Vlasenko17df8822018-12-14 23:00:24 +01004813 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkobb116032018-12-25 01:16:37 +01004814 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004815}
4816
Gavin Howard01055ba2018-11-03 11:00:21 -06004817#endif // ENABLE_BC
4818
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004819#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004820
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004821static BC_STATUS zdc_parse_register(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004822{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004823 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004824 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004825
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004826 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004827 if (s) RETURN_STATUS(s);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004828 if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004829
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004830 bc_parse_pushName(p->lex_strnumbuf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004831
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004832 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004833}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004834#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004835
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004836static void dc_parse_string(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004837{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004838 BcParse *p = &G.prs;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004839 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004840 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004841
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004842 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004843
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004844 str = xstrdup(p->lex_strnumbuf.v);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004845 bc_parse_push(XC_INST_STR);
4846 bc_parse_pushIndex(len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004847 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004848
4849 // Explanation needed here
4850 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004851 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004852
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004853 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004854}
4855
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004856static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004857{
4858 BcStatus s;
4859
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004860 bc_parse_push(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06004861 if (name) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004862 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004863 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004864 }
4865
4866 if (store) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004867 bc_parse_push(DC_INST_SWAP);
4868 bc_parse_push(XC_INST_ASSIGN);
4869 bc_parse_push(XC_INST_POP);
Gavin Howard01055ba2018-11-03 11:00:21 -06004870 }
4871
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004872 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004873}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004874#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004875
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004876static BC_STATUS zdc_parse_cond(uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004877{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004878 BcParse *p = &G.prs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004879 BcStatus s;
4880
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004881 bc_parse_push(inst);
4882 bc_parse_push(DC_INST_EXEC_COND);
Gavin Howard01055ba2018-11-03 11:00:21 -06004883
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004884 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004885 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004886
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004887 s = zbc_lex_next();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004888 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004889
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004890 // Note that 'else' part can not be on the next line:
4891 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4892 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004893 if (p->lex == DC_LEX_ELSE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004894 s = zdc_parse_register();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004895 if (s) RETURN_STATUS(s);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004896 s = zbc_lex_next();
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004897 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004898 bc_parse_push(BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004899 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004900
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004901 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004902}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004903#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004904
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004905static BC_STATUS zdc_parse_token(BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004906{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004907 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004908 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004909 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004910
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004911 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004912 s = BC_STATUS_SUCCESS;
4913 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004914 switch (t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004915 case XC_LEX_OP_REL_EQ:
4916 case XC_LEX_OP_REL_LE:
4917 case XC_LEX_OP_REL_GE:
4918 case XC_LEX_OP_REL_NE:
4919 case XC_LEX_OP_REL_LT:
4920 case XC_LEX_OP_REL_GT:
4921 dbg_lex("%s:%d LEX_OP_REL_xyz", __func__, __LINE__);
4922 s = zdc_parse_cond(t - XC_LEX_OP_REL_EQ + XC_INST_REL_EQ);
4923 get_token = false;
4924 break;
4925 case DC_LEX_SCOLON:
4926 case DC_LEX_COLON:
4927 dbg_lex("%s:%d LEX_[S]COLON", __func__, __LINE__);
4928 s = zdc_parse_mem(XC_INST_ARRAY_ELEM, true, t == DC_LEX_COLON);
4929 break;
4930 case XC_LEX_STR:
4931 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
4932 dc_parse_string();
4933 break;
4934 case XC_LEX_NEG:
4935 dbg_lex("%s:%d LEX_NEG", __func__, __LINE__);
4936 s = zbc_lex_next();
4937 if (s) RETURN_STATUS(s);
4938 if (G.prs.lex != XC_LEX_NUMBER)
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004939 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01004940 bc_parse_pushNUM();
4941 bc_parse_push(XC_INST_NEG);
4942 break;
4943 case XC_LEX_NUMBER:
4944 dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__);
4945 bc_parse_pushNUM();
4946 break;
4947 case DC_LEX_READ:
4948 dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__);
4949 bc_parse_push(XC_INST_READ);
4950 break;
4951 case DC_LEX_OP_ASSIGN:
4952 case DC_LEX_STORE_PUSH:
4953 dbg_lex("%s:%d LEX_OP_ASSIGN/STORE_PUSH", __func__, __LINE__);
4954 assign = (t == DC_LEX_OP_ASSIGN);
4955 inst = assign ? XC_INST_VAR : DC_INST_PUSH_TO_VAR;
4956 s = zdc_parse_mem(inst, true, assign);
4957 break;
4958 case DC_LEX_LOAD:
4959 case DC_LEX_LOAD_POP:
4960 dbg_lex("%s:%d LEX_OP_LOAD[_POP]", __func__, __LINE__);
4961 inst = t == DC_LEX_LOAD_POP ? DC_INST_PUSH_VAR : DC_INST_LOAD;
4962 s = zdc_parse_mem(inst, true, false);
4963 break;
4964 case DC_LEX_STORE_IBASE:
4965 case DC_LEX_STORE_SCALE:
4966 case DC_LEX_STORE_OBASE:
4967 dbg_lex("%s:%d LEX_OP_STORE_I/OBASE/SCALE", __func__, __LINE__);
4968 inst = t - DC_LEX_STORE_IBASE + XC_INST_IBASE;
4969 s = zdc_parse_mem(inst, false, true);
4970 break;
4971 default:
4972 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4973 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004974 }
4975
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004976 if (!s && get_token) s = zbc_lex_next();
Gavin Howard01055ba2018-11-03 11:00:21 -06004977
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004978 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004979 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004980}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004981#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004982
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004983static BC_STATUS zdc_parse_expr(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06004984{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004985 BcParse *p = &G.prs;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004986 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004987
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004988 i = (int)p->lex - (int)XC_LEX_OP_POWER;
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004989 if (i >= 0) {
4990 BcInst inst = dc_LEX_to_INST[i];
4991 if (inst != DC_INST_INVALID) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01004992 bc_parse_push(inst);
4993 RETURN_STATUS(zbc_lex_next());
Denys Vlasenko4accb6b2018-12-24 15:29:08 +01004994 }
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004995 }
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01004996 RETURN_STATUS(zdc_parse_token(p->lex));
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004997}
4998#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
4999
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005000static BC_STATUS zdc_parse_exprs_until_eof(void)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005001{
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005002 BcParse *p = &G.prs;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005003 dbg_lex_enter("%s:%d entered, p->lex:%d", __func__, __LINE__, p->lex);
5004 while (p->lex != XC_LEX_EOF) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005005 BcStatus s = zdc_parse_expr();
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005006 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005007 }
5008
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005009 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005010 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005011}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005012#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005013
Gavin Howard01055ba2018-11-03 11:00:21 -06005014#endif // ENABLE_DC
5015
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005016//
5017// Execution engine
5018//
5019
5020#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
5021#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
5022
Denys Vlasenkodf515392018-12-02 19:27:48 +01005023static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005024{
Gavin Howard01055ba2018-11-03 11:00:21 -06005025 BcId e, *ptr;
5026 BcVec *v, *map;
5027 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005028 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005029
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005030 v = var ? &G.prog.vars : &G.prog.arrs;
5031 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005032
5033 e.name = id;
5034 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005035 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005036
5037 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005038 BcVec v2;
5039 bc_array_init(&v2, var);
5040 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005041 }
5042
5043 ptr = bc_vec_item(map, i);
5044 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005045 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005046}
5047
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005048// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005049static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005050{
Gavin Howard01055ba2018-11-03 11:00:21 -06005051 switch (r->t) {
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005052 case XC_RESULT_STR:
5053 case XC_RESULT_TEMP:
5054 case XC_RESULT_IBASE:
5055 case XC_RESULT_SCALE:
5056 case XC_RESULT_OBASE:
5057 *num = &r->d.n;
5058 break;
5059 case XC_RESULT_CONSTANT: {
5060 BcStatus s;
5061 char *str;
5062 unsigned base_t;
5063 size_t len;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005064
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005065 str = *bc_program_const(r->d.id.idx);
5066 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005067
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005068 bc_num_init(&r->d.n, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06005069
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005070 hex = hex && len == 1;
5071 base_t = hex ? 16 : G.prog.ib_t;
5072 s = zbc_num_parse(&r->d.n, str, base_t);
5073 if (s) {
5074 bc_num_free(&r->d.n);
5075 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005076 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005077 *num = &r->d.n;
5078 r->t = XC_RESULT_TEMP;
5079 break;
5080 }
5081 case XC_RESULT_VAR:
5082 case XC_RESULT_ARRAY:
5083 case XC_RESULT_ARRAY_ELEM: {
5084 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005085
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005086 v = bc_program_search(r->d.id.name, r->t == XC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005087
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005088 if (r->t == XC_RESULT_ARRAY_ELEM) {
5089 v = bc_vec_top(v);
5090 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5091 *num = bc_vec_item(v, r->d.id.idx);
5092 } else
5093 *num = bc_vec_top(v);
5094 break;
5095 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005096#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005097 case BC_RESULT_LAST:
5098 *num = &G.prog.last;
5099 break;
5100 case BC_RESULT_ONE:
5101 *num = &G.prog.one;
5102 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005103#endif
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005104#if SANITY_CHECKS
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01005105 default:
5106 // Testing the theory that dc does not reach LAST/ONE
5107 bb_error_msg_and_die("BUG:%d", r->t);
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01005108#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005109 }
5110
Denys Vlasenko29301232018-12-11 15:29:32 +01005111 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005112}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005113#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005114
Denys Vlasenko29301232018-12-11 15:29:32 +01005115static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005116 BcResult **r, BcNum **rn, bool assign)
5117{
5118 BcStatus s;
5119 bool hex;
5120 BcResultType lt, rt;
5121
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005122 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005123 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005124
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005125 *r = bc_vec_item_rev(&G.prog.results, 0);
5126 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005127
5128 lt = (*l)->t;
5129 rt = (*r)->t;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005130 hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE);
Gavin Howard01055ba2018-11-03 11:00:21 -06005131
Denys Vlasenko29301232018-12-11 15:29:32 +01005132 s = zbc_program_num(*l, ln, false);
5133 if (s) RETURN_STATUS(s);
5134 s = zbc_program_num(*r, rn, hex);
5135 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005136
5137 // We run this again under these conditions in case any vector has been
5138 // reallocated out from under the BcNums or arrays we had.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005139 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005140 s = zbc_program_num(*l, ln, false);
5141 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005142 }
5143
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005144 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != XC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005145 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005146 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005147 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005148
Denys Vlasenko29301232018-12-11 15:29:32 +01005149 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005150}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005151#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005152
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005153static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005154{
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005155 r->t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005156 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005157 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005158}
5159
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005160// Note: *r and *n need not be initialized by caller
Denys Vlasenko29301232018-12-11 15:29:32 +01005161static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005162{
5163 BcStatus s;
5164
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005165 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005166 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005167 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005168
Denys Vlasenko29301232018-12-11 15:29:32 +01005169 s = zbc_program_num(*r, n, false);
5170 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005171
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005172 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005173 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005174
Denys Vlasenko29301232018-12-11 15:29:32 +01005175 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005176}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005177#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005178
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005179static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005180{
5181 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005182 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005183}
5184
Denys Vlasenko259137d2018-12-11 19:42:05 +01005185static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005186{
5187 BcStatus s;
5188 BcResult *opd1, *opd2, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005189 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06005190
Denys Vlasenko29301232018-12-11 15:29:32 +01005191 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005192 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005193 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005194
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005195 s = BC_STATUS_SUCCESS;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005196 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 -06005197 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005198 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005199
Denys Vlasenko259137d2018-12-11 19:42:05 +01005200 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005201 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005202 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005203 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005204}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005205#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005206
Denys Vlasenko26819db2018-12-12 16:08:46 +01005207static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005208{
5209 BcStatus s;
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005210 BcParse sv_parse;
Gavin Howard01055ba2018-11-03 11:00:21 -06005211 BcVec buf;
5212 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005213 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005214
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005215 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005216 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005217
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005218 f = bc_program_func(BC_PROG_READ);
5219 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005220
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005221 sv_parse = G.prs; // struct copy
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005222 bc_parse_create(BC_PROG_READ);
Denys Vlasenko7d32e252018-12-26 18:32:43 +01005223 //G.err_line = G.prs.lex_line = 1; - not needed, error line info is not printed for read()
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005224
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005225 s = zbc_parse_text_init(buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005226 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005227 if (IS_BC) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005228 IF_BC(s = zbc_parse_expr(0));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005229 } else {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005230 IF_DC(s = zdc_parse_exprs_until_eof());
Denys Vlasenko514967d2018-12-22 03:38:52 +01005231 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005232 if (s) goto exec_err;
5233
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01005234 if (G.prs.lex != XC_LEX_NLINE && G.prs.lex != XC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005235 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005236 goto exec_err;
5237 }
5238
5239 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005240 ip.inst_idx = 0;
5241 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005242
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005243 bc_parse_push(XC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005244 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005245
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005246 exec_err:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01005247 bc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01005248 G.prs = sv_parse; // struct copy
Gavin Howard01055ba2018-11-03 11:00:21 -06005249 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005250 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005251}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005252#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005253
5254static size_t bc_program_index(char *code, size_t *bgn)
5255{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005256 unsigned char *bytes = (void*)(code + *bgn);
5257 unsigned amt;
5258 unsigned i;
5259 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005260
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005261 amt = *bytes++;
5262 *bgn += amt + 1;
5263
5264 amt *= 8;
5265 res = 0;
5266 for (i = 0; i < amt; i += 8)
5267 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005268
5269 return res;
5270}
5271
5272static char *bc_program_name(char *code, size_t *bgn)
5273{
5274 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005275 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005276
Denys Vlasenko694d2982018-12-18 19:17:11 +01005277 code += *bgn;
5278 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005279 i = 0;
5280 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005281 char c = *code++;
5282 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005283 break;
5284 s[i++] = c;
5285 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005286 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005287 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005288
5289 return s;
5290}
5291
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005292static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005293{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005294#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005295 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005296 // Example: echo '[]ap' | dc
5297 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005298 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005299 return;
5300 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005301#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005302 while (*str) {
5303 int c = *str++;
5304 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005305 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005306 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005307 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005308 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005309 case 'a':
5310 bb_putchar('\a');
5311 break;
5312 case 'b':
5313 bb_putchar('\b');
5314 break;
5315 case '\\':
5316 case 'e':
5317 bb_putchar('\\');
5318 break;
5319 case 'f':
5320 bb_putchar('\f');
5321 break;
5322 case 'n':
5323 bb_putchar('\n');
5324 G.prog.nchars = SIZE_MAX;
5325 break;
5326 case 'r':
5327 bb_putchar('\r');
5328 break;
5329 case 'q':
5330 bb_putchar('"');
5331 break;
5332 case 't':
5333 bb_putchar('\t');
5334 break;
5335 default:
5336 // Just print the backslash and following character.
5337 bb_putchar('\\');
5338 ++G.prog.nchars;
5339 bb_putchar(c);
5340 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005341 }
5342 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005343 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005344 }
5345}
5346
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005347static void bc_num_printNewline(void)
5348{
5349 if (G.prog.nchars == G.prog.len - 1) {
5350 bb_putchar('\\');
5351 bb_putchar('\n');
5352 G.prog.nchars = 0;
5353 }
5354}
5355
5356#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005357static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005358{
5359 (void) radix;
5360 bb_putchar((char) num);
5361 G.prog.nchars += width;
5362}
5363#endif
5364
5365static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5366{
5367 size_t exp, pow;
5368
5369 bc_num_printNewline();
5370 bb_putchar(radix ? '.' : ' ');
5371 ++G.prog.nchars;
5372
5373 bc_num_printNewline();
5374 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5375 continue;
5376
5377 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5378 size_t dig;
5379 bc_num_printNewline();
5380 dig = num / pow;
5381 num -= dig * pow;
5382 bb_putchar(((char) dig) + '0');
5383 }
5384}
5385
5386static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5387{
5388 if (radix) {
5389 bc_num_printNewline();
5390 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005391 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005392 }
5393
5394 bc_num_printNewline();
5395 bb_putchar(bb_hexdigits_upcase[num]);
5396 G.prog.nchars += width;
5397}
5398
5399static void bc_num_printDecimal(BcNum *n)
5400{
5401 size_t i, rdx = n->rdx - 1;
5402
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005403 if (n->neg) {
5404 bb_putchar('-');
5405 G.prog.nchars++;
5406 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005407
5408 for (i = n->len - 1; i < n->len; --i)
5409 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5410}
5411
Denys Vlasenko2097ac82018-12-24 05:00:36 +01005412typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
5413
Denys Vlasenkod3401432018-12-18 17:00:35 +01005414static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005415{
5416 BcStatus s;
5417 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005418 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005419 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005420 BcNum intp, fracp, digit, frac_len;
5421 unsigned long dig, *ptr;
5422 size_t i;
5423 bool radix;
5424
5425 if (n->len == 0) {
5426 print(0, width, false);
5427 RETURN_STATUS(BC_STATUS_SUCCESS);
5428 }
5429
5430 bc_vec_init(&stack, sizeof(long), NULL);
5431 bc_num_init(&intp, n->len);
5432 bc_num_init(&fracp, n->rdx);
5433 bc_num_init(&digit, width);
5434 bc_num_init(&frac_len, BC_NUM_INT(n));
5435 bc_num_copy(&intp, n);
5436 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005437 base.cap = ARRAY_SIZE(base_digs);
5438 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005439 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005440
5441 bc_num_truncate(&intp, intp.rdx);
5442 s = zbc_num_sub(n, &intp, &fracp, 0);
5443 if (s) goto err;
5444
5445 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005446 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005447 if (s) goto err;
5448 s = zbc_num_ulong(&digit, &dig);
5449 if (s) goto err;
5450 bc_vec_push(&stack, &dig);
5451 }
5452
5453 for (i = 0; i < stack.len; ++i) {
5454 ptr = bc_vec_item_rev(&stack, i);
5455 print(*ptr, width, false);
5456 }
5457
5458 if (!n->rdx) goto err;
5459
5460 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005461 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005462 if (s) goto err;
5463 s = zbc_num_ulong(&fracp, &dig);
5464 if (s) goto err;
5465 bc_num_ulong2num(&intp, dig);
5466 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5467 if (s) goto err;
5468 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005469 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005470 if (s) goto err;
5471 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005472 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005473 bc_num_free(&frac_len);
5474 bc_num_free(&digit);
5475 bc_num_free(&fracp);
5476 bc_num_free(&intp);
5477 bc_vec_free(&stack);
5478 RETURN_STATUS(s);
5479}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005480#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005481
5482static BC_STATUS zbc_num_printBase(BcNum *n)
5483{
5484 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005485 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005486 BcNumDigitOp print;
5487 bool neg = n->neg;
5488
5489 if (neg) {
5490 bb_putchar('-');
5491 G.prog.nchars++;
5492 }
5493
5494 n->neg = false;
5495
5496 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5497 width = 1;
5498 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005499 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005500 unsigned i = G.prog.ob_t - 1;
5501 width = 0;
5502 for (;;) {
5503 width++;
5504 i /= 10;
5505 if (i == 0)
5506 break;
5507 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005508 print = bc_num_printDigits;
5509 }
5510
Denys Vlasenkod3401432018-12-18 17:00:35 +01005511 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005512 n->neg = neg;
5513
5514 RETURN_STATUS(s);
5515}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005516#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005517
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005518static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5519{
5520 BcStatus s = BC_STATUS_SUCCESS;
5521
5522 bc_num_printNewline();
5523
5524 if (n->len == 0) {
5525 bb_putchar('0');
5526 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005527 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005528 bc_num_printDecimal(n);
5529 else
5530 s = zbc_num_printBase(n);
5531
5532 if (newline) {
5533 bb_putchar('\n');
5534 G.prog.nchars = 0;
5535 }
5536
5537 RETURN_STATUS(s);
5538}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005539#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005540
Denys Vlasenko29301232018-12-11 15:29:32 +01005541static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005542{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005543 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005544 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005545 BcNum *num;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005546 bool pop = (inst != XC_INST_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06005547
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005548 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005549 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005550
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005551 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005552 s = zbc_program_num(r, &num, false);
5553 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005554
5555 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005556 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005557#if ENABLE_BC
5558 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5559#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005560 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005561 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005562
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005563 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005564 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005565
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005566 if (inst == XC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005567 for (;;) {
5568 char c = *str++;
5569 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005570 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005571 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005572 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005573 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005574 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005575 bc_program_printString(str);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005576 if (inst == XC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005577 }
5578 }
5579
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005580 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005581
Denys Vlasenko29301232018-12-11 15:29:32 +01005582 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005583}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005584#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005585
Denys Vlasenko29301232018-12-11 15:29:32 +01005586static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005587{
5588 BcStatus s;
5589 BcResult res, *ptr;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005590 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005591
Denys Vlasenko29301232018-12-11 15:29:32 +01005592 s = zbc_program_prep(&ptr, &num);
5593 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005594
5595 bc_num_init(&res.d.n, num->len);
5596 bc_num_copy(&res.d.n, num);
5597 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5598
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005599 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005600
Denys Vlasenko29301232018-12-11 15:29:32 +01005601 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005602}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005603#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005604
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005605static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005606{
5607 BcStatus s;
5608 BcResult *opd1, *opd2, res;
5609 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005610 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005611
Denys Vlasenko29301232018-12-11 15:29:32 +01005612 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005613 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005614
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005615 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005616
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005617 if (inst == XC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005618 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005619 else if (inst == XC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005620 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005621 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005622 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005623 switch (inst) {
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005624 case XC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005625 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005626 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005627 case XC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005628 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005629 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005630 case XC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005631 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005632 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005633 case XC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005634 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005635 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005636 case XC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005637 cond = (cond > 0);
5638 break;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005639 default: // = case XC_INST_REL_NE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005640 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005641 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005642 }
5643 }
5644
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005645 if (cond) bc_num_one(&res.d.n);
5646 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005647
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005648 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005649
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005650 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005651}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005652#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005653
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005654#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005655static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005656{
5657 BcNum n2;
5658 BcResult res;
5659
5660 memset(&n2, 0, sizeof(BcNum));
5661 n2.rdx = res.d.id.idx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005662 res.t = XC_RESULT_STR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005663
5664 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005665 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005666 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005667 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005668 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005669 }
5670
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005671 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005672 bc_vec_push(v, &n2);
5673
Denys Vlasenko29301232018-12-11 15:29:32 +01005674 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005675}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005676#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005677#endif // ENABLE_DC
5678
Denys Vlasenko29301232018-12-11 15:29:32 +01005679static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005680{
5681 BcStatus s;
5682 BcResult *ptr, r;
5683 BcVec *v;
5684 BcNum *n;
5685
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005686 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005687 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005688
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005689 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005690 if ((ptr->t == XC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005691 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005692 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005693
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005694#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005695 if (ptr->t == XC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005696 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005697 if (ptr->t == XC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005698 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005699#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005700
Denys Vlasenko29301232018-12-11 15:29:32 +01005701 s = zbc_program_num(ptr, &n, false);
5702 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005703
5704 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005705 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005706
5707 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005708 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005709 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005710 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005711 bc_array_init(&r.d.v, true);
5712 bc_array_copy(&r.d.v, (BcVec *) n);
5713 }
5714
5715 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005716 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005717
Denys Vlasenko29301232018-12-11 15:29:32 +01005718 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005719}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005720#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005721
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005722static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005723{
5724 BcStatus s;
5725 BcResult *left, *right, res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005726 BcNum *l, *r;
5727 bool assign = (inst == XC_INST_ASSIGN);
5728 bool ib, sc;
Gavin Howard01055ba2018-11-03 11:00:21 -06005729
Denys Vlasenko29301232018-12-11 15:29:32 +01005730 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005731 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005732
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005733 ib = left->t == XC_RESULT_IBASE;
5734 sc = left->t == XC_RESULT_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005735
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005736#if ENABLE_DC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005737 if (right->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005738 BcVec *v;
5739
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005740 if (left->t != XC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005741 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005742 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005743
Denys Vlasenkofa210792018-12-19 19:35:40 +01005744 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005745 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005746#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005747
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005748 if (left->t == XC_RESULT_CONSTANT || left->t == XC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005749 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005750 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005751 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005752 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005753
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005754#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005755 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005756 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005757
5758 if (assign)
5759 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005760 else {
5761 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005762 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 +01005763 }
5764 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005765#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005766 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005767#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005768
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005769 if (ib || sc || left->t == XC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005770 static const char *const msg[] = {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005771 "bad ibase; must be [2,16]", //XC_RESULT_IBASE
5772 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //XC_RESULT_OBASE
5773 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //XC_RESULT_SCALE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005774 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005775 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005776 size_t max;
5777 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005778
Denys Vlasenko29301232018-12-11 15:29:32 +01005779 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005780 if (s) RETURN_STATUS(s);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005781 s = left->t - XC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005782 if (sc) {
5783 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005784 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005785 } else {
5786 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005787 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005788 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005789 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005790 }
5791
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005792 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005793 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005794
5795 *ptr = (size_t) val;
5796 s = BC_STATUS_SUCCESS;
5797 }
5798
5799 bc_num_init(&res.d.n, l->len);
5800 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005801 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005802
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005803 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005804}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005805#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005806
Denys Vlasenko416ce762018-12-02 20:57:17 +01005807#if !ENABLE_DC
5808#define bc_program_pushVar(code, bgn, pop, copy) \
5809 bc_program_pushVar(code, bgn)
5810// for bc, 'pop' and 'copy' are always false
5811#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005812static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005813 bool pop, bool copy)
5814{
Gavin Howard01055ba2018-11-03 11:00:21 -06005815 BcResult r;
5816 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005817
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005818 r.t = XC_RESULT_VAR;
Gavin Howard01055ba2018-11-03 11:00:21 -06005819 r.d.id.name = name;
5820
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005821#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005822 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005823 BcVec *v = bc_program_search(name, true);
5824 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005825
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005826 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005827 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005828 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005829 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005830
5831 if (!BC_PROG_STR(num)) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005832 r.t = XC_RESULT_TEMP;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005833 bc_num_init_DEF_SIZE(&r.d.n);
5834 bc_num_copy(&r.d.n, num);
5835 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005836 r.t = XC_RESULT_STR;
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005837 r.d.id.idx = num->rdx;
5838 }
5839
5840 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005841 }
5842#endif // ENABLE_DC
5843
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005844 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005845
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005846 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005847}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005848#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005849
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005850static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005851{
5852 BcStatus s = BC_STATUS_SUCCESS;
5853 BcResult r;
5854 BcNum *num;
5855
5856 r.d.id.name = bc_program_name(code, bgn);
5857
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005858 if (inst == XC_INST_ARRAY) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005859 r.t = XC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005860 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005861 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005862 BcResult *operand;
5863 unsigned long temp;
5864
Denys Vlasenko29301232018-12-11 15:29:32 +01005865 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005866 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005867 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005868 if (s) goto err;
5869
5870 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005871 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005872 goto err;
5873 }
5874
5875 r.d.id.idx = (size_t) temp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005876 bc_program_retire(&r, XC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005877 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005878 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005879 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005880 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005881}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005882#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005883
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005884#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005885static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005886{
5887 BcStatus s;
5888 BcResult *ptr, res, copy;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005889 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005890 char inst2 = inst;
5891
Denys Vlasenko29301232018-12-11 15:29:32 +01005892 s = zbc_program_prep(&ptr, &num);
5893 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005894
5895 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005896 copy.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005897 bc_num_init(&copy.d.n, num->len);
5898 bc_num_copy(&copy.d.n, num);
5899 }
5900
5901 res.t = BC_RESULT_ONE;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01005902 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5903 ? BC_INST_ASSIGN_PLUS
5904 : BC_INST_ASSIGN_MINUS;
Gavin Howard01055ba2018-11-03 11:00:21 -06005905
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005906 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005907 s = zbc_program_assign(inst);
5908 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005909
5910 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005911 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005912 }
5913
Denys Vlasenko29301232018-12-11 15:29:32 +01005914 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005915}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005916#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005917
Denys Vlasenko29301232018-12-11 15:29:32 +01005918static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005919{
Gavin Howard01055ba2018-11-03 11:00:21 -06005920 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005921 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005922 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005923 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005924 BcResult *arg;
5925
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005926 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005927 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005928 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005929 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005930
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005931 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005932 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005933 }
5934 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005935 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005936 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005937 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005938
5939 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005940 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005941
5942 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005943 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005944
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005945 if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005946 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005947
Denys Vlasenko29301232018-12-11 15:29:32 +01005948 s = zbc_program_copyToVar(a->name, a->idx);
5949 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005950 }
5951
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005952 a = bc_vec_item(&func->autos, i);
5953 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005954 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005955
Denys Vlasenkodf515392018-12-02 19:27:48 +01005956 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005957 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005958 BcNum n2;
5959 bc_num_init_DEF_SIZE(&n2);
5960 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005961 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005962 BcVec v2;
5963 bc_array_init(&v2, true);
5964 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005965 }
5966 }
5967
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005968 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005969
Denys Vlasenko29301232018-12-11 15:29:32 +01005970 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005971}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005972#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005973
Denys Vlasenko29301232018-12-11 15:29:32 +01005974static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005975{
Gavin Howard01055ba2018-11-03 11:00:21 -06005976 BcResult res;
5977 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005978 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005979 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005980 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06005981
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005982 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 +01005983 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005984
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005985 f = bc_program_func(ip->func);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01005986 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005987
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01005988 if (inst == XC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005989 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005990 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005991 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005992
Denys Vlasenko29301232018-12-11 15:29:32 +01005993 s = zbc_program_num(operand, &num, false);
5994 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005995 bc_num_init(&res.d.n, num->len);
5996 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005997 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005998 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01005999 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006000 }
6001
6002 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006003 a = (void*)f->autos.v;
6004 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006005 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006006 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006007 bc_vec_pop(v);
6008 }
6009
Denys Vlasenko24e41942018-12-21 23:01:26 +01006010 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006011 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006012 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006013
Denys Vlasenko29301232018-12-11 15:29:32 +01006014 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006015}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006016#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006017#endif // ENABLE_BC
6018
6019static unsigned long bc_program_scale(BcNum *n)
6020{
6021 return (unsigned long) n->rdx;
6022}
6023
6024static unsigned long bc_program_len(BcNum *n)
6025{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006026 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006027
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006028 if (n->rdx != len) return len;
6029 for (;;) {
6030 if (len == 0) break;
6031 len--;
6032 if (n->num[len] != 0) break;
6033 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006034 return len;
6035}
6036
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006037static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006038{
6039 BcStatus s;
6040 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006041 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006042 BcResult res;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006043 bool len = (inst == XC_INST_LENGTH);
Gavin Howard01055ba2018-11-03 11:00:21 -06006044
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006045 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006046 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006047 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006048
Denys Vlasenko29301232018-12-11 15:29:32 +01006049 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006050 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006051
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006052#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006053 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006054 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006055#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006056
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006057 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006058
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006059 if (inst == XC_INST_SQRT)
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006060 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006061#if ENABLE_BC
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006062 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006063 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006064 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006065#endif
6066#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006067 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006068 char **str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006069 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006070
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006071 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006072 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006073 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006074#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006075 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006076 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006077 }
6078
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006079 bc_program_retire(&res, XC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006080
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006081 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006082}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006083#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006084
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006085#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006086static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006087{
6088 BcStatus s;
6089 BcResult *opd1, *opd2, res, res2;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006090 BcNum *n1, *n2;
Gavin Howard01055ba2018-11-03 11:00:21 -06006091
Denys Vlasenko29301232018-12-11 15:29:32 +01006092 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006093 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006094
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006095 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 bc_num_init(&res2.d.n, n2->len);
6097
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006098 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006099 if (s) goto err;
6100
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006101 bc_program_binOpRetire(&res2);
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006102 res.t = XC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006103 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006104
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006105 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006106 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006107 bc_num_free(&res2.d.n);
6108 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006109 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006110}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006111#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006112
Denys Vlasenkofa210792018-12-19 19:35:40 +01006113static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006114{
6115 BcStatus s;
6116 BcResult *r1, *r2, *r3, res;
6117 BcNum *n1, *n2, *n3;
6118
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006119 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006120 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006121 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006122 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006123
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006124 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006125 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006126 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006127 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006128 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006129
6130 // Make sure that the values have their pointers updated, if necessary.
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006131 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006132 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006133 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006134 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006135 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006136 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006137 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006138 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006139 }
6140 }
6141
6142 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006143 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006144 if (s) goto err;
6145
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006146 bc_vec_pop(&G.prog.results);
6147 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006148
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006149 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006150 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006151 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006152 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006153}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006154#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006155
Denys Vlasenkofa210792018-12-19 19:35:40 +01006156static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006157{
Gavin Howard01055ba2018-11-03 11:00:21 -06006158 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006159 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006160
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006161 res.t = XC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06006162
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006163 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006164 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006165 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006166}
6167
Denys Vlasenkofa210792018-12-19 19:35:40 +01006168static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006169{
6170 BcStatus s;
6171 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006172 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006173 char **strs;
6174 char *str;
6175 char c;
6176 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006177
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006178 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006179 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006180 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006181
Denys Vlasenko29301232018-12-11 15:29:32 +01006182 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006183 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006184
6185 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006186 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006187 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006188 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006189
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006190 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006191 bc_num_copy(&n, num);
6192 bc_num_truncate(&n, n.rdx);
6193
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006194 strmb.cap = ARRAY_SIZE(strmb_digs);
6195 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006196 bc_num_ulong2num(&strmb, 0x100);
6197 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006198
Gavin Howard01055ba2018-11-03 11:00:21 -06006199 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006200 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006201 if (s) goto num_err;
6202
6203 c = (char) val;
6204
6205 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006206 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006207 char *sp;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006208 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006209 sp = *bc_program_str(idx);
6210 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006211 }
6212
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006213 strs = (void*)G.prog.strs.v;
6214 for (idx = 0; idx < G.prog.strs.len; idx++) {
6215 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6216 goto dup;
6217 }
6218 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006219 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006220 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006221 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006222 bc_vec_push(&G.prog.strs, &str);
6223 dup:
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006224 res.t = XC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006225 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006226 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006227
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006228 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006229 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006230 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006231 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006232}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006233#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006234
Denys Vlasenkofa210792018-12-19 19:35:40 +01006235static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006236{
6237 BcStatus s;
6238 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006239 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006240 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006241
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006242 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006243 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006244 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006245
Denys Vlasenko29301232018-12-11 15:29:32 +01006246 s = zbc_program_num(r, &n, false);
6247 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006248
Denys Vlasenko57734c92018-12-17 21:14:05 +01006249 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006250 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006251 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006252 char *str;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006253 idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006254 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006255 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006256 }
6257
Denys Vlasenko29301232018-12-11 15:29:32 +01006258 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006259}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006260#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006261
Denys Vlasenkofa210792018-12-19 19:35:40 +01006262static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006263{
6264 BcStatus s;
6265 BcResult *opnd;
Denys Vlasenko65b6fe02018-12-24 17:15:34 +01006266 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06006267 unsigned long val;
6268
Denys Vlasenko29301232018-12-11 15:29:32 +01006269 s = zbc_program_prep(&opnd, &num);
6270 if (s) RETURN_STATUS(s);
6271 s = zbc_num_ulong(num, &val);
6272 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006273
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006274 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006275
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006276 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006277 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006278 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006279 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006280 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006281
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006282 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006283
Denys Vlasenko29301232018-12-11 15:29:32 +01006284 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006285}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006286#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006287
Denys Vlasenkofa210792018-12-19 19:35:40 +01006288static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006289{
6290 BcStatus s = BC_STATUS_SUCCESS;
6291 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006292 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006293 BcInstPtr ip;
6294 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006295
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006296 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006297 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006298
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006299 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006300
6301 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006302 BcNum *n = n; // for compiler
6303 bool exec;
6304 char *name;
6305 char *then_name = bc_program_name(code, bgn);
6306 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006307
6308 if (code[*bgn] == BC_PARSE_STREND)
6309 (*bgn) += 1;
6310 else
6311 else_name = bc_program_name(code, bgn);
6312
6313 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006314 name = then_name;
6315 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006316 exec = true;
6317 name = else_name;
6318 }
6319
6320 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006321 BcVec *v;
6322 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006323 n = bc_vec_top(v);
6324 }
6325
6326 free(then_name);
6327 free(else_name);
6328
6329 if (!exec) goto exit;
6330 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006331 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006332 goto exit;
6333 }
6334
6335 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006336 } else {
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006337 if (r->t == XC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006338 sidx = r->d.id.idx;
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006339 } else if (r->t == XC_RESULT_VAR) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006340 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006341 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006342 if (s || !BC_PROG_STR(n)) goto exit;
6343 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006344 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006345 goto exit;
6346 }
6347
6348 fidx = sidx + BC_PROG_REQ_FUNCS;
6349
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006350 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006351
6352 if (f->code.len == 0) {
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006353 BcParse sv_parse;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006354 char *str;
6355
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006356 sv_parse = G.prs; // struct copy
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006357 bc_parse_create(fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006358 str = *bc_program_str(sidx);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006359 s = zbc_parse_text_init(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006360 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006361
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006362 s = zdc_parse_exprs_until_eof();
Gavin Howard01055ba2018-11-03 11:00:21 -06006363 if (s) goto err;
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006364 bc_parse_push(DC_INST_POP_EXEC);
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006365 if (G.prs.lex != XC_LEX_EOF)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006366 s = bc_error_bad_expression();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006367 bc_parse_free();
Denys Vlasenko1fbe35a2018-12-25 19:38:13 +01006368 G.prs = sv_parse; // struct copy
6369 if (s) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006370 err:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006371 bc_vec_pop_all(&f->code);
6372 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006373 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006374 }
6375
Denys Vlasenko24e41942018-12-21 23:01:26 +01006376 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006377 ip.func = fidx;
6378
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006379 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006380 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006381
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006382 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006383 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006384 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006385 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006386}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006387#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006388#endif // ENABLE_DC
6389
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006390static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006391{
Gavin Howard01055ba2018-11-03 11:00:21 -06006392 BcResult res;
6393 unsigned long val;
6394
Denys Vlasenkod897c9a2018-12-24 23:41:31 +01006395 res.t = inst - XC_INST_IBASE + XC_RESULT_IBASE;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006396 if (inst == XC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006397 val = (unsigned long) G.prog.ib_t;
Denys Vlasenkoa7732d12018-12-24 04:26:07 +01006398 else if (inst == XC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006399 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006400 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006401 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006402
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006403 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006404 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006405 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006406}
6407
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006408static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006409{
Gavin Howard01055ba2018-11-03 11:00:21 -06006410 BcResult r, *ptr;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006411 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006412 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006413 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006414
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006415 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006416 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6417 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006418 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006419 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006420
Denys Vlasenko24e41942018-12-21 23:01:26 +01006421 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 -06006422 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006423#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006424 case BC_INST_JUMP_ZERO: {
6425 BcNum *num;
6426 bool zero;
6427 dbg_exec("BC_INST_JUMP_ZERO:");
6428 s = zbc_program_prep(&ptr, &num);
6429 if (s) RETURN_STATUS(s);
6430 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
6431 bc_vec_pop(&G.prog.results);
6432 if (!zero) {
6433 bc_program_index(code, &ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006434 break;
6435 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006436 // else: fall through
6437 }
6438 case BC_INST_JUMP: {
6439 size_t idx = bc_program_index(code, &ip->inst_idx);
6440 size_t *addr = bc_vec_item(&func->labels, idx);
6441 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
6442 ip->inst_idx = *addr;
6443 break;
6444 }
6445 case BC_INST_CALL:
6446 dbg_exec("BC_INST_CALL:");
6447 s = zbc_program_call(code, &ip->inst_idx);
6448 goto read_updated_ip;
6449 case BC_INST_INC_PRE:
6450 case BC_INST_DEC_PRE:
6451 case BC_INST_INC_POST:
6452 case BC_INST_DEC_POST:
6453 dbg_exec("BC_INST_INCDEC:");
6454 s = zbc_program_incdec(inst);
6455 break;
6456 case BC_INST_HALT:
6457 dbg_exec("BC_INST_HALT:");
6458 QUIT_OR_RETURN_TO_MAIN;
6459 break;
6460 case XC_INST_RET:
6461 case BC_INST_RET0:
6462 dbg_exec("BC_INST_RET[0]:");
6463 s = zbc_program_return(inst);
6464 goto read_updated_ip;
6465 case XC_INST_BOOL_OR:
6466 case XC_INST_BOOL_AND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006467#endif // ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006468 case XC_INST_REL_EQ:
6469 case XC_INST_REL_LE:
6470 case XC_INST_REL_GE:
6471 case XC_INST_REL_NE:
6472 case XC_INST_REL_LT:
6473 case XC_INST_REL_GT:
6474 dbg_exec("BC_INST_BOOL:");
6475 s = zbc_program_logical(inst);
6476 break;
6477 case XC_INST_READ:
6478 dbg_exec("XC_INST_READ:");
6479 s = zbc_program_read();
6480 goto read_updated_ip;
6481 case XC_INST_VAR:
6482 dbg_exec("XC_INST_VAR:");
6483 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
6484 break;
6485 case XC_INST_ARRAY_ELEM:
6486 case XC_INST_ARRAY:
6487 dbg_exec("XC_INST_ARRAY[_ELEM]:");
6488 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
6489 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006490#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006491 case BC_INST_LAST:
6492 dbg_exec("BC_INST_LAST:");
6493 r.t = BC_RESULT_LAST;
6494 bc_vec_push(&G.prog.results, &r);
6495 break;
Denys Vlasenkoad0bd382018-12-24 00:50:32 +01006496#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006497 case XC_INST_IBASE:
6498 case XC_INST_OBASE:
6499 case XC_INST_SCALE:
6500 dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE);
6501 bc_program_pushGlobal(inst);
6502 break;
6503 case XC_INST_SCALE_FUNC:
6504 case XC_INST_LENGTH:
6505 case XC_INST_SQRT:
6506 dbg_exec("BC_INST_builtin:");
6507 s = zbc_program_builtin(inst);
6508 break;
6509 case XC_INST_NUM:
6510 dbg_exec("XC_INST_NUM:");
6511 r.t = XC_RESULT_CONSTANT;
6512 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
6513 bc_vec_push(&G.prog.results, &r);
6514 break;
6515 case XC_INST_POP:
6516 dbg_exec("XC_INST_POP:");
6517 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6518 s = bc_error_stack_has_too_few_elements();
6519 else
6520 bc_vec_pop(&G.prog.results);
6521 break;
6522 case XC_INST_PRINT:
6523 case XC_INST_PRINT_POP:
6524 case XC_INST_PRINT_STR:
6525 dbg_exec("XC_INST_PRINTxyz:");
6526 s = zbc_program_print(inst, 0);
6527 break;
6528 case XC_INST_STR:
6529 dbg_exec("XC_INST_STR:");
6530 r.t = XC_RESULT_STR;
6531 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
6532 bc_vec_push(&G.prog.results, &r);
6533 break;
6534 case XC_INST_POWER:
6535 case XC_INST_MULTIPLY:
6536 case XC_INST_DIVIDE:
6537 case XC_INST_MODULUS:
6538 case XC_INST_PLUS:
6539 case XC_INST_MINUS:
6540 dbg_exec("BC_INST_binaryop:");
6541 s = zbc_program_op(inst);
6542 break;
6543 case XC_INST_BOOL_NOT: {
6544 BcNum *num;
6545 dbg_exec("XC_INST_BOOL_NOT:");
6546 s = zbc_program_prep(&ptr, &num);
6547 if (s) RETURN_STATUS(s);
6548 bc_num_init_DEF_SIZE(&r.d.n);
6549 if (bc_num_cmp(num, &G.prog.zero) == 0)
6550 bc_num_one(&r.d.n);
6551 //else bc_num_zero(&r.d.n); - already is
6552 bc_program_retire(&r, XC_RESULT_TEMP);
6553 break;
6554 }
6555 case XC_INST_NEG:
6556 dbg_exec("XC_INST_NEG:");
6557 s = zbc_program_negate();
6558 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006559#if ENABLE_BC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006560 case BC_INST_ASSIGN_POWER:
6561 case BC_INST_ASSIGN_MULTIPLY:
6562 case BC_INST_ASSIGN_DIVIDE:
6563 case BC_INST_ASSIGN_MODULUS:
6564 case BC_INST_ASSIGN_PLUS:
6565 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006566#endif
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006567 case XC_INST_ASSIGN:
6568 dbg_exec("BC_INST_ASSIGNxyz:");
6569 s = zbc_program_assign(inst);
6570 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006571#if ENABLE_DC
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006572 case DC_INST_POP_EXEC:
6573 dbg_exec("DC_INST_POP_EXEC:");
6574 bc_vec_pop(&G.prog.exestack);
6575 goto read_updated_ip;
6576 case DC_INST_MODEXP:
6577 dbg_exec("DC_INST_MODEXP:");
6578 s = zdc_program_modexp();
6579 break;
6580 case DC_INST_DIVMOD:
6581 dbg_exec("DC_INST_DIVMOD:");
6582 s = zdc_program_divmod();
6583 break;
6584 case DC_INST_EXECUTE:
6585 case DC_INST_EXEC_COND:
6586 dbg_exec("DC_INST_EXEC[_COND]:");
6587 s = zdc_program_execStr(code, &ip->inst_idx, inst == DC_INST_EXEC_COND);
6588 goto read_updated_ip;
6589 case DC_INST_PRINT_STACK: {
6590 size_t idx;
6591 dbg_exec("DC_INST_PRINT_STACK:");
6592 for (idx = 0; idx < G.prog.results.len; ++idx) {
6593 s = zbc_program_print(XC_INST_PRINT, idx);
6594 if (s) break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006595 }
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006596 break;
6597 }
6598 case DC_INST_CLEAR_STACK:
6599 dbg_exec("DC_INST_CLEAR_STACK:");
6600 bc_vec_pop_all(&G.prog.results);
6601 break;
6602 case DC_INST_STACK_LEN:
6603 dbg_exec("DC_INST_STACK_LEN:");
6604 dc_program_stackLen();
6605 break;
6606 case DC_INST_DUPLICATE:
6607 dbg_exec("DC_INST_DUPLICATE:");
6608 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6609 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6610 ptr = bc_vec_top(&G.prog.results);
6611 dc_result_copy(&r, ptr);
6612 bc_vec_push(&G.prog.results, &r);
6613 break;
6614 case DC_INST_SWAP: {
6615 BcResult *ptr2;
6616 dbg_exec("DC_INST_SWAP:");
6617 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
6618 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6619 ptr = bc_vec_item_rev(&G.prog.results, 0);
6620 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
6621 memcpy(&r, ptr, sizeof(BcResult));
6622 memcpy(ptr, ptr2, sizeof(BcResult));
6623 memcpy(ptr2, &r, sizeof(BcResult));
6624 break;
6625 }
6626 case DC_INST_ASCIIFY:
6627 dbg_exec("DC_INST_ASCIIFY:");
6628 s = zdc_program_asciify();
6629 break;
6630 case DC_INST_PRINT_STREAM:
6631 dbg_exec("DC_INST_PRINT_STREAM:");
6632 s = zdc_program_printStream();
6633 break;
6634 case DC_INST_LOAD:
6635 case DC_INST_PUSH_VAR: {
6636 bool copy = inst == DC_INST_LOAD;
6637 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
6638 break;
6639 }
6640 case DC_INST_PUSH_TO_VAR: {
6641 char *name = bc_program_name(code, &ip->inst_idx);
6642 s = zbc_program_copyToVar(name, true);
6643 free(name);
6644 break;
6645 }
6646 case DC_INST_QUIT:
6647 dbg_exec("DC_INST_QUIT:");
6648 if (G.prog.exestack.len <= 2)
6649 QUIT_OR_RETURN_TO_MAIN;
6650 bc_vec_npop(&G.prog.exestack, 2);
6651 goto read_updated_ip;
6652 case DC_INST_NQUIT:
6653 dbg_exec("DC_INST_NQUIT:");
6654 s = zdc_program_nquit();
6655 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006656#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006657 read_updated_ip:
Denys Vlasenko1c69ec12018-12-26 19:24:15 +01006658 // Instruction stack has changed, read new pointers
6659 ip = bc_vec_top(&G.prog.exestack);
6660 func = bc_program_func(ip->func);
6661 code = func->code.v;
6662 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006663 }
6664
Denys Vlasenkod38af482018-12-04 19:11:02 +01006665 if (s || G_interrupt) {
6666 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006667 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006668 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006669
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006670 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006671 }
6672
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006673 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006674}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006675#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006676
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006677static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006678{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006679 char *lenv;
6680 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006681
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006682 lenv = getenv(var);
6683 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006684 if (!lenv) return len;
6685
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006686 len = bb_strtou(lenv, NULL, 10) - 1;
6687 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006688 len = BC_NUM_PRINT_WIDTH;
6689
6690 return len;
6691}
6692
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006693static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006694{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006695 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006696
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006697 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006698 s = zbc_parse_text_init(text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006699 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006700
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006701 IF_BC(check_eof:)
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006702 while (G.prs.lex != XC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006703 BcInstPtr *ip;
6704 BcFunc *f;
6705
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006706 dbg_lex("%s:%d G.prs.lex:%d, parsing...", __func__, __LINE__, G.prs.lex);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006707 if (IS_BC) {
Denys Vlasenko26384542018-12-25 18:37:52 +01006708#if ENABLE_BC
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006709 if (G.prs.lex == BC_LEX_SCOLON
6710 || G.prs.lex == XC_LEX_NLINE
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006711 ) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006712 s = zbc_lex_next();
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006713 if (s) goto err;
6714 goto check_eof;
6715 }
6716
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006717 s = zbc_parse_stmt_or_funcdef();
Denys Vlasenko26384542018-12-25 18:37:52 +01006718 if (s) goto err;
6719
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006720 // Check that next token is a correct stmt delimiter -
6721 // disallows "print 1 print 2" and such.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006722 if (G.prs.lex != BC_LEX_SCOLON
6723 && G.prs.lex != XC_LEX_NLINE
6724 && G.prs.lex != XC_LEX_EOF
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006725 ) {
6726 const char *err_at;
6727//TODO: commonalize for other parse errors:
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006728 err_at = G.prs.lex_next_at ? G.prs.lex_next_at : "UNKNOWN";
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006729 bc_error_fmt("bad statement terminator at '%.*s'",
6730 (int)(strchrnul(err_at, '\n') - err_at),
6731 err_at
6732 );
6733 goto err;
Denys Vlasenko26384542018-12-25 18:37:52 +01006734 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006735 // The above logic is fragile. Check these examples:
6736 // - interative read() still works
Denys Vlasenko26384542018-12-25 18:37:52 +01006737#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006738 } else {
Denys Vlasenko26384542018-12-25 18:37:52 +01006739#if ENABLE_DC
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006740 // Most of dc parsing assumes all whitespace,
6741 // including '\n', is eaten.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006742 while (G.prs.lex == XC_LEX_NLINE) {
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006743 s = zbc_lex_next();
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006744 if (s) goto err;
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006745 if (G.prs.lex == XC_LEX_EOF)
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006746 goto done;
6747 }
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006748 s = zdc_parse_expr();
Denys Vlasenko26384542018-12-25 18:37:52 +01006749#endif
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006750 }
6751 if (s || G_interrupt) {
Denys Vlasenko9471bd42018-12-23 00:13:15 +01006752 err:
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01006753 bc_parse_reset(); // includes bc_program_reset()
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006754 RETURN_STATUS(BC_STATUS_FAILURE);
6755 }
Denys Vlasenko53e569c2018-12-25 19:37:23 +01006756
Denys Vlasenko39287e02018-12-22 02:23:08 +01006757 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006758 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006759 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006760 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006761 break;
6762 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006763
6764 ip = (void*)G.prog.exestack.v;
6765#if SANITY_CHECKS
6766 if (G.prog.exestack.len != 1) // should have only main's IP
6767 bb_error_msg_and_die("BUG:call stack");
6768 if (ip->func != BC_PROG_MAIN)
6769 bb_error_msg_and_die("BUG:not MAIN");
6770#endif
6771 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006772 // bc discards strings, constants and code after each
6773 // top-level statement in the "main program".
6774 // This prevents "yes 1 | bc" from growing its memory
6775 // without bound. This can be done because data stack
6776 // is empty and thus can't hold any references to
6777 // strings or constants, there is no generated code
6778 // which can hold references (after we discard one
6779 // we just executed). Code of functions can have references,
6780 // but bc stores function strings/constants in per-function
6781 // storage.
6782 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006783#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006784 if (G.prog.results.len != 0) // should be empty
6785 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006786#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006787 IF_BC(bc_vec_pop_all(&f->strs);)
6788 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006789 } else {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006790 if (G.prog.results.len == 0
6791 && G.prog.vars.len == 0
6792 ) {
6793 // If stack is empty and no registers exist (TODO: or they are all empty),
6794 // we can get rid of accumulated strings and constants.
6795 // In this example dc process should not grow
6796 // its memory consumption with time:
6797 // yes 1pc | dc
6798 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6799 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6800 }
6801 // The code is discarded always (below), thus this example
6802 // should also not grow its memory consumption with time,
6803 // even though its data stack is not empty:
6804 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006805 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006806 // We drop generated and executed code for both bc and dc:
6807 bc_vec_pop_all(&f->code);
6808 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006809 }
Denys Vlasenko8a56e362018-12-26 19:09:23 +01006810 IF_DC(done:)
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006811 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006812 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006813}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006814#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006815
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006816static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006817{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006818 // So far bc/dc have no way to include a file from another file,
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006819 // therefore we know G.prs.lex_filename == NULL on entry
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006820 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006821 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006822
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006823 G.prs.lex_filename = filename;
6824 G.prs.lex_input_fp = fp;
Denys Vlasenko7d32e252018-12-26 18:32:43 +01006825 G.err_line = G.prs.lex_line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06006826
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006827 do {
6828 s = zbc_vm_process("");
6829 // We do not stop looping on errors here if reading stdin.
6830 // Example: start interactive bc and enter "return".
6831 // It should say "'return' not in a function"
6832 // but should not exit.
Denys Vlasenkoecb62ed2018-12-25 22:32:41 +01006833 } while (G.prs.lex_input_fp == stdin);
6834 G.prs.lex_filename = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006835 RETURN_STATUS(s);
6836}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006837#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006838
6839static BC_STATUS zbc_vm_file(const char *file)
6840{
6841 BcStatus s;
6842 FILE *fp;
6843
6844 fp = xfopen_for_read(file);
6845 s = zbc_vm_execute_FILE(fp, file);
6846 fclose(fp);
6847
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006848 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006849}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006850#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006851
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006852#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006853static void bc_vm_info(void)
6854{
6855 printf("%s "BB_VER"\n"
Gavin Howardfa495ce2018-12-18 10:03:14 -07006856 "Adapted from https://github.com/gavinhoward/bc\n"
6857 "Original code (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko57b69182018-12-14 00:12:13 +01006858 , applet_name);
6859}
6860
6861static void bc_args(char **argv)
6862{
6863 unsigned opts;
6864 int i;
6865
6866 GETOPT_RESET();
6867#if ENABLE_FEATURE_BC_LONG_OPTIONS
6868 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6869 "warn\0" No_argument "w"
6870 "version\0" No_argument "v"
6871 "standard\0" No_argument "s"
6872 "quiet\0" No_argument "q"
6873 "mathlib\0" No_argument "l"
6874 "interactive\0" No_argument "i"
6875 );
6876#else
6877 opts = option_mask32 |= getopt32(argv, "wvsqli");
6878#endif
6879 if (getenv("POSIXLY_CORRECT"))
6880 option_mask32 |= BC_FLAG_S;
6881
6882 if (opts & BC_FLAG_V) {
6883 bc_vm_info();
6884 exit(0);
6885 }
6886
6887 for (i = optind; argv[i]; ++i)
6888 bc_vec_push(&G.files, argv + i);
6889}
6890
6891static void bc_vm_envArgs(void)
6892{
6893 BcVec v;
6894 char *buf;
6895 char *env_args = getenv("BC_ENV_ARGS");
6896
6897 if (!env_args) return;
6898
6899 G.env_args = xstrdup(env_args);
6900 buf = G.env_args;
6901
6902 bc_vec_init(&v, sizeof(char *), NULL);
6903
6904 while (*(buf = skip_whitespace(buf)) != '\0') {
6905 bc_vec_push(&v, &buf);
6906 buf = skip_non_whitespace(buf);
6907 if (!*buf)
6908 break;
6909 *buf++ = '\0';
6910 }
6911
6912 // NULL terminate, and pass argv[] so that first arg is argv[1]
6913 if (sizeof(int) == sizeof(char*)) {
6914 bc_vec_push(&v, &const_int_0);
6915 } else {
6916 static char *const nullptr = NULL;
6917 bc_vec_push(&v, &nullptr);
6918 }
6919 bc_args(((char **)v.v) - 1);
6920
6921 bc_vec_free(&v);
6922}
6923
6924static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006925 "scale=20"
6926"\n" "define e(x){"
6927"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006928////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6929//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6930//e(-.998896): GNU:.36828580434569428695
6931// above code:.36828580434569428696
6932// actual value:.3682858043456942869594...
6933// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006934"\n" "b=ibase"
6935"\n" "ibase=A"
6936"\n" "if(x<0){"
6937"\n" "n=1"
6938"\n" "x=-x"
6939"\n" "}"
6940"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006941"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006942"\n" "scale=scale(x)+1"
6943"\n" "while(x>1){"
6944"\n" "d+=1"
6945"\n" "x/=2"
6946"\n" "scale+=1"
6947"\n" "}"
6948"\n" "scale=r"
6949"\n" "r=x+1"
6950"\n" "p=x"
6951"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006952"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006953"\n" "p*=x"
6954"\n" "f*=i"
6955"\n" "v=p/f"
6956"\n" "r+=v"
6957"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006958"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006959"\n" "scale=s"
6960"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006961"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006962"\n" "return(r/1)"
6963"\n" "}"
6964"\n" "define l(x){"
6965"\n" "auto b,s,r,p,a,q,i,v"
6966"\n" "b=ibase"
6967"\n" "ibase=A"
6968"\n" "if(x<=0){"
6969"\n" "r=(1-10^scale)/1"
6970"\n" "ibase=b"
6971"\n" "return(r)"
6972"\n" "}"
6973"\n" "s=scale"
6974"\n" "scale+=6"
6975"\n" "p=2"
6976"\n" "while(x>=2){"
6977"\n" "p*=2"
6978"\n" "x=sqrt(x)"
6979"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006980"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006981"\n" "p*=2"
6982"\n" "x=sqrt(x)"
6983"\n" "}"
6984"\n" "r=a=(x-1)/(x+1)"
6985"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006986"\n" "v=1"
6987"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006988"\n" "a*=q"
6989"\n" "v=a/i"
6990"\n" "r+=v"
6991"\n" "}"
6992"\n" "r*=p"
6993"\n" "scale=s"
6994"\n" "ibase=b"
6995"\n" "return(r/1)"
6996"\n" "}"
6997"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006998"\n" "auto b,s,r,a,q,i"
6999"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007000"\n" "b=ibase"
7001"\n" "ibase=A"
7002"\n" "s=scale"
7003"\n" "scale=1.1*s+2"
7004"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007005"\n" "scale=0"
7006"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007007"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007008"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007009"\n" "scale=s+2"
7010"\n" "r=a=x"
7011"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007012"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007013"\n" "a*=q/(i*(i-1))"
7014"\n" "r+=a"
7015"\n" "}"
7016"\n" "scale=s"
7017"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007018"\n" "return(r/1)"
7019"\n" "}"
7020"\n" "define c(x){"
7021"\n" "auto b,s"
7022"\n" "b=ibase"
7023"\n" "ibase=A"
7024"\n" "s=scale"
7025"\n" "scale*=1.2"
7026"\n" "x=s(2*a(1)+x)"
7027"\n" "scale=s"
7028"\n" "ibase=b"
7029"\n" "return(x/1)"
7030"\n" "}"
7031"\n" "define a(x){"
7032"\n" "auto b,s,r,n,a,m,t,f,i,u"
7033"\n" "b=ibase"
7034"\n" "ibase=A"
7035"\n" "n=1"
7036"\n" "if(x<0){"
7037"\n" "n=-1"
7038"\n" "x=-x"
7039"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007040"\n" "if(scale<65){"
7041"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7042"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007043"\n" "}"
7044"\n" "s=scale"
7045"\n" "if(x>.2){"
7046"\n" "scale+=5"
7047"\n" "a=a(.2)"
7048"\n" "}"
7049"\n" "scale=s+3"
7050"\n" "while(x>.2){"
7051"\n" "m+=1"
7052"\n" "x=(x-.2)/(1+.2*x)"
7053"\n" "}"
7054"\n" "r=u=x"
7055"\n" "f=-x*x"
7056"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007057"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007058"\n" "u*=f"
7059"\n" "t=u/i"
7060"\n" "r+=t"
7061"\n" "}"
7062"\n" "scale=s"
7063"\n" "ibase=b"
7064"\n" "return((m*a+r)/n)"
7065"\n" "}"
7066"\n" "define j(n,x){"
7067"\n" "auto b,s,o,a,i,v,f"
7068"\n" "b=ibase"
7069"\n" "ibase=A"
7070"\n" "s=scale"
7071"\n" "scale=0"
7072"\n" "n/=1"
7073"\n" "if(n<0){"
7074"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007075"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007076"\n" "}"
7077"\n" "a=1"
7078"\n" "for(i=2;i<=n;++i)a*=i"
7079"\n" "scale=1.5*s"
7080"\n" "a=(x^n)/2^n/a"
7081"\n" "r=v=1"
7082"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007083"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007084"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007085"\n" "v=v*f/i/(n+i)"
7086"\n" "r+=v"
7087"\n" "}"
7088"\n" "scale=s"
7089"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007090"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007091"\n" "return(a*r/1)"
7092"\n" "}"
7093};
7094#endif // ENABLE_BC
7095
Denys Vlasenko19f11072018-12-12 22:48:19 +01007096static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007097{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007098 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007099 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007100 size_t i;
7101
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007102#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007103 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007104 // We know that internal library is not buggy,
7105 // thus error checking is normally disabled.
7106# define DEBUG_LIB 0
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007107 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007108 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007109 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007110#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007111
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007112 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007113 fname = (void*)G.files.v;
7114 for (i = 0; i < G.files.len; i++) {
7115 s = zbc_vm_file(*fname++);
7116 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7117 // Debug config, non-interactive mode:
7118 // return all the way back to main.
7119 // Non-debug builds do not come here
7120 // in non-interactive mode, they exit.
7121 RETURN_STATUS(s);
7122 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007123 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007124
Denys Vlasenko91cde952018-12-10 20:56:08 +01007125 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007126 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007127
Denys Vlasenko19f11072018-12-12 22:48:19 +01007128 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007129}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007130#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007131
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007132#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007133static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007134{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007135 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007136 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007137 bc_vec_free(&G.prog.vars);
7138 bc_vec_free(&G.prog.var_map);
7139 bc_vec_free(&G.prog.arrs);
7140 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007141 IF_DC(bc_vec_free(&G.prog.strs);)
7142 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007143 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007144 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007145 IF_BC(bc_num_free(&G.prog.last);)
7146 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007147 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007148 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007149}
7150
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007151static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007152{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007153 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007154 bc_program_free();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01007155 bc_parse_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007156 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007157}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007158#endif
7159
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007160static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007161{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007162 BcInstPtr ip;
7163
Denys Vlasenko82269122018-12-14 16:30:56 +01007164 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007165 memset(&ip, 0, sizeof(BcInstPtr));
7166
Denys Vlasenko82269122018-12-14 16:30:56 +01007167 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007168 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007169 G.prog.ob_t = 10;
7170
Denys Vlasenko503faf92018-12-20 16:24:18 +01007171 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7172 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007173
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007174 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007175 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007176
Denys Vlasenko503faf92018-12-20 16:24:18 +01007177 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7178 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007179
7180 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007181 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007182
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007183 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007184 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007185 // a valid function name (and be short)
7186 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007187 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007188 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007189 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007190 bc_program_add_fn();
7191 bc_program_add_fn();
7192 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007193
7194 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007195 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007196
7197 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007198 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007199
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007200 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7201 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007202 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007203 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7204 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007205
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007206 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007207}
Gavin Howard01055ba2018-11-03 11:00:21 -06007208
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007209static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007210{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007211#if ENABLE_FEATURE_EDITING
7212 G.line_input_state = new_line_input_t(DO_HISTORY);
7213#endif
7214 G.prog.len = bc_vm_envLen(env_len);
7215
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007216 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007217 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007218 bc_program_init();
Denys Vlasenkoa2e62e32018-12-25 20:40:55 +01007219 bc_parse_create(BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007220
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007221//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7222//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007223 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007224#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007225 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007226 // With SA_RESTART, most system calls will restart
7227 // (IOW: they won't fail with EINTR).
7228 // In particular, this means ^C won't cause
7229 // stdout to get into "error state" if SIGINT hits
7230 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007231 //
7232 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007233 // will only be handled after [Enter] since read()
7234 // from stdin is not interrupted by ^C either,
7235 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007236 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007237 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7238
7239 // Without SA_RESTART, this exhibits a bug:
7240 // "while (1) print 1" and try ^C-ing it.
7241 // Intermittently, instead of returning to input line,
7242 // you'll get "output error: Interrupted system call"
7243 // and exit.
7244 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007245#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007246 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007247 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007248 return 0; // "not a tty"
7249}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007250
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007251static BcStatus bc_vm_run(void)
7252{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007253 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007254#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007255 if (G_exiting) // it was actually "halt" or "quit"
7256 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007257 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007258# if ENABLE_FEATURE_EDITING
7259 free_line_input_t(G.line_input_state);
7260# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007261 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007262#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007263 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007264 return st;
7265}
7266
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007267#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007268int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007269int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007270{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007271 int is_tty;
7272
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007273 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007274
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007275 is_tty = bc_vm_init("BC_LINE_LENGTH");
7276
7277 bc_args(argv);
7278
7279 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7280 bc_vm_info();
7281
7282 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007283}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007284#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007285
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007286#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007287int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007288int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007289{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007290 int noscript;
7291
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007292 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007293
7294 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7295 // 1 char wider than bc from the same package.
7296 // Both default width, and xC_LINE_LENGTH=N are wider:
7297 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007298 // |1234\ |
7299 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007300 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007301 // |123\ |
7302 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007303 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007304 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007305
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007306 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7307 noscript = BC_FLAG_I;
7308 for (;;) {
7309 int n = getopt(argc, argv, "e:f:x");
7310 if (n <= 0)
7311 break;
7312 switch (n) {
7313 case 'e':
7314 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007315 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007316 if (n) return n;
7317 break;
7318 case 'f':
7319 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007320 n = zbc_vm_file(optarg);
7321 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007322 break;
7323 case 'x':
7324 option_mask32 |= DC_FLAG_X;
7325 break;
7326 default:
7327 bb_show_usage();
7328 }
7329 }
7330 argv += optind;
7331
7332 while (*argv) {
7333 noscript = 0;
7334 bc_vec_push(&G.files, argv++);
7335 }
7336
7337 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7338
7339 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007340}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007341#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007342
7343#endif // not DC_SMALL