blob: 441cb04676ed235e5f669836c70101d8fda1d6cf [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 * Copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06005 */
6//config:config BC
7//config: bool "bc (45 kb; 49 kb when combined with dc)"
8//config: default y
9//config: help
10//config: bc is a command-line, arbitrary-precision calculator with a
11//config: Turing-complete language. See the GNU bc manual
12//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
Denys Vlasenko89e785a2018-12-13 16:35:52 +010013//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060014//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010015//config: This bc has five differences to the GNU bc:
16//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060017//config: 2) Arrays are copied before being passed as arguments to
18//config: functions. This behavior is required by the bc spec.
19//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010020//config: the number of elements in the array. This prints "1":
21//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060022//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010023//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060024//config: allows POSIX-compliant scripts to work while somewhat
25//config: preserving expected behavior (versus C) and making parsing
26//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010027//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060028//config:
29//config: Options:
Gavin Howard01055ba2018-11-03 11:00:21 -060030//config: -i --interactive force interactive mode
Denys Vlasenko89e785a2018-12-13 16:35:52 +010031//config: -q --quiet don't print version and copyright
32//config: -s --standard error if any non-POSIX extensions are used
33//config: -w --warn warn if any non-POSIX extensions are used
Gavin Howard01055ba2018-11-03 11:00:21 -060034//config: -l --mathlib use predefined math routines:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010035//config: s(expr) sine in radians
36//config: c(expr) cosine in radians
37//config: a(expr) arctangent, returning radians
38//config: l(expr) natural log
39//config: e(expr) raises e to the power of expr
40//config: j(n, x) Bessel function of integer order n of x
Gavin Howard01055ba2018-11-03 11:00:21 -060041//config:
42//config:config DC
43//config: bool "dc (38 kb; 49 kb when combined with bc)"
44//config: default y
45//config: help
46//config: dc is a reverse-polish notation command-line calculator which
47//config: supports unlimited precision arithmetic. See the FreeBSD man page
48//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010049//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060050//config:
51//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060053//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010054//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060055//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010056//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060057//config: 4) Like the FreeBSD dc, this dc supports extended registers.
58//config: However, they are implemented differently. When it encounters
59//config: whitespace where a register should be, it skips the whitespace.
60//config: If the character following is not a lowercase letter, an error
61//config: is issued. Otherwise, the register name is parsed by the
62//config: following regex:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010063//config: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060064//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010065//config: whitespace. Examples:
66//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060067//config: Also note that, like the FreeBSD dc, extended registers are not
68//config: allowed unless the "-x" option is given.
69//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010070//config:config FEATURE_DC_SMALL
71//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
72//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010073//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010074//config:
75//config:config FEATURE_DC_LIBM
76//config: bool "Enable power and exp functions (requires libm)"
77//config: default y
78//config: depends on FEATURE_DC_SMALL
79//config: help
80//config: Enable power and exp functions.
81//config: NOTE: This will require libm to be present for linking.
82//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060083//config:config FEATURE_BC_SIGNALS
Denys Vlasenko89e785a2018-12-13 16:35:52 +010084//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060085//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010086//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -060087//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010088//config: Enable interactive mode: when started on a tty,
89//config: ^C interrupts execution and returns to command line,
90//config: errors also return to command line instead of exiting,
91//config: line editing with history is available.
92//config:
93//config: With this option off, input can still be taken from tty,
94//config: but all errors are fatal, ^C is fatal,
95//config: tty is treated exactly the same as any other
96//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060097//config:
98//config:config FEATURE_BC_LONG_OPTIONS
99//config: bool "Enable bc/dc long options"
100//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100101//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600102//config: help
103//config: Enable long options for bc and dc.
104
105//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
106//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
107
108//kbuild:lib-$(CONFIG_BC) += bc.o
109//kbuild:lib-$(CONFIG_DC) += bc.o
110
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100111//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600112//usage:#define bc_trivial_usage
Denys Vlasenko09fe0aa2018-12-18 16:32:25 +0100113//usage: "[-sqlw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600114//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100115//usage:#define bc_full_usage "\n"
116//usage: "\nArbitrary precision calculator"
117//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100118///////: "\n -i Interactive" - has no effect for now
119//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100120//usage: "\n -l Load standard math library"
121//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100122//usage: "\n -w Warn if extensions are used"
123///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100124//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100125//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600126//usage:
127//usage:#define bc_example_usage
128//usage: "3 + 4.129\n"
129//usage: "1903 - 2893\n"
130//usage: "-129 * 213.28935\n"
131//usage: "12 / -1932\n"
132//usage: "12 % 12\n"
133//usage: "34 ^ 189\n"
134//usage: "scale = 13\n"
135//usage: "ibase = 2\n"
136//usage: "obase = A\n"
137//usage:
138//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100139//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600140//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100141//usage:#define dc_full_usage "\n"
142//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100143//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100144//usage: "\np - print top of the stack (without popping)"
145//usage: "\nf - print entire stack"
146//usage: "\nk - pop the value and set the precision"
147//usage: "\ni - pop the value and set input radix"
148//usage: "\no - pop the value and set output radix"
149//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600150//usage:
151//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100152//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600153//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100154//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600155//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100156//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600157//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100158//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600159//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600161//usage: "64\n"
162
163#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100164#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600165
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100166#if ENABLE_FEATURE_DC_SMALL
167# include "dc.c"
168#else
169
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100170#define DEBUG_LEXER 0
171#define DEBUG_COMPILE 0
172#define DEBUG_EXEC 0
Denys Vlasenko19eee8e2018-12-21 20:29:34 +0100173// This can be left enabled for production as well:
174#define SANITY_CHECKS 1
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100175
176#if DEBUG_LEXER
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100177static uint8_t lex_indent;
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100178#define dbg_lex(...) \
179 do { \
180 fprintf(stderr, "%*s", lex_indent, ""); \
181 bb_error_msg(__VA_ARGS__); \
182 } while (0)
183#define dbg_lex_enter(...) \
184 do { \
185 dbg_lex(__VA_ARGS__); \
186 lex_indent++; \
187 } while (0)
188#define dbg_lex_done(...) \
189 do { \
190 lex_indent--; \
191 dbg_lex(__VA_ARGS__); \
192 } while (0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100193#else
Denys Vlasenko2ea53a42018-12-14 17:51:17 +0100194# define dbg_lex(...) ((void)0)
195# define dbg_lex_enter(...) ((void)0)
196# define dbg_lex_done(...) ((void)0)
Denys Vlasenko0a238142018-12-14 16:48:34 +0100197#endif
198
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100199#if DEBUG_COMPILE
200# define dbg_compile(...) bb_error_msg(__VA_ARGS__)
201#else
202# define dbg_compile(...) ((void)0)
203#endif
204
Denys Vlasenko99b37622018-12-15 20:06:59 +0100205#if DEBUG_EXEC
206# define dbg_exec(...) bb_error_msg(__VA_ARGS__)
207#else
208# define dbg_exec(...) ((void)0)
209#endif
210
Gavin Howard01055ba2018-11-03 11:00:21 -0600211typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100212 BC_STATUS_SUCCESS = 0,
213 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100214 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600215} BcStatus;
216
Gavin Howard01055ba2018-11-03 11:00:21 -0600217#define BC_VEC_INVALID_IDX ((size_t) -1)
218#define BC_VEC_START_CAP (1 << 5)
219
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100220typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600221
222typedef struct BcVec {
223 char *v;
224 size_t len;
225 size_t cap;
226 size_t size;
227 BcVecFree dtor;
228} BcVec;
229
Gavin Howard01055ba2018-11-03 11:00:21 -0600230typedef signed char BcDig;
231
232typedef struct BcNum {
233 BcDig *restrict num;
234 size_t rdx;
235 size_t len;
236 size_t cap;
237 bool neg;
238} BcNum;
239
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100240#define BC_NUM_MAX_IBASE ((unsigned long) 16)
241// larger value might speed up BIGNUM calculations a bit:
242#define BC_NUM_DEF_SIZE (16)
243#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600244
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100245#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600246
Gavin Howard01055ba2018-11-03 11:00:21 -0600247typedef enum BcInst {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100248#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600249 BC_INST_INC_PRE,
250 BC_INST_DEC_PRE,
251 BC_INST_INC_POST,
252 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100253#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600254
255 BC_INST_NEG,
256
257 BC_INST_POWER,
258 BC_INST_MULTIPLY,
259 BC_INST_DIVIDE,
260 BC_INST_MODULUS,
261 BC_INST_PLUS,
262 BC_INST_MINUS,
263
264 BC_INST_REL_EQ,
265 BC_INST_REL_LE,
266 BC_INST_REL_GE,
267 BC_INST_REL_NE,
268 BC_INST_REL_LT,
269 BC_INST_REL_GT,
270
271 BC_INST_BOOL_NOT,
272 BC_INST_BOOL_OR,
273 BC_INST_BOOL_AND,
274
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100275#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600276 BC_INST_ASSIGN_POWER,
277 BC_INST_ASSIGN_MULTIPLY,
278 BC_INST_ASSIGN_DIVIDE,
279 BC_INST_ASSIGN_MODULUS,
280 BC_INST_ASSIGN_PLUS,
281 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100282#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600283 BC_INST_ASSIGN,
284
285 BC_INST_NUM,
286 BC_INST_VAR,
287 BC_INST_ARRAY_ELEM,
288 BC_INST_ARRAY,
289
290 BC_INST_SCALE_FUNC,
291 BC_INST_IBASE,
292 BC_INST_SCALE,
293 BC_INST_LAST,
294 BC_INST_LENGTH,
295 BC_INST_READ,
296 BC_INST_OBASE,
297 BC_INST_SQRT,
298
299 BC_INST_PRINT,
300 BC_INST_PRINT_POP,
301 BC_INST_STR,
302 BC_INST_PRINT_STR,
303
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100304#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600305 BC_INST_JUMP,
306 BC_INST_JUMP_ZERO,
307
308 BC_INST_CALL,
309
310 BC_INST_RET,
311 BC_INST_RET0,
312
313 BC_INST_HALT,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100314#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600315
316 BC_INST_POP,
317 BC_INST_POP_EXEC,
318
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100319#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600320 BC_INST_MODEXP,
321 BC_INST_DIVMOD,
322
323 BC_INST_EXECUTE,
324 BC_INST_EXEC_COND,
325
326 BC_INST_ASCIIFY,
327 BC_INST_PRINT_STREAM,
328
329 BC_INST_PRINT_STACK,
330 BC_INST_CLEAR_STACK,
331 BC_INST_STACK_LEN,
332 BC_INST_DUPLICATE,
333 BC_INST_SWAP,
334
335 BC_INST_LOAD,
336 BC_INST_PUSH_VAR,
337 BC_INST_PUSH_TO_VAR,
338
339 BC_INST_QUIT,
340 BC_INST_NQUIT,
341
342 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100343#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600344} BcInst;
345
346typedef struct BcId {
347 char *name;
348 size_t idx;
349} BcId;
350
351typedef struct BcFunc {
352 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100353 IF_BC(BcVec labels;)
354 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100355 IF_BC(BcVec strs;)
356 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100357 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600358} BcFunc;
359
360typedef enum BcResultType {
Gavin Howard01055ba2018-11-03 11:00:21 -0600361 BC_RESULT_TEMP,
362
363 BC_RESULT_VAR,
364 BC_RESULT_ARRAY_ELEM,
365 BC_RESULT_ARRAY,
366
367 BC_RESULT_STR,
368
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100369 //code uses "inst - BC_INST_IBASE + BC_RESULT_IBASE" construct,
370 BC_RESULT_IBASE, // relative order should match for: BC_INST_IBASE
371 BC_RESULT_SCALE, // relative order should match for: BC_INST_SCALE
372 BC_RESULT_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600373 BC_RESULT_CONSTANT,
374 BC_RESULT_ONE,
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100375 BC_RESULT_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600376} BcResultType;
377
378typedef union BcResultData {
379 BcNum n;
380 BcVec v;
381 BcId id;
382} BcResultData;
383
384typedef struct BcResult {
385 BcResultType t;
386 BcResultData d;
387} BcResult;
388
389typedef struct BcInstPtr {
390 size_t func;
391 size_t idx;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100392 IF_BC(size_t len;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600393} BcInstPtr;
394
Gavin Howard01055ba2018-11-03 11:00:21 -0600395// BC_LEX_NEG is not used in lexing; it is only for parsing.
396typedef enum BcLexType {
Gavin Howard01055ba2018-11-03 11:00:21 -0600397 BC_LEX_EOF,
398 BC_LEX_INVALID,
399
400 BC_LEX_OP_INC,
401 BC_LEX_OP_DEC,
402
403 BC_LEX_NEG,
404
405 BC_LEX_OP_POWER,
406 BC_LEX_OP_MULTIPLY,
407 BC_LEX_OP_DIVIDE,
408 BC_LEX_OP_MODULUS,
409 BC_LEX_OP_PLUS,
410 BC_LEX_OP_MINUS,
411
412 BC_LEX_OP_REL_EQ,
413 BC_LEX_OP_REL_LE,
414 BC_LEX_OP_REL_GE,
415 BC_LEX_OP_REL_NE,
416 BC_LEX_OP_REL_LT,
417 BC_LEX_OP_REL_GT,
418
419 BC_LEX_OP_BOOL_NOT,
420 BC_LEX_OP_BOOL_OR,
421 BC_LEX_OP_BOOL_AND,
422
423 BC_LEX_OP_ASSIGN_POWER,
424 BC_LEX_OP_ASSIGN_MULTIPLY,
425 BC_LEX_OP_ASSIGN_DIVIDE,
426 BC_LEX_OP_ASSIGN_MODULUS,
427 BC_LEX_OP_ASSIGN_PLUS,
428 BC_LEX_OP_ASSIGN_MINUS,
429 BC_LEX_OP_ASSIGN,
430
431 BC_LEX_NLINE,
432 BC_LEX_WHITESPACE,
433
434 BC_LEX_LPAREN,
435 BC_LEX_RPAREN,
436
437 BC_LEX_LBRACKET,
438 BC_LEX_COMMA,
439 BC_LEX_RBRACKET,
440
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100441 BC_LEX_LBRACE, // '{' is 0x7B, '}' is 0x7D,
Gavin Howard01055ba2018-11-03 11:00:21 -0600442 BC_LEX_SCOLON,
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100443 BC_LEX_RBRACE, // should be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600444
445 BC_LEX_STR,
446 BC_LEX_NAME,
447 BC_LEX_NUMBER,
448
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100449 BC_LEX_KEY_1st_keyword,
450 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600451 BC_LEX_KEY_BREAK,
452 BC_LEX_KEY_CONTINUE,
453 BC_LEX_KEY_DEFINE,
454 BC_LEX_KEY_ELSE,
455 BC_LEX_KEY_FOR,
456 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100457 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
458 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600459 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100460 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600461 BC_LEX_KEY_LENGTH,
462 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100463 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600464 BC_LEX_KEY_PRINT,
465 BC_LEX_KEY_QUIT,
466 BC_LEX_KEY_READ,
467 BC_LEX_KEY_RETURN,
468 BC_LEX_KEY_SCALE,
469 BC_LEX_KEY_SQRT,
470 BC_LEX_KEY_WHILE,
471
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100472#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600473 BC_LEX_EQ_NO_REG,
474 BC_LEX_OP_MODEXP,
475 BC_LEX_OP_DIVMOD,
476
477 BC_LEX_COLON,
478 BC_LEX_ELSE,
479 BC_LEX_EXECUTE,
480 BC_LEX_PRINT_STACK,
481 BC_LEX_CLEAR_STACK,
482 BC_LEX_STACK_LEVEL,
483 BC_LEX_DUPLICATE,
484 BC_LEX_SWAP,
485 BC_LEX_POP,
486
487 BC_LEX_ASCIIFY,
488 BC_LEX_PRINT_STREAM,
489
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100490 // code uses "t - BC_LEX_STORE_IBASE + BC_INST_IBASE" construct,
491 BC_LEX_STORE_IBASE, // relative order should match for: BC_INST_IBASE
492 BC_LEX_STORE_SCALE, // relative order should match for: BC_INST_SCALE
Gavin Howard01055ba2018-11-03 11:00:21 -0600493 BC_LEX_LOAD,
494 BC_LEX_LOAD_POP,
495 BC_LEX_STORE_PUSH,
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100496 BC_LEX_STORE_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600497 BC_LEX_PRINT_POP,
498 BC_LEX_NQUIT,
499 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100500#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600501} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100502// must match order of BC_LEX_KEY_foo etc above
503#if ENABLE_BC
504struct BcLexKeyword {
505 char name8[8];
506};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100507#define BC_LEX_KW_ENTRY(a, b) \
508 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100509static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100510 BC_LEX_KW_ENTRY("auto" , 1), // 0
511 BC_LEX_KW_ENTRY("break" , 1), // 1
512 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
513 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100514 BC_LEX_KW_ENTRY("else" , 0), // 4
515 BC_LEX_KW_ENTRY("for" , 1), // 5
516 BC_LEX_KW_ENTRY("halt" , 0), // 6
517 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100518 BC_LEX_KW_ENTRY("if" , 1), // 8
519 BC_LEX_KW_ENTRY("last" , 0), // 9
520 BC_LEX_KW_ENTRY("length" , 1), // 10
521 BC_LEX_KW_ENTRY("limits" , 0), // 11
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100522 BC_LEX_KW_ENTRY("obase" , 1), // 12
523 BC_LEX_KW_ENTRY("print" , 0), // 13
524 BC_LEX_KW_ENTRY("quit" , 1), // 14
525 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100526 BC_LEX_KW_ENTRY("return" , 1), // 16
527 BC_LEX_KW_ENTRY("scale" , 1), // 17
528 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
529 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100530};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100531#undef BC_LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100532#define STRING_if (bc_lex_kws[8].name8)
533#define STRING_else (bc_lex_kws[4].name8)
534#define STRING_while (bc_lex_kws[19].name8)
535#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100536enum {
537 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100538 | (1 << 0) // 0
539 | (1 << 1) // 1
540 | (0 << 2) // 2
541 | (1 << 3) // 3
542 | (0 << 4) // 4
543 | (1 << 5) // 5
544 | (0 << 6) // 6
545 | (1 << 7) // 7
546 | (1 << 8) // 8
547 | (0 << 9) // 9
548 | (1 << 10) // 10
549 | (0 << 11) // 11
550 | (1 << 12) // 12
551 | (0 << 13) // 13
552 | (1 << 14) // 14
553 | (0 << 15) // 15
554 | (1 << 16) // 16
555 | (1 << 17) // 17
556 | (1 << 18) // 18
557 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100558};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100559#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100560
561// This is a bit array that corresponds to token types. An entry is
562// true if the token is valid in an expression, false otherwise.
563// Used to figure out when expr parsing should stop *without error message*
564// - 0 element indicates this condition. 1 means "this token is to be eaten
565// as part of the expression", token can them still be determined to be invalid
566// by later processing.
567enum {
568#define EXBITS(a,b,c,d,e,f,g,h) \
569 ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7)))
570 BC_PARSE_EXPRS_BITS = 0
571 + (EXBITS(0,0,1,1,1,1,1,1) << (0*8)) // 0: eof inval ++ -- - ^ * /
572 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: % + - == <= >= != <
573 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: > ! || && ^= *= /= %=
574 + (EXBITS(1,1,1,0,0,1,1,0) << (3*8)) // 24: += -= = NL WS ( ) [
575 + (EXBITS(0,0,0,0,0,0,1,1) << (4*8)) // 32: , ] { ; } STR NAME NUM
576 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
577 + (EXBITS(0,1,1,1,1,0,0,1) << (6*8)) // 48: if last len limits obase print quit read - bug, why "limits" is allowed?
578 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
579#undef EXBITS
580};
581static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
582{
583#if ULONG_MAX > 0xffffffff
584 // 64-bit version (will not work correctly for 32-bit longs!)
585 return BC_PARSE_EXPRS_BITS & (1UL << i);
586#else
587 // 32-bit version
588 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
589 if (i >= 32) {
590 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
591 i &= 31;
592 }
593 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100594#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100595}
596
597// This is an array of data for operators that correspond to
598// [BC_LEX_OP_INC...BC_LEX_OP_ASSIGN] token types.
599static const uint8_t bc_parse_ops[] = {
600#define OP(p,l) ((int)(l) * 0x10 + (p))
601 OP(0, false), OP( 0, false ), // inc dec
602 OP(1, false), // neg
603 OP(2, false), // pow
604 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
605 OP(4, true ), OP( 4, true ), // + -
606 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
607 OP(1, false), // not
608 OP(7, true ), OP( 7, true ), // or and
609 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
610 OP(5, false), OP( 5, false ), // -= =
611#undef OP
612};
613#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
614#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
615#endif // ENABLE_BC
616
617#if ENABLE_DC
618static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
619int8_t
620dc_parse_insts[] = {
621 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
622 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
623 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
624 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
625 BC_INST_INVALID, BC_INST_INVALID,
626 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
627 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
628 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
629 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
630 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
631 BC_INST_INVALID, BC_INST_INVALID,
632 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
633 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
634 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
635 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
636 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
637 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
638 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
639 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
640 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
641 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
642 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
643 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
644};
645#endif // ENABLE_DC
646
Gavin Howard01055ba2018-11-03 11:00:21 -0600647typedef struct BcLex {
Gavin Howard01055ba2018-11-03 11:00:21 -0600648 const char *buf;
649 size_t i;
650 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600651 size_t len;
652 bool newline;
Gavin Howard01055ba2018-11-03 11:00:21 -0600653 struct {
654 BcLexType t;
655 BcLexType last;
656 BcVec v;
657 } t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600658} BcLex;
659
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100660#define BC_PARSE_STREND (0xff)
Gavin Howard01055ba2018-11-03 11:00:21 -0600661
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100662#define BC_PARSE_REL (1 << 0)
663#define BC_PARSE_PRINT (1 << 1)
664#define BC_PARSE_NOCALL (1 << 2)
665#define BC_PARSE_NOREAD (1 << 3)
666#define BC_PARSE_ARRAY (1 << 4)
Gavin Howard01055ba2018-11-03 11:00:21 -0600667
Gavin Howard01055ba2018-11-03 11:00:21 -0600668typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600669 BcLex l;
670
Denys Vlasenko503faf92018-12-20 16:24:18 +0100671 IF_BC(BcVec exits;)
672 IF_BC(BcVec conds;)
673 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600674
Gavin Howard01055ba2018-11-03 11:00:21 -0600675 BcFunc *func;
676 size_t fidx;
677
Denys Vlasenko503faf92018-12-20 16:24:18 +0100678 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600679} BcParse;
680
Gavin Howard01055ba2018-11-03 11:00:21 -0600681typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600682 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100683 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600684
Denys Vlasenko503faf92018-12-20 16:24:18 +0100685 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600686 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600687 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600688
Gavin Howard01055ba2018-11-03 11:00:21 -0600689 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100690 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600691
692 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100693 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600694
695 BcVec vars;
696 BcVec var_map;
697
698 BcVec arrs;
699 BcVec arr_map;
700
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100701 IF_DC(BcVec strs;)
702 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600703
704 const char *file;
705
Gavin Howard01055ba2018-11-03 11:00:21 -0600706 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100707 IF_BC(BcNum one;)
708 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600709} BcProgram;
710
Gavin Howard01055ba2018-11-03 11:00:21 -0600711#define BC_PROG_MAIN (0)
712#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100713#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600714#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100715#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600716
717#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
718#define BC_PROG_NUM(r, n) \
719 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
720
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100721#define BC_FLAG_W (1 << 0)
722#define BC_FLAG_V (1 << 1)
723#define BC_FLAG_S (1 << 2)
724#define BC_FLAG_Q (1 << 3)
725#define BC_FLAG_L (1 << 4)
726#define BC_FLAG_I (1 << 5)
727#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600728
729#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
730#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
731
Denys Vlasenko64074a12018-12-07 15:50:14 +0100732#define BC_MAX_OBASE ((unsigned) 999)
733#define BC_MAX_DIM ((unsigned) INT_MAX)
734#define BC_MAX_SCALE ((unsigned) UINT_MAX)
735#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
736#define BC_MAX_NUM BC_MAX_STRING
737// Unused apart from "limits" message. Just show a "biggish number" there.
738//#define BC_MAX_NAME BC_MAX_STRING
739//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
740//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
741#define BC_MAX_NAME_STR "999999999"
742#define BC_MAX_EXP_STR "999999999"
743#define BC_MAX_VARS_STR "999999999"
744
745#define BC_MAX_OBASE_STR "999"
746
747#if INT_MAX == 2147483647
748# define BC_MAX_DIM_STR "2147483647"
749#elif INT_MAX == 9223372036854775807
750# define BC_MAX_DIM_STR "9223372036854775807"
751#else
752# error Strange INT_MAX
753#endif
754
755#if UINT_MAX == 4294967295
756# define BC_MAX_SCALE_STR "4294967295"
757# define BC_MAX_STRING_STR "4294967294"
758#elif UINT_MAX == 18446744073709551615
759# define BC_MAX_SCALE_STR "18446744073709551615"
760# define BC_MAX_STRING_STR "18446744073709551614"
761#else
762# error Strange UINT_MAX
763#endif
764#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600765
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100766struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100767 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100768 IF_FEATURE_CLEAN_UP(smallint exiting;)
Denys Vlasenko69171dc2018-12-12 00:29:24 +0100769 smallint in_read;
Gavin Howard01055ba2018-11-03 11:00:21 -0600770
771 BcParse prs;
772 BcProgram prog;
773
Denys Vlasenko5318f812018-12-05 17:48:01 +0100774 // For error messages. Can be set to current parsed line,
775 // or [TODO] to current executing line (can be before last parsed one)
776 unsigned err_line;
777
Gavin Howard01055ba2018-11-03 11:00:21 -0600778 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100779 BcVec input_buffer;
780 FILE *input_fp;
Gavin Howard01055ba2018-11-03 11:00:21 -0600781
782 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100783
784#if ENABLE_FEATURE_EDITING
785 line_input_t *line_input_state;
786#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100787} FIX_ALIASING;
788#define G (*ptr_to_globals)
789#define INIT_G() do { \
790 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
791} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100792#define FREE_G() do { \
793 FREE_PTR_TO_GLOBALS(); \
794} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100795#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
796#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100797#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100798#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100799# define G_interrupt bb_got_signal
800# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100801#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100802# define G_interrupt 0
803# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100804#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100805#if ENABLE_FEATURE_CLEAN_UP
806# define G_exiting G.exiting
807#else
808# define G_exiting 0
809#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100810#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100811#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100812
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100813// In configurations where errors abort instead of propagating error
814// return code up the call chain, functions returning BC_STATUS
815// actually don't return anything, they always succeed and return "void".
816// A macro wrapper is provided, which makes this statement work:
817// s = zbc_func(...)
818// and makes it visible to the compiler that s is always zero,
819// allowing compiler to optimize dead code after the statement.
820//
821// To make code more readable, each such function has a "z"
822// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
823//
824#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100825# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100826# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100827# define IF_ERROR_RETURN_POSSIBLE(a) a
828# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100829# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100830# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100831#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100832# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100833# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100834# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
835# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100836# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100837# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100838#endif
839
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +0100840#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
841#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
842
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100843#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
844#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
845#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
846//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
847static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
848{
849 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
850}
851//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
852static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
853{
854 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
855}
856
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100857typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
858
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100859typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100860
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100861static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
862 BcNumBinaryOp op, size_t req);
863static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
864static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
865static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
866static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
867static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
868static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
869
870static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
871{
872 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
873 (void) scale;
874 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
875}
876
877static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
878{
879 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
880 (void) scale;
881 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
882}
883
884static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
885{
886 size_t req = BC_NUM_MREQ(a, b, scale);
887 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
888}
889
890static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
891{
892 size_t req = BC_NUM_MREQ(a, b, scale);
893 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
894}
895
896static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
897{
898 size_t req = BC_NUM_MREQ(a, b, scale);
899 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
900}
901
902static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
903{
904 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
905}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100906
Denys Vlasenko1aeacef2018-12-11 19:12:13 +0100907static const BcNumBinaryOp zbc_program_ops[] = {
908 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
Gavin Howard01055ba2018-11-03 11:00:21 -0600909};
Denys Vlasenkoec603182018-12-17 10:34:02 +0100910#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
911#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
912#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
913#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
914#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
915#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -0600916
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100917static void fflush_and_check(void)
918{
919 fflush_all();
920 if (ferror(stdout) || ferror(stderr))
921 bb_perror_msg_and_die("output error");
922}
923
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100924#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100925#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100926do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100927 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100928 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100929 return BC_STATUS_FAILURE; \
930} while (0)
931#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100932#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100933#endif
934
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100935static void quit(void) NORETURN;
936static void quit(void)
937{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100938 if (ferror(stdin))
939 bb_perror_msg_and_die("input error");
940 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100941 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100942 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100943}
944
Denys Vlasenko5318f812018-12-05 17:48:01 +0100945static void bc_verror_msg(const char *fmt, va_list p)
946{
Denys Vlasenko82269122018-12-14 16:30:56 +0100947 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +0100948 if (G.prog.file) {
949 sv = applet_name;
950 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
951 }
952 bb_verror_msg(fmt, p, NULL);
953 if (G.prog.file) {
954 free((char*)applet_name);
955 applet_name = sv;
956 }
957}
958
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100959static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100960{
961 va_list p;
962
963 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100964 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100965 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100966
Denys Vlasenkoec603182018-12-17 10:34:02 +0100967 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
968 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
969 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100970}
971
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100972#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100973static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100974{
975 va_list p;
976
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100977 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100978 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100979 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100980
981 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100982 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100983 va_end(p);
984
985 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100986 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100987 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenkoec603182018-12-17 10:34:02 +0100988 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
989 return BC_STATUS_FAILURE;
990 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100991}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100992#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100993
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100994// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
995// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100996// function must not have caller-cleaned parameters on stack.
997// Unfortunately, vararg function API does exactly that on most arches.
998// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100999static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001000{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001001 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001002}
1003static ERRORFUNC int bc_error_bad_character(char c)
1004{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001005 if (!c)
1006 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +01001007 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001008}
1009static ERRORFUNC int bc_error_bad_expression(void)
1010{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001011 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001012}
1013static ERRORFUNC int bc_error_bad_token(void)
1014{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001015 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001016}
1017static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1018{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001019 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001020}
1021static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1022{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001023 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001024}
1025static ERRORFUNC int bc_error_nested_read_call(void)
1026{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001027 IF_ERROR_RETURN_POSSIBLE(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001028}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001029#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001030static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001031{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001032 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001033}
Denys Vlasenko00646792018-12-05 18:12:27 +01001034static int bc_POSIX_does_not_allow(const char *msg)
1035{
1036 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1037}
1038static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1039{
1040 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1041}
1042static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1043{
1044 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1045}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001046#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001047
Gavin Howard01055ba2018-11-03 11:00:21 -06001048static void bc_vec_grow(BcVec *v, size_t n)
1049{
1050 size_t cap = v->cap * 2;
1051 while (cap < v->len + n) cap *= 2;
1052 v->v = xrealloc(v->v, v->size * cap);
1053 v->cap = cap;
1054}
1055
1056static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1057{
1058 v->size = esize;
1059 v->cap = BC_VEC_START_CAP;
1060 v->len = 0;
1061 v->dtor = dtor;
1062 v->v = xmalloc(esize * BC_VEC_START_CAP);
1063}
1064
Denys Vlasenko7d628012018-12-04 21:46:47 +01001065static void bc_char_vec_init(BcVec *v)
1066{
1067 bc_vec_init(v, sizeof(char), NULL);
1068}
1069
Gavin Howard01055ba2018-11-03 11:00:21 -06001070static void bc_vec_expand(BcVec *v, size_t req)
1071{
1072 if (v->cap < req) {
1073 v->v = xrealloc(v->v, v->size * req);
1074 v->cap = req;
1075 }
1076}
1077
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001078static void bc_vec_pop(BcVec *v)
1079{
1080 v->len--;
1081 if (v->dtor)
1082 v->dtor(v->v + (v->size * v->len));
1083}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001084
Gavin Howard01055ba2018-11-03 11:00:21 -06001085static void bc_vec_npop(BcVec *v, size_t n)
1086{
1087 if (!v->dtor)
1088 v->len -= n;
1089 else {
1090 size_t len = v->len - n;
1091 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1092 }
1093}
1094
Denys Vlasenko7d628012018-12-04 21:46:47 +01001095static void bc_vec_pop_all(BcVec *v)
1096{
1097 bc_vec_npop(v, v->len);
1098}
1099
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001100//TODO: return index of pushed data - many callers can use that
Gavin Howard01055ba2018-11-03 11:00:21 -06001101static void bc_vec_push(BcVec *v, const void *data)
1102{
1103 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1104 memmove(v->v + (v->size * v->len), data, v->size);
1105 v->len += 1;
1106}
1107
1108static void bc_vec_pushByte(BcVec *v, char data)
1109{
1110 bc_vec_push(v, &data);
1111}
1112
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001113static void bc_vec_pushZeroByte(BcVec *v)
1114{
1115 //bc_vec_pushByte(v, '\0');
1116 // better:
1117 bc_vec_push(v, &const_int_0);
1118}
1119
Gavin Howard01055ba2018-11-03 11:00:21 -06001120static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1121{
1122 if (idx == v->len)
1123 bc_vec_push(v, data);
1124 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001125 char *ptr;
1126
1127 if (v->len == v->cap) bc_vec_grow(v, 1);
1128
1129 ptr = v->v + v->size * idx;
1130
1131 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1132 memmove(ptr, data, v->size);
1133 }
1134}
1135
1136static void bc_vec_string(BcVec *v, size_t len, const char *str)
1137{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001138 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001139 bc_vec_expand(v, len + 1);
1140 memcpy(v->v, str, len);
1141 v->len = len;
1142
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001143 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001144}
1145
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001146#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001147static void bc_vec_concat(BcVec *v, const char *str)
1148{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001149 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001150
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001151 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001152
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001153 slen = strlen(str);
1154 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001155
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001156 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001157 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001158
1159 v->len = len;
1160}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001161#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001162
1163static void *bc_vec_item(const BcVec *v, size_t idx)
1164{
1165 return v->v + v->size * idx;
1166}
1167
1168static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1169{
1170 return v->v + v->size * (v->len - idx - 1);
1171}
1172
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001173static void *bc_vec_top(const BcVec *v)
1174{
1175 return v->v + v->size * (v->len - 1);
1176}
1177
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001178static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001179{
1180 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001181 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001182 free(v->v);
1183}
1184
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001185static BcFunc* bc_program_func(size_t idx)
1186{
1187 return bc_vec_item(&G.prog.fns, idx);
1188}
1189// BC_PROG_MAIN is zeroth element, so:
1190#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1191
1192#if ENABLE_BC
1193static BcFunc* bc_program_current_func(void)
1194{
1195 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1196 BcFunc *func = bc_program_func(ip->func);
1197 return func;
1198}
1199#endif
1200
1201static char** bc_program_str(size_t idx)
1202{
1203#if ENABLE_BC
1204 if (IS_BC) {
1205 BcFunc *func = bc_program_current_func();
1206 return bc_vec_item(&func->strs, idx);
1207 }
1208#endif
1209 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1210}
1211
1212static char** bc_program_const(size_t idx)
1213{
1214#if ENABLE_BC
1215 if (IS_BC) {
1216 BcFunc *func = bc_program_current_func();
1217 return bc_vec_item(&func->consts, idx);
1218 }
1219#endif
1220 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1221}
1222
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001223static int bc_id_cmp(const void *e1, const void *e2)
1224{
1225 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1226}
1227
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001228static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001229{
1230 free(((BcId *) id)->name);
1231}
1232
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001233static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001234{
1235 size_t low = 0, high = v->len;
1236
1237 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001238 size_t mid = (low + high) / 2;
1239 BcId *id = bc_vec_item(v, mid);
1240 int result = bc_id_cmp(ptr, id);
1241
1242 if (result == 0)
1243 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001244 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001245 high = mid;
1246 else
1247 low = mid + 1;
1248 }
1249
1250 return low;
1251}
1252
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001253static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001254{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001255 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001256
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001257 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001258 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001259 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1260 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001261 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001262 bc_vec_pushAt(v, ptr, n);
1263 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001264}
1265
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001266#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001267static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001268{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001269 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001270 if (i >= v->len) return BC_VEC_INVALID_IDX;
1271 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1272}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001273#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001274
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001275static int bad_input_byte(char c)
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001276{
1277 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1278 || c > 0x7e
1279 ) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001280 bc_error_fmt("illegal character 0x%02x", c);
1281 return 1;
1282 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001283 return 0;
1284}
1285
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001286// Note: it _appends_ data from fp to vec.
1287static void bc_read_line(BcVec *vec, FILE *fp)
Gavin Howard01055ba2018-11-03 11:00:21 -06001288{
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001289 again:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001290 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001291
Gavin Howard01055ba2018-11-03 11:00:21 -06001292#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001293 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001294 intr:
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001295 if (fp != stdin) {
1296 // ^C while running a script (bc SCRIPT): die.
1297 // We do not return to interactive prompt:
1298 // user might be running us from a shell,
1299 // and SCRIPT might be intended to terminate
1300 // (e.g. contain a "halt" stmt).
1301 // ^C dropping user into a bc prompt instead of
1302 // the shell would be unexpected.
1303 xfunc_die();
1304 }
1305 // ^C while interactive input
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001306 G_interrupt = 0;
1307 // GNU bc says "interrupted execution."
1308 // GNU dc says "Interrupt!"
1309 fputs("\ninterrupted execution\n", stderr);
1310 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001311
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001312# if ENABLE_FEATURE_EDITING
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001313 if (G_ttyin && fp == stdin) {
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001314 int n, i;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001315# define line_buf bb_common_bufsiz1
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001316 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
1317 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1318 if (n == 0) // ^C
1319 goto intr;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001320 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
Denys Vlasenkoe755e302018-12-13 22:25:28 +01001321 return;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001322 }
1323 i = 0;
1324 for (;;) {
1325 char c = line_buf[i++];
1326 if (!c) break;
1327 if (bad_input_byte(c)) goto again;
1328 }
1329 bc_vec_concat(vec, line_buf);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001330# undef line_buf
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001331 } else
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001332# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001333#endif
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001334 {
1335 int c;
1336 bool bad_chars = 0;
1337 size_t len = vec->len;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001338
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001339 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001340#if ENABLE_FEATURE_BC_SIGNALS
1341 if (G_interrupt) {
1342 // ^C was pressed: ignore entire line, get another one
1343 vec->len = len;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001344 goto intr;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001345 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001346#endif
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001347 do c = fgetc(fp); while (c == '\0');
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001348 if (c == EOF) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001349 if (ferror(fp))
1350 bb_perror_msg_and_die("input error");
1351 // Note: EOF does not append '\n'
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001352 break;
1353 }
1354 bad_chars |= bad_input_byte(c);
1355 bc_vec_pushByte(vec, (char)c);
1356 } while (c != '\n');
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001357
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001358 if (bad_chars) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001359 // Bad chars on this line
1360 if (!G.prog.file) { // stdin
1361 // ignore entire line, get another one
1362 vec->len = len;
1363 goto again;
1364 }
1365 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
Denys Vlasenkoed849352018-12-06 10:26:13 +01001366 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001367 bc_vec_pushZeroByte(vec);
1368 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001369}
1370
Gavin Howard01055ba2018-11-03 11:00:21 -06001371static void bc_num_setToZero(BcNum *n, size_t scale)
1372{
1373 n->len = 0;
1374 n->neg = false;
1375 n->rdx = scale;
1376}
1377
1378static void bc_num_zero(BcNum *n)
1379{
1380 bc_num_setToZero(n, 0);
1381}
1382
1383static void bc_num_one(BcNum *n)
1384{
1385 bc_num_setToZero(n, 0);
1386 n->len = 1;
1387 n->num[0] = 1;
1388}
1389
Denys Vlasenko3129f702018-12-09 12:04:44 +01001390// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001391static void bc_num_init(BcNum *n, size_t req)
1392{
1393 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001394 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001395 n->num = xmalloc(req);
1396 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001397 n->rdx = 0;
1398 n->len = 0;
1399 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001400}
1401
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001402static void bc_num_init_DEF_SIZE(BcNum *n)
1403{
1404 bc_num_init(n, BC_NUM_DEF_SIZE);
1405}
1406
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001407static void bc_num_expand(BcNum *n, size_t req)
1408{
1409 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1410 if (req > n->cap) {
1411 n->num = xrealloc(n->num, req);
1412 n->cap = req;
1413 }
1414}
1415
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001416static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001417{
1418 free(((BcNum *) num)->num);
1419}
1420
1421static void bc_num_copy(BcNum *d, BcNum *s)
1422{
1423 if (d != s) {
1424 bc_num_expand(d, s->cap);
1425 d->len = s->len;
1426 d->neg = s->neg;
1427 d->rdx = s->rdx;
1428 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1429 }
1430}
1431
Denys Vlasenko29301232018-12-11 15:29:32 +01001432static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001433{
1434 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001435 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001436
Denys Vlasenko29301232018-12-11 15:29:32 +01001437 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001438
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001439 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001440 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001441
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001442 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001443 pow *= 10;
1444
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001445 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001446 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001447 prev = result;
1448 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001449 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001450 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001451
Denys Vlasenko29301232018-12-11 15:29:32 +01001452 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001453}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001454#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001455
1456static void bc_num_ulong2num(BcNum *n, unsigned long val)
1457{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001458 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001459
1460 bc_num_zero(n);
1461
1462 if (val == 0) return;
1463
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001464 if (ULONG_MAX == 0xffffffffUL)
1465 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001466 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001467 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001468 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001469
1470 ptr = n->num;
1471 for (;;) {
1472 n->len++;
1473 *ptr++ = val % 10;
1474 val /= 10;
1475 if (val == 0) break;
1476 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001477}
1478
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001479static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001480{
1481 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001482 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001483 a[i] -= b[i];
1484 for (j = i; a[j] < 0;) {
1485 a[j++] += 10;
1486 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001487 }
1488 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001489}
1490
1491static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1492{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001493 size_t i = len;
1494 for (;;) {
1495 int c;
1496 if (i == 0)
1497 return 0;
1498 i--;
1499 c = a[i] - b[i];
1500 if (c != 0) {
1501 i++;
1502 if (c < 0)
1503 return -i;
1504 return i;
1505 }
1506 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001507}
1508
1509static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1510{
1511 size_t i, min, a_int, b_int, diff;
1512 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001513 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001514 ssize_t cmp;
1515
1516 if (a == b) return 0;
1517 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1518 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001519
1520 if (a->neg != b->neg) // signs of a and b differ
1521 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1522 return (int)b->neg - (int)a->neg;
1523 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001524
1525 a_int = BC_NUM_INT(a);
1526 b_int = BC_NUM_INT(b);
1527 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001528
1529 if (a_int != 0) return (ssize_t) a_int;
1530
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001531 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001532 if (a_max) {
1533 min = b->rdx;
1534 diff = a->rdx - b->rdx;
1535 max_num = a->num + diff;
1536 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001537 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1538 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001539 min = a->rdx;
1540 diff = b->rdx - a->rdx;
1541 max_num = b->num + diff;
1542 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001543 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001544 }
1545
1546 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001547 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001548
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001549 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001550 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001551 }
1552
1553 return 0;
1554}
1555
1556static void bc_num_truncate(BcNum *n, size_t places)
1557{
1558 if (places == 0) return;
1559
1560 n->rdx -= places;
1561
1562 if (n->len != 0) {
1563 n->len -= places;
1564 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1565 }
1566}
1567
1568static void bc_num_extend(BcNum *n, size_t places)
1569{
1570 size_t len = n->len + places;
1571
1572 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001573 if (n->cap < len) bc_num_expand(n, len);
1574
1575 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1576 memset(n->num, 0, sizeof(BcDig) * places);
1577
1578 n->len += places;
1579 n->rdx += places;
1580 }
1581}
1582
1583static void bc_num_clean(BcNum *n)
1584{
1585 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1586 if (n->len == 0)
1587 n->neg = false;
1588 else if (n->len < n->rdx)
1589 n->len = n->rdx;
1590}
1591
1592static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1593{
1594 if (n->rdx < scale)
1595 bc_num_extend(n, scale - n->rdx);
1596 else
1597 bc_num_truncate(n, n->rdx - scale);
1598
1599 bc_num_clean(n);
1600 if (n->len != 0) n->neg = !neg1 != !neg2;
1601}
1602
1603static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1604 BcNum *restrict b)
1605{
1606 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001607 b->len = n->len - idx;
1608 a->len = idx;
1609 a->rdx = b->rdx = 0;
1610
1611 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1612 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001613 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001614 bc_num_zero(b);
1615 bc_num_copy(a, n);
1616 }
1617
1618 bc_num_clean(a);
1619 bc_num_clean(b);
1620}
1621
Denys Vlasenko29301232018-12-11 15:29:32 +01001622static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001623{
Denys Vlasenko29301232018-12-11 15:29:32 +01001624 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001625
1626 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1627 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1628 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001629 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001630 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001631
1632 if (n->rdx >= places)
1633 n->rdx -= places;
1634 else {
1635 bc_num_extend(n, places - n->rdx);
1636 n->rdx = 0;
1637 }
1638
1639 bc_num_clean(n);
1640
Denys Vlasenko29301232018-12-11 15:29:32 +01001641 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001642}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001643#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001644
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001645static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001646{
Denys Vlasenko503faf92018-12-20 16:24:18 +01001647//TODO: nice example of non-allocated BcNum, use in other places as well!
Gavin Howard01055ba2018-11-03 11:00:21 -06001648 BcNum one;
1649 BcDig num[2];
1650
1651 one.cap = 2;
1652 one.num = num;
1653 bc_num_one(&one);
1654
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001655 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001656}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001657#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001658
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001659static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001660{
1661 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1662 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001663 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001664
1665 // Because this function doesn't need to use scale (per the bc spec),
1666 // I am hijacking it to say whether it's doing an add or a subtract.
1667
1668 if (a->len == 0) {
1669 bc_num_copy(c, b);
1670 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001671 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001672 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001673 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001674 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001675 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001676 }
1677
1678 c->neg = a->neg;
1679 c->rdx = BC_MAX(a->rdx, b->rdx);
1680 min_rdx = BC_MIN(a->rdx, b->rdx);
1681 c->len = 0;
1682
1683 if (a->rdx > b->rdx) {
1684 diff = a->rdx - b->rdx;
1685 ptr = a->num;
1686 ptr_a = a->num + diff;
1687 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001688 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001689 diff = b->rdx - a->rdx;
1690 ptr = b->num;
1691 ptr_a = a->num;
1692 ptr_b = b->num + diff;
1693 }
1694
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001695 ptr_c = c->num;
1696 for (i = 0; i < diff; ++i, ++c->len)
1697 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001698
1699 ptr_c += diff;
1700 a_int = BC_NUM_INT(a);
1701 b_int = BC_NUM_INT(b);
1702
1703 if (a_int > b_int) {
1704 min_int = b_int;
1705 max = a_int;
1706 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001707 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001708 min_int = a_int;
1709 max = b_int;
1710 ptr = ptr_b;
1711 }
1712
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001713 carry = 0;
1714 for (i = 0; i < min_rdx + min_int; ++i) {
1715 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001716 carry = in / 10;
1717 ptr_c[i] = (BcDig)(in % 10);
1718 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001719 for (; i < max + min_rdx; ++i) {
1720 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001721 carry = in / 10;
1722 ptr_c[i] = (BcDig)(in % 10);
1723 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001724 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001725
1726 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1727
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001728 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001729}
1730
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001731static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001732{
Gavin Howard01055ba2018-11-03 11:00:21 -06001733 ssize_t cmp;
1734 BcNum *minuend, *subtrahend;
1735 size_t start;
1736 bool aneg, bneg, neg;
1737
1738 // Because this function doesn't need to use scale (per the bc spec),
1739 // I am hijacking it to say whether it's doing an add or a subtract.
1740
1741 if (a->len == 0) {
1742 bc_num_copy(c, b);
1743 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001744 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001745 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001746 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001747 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001748 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001749 }
1750
1751 aneg = a->neg;
1752 bneg = b->neg;
1753 a->neg = b->neg = false;
1754
1755 cmp = bc_num_cmp(a, b);
1756
1757 a->neg = aneg;
1758 b->neg = bneg;
1759
1760 if (cmp == 0) {
1761 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001763 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001764 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001765 neg = a->neg;
1766 minuend = a;
1767 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001768 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001769 neg = b->neg;
1770 if (sub) neg = !neg;
1771 minuend = b;
1772 subtrahend = a;
1773 }
1774
1775 bc_num_copy(c, minuend);
1776 c->neg = neg;
1777
1778 if (c->rdx < subtrahend->rdx) {
1779 bc_num_extend(c, subtrahend->rdx - c->rdx);
1780 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001781 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001782 start = c->rdx - subtrahend->rdx;
1783
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001784 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001785
1786 bc_num_clean(c);
1787
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001788 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001789}
1790
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001791static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001792 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001793#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001794{
1795 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001796 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001797 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001798 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001799
Gavin Howard01055ba2018-11-03 11:00:21 -06001800 if (a->len == 0 || b->len == 0) {
1801 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001802 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001803 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001804 aone = BC_NUM_ONE(a);
1805 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001806 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001807 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001808 }
1809
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001810 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1811 || a->len < BC_NUM_KARATSUBA_LEN
1812 || b->len < BC_NUM_KARATSUBA_LEN
1813 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001814 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001815
Gavin Howard01055ba2018-11-03 11:00:21 -06001816 bc_num_expand(c, a->len + b->len + 1);
1817
1818 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001819 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001820
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001821 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001822 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001823 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001824 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001825 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001826 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001827 carry = in / 10;
1828 c->num[i + j] = (BcDig)(in % 10);
1829 }
1830
1831 c->num[i + j] += (BcDig) carry;
1832 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001833
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001834#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001835 // a=2^1000000
1836 // a*a <- without check below, this will not be interruptible
1837 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001838#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001839 }
1840
1841 c->len = len;
1842
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001843 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001844 }
1845
1846 bc_num_init(&l1, max);
1847 bc_num_init(&h1, max);
1848 bc_num_init(&l2, max);
1849 bc_num_init(&h2, max);
1850 bc_num_init(&m1, max);
1851 bc_num_init(&m2, max);
1852 bc_num_init(&z0, max);
1853 bc_num_init(&z1, max);
1854 bc_num_init(&z2, max);
1855 bc_num_init(&temp, max + max);
1856
1857 bc_num_split(a, max2, &l1, &h1);
1858 bc_num_split(b, max2, &l2, &h2);
1859
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001860 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001861 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001862 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001863 if (s) goto err;
1864
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001865 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001866 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001867 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001868 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001869 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001870 if (s) goto err;
1871
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001872 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001873 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001874 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001875 if (s) goto err;
1876
Denys Vlasenko29301232018-12-11 15:29:32 +01001877 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001878 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001879 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001880 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001881 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001882 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001883 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001884 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001885 bc_num_free(&temp);
1886 bc_num_free(&z2);
1887 bc_num_free(&z1);
1888 bc_num_free(&z0);
1889 bc_num_free(&m2);
1890 bc_num_free(&m1);
1891 bc_num_free(&h2);
1892 bc_num_free(&l2);
1893 bc_num_free(&h1);
1894 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001895 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001896}
1897
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001898static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001899{
1900 BcStatus s;
1901 BcNum cpa, cpb;
1902 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1903
1904 scale = BC_MAX(scale, a->rdx);
1905 scale = BC_MAX(scale, b->rdx);
1906 scale = BC_MIN(a->rdx + b->rdx, scale);
1907 maxrdx = BC_MAX(maxrdx, scale);
1908
1909 bc_num_init(&cpa, a->len);
1910 bc_num_init(&cpb, b->len);
1911
1912 bc_num_copy(&cpa, a);
1913 bc_num_copy(&cpb, b);
1914 cpa.neg = cpb.neg = false;
1915
Denys Vlasenko29301232018-12-11 15:29:32 +01001916 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001917 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001918 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001919 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001920 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001921 if (s) goto err;
1922
1923 maxrdx += scale;
1924 bc_num_expand(c, c->len + maxrdx);
1925
1926 if (c->len < maxrdx) {
1927 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1928 c->len += maxrdx;
1929 }
1930
1931 c->rdx = maxrdx;
1932 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001933 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001934 bc_num_free(&cpb);
1935 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001936 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001937}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001938#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001939
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001940static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001941{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001942 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001943 size_t len, end, i;
1944 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001945
1946 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001947 RETURN_STATUS(bc_error("divide by zero"));
1948 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001949 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001950 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001951 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001952 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001953 bc_num_copy(c, a);
1954 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001955 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001956 }
1957
1958 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1959 bc_num_copy(&cp, a);
1960 len = b->len;
1961
1962 if (len > cp.len) {
1963 bc_num_expand(&cp, len + 2);
1964 bc_num_extend(&cp, len - cp.len);
1965 }
1966
1967 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1968 cp.rdx -= b->rdx;
1969 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1970
1971 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001972 for (;;) {
1973 if (len == 0) break;
1974 len--;
1975 if (b->num[len] != 0)
1976 break;
1977 }
1978 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001979 }
1980
1981 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1982
1983 // We want an extra zero in front to make things simpler.
1984 cp.num[cp.len++] = 0;
1985 end = cp.len - len;
1986
1987 bc_num_expand(c, cp.len);
1988
1989 bc_num_zero(c);
1990 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
1991 c->rdx = cp.rdx;
1992 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001993
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001994 s = BC_STATUS_SUCCESS;
1995 for (i = end - 1; i < end; --i) {
1996 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06001997 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001998 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
1999 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002000 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002001#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002002 // a=2^100000
2003 // scale=40000
2004 // 1/a <- without check below, this will not be interruptible
2005 if (G_interrupt) {
2006 s = BC_STATUS_FAILURE;
2007 break;
2008 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002009#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002010 }
2011
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002012 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002013 bc_num_free(&cp);
2014
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002015 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002016}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002017#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002018
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002019static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002020 BcNum *restrict d, size_t scale, size_t ts)
2021{
2022 BcStatus s;
2023 BcNum temp;
2024 bool neg;
2025
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002026 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002027 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002028
2029 if (a->len == 0) {
2030 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002031 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002032 }
2033
2034 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002035 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002036 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002037
2038 if (scale != 0) scale = ts;
2039
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002040 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002041 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002042 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002043 if (s) goto err;
2044
2045 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2046
2047 neg = d->neg;
2048 bc_num_retireMul(d, ts, a->neg, b->neg);
2049 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002050 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002051 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002052 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002053}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002054#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002055
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002056static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002057{
2058 BcStatus s;
2059 BcNum c1;
2060 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2061
2062 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002063 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002064 bc_num_free(&c1);
2065
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002066 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002067}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002068#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002069
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002070static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002071{
2072 BcStatus s = BC_STATUS_SUCCESS;
2073 BcNum copy;
2074 unsigned long pow;
2075 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002076 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002077
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002078 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002079
2080 if (b->len == 0) {
2081 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002082 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002083 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002084 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002085 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002086 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002087 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002088 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002089 if (!b->neg)
2090 bc_num_copy(c, a);
2091 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002092 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002093 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002094 }
2095
2096 neg = b->neg;
2097 b->neg = false;
2098
Denys Vlasenko29301232018-12-11 15:29:32 +01002099 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002100 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002101
2102 bc_num_init(&copy, a->len);
2103 bc_num_copy(&copy, a);
2104
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002105 if (!neg) {
2106 if (a->rdx > scale)
2107 scale = a->rdx;
2108 if (a->rdx * pow < scale)
2109 scale = a->rdx * pow;
2110 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002111
2112 b->neg = neg;
2113
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002114 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002115 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002116 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002118 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002119 //if (G_interrupt) {
2120 // s = BC_STATUS_FAILURE;
2121 // goto err;
2122 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002123 }
2124
Gavin Howard01055ba2018-11-03 11:00:21 -06002125 bc_num_copy(c, &copy);
2126
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002127 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002128 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002129 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002130 if (s) goto err;
2131
2132 if (pow & 1) {
2133 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002134 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002135 if (s) goto err;
2136 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002137 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002138 //if (G_interrupt) {
2139 // s = BC_STATUS_FAILURE;
2140 // goto err;
2141 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002142 }
2143
2144 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002145 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002146 if (s) goto err;
2147 }
2148
Gavin Howard01055ba2018-11-03 11:00:21 -06002149 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2150
2151 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002152 for (i = 0; i < c->len; ++i)
2153 if (c->num[i] != 0)
2154 goto skip;
2155 bc_num_setToZero(c, scale);
2156 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002157
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002158 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002159 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002160 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002161}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002162#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002163
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002164static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002165 BcNumBinaryOp op, size_t req)
2166{
2167 BcStatus s;
2168 BcNum num2, *ptr_a, *ptr_b;
2169 bool init = false;
2170
2171 if (c == a) {
2172 ptr_a = &num2;
2173 memcpy(ptr_a, c, sizeof(BcNum));
2174 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002175 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06002176 ptr_a = a;
2177
2178 if (c == b) {
2179 ptr_b = &num2;
2180 if (c != a) {
2181 memcpy(ptr_b, c, sizeof(BcNum));
2182 init = true;
2183 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002184 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06002185 ptr_b = b;
2186
2187 if (init)
2188 bc_num_init(c, req);
2189 else
2190 bc_num_expand(c, req);
2191
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002192 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01002193 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002194
2195 if (init) bc_num_free(&num2);
2196
Denys Vlasenko29301232018-12-11 15:29:32 +01002197 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002198}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002199#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002200
Denys Vlasenko9311e012018-12-10 11:54:18 +01002201static bool bc_num_strValid(const char *val, size_t base)
2202{
2203 BcDig b;
2204 bool radix;
2205
2206 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2207 radix = false;
2208 for (;;) {
2209 BcDig c = *val++;
2210 if (c == '\0')
2211 break;
2212 if (c == '.') {
2213 if (radix) return false;
2214 radix = true;
2215 continue;
2216 }
2217 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2218 return false;
2219 }
2220 return true;
2221}
2222
2223// Note: n is already "bc_num_zero()"ed,
2224// leading zeroes in "val" are removed
2225static void bc_num_parseDecimal(BcNum *n, const char *val)
2226{
2227 size_t len, i;
2228 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002229
2230 len = strlen(val);
2231 if (len == 0)
2232 return;
2233
Denys Vlasenko9311e012018-12-10 11:54:18 +01002234 bc_num_expand(n, len);
2235
2236 ptr = strchr(val, '.');
2237
2238 n->rdx = 0;
2239 if (ptr != NULL)
2240 n->rdx = (size_t)((val + len) - (ptr + 1));
2241
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002242 for (i = 0; val[i]; ++i) {
2243 if (val[i] != '0' && val[i] != '.') {
2244 // Not entirely zero value - convert it, and exit
2245 i = len - 1;
2246 for (;;) {
2247 n->num[n->len] = val[i] - '0';
2248 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002249 skip_dot:
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002250 if (i == 0) break;
2251 if (val[--i] == '.') goto skip_dot;
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002252 }
2253 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002254 }
2255 }
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002256 // if for() exits without hitting if(), the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002257}
2258
2259// Note: n is already "bc_num_zero()"ed,
2260// leading zeroes in "val" are removed
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002261static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko9311e012018-12-10 11:54:18 +01002262{
2263 BcStatus s;
2264 BcNum temp, mult, result;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002265 BcNum base;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002266 BcDig c = '\0';
2267 unsigned long v;
2268 size_t i, digits;
2269
2270 for (i = 0; ; ++i) {
2271 if (val[i] == '\0')
2272 return;
2273 if (val[i] != '.' && val[i] != '0')
2274 break;
2275 }
2276
2277 bc_num_init_DEF_SIZE(&temp);
2278 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenkod3401432018-12-18 17:00:35 +01002279//TODO: have BcNumSmall type, with static buffer
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002280 bc_num_init_DEF_SIZE(&base);
2281 bc_num_ulong2num(&base, base_t);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002282
2283 for (;;) {
2284 c = *val++;
2285 if (c == '\0') goto int_err;
2286 if (c == '.') break;
2287
2288 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2289
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002290 s = zbc_num_mul(n, &base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002291 if (s) goto int_err;
2292 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002293 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002294 if (s) goto int_err;
2295 }
2296
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002297 bc_num_init(&result, base.len);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002298 //bc_num_zero(&result); - already is
2299 bc_num_one(&mult);
2300
2301 digits = 0;
2302 for (;;) {
2303 c = *val++;
2304 if (c == '\0') break;
2305 digits++;
2306
2307 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2308
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002309 s = zbc_num_mul(&result, &base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002310 if (s) goto err;
2311 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002312 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002313 if (s) goto err;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002314 s = zbc_num_mul(&mult, &base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002315 if (s) goto err;
2316 }
2317
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002318 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002319 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002320 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002321 if (s) goto err;
2322
2323 if (n->len != 0) {
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002324 if (n->rdx < digits)
2325 bc_num_extend(n, digits - n->rdx);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002326 } else
2327 bc_num_zero(n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002328 err:
Denys Vlasenko9311e012018-12-10 11:54:18 +01002329 bc_num_free(&result);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002330 int_err:
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002331 bc_num_free(&base);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002332 bc_num_free(&mult);
2333 bc_num_free(&temp);
2334}
2335
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002336static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
Gavin Howard01055ba2018-11-03 11:00:21 -06002337{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002338 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002339 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002340
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002341 bc_num_zero(n);
2342 while (*val == '0') val++;
2343
Gavin Howard01055ba2018-11-03 11:00:21 -06002344 if (base_t == 10)
2345 bc_num_parseDecimal(n, val);
2346 else
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002347 bc_num_parseBase(n, val, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002348
Denys Vlasenko29301232018-12-11 15:29:32 +01002349 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002350}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002351#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002352
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002353static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002354{
2355 BcStatus s;
2356 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2357 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2358 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2359
2360 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2361 bc_num_expand(b, req);
2362
2363 if (a->len == 0) {
2364 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002365 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002366 }
2367 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002368 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002369 }
2370 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002371 bc_num_one(b);
2372 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002373 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002374 }
2375
2376 scale = BC_MAX(scale, a->rdx) + 1;
2377 len = a->len + scale;
2378
2379 bc_num_init(&num1, len);
2380 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002381 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002382
2383 bc_num_one(&half);
2384 half.num[0] = 5;
2385 half.rdx = 1;
2386
2387 bc_num_init(&f, len);
2388 bc_num_init(&fprime, len);
2389
2390 x0 = &num1;
2391 x1 = &num2;
2392
2393 bc_num_one(x0);
2394 pow = BC_NUM_INT(a);
2395
2396 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002397 if (pow & 1)
2398 x0->num[0] = 2;
2399 else
2400 x0->num[0] = 6;
2401
2402 pow -= 2 - (pow & 1);
2403
2404 bc_num_extend(x0, pow);
2405
2406 // Make sure to move the radix back.
2407 x0->rdx -= pow;
2408 }
2409
2410 x0->rdx = digs = digs1 = 0;
2411 resrdx = scale + 2;
2412 len = BC_NUM_INT(x0) + resrdx - 1;
2413
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002414 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002415 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002416 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002417 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002418 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002419 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002420 if (s) goto err;
2421
2422 cmp = bc_num_cmp(x1, x0);
2423 digs = x1->len - (unsigned long long) llabs(cmp);
2424
2425 if (cmp == cmp2 && digs == digs1)
2426 times += 1;
2427 else
2428 times = 0;
2429
2430 resrdx += times > 4;
2431
2432 cmp2 = cmp1;
2433 cmp1 = cmp;
2434 digs1 = digs;
2435
2436 temp = x0;
2437 x0 = x1;
2438 x1 = temp;
2439 }
2440
Gavin Howard01055ba2018-11-03 11:00:21 -06002441 bc_num_copy(b, x0);
2442 scale -= 1;
2443 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002444 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002445 bc_num_free(&fprime);
2446 bc_num_free(&f);
2447 bc_num_free(&half);
2448 bc_num_free(&num2);
2449 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002450 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002451}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002452#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002453
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002454static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002455 size_t scale)
2456{
2457 BcStatus s;
2458 BcNum num2, *ptr_a;
2459 bool init = false;
2460 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2461
2462 if (c == a) {
2463 memcpy(&num2, c, sizeof(BcNum));
2464 ptr_a = &num2;
2465 bc_num_init(c, len);
2466 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002467 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002468 ptr_a = a;
2469 bc_num_expand(c, len);
2470 }
2471
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002472 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002473
2474 if (init) bc_num_free(&num2);
2475
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002476 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002477}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002478#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002479
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002480#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002481static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002482{
2483 BcStatus s;
2484 BcNum base, exp, two, temp;
2485
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002486 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002487 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002488 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002489 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002490 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002491 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002492
2493 bc_num_expand(d, c->len);
2494 bc_num_init(&base, c->len);
2495 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002496 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002497 bc_num_init(&temp, b->len);
2498
2499 bc_num_one(&two);
2500 two.num[0] = 2;
2501 bc_num_one(d);
2502
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002503 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002504 if (s) goto err;
2505 bc_num_copy(&exp, b);
2506
2507 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002508 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002509 if (s) goto err;
2510
2511 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002512 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002513 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002514 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002515 if (s) goto err;
2516 }
2517
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002518 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002519 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002520 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002521 if (s) goto err;
2522 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002523 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002524 bc_num_free(&temp);
2525 bc_num_free(&two);
2526 bc_num_free(&exp);
2527 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002528 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002529}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002530#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002531#endif // ENABLE_DC
2532
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002533#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002534static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002535{
Denys Vlasenko87888ce2018-12-19 17:55:23 +01002536 BcId *autoid;
Gavin Howard01055ba2018-11-03 11:00:21 -06002537 BcId a;
2538 size_t i;
2539
Denys Vlasenko87888ce2018-12-19 17:55:23 +01002540 autoid = (void*)f->autos.v;
2541 for (i = 0; i < f->autos.len; i++, autoid++) {
2542 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002543 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002544 }
2545
2546 a.idx = var;
2547 a.name = name;
2548
2549 bc_vec_push(&f->autos, &a);
2550
Denys Vlasenko29301232018-12-11 15:29:32 +01002551 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002552}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002553#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002554#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002555
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002556static FAST_FUNC void bc_string_free(void *string)
2557{
2558 free(*(char**)string);
2559}
2560
Gavin Howard01055ba2018-11-03 11:00:21 -06002561static void bc_func_init(BcFunc *f)
2562{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002563 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002564 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2565 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002566 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2567 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002568 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002569}
2570
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002571static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002572{
2573 BcFunc *f = (BcFunc *) func;
2574 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002575 IF_BC(bc_vec_free(&f->labels);)
2576 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002577 IF_BC(bc_vec_free(&f->strs);)
2578 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002579}
2580
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002581static void bc_array_expand(BcVec *a, size_t len);
2582
Gavin Howard01055ba2018-11-03 11:00:21 -06002583static void bc_array_init(BcVec *a, bool nums)
2584{
2585 if (nums)
2586 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2587 else
2588 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2589 bc_array_expand(a, 1);
2590}
2591
Gavin Howard01055ba2018-11-03 11:00:21 -06002592static void bc_array_expand(BcVec *a, size_t len)
2593{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002594 if (a->dtor == bc_num_free
2595 // && a->size == sizeof(BcNum) - always true
2596 ) {
2597 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002598 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002599 bc_num_init_DEF_SIZE(&n);
2600 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002601 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002602 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002603 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002604 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002605 bc_array_init(&v, true);
2606 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002607 }
2608 }
2609}
2610
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002611static void bc_array_copy(BcVec *d, const BcVec *s)
2612{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002613 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002614 size_t i;
2615
2616 bc_vec_pop_all(d);
2617 bc_vec_expand(d, s->cap);
2618 d->len = s->len;
2619
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002620 dnum = (void*)d->v;
2621 snum = (void*)s->v;
2622 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002623 bc_num_init(dnum, snum->len);
2624 bc_num_copy(dnum, snum);
2625 }
2626}
2627
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002628#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002629static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002630{
2631 d->t = src->t;
2632
2633 switch (d->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002634 case BC_RESULT_TEMP:
2635 case BC_RESULT_IBASE:
2636 case BC_RESULT_SCALE:
2637 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002638 bc_num_init(&d->d.n, src->d.n.len);
2639 bc_num_copy(&d->d.n, &src->d.n);
2640 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002641 case BC_RESULT_VAR:
2642 case BC_RESULT_ARRAY:
2643 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002644 d->d.id.name = xstrdup(src->d.id.name);
2645 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002646 case BC_RESULT_CONSTANT:
2647 case BC_RESULT_LAST:
2648 case BC_RESULT_ONE:
2649 case BC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002650 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2651 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002652 }
2653}
2654#endif // ENABLE_DC
2655
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002656static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002657{
2658 BcResult *r = (BcResult *) result;
2659
2660 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002661 case BC_RESULT_TEMP:
2662 case BC_RESULT_IBASE:
2663 case BC_RESULT_SCALE:
2664 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002665 bc_num_free(&r->d.n);
2666 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002667 case BC_RESULT_VAR:
2668 case BC_RESULT_ARRAY:
2669 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002670 free(r->d.id.name);
2671 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002672 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002673 // Do nothing.
2674 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002675 }
2676}
2677
2678static void bc_lex_lineComment(BcLex *l)
2679{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002680 // Try: echo -n '#foo' | bc
2681 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002682 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002683 i = l->i;
2684 while (i < l->len && l->buf[i] != '\n')
2685 i++;
2686 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002687}
2688
2689static void bc_lex_whitespace(BcLex *l)
2690{
Gavin Howard01055ba2018-11-03 11:00:21 -06002691 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002692 for (;;) {
2693 char c = l->buf[l->i];
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002694 if (c == '\n') // this is BC_LEX_NLINE, not BC_LEX_WHITESPACE
2695 break;
2696 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002697 break;
2698 l->i++;
2699 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002700}
2701
Denys Vlasenko29301232018-12-11 15:29:32 +01002702static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002703{
2704 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002705 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002706 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002707
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002708 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002709 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002710 ccnt = i = 0;
2711 for (;;) {
2712 char c = buf[i];
2713 if (c == '\0')
2714 break;
2715 if (c == '\\' && buf[i + 1] == '\n') {
2716 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002717 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002718 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002719 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002720 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2721 if (c != '.') break;
2722 // if '.' was already seen, stop on second one:
2723 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002724 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002725 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002726 // buf[i] is one of "0-9A-F."
2727 i++;
2728 if (c != '.')
2729 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002730 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002731 //ccnt is the number of chars in the number string, excluding possible
2732 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2733 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002734 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002735
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002736 // This might overestimate the size, if there are "\<NL>"'s
2737 // in the number. Subtracting number_of_backslashes*2 correctly
2738 // is not that easy: consider that in the case of "NNN.\<NL>"
2739 // loop above will count "\<NL>" before it realizes it is not
2740 // in fact *inside* the number:
2741 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002742
Denys Vlasenko64074a12018-12-07 15:50:14 +01002743 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2744 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2745 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002746 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002747 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002748
Denys Vlasenko7d628012018-12-04 21:46:47 +01002749 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002750 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002751 bc_vec_push(&l->t.v, &start);
2752
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002753 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002754 // If we have hit a backslash, skip it. We don't have
2755 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002756 if (*buf == '\\') {
2757 buf += 2;
2758 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002759 continue;
2760 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002761 bc_vec_push(&l->t.v, buf);
2762 buf++;
2763 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002764 }
2765
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002766 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002767
Denys Vlasenko29301232018-12-11 15:29:32 +01002768 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002769}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002770#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002771
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002772static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002773{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002774 size_t i;
2775 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002776
2777 l->t.t = BC_LEX_NAME;
2778
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002779 i = 0;
2780 buf = l->buf + l->i - 1;
2781 for (;;) {
2782 char c = buf[i];
2783 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2784 i++;
2785 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002786
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002787#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002788 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2789 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2790 if (i > BC_MAX_STRING)
2791 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2792 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002793#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002794 bc_vec_string(&l->t.v, i, buf);
2795
2796 // Increment the index. We minus 1 because it has already been incremented.
2797 l->i += i - 1;
2798
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002799 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002800}
2801
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002802static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002803{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002804 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002805}
2806
2807static void bc_lex_free(BcLex *l)
2808{
2809 bc_vec_free(&l->t.v);
2810}
2811
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002812static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002813{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002814 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002815 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002816}
2817
Denys Vlasenkoe755e302018-12-13 22:25:28 +01002818IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2819IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002820
2821static BC_STATUS zcommon_lex_token(BcLex *l)
2822{
2823 if (IS_BC) {
2824 IF_BC(RETURN_STATUS(zbc_lex_token(l));)
2825 }
2826 IF_DC(RETURN_STATUS(zdc_lex_token(l));)
2827}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002828#define zcommon_lex_token(...) (zcommon_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002829
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002830static bool bc_lex_more_input(BcLex *l)
2831{
2832 size_t str;
2833 bool comment;
2834
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002835 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002836
2837 // This loop is complex because the vm tries not to send any lines that end
2838 // with a backslash to the parser. The reason for that is because the parser
2839 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2840 // case, and for strings and comments, the parser will expect more stuff.
2841 comment = false;
2842 str = 0;
2843 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002844 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002845 char *string;
2846
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002847 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002848 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002849 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002850 break;
2851
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002852 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002853 while (*string) {
2854 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002855 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002856 if (IS_BC)
2857 str ^= (c == '"');
2858 else {
2859 if (c == ']')
2860 str -= 1;
2861 else if (c == '[')
2862 str += 1;
2863 }
2864 }
2865 string++;
2866 if (c == '/' && *string == '*') {
2867 comment = true;
2868 string++;
2869 continue;
2870 }
2871 if (c == '*' && *string == '/') {
2872 comment = false;
2873 string++;
2874 }
2875 }
2876 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002877 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002878 continue;
2879 }
2880
2881 // Check for backslash+newline.
2882 // we do not check that last char is '\n' -
2883 // if it is not, then it's EOF, and looping back
2884 // to bc_read_line() will detect it:
2885 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002886 if (string >= G.input_buffer.v && *string == '\\') {
2887 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002888 continue;
2889 }
2890
2891 break;
2892 }
2893
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002894 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002895 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002896// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2897 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002898
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002899 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002900}
2901
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002902static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002903{
2904 BcStatus s;
2905
2906 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002907 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002908
2909 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002910 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002911 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002912
2913 // Loop until failure or we don't have whitespace. This
2914 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002915 // Comments are also BC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002916 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002917 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002918 if (l->i == l->len) {
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002919 l->t.t = BC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002920 if (!G.input_fp)
2921 RETURN_STATUS(BC_STATUS_SUCCESS);
2922 if (!bc_lex_more_input(l)) {
2923 G.input_fp = NULL;
2924 RETURN_STATUS(BC_STATUS_SUCCESS);
2925 }
2926 // here it's guaranteed that l->i is below l->len
2927 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002928 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002929 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2930 l->buf + l->i);
Denys Vlasenkoec603182018-12-17 10:34:02 +01002931 s = zcommon_lex_token(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002932 } while (!s && l->t.t == BC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01002933 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002934
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002935 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002936}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002937#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002938
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002939#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01002940static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
2941{
2942 if (l->t.t == BC_LEX_NLINE)
2943 RETURN_STATUS(zbc_lex_next(l));
2944 RETURN_STATUS(BC_STATUS_SUCCESS);
2945}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002946#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002947
2948static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
2949{
2950 BcStatus s;
2951 s = zbc_lex_next(l);
2952 if (s) RETURN_STATUS(s);
2953 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
2954 s = zbc_lex_skip_if_at_NLINE(l);
2955 RETURN_STATUS(s);
2956}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002957#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002958#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01002959
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002960static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002961{
2962 l->buf = text;
2963 l->i = 0;
2964 l->len = strlen(text);
2965 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002966 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002967}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002968#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002969
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002970#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002971static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002972{
2973 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002974 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002975 const char *buf = l->buf + l->i - 1;
2976
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002977 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2978 const char *keyword8 = bc_lex_kws[i].name8;
2979 unsigned j = 0;
2980 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2981 j++;
2982 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002983 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002984 if (keyword8[j] != '\0')
2985 continue;
2986 match:
2987 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01002988 if (isalnum(buf[j]) || buf[j]=='_')
2989 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002990 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01002991 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002992 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenkoec603182018-12-17 10:34:02 +01002993 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002994 }
2995
2996 // We minus 1 because the index has already been incremented.
2997 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002998 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002999 }
3000
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003001 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003002 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003003
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003004 if (l->t.v.len > 2) {
3005 // Prevent this:
3006 // >>> qwe=1
3007 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3008 // '
3009 unsigned len = strchrnul(buf, '\n') - buf;
3010 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3011 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003012
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003013 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003014}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003015#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003016
Denys Vlasenko26819db2018-12-12 16:08:46 +01003017static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003018{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003019 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003020
3021 l->t.t = BC_LEX_STR;
3022
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003023 nls = 0;
3024 i = l->i;
3025 for (;;) {
3026 char c = l->buf[i];
3027 if (c == '\0') {
3028 l->i = i;
3029 RETURN_STATUS(bc_error("string end could not be found"));
3030 }
3031 if (c == '"')
3032 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003033 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003034 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003035 }
3036
3037 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003038 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3039 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3040 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003041 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003042 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003043 bc_vec_string(&l->t.v, len, l->buf + l->i);
3044
3045 l->i = i + 1;
3046 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003047 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003048
Denys Vlasenko26819db2018-12-12 16:08:46 +01003049 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003050}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003051#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003052
Denys Vlasenko0a238142018-12-14 16:48:34 +01003053static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003054{
3055 if (l->buf[l->i] == '=') {
3056 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003057 with_and_without >>= 8; // store "with" value
3058 } // else store "without" value
3059 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003060}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003061#define bc_lex_assign(l, with, without) \
3062 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003063
Denys Vlasenko29301232018-12-11 15:29:32 +01003064static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003065{
3066 size_t i, nls = 0;
3067 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003068
3069 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003070 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003071 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003072 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003073 check_star:
3074 if (c == '*') {
3075 c = buf[++i];
3076 if (c == '/')
3077 break;
3078 goto check_star;
3079 }
3080 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003081 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003082 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003083 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003084 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003085 }
3086
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003087 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003088 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003089 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003090
Denys Vlasenko29301232018-12-11 15:29:32 +01003091 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003092}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003093#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003094
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003095static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003096{
3097 BcStatus s = BC_STATUS_SUCCESS;
3098 char c = l->buf[l->i++], c2;
3099
3100 // This is the workhorse of the lexer.
3101 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003102// case '\0': // probably never reached
3103// l->i--;
3104// l->t.t = BC_LEX_EOF;
3105// l->newline = true;
3106// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003107 case '\n':
3108 l->t.t = BC_LEX_NLINE;
3109 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003110 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003111 case '\t':
3112 case '\v':
3113 case '\f':
3114 case '\r':
3115 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003116 bc_lex_whitespace(l);
3117 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003118 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003119 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003120 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003121 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003122 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003123 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003124 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003126 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003127 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003128 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003129 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003130 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003131 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003132 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003133 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003134 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3135 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 c2 = l->buf[l->i];
3138 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003139 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003140 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 ++l->i;
3142 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003143 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003145 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003146 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003147 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003148 case '(':
3149 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3151 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003152 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3154 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003155 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 c2 = l->buf[l->i];
3157 if (c2 == '+') {
3158 ++l->i;
3159 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003160 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003161 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3162 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003163 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 l->t.t = BC_LEX_COMMA;
3165 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003167 c2 = l->buf[l->i];
3168 if (c2 == '-') {
3169 ++l->i;
3170 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003171 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003172 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3173 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003174 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003175 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003176 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003177 else {
3178 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003179 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003180 }
3181 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003182 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003183 c2 = l->buf[l->i];
3184 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003185 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003186 else
3187 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3188 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003189 case '0':
3190 case '1':
3191 case '2':
3192 case '3':
3193 case '4':
3194 case '5':
3195 case '6':
3196 case '7':
3197 case '8':
3198 case '9':
3199 case 'A':
3200 case 'B':
3201 case 'C':
3202 case 'D':
3203 case 'E':
3204 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003205 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003207 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 l->t.t = BC_LEX_SCOLON;
3209 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003210 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003211 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3212 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003214 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3215 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003217 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3218 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 case '[':
3220 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3222 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003223 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003224 if (l->buf[l->i] == '\n') {
3225 l->t.t = BC_LEX_WHITESPACE;
3226 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003227 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003228 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003229 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003230 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003231 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3232 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003233 case 'a':
3234 case 'b':
3235 case 'c':
3236 case 'd':
3237 case 'e':
3238 case 'f':
3239 case 'g':
3240 case 'h':
3241 case 'i':
3242 case 'j':
3243 case 'k':
3244 case 'l':
3245 case 'm':
3246 case 'n':
3247 case 'o':
3248 case 'p':
3249 case 'q':
3250 case 'r':
3251 case 's':
3252 case 't':
3253 case 'u':
3254 case 'v':
3255 case 'w':
3256 case 'x':
3257 case 'y':
3258 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003259 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003260 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003261 case '{':
3262 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003263 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3264 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003265 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003266 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003267 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003268 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003269 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003270 ++l->i;
3271 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003272 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003273 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003274 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003275 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003276 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003277 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003278 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003279 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003280 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003281 }
3282
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003283 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003284}
3285#endif // ENABLE_BC
3286
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003287#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003288static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003289{
Gavin Howard01055ba2018-11-03 11:00:21 -06003290 if (isspace(l->buf[l->i - 1])) {
3291 bc_lex_whitespace(l);
3292 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003293 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003294 RETURN_STATUS(bc_error("extended register"));
3295 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003296 }
3297 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003298 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003299 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003300 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003301 l->t.t = BC_LEX_NAME;
3302 }
3303
Denys Vlasenko29301232018-12-11 15:29:32 +01003304 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003305}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003306#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003307
Denys Vlasenko29301232018-12-11 15:29:32 +01003308static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003309{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003310 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003311
3312 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003313 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003314
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003315 nls = 0;
3316 depth = 1;
3317 i = l->i;
3318 for (;;) {
3319 char c = l->buf[i];
3320 if (c == '\0') {
3321 l->i = i;
3322 RETURN_STATUS(bc_error("string end could not be found"));
3323 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003324 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003325 if (i == l->i || l->buf[i - 1] != '\\') {
3326 if (c == '[') depth++;
3327 if (c == ']')
3328 if (--depth == 0)
3329 break;
3330 }
3331 bc_vec_push(&l->t.v, &l->buf[i]);
3332 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003333 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003334 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003335
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003336 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003337 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3338 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3339 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003340 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003341 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003342
3343 l->i = i;
3344 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003345 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003346
Denys Vlasenko29301232018-12-11 15:29:32 +01003347 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003348}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003349#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003350
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003351static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003352{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003353 static const //BcLexType - should be this type, but narrower type saves size:
3354 uint8_t
3355 dc_lex_regs[] = {
3356 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
3357 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
3358 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
3359 BC_LEX_STORE_PUSH,
3360 };
3361 static const //BcLexType - should be this type
3362 uint8_t
3363 dc_lex_tokens[] = {
3364 /* %&'( */
3365 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
3366 /* )*+, */
3367 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
3368 /* -./ */
3369 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
3370 /* 0123456789 */
3371 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3372 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3373 BC_LEX_INVALID, BC_LEX_INVALID,
3374 /* :;<=>?@ */
3375 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
3376 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
3377 /* ABCDEFGH */
3378 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3379 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
3380 /* IJKLMNOP */
3381 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
3382 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
3383 /* QRSTUVWXY */
3384 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
3385 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
3386 /* Z[\] */
3387 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3388 /* ^_` */
3389 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
3390 /* abcdefgh */
3391 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
3392 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
3393 /* ijklmnop */
3394 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
3395 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
3396 /* qrstuvwx */
3397 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
3398 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
3399 /* yz */
3400 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
3401 /* {|}~ */
3402 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
3403 };
3404
Gavin Howard01055ba2018-11-03 11:00:21 -06003405 BcStatus s = BC_STATUS_SUCCESS;
3406 char c = l->buf[l->i++], c2;
3407 size_t i;
3408
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003409 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3410 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003411 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003412 }
3413
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003414 if (c >= '%' && c <= '~'
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003415 && (l->t.t = dc_lex_tokens[c - '%']) != BC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003416 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003417 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003418 }
3419
3420 // This is the workhorse of the lexer.
3421 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003422// case '\0': // probably never reached
3423// l->t.t = BC_LEX_EOF;
3424// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003425 case '\n':
3426 case '\t':
3427 case '\v':
3428 case '\f':
3429 case '\r':
3430 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003431 l->newline = (c == '\n');
3432 bc_lex_whitespace(l);
3433 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003434 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003435 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003436 if (c2 == '=')
3437 l->t.t = BC_LEX_OP_REL_NE;
3438 else if (c2 == '<')
3439 l->t.t = BC_LEX_OP_REL_LE;
3440 else if (c2 == '>')
3441 l->t.t = BC_LEX_OP_REL_GE;
3442 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003443 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003444 ++l->i;
3445 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003446 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003447 bc_lex_lineComment(l);
3448 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003449 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003450 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003451 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003452 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003453 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003454 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003455 case '0':
3456 case '1':
3457 case '2':
3458 case '3':
3459 case '4':
3460 case '5':
3461 case '6':
3462 case '7':
3463 case '8':
3464 case '9':
3465 case 'A':
3466 case 'B':
3467 case 'C':
3468 case 'D':
3469 case 'E':
3470 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003471 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003472 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003473 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003474 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003475 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003476 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003477 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003478 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003479 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003480 }
3481
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003482 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003483}
3484#endif // ENABLE_DC
3485
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003486static void bc_parse_push(BcParse *p, char i)
3487{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003488 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003489 bc_vec_pushByte(&p->func->code, i);
3490}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003491
Gavin Howard01055ba2018-11-03 11:00:21 -06003492static void bc_parse_pushName(BcParse *p, char *name)
3493{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003494 while (*name)
3495 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003496 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003497}
3498
3499static void bc_parse_pushIndex(BcParse *p, size_t idx)
3500{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003501 size_t mask;
3502 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003503
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003504 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003505 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3506 amt = sizeof(idx);
3507 do {
3508 if (idx & mask) break;
3509 mask >>= 8;
3510 amt--;
3511 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003512
3513 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003514
3515 while (idx != 0) {
3516 bc_parse_push(p, (unsigned char)idx);
3517 idx >>= 8;
3518 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003519}
3520
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003521#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003522static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3523{
3524 bc_parse_push(p, BC_INST_JUMP);
3525 bc_parse_pushIndex(p, idx);
3526}
3527
3528static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3529{
3530 bc_parse_push(p, BC_INST_JUMP_ZERO);
3531 bc_parse_pushIndex(p, idx);
3532}
3533
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003534static BC_STATUS bc_parse_pushSTR(BcParse *p)
3535{
3536 char *str = xstrdup(p->l.t.v.v);
3537
3538 bc_parse_push(p, BC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003539 bc_parse_pushIndex(p, p->func->strs.len);
3540 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003541
3542 RETURN_STATUS(zbc_lex_next(&p->l));
3543}
3544#define bc_parse_pushSTR(...) (bc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003545#endif
3546
3547static void bc_parse_pushNUM(BcParse *p)
3548{
3549 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003550#if ENABLE_BC && ENABLE_DC
3551 size_t idx = IS_BC ? p->func->consts.len : G.prog.consts.len;
3552 bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
3553#elif ENABLE_BC
3554 size_t idx = p->func->consts.len;
3555 bc_vec_push(&p->func->consts, &num);
3556#else // DC
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003557 size_t idx = G.prog.consts.len;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003558 bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003559#endif
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003560 bc_parse_push(p, BC_INST_NUM);
3561 bc_parse_pushIndex(p, idx);
3562}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003563
Denys Vlasenko99b37622018-12-15 20:06:59 +01003564IF_BC(static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p);)
Denys Vlasenkoe755e302018-12-13 22:25:28 +01003565IF_DC(static BC_STATUS zdc_parse_parse(BcParse *p);)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003566
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003567// "Parse" half of "parse,execute,repeat" main loop
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003568static BC_STATUS zcommon_parse(BcParse *p)
3569{
3570 if (IS_BC) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003571// FIXME: "eating" of stmt delemiters is coded inconsistently
3572// (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't),
3573// which causes bugs such as "print 1 print 2" erroneously accepted,
3574// or "print 1 else 2" detecting parse error only after executing
3575// "print 1" part.
Denys Vlasenko99b37622018-12-15 20:06:59 +01003576 IF_BC(RETURN_STATUS(zbc_parse_stmt_or_funcdef(p));)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003577 }
3578 IF_DC(RETURN_STATUS(zdc_parse_parse(p));)
3579}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003580#define zcommon_parse(...) (zcommon_parse(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003581
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003582static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003583{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003584 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003585
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003586 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003587}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003588#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003589
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003590// Called when parsing or execution detects a failure,
3591// resets execution structures.
3592static void bc_program_reset(void)
3593{
3594 BcFunc *f;
3595 BcInstPtr *ip;
3596
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003597 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003598 bc_vec_pop_all(&G.prog.results);
3599
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003600 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003601 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003602 ip->idx = f->code.len;
3603}
3604
Denys Vlasenko26819db2018-12-12 16:08:46 +01003605// Called when zbc/zdc_parse_parse() detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003606// resets parsing structures.
3607static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003608{
3609 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003610 bc_vec_pop_all(&p->func->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003611 IF_BC(bc_vec_pop_all(&p->func->labels);)
3612 IF_BC(bc_vec_pop_all(&p->func->autos);)
3613 IF_BC(p->func->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06003614
Denys Vlasenko65e10462018-12-19 15:13:14 +01003615 p->fidx = BC_PROG_MAIN;
3616 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003617 }
3618
3619 p->l.i = p->l.len;
3620 p->l.t.t = BC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003621
Denys Vlasenko503faf92018-12-20 16:24:18 +01003622 IF_BC(bc_vec_pop_all(&p->exits);)
3623 IF_BC(bc_vec_pop_all(&p->conds);)
3624 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003625
Denys Vlasenkod38af482018-12-04 19:11:02 +01003626 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003627}
3628
3629static void bc_parse_free(BcParse *p)
3630{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003631 IF_BC(bc_vec_free(&p->exits);)
3632 IF_BC(bc_vec_free(&p->conds);)
3633 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003634 bc_lex_free(&p->l);
3635}
3636
Denys Vlasenkofa210792018-12-19 19:35:40 +01003637static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003638{
3639 memset(p, 0, sizeof(BcParse));
3640
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003641 bc_lex_init(&p->l);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003642 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3643 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3644 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003645
Denys Vlasenkofa210792018-12-19 19:35:40 +01003646 p->fidx = fidx;
3647 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003648}
3649
Denys Vlasenko04715442018-12-21 00:10:26 +01003650static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003651{
Denys Vlasenko04715442018-12-21 00:10:26 +01003652 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003653 BcFunc f;
3654 bc_func_init(&f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003655 //idx = G.prog.fns.len;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003656 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003657 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003658}
3659
3660#if ENABLE_BC
3661
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003662// Note: takes ownership of 'name' (must be malloced)
3663static size_t bc_program_addFunc(char *name)
3664{
3665 size_t idx;
3666 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003667 int inserted;
3668
3669 entry.name = name;
3670 entry.idx = G.prog.fns.len;
3671
3672 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3673 if (!inserted) free(name);
3674
3675 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3676 idx = entry_ptr->idx;
3677
3678 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003679 // There is already a function with this name.
3680 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003681 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003682 bc_func_free(func);
3683 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003684 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003685 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003686 }
3687
3688 return idx;
3689}
3690
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003691#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3692#define BC_PARSE_LEAF(p, rparen) \
3693 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3694 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3695
3696// We can calculate the conversion between tokens and exprs by subtracting the
3697// position of the first operator in the lex enum and adding the position of the
3698// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko0154d782018-12-14 23:32:51 +01003699#define BC_TOKEN_2_INST(t) ((char) ((t) - BC_LEX_NEG + BC_INST_NEG))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003700
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003701static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags);
3702
3703static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags)
3704{
3705 BcStatus s;
3706
3707 s = bc_parse_expr_empty_ok(p, flags);
3708 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3709 RETURN_STATUS(bc_error("empty expression"));
3710 RETURN_STATUS(s);
3711}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003712#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003713
3714static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003715#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003716
3717static BC_STATUS zbc_parse_stmt(BcParse *p)
3718{
3719 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3720}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003721#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003722
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003723static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003724{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003725 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3726 // Same for "else", "while()", "for()".
3727 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3728 if (s) RETURN_STATUS(s);
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003729 if (p->l.t.t == BC_LEX_NLINE)
3730 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003731
3732 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003733}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003734#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003735
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003736static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3737 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003738{
Denys Vlasenko65437582018-12-05 19:37:19 +01003739 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3740 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003741
3742 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003743 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003744 if (t == BC_LEX_LPAREN) break;
3745
Denys Vlasenko65437582018-12-05 19:37:19 +01003746 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003747 if (l >= r && (l != r || !left)) break;
3748
Denys Vlasenko0154d782018-12-14 23:32:51 +01003749 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003750 bc_vec_pop(&p->ops);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003751 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003752 }
3753
3754 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003755}
3756
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003757static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003758{
3759 BcLexType top;
3760
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003761 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003762 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003763 top = BC_PARSE_TOP_OP(p);
3764
3765 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003766 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003767
3768 bc_vec_pop(&p->ops);
3769 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3770
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003771 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003772 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003773 top = BC_PARSE_TOP_OP(p);
3774 }
3775
3776 bc_vec_pop(&p->ops);
3777
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003778 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003779}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003780#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003781
Denys Vlasenko26819db2018-12-12 16:08:46 +01003782static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003783{
3784 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003785 size_t nparams;
3786
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003787 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003788 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3789
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003790 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003791 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003792
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003793 nparams = 0;
3794 if (p->l.t.t != BC_LEX_RPAREN) {
3795 for (;;) {
3796 s = zbc_parse_expr(p, flags);
3797 if (s) RETURN_STATUS(s);
3798 nparams++;
3799 if (p->l.t.t != BC_LEX_COMMA) {
3800 if (p->l.t.t == BC_LEX_RPAREN)
3801 break;
3802 RETURN_STATUS(bc_error_bad_token());
3803 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003804 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003805 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003806 }
3807 }
3808
Gavin Howard01055ba2018-11-03 11:00:21 -06003809 bc_parse_push(p, BC_INST_CALL);
3810 bc_parse_pushIndex(p, nparams);
3811
Denys Vlasenko26819db2018-12-12 16:08:46 +01003812 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003813}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003814#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003815
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003816// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003817static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003818{
3819 BcStatus s;
3820 BcId entry, *entry_ptr;
3821 size_t idx;
3822
3823 entry.name = name;
3824
Denys Vlasenko26819db2018-12-12 16:08:46 +01003825 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003826 if (s) goto err;
3827
3828 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003829 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003830 goto err;
3831 }
3832
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003833 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003834
3835 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003836 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003837 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003838 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003839 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003840 free(name);
3841
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003842 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003843 bc_parse_pushIndex(p, entry_ptr->idx);
3844
Denys Vlasenko26819db2018-12-12 16:08:46 +01003845 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003846 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003847 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003848 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003849}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003850#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003851
Denys Vlasenko26819db2018-12-12 16:08:46 +01003852static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003853{
3854 BcStatus s;
3855 char *name;
3856
3857 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003858 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003859 if (s) goto err;
3860
3861 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003862 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003863 if (s) goto err;
3864
3865 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003866 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003867 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003868 goto err;
3869 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003870 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003871 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003872 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003873 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003874 s = zbc_parse_expr(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003875 if (s) goto err;
3876 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003877 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003878 if (s) goto err;
3879 bc_parse_push(p, *type);
3880 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003881 free(name);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003882 } else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003883 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003884 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 goto err;
3886 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003887 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003888 s = zbc_parse_call(p, name, flags);
3889 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003890 *type = BC_INST_VAR;
3891 bc_parse_push(p, BC_INST_VAR);
3892 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003893 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 }
3895
Denys Vlasenko26819db2018-12-12 16:08:46 +01003896 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003897 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003898 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003899 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003900}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003901#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003902
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003903static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003904{
3905 BcStatus s;
3906
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003907 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003908 if (s) RETURN_STATUS(s);
3909 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003910
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003911 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003912 if (s) RETURN_STATUS(s);
3913 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003914
3915 bc_parse_push(p, BC_INST_READ);
3916
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003917 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003918}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003919#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003920
Denys Vlasenko26819db2018-12-12 16:08:46 +01003921static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003922 BcInst *prev)
3923{
3924 BcStatus s;
3925
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003926 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003927 if (s) RETURN_STATUS(s);
3928 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003929
3930 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3931
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003932 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003933 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003934
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003935 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003936 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003937
Denys Vlasenko26819db2018-12-12 16:08:46 +01003938 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003939
3940 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3941 bc_parse_push(p, *prev);
3942
Denys Vlasenko26819db2018-12-12 16:08:46 +01003943 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003944}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003945#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003946
Denys Vlasenko26819db2018-12-12 16:08:46 +01003947static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003948{
3949 BcStatus s;
3950
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003951 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003952 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003953
3954 if (p->l.t.t != BC_LEX_LPAREN) {
3955 *type = BC_INST_SCALE;
3956 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003957 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003958 }
3959
3960 *type = BC_INST_SCALE_FUNC;
3961 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3962
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003963 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003964 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003965
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003966 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003967 if (s) RETURN_STATUS(s);
3968 if (p->l.t.t != BC_LEX_RPAREN)
3969 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003970 bc_parse_push(p, BC_INST_SCALE_FUNC);
3971
Denys Vlasenko26819db2018-12-12 16:08:46 +01003972 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003973}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003974#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003975
Denys Vlasenko26819db2018-12-12 16:08:46 +01003976static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003977 size_t *nexprs, uint8_t flags)
3978{
3979 BcStatus s;
3980 BcLexType type;
3981 char inst;
3982 BcInst etype = *prev;
3983
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003984 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM
3985 || etype == BC_INST_SCALE || etype == BC_INST_LAST
3986 || etype == BC_INST_IBASE || etype == BC_INST_OBASE
3987 ) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003988 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3989 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003990 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003991 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003992 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3993 *paren_expr = true;
3994
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003995 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003996 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003997 type = p->l.t.t;
3998
3999 // Because we parse the next part of the expression
4000 // right here, we need to increment this.
4001 *nexprs = *nexprs + 1;
4002
4003 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004004 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004005 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004006 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004007 case BC_LEX_KEY_IBASE:
4008 case BC_LEX_KEY_LAST:
4009 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004010 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004011 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004012 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004013 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004014 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004015 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004016 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004017 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004018 else
4019 bc_parse_push(p, BC_INST_SCALE);
4020 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004021 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004022 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004024 }
4025
4026 if (!s) bc_parse_push(p, inst);
4027 }
4028
Denys Vlasenko26819db2018-12-12 16:08:46 +01004029 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004030}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004031#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004032
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004033static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004034 bool rparen, size_t *nexprs)
4035{
4036 BcStatus s;
4037 BcLexType type;
4038 BcInst etype = *prev;
4039
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004040 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004041 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004042
4043 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4044 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4045 BC_LEX_OP_MINUS :
4046 BC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004047 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004048
4049 // We can just push onto the op stack because this is the largest
4050 // precedence operator that gets pushed. Inc/dec does not.
4051 if (type != BC_LEX_OP_MINUS)
4052 bc_vec_push(&p->ops, &type);
4053 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004054 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004055
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004056 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004057}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004058#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004059
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004060static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004061{
4062 BcStatus s;
4063 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004064
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004065 for (;;) {
4066 s = zbc_lex_next(&p->l);
4067 if (s) RETURN_STATUS(s);
4068 type = p->l.t.t;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004069 if (type == BC_LEX_STR) {
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004070 s = bc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004071 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004072 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004073 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004074 if (s) RETURN_STATUS(s);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004075 bc_parse_push(p, BC_INST_PRINT_POP);
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004076 if (p->l.t.t != BC_LEX_COMMA)
4077 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004078 }
4079
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004080 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004081}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004082#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004083
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004084static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004085{
4086 BcStatus s;
4087 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004088
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004089 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004090 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004091 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004092
4093 t = p->l.t.t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004094 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4095 bc_parse_push(p, BC_INST_RET0);
4096 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004097 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004098 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004099 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004100 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004101 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004102 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004103 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004104
4105 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004106 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004107 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06004108 }
4109
4110 bc_parse_push(p, BC_INST_RET);
4111 }
4112
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004113 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004114 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004115}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004116#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004117
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004118static void rewrite_label_to_current(BcParse *p, size_t idx)
4119{
4120 size_t *label = bc_vec_item(&p->func->labels, idx);
4121 *label = p->func->code.len;
4122}
4123
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004124static BC_STATUS zbc_parse_if(BcParse *p)
4125{
4126 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004127 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004128
4129 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4130 s = zbc_lex_next(&p->l);
4131 if (s) RETURN_STATUS(s);
4132 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
4133
4134 s = zbc_lex_next(&p->l);
4135 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004136 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004137 if (s) RETURN_STATUS(s);
4138
4139 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004140
Denys Vlasenko15850832018-12-16 21:40:54 +01004141 ip_idx = p->func->labels.len;
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004142 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenko15850832018-12-16 21:40:54 +01004143 bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004144
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004145 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004146 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004147
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004148 dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4149 if (p->l.t.t == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004150 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004151
Denys Vlasenko15850832018-12-16 21:40:54 +01004152 ip2_idx = p->func->labels.len;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004153
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004154 dbg_lex("%s:%d after if() body: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004155 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004156
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004157 dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004158 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004159
Denys Vlasenko15850832018-12-16 21:40:54 +01004160 bc_vec_push(&p->func->labels, &ip2_idx);
4161 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004162
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004163 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004164 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004165 }
4166
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004167 dbg_lex("%s:%d rewriting label to jump after 'if' body-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004168 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004169
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004170 dbg_lex_done("%s:%d done", __func__, __LINE__);
4171 RETURN_STATUS(s);
4172}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004173#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004174
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004175static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004176{
4177 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004178 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004179 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004180
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004181 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004182 if (s) RETURN_STATUS(s);
4183 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004184 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004185 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004186
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004187 cond_idx = p->func->labels.len;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004188 ip_idx = cond_idx + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06004189
4190 bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004191 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004192
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004193 bc_vec_push(&p->exits, &ip_idx);
4194 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004195
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004196 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004197 if (s) RETURN_STATUS(s);
4198 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004199
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004200 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004201
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004202 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004203 if (s) RETURN_STATUS(s);
4204
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004205 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004206 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004207
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004208 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004209 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004210
4211 bc_vec_pop(&p->exits);
4212 bc_vec_pop(&p->conds);
4213
4214 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004215}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004216#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004217
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004218static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004219{
4220 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004221 size_t cond_idx, exit_idx, body_idx, update_idx;
4222
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004223 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004224 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004225 if (s) RETURN_STATUS(s);
4226 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004227 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004228 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004229
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004230 if (p->l.t.t != BC_LEX_SCOLON) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004231 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004232 bc_parse_push(p, BC_INST_POP);
4233 if (s) RETURN_STATUS(s);
4234 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004235 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004236 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4237 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004238
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004239 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004240 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004241 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004242
4243 cond_idx = p->func->labels.len;
4244 update_idx = cond_idx + 1;
4245 body_idx = update_idx + 1;
4246 exit_idx = body_idx + 1;
4247
4248 bc_vec_push(&p->func->labels, &p->func->code.len);
4249
4250 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004251 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004252 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004253 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004254 // This is safe to set because the current token is a semicolon,
4255 // which has no string requirement.
4256 bc_vec_string(&p->l.t.v, 1, "1");
4257 bc_parse_pushNUM(p);
Denys Vlasenko00646792018-12-05 18:12:27 +01004258 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004259 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004260 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004261
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004262 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004263
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004264 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004265 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004266
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004267 bc_parse_pushJUMP_ZERO(p, exit_idx);
4268 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004269
Gavin Howard01055ba2018-11-03 11:00:21 -06004270 bc_vec_push(&p->conds, &update_idx);
4271 bc_vec_push(&p->func->labels, &p->func->code.len);
4272
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004273 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004274 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004275 bc_parse_push(p, BC_INST_POP);
4276 if (s) RETURN_STATUS(s);
4277 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004278 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004279 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4280 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004281
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004282 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004283 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004284 bc_vec_push(&p->func->labels, &p->func->code.len);
4285
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004286 bc_vec_push(&p->exits, &exit_idx);
4287 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004288
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004289 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004290 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004291
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004292 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004293 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004294
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004295 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004296 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004297
4298 bc_vec_pop(&p->exits);
4299 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004300
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004301 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004302}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004303#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004304
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004305static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004306{
4307 BcStatus s;
4308 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004309
Gavin Howard01055ba2018-11-03 11:00:21 -06004310 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004311 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4312 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004313 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004314 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004315 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004316 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004317
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004318 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004319
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004320 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004321 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004322
4323 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004324 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004325
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004326 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004327}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004328#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004329
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004330static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004331{
4332 BcStatus s;
4333 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004334 char *name;
4335
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004336 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004337 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004338 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004339 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004340 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004341
4342 name = xstrdup(p->l.t.v.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004343 p->fidx = bc_program_addFunc(name);
4344 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004345
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004346 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004347 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004348 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004349 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004350 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004351 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004352
4353 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004354 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004355 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004356
4357 ++p->func->nparams;
4358
4359 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004360 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004361 if (s) goto err;
4362
4363 var = p->l.t.t != BC_LEX_LBRACKET;
4364
4365 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004366 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004367 if (s) goto err;
4368
4369 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004370 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004371 goto err;
4372 }
4373
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004374 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004375 if (s) goto err;
4376 }
4377
4378 comma = p->l.t.t == BC_LEX_COMMA;
4379 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004380 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004381 if (s) goto err;
4382 }
4383
Denys Vlasenko29301232018-12-11 15:29:32 +01004384 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004385 if (s) goto err;
4386 }
4387
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004388 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004389
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004390 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004391 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004392
4393 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004394 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004395
Denys Vlasenko202dd192018-12-16 17:30:35 +01004396 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4397 s = zbc_lex_skip_if_at_NLINE(&p->l);
4398 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004399 //GNU bc requires a {} block even if function body has single stmt, enforce this?
4400 if (p->l.t.t != BC_LEX_LBRACE)
4401 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004402
4403 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004404 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004405 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004406 if (s) RETURN_STATUS(s);
4407
4408 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004409
4410 // Subsequent code generation is into main program
4411 p->fidx = BC_PROG_MAIN;
4412 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004413
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004414 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004415 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004416 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004417 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004418 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004419 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004420}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004421#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004422
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004423static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004424{
4425 BcStatus s;
4426 bool comma, var, one;
4427 char *name;
4428
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004429 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004430 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004431 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004432
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004433 comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004434 one = p->l.t.t == BC_LEX_NAME;
4435
4436 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004437 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004438 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004439 if (s) goto err;
4440
4441 var = p->l.t.t != BC_LEX_LBRACKET;
4442 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004443 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004444 if (s) goto err;
4445
4446 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004447 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004448 goto err;
4449 }
4450
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004451 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004452 if (s) goto err;
4453 }
4454
4455 comma = p->l.t.t == BC_LEX_COMMA;
4456 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004457 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004458 if (s) goto err;
4459 }
4460
Denys Vlasenko29301232018-12-11 15:29:32 +01004461 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004462 if (s) goto err;
4463 }
4464
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004465 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4466 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004467
4468 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004469 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004470
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004471 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004472 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004473 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004474 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004475 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004476 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004477}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004478#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004479
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004480#undef zbc_parse_stmt_possibly_auto
4481static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004482{
4483 BcStatus s = BC_STATUS_SUCCESS;
4484
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004485 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004486
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004487 if (p->l.t.t == BC_LEX_NLINE) {
4488 dbg_lex_done("%s:%d done (seen BC_LEX_NLINE)", __func__, __LINE__);
4489 RETURN_STATUS(zbc_lex_next(&p->l));
4490 }
4491 if (p->l.t.t == BC_LEX_SCOLON) {
4492 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4493 RETURN_STATUS(zbc_lex_next(&p->l));
4494 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004495
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004496 if (p->l.t.t == BC_LEX_LBRACE) {
4497 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4498 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004499 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004500 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004501 } while (p->l.t.t == BC_LEX_NLINE);
4502 if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) {
4503 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4504 s = zbc_parse_auto(p);
4505 if (s) RETURN_STATUS(s);
4506 }
4507 while (p->l.t.t != BC_LEX_RBRACE) {
4508 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4509 s = zbc_parse_stmt(p);
4510 if (s) RETURN_STATUS(s);
4511 }
4512 s = zbc_lex_next(&p->l);
4513 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4514 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004515 }
4516
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004517 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004518 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004519 case BC_LEX_OP_INC:
4520 case BC_LEX_OP_DEC:
4521 case BC_LEX_OP_MINUS:
4522 case BC_LEX_OP_BOOL_NOT:
4523 case BC_LEX_LPAREN:
4524 case BC_LEX_NAME:
4525 case BC_LEX_NUMBER:
4526 case BC_LEX_KEY_IBASE:
4527 case BC_LEX_KEY_LAST:
4528 case BC_LEX_KEY_LENGTH:
4529 case BC_LEX_KEY_OBASE:
4530 case BC_LEX_KEY_READ:
4531 case BC_LEX_KEY_SCALE:
4532 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004533 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004534 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004535 case BC_LEX_STR:
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004536 s = bc_parse_pushSTR(p);
4537 bc_parse_push(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004538 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004539 case BC_LEX_KEY_BREAK:
4540 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004541 s = zbc_parse_break_or_continue(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004542 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004543 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004544 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004545 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004546 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004547 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004548 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004549 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004550 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004551 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004552 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004553 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004554 // "limits" is a compile-time command,
4555 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004556 printf(
4557 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4558 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4559 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4560 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4561 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4562 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4563 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4564 "Number of vars = "BC_MAX_VARS_STR "\n"
4565 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004566 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004567 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004568 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004569 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004570 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004571 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004572 // "quit" is a compile-time command. For example,
4573 // "if (0 == 1) quit" terminates when parsing the statement,
4574 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004575 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004577 if (!p->in_funcdef)
4578 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004579 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004580 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004581 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004582 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004584 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004585 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004586 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004587 }
4588
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004589 if (s || G_interrupt) {
4590 bc_parse_reset(p);
4591 s = BC_STATUS_FAILURE;
4592 }
4593
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004594 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004595 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004596}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004597#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004598
Denys Vlasenko99b37622018-12-15 20:06:59 +01004599static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004600{
4601 BcStatus s;
4602
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004603 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004604 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004605 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004606 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004607 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004608 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004609 } else {
4610 dbg_lex("%s:%d p->l.t.t:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.t.t);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004611 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004612 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004613
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004614 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004615 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004616}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004617#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004618
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004619// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004620static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004621{
4622 BcStatus s = BC_STATUS_SUCCESS;
4623 BcInst prev = BC_INST_PRINT;
4624 BcLexType top, t = p->l.t.t;
4625 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004626 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004627 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4628
Denys Vlasenko17df8822018-12-14 23:00:24 +01004629 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004630 paren_first = p->l.t.t == BC_LEX_LPAREN;
4631 nparens = nrelops = 0;
4632 paren_expr = rprn = done = get_token = assign = false;
4633 bin_last = true;
4634
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004635 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004636 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004637 case BC_LEX_OP_INC:
4638 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004639 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004640 rprn = get_token = bin_last = false;
4641 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004642 case BC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004643 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004644 rprn = get_token = false;
4645 bin_last = prev == BC_INST_MINUS;
4646 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004647 case BC_LEX_OP_ASSIGN_POWER:
4648 case BC_LEX_OP_ASSIGN_MULTIPLY:
4649 case BC_LEX_OP_ASSIGN_DIVIDE:
4650 case BC_LEX_OP_ASSIGN_MODULUS:
4651 case BC_LEX_OP_ASSIGN_PLUS:
4652 case BC_LEX_OP_ASSIGN_MINUS:
4653 case BC_LEX_OP_ASSIGN:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004654 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM
4655 && prev != BC_INST_SCALE && prev != BC_INST_IBASE
4656 && prev != BC_INST_OBASE && prev != BC_INST_LAST
4657 ) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004658 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004659 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004660 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004661 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004662 break;
4663 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004664 // Fallthrough.
4665 case BC_LEX_OP_POWER:
4666 case BC_LEX_OP_MULTIPLY:
4667 case BC_LEX_OP_DIVIDE:
4668 case BC_LEX_OP_MODULUS:
4669 case BC_LEX_OP_PLUS:
4670 case BC_LEX_OP_REL_EQ:
4671 case BC_LEX_OP_REL_LE:
4672 case BC_LEX_OP_REL_GE:
4673 case BC_LEX_OP_REL_NE:
4674 case BC_LEX_OP_REL_LT:
4675 case BC_LEX_OP_REL_GT:
4676 case BC_LEX_OP_BOOL_NOT:
4677 case BC_LEX_OP_BOOL_OR:
4678 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004679 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4680 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4681 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004682 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004683 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004684 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004685 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004686 bc_parse_operator(p, t, ops_bgn, &nexprs);
4687 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004688 rprn = get_token = false;
4689 bin_last = t != BC_LEX_OP_BOOL_NOT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004690 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004691 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004692 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004693 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004694 ++nparens;
4695 paren_expr = rprn = bin_last = false;
4696 get_token = true;
4697 bc_vec_push(&p->ops, &t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004698 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004699 case BC_LEX_RPAREN:
Gavin Howard01055ba2018-11-03 11:00:21 -06004700 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004701 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004702 if (nparens == 0) {
4703 s = BC_STATUS_SUCCESS;
4704 done = true;
4705 get_token = false;
4706 break;
4707 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004708 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004709 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004710 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004711 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004712 --nparens;
4713 paren_expr = rprn = true;
4714 get_token = bin_last = false;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004715 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004716 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004717 case BC_LEX_NAME:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004718 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004719 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004720 paren_expr = true;
4721 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004722 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004723 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004724 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004725 case BC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004726 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004727 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004728 bc_parse_pushNUM(p);
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004729 nexprs++;
4730 prev = BC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004731 paren_expr = get_token = true;
4732 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004733 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004734 case BC_LEX_KEY_IBASE:
4735 case BC_LEX_KEY_LAST:
4736 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004737 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004738 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004739 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4740 bc_parse_push(p, (char) prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004741 paren_expr = get_token = true;
4742 rprn = bin_last = false;
4743 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004744 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004745 case BC_LEX_KEY_LENGTH:
4746 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004747 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004748 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004749 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004750 paren_expr = true;
4751 rprn = get_token = bin_last = false;
4752 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004753 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004754 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004755 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004756 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004757 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004758 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004759 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004760 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004761 paren_expr = true;
4762 rprn = get_token = bin_last = false;
4763 ++nexprs;
4764 prev = BC_INST_READ;
Gavin Howard01055ba2018-11-03 11:00:21 -06004765 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004766 case BC_LEX_KEY_SCALE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004767 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004768 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004769 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004770 paren_expr = true;
4771 rprn = get_token = bin_last = false;
4772 ++nexprs;
4773 prev = BC_INST_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06004774 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004775 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004776 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004777 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004778 }
4779
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004780 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004781 }
4782
4783 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004784 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004785
4786 while (p->ops.len > ops_bgn) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004787 top = BC_PARSE_TOP_OP(p);
4788 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4789
4790 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004791 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004792
Denys Vlasenko0154d782018-12-14 23:32:51 +01004793 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004794
4795 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4796 bc_vec_pop(&p->ops);
4797 }
4798
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004799 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004800 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004801
Gavin Howard01055ba2018-11-03 11:00:21 -06004802 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004803 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004804 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004805 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004806 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004807 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004808 }
4809
4810 if (flags & BC_PARSE_PRINT) {
4811 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4812 bc_parse_push(p, BC_INST_POP);
4813 }
4814
Denys Vlasenko17df8822018-12-14 23:00:24 +01004815 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004816 return s;
4817}
4818
Gavin Howard01055ba2018-11-03 11:00:21 -06004819#endif // ENABLE_BC
4820
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004821#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004822
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004823static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004824{
4825 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004826
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004827 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004828 if (s) RETURN_STATUS(s);
4829 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004830
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01004831 bc_parse_pushName(p, p->l.t.v.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004832
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004833 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004834}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004835#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004836
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004837static BC_STATUS zdc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004838{
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
4844 str = xstrdup(p->l.t.v.v);
4845 bc_parse_push(p, BC_INST_STR);
4846 bc_parse_pushIndex(p, 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__);
4854
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004855 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004856}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004857#define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004858
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004859static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004860{
4861 BcStatus s;
4862
4863 bc_parse_push(p, inst);
4864 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004865 s = zdc_parse_register(p);
4866 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004867 }
4868
4869 if (store) {
4870 bc_parse_push(p, BC_INST_SWAP);
4871 bc_parse_push(p, BC_INST_ASSIGN);
4872 bc_parse_push(p, BC_INST_POP);
4873 }
4874
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004875 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004876}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004877#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004878
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004879static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004880{
4881 BcStatus s;
4882
4883 bc_parse_push(p, inst);
4884 bc_parse_push(p, BC_INST_EXEC_COND);
4885
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004886 s = zdc_parse_register(p);
4887 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004888
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004889 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004890 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004891
4892 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004893 s = zdc_parse_register(p);
4894 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004895 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004896 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06004897 bc_parse_push(p, BC_PARSE_STREND);
4898
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004899 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004900}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004901#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004902
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004903static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004904{
4905 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06004906 uint8_t inst;
4907 bool assign, get_token = false;
4908
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004909 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004910 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004911 case BC_LEX_OP_REL_EQ:
4912 case BC_LEX_OP_REL_LE:
4913 case BC_LEX_OP_REL_GE:
4914 case BC_LEX_OP_REL_NE:
4915 case BC_LEX_OP_REL_LT:
4916 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004917 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004918 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004919 case BC_LEX_SCOLON:
4920 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004921 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004922 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004923 case BC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004924 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004925 s = zdc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004926 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004927 case BC_LEX_NEG:
4928 case BC_LEX_NUMBER:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004929 dbg_lex("%s:%d LEX_NEG/NUMBER", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004930 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004931 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004932 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004933 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004934 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004935 }
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004936 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004937 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
4938 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004939 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004940 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004941 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004942 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004943 else
4944 bc_parse_push(p, BC_INST_READ);
4945 get_token = true;
4946 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004947 case BC_LEX_OP_ASSIGN:
4948 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06004949 assign = t == BC_LEX_OP_ASSIGN;
4950 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004951 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004952 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004953 case BC_LEX_LOAD:
4954 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06004955 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004956 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004957 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004958 case BC_LEX_STORE_IBASE:
4959 case BC_LEX_STORE_SCALE:
4960 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004961 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004962 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004963 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004964 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004965 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004966 get_token = true;
4967 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004968 }
4969
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004970 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004971
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004972 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004973 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004974}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004975#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004976
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004977static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004978{
Gavin Howard01055ba2018-11-03 11:00:21 -06004979 BcLexType t;
4980
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004981 dbg_lex_enter("%s:%d entered G.prs.l.t.t:%d", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004982 for (;;) {
4983 BcInst inst;
4984 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004985
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004986 t = p->l.t.t;
4987 if (t == BC_LEX_EOF) break;
4988
4989 inst = dc_parse_insts[t];
Gavin Howard01055ba2018-11-03 11:00:21 -06004990 if (inst != BC_INST_INVALID) {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004991 dbg_lex("%s:%d", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004992 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004993 s = zbc_lex_next(&p->l);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004994 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004995 dbg_lex("%s:%d", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004996 s = zdc_parse_token(p, t, flags);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004997 }
4998 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004999 }
5000
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005001 if (flags & BC_PARSE_NOCALL)
Gavin Howard01055ba2018-11-03 11:00:21 -06005002 bc_parse_push(p, BC_INST_POP_EXEC);
5003
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005004 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005005 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005006}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005007#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005008
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01005009static BC_STATUS zdc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005010{
5011 BcStatus s;
5012
5013 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005014 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005015 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005016 s = zdc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005017
Denys Vlasenkod38af482018-12-04 19:11:02 +01005018 if (s || G_interrupt) {
5019 bc_parse_reset(p);
5020 s = BC_STATUS_FAILURE;
5021 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005022
Denys Vlasenko26819db2018-12-12 16:08:46 +01005023 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005024}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005025#define zdc_parse_parse(...) (zdc_parse_parse(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005026
Gavin Howard01055ba2018-11-03 11:00:21 -06005027#endif // ENABLE_DC
5028
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005029static BC_STATUS zcommon_parse_expr(BcParse *p, uint8_t flags)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005030{
5031 if (IS_BC) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01005032 IF_BC(RETURN_STATUS(zbc_parse_expr(p, flags)));
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005033 } else {
Denys Vlasenko99b37622018-12-15 20:06:59 +01005034 IF_DC(RETURN_STATUS(zdc_parse_expr(p, flags)));
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005035 }
5036}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005037#define zcommon_parse_expr(...) (zcommon_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005038
Denys Vlasenkodf515392018-12-02 19:27:48 +01005039static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005040{
Gavin Howard01055ba2018-11-03 11:00:21 -06005041 BcId e, *ptr;
5042 BcVec *v, *map;
5043 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005044 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005045
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005046 v = var ? &G.prog.vars : &G.prog.arrs;
5047 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005048
5049 e.name = id;
5050 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005051 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005052
5053 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005054 BcVec v2;
5055 bc_array_init(&v2, var);
5056 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005057 }
5058
5059 ptr = bc_vec_item(map, i);
5060 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005061 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005062}
5063
Denys Vlasenko29301232018-12-11 15:29:32 +01005064static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005065{
Gavin Howard01055ba2018-11-03 11:00:21 -06005066 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005067 case BC_RESULT_STR:
5068 case BC_RESULT_TEMP:
5069 case BC_RESULT_IBASE:
5070 case BC_RESULT_SCALE:
5071 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005072 *num = &r->d.n;
5073 break;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005074 case BC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005075 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005076 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005077 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005078 size_t len;
5079
5080 str = *bc_program_const(r->d.id.idx);
5081 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005082
5083 bc_num_init(&r->d.n, len);
5084
5085 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005086 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005087 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005088
5089 if (s) {
5090 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005091 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005092 }
5093
5094 *num = &r->d.n;
5095 r->t = BC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005096 break;
5097 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005098 case BC_RESULT_VAR:
5099 case BC_RESULT_ARRAY:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005100 case BC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005101 BcVec *v;
5102
Denys Vlasenkodf515392018-12-02 19:27:48 +01005103 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005104
5105 if (r->t == BC_RESULT_ARRAY_ELEM) {
5106 v = bc_vec_top(v);
5107 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5108 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005109 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005110 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005111 break;
5112 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005113#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005114 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005115 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005116 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005117 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005118 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005119 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005120#endif
5121 default:
5122 // Testing the theory that dc does not reach LAST/ONE
5123 bb_error_msg_and_die("BUG:%d", r->t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005124 }
5125
Denys Vlasenko29301232018-12-11 15:29:32 +01005126 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005127}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005128#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005129
Denys Vlasenko29301232018-12-11 15:29:32 +01005130static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005131 BcResult **r, BcNum **rn, bool assign)
5132{
5133 BcStatus s;
5134 bool hex;
5135 BcResultType lt, rt;
5136
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005137 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005138 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005139
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005140 *r = bc_vec_item_rev(&G.prog.results, 0);
5141 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005142
5143 lt = (*l)->t;
5144 rt = (*r)->t;
5145 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5146
Denys Vlasenko29301232018-12-11 15:29:32 +01005147 s = zbc_program_num(*l, ln, false);
5148 if (s) RETURN_STATUS(s);
5149 s = zbc_program_num(*r, rn, hex);
5150 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005151
5152 // We run this again under these conditions in case any vector has been
5153 // reallocated out from under the BcNums or arrays we had.
5154 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005155 s = zbc_program_num(*l, ln, false);
5156 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005157 }
5158
5159 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005160 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005161 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005162 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005163
Denys Vlasenko29301232018-12-11 15:29:32 +01005164 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005165}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005166#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005167
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005168static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005169{
5170 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005171 bc_vec_pop(&G.prog.results);
5172 bc_vec_pop(&G.prog.results);
5173 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005174}
5175
Denys Vlasenko29301232018-12-11 15:29:32 +01005176static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005177{
5178 BcStatus s;
5179
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005180 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005181 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005182 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005183
Denys Vlasenko29301232018-12-11 15:29:32 +01005184 s = zbc_program_num(*r, n, false);
5185 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005186
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005187 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005188 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005189
Denys Vlasenko29301232018-12-11 15:29:32 +01005190 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005191}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005192#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005193
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005194static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005195{
5196 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005197 bc_vec_pop(&G.prog.results);
5198 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005199}
5200
Denys Vlasenko259137d2018-12-11 19:42:05 +01005201static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005202{
5203 BcStatus s;
5204 BcResult *opd1, *opd2, res;
5205 BcNum *n1, *n2 = NULL;
5206
Denys Vlasenko29301232018-12-11 15:29:32 +01005207 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005208 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005209 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005210
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005211 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005212 IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005213 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005214 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005215
Denys Vlasenko259137d2018-12-11 19:42:05 +01005216 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005217 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005218 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005219 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005220}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005221#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005222
Denys Vlasenko26819db2018-12-12 16:08:46 +01005223static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005224{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005225 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005226 BcStatus s;
5227 BcParse parse;
5228 BcVec buf;
5229 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005230 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005231
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005232 if (G.in_read)
Denys Vlasenko26819db2018-12-12 16:08:46 +01005233 RETURN_STATUS(bc_error_nested_read_call());
Gavin Howard01055ba2018-11-03 11:00:21 -06005234
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005235 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005236 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005237
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005238 sv_file = G.prog.file;
5239 G.prog.file = NULL;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005240 G.in_read = 1;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005241
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005242 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005243 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005244
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005245 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005246 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005247
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005248 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005249 if (s) goto exec_err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005250 s = zcommon_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005251 if (s) goto exec_err;
5252
5253 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005254 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005255 goto exec_err;
5256 }
5257
5258 ip.func = BC_PROG_READ;
5259 ip.idx = 0;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005260 IF_BC(ip.len = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005261
5262 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005263 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005264
5265 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005266 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005267 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005268 bc_parse_free(&parse);
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005269 G.in_read = 0;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005270 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005271 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005272 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005273}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005274#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005275
5276static size_t bc_program_index(char *code, size_t *bgn)
5277{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005278 unsigned char *bytes = (void*)(code + *bgn);
5279 unsigned amt;
5280 unsigned i;
5281 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005282
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005283 amt = *bytes++;
5284 *bgn += amt + 1;
5285
5286 amt *= 8;
5287 res = 0;
5288 for (i = 0; i < amt; i += 8)
5289 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005290
5291 return res;
5292}
5293
5294static char *bc_program_name(char *code, size_t *bgn)
5295{
5296 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005297 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005298
Denys Vlasenko694d2982018-12-18 19:17:11 +01005299 code += *bgn;
5300 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005301 i = 0;
5302 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005303 char c = *code++;
5304 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005305 break;
5306 s[i++] = c;
5307 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005308 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005309 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005310
5311 return s;
5312}
5313
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005314static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005315{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005316#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005317 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005318 // Example: echo '[]ap' | dc
5319 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005320 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005321 return;
5322 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005323#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005324 while (*str) {
5325 int c = *str++;
5326 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005327 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005328 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005329 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005330 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005331 case 'a':
5332 bb_putchar('\a');
5333 break;
5334 case 'b':
5335 bb_putchar('\b');
5336 break;
5337 case '\\':
5338 case 'e':
5339 bb_putchar('\\');
5340 break;
5341 case 'f':
5342 bb_putchar('\f');
5343 break;
5344 case 'n':
5345 bb_putchar('\n');
5346 G.prog.nchars = SIZE_MAX;
5347 break;
5348 case 'r':
5349 bb_putchar('\r');
5350 break;
5351 case 'q':
5352 bb_putchar('"');
5353 break;
5354 case 't':
5355 bb_putchar('\t');
5356 break;
5357 default:
5358 // Just print the backslash and following character.
5359 bb_putchar('\\');
5360 ++G.prog.nchars;
5361 bb_putchar(c);
5362 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005363 }
5364 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005365 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005366 }
5367}
5368
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005369static void bc_num_printNewline(void)
5370{
5371 if (G.prog.nchars == G.prog.len - 1) {
5372 bb_putchar('\\');
5373 bb_putchar('\n');
5374 G.prog.nchars = 0;
5375 }
5376}
5377
5378#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005379static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005380{
5381 (void) radix;
5382 bb_putchar((char) num);
5383 G.prog.nchars += width;
5384}
5385#endif
5386
5387static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5388{
5389 size_t exp, pow;
5390
5391 bc_num_printNewline();
5392 bb_putchar(radix ? '.' : ' ');
5393 ++G.prog.nchars;
5394
5395 bc_num_printNewline();
5396 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5397 continue;
5398
5399 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5400 size_t dig;
5401 bc_num_printNewline();
5402 dig = num / pow;
5403 num -= dig * pow;
5404 bb_putchar(((char) dig) + '0');
5405 }
5406}
5407
5408static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5409{
5410 if (radix) {
5411 bc_num_printNewline();
5412 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005413 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005414 }
5415
5416 bc_num_printNewline();
5417 bb_putchar(bb_hexdigits_upcase[num]);
5418 G.prog.nchars += width;
5419}
5420
5421static void bc_num_printDecimal(BcNum *n)
5422{
5423 size_t i, rdx = n->rdx - 1;
5424
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005425 if (n->neg) {
5426 bb_putchar('-');
5427 G.prog.nchars++;
5428 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005429
5430 for (i = n->len - 1; i < n->len; --i)
5431 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5432}
5433
Denys Vlasenkod3401432018-12-18 17:00:35 +01005434static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005435{
5436 BcStatus s;
5437 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005438 BcNum base;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005439 BcNum intp, fracp, digit, frac_len;
5440 unsigned long dig, *ptr;
5441 size_t i;
5442 bool radix;
5443
5444 if (n->len == 0) {
5445 print(0, width, false);
5446 RETURN_STATUS(BC_STATUS_SUCCESS);
5447 }
5448
5449 bc_vec_init(&stack, sizeof(long), NULL);
5450 bc_num_init(&intp, n->len);
5451 bc_num_init(&fracp, n->rdx);
5452 bc_num_init(&digit, width);
5453 bc_num_init(&frac_len, BC_NUM_INT(n));
5454 bc_num_copy(&intp, n);
5455 bc_num_one(&frac_len);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005456//TODO: have BcNumSmall type, with static buffer
5457 bc_num_init_DEF_SIZE(&base);
5458 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005459
5460 bc_num_truncate(&intp, intp.rdx);
5461 s = zbc_num_sub(n, &intp, &fracp, 0);
5462 if (s) goto err;
5463
5464 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005465 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005466 if (s) goto err;
5467 s = zbc_num_ulong(&digit, &dig);
5468 if (s) goto err;
5469 bc_vec_push(&stack, &dig);
5470 }
5471
5472 for (i = 0; i < stack.len; ++i) {
5473 ptr = bc_vec_item_rev(&stack, i);
5474 print(*ptr, width, false);
5475 }
5476
5477 if (!n->rdx) goto err;
5478
5479 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005480 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005481 if (s) goto err;
5482 s = zbc_num_ulong(&fracp, &dig);
5483 if (s) goto err;
5484 bc_num_ulong2num(&intp, dig);
5485 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5486 if (s) goto err;
5487 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005488 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005489 if (s) goto err;
5490 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005491 err:
Denys Vlasenkod3401432018-12-18 17:00:35 +01005492 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005493 bc_num_free(&frac_len);
5494 bc_num_free(&digit);
5495 bc_num_free(&fracp);
5496 bc_num_free(&intp);
5497 bc_vec_free(&stack);
5498 RETURN_STATUS(s);
5499}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005500#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005501
5502static BC_STATUS zbc_num_printBase(BcNum *n)
5503{
5504 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005505 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005506 BcNumDigitOp print;
5507 bool neg = n->neg;
5508
5509 if (neg) {
5510 bb_putchar('-');
5511 G.prog.nchars++;
5512 }
5513
5514 n->neg = false;
5515
5516 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5517 width = 1;
5518 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005519 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005520 unsigned i = G.prog.ob_t - 1;
5521 width = 0;
5522 for (;;) {
5523 width++;
5524 i /= 10;
5525 if (i == 0)
5526 break;
5527 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005528 print = bc_num_printDigits;
5529 }
5530
Denys Vlasenkod3401432018-12-18 17:00:35 +01005531 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005532 n->neg = neg;
5533
5534 RETURN_STATUS(s);
5535}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005536#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005537
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005538static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5539{
5540 BcStatus s = BC_STATUS_SUCCESS;
5541
5542 bc_num_printNewline();
5543
5544 if (n->len == 0) {
5545 bb_putchar('0');
5546 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005547 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005548 bc_num_printDecimal(n);
5549 else
5550 s = zbc_num_printBase(n);
5551
5552 if (newline) {
5553 bb_putchar('\n');
5554 G.prog.nchars = 0;
5555 }
5556
5557 RETURN_STATUS(s);
5558}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005559#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005560
Denys Vlasenko29301232018-12-11 15:29:32 +01005561static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005562{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005563 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005564 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005565 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005566 bool pop = inst != BC_INST_PRINT;
5567
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005568 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005569 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005570
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005571 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005572 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005573 s = zbc_program_num(r, &num, false);
5574 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005575
5576 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005577 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005578#if ENABLE_BC
5579 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5580#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005581 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005582 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005583
5584 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005585 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005586
5587 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005588 for (;;) {
5589 char c = *str++;
5590 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005591 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005592 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005593 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005594 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005595 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005596 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005597 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005598 }
5599 }
5600
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005601 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005602
Denys Vlasenko29301232018-12-11 15:29:32 +01005603 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005604}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005605#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005606
Denys Vlasenko29301232018-12-11 15:29:32 +01005607static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005608{
5609 BcStatus s;
5610 BcResult res, *ptr;
5611 BcNum *num = NULL;
5612
Denys Vlasenko29301232018-12-11 15:29:32 +01005613 s = zbc_program_prep(&ptr, &num);
5614 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005615
5616 bc_num_init(&res.d.n, num->len);
5617 bc_num_copy(&res.d.n, num);
5618 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5619
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005620 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005621
Denys Vlasenko29301232018-12-11 15:29:32 +01005622 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005623}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005624#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005625
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005626static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005627{
5628 BcStatus s;
5629 BcResult *opd1, *opd2, res;
5630 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005631 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005632
Denys Vlasenko29301232018-12-11 15:29:32 +01005633 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005634 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005635
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005636 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005637
5638 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005639 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005640 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005641 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005642 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005643 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005644 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005645 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005646 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005647 break;
5648 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005649 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005650 break;
5651 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005652 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005653 break;
5654 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005655 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005656 break;
5657 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005658 cond = (cond > 0);
5659 break;
5660 default: // = case BC_INST_REL_NE:
5661 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005662 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005663 }
5664 }
5665
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005666 if (cond) bc_num_one(&res.d.n);
5667 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005668
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005669 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005670
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005671 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005672}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005673#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005674
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005675#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005676static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005677{
5678 BcNum n2;
5679 BcResult res;
5680
5681 memset(&n2, 0, sizeof(BcNum));
5682 n2.rdx = res.d.id.idx = r->d.id.idx;
5683 res.t = BC_RESULT_STR;
5684
5685 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005686 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
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 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005689 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005690 }
5691
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005692 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005693
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005694 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005695 bc_vec_push(v, &n2);
5696
Denys Vlasenko29301232018-12-11 15:29:32 +01005697 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005698}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005699#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005700#endif // ENABLE_DC
5701
Denys Vlasenko29301232018-12-11 15:29:32 +01005702static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005703{
5704 BcStatus s;
5705 BcResult *ptr, r;
5706 BcVec *v;
5707 BcNum *n;
5708
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005709 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005710 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005711
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005712 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005713 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005714 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005715 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005716
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005717#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005718 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005719 RETURN_STATUS(bc_error_variable_is_wrong_type());
5720 if (ptr->t == BC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005721 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005722#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005723
Denys Vlasenko29301232018-12-11 15:29:32 +01005724 s = zbc_program_num(ptr, &n, false);
5725 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005726
5727 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005728 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005729
5730 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005731 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005732 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005733 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005734 bc_array_init(&r.d.v, true);
5735 bc_array_copy(&r.d.v, (BcVec *) n);
5736 }
5737
5738 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005739 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005740
Denys Vlasenko29301232018-12-11 15:29:32 +01005741 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005742}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005743#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005744
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005745static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005746{
5747 BcStatus s;
5748 BcResult *left, *right, res;
5749 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005750 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5751
Denys Vlasenko29301232018-12-11 15:29:32 +01005752 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005753 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005754
5755 ib = left->t == BC_RESULT_IBASE;
5756 sc = left->t == BC_RESULT_SCALE;
5757
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005758#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005759 if (right->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005760 BcVec *v;
5761
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005762 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005763 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005764 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005765
Denys Vlasenkofa210792018-12-19 19:35:40 +01005766 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005767 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005768#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005769
5770 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005771 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005772 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005773 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005774 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005775
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005776#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005777 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005778 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005779
5780 if (assign)
5781 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005782 else {
5783 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005784 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 +01005785 }
5786 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005787#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005788 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005789#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005790
5791 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005792 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005793 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5794 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5795 NULL, //can't happen //BC_RESULT_LAST
5796 NULL, //can't happen //BC_RESULT_CONSTANT
5797 NULL, //can't happen //BC_RESULT_ONE
5798 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005799 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005800 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005801 size_t max;
5802 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005803
Denys Vlasenko29301232018-12-11 15:29:32 +01005804 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005805 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005806 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005807 if (sc) {
5808 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005809 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005810 } else {
5811 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005812 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005813 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005814 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005815 }
5816
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005817 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005818 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005819
5820 *ptr = (size_t) val;
5821 s = BC_STATUS_SUCCESS;
5822 }
5823
5824 bc_num_init(&res.d.n, l->len);
5825 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005826 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005827
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005828 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005829}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005830#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005831
Denys Vlasenko416ce762018-12-02 20:57:17 +01005832#if !ENABLE_DC
5833#define bc_program_pushVar(code, bgn, pop, copy) \
5834 bc_program_pushVar(code, bgn)
5835// for bc, 'pop' and 'copy' are always false
5836#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005837static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005838 bool pop, bool copy)
5839{
Gavin Howard01055ba2018-11-03 11:00:21 -06005840 BcResult r;
5841 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005842
5843 r.t = BC_RESULT_VAR;
5844 r.d.id.name = name;
5845
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005846#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005847 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005848 BcVec *v = bc_program_search(name, true);
5849 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005850
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005851 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005852 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005853 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005854 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005855
5856 if (!BC_PROG_STR(num)) {
5857 r.t = BC_RESULT_TEMP;
5858 bc_num_init_DEF_SIZE(&r.d.n);
5859 bc_num_copy(&r.d.n, num);
5860 } else {
5861 r.t = BC_RESULT_STR;
5862 r.d.id.idx = num->rdx;
5863 }
5864
5865 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005866 }
5867#endif // ENABLE_DC
5868
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005869 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005870
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005871 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005872}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005873#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005874
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005875static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005876{
5877 BcStatus s = BC_STATUS_SUCCESS;
5878 BcResult r;
5879 BcNum *num;
5880
5881 r.d.id.name = bc_program_name(code, bgn);
5882
5883 if (inst == BC_INST_ARRAY) {
5884 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005885 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005886 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005887 BcResult *operand;
5888 unsigned long temp;
5889
Denys Vlasenko29301232018-12-11 15:29:32 +01005890 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005891 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005892 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005893 if (s) goto err;
5894
5895 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005896 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005897 goto err;
5898 }
5899
5900 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005901 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005902 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005903 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005904 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005905 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005906}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005907#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005908
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005909#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005910static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005911{
5912 BcStatus s;
5913 BcResult *ptr, res, copy;
5914 BcNum *num = NULL;
5915 char inst2 = inst;
5916
Denys Vlasenko29301232018-12-11 15:29:32 +01005917 s = zbc_program_prep(&ptr, &num);
5918 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005919
5920 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5921 copy.t = BC_RESULT_TEMP;
5922 bc_num_init(&copy.d.n, num->len);
5923 bc_num_copy(&copy.d.n, num);
5924 }
5925
5926 res.t = BC_RESULT_ONE;
5927 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
5928 BC_INST_ASSIGN_PLUS :
5929 BC_INST_ASSIGN_MINUS;
5930
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005931 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005932 s = zbc_program_assign(inst);
5933 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005934
5935 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005936 bc_vec_pop(&G.prog.results);
5937 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005938 }
5939
Denys Vlasenko29301232018-12-11 15:29:32 +01005940 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005941}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005942#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005943
Denys Vlasenko29301232018-12-11 15:29:32 +01005944static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005945{
Gavin Howard01055ba2018-11-03 11:00:21 -06005946 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005947 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005948 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005949 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005950 BcResult *arg;
5951
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005952 nparams = bc_program_index(code, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005953 ip.idx = 0;
5954 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005955 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005956
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005957 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005958 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005959 }
5960 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005961 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005962 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005963 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005964
5965 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005966 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005967
5968 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005969 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005970
5971 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005972 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005973
Denys Vlasenko29301232018-12-11 15:29:32 +01005974 s = zbc_program_copyToVar(a->name, a->idx);
5975 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005976 }
5977
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005978 a = bc_vec_item(&func->autos, i);
5979 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005980 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005981
Denys Vlasenkodf515392018-12-02 19:27:48 +01005982 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005983 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005984 BcNum n2;
5985 bc_num_init_DEF_SIZE(&n2);
5986 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005987 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005988 BcVec v2;
5989 bc_array_init(&v2, true);
5990 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005991 }
5992 }
5993
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005994 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005995
Denys Vlasenko29301232018-12-11 15:29:32 +01005996 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005997}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005998#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005999
Denys Vlasenko29301232018-12-11 15:29:32 +01006000static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006001{
Gavin Howard01055ba2018-11-03 11:00:21 -06006002 BcResult res;
6003 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006004 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06006005 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006006 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006007
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006008 if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->len + (inst == BC_INST_RET)))
Denys Vlasenko29301232018-12-11 15:29:32 +01006009 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006010
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006011 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006012 res.t = BC_RESULT_TEMP;
6013
6014 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006015 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006016 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006017 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006018
Denys Vlasenko29301232018-12-11 15:29:32 +01006019 s = zbc_program_num(operand, &num, false);
6020 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006021 bc_num_init(&res.d.n, num->len);
6022 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006023 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006024 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006025 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006026 }
6027
6028 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01006029 a = (void*)f->autos.v;
6030 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006031 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01006032 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006033 bc_vec_pop(v);
6034 }
6035
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006036 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6037 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006038 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006039
Denys Vlasenko29301232018-12-11 15:29:32 +01006040 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006041}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006042#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006043#endif // ENABLE_BC
6044
6045static unsigned long bc_program_scale(BcNum *n)
6046{
6047 return (unsigned long) n->rdx;
6048}
6049
6050static unsigned long bc_program_len(BcNum *n)
6051{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006052 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006053
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006054 if (n->rdx != len) return len;
6055 for (;;) {
6056 if (len == 0) break;
6057 len--;
6058 if (n->num[len] != 0) break;
6059 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006060 return len;
6061}
6062
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006063static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006064{
6065 BcStatus s;
6066 BcResult *opnd;
6067 BcNum *num = NULL;
6068 BcResult res;
6069 bool len = inst == BC_INST_LENGTH;
6070
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006071 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006072 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006073 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006074
Denys Vlasenko29301232018-12-11 15:29:32 +01006075 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006076 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006077
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006078#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006079 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006080 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006081#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006082
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006083 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006084
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006085 if (inst == BC_INST_SQRT)
6086 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006087#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006088 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006089 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006090 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006091#endif
6092#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006093 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006094 char **str;
6095 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6096
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006097 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006098 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006099 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006100#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006101 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006102 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006103 }
6104
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006105 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006106
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006107 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006108}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006109#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006110
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006111#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006112static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006113{
6114 BcStatus s;
6115 BcResult *opd1, *opd2, res, res2;
6116 BcNum *n1, *n2 = NULL;
6117
Denys Vlasenko29301232018-12-11 15:29:32 +01006118 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006119 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006120
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006121 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006122 bc_num_init(&res2.d.n, n2->len);
6123
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006124 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006125 if (s) goto err;
6126
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006127 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006128 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006129 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006130
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006131 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006132 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006133 bc_num_free(&res2.d.n);
6134 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006135 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006136}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006137#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006138
Denys Vlasenkofa210792018-12-19 19:35:40 +01006139static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006140{
6141 BcStatus s;
6142 BcResult *r1, *r2, *r3, res;
6143 BcNum *n1, *n2, *n3;
6144
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006145 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006146 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006147 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006148 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006149
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006150 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006151 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006152 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006153 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006154 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006155
6156 // Make sure that the values have their pointers updated, if necessary.
6157 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006158 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006159 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006160 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006161 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006162 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006163 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006164 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006165 }
6166 }
6167
6168 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006169 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006170 if (s) goto err;
6171
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006172 bc_vec_pop(&G.prog.results);
6173 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006174
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006175 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006176 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006177 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006178 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006179}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006180#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006181
Denys Vlasenkofa210792018-12-19 19:35:40 +01006182static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006183{
Gavin Howard01055ba2018-11-03 11:00:21 -06006184 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006185 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006186
6187 res.t = BC_RESULT_TEMP;
6188
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006189 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006190 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006191 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006192}
6193
Denys Vlasenkofa210792018-12-19 19:35:40 +01006194static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006195{
6196 BcStatus s;
6197 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006198 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006199 char **strs;
6200 char *str;
6201 char c;
6202 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006203 unsigned long val;
6204
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006205 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006206 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006207 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006208
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006209 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006210 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006211 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006212
6213 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01006214 BcNum strmb;
6215
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006216 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006217 bc_num_copy(&n, num);
6218 bc_num_truncate(&n, n.rdx);
6219
Denys Vlasenkod3401432018-12-18 17:00:35 +01006220//TODO: have BcNumSmall type, with static buffer
6221 bc_num_init_DEF_SIZE(&strmb);
6222 bc_num_ulong2num(&strmb, 0x100);
6223 s = zbc_num_mod(&n, &strmb, &n, 0);
6224 bc_num_free(&strmb);
6225
Gavin Howard01055ba2018-11-03 11:00:21 -06006226 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006227 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006228 if (s) goto num_err;
6229
6230 c = (char) val;
6231
6232 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006233 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006234 char *sp;
Gavin Howard01055ba2018-11-03 11:00:21 -06006235 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006236 sp = *bc_program_str(idx);
6237 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006238 }
6239
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006240 strs = (void*)G.prog.strs.v;
6241 for (idx = 0; idx < G.prog.strs.len; idx++) {
6242 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6243 goto dup;
6244 }
6245 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006246 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006247 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006248 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006249 bc_vec_push(&G.prog.strs, &str);
6250 dup:
Gavin Howard01055ba2018-11-03 11:00:21 -06006251 res.t = BC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006252 res.d.id.idx = idx;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006253 bc_vec_pop(&G.prog.results);
6254 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006255
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006256 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006257 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006258 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006259 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006260}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006261#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006262
Denys Vlasenkofa210792018-12-19 19:35:40 +01006263static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006264{
6265 BcStatus s;
6266 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006267 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006268 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006269
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006270 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006271 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006272 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006273
Denys Vlasenkofa210792018-12-19 19:35:40 +01006274 n = NULL; // is this needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006275 s = zbc_program_num(r, &n, false);
6276 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006277
Denys Vlasenko57734c92018-12-17 21:14:05 +01006278 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006279 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006280 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006281 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06006282 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006283 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006284 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006285 }
6286
Denys Vlasenko29301232018-12-11 15:29:32 +01006287 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006288}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006289#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006290
Denys Vlasenkofa210792018-12-19 19:35:40 +01006291static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006292{
6293 BcStatus s;
6294 BcResult *opnd;
6295 BcNum *num = NULL;
6296 unsigned long val;
6297
Denys Vlasenko29301232018-12-11 15:29:32 +01006298 s = zbc_program_prep(&opnd, &num);
6299 if (s) RETURN_STATUS(s);
6300 s = zbc_num_ulong(num, &val);
6301 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006302
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006303 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006304
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006305 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006306 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006307 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006308 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006309 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006310
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006311 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006312
Denys Vlasenko29301232018-12-11 15:29:32 +01006313 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006314}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006315#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006316
Denys Vlasenkofa210792018-12-19 19:35:40 +01006317static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006318{
6319 BcStatus s = BC_STATUS_SUCCESS;
6320 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006321 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006322 BcInstPtr ip;
6323 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006324
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006325 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006326 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006327
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006328 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006329
6330 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006331 BcNum *n = n; // for compiler
6332 bool exec;
6333 char *name;
6334 char *then_name = bc_program_name(code, bgn);
6335 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006336
6337 if (code[*bgn] == BC_PARSE_STREND)
6338 (*bgn) += 1;
6339 else
6340 else_name = bc_program_name(code, bgn);
6341
6342 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006343 name = then_name;
6344 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006345 exec = true;
6346 name = else_name;
6347 }
6348
6349 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006350 BcVec *v;
6351 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006352 n = bc_vec_top(v);
6353 }
6354
6355 free(then_name);
6356 free(else_name);
6357
6358 if (!exec) goto exit;
6359 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006360 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006361 goto exit;
6362 }
6363
6364 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006365 } else {
6366 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006367 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006368 } else if (r->t == BC_RESULT_VAR) {
6369 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006370 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006371 if (s || !BC_PROG_STR(n)) goto exit;
6372 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006373 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006374 goto exit;
6375 }
6376
6377 fidx = sidx + BC_PROG_REQ_FUNCS;
6378
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006379 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006380
6381 if (f->code.len == 0) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006382 BcParse prs;
6383 char *str;
6384
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006385 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006386 str = *bc_program_str(sidx);
6387 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006388 if (s) goto err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01006389 s = zcommon_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006390 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06006391 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006392 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006393 err:
6394 bc_parse_free(&prs);
6395 bc_vec_pop_all(&f->code);
6396 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006397 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006398 bc_parse_free(&prs);
6399 }
6400
6401 ip.idx = 0;
Denys Vlasenko503faf92018-12-20 16:24:18 +01006402 IF_BC(ip.len = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 ip.func = fidx;
6404
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006405 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006406 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006407
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006408 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006409 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006410 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006411 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006412}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006413#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006414#endif // ENABLE_DC
6415
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006416static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006417{
Gavin Howard01055ba2018-11-03 11:00:21 -06006418 BcResult res;
6419 unsigned long val;
6420
6421 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6422 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006423 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006424 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006425 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006426 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006427 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006428
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006429 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006430 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006431 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006432}
6433
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006434static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006435{
Gavin Howard01055ba2018-11-03 11:00:21 -06006436 BcResult r, *ptr;
6437 BcNum *num;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006438 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006439 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006440 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006441
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006442 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
6443 ip->func, func->code.len, ip->idx, G.prog.results.len);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006444 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006445 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006446 char inst = code[ip->idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006447
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006448 dbg_exec("inst at %zd:%d results.len:%d", ip->idx - 1, inst, G.prog.results.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006449 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006450#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006451 case BC_INST_JUMP_ZERO: {
6452 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006453 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006454 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006455 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006456 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006457 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006458 if (!zero) {
6459 bc_program_index(code, &ip->idx);
6460 break;
6461 }
6462 // else: fall through
6463 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006464 case BC_INST_JUMP: {
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006465 size_t idx = bc_program_index(code, &ip->idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006466 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006467 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006468 ip->idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006469 break;
6470 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006471 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006472 dbg_exec("BC_INST_CALL:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006473 s = zbc_program_call(code, &ip->idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006474 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006475 case BC_INST_INC_PRE:
6476 case BC_INST_DEC_PRE:
6477 case BC_INST_INC_POST:
6478 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006479 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006480 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006481 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006482 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006483 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006484 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006485 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006486 case BC_INST_RET:
6487 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006488 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006489 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006490 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006491 case BC_INST_BOOL_OR:
6492 case BC_INST_BOOL_AND:
6493#endif // ENABLE_BC
6494 case BC_INST_REL_EQ:
6495 case BC_INST_REL_LE:
6496 case BC_INST_REL_GE:
6497 case BC_INST_REL_NE:
6498 case BC_INST_REL_LT:
6499 case BC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006500 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006501 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006502 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006503 case BC_INST_READ:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006504 dbg_exec("BC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006505 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006506 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006507 case BC_INST_VAR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006508 dbg_exec("BC_INST_VAR:");
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006509 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006510 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006511 case BC_INST_ARRAY_ELEM:
6512 case BC_INST_ARRAY:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006513 dbg_exec("BC_INST_ARRAY[_ELEM]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006514 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006515 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006516 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006517 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006518 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006519 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006520 case BC_INST_IBASE:
6521 case BC_INST_SCALE:
6522 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006523 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006524 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006525 case BC_INST_SCALE_FUNC:
6526 case BC_INST_LENGTH:
6527 case BC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006528 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006529 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006530 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006531 case BC_INST_NUM:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006532 dbg_exec("BC_INST_NUM:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006533 r.t = BC_RESULT_CONSTANT;
6534 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006535 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006536 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006537 case BC_INST_POP:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006538 dbg_exec("BC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006539 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006540 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006541 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006542 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006543 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006544 case BC_INST_POP_EXEC:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006545 dbg_exec("BC_INST_POP_EXEC:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006546 bc_vec_pop(&G.prog.exestack);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006547 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006548 case BC_INST_PRINT:
6549 case BC_INST_PRINT_POP:
6550 case BC_INST_PRINT_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006551 dbg_exec("BC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006552 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006553 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006554 case BC_INST_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006555 dbg_exec("BC_INST_STR:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006556 r.t = BC_RESULT_STR;
6557 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006558 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006559 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006560 case BC_INST_POWER:
6561 case BC_INST_MULTIPLY:
6562 case BC_INST_DIVIDE:
6563 case BC_INST_MODULUS:
6564 case BC_INST_PLUS:
6565 case BC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006566 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006567 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006568 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006569 case BC_INST_BOOL_NOT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006570 dbg_exec("BC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006571 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006572 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006573 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006574 if (!bc_num_cmp(num, &G.prog.zero))
6575 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006576 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006577 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006578 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006579 case BC_INST_NEG:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006580 dbg_exec("BC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006581 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006582 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006583#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006584 case BC_INST_ASSIGN_POWER:
6585 case BC_INST_ASSIGN_MULTIPLY:
6586 case BC_INST_ASSIGN_DIVIDE:
6587 case BC_INST_ASSIGN_MODULUS:
6588 case BC_INST_ASSIGN_PLUS:
6589 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006590#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006591 case BC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006592 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006593 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006594 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006595#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006596 case BC_INST_MODEXP:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006597 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006598 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006599 case BC_INST_DIVMOD:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006600 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006601 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006602 case BC_INST_EXECUTE:
6603 case BC_INST_EXEC_COND:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006604 s = zdc_program_execStr(code, &ip->idx, inst == BC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006605 goto read_updated_ip;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006606 case BC_INST_PRINT_STACK: {
6607 size_t idx;
6608 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006609 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006610 if (s) break;
6611 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006612 break;
6613 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006614 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006615 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006616 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006617 case BC_INST_STACK_LEN:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006618 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006619 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006620 case BC_INST_DUPLICATE:
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006621 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006622 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006623 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006624 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006625 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006626 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006627 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006628 BcResult *ptr2;
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006629 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006630 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006631 ptr = bc_vec_item_rev(&G.prog.results, 0);
6632 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006633 memcpy(&r, ptr, sizeof(BcResult));
6634 memcpy(ptr, ptr2, sizeof(BcResult));
6635 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006636 break;
6637 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006638 case BC_INST_ASCIIFY:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006639 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006640 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006641 case BC_INST_PRINT_STREAM:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006642 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006643 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006644 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006645 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006646 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006647 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006648 break;
6649 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006650 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006651 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006652 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006653 free(name);
6654 break;
6655 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006656 case BC_INST_QUIT:
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006657 dbg_exec("BC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006658 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006659 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006660 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006661 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006662 case BC_INST_NQUIT:
Denys Vlasenkofa210792018-12-19 19:35:40 +01006663 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006664 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006665#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006666 read_updated_ip:
6667 // Instruction stack has changed, read new pointers
6668 ip = bc_vec_top(&G.prog.exestack);
6669 func = bc_program_func(ip->func);
6670 code = func->code.v;
6671 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006672 }
6673
Denys Vlasenkod38af482018-12-04 19:11:02 +01006674 if (s || G_interrupt) {
6675 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006676 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006677 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006678
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006679 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006680 }
6681
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006682 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006683}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006684#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006685
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006686static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006687{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006688 char *lenv;
6689 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006690
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006691 lenv = getenv(var);
6692 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006693 if (!lenv) return len;
6694
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006695 len = bb_strtou(lenv, NULL, 10) - 1;
6696 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006697 len = BC_NUM_PRINT_WIDTH;
6698
6699 return len;
6700}
6701
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006702static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006703{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006704 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006705
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006706 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
6707 s = zbc_parse_text_init(&G.prs, text);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006708 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006709
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006710 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006711 dbg_lex("%s:%d G.prs.l.t.t:%d", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenkoec603182018-12-17 10:34:02 +01006712 s = zcommon_parse(&G.prs);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006713 if (s) RETURN_STATUS(s);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006714 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006715 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006716 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006717 break;
6718 }
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006719 // bc discards strings, constants and code after each
6720 // top-level statement in the "main program".
6721 // This prevents "yes 1 | bc" from growing its memory
6722 // without bound. This can be done because data stack
6723 // is empty and thus can't hold any references to
6724 // strings or constants, there is no generated code
6725 // which can hold references (after we discard one
6726 // we just executed). Code of functions can have references,
6727 // but bc stores function strings/constants in per-function
6728 // storage.
6729 if (IS_BC) {
6730 BcFunc *f;
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006731 BcInstPtr *ip = (void*)G.prog.exestack.v;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006732
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006733#if SANITY_CHECKS
6734 if (G.prog.results.len != 0)
6735 bb_error_msg_and_die("data stack not empty: %d slots", G.prog.results.len);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006736 if (G.prog.exestack.len != 1) // should be empty
6737 bb_error_msg_and_die("BUG:call stack");
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006738 if (ip->func != BC_PROG_MAIN)
6739 bb_error_msg_and_die("BUG:not MAIN");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006740#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006741//bb_error_msg("ip->func:%d >idx:%d >len:%d", ip->func, ip->idx, ip->len);
6742 f = bc_program_func_BC_PROG_MAIN();
6743//bb_error_msg("MAIN->code.len:%d >strs.len:%d >consts.len:%d", f->code.len, f->strs.len, f->consts.len); // labels, autos, nparams
6744 bc_vec_pop_all(&f->code);
6745 ip->idx = 0;
6746 IF_BC(bc_vec_pop_all(&f->strs);)
6747 IF_BC(bc_vec_pop_all(&f->consts);)
6748 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006749 }
6750
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006751 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006752 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006753}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006754#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006755
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006756static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006757{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006758 // So far bc/dc have no way to include a file from another file,
6759 // therefore we know G.prog.file == NULL on entry
6760 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006761 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006762
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006763 G.prog.file = filename;
6764 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006765 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006766
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006767 do {
6768 s = zbc_vm_process("");
6769 // We do not stop looping on errors here if reading stdin.
6770 // Example: start interactive bc and enter "return".
6771 // It should say "'return' not in a function"
6772 // but should not exit.
6773 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006774 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006775 RETURN_STATUS(s);
6776}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006777#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006778
6779static BC_STATUS zbc_vm_file(const char *file)
6780{
6781 BcStatus s;
6782 FILE *fp;
6783
6784 fp = xfopen_for_read(file);
6785 s = zbc_vm_execute_FILE(fp, file);
6786 fclose(fp);
6787
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006788 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006789}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006790#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006791
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006792#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006793static void bc_vm_info(void)
6794{
6795 printf("%s "BB_VER"\n"
6796 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6797 , applet_name);
6798}
6799
6800static void bc_args(char **argv)
6801{
6802 unsigned opts;
6803 int i;
6804
6805 GETOPT_RESET();
6806#if ENABLE_FEATURE_BC_LONG_OPTIONS
6807 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6808 "warn\0" No_argument "w"
6809 "version\0" No_argument "v"
6810 "standard\0" No_argument "s"
6811 "quiet\0" No_argument "q"
6812 "mathlib\0" No_argument "l"
6813 "interactive\0" No_argument "i"
6814 );
6815#else
6816 opts = option_mask32 |= getopt32(argv, "wvsqli");
6817#endif
6818 if (getenv("POSIXLY_CORRECT"))
6819 option_mask32 |= BC_FLAG_S;
6820
6821 if (opts & BC_FLAG_V) {
6822 bc_vm_info();
6823 exit(0);
6824 }
6825
6826 for (i = optind; argv[i]; ++i)
6827 bc_vec_push(&G.files, argv + i);
6828}
6829
6830static void bc_vm_envArgs(void)
6831{
6832 BcVec v;
6833 char *buf;
6834 char *env_args = getenv("BC_ENV_ARGS");
6835
6836 if (!env_args) return;
6837
6838 G.env_args = xstrdup(env_args);
6839 buf = G.env_args;
6840
6841 bc_vec_init(&v, sizeof(char *), NULL);
6842
6843 while (*(buf = skip_whitespace(buf)) != '\0') {
6844 bc_vec_push(&v, &buf);
6845 buf = skip_non_whitespace(buf);
6846 if (!*buf)
6847 break;
6848 *buf++ = '\0';
6849 }
6850
6851 // NULL terminate, and pass argv[] so that first arg is argv[1]
6852 if (sizeof(int) == sizeof(char*)) {
6853 bc_vec_push(&v, &const_int_0);
6854 } else {
6855 static char *const nullptr = NULL;
6856 bc_vec_push(&v, &nullptr);
6857 }
6858 bc_args(((char **)v.v) - 1);
6859
6860 bc_vec_free(&v);
6861}
6862
6863static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006864 "scale=20"
6865"\n" "define e(x){"
6866"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006867////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6868//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6869//e(-.998896): GNU:.36828580434569428695
6870// above code:.36828580434569428696
6871// actual value:.3682858043456942869594...
6872// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006873"\n" "b=ibase"
6874"\n" "ibase=A"
6875"\n" "if(x<0){"
6876"\n" "n=1"
6877"\n" "x=-x"
6878"\n" "}"
6879"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006880"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006881"\n" "scale=scale(x)+1"
6882"\n" "while(x>1){"
6883"\n" "d+=1"
6884"\n" "x/=2"
6885"\n" "scale+=1"
6886"\n" "}"
6887"\n" "scale=r"
6888"\n" "r=x+1"
6889"\n" "p=x"
6890"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006891"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006892"\n" "p*=x"
6893"\n" "f*=i"
6894"\n" "v=p/f"
6895"\n" "r+=v"
6896"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006897"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006898"\n" "scale=s"
6899"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006900"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006901"\n" "return(r/1)"
6902"\n" "}"
6903"\n" "define l(x){"
6904"\n" "auto b,s,r,p,a,q,i,v"
6905"\n" "b=ibase"
6906"\n" "ibase=A"
6907"\n" "if(x<=0){"
6908"\n" "r=(1-10^scale)/1"
6909"\n" "ibase=b"
6910"\n" "return(r)"
6911"\n" "}"
6912"\n" "s=scale"
6913"\n" "scale+=6"
6914"\n" "p=2"
6915"\n" "while(x>=2){"
6916"\n" "p*=2"
6917"\n" "x=sqrt(x)"
6918"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006919"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006920"\n" "p*=2"
6921"\n" "x=sqrt(x)"
6922"\n" "}"
6923"\n" "r=a=(x-1)/(x+1)"
6924"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006925"\n" "v=1"
6926"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006927"\n" "a*=q"
6928"\n" "v=a/i"
6929"\n" "r+=v"
6930"\n" "}"
6931"\n" "r*=p"
6932"\n" "scale=s"
6933"\n" "ibase=b"
6934"\n" "return(r/1)"
6935"\n" "}"
6936"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006937"\n" "auto b,s,r,a,q,i"
6938"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006939"\n" "b=ibase"
6940"\n" "ibase=A"
6941"\n" "s=scale"
6942"\n" "scale=1.1*s+2"
6943"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006944"\n" "scale=0"
6945"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006946"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006947"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006948"\n" "scale=s+2"
6949"\n" "r=a=x"
6950"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006951"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006952"\n" "a*=q/(i*(i-1))"
6953"\n" "r+=a"
6954"\n" "}"
6955"\n" "scale=s"
6956"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006957"\n" "return(r/1)"
6958"\n" "}"
6959"\n" "define c(x){"
6960"\n" "auto b,s"
6961"\n" "b=ibase"
6962"\n" "ibase=A"
6963"\n" "s=scale"
6964"\n" "scale*=1.2"
6965"\n" "x=s(2*a(1)+x)"
6966"\n" "scale=s"
6967"\n" "ibase=b"
6968"\n" "return(x/1)"
6969"\n" "}"
6970"\n" "define a(x){"
6971"\n" "auto b,s,r,n,a,m,t,f,i,u"
6972"\n" "b=ibase"
6973"\n" "ibase=A"
6974"\n" "n=1"
6975"\n" "if(x<0){"
6976"\n" "n=-1"
6977"\n" "x=-x"
6978"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006979"\n" "if(scale<65){"
6980"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
6981"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006982"\n" "}"
6983"\n" "s=scale"
6984"\n" "if(x>.2){"
6985"\n" "scale+=5"
6986"\n" "a=a(.2)"
6987"\n" "}"
6988"\n" "scale=s+3"
6989"\n" "while(x>.2){"
6990"\n" "m+=1"
6991"\n" "x=(x-.2)/(1+.2*x)"
6992"\n" "}"
6993"\n" "r=u=x"
6994"\n" "f=-x*x"
6995"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006996"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006997"\n" "u*=f"
6998"\n" "t=u/i"
6999"\n" "r+=t"
7000"\n" "}"
7001"\n" "scale=s"
7002"\n" "ibase=b"
7003"\n" "return((m*a+r)/n)"
7004"\n" "}"
7005"\n" "define j(n,x){"
7006"\n" "auto b,s,o,a,i,v,f"
7007"\n" "b=ibase"
7008"\n" "ibase=A"
7009"\n" "s=scale"
7010"\n" "scale=0"
7011"\n" "n/=1"
7012"\n" "if(n<0){"
7013"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007014"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007015"\n" "}"
7016"\n" "a=1"
7017"\n" "for(i=2;i<=n;++i)a*=i"
7018"\n" "scale=1.5*s"
7019"\n" "a=(x^n)/2^n/a"
7020"\n" "r=v=1"
7021"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007022"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007023"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007024"\n" "v=v*f/i/(n+i)"
7025"\n" "r+=v"
7026"\n" "}"
7027"\n" "scale=s"
7028"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007029"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007030"\n" "return(a*r/1)"
7031"\n" "}"
7032};
7033#endif // ENABLE_BC
7034
Denys Vlasenko19f11072018-12-12 22:48:19 +01007035static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007036{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007037 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007038 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007039 size_t i;
7040
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007041#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007042 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007043 // We know that internal library is not buggy,
7044 // thus error checking is normally disabled.
7045# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007046 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007047 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007048 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007049 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007050#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007051
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007052 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007053 fname = (void*)G.files.v;
7054 for (i = 0; i < G.files.len; i++) {
7055 s = zbc_vm_file(*fname++);
7056 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7057 // Debug config, non-interactive mode:
7058 // return all the way back to main.
7059 // Non-debug builds do not come here
7060 // in non-interactive mode, they exit.
7061 RETURN_STATUS(s);
7062 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007063 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007064
Denys Vlasenko91cde952018-12-10 20:56:08 +01007065 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007066 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007067
Denys Vlasenko19f11072018-12-12 22:48:19 +01007068 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007069}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007070#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007071
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007072#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007073static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007074{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007075 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007076 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007077 bc_vec_free(&G.prog.vars);
7078 bc_vec_free(&G.prog.var_map);
7079 bc_vec_free(&G.prog.arrs);
7080 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007081 IF_DC(bc_vec_free(&G.prog.strs);)
7082 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007083 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007084 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007085 IF_BC(bc_num_free(&G.prog.last);)
7086 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007087 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007088 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007089}
7090
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007091static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007092{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007093 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007094 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007095 bc_parse_free(&G.prs);
7096 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007097}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007098#endif
7099
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007100static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007101{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007102 BcInstPtr ip;
7103
Denys Vlasenko82269122018-12-14 16:30:56 +01007104 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007105 memset(&ip, 0, sizeof(BcInstPtr));
7106
Denys Vlasenko82269122018-12-14 16:30:56 +01007107 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007108 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007109 G.prog.ob_t = 10;
7110
Denys Vlasenko503faf92018-12-20 16:24:18 +01007111 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7112 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007113
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007114 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007115 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007116
Denys Vlasenko503faf92018-12-20 16:24:18 +01007117 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7118 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007119
7120 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007121 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007122
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007123 if (IS_BC) {
Denys Vlasenko04715442018-12-21 00:10:26 +01007124 // Names are chosen simply to never match
7125 // a valid function name (and be short)
7126 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
7127 IF_BC(bc_program_addFunc(xstrdup(""))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007128 } else {
7129 bc_program_add_fn();
7130 bc_program_add_fn();
7131 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007132
7133 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007134 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007135
7136 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007137 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007138
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007139 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7140 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007141 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007142 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7143 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007144
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007145 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007146}
Gavin Howard01055ba2018-11-03 11:00:21 -06007147
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007148static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007149{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007150#if ENABLE_FEATURE_EDITING
7151 G.line_input_state = new_line_input_t(DO_HISTORY);
7152#endif
7153 G.prog.len = bc_vm_envLen(env_len);
7154
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007155 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007156 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007157 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007158 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007159
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007160//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7161//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007162 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007163#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007164 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007165 // With SA_RESTART, most system calls will restart
7166 // (IOW: they won't fail with EINTR).
7167 // In particular, this means ^C won't cause
7168 // stdout to get into "error state" if SIGINT hits
7169 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007170 //
7171 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007172 // will only be handled after [Enter] since read()
7173 // from stdin is not interrupted by ^C either,
7174 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007175 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007176 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7177
7178 // Without SA_RESTART, this exhibits a bug:
7179 // "while (1) print 1" and try ^C-ing it.
7180 // Intermittently, instead of returning to input line,
7181 // you'll get "output error: Interrupted system call"
7182 // and exit.
7183 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007184#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007185 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007186 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007187 return 0; // "not a tty"
7188}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007189
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007190static BcStatus bc_vm_run(void)
7191{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007192 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007193#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007194 if (G_exiting) // it was actually "halt" or "quit"
7195 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007196 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007197# if ENABLE_FEATURE_EDITING
7198 free_line_input_t(G.line_input_state);
7199# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007200 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007201#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007202 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007203 return st;
7204}
7205
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007206#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007207int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007208int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007209{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007210 int is_tty;
7211
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007212 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007213
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007214 is_tty = bc_vm_init("BC_LINE_LENGTH");
7215
7216 bc_args(argv);
7217
7218 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7219 bc_vm_info();
7220
7221 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007222}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007223#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007224
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007225#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007226int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007227int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007228{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007229 int noscript;
7230
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007231 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007232
7233 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7234 // 1 char wider than bc from the same package.
7235 // Both default width, and xC_LINE_LENGTH=N are wider:
7236 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007237 // |1234\ |
7238 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007239 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007240 // |123\ |
7241 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007242 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007243 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007244
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007245 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7246 noscript = BC_FLAG_I;
7247 for (;;) {
7248 int n = getopt(argc, argv, "e:f:x");
7249 if (n <= 0)
7250 break;
7251 switch (n) {
7252 case 'e':
7253 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007254 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007255 if (n) return n;
7256 break;
7257 case 'f':
7258 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007259 n = zbc_vm_file(optarg);
7260 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007261 break;
7262 case 'x':
7263 option_mask32 |= DC_FLAG_X;
7264 break;
7265 default:
7266 bb_show_usage();
7267 }
7268 }
7269 argv += optind;
7270
7271 while (*argv) {
7272 noscript = 0;
7273 bc_vec_push(&G.files, argv++);
7274 }
7275
7276 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7277
7278 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007279}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007280#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007281
7282#endif // not DC_SMALL