blob: 8cd67b0f5bce4295d396aff1c60dd495758793e7 [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
Denys Vlasenko39287e02018-12-22 02:23:08 +0100305 BC_INST_HALT,
Gavin Howard01055ba2018-11-03 11:00:21 -0600306 BC_INST_JUMP,
307 BC_INST_JUMP_ZERO,
308
309 BC_INST_CALL,
Gavin Howard01055ba2018-11-03 11:00:21 -0600310 BC_INST_RET0,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100311#endif
Denys Vlasenko39287e02018-12-22 02:23:08 +0100312 BC_INST_RET,
Gavin Howard01055ba2018-11-03 11:00:21 -0600313
314 BC_INST_POP,
Denys Vlasenko8c1e7232018-12-22 01:34:10 +0100315#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600316 BC_INST_POP_EXEC,
317
Gavin Howard01055ba2018-11-03 11:00:21 -0600318 BC_INST_MODEXP,
319 BC_INST_DIVMOD,
320
321 BC_INST_EXECUTE,
322 BC_INST_EXEC_COND,
323
324 BC_INST_ASCIIFY,
325 BC_INST_PRINT_STREAM,
326
327 BC_INST_PRINT_STACK,
328 BC_INST_CLEAR_STACK,
329 BC_INST_STACK_LEN,
330 BC_INST_DUPLICATE,
331 BC_INST_SWAP,
332
333 BC_INST_LOAD,
334 BC_INST_PUSH_VAR,
335 BC_INST_PUSH_TO_VAR,
336
337 BC_INST_QUIT,
338 BC_INST_NQUIT,
339
340 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100341#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600342} BcInst;
343
344typedef struct BcId {
345 char *name;
346 size_t idx;
347} BcId;
348
349typedef struct BcFunc {
350 BcVec code;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100351 IF_BC(BcVec labels;)
352 IF_BC(BcVec autos;)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100353 IF_BC(BcVec strs;)
354 IF_BC(BcVec consts;)
Denys Vlasenko503faf92018-12-20 16:24:18 +0100355 IF_BC(size_t nparams;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600356} BcFunc;
357
358typedef enum BcResultType {
Gavin Howard01055ba2018-11-03 11:00:21 -0600359 BC_RESULT_TEMP,
360
361 BC_RESULT_VAR,
362 BC_RESULT_ARRAY_ELEM,
363 BC_RESULT_ARRAY,
364
365 BC_RESULT_STR,
366
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100367 //code uses "inst - BC_INST_IBASE + BC_RESULT_IBASE" construct,
368 BC_RESULT_IBASE, // relative order should match for: BC_INST_IBASE
369 BC_RESULT_SCALE, // relative order should match for: BC_INST_SCALE
370 BC_RESULT_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600371 BC_RESULT_CONSTANT,
372 BC_RESULT_ONE,
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100373 BC_RESULT_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600374} BcResultType;
375
376typedef union BcResultData {
377 BcNum n;
378 BcVec v;
379 BcId id;
380} BcResultData;
381
382typedef struct BcResult {
383 BcResultType t;
384 BcResultData d;
385} BcResult;
386
387typedef struct BcInstPtr {
388 size_t func;
Denys Vlasenko24e41942018-12-21 23:01:26 +0100389 size_t inst_idx;
390 IF_BC(size_t results_len_before_call;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600391} BcInstPtr;
392
Gavin Howard01055ba2018-11-03 11:00:21 -0600393// BC_LEX_NEG is not used in lexing; it is only for parsing.
394typedef enum BcLexType {
Gavin Howard01055ba2018-11-03 11:00:21 -0600395 BC_LEX_EOF,
396 BC_LEX_INVALID,
397
398 BC_LEX_OP_INC,
399 BC_LEX_OP_DEC,
400
401 BC_LEX_NEG,
402
403 BC_LEX_OP_POWER,
404 BC_LEX_OP_MULTIPLY,
405 BC_LEX_OP_DIVIDE,
406 BC_LEX_OP_MODULUS,
407 BC_LEX_OP_PLUS,
408 BC_LEX_OP_MINUS,
409
410 BC_LEX_OP_REL_EQ,
411 BC_LEX_OP_REL_LE,
412 BC_LEX_OP_REL_GE,
413 BC_LEX_OP_REL_NE,
414 BC_LEX_OP_REL_LT,
415 BC_LEX_OP_REL_GT,
416
417 BC_LEX_OP_BOOL_NOT,
418 BC_LEX_OP_BOOL_OR,
419 BC_LEX_OP_BOOL_AND,
420
421 BC_LEX_OP_ASSIGN_POWER,
422 BC_LEX_OP_ASSIGN_MULTIPLY,
423 BC_LEX_OP_ASSIGN_DIVIDE,
424 BC_LEX_OP_ASSIGN_MODULUS,
425 BC_LEX_OP_ASSIGN_PLUS,
426 BC_LEX_OP_ASSIGN_MINUS,
427 BC_LEX_OP_ASSIGN,
428
429 BC_LEX_NLINE,
430 BC_LEX_WHITESPACE,
431
432 BC_LEX_LPAREN,
433 BC_LEX_RPAREN,
434
435 BC_LEX_LBRACKET,
436 BC_LEX_COMMA,
437 BC_LEX_RBRACKET,
438
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100439 BC_LEX_LBRACE, // '{' is 0x7B, '}' is 0x7D,
Gavin Howard01055ba2018-11-03 11:00:21 -0600440 BC_LEX_SCOLON,
Denys Vlasenko9dc5d082018-12-16 18:43:51 +0100441 BC_LEX_RBRACE, // should be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE)
Gavin Howard01055ba2018-11-03 11:00:21 -0600442
443 BC_LEX_STR,
444 BC_LEX_NAME,
445 BC_LEX_NUMBER,
446
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100447 BC_LEX_KEY_1st_keyword,
448 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600449 BC_LEX_KEY_BREAK,
450 BC_LEX_KEY_CONTINUE,
451 BC_LEX_KEY_DEFINE,
452 BC_LEX_KEY_ELSE,
453 BC_LEX_KEY_FOR,
454 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100455 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
456 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600457 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100458 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600459 BC_LEX_KEY_LENGTH,
460 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100461 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600462 BC_LEX_KEY_PRINT,
463 BC_LEX_KEY_QUIT,
464 BC_LEX_KEY_READ,
465 BC_LEX_KEY_RETURN,
466 BC_LEX_KEY_SCALE,
467 BC_LEX_KEY_SQRT,
468 BC_LEX_KEY_WHILE,
469
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100470#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600471 BC_LEX_EQ_NO_REG,
472 BC_LEX_OP_MODEXP,
473 BC_LEX_OP_DIVMOD,
474
475 BC_LEX_COLON,
476 BC_LEX_ELSE,
477 BC_LEX_EXECUTE,
478 BC_LEX_PRINT_STACK,
479 BC_LEX_CLEAR_STACK,
480 BC_LEX_STACK_LEVEL,
481 BC_LEX_DUPLICATE,
482 BC_LEX_SWAP,
483 BC_LEX_POP,
484
485 BC_LEX_ASCIIFY,
486 BC_LEX_PRINT_STREAM,
487
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100488 // code uses "t - BC_LEX_STORE_IBASE + BC_INST_IBASE" construct,
489 BC_LEX_STORE_IBASE, // relative order should match for: BC_INST_IBASE
490 BC_LEX_STORE_SCALE, // relative order should match for: BC_INST_SCALE
Gavin Howard01055ba2018-11-03 11:00:21 -0600491 BC_LEX_LOAD,
492 BC_LEX_LOAD_POP,
493 BC_LEX_STORE_PUSH,
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100494 BC_LEX_STORE_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600495 BC_LEX_PRINT_POP,
496 BC_LEX_NQUIT,
497 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100498#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600499} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100500// must match order of BC_LEX_KEY_foo etc above
501#if ENABLE_BC
502struct BcLexKeyword {
503 char name8[8];
504};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100505#define BC_LEX_KW_ENTRY(a, b) \
506 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100507static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100508 BC_LEX_KW_ENTRY("auto" , 1), // 0
509 BC_LEX_KW_ENTRY("break" , 1), // 1
510 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
511 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100512 BC_LEX_KW_ENTRY("else" , 0), // 4
513 BC_LEX_KW_ENTRY("for" , 1), // 5
514 BC_LEX_KW_ENTRY("halt" , 0), // 6
515 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100516 BC_LEX_KW_ENTRY("if" , 1), // 8
517 BC_LEX_KW_ENTRY("last" , 0), // 9
518 BC_LEX_KW_ENTRY("length" , 1), // 10
519 BC_LEX_KW_ENTRY("limits" , 0), // 11
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100520 BC_LEX_KW_ENTRY("obase" , 1), // 12
521 BC_LEX_KW_ENTRY("print" , 0), // 13
522 BC_LEX_KW_ENTRY("quit" , 1), // 14
523 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100524 BC_LEX_KW_ENTRY("return" , 1), // 16
525 BC_LEX_KW_ENTRY("scale" , 1), // 17
526 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
527 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100528};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100529#undef BC_LEX_KW_ENTRY
Denys Vlasenko59d4ce92018-12-17 10:42:31 +0100530#define STRING_if (bc_lex_kws[8].name8)
531#define STRING_else (bc_lex_kws[4].name8)
532#define STRING_while (bc_lex_kws[19].name8)
533#define STRING_for (bc_lex_kws[5].name8)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100534enum {
535 POSIX_KWORD_MASK = 0
Denys Vlasenko82269122018-12-14 16:30:56 +0100536 | (1 << 0) // 0
537 | (1 << 1) // 1
538 | (0 << 2) // 2
539 | (1 << 3) // 3
540 | (0 << 4) // 4
541 | (1 << 5) // 5
542 | (0 << 6) // 6
543 | (1 << 7) // 7
544 | (1 << 8) // 8
545 | (0 << 9) // 9
546 | (1 << 10) // 10
547 | (0 << 11) // 11
548 | (1 << 12) // 12
549 | (0 << 13) // 13
550 | (1 << 14) // 14
551 | (0 << 15) // 15
552 | (1 << 16) // 16
553 | (1 << 17) // 17
554 | (1 << 18) // 18
555 | (1 << 19) // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100556};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100557#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100558
559// This is a bit array that corresponds to token types. An entry is
560// true if the token is valid in an expression, false otherwise.
561// Used to figure out when expr parsing should stop *without error message*
562// - 0 element indicates this condition. 1 means "this token is to be eaten
563// as part of the expression", token can them still be determined to be invalid
564// by later processing.
565enum {
566#define EXBITS(a,b,c,d,e,f,g,h) \
567 ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7)))
568 BC_PARSE_EXPRS_BITS = 0
569 + (EXBITS(0,0,1,1,1,1,1,1) << (0*8)) // 0: eof inval ++ -- - ^ * /
570 + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: % + - == <= >= != <
571 + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: > ! || && ^= *= /= %=
572 + (EXBITS(1,1,1,0,0,1,1,0) << (3*8)) // 24: += -= = NL WS ( ) [
573 + (EXBITS(0,0,0,0,0,0,1,1) << (4*8)) // 32: , ] { ; } STR NAME NUM
574 + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase
575 + (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?
576 + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while
577#undef EXBITS
578};
579static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
580{
581#if ULONG_MAX > 0xffffffff
582 // 64-bit version (will not work correctly for 32-bit longs!)
583 return BC_PARSE_EXPRS_BITS & (1UL << i);
584#else
585 // 32-bit version
586 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
587 if (i >= 32) {
588 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
589 i &= 31;
590 }
591 return m & (1UL << i);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100592#endif
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +0100593}
594
595// This is an array of data for operators that correspond to
596// [BC_LEX_OP_INC...BC_LEX_OP_ASSIGN] token types.
597static const uint8_t bc_parse_ops[] = {
598#define OP(p,l) ((int)(l) * 0x10 + (p))
599 OP(0, false), OP( 0, false ), // inc dec
600 OP(1, false), // neg
601 OP(2, false), // pow
602 OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod
603 OP(4, true ), OP( 4, true ), // + -
604 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
605 OP(1, false), // not
606 OP(7, true ), OP( 7, true ), // or and
607 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
608 OP(5, false), OP( 5, false ), // -= =
609#undef OP
610};
611#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
612#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
613#endif // ENABLE_BC
614
615#if ENABLE_DC
616static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
617int8_t
618dc_parse_insts[] = {
619 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
620 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
621 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
622 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
623 BC_INST_INVALID, BC_INST_INVALID,
624 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
625 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
626 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
627 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
628 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
629 BC_INST_INVALID, BC_INST_INVALID,
630 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
631 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
632 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
633 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
634 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
635 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
636 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
637 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
638 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
639 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
640 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
641 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
642};
643#endif // ENABLE_DC
644
Gavin Howard01055ba2018-11-03 11:00:21 -0600645typedef struct BcLex {
Gavin Howard01055ba2018-11-03 11:00:21 -0600646 const char *buf;
647 size_t i;
648 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600649 size_t len;
650 bool newline;
Gavin Howard01055ba2018-11-03 11:00:21 -0600651 struct {
652 BcLexType t;
653 BcLexType last;
654 BcVec v;
655 } t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600656} BcLex;
657
Denys Vlasenko4796a1d2018-12-19 12:47:45 +0100658#define BC_PARSE_STREND (0xff)
Gavin Howard01055ba2018-11-03 11:00:21 -0600659
Denys Vlasenko39287e02018-12-22 02:23:08 +0100660#if ENABLE_BC
661# define BC_PARSE_REL (1 << 0)
662# define BC_PARSE_PRINT (1 << 1)
663# define BC_PARSE_ARRAY (1 << 2)
664# define BC_PARSE_NOCALL (1 << 3)
665#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600666
Gavin Howard01055ba2018-11-03 11:00:21 -0600667typedef struct BcParse {
Gavin Howard01055ba2018-11-03 11:00:21 -0600668 BcLex l;
669
Denys Vlasenko503faf92018-12-20 16:24:18 +0100670 IF_BC(BcVec exits;)
671 IF_BC(BcVec conds;)
672 IF_BC(BcVec ops;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600673
Gavin Howard01055ba2018-11-03 11:00:21 -0600674 BcFunc *func;
675 size_t fidx;
676
Denys Vlasenko503faf92018-12-20 16:24:18 +0100677 IF_BC(size_t in_funcdef;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600678} BcParse;
679
Gavin Howard01055ba2018-11-03 11:00:21 -0600680typedef struct BcProgram {
Gavin Howard01055ba2018-11-03 11:00:21 -0600681 size_t len;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100682 size_t nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -0600683
Denys Vlasenko503faf92018-12-20 16:24:18 +0100684 size_t scale;
Gavin Howard01055ba2018-11-03 11:00:21 -0600685 size_t ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600686 size_t ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -0600687
Gavin Howard01055ba2018-11-03 11:00:21 -0600688 BcVec results;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +0100689 BcVec exestack;
Gavin Howard01055ba2018-11-03 11:00:21 -0600690
691 BcVec fns;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +0100692 IF_BC(BcVec fn_map;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600693
694 BcVec vars;
695 BcVec var_map;
696
697 BcVec arrs;
698 BcVec arr_map;
699
Denys Vlasenko5d57bc42018-12-21 16:22:26 +0100700 IF_DC(BcVec strs;)
701 IF_DC(BcVec consts;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600702
703 const char *file;
704
Gavin Howard01055ba2018-11-03 11:00:21 -0600705 BcNum zero;
Denys Vlasenko503faf92018-12-20 16:24:18 +0100706 IF_BC(BcNum one;)
707 IF_BC(BcNum last;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600708} BcProgram;
709
Gavin Howard01055ba2018-11-03 11:00:21 -0600710#define BC_PROG_MAIN (0)
711#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100712#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600713#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100714#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600715
716#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
717#define BC_PROG_NUM(r, n) \
718 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
719
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100720#define BC_FLAG_W (1 << 0)
721#define BC_FLAG_V (1 << 1)
722#define BC_FLAG_S (1 << 2)
723#define BC_FLAG_Q (1 << 3)
724#define BC_FLAG_L (1 << 4)
725#define BC_FLAG_I (1 << 5)
726#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600727
728#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
729#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
730
Denys Vlasenko64074a12018-12-07 15:50:14 +0100731#define BC_MAX_OBASE ((unsigned) 999)
732#define BC_MAX_DIM ((unsigned) INT_MAX)
733#define BC_MAX_SCALE ((unsigned) UINT_MAX)
734#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
735#define BC_MAX_NUM BC_MAX_STRING
736// Unused apart from "limits" message. Just show a "biggish number" there.
737//#define BC_MAX_NAME BC_MAX_STRING
738//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
739//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
740#define BC_MAX_NAME_STR "999999999"
741#define BC_MAX_EXP_STR "999999999"
742#define BC_MAX_VARS_STR "999999999"
743
744#define BC_MAX_OBASE_STR "999"
745
746#if INT_MAX == 2147483647
747# define BC_MAX_DIM_STR "2147483647"
748#elif INT_MAX == 9223372036854775807
749# define BC_MAX_DIM_STR "9223372036854775807"
750#else
751# error Strange INT_MAX
752#endif
753
754#if UINT_MAX == 4294967295
755# define BC_MAX_SCALE_STR "4294967295"
756# define BC_MAX_STRING_STR "4294967294"
757#elif UINT_MAX == 18446744073709551615
758# define BC_MAX_SCALE_STR "18446744073709551615"
759# define BC_MAX_STRING_STR "18446744073709551614"
760#else
761# error Strange UINT_MAX
762#endif
763#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600764
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100765struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100766 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100767 IF_FEATURE_CLEAN_UP(smallint exiting;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600768
769 BcParse prs;
770 BcProgram prog;
771
Denys Vlasenko5318f812018-12-05 17:48:01 +0100772 // For error messages. Can be set to current parsed line,
773 // or [TODO] to current executing line (can be before last parsed one)
774 unsigned err_line;
775
Gavin Howard01055ba2018-11-03 11:00:21 -0600776 BcVec files;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +0100777 BcVec input_buffer;
778 FILE *input_fp;
Gavin Howard01055ba2018-11-03 11:00:21 -0600779
780 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100781
782#if ENABLE_FEATURE_EDITING
783 line_input_t *line_input_state;
784#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100785} FIX_ALIASING;
786#define G (*ptr_to_globals)
787#define INIT_G() do { \
788 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
789} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100790#define FREE_G() do { \
791 FREE_PTR_TO_GLOBALS(); \
792} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100793#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
794#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100795#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100796#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100797# define G_interrupt bb_got_signal
798# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100799#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100800# define G_interrupt 0
801# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100802#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100803#if ENABLE_FEATURE_CLEAN_UP
804# define G_exiting G.exiting
805#else
806# define G_exiting 0
807#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100808#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
Denys Vlasenko40534bb2018-12-13 16:59:24 +0100809#define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
Denys Vlasenko00d77792018-11-30 23:13:42 +0100810
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100811// In configurations where errors abort instead of propagating error
812// return code up the call chain, functions returning BC_STATUS
813// actually don't return anything, they always succeed and return "void".
814// A macro wrapper is provided, which makes this statement work:
815// s = zbc_func(...)
816// and makes it visible to the compiler that s is always zero,
817// allowing compiler to optimize dead code after the statement.
818//
819// To make code more readable, each such function has a "z"
820// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
821//
822#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100823# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100824# define ERRORFUNC /*nothing*/
Denys Vlasenkoec603182018-12-17 10:34:02 +0100825# define IF_ERROR_RETURN_POSSIBLE(a) a
826# define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100827# define RETURN_STATUS(v) return (v)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100828# define COMMA_SUCCESS /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100829#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100830# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100831# define ERRORFUNC NORETURN
Denys Vlasenkoec603182018-12-17 10:34:02 +0100832# define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/
833# define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100834# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenkoec603182018-12-17 10:34:02 +0100835# define COMMA_SUCCESS ,BC_STATUS_SUCCESS
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100836#endif
837
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +0100838#define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n)))
839#define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n)))
840
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100841#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
842#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
843#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
844//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
845static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
846{
847 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
848}
849//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
850static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
851{
852 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
853}
854
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100855typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
856
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100857typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100858
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100859static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
860 BcNumBinaryOp op, size_t req);
861static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
862static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
863static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
864static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
865static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
866static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
867
868static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
869{
870 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
871 (void) scale;
872 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
873}
874
875static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
876{
877 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
878 (void) scale;
879 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
880}
881
882static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
883{
884 size_t req = BC_NUM_MREQ(a, b, scale);
885 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
886}
887
888static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
889{
890 size_t req = BC_NUM_MREQ(a, b, scale);
891 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
892}
893
894static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
895{
896 size_t req = BC_NUM_MREQ(a, b, scale);
897 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
898}
899
900static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
901{
902 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
903}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100904
Denys Vlasenko1aeacef2018-12-11 19:12:13 +0100905static const BcNumBinaryOp zbc_program_ops[] = {
906 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 -0600907};
Denys Vlasenkoec603182018-12-17 10:34:02 +0100908#define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS)
909#define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS)
910#define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS)
911#define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS)
912#define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS)
913#define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -0600914
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100915static void fflush_and_check(void)
916{
917 fflush_all();
918 if (ferror(stdout) || ferror(stderr))
919 bb_perror_msg_and_die("output error");
920}
921
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100922#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100923#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100924do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100925 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100926 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100927 return BC_STATUS_FAILURE; \
928} while (0)
929#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100930#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100931#endif
932
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100933static void quit(void) NORETURN;
934static void quit(void)
935{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100936 if (ferror(stdin))
937 bb_perror_msg_and_die("input error");
938 fflush_and_check();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +0100939 dbg_exec("quit(): exiting with exitcode SUCCESS");
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100940 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100941}
942
Denys Vlasenko5318f812018-12-05 17:48:01 +0100943static void bc_verror_msg(const char *fmt, va_list p)
944{
Denys Vlasenko82269122018-12-14 16:30:56 +0100945 const char *sv = sv; // for compiler
Denys Vlasenko5318f812018-12-05 17:48:01 +0100946 if (G.prog.file) {
947 sv = applet_name;
948 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
949 }
950 bb_verror_msg(fmt, p, NULL);
951 if (G.prog.file) {
952 free((char*)applet_name);
953 applet_name = sv;
954 }
955}
956
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100957static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100958{
959 va_list p;
960
961 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100962 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100963 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100964
Denys Vlasenkoec603182018-12-17 10:34:02 +0100965 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
966 IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE);
967 exit(1);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100968}
969
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100970#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100971static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100972{
973 va_list p;
974
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100975 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100976 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100977 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100978
979 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100980 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100981 va_end(p);
982
983 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100984 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100985 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenkoec603182018-12-17 10:34:02 +0100986 if (ENABLE_FEATURE_CLEAN_UP || G_ttyin)
987 return BC_STATUS_FAILURE;
988 exit(1);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100989}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100990#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100991
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100992// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
993// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100994// function must not have caller-cleaned parameters on stack.
995// Unfortunately, vararg function API does exactly that on most arches.
996// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100997static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100998{
Denys Vlasenkoec603182018-12-17 10:34:02 +0100999 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001000}
1001static ERRORFUNC int bc_error_bad_character(char c)
1002{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001003 if (!c)
1004 IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character");
Denys Vlasenkoec603182018-12-17 10:34:02 +01001005 IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001006}
1007static ERRORFUNC int bc_error_bad_expression(void)
1008{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001009 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001010}
1011static ERRORFUNC int bc_error_bad_token(void)
1012{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001013 IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001014}
1015static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1016{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001017 IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001018}
1019static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1020{
Denys Vlasenkoec603182018-12-17 10:34:02 +01001021 IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type");
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001022}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001023#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001024static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001025{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001026 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001027}
Denys Vlasenko00646792018-12-05 18:12:27 +01001028static int bc_POSIX_does_not_allow(const char *msg)
1029{
1030 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1031}
1032static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1033{
1034 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1035}
1036static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1037{
1038 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1039}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001040#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001041
Gavin Howard01055ba2018-11-03 11:00:21 -06001042static void bc_vec_grow(BcVec *v, size_t n)
1043{
1044 size_t cap = v->cap * 2;
1045 while (cap < v->len + n) cap *= 2;
1046 v->v = xrealloc(v->v, v->size * cap);
1047 v->cap = cap;
1048}
1049
1050static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1051{
1052 v->size = esize;
1053 v->cap = BC_VEC_START_CAP;
1054 v->len = 0;
1055 v->dtor = dtor;
1056 v->v = xmalloc(esize * BC_VEC_START_CAP);
1057}
1058
Denys Vlasenko7d628012018-12-04 21:46:47 +01001059static void bc_char_vec_init(BcVec *v)
1060{
1061 bc_vec_init(v, sizeof(char), NULL);
1062}
1063
Gavin Howard01055ba2018-11-03 11:00:21 -06001064static void bc_vec_expand(BcVec *v, size_t req)
1065{
1066 if (v->cap < req) {
1067 v->v = xrealloc(v->v, v->size * req);
1068 v->cap = req;
1069 }
1070}
1071
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001072static void bc_vec_pop(BcVec *v)
1073{
1074 v->len--;
1075 if (v->dtor)
1076 v->dtor(v->v + (v->size * v->len));
1077}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001078
Gavin Howard01055ba2018-11-03 11:00:21 -06001079static void bc_vec_npop(BcVec *v, size_t n)
1080{
1081 if (!v->dtor)
1082 v->len -= n;
1083 else {
1084 size_t len = v->len - n;
1085 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1086 }
1087}
1088
Denys Vlasenko7d628012018-12-04 21:46:47 +01001089static void bc_vec_pop_all(BcVec *v)
1090{
1091 bc_vec_npop(v, v->len);
1092}
1093
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001094static size_t bc_vec_push(BcVec *v, const void *data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001095{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001096 size_t len = v->len;
1097 if (len >= v->cap) bc_vec_grow(v, 1);
1098 memmove(v->v + (v->size * len), data, v->size);
1099 v->len++;
1100 return len;
Gavin Howard01055ba2018-11-03 11:00:21 -06001101}
1102
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01001103// G.prog.results often needs "pop old operand, push result" idiom.
1104// Can do this without a few extra ops
1105static size_t bc_result_pop_and_push(const void *data)
1106{
1107 BcVec *v = &G.prog.results;
1108 char *last;
1109 size_t len = v->len - 1;
1110
1111 last = v->v + (v->size * len);
1112 if (v->dtor)
1113 v->dtor(last);
1114 memmove(last, data, v->size);
1115 return len;
1116}
1117
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001118static size_t bc_vec_pushByte(BcVec *v, char data)
Gavin Howard01055ba2018-11-03 11:00:21 -06001119{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001120 return bc_vec_push(v, &data);
Gavin Howard01055ba2018-11-03 11:00:21 -06001121}
1122
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001123static size_t bc_vec_pushZeroByte(BcVec *v)
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001124{
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001125 //return bc_vec_pushByte(v, '\0');
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001126 // better:
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01001127 return bc_vec_push(v, &const_int_0);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001128}
1129
Gavin Howard01055ba2018-11-03 11:00:21 -06001130static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1131{
1132 if (idx == v->len)
1133 bc_vec_push(v, data);
1134 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001135 char *ptr;
1136
1137 if (v->len == v->cap) bc_vec_grow(v, 1);
1138
1139 ptr = v->v + v->size * idx;
1140
1141 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1142 memmove(ptr, data, v->size);
1143 }
1144}
1145
1146static void bc_vec_string(BcVec *v, size_t len, const char *str)
1147{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001148 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001149 bc_vec_expand(v, len + 1);
1150 memcpy(v->v, str, len);
1151 v->len = len;
1152
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001153 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001154}
1155
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001156#if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING
Gavin Howard01055ba2018-11-03 11:00:21 -06001157static void bc_vec_concat(BcVec *v, const char *str)
1158{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001159 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001160
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001161 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001162
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001163 slen = strlen(str);
1164 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001165
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001166 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001167 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001168
1169 v->len = len;
1170}
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001171#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001172
1173static void *bc_vec_item(const BcVec *v, size_t idx)
1174{
1175 return v->v + v->size * idx;
1176}
1177
1178static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1179{
1180 return v->v + v->size * (v->len - idx - 1);
1181}
1182
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001183static void *bc_vec_top(const BcVec *v)
1184{
1185 return v->v + v->size * (v->len - 1);
1186}
1187
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001188static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001189{
1190 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001191 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001192 free(v->v);
1193}
1194
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01001195static BcFunc* bc_program_func(size_t idx)
1196{
1197 return bc_vec_item(&G.prog.fns, idx);
1198}
1199// BC_PROG_MAIN is zeroth element, so:
1200#define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v))
1201
1202#if ENABLE_BC
1203static BcFunc* bc_program_current_func(void)
1204{
1205 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
1206 BcFunc *func = bc_program_func(ip->func);
1207 return func;
1208}
1209#endif
1210
1211static char** bc_program_str(size_t idx)
1212{
1213#if ENABLE_BC
1214 if (IS_BC) {
1215 BcFunc *func = bc_program_current_func();
1216 return bc_vec_item(&func->strs, idx);
1217 }
1218#endif
1219 IF_DC(return bc_vec_item(&G.prog.strs, idx);)
1220}
1221
1222static char** bc_program_const(size_t idx)
1223{
1224#if ENABLE_BC
1225 if (IS_BC) {
1226 BcFunc *func = bc_program_current_func();
1227 return bc_vec_item(&func->consts, idx);
1228 }
1229#endif
1230 IF_DC(return bc_vec_item(&G.prog.consts, idx);)
1231}
1232
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001233static int bc_id_cmp(const void *e1, const void *e2)
1234{
1235 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1236}
1237
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001238static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001239{
1240 free(((BcId *) id)->name);
1241}
1242
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001243static size_t bc_map_find_ge(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001244{
1245 size_t low = 0, high = v->len;
1246
1247 while (low < high) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001248 size_t mid = (low + high) / 2;
1249 BcId *id = bc_vec_item(v, mid);
1250 int result = bc_id_cmp(ptr, id);
1251
1252 if (result == 0)
1253 return mid;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01001254 if (result < 0)
Gavin Howard01055ba2018-11-03 11:00:21 -06001255 high = mid;
1256 else
1257 low = mid + 1;
1258 }
1259
1260 return low;
1261}
1262
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001263static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001264{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001265 size_t n = *i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001266
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001267 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001268 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001269 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1270 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001271 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001272 bc_vec_pushAt(v, ptr, n);
1273 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001274}
1275
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001276#if ENABLE_BC
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001277static size_t bc_map_find_exact(const BcVec *v, const void *ptr)
Gavin Howard01055ba2018-11-03 11:00:21 -06001278{
Denys Vlasenko5aa54832018-12-19 13:55:53 +01001279 size_t i = bc_map_find_ge(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001280 if (i >= v->len) return BC_VEC_INVALID_IDX;
1281 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1282}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001283#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001284
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001285static int bad_input_byte(char c)
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001286{
1287 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1288 || c > 0x7e
1289 ) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001290 bc_error_fmt("illegal character 0x%02x", c);
1291 return 1;
1292 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001293 return 0;
1294}
1295
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001296// Note: it _appends_ data from fp to vec.
1297static void bc_read_line(BcVec *vec, FILE *fp)
Gavin Howard01055ba2018-11-03 11:00:21 -06001298{
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001299 again:
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001300 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001301
Gavin Howard01055ba2018-11-03 11:00:21 -06001302#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001303 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001304 intr:
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001305 if (fp != stdin) {
1306 // ^C while running a script (bc SCRIPT): die.
1307 // We do not return to interactive prompt:
1308 // user might be running us from a shell,
1309 // and SCRIPT might be intended to terminate
1310 // (e.g. contain a "halt" stmt).
1311 // ^C dropping user into a bc prompt instead of
1312 // the shell would be unexpected.
1313 xfunc_die();
1314 }
1315 // ^C while interactive input
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001316 G_interrupt = 0;
1317 // GNU bc says "interrupted execution."
1318 // GNU dc says "Interrupt!"
1319 fputs("\ninterrupted execution\n", stderr);
1320 }
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001321
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001322# if ENABLE_FEATURE_EDITING
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001323 if (G_ttyin && fp == stdin) {
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001324 int n, i;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001325# define line_buf bb_common_bufsiz1
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001326 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
1327 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1328 if (n == 0) // ^C
1329 goto intr;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001330 bc_vec_pushZeroByte(vec); // ^D or EOF (or error)
Denys Vlasenkoe755e302018-12-13 22:25:28 +01001331 return;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001332 }
1333 i = 0;
1334 for (;;) {
1335 char c = line_buf[i++];
1336 if (!c) break;
1337 if (bad_input_byte(c)) goto again;
1338 }
1339 bc_vec_concat(vec, line_buf);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001340# undef line_buf
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001341 } else
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001342# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001343#endif
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001344 {
1345 int c;
1346 bool bad_chars = 0;
1347 size_t len = vec->len;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001348
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001349 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001350#if ENABLE_FEATURE_BC_SIGNALS
1351 if (G_interrupt) {
1352 // ^C was pressed: ignore entire line, get another one
1353 vec->len = len;
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001354 goto intr;
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01001355 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001356#endif
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01001357 do c = fgetc(fp); while (c == '\0');
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001358 if (c == EOF) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001359 if (ferror(fp))
1360 bb_perror_msg_and_die("input error");
1361 // Note: EOF does not append '\n'
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001362 break;
1363 }
1364 bad_chars |= bad_input_byte(c);
1365 bc_vec_pushByte(vec, (char)c);
1366 } while (c != '\n');
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001367
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001368 if (bad_chars) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01001369 // Bad chars on this line
1370 if (!G.prog.file) { // stdin
1371 // ignore entire line, get another one
1372 vec->len = len;
1373 goto again;
1374 }
1375 bb_perror_msg_and_die("file '%s' is not text", G.prog.file);
Denys Vlasenkoed849352018-12-06 10:26:13 +01001376 }
Denys Vlasenko915c72b2018-12-13 19:28:41 +01001377 bc_vec_pushZeroByte(vec);
1378 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001379}
1380
Gavin Howard01055ba2018-11-03 11:00:21 -06001381static void bc_num_setToZero(BcNum *n, size_t scale)
1382{
1383 n->len = 0;
1384 n->neg = false;
1385 n->rdx = scale;
1386}
1387
1388static void bc_num_zero(BcNum *n)
1389{
1390 bc_num_setToZero(n, 0);
1391}
1392
1393static void bc_num_one(BcNum *n)
1394{
1395 bc_num_setToZero(n, 0);
1396 n->len = 1;
1397 n->num[0] = 1;
1398}
1399
Denys Vlasenko3129f702018-12-09 12:04:44 +01001400// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001401static void bc_num_init(BcNum *n, size_t req)
1402{
1403 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001404 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001405 n->num = xmalloc(req);
1406 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001407 n->rdx = 0;
1408 n->len = 0;
1409 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001410}
1411
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001412static void bc_num_init_DEF_SIZE(BcNum *n)
1413{
1414 bc_num_init(n, BC_NUM_DEF_SIZE);
1415}
1416
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001417static void bc_num_expand(BcNum *n, size_t req)
1418{
1419 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1420 if (req > n->cap) {
1421 n->num = xrealloc(n->num, req);
1422 n->cap = req;
1423 }
1424}
1425
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001426static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001427{
1428 free(((BcNum *) num)->num);
1429}
1430
1431static void bc_num_copy(BcNum *d, BcNum *s)
1432{
1433 if (d != s) {
1434 bc_num_expand(d, s->cap);
1435 d->len = s->len;
1436 d->neg = s->neg;
1437 d->rdx = s->rdx;
1438 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1439 }
1440}
1441
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001442static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001443{
1444 size_t i;
Denys Vlasenko1557b762018-12-22 21:37:46 +01001445 unsigned long result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001446
Denys Vlasenko1557b762018-12-22 21:37:46 +01001447 result = 0;
1448 i = n->len;
1449 while (i > n->rdx) {
1450 unsigned long prev = result;
1451 result = result * 10 + n->num[--i];
1452 // Even overflowed N*10 can still satisfy N*10>=N. For example,
1453 // 0x1ff00000 * 10 is 0x13f600000,
1454 // or 0x3f600000 truncated to 32 bits. Which is larger.
1455 // However, (N*10)/8 < N check is always correct.
1456 if ((result / 8) < prev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001457 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001458 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001459 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001460
Denys Vlasenko29301232018-12-11 15:29:32 +01001461 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001462}
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01001463#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
1464
1465static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1466{
1467 if (n->neg) RETURN_STATUS(bc_error("negative number"));
1468
1469 RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
1470}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001471#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001472
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001473#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1474# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1475#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1476# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1477#endif
1478// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1479// would not hit realloc() code path - not good if num[] is not malloced
1480
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001481static void bc_num_ulong2num(BcNum *n, unsigned long val)
1482{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001483 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001484
1485 bc_num_zero(n);
1486
1487 if (val == 0) return;
1488
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01001489 bc_num_expand(n, ULONG_NUM_BUFSIZE);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001490
1491 ptr = n->num;
1492 for (;;) {
1493 n->len++;
1494 *ptr++ = val % 10;
1495 val /= 10;
1496 if (val == 0) break;
1497 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001498}
1499
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001500static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001501{
1502 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001503 for (i = 0; i < len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001504 a[i] -= b[i];
1505 for (j = i; a[j] < 0;) {
1506 a[j++] += 10;
1507 a[j] -= 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06001508 }
1509 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001510}
1511
1512static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1513{
Denys Vlasenko4113e1f2018-12-18 00:39:24 +01001514 size_t i = len;
1515 for (;;) {
1516 int c;
1517 if (i == 0)
1518 return 0;
1519 i--;
1520 c = a[i] - b[i];
1521 if (c != 0) {
1522 i++;
1523 if (c < 0)
1524 return -i;
1525 return i;
1526 }
1527 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001528}
1529
1530static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1531{
1532 size_t i, min, a_int, b_int, diff;
1533 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001534 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001535 ssize_t cmp;
1536
1537 if (a == b) return 0;
1538 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1539 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001540
1541 if (a->neg != b->neg) // signs of a and b differ
1542 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1543 return (int)b->neg - (int)a->neg;
1544 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001545
1546 a_int = BC_NUM_INT(a);
1547 b_int = BC_NUM_INT(b);
1548 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001549
1550 if (a_int != 0) return (ssize_t) a_int;
1551
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001552 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001553 if (a_max) {
1554 min = b->rdx;
1555 diff = a->rdx - b->rdx;
1556 max_num = a->num + diff;
1557 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001558 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1559 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001560 min = a->rdx;
1561 diff = b->rdx - a->rdx;
1562 max_num = b->num + diff;
1563 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001564 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001565 }
1566
1567 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001568 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001569
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001570 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001571 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001572 }
1573
1574 return 0;
1575}
1576
1577static void bc_num_truncate(BcNum *n, size_t places)
1578{
1579 if (places == 0) return;
1580
1581 n->rdx -= places;
1582
1583 if (n->len != 0) {
1584 n->len -= places;
1585 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1586 }
1587}
1588
1589static void bc_num_extend(BcNum *n, size_t places)
1590{
1591 size_t len = n->len + places;
1592
1593 if (places != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001594 if (n->cap < len) bc_num_expand(n, len);
1595
1596 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1597 memset(n->num, 0, sizeof(BcDig) * places);
1598
1599 n->len += places;
1600 n->rdx += places;
1601 }
1602}
1603
1604static void bc_num_clean(BcNum *n)
1605{
1606 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1607 if (n->len == 0)
1608 n->neg = false;
1609 else if (n->len < n->rdx)
1610 n->len = n->rdx;
1611}
1612
1613static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1614{
1615 if (n->rdx < scale)
1616 bc_num_extend(n, scale - n->rdx);
1617 else
1618 bc_num_truncate(n, n->rdx - scale);
1619
1620 bc_num_clean(n);
1621 if (n->len != 0) n->neg = !neg1 != !neg2;
1622}
1623
1624static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1625 BcNum *restrict b)
1626{
1627 if (idx < n->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001628 b->len = n->len - idx;
1629 a->len = idx;
1630 a->rdx = b->rdx = 0;
1631
1632 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1633 memcpy(a->num, n->num, idx * sizeof(BcDig));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001634 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001635 bc_num_zero(b);
1636 bc_num_copy(a, n);
1637 }
1638
1639 bc_num_clean(a);
1640 bc_num_clean(b);
1641}
1642
Denys Vlasenko29301232018-12-11 15:29:32 +01001643static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001644{
Denys Vlasenko29301232018-12-11 15:29:32 +01001645 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001646
1647 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1648 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1649 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001650 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001651 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001652
1653 if (n->rdx >= places)
1654 n->rdx -= places;
1655 else {
1656 bc_num_extend(n, places - n->rdx);
1657 n->rdx = 0;
1658 }
1659
1660 bc_num_clean(n);
1661
Denys Vlasenko29301232018-12-11 15:29:32 +01001662 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001663}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001664#define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001665
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001666static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001667{
1668 BcNum one;
1669 BcDig num[2];
1670
1671 one.cap = 2;
1672 one.num = num;
1673 bc_num_one(&one);
1674
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001675 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001676}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001677#define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001678
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001679static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001680{
1681 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1682 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001683 unsigned carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001684
1685 // Because this function doesn't need to use scale (per the bc spec),
1686 // I am hijacking it to say whether it's doing an add or a subtract.
1687
1688 if (a->len == 0) {
1689 bc_num_copy(c, b);
1690 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001691 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001692 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001693 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001694 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001695 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001696 }
1697
1698 c->neg = a->neg;
1699 c->rdx = BC_MAX(a->rdx, b->rdx);
1700 min_rdx = BC_MIN(a->rdx, b->rdx);
1701 c->len = 0;
1702
1703 if (a->rdx > b->rdx) {
1704 diff = a->rdx - b->rdx;
1705 ptr = a->num;
1706 ptr_a = a->num + diff;
1707 ptr_b = b->num;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001708 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001709 diff = b->rdx - a->rdx;
1710 ptr = b->num;
1711 ptr_a = a->num;
1712 ptr_b = b->num + diff;
1713 }
1714
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001715 ptr_c = c->num;
1716 for (i = 0; i < diff; ++i, ++c->len)
1717 ptr_c[i] = ptr[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06001718
1719 ptr_c += diff;
1720 a_int = BC_NUM_INT(a);
1721 b_int = BC_NUM_INT(b);
1722
1723 if (a_int > b_int) {
1724 min_int = b_int;
1725 max = a_int;
1726 ptr = ptr_a;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001727 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001728 min_int = a_int;
1729 max = b_int;
1730 ptr = ptr_b;
1731 }
1732
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001733 carry = 0;
1734 for (i = 0; i < min_rdx + min_int; ++i) {
1735 unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001736 carry = in / 10;
1737 ptr_c[i] = (BcDig)(in % 10);
1738 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001739 for (; i < max + min_rdx; ++i) {
1740 unsigned in = (unsigned)ptr[i] + carry;
Gavin Howard01055ba2018-11-03 11:00:21 -06001741 carry = in / 10;
1742 ptr_c[i] = (BcDig)(in % 10);
1743 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001744 c->len += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001745
1746 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1747
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001748 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001749}
1750
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001751static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001752{
Gavin Howard01055ba2018-11-03 11:00:21 -06001753 ssize_t cmp;
1754 BcNum *minuend, *subtrahend;
1755 size_t start;
1756 bool aneg, bneg, neg;
1757
1758 // Because this function doesn't need to use scale (per the bc spec),
1759 // I am hijacking it to say whether it's doing an add or a subtract.
1760
1761 if (a->len == 0) {
1762 bc_num_copy(c, b);
1763 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001764 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001765 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001766 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001767 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001768 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001769 }
1770
1771 aneg = a->neg;
1772 bneg = b->neg;
1773 a->neg = b->neg = false;
1774
1775 cmp = bc_num_cmp(a, b);
1776
1777 a->neg = aneg;
1778 b->neg = bneg;
1779
1780 if (cmp == 0) {
1781 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001782 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001783 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001784 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001785 neg = a->neg;
1786 minuend = a;
1787 subtrahend = b;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001788 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001789 neg = b->neg;
1790 if (sub) neg = !neg;
1791 minuend = b;
1792 subtrahend = a;
1793 }
1794
1795 bc_num_copy(c, minuend);
1796 c->neg = neg;
1797
1798 if (c->rdx < subtrahend->rdx) {
1799 bc_num_extend(c, subtrahend->rdx - c->rdx);
1800 start = 0;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001801 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06001802 start = c->rdx - subtrahend->rdx;
1803
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001804 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001805
1806 bc_num_clean(c);
1807
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001808 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001809}
1810
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001811static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001812 BcNum *restrict c)
Denys Vlasenkoec603182018-12-17 10:34:02 +01001813#define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001814{
1815 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001816 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001817 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001818 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001819
Gavin Howard01055ba2018-11-03 11:00:21 -06001820 if (a->len == 0 || b->len == 0) {
1821 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001822 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001823 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001824 aone = BC_NUM_ONE(a);
1825 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001826 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001827 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001828 }
1829
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001830 if (a->len + b->len < BC_NUM_KARATSUBA_LEN
1831 || a->len < BC_NUM_KARATSUBA_LEN
1832 || b->len < BC_NUM_KARATSUBA_LEN
1833 ) {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001834 size_t i, j, len;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001835
Gavin Howard01055ba2018-11-03 11:00:21 -06001836 bc_num_expand(c, a->len + b->len + 1);
1837
1838 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001839 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001840
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001841 for (i = 0; i < b->len; ++i) {
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001842 unsigned carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001843 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001844 unsigned in = c->num[i + j];
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01001845 in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001846 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001847 carry = in / 10;
1848 c->num[i + j] = (BcDig)(in % 10);
1849 }
1850
1851 c->num[i + j] += (BcDig) carry;
1852 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001853
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001854#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001855 // a=2^1000000
1856 // a*a <- without check below, this will not be interruptible
1857 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001858#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001859 }
1860
1861 c->len = len;
1862
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001863 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001864 }
1865
1866 bc_num_init(&l1, max);
1867 bc_num_init(&h1, max);
1868 bc_num_init(&l2, max);
1869 bc_num_init(&h2, max);
1870 bc_num_init(&m1, max);
1871 bc_num_init(&m2, max);
1872 bc_num_init(&z0, max);
1873 bc_num_init(&z1, max);
1874 bc_num_init(&z2, max);
1875 bc_num_init(&temp, max + max);
1876
1877 bc_num_split(a, max2, &l1, &h1);
1878 bc_num_split(b, max2, &l2, &h2);
1879
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001880 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001881 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001882 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001883 if (s) goto err;
1884
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001885 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001886 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001887 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001888 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001889 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001890 if (s) goto err;
1891
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001892 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001893 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001894 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001895 if (s) goto err;
1896
Denys Vlasenko29301232018-12-11 15:29:32 +01001897 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001898 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001899 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001900 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001901 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001902 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001903 s = zbc_num_add(&temp, &z2, c, 0);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001904 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001905 bc_num_free(&temp);
1906 bc_num_free(&z2);
1907 bc_num_free(&z1);
1908 bc_num_free(&z0);
1909 bc_num_free(&m2);
1910 bc_num_free(&m1);
1911 bc_num_free(&h2);
1912 bc_num_free(&l2);
1913 bc_num_free(&h1);
1914 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001915 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001916}
1917
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001918static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001919{
1920 BcStatus s;
1921 BcNum cpa, cpb;
1922 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1923
1924 scale = BC_MAX(scale, a->rdx);
1925 scale = BC_MAX(scale, b->rdx);
1926 scale = BC_MIN(a->rdx + b->rdx, scale);
1927 maxrdx = BC_MAX(maxrdx, scale);
1928
1929 bc_num_init(&cpa, a->len);
1930 bc_num_init(&cpb, b->len);
1931
1932 bc_num_copy(&cpa, a);
1933 bc_num_copy(&cpb, b);
1934 cpa.neg = cpb.neg = false;
1935
Denys Vlasenko29301232018-12-11 15:29:32 +01001936 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001937 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001938 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001939 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001940 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001941 if (s) goto err;
1942
1943 maxrdx += scale;
1944 bc_num_expand(c, c->len + maxrdx);
1945
1946 if (c->len < maxrdx) {
1947 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1948 c->len += maxrdx;
1949 }
1950
1951 c->rdx = maxrdx;
1952 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01001953 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06001954 bc_num_free(&cpb);
1955 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001956 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001957}
Denys Vlasenkoec603182018-12-17 10:34:02 +01001958#define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06001959
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001960static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001961{
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01001962 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001963 size_t len, end, i;
1964 BcNum cp;
Gavin Howard01055ba2018-11-03 11:00:21 -06001965
1966 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001967 RETURN_STATUS(bc_error("divide by zero"));
1968 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001969 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001970 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001971 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001972 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001973 bc_num_copy(c, a);
1974 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001975 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001976 }
1977
1978 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1979 bc_num_copy(&cp, a);
1980 len = b->len;
1981
1982 if (len > cp.len) {
1983 bc_num_expand(&cp, len + 2);
1984 bc_num_extend(&cp, len - cp.len);
1985 }
1986
1987 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1988 cp.rdx -= b->rdx;
1989 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1990
1991 if (b->rdx == b->len) {
Denys Vlasenko71c82d12018-12-18 12:43:21 +01001992 for (;;) {
1993 if (len == 0) break;
1994 len--;
1995 if (b->num[len] != 0)
1996 break;
1997 }
1998 len++;
Gavin Howard01055ba2018-11-03 11:00:21 -06001999 }
2000
2001 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2002
2003 // We want an extra zero in front to make things simpler.
2004 cp.num[cp.len++] = 0;
2005 end = cp.len - len;
2006
2007 bc_num_expand(c, cp.len);
2008
2009 bc_num_zero(c);
2010 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2011 c->rdx = cp.rdx;
2012 c->len = cp.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06002013
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002014 s = BC_STATUS_SUCCESS;
2015 for (i = end - 1; i < end; --i) {
2016 BcDig *n, q;
Gavin Howard01055ba2018-11-03 11:00:21 -06002017 n = cp.num + i;
Denys Vlasenko5c0c5ab2018-12-18 13:15:55 +01002018 for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q)
2019 bc_num_subArrays(n, b->num, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002020 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002021#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002022 // a=2^100000
2023 // scale=40000
2024 // 1/a <- without check below, this will not be interruptible
2025 if (G_interrupt) {
2026 s = BC_STATUS_FAILURE;
2027 break;
2028 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002029#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002030 }
2031
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002032 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002033 bc_num_free(&cp);
2034
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002035 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002036}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002037#define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002038
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002039static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002040 BcNum *restrict d, size_t scale, size_t ts)
2041{
2042 BcStatus s;
2043 BcNum temp;
2044 bool neg;
2045
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002046 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002047 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002048
2049 if (a->len == 0) {
2050 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002051 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002052 }
2053
2054 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002055 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002056 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002057
2058 if (scale != 0) scale = ts;
2059
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002060 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002061 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002062 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002063 if (s) goto err;
2064
2065 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2066
2067 neg = d->neg;
2068 bc_num_retireMul(d, ts, a->neg, b->neg);
2069 d->neg = neg;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002070 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002071 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002072 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002073}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002074#define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002075
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002076static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002077{
2078 BcStatus s;
2079 BcNum c1;
2080 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2081
2082 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002083 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002084 bc_num_free(&c1);
2085
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002086 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002087}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002088#define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002089
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002090static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002091{
2092 BcStatus s = BC_STATUS_SUCCESS;
2093 BcNum copy;
2094 unsigned long pow;
2095 size_t i, powrdx, resrdx;
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002096 bool neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002097
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002098 // GNU bc does not allow 2^2.0 - we do
2099 for (i = 0; i < b->rdx; i++)
2100 if (b->num[i] != 0)
2101 RETURN_STATUS(bc_error("not an integer"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002102
2103 if (b->len == 0) {
2104 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002105 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002106 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002107 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002108 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002109 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002110 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002111 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002112 if (!b->neg)
2113 bc_num_copy(c, a);
2114 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002115 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002116 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 }
2118
2119 neg = b->neg;
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01002120 s = zbc_num_ulong_abs(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002121 if (s) RETURN_STATUS(s);
Denys Vlasenko2ea8ddf2018-12-22 21:45:18 +01002122 // b is not used beyond this point
Gavin Howard01055ba2018-11-03 11:00:21 -06002123
2124 bc_num_init(&copy, a->len);
2125 bc_num_copy(&copy, a);
2126
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002127 if (!neg) {
2128 if (a->rdx > scale)
2129 scale = a->rdx;
2130 if (a->rdx * pow < scale)
2131 scale = a->rdx * pow;
2132 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002133
Gavin Howard01055ba2018-11-03 11:00:21 -06002134
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002135 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002136 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002137 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002138 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002139 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002140 //if (G_interrupt) {
2141 // s = BC_STATUS_FAILURE;
2142 // goto err;
2143 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002144 }
2145
Gavin Howard01055ba2018-11-03 11:00:21 -06002146 bc_num_copy(c, &copy);
2147
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002148 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002149 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002150 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002151 if (s) goto err;
2152
2153 if (pow & 1) {
2154 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002155 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002156 if (s) goto err;
2157 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002158 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002159 //if (G_interrupt) {
2160 // s = BC_STATUS_FAILURE;
2161 // goto err;
2162 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002163 }
2164
2165 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002166 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002167 if (s) goto err;
2168 }
2169
Gavin Howard01055ba2018-11-03 11:00:21 -06002170 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2171
2172 // We can't use bc_num_clean() here.
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002173 for (i = 0; i < c->len; ++i)
2174 if (c->num[i] != 0)
2175 goto skip;
2176 bc_num_setToZero(c, scale);
2177 skip:
Gavin Howard01055ba2018-11-03 11:00:21 -06002178
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01002179 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002180 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002181 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002182}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002183#define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002184
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002185static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002186 BcNumBinaryOp op, size_t req)
2187{
2188 BcStatus s;
2189 BcNum num2, *ptr_a, *ptr_b;
2190 bool init = false;
2191
2192 if (c == a) {
2193 ptr_a = &num2;
2194 memcpy(ptr_a, c, sizeof(BcNum));
2195 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002196 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06002197 ptr_a = a;
2198
2199 if (c == b) {
2200 ptr_b = &num2;
2201 if (c != a) {
2202 memcpy(ptr_b, c, sizeof(BcNum));
2203 init = true;
2204 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002205 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06002206 ptr_b = b;
2207
2208 if (init)
2209 bc_num_init(c, req);
2210 else
2211 bc_num_expand(c, req);
2212
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002213 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01002214 IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002215
2216 if (init) bc_num_free(&num2);
2217
Denys Vlasenko29301232018-12-11 15:29:32 +01002218 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002219}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002220#define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002221
Denys Vlasenko9311e012018-12-10 11:54:18 +01002222static bool bc_num_strValid(const char *val, size_t base)
2223{
2224 BcDig b;
2225 bool radix;
2226
2227 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2228 radix = false;
2229 for (;;) {
2230 BcDig c = *val++;
2231 if (c == '\0')
2232 break;
2233 if (c == '.') {
2234 if (radix) return false;
2235 radix = true;
2236 continue;
2237 }
2238 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2239 return false;
2240 }
2241 return true;
2242}
2243
2244// Note: n is already "bc_num_zero()"ed,
2245// leading zeroes in "val" are removed
2246static void bc_num_parseDecimal(BcNum *n, const char *val)
2247{
2248 size_t len, i;
2249 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002250
2251 len = strlen(val);
2252 if (len == 0)
2253 return;
2254
Denys Vlasenko9311e012018-12-10 11:54:18 +01002255 bc_num_expand(n, len);
2256
2257 ptr = strchr(val, '.');
2258
2259 n->rdx = 0;
2260 if (ptr != NULL)
2261 n->rdx = (size_t)((val + len) - (ptr + 1));
2262
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002263 for (i = 0; val[i]; ++i) {
2264 if (val[i] != '0' && val[i] != '.') {
2265 // Not entirely zero value - convert it, and exit
2266 i = len - 1;
2267 for (;;) {
2268 n->num[n->len] = val[i] - '0';
2269 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002270 skip_dot:
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002271 if (i == 0) break;
2272 if (val[--i] == '.') goto skip_dot;
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002273 }
2274 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002275 }
2276 }
Denys Vlasenko87b49be2018-12-14 16:24:01 +01002277 // if for() exits without hitting if(), the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002278}
2279
2280// Note: n is already "bc_num_zero()"ed,
2281// leading zeroes in "val" are removed
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002282static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
Denys Vlasenko9311e012018-12-10 11:54:18 +01002283{
2284 BcStatus s;
2285 BcNum temp, mult, result;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002286 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01002287 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko9311e012018-12-10 11:54:18 +01002288 BcDig c = '\0';
2289 unsigned long v;
2290 size_t i, digits;
2291
2292 for (i = 0; ; ++i) {
2293 if (val[i] == '\0')
2294 return;
2295 if (val[i] != '.' && val[i] != '0')
2296 break;
2297 }
2298
2299 bc_num_init_DEF_SIZE(&temp);
2300 bc_num_init_DEF_SIZE(&mult);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01002301 base.cap = ARRAY_SIZE(base_digs);
2302 base.num = base_digs;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002303 bc_num_ulong2num(&base, base_t);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002304
2305 for (;;) {
2306 c = *val++;
2307 if (c == '\0') goto int_err;
2308 if (c == '.') break;
2309
2310 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2311
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002312 s = zbc_num_mul(n, &base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002313 if (s) goto int_err;
2314 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002315 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002316 if (s) goto int_err;
2317 }
2318
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002319 bc_num_init(&result, base.len);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002320 //bc_num_zero(&result); - already is
2321 bc_num_one(&mult);
2322
2323 digits = 0;
2324 for (;;) {
2325 c = *val++;
2326 if (c == '\0') break;
2327 digits++;
2328
2329 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2330
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002331 s = zbc_num_mul(&result, &base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002332 if (s) goto err;
2333 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002334 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002335 if (s) goto err;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002336 s = zbc_num_mul(&mult, &base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002337 if (s) goto err;
2338 }
2339
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002340 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002341 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002342 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002343 if (s) goto err;
2344
2345 if (n->len != 0) {
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002346 if (n->rdx < digits)
2347 bc_num_extend(n, digits - n->rdx);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002348 } else
2349 bc_num_zero(n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002350 err:
Denys Vlasenko9311e012018-12-10 11:54:18 +01002351 bc_num_free(&result);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002352 int_err:
Denys Vlasenko9311e012018-12-10 11:54:18 +01002353 bc_num_free(&mult);
2354 bc_num_free(&temp);
2355}
2356
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002357static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
Gavin Howard01055ba2018-11-03 11:00:21 -06002358{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002359 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002360 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002361
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002362 bc_num_zero(n);
2363 while (*val == '0') val++;
2364
Gavin Howard01055ba2018-11-03 11:00:21 -06002365 if (base_t == 10)
2366 bc_num_parseDecimal(n, val);
2367 else
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01002368 bc_num_parseBase(n, val, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002369
Denys Vlasenko29301232018-12-11 15:29:32 +01002370 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002371}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002372#define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002373
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002374static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002375{
2376 BcStatus s;
2377 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2378 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2379 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2380
2381 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2382 bc_num_expand(b, req);
2383
2384 if (a->len == 0) {
2385 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002386 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002387 }
2388 if (a->neg) {
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002389 RETURN_STATUS(bc_error("negative number"));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01002390 }
2391 if (BC_NUM_ONE(a)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002392 bc_num_one(b);
2393 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002394 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002395 }
2396
2397 scale = BC_MAX(scale, a->rdx) + 1;
2398 len = a->len + scale;
2399
2400 bc_num_init(&num1, len);
2401 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002402 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002403
2404 bc_num_one(&half);
2405 half.num[0] = 5;
2406 half.rdx = 1;
2407
2408 bc_num_init(&f, len);
2409 bc_num_init(&fprime, len);
2410
2411 x0 = &num1;
2412 x1 = &num2;
2413
2414 bc_num_one(x0);
2415 pow = BC_NUM_INT(a);
2416
2417 if (pow) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002418 if (pow & 1)
2419 x0->num[0] = 2;
2420 else
2421 x0->num[0] = 6;
2422
2423 pow -= 2 - (pow & 1);
2424
2425 bc_num_extend(x0, pow);
2426
2427 // Make sure to move the radix back.
2428 x0->rdx -= pow;
2429 }
2430
2431 x0->rdx = digs = digs1 = 0;
2432 resrdx = scale + 2;
2433 len = BC_NUM_INT(x0) + resrdx - 1;
2434
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002435 while (cmp != 0 || digs < len) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002436 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002437 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002438 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002439 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002440 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002441 if (s) goto err;
2442
2443 cmp = bc_num_cmp(x1, x0);
2444 digs = x1->len - (unsigned long long) llabs(cmp);
2445
2446 if (cmp == cmp2 && digs == digs1)
2447 times += 1;
2448 else
2449 times = 0;
2450
2451 resrdx += times > 4;
2452
2453 cmp2 = cmp1;
2454 cmp1 = cmp;
2455 digs1 = digs;
2456
2457 temp = x0;
2458 x0 = x1;
2459 x1 = temp;
2460 }
2461
Gavin Howard01055ba2018-11-03 11:00:21 -06002462 bc_num_copy(b, x0);
2463 scale -= 1;
2464 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002465 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002466 bc_num_free(&fprime);
2467 bc_num_free(&f);
2468 bc_num_free(&half);
2469 bc_num_free(&num2);
2470 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002471 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002472}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002473#define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002474
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002475static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002476 size_t scale)
2477{
2478 BcStatus s;
2479 BcNum num2, *ptr_a;
2480 bool init = false;
2481 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2482
2483 if (c == a) {
2484 memcpy(&num2, c, sizeof(BcNum));
2485 ptr_a = &num2;
2486 bc_num_init(c, len);
2487 init = true;
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002488 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06002489 ptr_a = a;
2490 bc_num_expand(c, len);
2491 }
2492
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002493 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002494
2495 if (init) bc_num_free(&num2);
2496
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002497 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002498}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002499#define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002500
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002501#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002502static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002503{
2504 BcStatus s;
2505 BcNum base, exp, two, temp;
2506
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002507 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002508 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002509 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002510 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002511 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002512 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002513
2514 bc_num_expand(d, c->len);
2515 bc_num_init(&base, c->len);
2516 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002517 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002518 bc_num_init(&temp, b->len);
2519
2520 bc_num_one(&two);
2521 two.num[0] = 2;
2522 bc_num_one(d);
2523
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002524 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002525 if (s) goto err;
2526 bc_num_copy(&exp, b);
2527
2528 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002529 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002530 if (s) goto err;
2531
2532 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002533 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002534 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002535 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002536 if (s) goto err;
2537 }
2538
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002539 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002540 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002541 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002542 if (s) goto err;
2543 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002544 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002545 bc_num_free(&temp);
2546 bc_num_free(&two);
2547 bc_num_free(&exp);
2548 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002549 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002550}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002551#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002552#endif // ENABLE_DC
2553
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002554#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002555static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002556{
Denys Vlasenko87888ce2018-12-19 17:55:23 +01002557 BcId *autoid;
Gavin Howard01055ba2018-11-03 11:00:21 -06002558 BcId a;
2559 size_t i;
2560
Denys Vlasenko87888ce2018-12-19 17:55:23 +01002561 autoid = (void*)f->autos.v;
2562 for (i = 0; i < f->autos.len; i++, autoid++) {
2563 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002564 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002565 }
2566
2567 a.idx = var;
2568 a.name = name;
2569
2570 bc_vec_push(&f->autos, &a);
2571
Denys Vlasenko29301232018-12-11 15:29:32 +01002572 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002573}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002574#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002575#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002576
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002577static FAST_FUNC void bc_string_free(void *string)
2578{
2579 free(*(char**)string);
2580}
2581
Gavin Howard01055ba2018-11-03 11:00:21 -06002582static void bc_func_init(BcFunc *f)
2583{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002584 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002585 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2586 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002587 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2588 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002589 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002590}
2591
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002592static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002593{
2594 BcFunc *f = (BcFunc *) func;
2595 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002596 IF_BC(bc_vec_free(&f->labels);)
2597 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002598 IF_BC(bc_vec_free(&f->strs);)
2599 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002600}
2601
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002602static void bc_array_expand(BcVec *a, size_t len);
2603
Gavin Howard01055ba2018-11-03 11:00:21 -06002604static void bc_array_init(BcVec *a, bool nums)
2605{
2606 if (nums)
2607 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2608 else
2609 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2610 bc_array_expand(a, 1);
2611}
2612
Gavin Howard01055ba2018-11-03 11:00:21 -06002613static void bc_array_expand(BcVec *a, size_t len)
2614{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002615 if (a->dtor == bc_num_free
2616 // && a->size == sizeof(BcNum) - always true
2617 ) {
2618 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002619 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002620 bc_num_init_DEF_SIZE(&n);
2621 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002622 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002623 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002624 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002625 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002626 bc_array_init(&v, true);
2627 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002628 }
2629 }
2630}
2631
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002632static void bc_array_copy(BcVec *d, const BcVec *s)
2633{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002634 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002635 size_t i;
2636
2637 bc_vec_pop_all(d);
2638 bc_vec_expand(d, s->cap);
2639 d->len = s->len;
2640
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002641 dnum = (void*)d->v;
2642 snum = (void*)s->v;
2643 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002644 bc_num_init(dnum, snum->len);
2645 bc_num_copy(dnum, snum);
2646 }
2647}
2648
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002649#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002650static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002651{
2652 d->t = src->t;
2653
2654 switch (d->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002655 case BC_RESULT_TEMP:
2656 case BC_RESULT_IBASE:
2657 case BC_RESULT_SCALE:
2658 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002659 bc_num_init(&d->d.n, src->d.n.len);
2660 bc_num_copy(&d->d.n, &src->d.n);
2661 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002662 case BC_RESULT_VAR:
2663 case BC_RESULT_ARRAY:
2664 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002665 d->d.id.name = xstrdup(src->d.id.name);
2666 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002667 case BC_RESULT_CONSTANT:
2668 case BC_RESULT_LAST:
2669 case BC_RESULT_ONE:
2670 case BC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002671 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2672 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002673 }
2674}
2675#endif // ENABLE_DC
2676
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002677static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002678{
2679 BcResult *r = (BcResult *) result;
2680
2681 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002682 case BC_RESULT_TEMP:
2683 case BC_RESULT_IBASE:
2684 case BC_RESULT_SCALE:
2685 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002686 bc_num_free(&r->d.n);
2687 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002688 case BC_RESULT_VAR:
2689 case BC_RESULT_ARRAY:
2690 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002691 free(r->d.id.name);
2692 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002693 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002694 // Do nothing.
2695 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002696 }
2697}
2698
2699static void bc_lex_lineComment(BcLex *l)
2700{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002701 // Try: echo -n '#foo' | bc
2702 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002703 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002704 i = l->i;
2705 while (i < l->len && l->buf[i] != '\n')
2706 i++;
2707 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002708}
2709
2710static void bc_lex_whitespace(BcLex *l)
2711{
Gavin Howard01055ba2018-11-03 11:00:21 -06002712 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002713 for (;;) {
2714 char c = l->buf[l->i];
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002715 if (c == '\n') // this is BC_LEX_NLINE, not BC_LEX_WHITESPACE
2716 break;
2717 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002718 break;
2719 l->i++;
2720 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002721}
2722
Denys Vlasenko29301232018-12-11 15:29:32 +01002723static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002724{
2725 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002726 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002727 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002728
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002729 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002730 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002731 ccnt = i = 0;
2732 for (;;) {
2733 char c = buf[i];
2734 if (c == '\0')
2735 break;
2736 if (c == '\\' && buf[i + 1] == '\n') {
2737 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002738 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002739 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002740 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002741 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2742 if (c != '.') break;
2743 // if '.' was already seen, stop on second one:
2744 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002745 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002746 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002747 // buf[i] is one of "0-9A-F."
2748 i++;
2749 if (c != '.')
2750 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002751 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002752 //ccnt is the number of chars in the number string, excluding possible
2753 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2754 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002755 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002756
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002757 // This might overestimate the size, if there are "\<NL>"'s
2758 // in the number. Subtracting number_of_backslashes*2 correctly
2759 // is not that easy: consider that in the case of "NNN.\<NL>"
2760 // loop above will count "\<NL>" before it realizes it is not
2761 // in fact *inside* the number:
2762 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002763
Denys Vlasenko64074a12018-12-07 15:50:14 +01002764 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2765 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2766 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002767 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002768 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002769
Denys Vlasenko7d628012018-12-04 21:46:47 +01002770 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002771 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002772 bc_vec_push(&l->t.v, &start);
2773
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002774 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002775 // If we have hit a backslash, skip it. We don't have
2776 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002777 if (*buf == '\\') {
2778 buf += 2;
2779 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002780 continue;
2781 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002782 bc_vec_push(&l->t.v, buf);
2783 buf++;
2784 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002785 }
2786
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002787 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002788
Denys Vlasenko29301232018-12-11 15:29:32 +01002789 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002790}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002791#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002792
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002793static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002794{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002795 size_t i;
2796 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002797
2798 l->t.t = BC_LEX_NAME;
2799
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002800 i = 0;
2801 buf = l->buf + l->i - 1;
2802 for (;;) {
2803 char c = buf[i];
2804 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2805 i++;
2806 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002807
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002808#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002809 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2810 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2811 if (i > BC_MAX_STRING)
2812 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2813 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002814#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002815 bc_vec_string(&l->t.v, i, buf);
2816
2817 // Increment the index. We minus 1 because it has already been incremented.
2818 l->i += i - 1;
2819
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002820 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002821}
2822
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002823static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002824{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002825 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002826}
2827
2828static void bc_lex_free(BcLex *l)
2829{
2830 bc_vec_free(&l->t.v);
2831}
2832
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002833static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002834{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002835 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002836 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002837}
2838
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002839static bool bc_lex_more_input(BcLex *l)
2840{
2841 size_t str;
2842 bool comment;
2843
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002844 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002845
2846 // This loop is complex because the vm tries not to send any lines that end
2847 // with a backslash to the parser. The reason for that is because the parser
2848 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2849 // case, and for strings and comments, the parser will expect more stuff.
2850 comment = false;
2851 str = 0;
2852 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002853 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002854 char *string;
2855
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002856 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002857 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002858 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002859 break;
2860
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002861 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002862 while (*string) {
2863 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002864 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002865 if (IS_BC)
2866 str ^= (c == '"');
2867 else {
2868 if (c == ']')
2869 str -= 1;
2870 else if (c == '[')
2871 str += 1;
2872 }
2873 }
2874 string++;
2875 if (c == '/' && *string == '*') {
2876 comment = true;
2877 string++;
2878 continue;
2879 }
2880 if (c == '*' && *string == '/') {
2881 comment = false;
2882 string++;
2883 }
2884 }
2885 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002886 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002887 continue;
2888 }
2889
2890 // Check for backslash+newline.
2891 // we do not check that last char is '\n' -
2892 // if it is not, then it's EOF, and looping back
2893 // to bc_read_line() will detect it:
2894 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002895 if (string >= G.input_buffer.v && *string == '\\') {
2896 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002897 continue;
2898 }
2899
2900 break;
2901 }
2902
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002903 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002904 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002905// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2906 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002907
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002908 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002909}
2910
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002911IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2912IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002913#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2914#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002915
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002916static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002917{
2918 BcStatus s;
2919
2920 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002921 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002922
2923 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002924 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002925 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002926
2927 // Loop until failure or we don't have whitespace. This
2928 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002929 // Comments are also BC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002930 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002931 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002932 if (l->i == l->len) {
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002933 l->t.t = BC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002934 if (!G.input_fp)
2935 RETURN_STATUS(BC_STATUS_SUCCESS);
2936 if (!bc_lex_more_input(l)) {
2937 G.input_fp = NULL;
2938 RETURN_STATUS(BC_STATUS_SUCCESS);
2939 }
2940 // here it's guaranteed that l->i is below l->len
2941 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002942 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002943 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2944 l->buf + l->i);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002945 if (IS_BC) {
2946 IF_BC(s = zbc_lex_token(l));
2947 } else {
2948 IF_DC(s = zdc_lex_token(l));
2949 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002950 } while (!s && l->t.t == BC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01002951 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002952
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002953 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002954}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002955#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002956
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002957#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01002958static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
2959{
2960 if (l->t.t == BC_LEX_NLINE)
2961 RETURN_STATUS(zbc_lex_next(l));
2962 RETURN_STATUS(BC_STATUS_SUCCESS);
2963}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002964#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002965
2966static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
2967{
2968 BcStatus s;
2969 s = zbc_lex_next(l);
2970 if (s) RETURN_STATUS(s);
2971 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
2972 s = zbc_lex_skip_if_at_NLINE(l);
2973 RETURN_STATUS(s);
2974}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002975#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002976#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01002977
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002978static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002979{
2980 l->buf = text;
2981 l->i = 0;
2982 l->len = strlen(text);
2983 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002984 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002985}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002986#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002987
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002988#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002989static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002990{
2991 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002992 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002993 const char *buf = l->buf + l->i - 1;
2994
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002995 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2996 const char *keyword8 = bc_lex_kws[i].name8;
2997 unsigned j = 0;
2998 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2999 j++;
3000 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003001 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003002 if (keyword8[j] != '\0')
3003 continue;
3004 match:
3005 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003006 if (isalnum(buf[j]) || buf[j]=='_')
3007 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003008 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003009 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003010 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003011 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003012 }
3013
3014 // We minus 1 because the index has already been incremented.
3015 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003016 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003017 }
3018
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003019 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003020 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003021
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003022 if (l->t.v.len > 2) {
3023 // Prevent this:
3024 // >>> qwe=1
3025 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3026 // '
3027 unsigned len = strchrnul(buf, '\n') - buf;
3028 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3029 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003030
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003031 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003032}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003033#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003034
Denys Vlasenko26819db2018-12-12 16:08:46 +01003035static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003036{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003037 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003038
3039 l->t.t = BC_LEX_STR;
3040
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003041 nls = 0;
3042 i = l->i;
3043 for (;;) {
3044 char c = l->buf[i];
3045 if (c == '\0') {
3046 l->i = i;
3047 RETURN_STATUS(bc_error("string end could not be found"));
3048 }
3049 if (c == '"')
3050 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003051 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003052 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003053 }
3054
3055 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003056 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3057 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3058 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003059 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003060 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003061 bc_vec_string(&l->t.v, len, l->buf + l->i);
3062
3063 l->i = i + 1;
3064 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003065 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003066
Denys Vlasenko26819db2018-12-12 16:08:46 +01003067 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003068}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003069#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003070
Denys Vlasenko0a238142018-12-14 16:48:34 +01003071static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003072{
3073 if (l->buf[l->i] == '=') {
3074 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003075 with_and_without >>= 8; // store "with" value
3076 } // else store "without" value
3077 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003078}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003079#define bc_lex_assign(l, with, without) \
3080 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003081
Denys Vlasenko29301232018-12-11 15:29:32 +01003082static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003083{
3084 size_t i, nls = 0;
3085 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003086
3087 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003088 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003089 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003090 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003091 check_star:
3092 if (c == '*') {
3093 c = buf[++i];
3094 if (c == '/')
3095 break;
3096 goto check_star;
3097 }
3098 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003099 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003100 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003101 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003102 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003103 }
3104
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003105 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003106 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003107 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003108
Denys Vlasenko29301232018-12-11 15:29:32 +01003109 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003110}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003111#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003112
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003113#undef zbc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003114static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003115{
3116 BcStatus s = BC_STATUS_SUCCESS;
3117 char c = l->buf[l->i++], c2;
3118
3119 // This is the workhorse of the lexer.
3120 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003121// case '\0': // probably never reached
3122// l->i--;
3123// l->t.t = BC_LEX_EOF;
3124// l->newline = true;
3125// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003126 case '\n':
3127 l->t.t = BC_LEX_NLINE;
3128 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003129 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003130 case '\t':
3131 case '\v':
3132 case '\f':
3133 case '\r':
3134 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003135 bc_lex_whitespace(l);
3136 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003138 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003139 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003140 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003141 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003142 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003143 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003145 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003146 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003147 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003148 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003149 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003151 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_MODULUS, BC_LEX_OP_MODULUS);
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 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003158 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003159 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003160 ++l->i;
3161 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003162 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003163 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003164 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003165 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003167 case '(':
3168 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003169 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3170 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003171 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003172 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3173 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003174 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003175 c2 = l->buf[l->i];
3176 if (c2 == '+') {
3177 ++l->i;
3178 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003179 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003180 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3181 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003182 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003183 l->t.t = BC_LEX_COMMA;
3184 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003185 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003186 c2 = l->buf[l->i];
3187 if (c2 == '-') {
3188 ++l->i;
3189 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003190 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003191 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3192 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003193 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003194 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003195 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 else {
3197 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003198 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 }
3200 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003201 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003202 c2 = l->buf[l->i];
3203 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003204 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 else
3206 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3207 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 case '0':
3209 case '1':
3210 case '2':
3211 case '3':
3212 case '4':
3213 case '5':
3214 case '6':
3215 case '7':
3216 case '8':
3217 case '9':
3218 case 'A':
3219 case 'B':
3220 case 'C':
3221 case 'D':
3222 case 'E':
3223 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003224 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003225 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003226 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003227 l->t.t = BC_LEX_SCOLON;
3228 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003229 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003230 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3231 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003232 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003233 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3234 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003235 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003236 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3237 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003238 case '[':
3239 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003240 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3241 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003242 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003243 if (l->buf[l->i] == '\n') {
3244 l->t.t = BC_LEX_WHITESPACE;
3245 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003246 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003247 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003248 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003249 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3251 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 case 'a':
3253 case 'b':
3254 case 'c':
3255 case 'd':
3256 case 'e':
3257 case 'f':
3258 case 'g':
3259 case 'h':
3260 case 'i':
3261 case 'j':
3262 case 'k':
3263 case 'l':
3264 case 'm':
3265 case 'n':
3266 case 'o':
3267 case 'p':
3268 case 'q':
3269 case 'r':
3270 case 's':
3271 case 't':
3272 case 'u':
3273 case 'v':
3274 case 'w':
3275 case 'x':
3276 case 'y':
3277 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003278 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003280 case '{':
3281 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003282 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3283 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003284 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003286 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003287 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003288 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003289 ++l->i;
3290 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003291 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003292 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003293 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003294 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003295 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003296 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003297 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003298 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003299 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003300 }
3301
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003302 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003303}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003304#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003305#endif // ENABLE_BC
3306
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003307#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003308static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003309{
Gavin Howard01055ba2018-11-03 11:00:21 -06003310 if (isspace(l->buf[l->i - 1])) {
3311 bc_lex_whitespace(l);
3312 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003313 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003314 RETURN_STATUS(bc_error("extended register"));
3315 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003316 }
3317 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003318 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003319 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003320 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003321 l->t.t = BC_LEX_NAME;
3322 }
3323
Denys Vlasenko29301232018-12-11 15:29:32 +01003324 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003325}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003326#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003327
Denys Vlasenko29301232018-12-11 15:29:32 +01003328static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003329{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003330 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003331
3332 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003333 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003334
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003335 nls = 0;
3336 depth = 1;
3337 i = l->i;
3338 for (;;) {
3339 char c = l->buf[i];
3340 if (c == '\0') {
3341 l->i = i;
3342 RETURN_STATUS(bc_error("string end could not be found"));
3343 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003344 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003345 if (i == l->i || l->buf[i - 1] != '\\') {
3346 if (c == '[') depth++;
3347 if (c == ']')
3348 if (--depth == 0)
3349 break;
3350 }
3351 bc_vec_push(&l->t.v, &l->buf[i]);
3352 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003353 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003354 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003355
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003356 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003357 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3358 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3359 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003360 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003361 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003362
3363 l->i = i;
3364 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003365 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003366
Denys Vlasenko29301232018-12-11 15:29:32 +01003367 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003368}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003369#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003370
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003371#undef zdc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003372static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003373{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003374 static const //BcLexType - should be this type, but narrower type saves size:
3375 uint8_t
3376 dc_lex_regs[] = {
3377 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
3378 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
3379 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
3380 BC_LEX_STORE_PUSH,
3381 };
3382 static const //BcLexType - should be this type
3383 uint8_t
3384 dc_lex_tokens[] = {
3385 /* %&'( */
3386 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
3387 /* )*+, */
3388 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
3389 /* -./ */
3390 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
3391 /* 0123456789 */
3392 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3393 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3394 BC_LEX_INVALID, BC_LEX_INVALID,
3395 /* :;<=>?@ */
3396 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
3397 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
3398 /* ABCDEFGH */
3399 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3400 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
3401 /* IJKLMNOP */
3402 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
3403 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
3404 /* QRSTUVWXY */
3405 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
3406 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
3407 /* Z[\] */
3408 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3409 /* ^_` */
3410 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
3411 /* abcdefgh */
3412 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
3413 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
3414 /* ijklmnop */
3415 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
3416 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
3417 /* qrstuvwx */
3418 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
3419 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
3420 /* yz */
3421 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
3422 /* {|}~ */
3423 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
3424 };
3425
Gavin Howard01055ba2018-11-03 11:00:21 -06003426 BcStatus s = BC_STATUS_SUCCESS;
3427 char c = l->buf[l->i++], c2;
3428 size_t i;
3429
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003430 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3431 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003432 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003433 }
3434
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003435 if (c >= '%' && c <= '~'
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003436 && (l->t.t = dc_lex_tokens[c - '%']) != BC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003437 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003438 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003439 }
3440
3441 // This is the workhorse of the lexer.
3442 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003443// case '\0': // probably never reached
3444// l->t.t = BC_LEX_EOF;
3445// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003446 case '\n':
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003447 // '\n' is BC_LEX_NLINE, not BC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003448 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003449 // only to allow interactive dc have a way to exit
3450 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003451 // on <enter>, not on _next_ token (which would mean
3452 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003453 // IOW: typing "1p<enter>" should print "1" _at once_,
3454 // not after some more input.
3455 l->t.t = BC_LEX_NLINE;
3456 l->newline = true;
3457 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003458 case '\t':
3459 case '\v':
3460 case '\f':
3461 case '\r':
3462 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003463 l->newline = (c == '\n');
3464 bc_lex_whitespace(l);
3465 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003466 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003467 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003468 if (c2 == '=')
3469 l->t.t = BC_LEX_OP_REL_NE;
3470 else if (c2 == '<')
3471 l->t.t = BC_LEX_OP_REL_LE;
3472 else if (c2 == '>')
3473 l->t.t = BC_LEX_OP_REL_GE;
3474 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003475 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003476 ++l->i;
3477 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003478 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003479 bc_lex_lineComment(l);
3480 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003481 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003482 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003483 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003484 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003485 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003486 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003487 case '0':
3488 case '1':
3489 case '2':
3490 case '3':
3491 case '4':
3492 case '5':
3493 case '6':
3494 case '7':
3495 case '8':
3496 case '9':
3497 case 'A':
3498 case 'B':
3499 case 'C':
3500 case 'D':
3501 case 'E':
3502 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003503 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003504 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003505 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003506 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003507 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003508 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003509 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003510 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003511 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003512 }
3513
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003514 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003515}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003516#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003517#endif // ENABLE_DC
3518
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003519static void bc_parse_push(BcParse *p, char i)
3520{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003521 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003522 bc_vec_pushByte(&p->func->code, i);
3523}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003524
Gavin Howard01055ba2018-11-03 11:00:21 -06003525static void bc_parse_pushName(BcParse *p, char *name)
3526{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003527 while (*name)
3528 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003529 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003530}
3531
3532static void bc_parse_pushIndex(BcParse *p, size_t idx)
3533{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003534 size_t mask;
3535 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003536
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003537 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003538 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3539 amt = sizeof(idx);
3540 do {
3541 if (idx & mask) break;
3542 mask >>= 8;
3543 amt--;
3544 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003545
3546 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003547
3548 while (idx != 0) {
3549 bc_parse_push(p, (unsigned char)idx);
3550 idx >>= 8;
3551 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003552}
3553
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003554#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003555static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3556{
3557 bc_parse_push(p, BC_INST_JUMP);
3558 bc_parse_pushIndex(p, idx);
3559}
3560
3561static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3562{
3563 bc_parse_push(p, BC_INST_JUMP_ZERO);
3564 bc_parse_pushIndex(p, idx);
3565}
3566
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003567static BC_STATUS zbc_parse_pushSTR(BcParse *p)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003568{
3569 char *str = xstrdup(p->l.t.v.v);
3570
3571 bc_parse_push(p, BC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003572 bc_parse_pushIndex(p, p->func->strs.len);
3573 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003574
3575 RETURN_STATUS(zbc_lex_next(&p->l));
3576}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003577#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003578#endif
3579
3580static void bc_parse_pushNUM(BcParse *p)
3581{
3582 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003583#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003584 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003585#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003586 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003587#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003588 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003589#endif
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003590 bc_parse_push(p, BC_INST_NUM);
3591 bc_parse_pushIndex(p, idx);
3592}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003593
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003594static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003595{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003596 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003597
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003598 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003599}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003600#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003601
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003602// Called when parsing or execution detects a failure,
3603// resets execution structures.
3604static void bc_program_reset(void)
3605{
3606 BcFunc *f;
3607 BcInstPtr *ip;
3608
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003609 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003610 bc_vec_pop_all(&G.prog.results);
3611
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003612 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003613 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003614 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003615}
3616
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003617// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003618// resets parsing structures.
3619static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003620{
3621 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003622 bc_func_free(p->func);
3623 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003624
Denys Vlasenko65e10462018-12-19 15:13:14 +01003625 p->fidx = BC_PROG_MAIN;
3626 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003627 }
3628
3629 p->l.i = p->l.len;
3630 p->l.t.t = BC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003631
Denys Vlasenko503faf92018-12-20 16:24:18 +01003632 IF_BC(bc_vec_pop_all(&p->exits);)
3633 IF_BC(bc_vec_pop_all(&p->conds);)
3634 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003635
Denys Vlasenkod38af482018-12-04 19:11:02 +01003636 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003637}
3638
3639static void bc_parse_free(BcParse *p)
3640{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003641 IF_BC(bc_vec_free(&p->exits);)
3642 IF_BC(bc_vec_free(&p->conds);)
3643 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003644 bc_lex_free(&p->l);
3645}
3646
Denys Vlasenkofa210792018-12-19 19:35:40 +01003647static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003648{
3649 memset(p, 0, sizeof(BcParse));
3650
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003651 bc_lex_init(&p->l);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003652 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3653 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3654 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003655
Denys Vlasenkofa210792018-12-19 19:35:40 +01003656 p->fidx = fidx;
3657 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003658}
3659
Denys Vlasenko04715442018-12-21 00:10:26 +01003660static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003661{
Denys Vlasenko04715442018-12-21 00:10:26 +01003662 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003663 BcFunc f;
3664 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003665 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003666 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003667 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003668}
3669
3670#if ENABLE_BC
3671
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003672// Note: takes ownership of 'name' (must be malloced)
3673static size_t bc_program_addFunc(char *name)
3674{
3675 size_t idx;
3676 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003677 int inserted;
3678
3679 entry.name = name;
3680 entry.idx = G.prog.fns.len;
3681
3682 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3683 if (!inserted) free(name);
3684
3685 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3686 idx = entry_ptr->idx;
3687
3688 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003689 // There is already a function with this name.
3690 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003691 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003692 bc_func_free(func);
3693 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003694 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003695 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003696 }
3697
3698 return idx;
3699}
3700
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003701#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3702#define BC_PARSE_LEAF(p, rparen) \
3703 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3704 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3705
3706// We can calculate the conversion between tokens and exprs by subtracting the
3707// position of the first operator in the lex enum and adding the position of the
3708// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko0154d782018-12-14 23:32:51 +01003709#define BC_TOKEN_2_INST(t) ((char) ((t) - BC_LEX_NEG + BC_INST_NEG))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003710
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003711static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags);
3712
3713static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags)
3714{
3715 BcStatus s;
3716
3717 s = bc_parse_expr_empty_ok(p, flags);
3718 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3719 RETURN_STATUS(bc_error("empty expression"));
3720 RETURN_STATUS(s);
3721}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003722#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003723
3724static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003725#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003726
3727static BC_STATUS zbc_parse_stmt(BcParse *p)
3728{
3729 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3730}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003731#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003732
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003733static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003734{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003735 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3736 // Same for "else", "while()", "for()".
3737 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3738 if (s) RETURN_STATUS(s);
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003739 if (p->l.t.t == BC_LEX_NLINE)
3740 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003741
3742 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003743}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003744#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003745
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003746static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3747 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003748{
Denys Vlasenko65437582018-12-05 19:37:19 +01003749 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3750 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003751
3752 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003753 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003754 if (t == BC_LEX_LPAREN) break;
3755
Denys Vlasenko65437582018-12-05 19:37:19 +01003756 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003757 if (l >= r && (l != r || !left)) break;
3758
Denys Vlasenko0154d782018-12-14 23:32:51 +01003759 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003760 bc_vec_pop(&p->ops);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003761 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003762 }
3763
3764 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003765}
3766
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003767static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003768{
3769 BcLexType top;
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 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003776 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003777
3778 bc_vec_pop(&p->ops);
3779 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3780
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003781 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003782 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003783 top = BC_PARSE_TOP_OP(p);
3784 }
3785
3786 bc_vec_pop(&p->ops);
3787
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003788 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003789}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003790#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003791
Denys Vlasenko26819db2018-12-12 16:08:46 +01003792static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003793{
3794 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003795 size_t nparams;
3796
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003797 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003798 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3799
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003800 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003801 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003802
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003803 nparams = 0;
3804 if (p->l.t.t != BC_LEX_RPAREN) {
3805 for (;;) {
3806 s = zbc_parse_expr(p, flags);
3807 if (s) RETURN_STATUS(s);
3808 nparams++;
3809 if (p->l.t.t != BC_LEX_COMMA) {
3810 if (p->l.t.t == BC_LEX_RPAREN)
3811 break;
3812 RETURN_STATUS(bc_error_bad_token());
3813 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003814 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003815 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003816 }
3817 }
3818
Gavin Howard01055ba2018-11-03 11:00:21 -06003819 bc_parse_push(p, BC_INST_CALL);
3820 bc_parse_pushIndex(p, nparams);
3821
Denys Vlasenko26819db2018-12-12 16:08:46 +01003822 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003823}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003824#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003825
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003826// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003827static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003828{
3829 BcStatus s;
3830 BcId entry, *entry_ptr;
3831 size_t idx;
3832
3833 entry.name = name;
3834
Denys Vlasenko26819db2018-12-12 16:08:46 +01003835 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003836 if (s) goto err;
3837
3838 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003839 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003840 goto err;
3841 }
3842
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003843 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003844
3845 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003846 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003847 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003848 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003849 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003850 free(name);
3851
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003852 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003853 bc_parse_pushIndex(p, entry_ptr->idx);
3854
Denys Vlasenko26819db2018-12-12 16:08:46 +01003855 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003856 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003857 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003858 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003859}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003860#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003861
Denys Vlasenko26819db2018-12-12 16:08:46 +01003862static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003863{
3864 BcStatus s;
3865 char *name;
3866
3867 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003868 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003869 if (s) goto err;
3870
3871 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003872 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003873 if (s) goto err;
3874
3875 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003876 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003877 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003878 goto err;
3879 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003880 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003881 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003882 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003883 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003884 s = zbc_parse_expr(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 if (s) goto err;
3886 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003887 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003888 if (s) goto err;
3889 bc_parse_push(p, *type);
3890 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003891 free(name);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003892 } else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003893 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003894 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003895 goto err;
3896 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003898 s = zbc_parse_call(p, name, flags);
3899 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003900 *type = BC_INST_VAR;
3901 bc_parse_push(p, BC_INST_VAR);
3902 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003903 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003904 }
3905
Denys Vlasenko26819db2018-12-12 16:08:46 +01003906 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003907 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003908 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003909 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003910}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003911#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003912
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003913static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003914{
3915 BcStatus s;
3916
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003917 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003918 if (s) RETURN_STATUS(s);
3919 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003920
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003921 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003922 if (s) RETURN_STATUS(s);
3923 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003924
3925 bc_parse_push(p, BC_INST_READ);
3926
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003927 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003928}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003929#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003930
Denys Vlasenko26819db2018-12-12 16:08:46 +01003931static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003932 BcInst *prev)
3933{
3934 BcStatus s;
3935
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003936 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003937 if (s) RETURN_STATUS(s);
3938 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003939
3940 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3941
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003942 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003943 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003944
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003945 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003946 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003947
Denys Vlasenko26819db2018-12-12 16:08:46 +01003948 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003949
3950 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3951 bc_parse_push(p, *prev);
3952
Denys Vlasenko26819db2018-12-12 16:08:46 +01003953 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003954}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003955#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003956
Denys Vlasenko26819db2018-12-12 16:08:46 +01003957static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003958{
3959 BcStatus s;
3960
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003961 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003962 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003963
3964 if (p->l.t.t != BC_LEX_LPAREN) {
3965 *type = BC_INST_SCALE;
3966 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003967 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003968 }
3969
3970 *type = BC_INST_SCALE_FUNC;
3971 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3972
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003973 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003974 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003975
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003976 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003977 if (s) RETURN_STATUS(s);
3978 if (p->l.t.t != BC_LEX_RPAREN)
3979 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003980 bc_parse_push(p, BC_INST_SCALE_FUNC);
3981
Denys Vlasenko26819db2018-12-12 16:08:46 +01003982 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003983}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003984#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003985
Denys Vlasenko26819db2018-12-12 16:08:46 +01003986static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003987 size_t *nexprs, uint8_t flags)
3988{
3989 BcStatus s;
3990 BcLexType type;
3991 char inst;
3992 BcInst etype = *prev;
3993
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003994 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM
3995 || etype == BC_INST_SCALE || etype == BC_INST_LAST
3996 || etype == BC_INST_IBASE || etype == BC_INST_OBASE
3997 ) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003998 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3999 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004000 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004001 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004002 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
4003 *paren_expr = true;
4004
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004005 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004006 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004007 type = p->l.t.t;
4008
4009 // Because we parse the next part of the expression
4010 // right here, we need to increment this.
4011 *nexprs = *nexprs + 1;
4012
4013 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004014 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004015 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004016 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004017 case BC_LEX_KEY_IBASE:
4018 case BC_LEX_KEY_LAST:
4019 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004020 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004021 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004022 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004024 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004025 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004026 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004027 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004028 else
4029 bc_parse_push(p, BC_INST_SCALE);
4030 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004031 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004032 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004033 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004034 }
4035
4036 if (!s) bc_parse_push(p, inst);
4037 }
4038
Denys Vlasenko26819db2018-12-12 16:08:46 +01004039 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004040}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004041#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004042
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004043static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004044 bool rparen, size_t *nexprs)
4045{
4046 BcStatus s;
4047 BcLexType type;
4048 BcInst etype = *prev;
4049
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004050 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004051 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004052
4053 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4054 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4055 BC_LEX_OP_MINUS :
4056 BC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004057 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004058
4059 // We can just push onto the op stack because this is the largest
4060 // precedence operator that gets pushed. Inc/dec does not.
4061 if (type != BC_LEX_OP_MINUS)
4062 bc_vec_push(&p->ops, &type);
4063 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004064 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004065
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004066 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004067}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004068#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004069
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004070static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004071{
4072 BcStatus s;
4073 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004074
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004075 for (;;) {
4076 s = zbc_lex_next(&p->l);
4077 if (s) RETURN_STATUS(s);
4078 type = p->l.t.t;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004079 if (type == BC_LEX_STR) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004080 s = zbc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004081 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004082 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004083 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004084 if (s) RETURN_STATUS(s);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004085 bc_parse_push(p, BC_INST_PRINT_POP);
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004086 if (p->l.t.t != BC_LEX_COMMA)
4087 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004088 }
4089
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004090 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004091}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004092#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004093
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004094static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004095{
4096 BcStatus s;
4097 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004098
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004099 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004100 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004101 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004102
4103 t = p->l.t.t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004104 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4105 bc_parse_push(p, BC_INST_RET0);
4106 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004107 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004108 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004109 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004110 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004111 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004112 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004113 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004114
4115 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004116 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004117 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06004118 }
4119
4120 bc_parse_push(p, BC_INST_RET);
4121 }
4122
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004123 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004124 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004125}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004126#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004127
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004128static void rewrite_label_to_current(BcParse *p, size_t idx)
4129{
4130 size_t *label = bc_vec_item(&p->func->labels, idx);
4131 *label = p->func->code.len;
4132}
4133
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004134static BC_STATUS zbc_parse_if(BcParse *p)
4135{
4136 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004137 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004138
4139 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4140 s = zbc_lex_next(&p->l);
4141 if (s) RETURN_STATUS(s);
4142 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
4143
4144 s = zbc_lex_next(&p->l);
4145 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004146 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004147 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004148 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004149
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004150 // Encode "if zero, jump to ..."
4151 // Pushed value (destination of the jump) is uninitialized,
4152 // will be rewritten to be address of "end of if()" or of "else".
4153 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004154 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004155
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004156 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004157 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004158
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004159 dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4160 if (p->l.t.t == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004161 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004162
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004163 // Encode "after then_stmt, jump to end of if()"
4164 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4165 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004166 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004167
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004168 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 +01004169 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004170
Denys Vlasenko15850832018-12-16 21:40:54 +01004171 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004172
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004173 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004174 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004175 }
4176
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004177 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 +01004178 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004179
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004180 dbg_lex_done("%s:%d done", __func__, __LINE__);
4181 RETURN_STATUS(s);
4182}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004183#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004184
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004185static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004186{
4187 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004188 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004189 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004190
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004191 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004192 if (s) RETURN_STATUS(s);
4193 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004194 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004195 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004196
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004197 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004198 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004199 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004200
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004201 bc_vec_push(&p->exits, &ip_idx);
4202 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004203
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004204 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004205 if (s) RETURN_STATUS(s);
4206 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004207
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004208 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004209
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004210 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004211 if (s) RETURN_STATUS(s);
4212
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004213 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004214 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004215
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004216 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004217 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004218
4219 bc_vec_pop(&p->exits);
4220 bc_vec_pop(&p->conds);
4221
4222 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004223}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004224#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004225
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004226static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004227{
4228 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004229 size_t cond_idx, exit_idx, body_idx, update_idx;
4230
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004231 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004232 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004233 if (s) RETURN_STATUS(s);
4234 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004235 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004236 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004237
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004238 if (p->l.t.t != BC_LEX_SCOLON) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004239 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004240 bc_parse_push(p, BC_INST_POP);
4241 if (s) RETURN_STATUS(s);
4242 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004243 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004244 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4245 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004246
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004247 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004248 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004249 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004250
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004251 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004252 update_idx = cond_idx + 1;
4253 body_idx = update_idx + 1;
4254 exit_idx = body_idx + 1;
4255
Gavin Howard01055ba2018-11-03 11:00:21 -06004256 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004257 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004258 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004259 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004260 // This is safe to set because the current token is a semicolon,
4261 // which has no string requirement.
4262 bc_vec_string(&p->l.t.v, 1, "1");
4263 bc_parse_pushNUM(p);
Denys Vlasenko00646792018-12-05 18:12:27 +01004264 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004265 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004266 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004267
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004268 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004269
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004270 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004271 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004272
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004273 bc_parse_pushJUMP_ZERO(p, exit_idx);
4274 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004275
Gavin Howard01055ba2018-11-03 11:00:21 -06004276 bc_vec_push(&p->conds, &update_idx);
4277 bc_vec_push(&p->func->labels, &p->func->code.len);
4278
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004279 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004280 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004281 if (s) RETURN_STATUS(s);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004282 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
4283 bc_parse_push(p, BC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004284 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004285 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004286 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4287 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004288
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004289 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004290 bc_vec_push(&p->func->labels, &p->func->code.len);
4291
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004292 bc_vec_push(&p->exits, &exit_idx);
4293 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004294
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004295 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004296 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004297
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004298 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004299 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004300
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004301 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004302 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004303
4304 bc_vec_pop(&p->exits);
4305 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004306
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004307 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004308}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004309#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004310
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004311static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004312{
4313 BcStatus s;
4314 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004315
Gavin Howard01055ba2018-11-03 11:00:21 -06004316 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004317 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4318 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004319 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004320 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004321 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004322 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004323
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004324 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004325
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004326 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004327 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004328
4329 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004330 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004331
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004332 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004333}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004334#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004335
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004336static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004337{
4338 BcStatus s;
4339 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004340 char *name;
4341
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004342 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004343 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004344 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004345 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004346 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004347
4348 name = xstrdup(p->l.t.v.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004349 p->fidx = bc_program_addFunc(name);
4350 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004351
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004352 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004353 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004354 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004355 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004356 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004357 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004358
4359 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004360 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004361 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004362
4363 ++p->func->nparams;
4364
4365 name = xstrdup(p->l.t.v.v);
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 var = p->l.t.t != BC_LEX_LBRACKET;
4370
4371 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004372 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004373 if (s) goto err;
4374
4375 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004376 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004377 goto err;
4378 }
4379
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
4384 comma = p->l.t.t == BC_LEX_COMMA;
4385 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004386 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004387 if (s) goto err;
4388 }
4389
Denys Vlasenko29301232018-12-11 15:29:32 +01004390 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004391 if (s) goto err;
4392 }
4393
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004394 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004395
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004396 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004397 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004398
4399 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004400 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004401
Denys Vlasenko202dd192018-12-16 17:30:35 +01004402 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4403 s = zbc_lex_skip_if_at_NLINE(&p->l);
4404 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004405 //GNU bc requires a {} block even if function body has single stmt, enforce this?
4406 if (p->l.t.t != BC_LEX_LBRACE)
4407 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004408
4409 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004410 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004411 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004412 if (s) RETURN_STATUS(s);
4413
4414 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004415
4416 // Subsequent code generation is into main program
4417 p->fidx = BC_PROG_MAIN;
4418 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004419
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004420 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004421 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004422 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004423 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004424 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004425 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004426}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004427#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004428
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004429static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004430{
4431 BcStatus s;
4432 bool comma, var, one;
4433 char *name;
4434
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004435 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004436 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004437 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004438
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004439 comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004440 one = p->l.t.t == BC_LEX_NAME;
4441
4442 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004443 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004444 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004445 if (s) goto err;
4446
4447 var = p->l.t.t != BC_LEX_LBRACKET;
4448 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004449 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004450 if (s) goto err;
4451
4452 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004453 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004454 goto err;
4455 }
4456
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
4461 comma = p->l.t.t == BC_LEX_COMMA;
4462 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004463 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004464 if (s) goto err;
4465 }
4466
Denys Vlasenko29301232018-12-11 15:29:32 +01004467 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004468 if (s) goto err;
4469 }
4470
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004471 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4472 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004473
4474 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004475 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004476
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004477 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004478 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004479 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004480 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004481 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004482 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004483}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004484#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004485
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004486#undef zbc_parse_stmt_possibly_auto
4487static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004488{
4489 BcStatus s = BC_STATUS_SUCCESS;
4490
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004491 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004492
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004493 if (p->l.t.t == BC_LEX_NLINE) {
4494 dbg_lex_done("%s:%d done (seen BC_LEX_NLINE)", __func__, __LINE__);
4495 RETURN_STATUS(zbc_lex_next(&p->l));
4496 }
4497 if (p->l.t.t == BC_LEX_SCOLON) {
4498 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4499 RETURN_STATUS(zbc_lex_next(&p->l));
4500 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004501
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004502 if (p->l.t.t == BC_LEX_LBRACE) {
4503 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4504 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004505 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004506 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004507 } while (p->l.t.t == BC_LEX_NLINE);
4508 if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) {
4509 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4510 s = zbc_parse_auto(p);
4511 if (s) RETURN_STATUS(s);
4512 }
4513 while (p->l.t.t != BC_LEX_RBRACE) {
4514 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4515 s = zbc_parse_stmt(p);
4516 if (s) RETURN_STATUS(s);
4517 }
4518 s = zbc_lex_next(&p->l);
4519 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4520 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004521 }
4522
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004523 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004524 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004525 case BC_LEX_OP_INC:
4526 case BC_LEX_OP_DEC:
4527 case BC_LEX_OP_MINUS:
4528 case BC_LEX_OP_BOOL_NOT:
4529 case BC_LEX_LPAREN:
4530 case BC_LEX_NAME:
4531 case BC_LEX_NUMBER:
4532 case BC_LEX_KEY_IBASE:
4533 case BC_LEX_KEY_LAST:
4534 case BC_LEX_KEY_LENGTH:
4535 case BC_LEX_KEY_OBASE:
4536 case BC_LEX_KEY_READ:
4537 case BC_LEX_KEY_SCALE:
4538 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004539 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004540 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004541 case BC_LEX_STR:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004542 s = zbc_parse_pushSTR(p);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004543 bc_parse_push(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004544 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004545 case BC_LEX_KEY_BREAK:
4546 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004547 s = zbc_parse_break_or_continue(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004548 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004549 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004550 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004551 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004552 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004553 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004554 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004555 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004556 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004557 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004558 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004559 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004560 // "limits" is a compile-time command,
4561 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004562 printf(
4563 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4564 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4565 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4566 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4567 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4568 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4569 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4570 "Number of vars = "BC_MAX_VARS_STR "\n"
4571 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004572 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004573 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004574 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004575 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004577 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004578 // "quit" is a compile-time command. For example,
4579 // "if (0 == 1) quit" terminates when parsing the statement,
4580 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004581 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004582 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004583 if (!p->in_funcdef)
4584 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004585 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004586 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004587 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004588 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004589 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004590 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004591 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004592 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004593 }
4594
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004595 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004596 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004597}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004598#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004599
Denys Vlasenko99b37622018-12-15 20:06:59 +01004600static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004601{
4602 BcStatus s;
4603
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004604 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004605 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004606 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004607 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004608 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004609 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004610 } else {
4611 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 +01004612 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004613 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004614
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004615 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004616 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004617}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004618#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004619
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004620// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004621static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004622{
4623 BcStatus s = BC_STATUS_SUCCESS;
4624 BcInst prev = BC_INST_PRINT;
4625 BcLexType top, t = p->l.t.t;
4626 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004627 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004628 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4629
Denys Vlasenko17df8822018-12-14 23:00:24 +01004630 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004631 paren_first = p->l.t.t == BC_LEX_LPAREN;
4632 nparens = nrelops = 0;
4633 paren_expr = rprn = done = get_token = assign = false;
4634 bin_last = true;
4635
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004636 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004637 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004638 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004639 case BC_LEX_OP_INC:
4640 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004641 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004642 rprn = get_token = bin_last = false;
4643 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004644 case BC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004645 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004646 rprn = get_token = false;
4647 bin_last = prev == BC_INST_MINUS;
4648 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004649 case BC_LEX_OP_ASSIGN_POWER:
4650 case BC_LEX_OP_ASSIGN_MULTIPLY:
4651 case BC_LEX_OP_ASSIGN_DIVIDE:
4652 case BC_LEX_OP_ASSIGN_MODULUS:
4653 case BC_LEX_OP_ASSIGN_PLUS:
4654 case BC_LEX_OP_ASSIGN_MINUS:
4655 case BC_LEX_OP_ASSIGN:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004656 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM
4657 && prev != BC_INST_SCALE && prev != BC_INST_IBASE
4658 && prev != BC_INST_OBASE && prev != BC_INST_LAST
4659 ) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004660 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004661 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004662 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004663 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004664 break;
4665 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004666 // Fallthrough.
4667 case BC_LEX_OP_POWER:
4668 case BC_LEX_OP_MULTIPLY:
4669 case BC_LEX_OP_DIVIDE:
4670 case BC_LEX_OP_MODULUS:
4671 case BC_LEX_OP_PLUS:
4672 case BC_LEX_OP_REL_EQ:
4673 case BC_LEX_OP_REL_LE:
4674 case BC_LEX_OP_REL_GE:
4675 case BC_LEX_OP_REL_NE:
4676 case BC_LEX_OP_REL_LT:
4677 case BC_LEX_OP_REL_GT:
4678 case BC_LEX_OP_BOOL_NOT:
4679 case BC_LEX_OP_BOOL_OR:
4680 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004681 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4682 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4683 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004684 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004685 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004686 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004687 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004688 bc_parse_operator(p, t, ops_bgn, &nexprs);
4689 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004690 rprn = get_token = false;
4691 bin_last = t != BC_LEX_OP_BOOL_NOT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004692 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004693 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004694 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004695 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004696 ++nparens;
4697 paren_expr = rprn = bin_last = false;
4698 get_token = true;
4699 bc_vec_push(&p->ops, &t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004700 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004701 case BC_LEX_RPAREN:
Gavin Howard01055ba2018-11-03 11:00:21 -06004702 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004703 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004704 if (nparens == 0) {
4705 s = BC_STATUS_SUCCESS;
4706 done = true;
4707 get_token = false;
4708 break;
4709 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004710 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004711 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004712 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004713 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004714 --nparens;
4715 paren_expr = rprn = true;
4716 get_token = bin_last = false;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004717 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004718 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004719 case BC_LEX_NAME:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004720 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004721 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004722 paren_expr = true;
4723 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004724 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004725 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004726 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004727 case BC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004728 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004729 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004730 bc_parse_pushNUM(p);
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004731 nexprs++;
4732 prev = BC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004733 paren_expr = get_token = true;
4734 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004735 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004736 case BC_LEX_KEY_IBASE:
4737 case BC_LEX_KEY_LAST:
4738 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004739 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004740 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004741 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4742 bc_parse_push(p, (char) prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004743 paren_expr = get_token = true;
4744 rprn = bin_last = false;
4745 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004746 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004747 case BC_LEX_KEY_LENGTH:
4748 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004749 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004750 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004751 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004752 paren_expr = true;
4753 rprn = get_token = bin_last = false;
4754 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004755 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004756 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004757 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004758 return bc_error_bad_expression();
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 Vlasenko5daa1a02018-12-22 16:40:38 +01004837static void dc_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__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004854}
4855
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004856static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004857{
4858 BcStatus s;
4859
4860 bc_parse_push(p, inst);
4861 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004862 s = zdc_parse_register(p);
4863 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004864 }
4865
4866 if (store) {
4867 bc_parse_push(p, BC_INST_SWAP);
4868 bc_parse_push(p, BC_INST_ASSIGN);
4869 bc_parse_push(p, BC_INST_POP);
4870 }
4871
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004872 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004873}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004874#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004875
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004876static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004877{
4878 BcStatus s;
4879
4880 bc_parse_push(p, inst);
4881 bc_parse_push(p, BC_INST_EXEC_COND);
4882
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004883 s = zdc_parse_register(p);
4884 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004885
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004886 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004887 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004888
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004889 // Note that 'else' part can not be on the next line:
4890 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4891 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Gavin Howard01055ba2018-11-03 11:00:21 -06004892 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 Vlasenkobadf6832018-12-22 18:04:08 +01004896 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004897 bc_parse_push(p, BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004898 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004899
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004900 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004901}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004902#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004903
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004904static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004905{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004906 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004907 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004908 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004909
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004910 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004911 s = BC_STATUS_SUCCESS;
4912 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004913 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004914 case BC_LEX_OP_REL_EQ:
4915 case BC_LEX_OP_REL_LE:
4916 case BC_LEX_OP_REL_GE:
4917 case BC_LEX_OP_REL_NE:
4918 case BC_LEX_OP_REL_LT:
4919 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004920 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004921 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004922 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004923 case BC_LEX_SCOLON:
4924 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004925 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004926 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004927 case BC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004928 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004929 dc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004930 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004931 case BC_LEX_NEG:
4932 case BC_LEX_NUMBER:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004933 dbg_lex("%s:%d LEX_NEG/NUMBER", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004934 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004935 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004936 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004937 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004938 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004939 }
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004940 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004941 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004942 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004943 case BC_LEX_KEY_READ:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004944 bc_parse_push(p, BC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004945 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004946 case BC_LEX_OP_ASSIGN:
4947 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06004948 assign = t == BC_LEX_OP_ASSIGN;
4949 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004950 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004951 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004952 case BC_LEX_LOAD:
4953 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06004954 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004955 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004956 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004957 case BC_LEX_STORE_IBASE:
4958 case BC_LEX_STORE_SCALE:
4959 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004960 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004961 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004962 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004963 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004964 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4965 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004966 }
4967
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004968 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004969
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004970 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004971 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004972}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004973#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004974
Denys Vlasenko39287e02018-12-22 02:23:08 +01004975static BC_STATUS zdc_parse_expr(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004976{
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004977 BcInst inst;
4978 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004979
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004980 inst = dc_parse_insts[p->l.t.t];
4981 if (inst != BC_INST_INVALID) {
4982 bc_parse_push(p, inst);
4983 s = zbc_lex_next(&p->l);
4984 } else {
4985 s = zdc_parse_token(p, p->l.t.t);
4986 }
4987 RETURN_STATUS(s);
4988}
4989#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
4990
4991static BC_STATUS zdc_parse_exprs_until_eof(BcParse *p)
4992{
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01004993 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004994 while (p->l.t.t != BC_LEX_EOF) {
4995 BcStatus s = zdc_parse_expr(p);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004996 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004997 }
4998
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004999 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005000 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005001}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005002#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005003
Gavin Howard01055ba2018-11-03 11:00:21 -06005004#endif // ENABLE_DC
5005
Denys Vlasenkodf515392018-12-02 19:27:48 +01005006static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005007{
Gavin Howard01055ba2018-11-03 11:00:21 -06005008 BcId e, *ptr;
5009 BcVec *v, *map;
5010 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005011 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005012
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005013 v = var ? &G.prog.vars : &G.prog.arrs;
5014 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005015
5016 e.name = id;
5017 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005018 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005019
5020 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005021 BcVec v2;
5022 bc_array_init(&v2, var);
5023 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005024 }
5025
5026 ptr = bc_vec_item(map, i);
5027 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005028 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005029}
5030
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005031// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005032static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005033{
Gavin Howard01055ba2018-11-03 11:00:21 -06005034 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005035 case BC_RESULT_STR:
5036 case BC_RESULT_TEMP:
5037 case BC_RESULT_IBASE:
5038 case BC_RESULT_SCALE:
5039 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005040 *num = &r->d.n;
5041 break;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005042 case BC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005043 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005044 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005045 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005046 size_t len;
5047
5048 str = *bc_program_const(r->d.id.idx);
5049 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005050
5051 bc_num_init(&r->d.n, len);
5052
5053 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005054 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005055 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005056 if (s) {
5057 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005058 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005059 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005060 *num = &r->d.n;
5061 r->t = BC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005062 break;
5063 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005064 case BC_RESULT_VAR:
5065 case BC_RESULT_ARRAY:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005066 case BC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005067 BcVec *v;
5068
Denys Vlasenkodf515392018-12-02 19:27:48 +01005069 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005070
5071 if (r->t == BC_RESULT_ARRAY_ELEM) {
5072 v = bc_vec_top(v);
5073 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5074 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005075 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005076 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005077 break;
5078 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005079#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005080 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005081 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005082 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005083 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005084 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005085 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005086#endif
5087 default:
5088 // Testing the theory that dc does not reach LAST/ONE
5089 bb_error_msg_and_die("BUG:%d", r->t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005090 }
5091
Denys Vlasenko29301232018-12-11 15:29:32 +01005092 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005093}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005094#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005095
Denys Vlasenko29301232018-12-11 15:29:32 +01005096static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005097 BcResult **r, BcNum **rn, bool assign)
5098{
5099 BcStatus s;
5100 bool hex;
5101 BcResultType lt, rt;
5102
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005103 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005104 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005105
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005106 *r = bc_vec_item_rev(&G.prog.results, 0);
5107 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005108
5109 lt = (*l)->t;
5110 rt = (*r)->t;
5111 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5112
Denys Vlasenko29301232018-12-11 15:29:32 +01005113 s = zbc_program_num(*l, ln, false);
5114 if (s) RETURN_STATUS(s);
5115 s = zbc_program_num(*r, rn, hex);
5116 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005117
5118 // We run this again under these conditions in case any vector has been
5119 // reallocated out from under the BcNums or arrays we had.
5120 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005121 s = zbc_program_num(*l, ln, false);
5122 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005123 }
5124
5125 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005126 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005127 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005128 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005129
Denys Vlasenko29301232018-12-11 15:29:32 +01005130 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005131}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005132#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005133
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005134static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005135{
5136 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005137 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005138 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005139}
5140
Denys Vlasenko29301232018-12-11 15:29:32 +01005141static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005142{
5143 BcStatus s;
5144
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005145 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005146 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005147 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005148
Denys Vlasenko29301232018-12-11 15:29:32 +01005149 s = zbc_program_num(*r, n, false);
5150 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005151
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005152 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005153 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005154
Denys Vlasenko29301232018-12-11 15:29:32 +01005155 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005156}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005157#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005158
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005159static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005160{
5161 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005162 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005163}
5164
Denys Vlasenko259137d2018-12-11 19:42:05 +01005165static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005166{
5167 BcStatus s;
5168 BcResult *opd1, *opd2, res;
5169 BcNum *n1, *n2 = NULL;
5170
Denys Vlasenko29301232018-12-11 15:29:32 +01005171 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005172 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005173 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005174
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005175 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005176 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 -06005177 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005178 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005179
Denys Vlasenko259137d2018-12-11 19:42:05 +01005180 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005181 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005182 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005183 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005184}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005185#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005186
Denys Vlasenko26819db2018-12-12 16:08:46 +01005187static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005188{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005189 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005190 BcStatus s;
5191 BcParse parse;
5192 BcVec buf;
5193 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005194 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005195
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005196 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005197 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005198
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005199 sv_file = G.prog.file;
5200 G.prog.file = NULL;
5201
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005202 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005203 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005204
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005205 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005206 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005207
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005208 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005209 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005210 if (IS_BC) {
5211 IF_BC(s = zbc_parse_expr(&parse, 0));
5212 } else {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005213 IF_DC(s = zdc_parse_exprs_until_eof(&parse));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005214 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005215 if (s) goto exec_err;
5216
5217 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005218 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005219 goto exec_err;
5220 }
5221
5222 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005223 ip.inst_idx = 0;
5224 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005225
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01005226 bc_parse_push(&parse, BC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005227 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005228 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005229 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005230 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005231 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005232 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005233}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005234#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005235
5236static size_t bc_program_index(char *code, size_t *bgn)
5237{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005238 unsigned char *bytes = (void*)(code + *bgn);
5239 unsigned amt;
5240 unsigned i;
5241 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005242
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005243 amt = *bytes++;
5244 *bgn += amt + 1;
5245
5246 amt *= 8;
5247 res = 0;
5248 for (i = 0; i < amt; i += 8)
5249 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005250
5251 return res;
5252}
5253
5254static char *bc_program_name(char *code, size_t *bgn)
5255{
5256 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005257 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005258
Denys Vlasenko694d2982018-12-18 19:17:11 +01005259 code += *bgn;
5260 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005261 i = 0;
5262 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005263 char c = *code++;
5264 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005265 break;
5266 s[i++] = c;
5267 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005268 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005269 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005270
5271 return s;
5272}
5273
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005274static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005275{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005276#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005277 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005278 // Example: echo '[]ap' | dc
5279 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005280 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005281 return;
5282 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005283#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005284 while (*str) {
5285 int c = *str++;
5286 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005287 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005288 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005289 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005290 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005291 case 'a':
5292 bb_putchar('\a');
5293 break;
5294 case 'b':
5295 bb_putchar('\b');
5296 break;
5297 case '\\':
5298 case 'e':
5299 bb_putchar('\\');
5300 break;
5301 case 'f':
5302 bb_putchar('\f');
5303 break;
5304 case 'n':
5305 bb_putchar('\n');
5306 G.prog.nchars = SIZE_MAX;
5307 break;
5308 case 'r':
5309 bb_putchar('\r');
5310 break;
5311 case 'q':
5312 bb_putchar('"');
5313 break;
5314 case 't':
5315 bb_putchar('\t');
5316 break;
5317 default:
5318 // Just print the backslash and following character.
5319 bb_putchar('\\');
5320 ++G.prog.nchars;
5321 bb_putchar(c);
5322 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005323 }
5324 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005325 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005326 }
5327}
5328
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005329static void bc_num_printNewline(void)
5330{
5331 if (G.prog.nchars == G.prog.len - 1) {
5332 bb_putchar('\\');
5333 bb_putchar('\n');
5334 G.prog.nchars = 0;
5335 }
5336}
5337
5338#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005339static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005340{
5341 (void) radix;
5342 bb_putchar((char) num);
5343 G.prog.nchars += width;
5344}
5345#endif
5346
5347static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5348{
5349 size_t exp, pow;
5350
5351 bc_num_printNewline();
5352 bb_putchar(radix ? '.' : ' ');
5353 ++G.prog.nchars;
5354
5355 bc_num_printNewline();
5356 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5357 continue;
5358
5359 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5360 size_t dig;
5361 bc_num_printNewline();
5362 dig = num / pow;
5363 num -= dig * pow;
5364 bb_putchar(((char) dig) + '0');
5365 }
5366}
5367
5368static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5369{
5370 if (radix) {
5371 bc_num_printNewline();
5372 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005373 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005374 }
5375
5376 bc_num_printNewline();
5377 bb_putchar(bb_hexdigits_upcase[num]);
5378 G.prog.nchars += width;
5379}
5380
5381static void bc_num_printDecimal(BcNum *n)
5382{
5383 size_t i, rdx = n->rdx - 1;
5384
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005385 if (n->neg) {
5386 bb_putchar('-');
5387 G.prog.nchars++;
5388 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005389
5390 for (i = n->len - 1; i < n->len; --i)
5391 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5392}
5393
Denys Vlasenkod3401432018-12-18 17:00:35 +01005394static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005395{
5396 BcStatus s;
5397 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005398 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005399 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005400 BcNum intp, fracp, digit, frac_len;
5401 unsigned long dig, *ptr;
5402 size_t i;
5403 bool radix;
5404
5405 if (n->len == 0) {
5406 print(0, width, false);
5407 RETURN_STATUS(BC_STATUS_SUCCESS);
5408 }
5409
5410 bc_vec_init(&stack, sizeof(long), NULL);
5411 bc_num_init(&intp, n->len);
5412 bc_num_init(&fracp, n->rdx);
5413 bc_num_init(&digit, width);
5414 bc_num_init(&frac_len, BC_NUM_INT(n));
5415 bc_num_copy(&intp, n);
5416 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005417 base.cap = ARRAY_SIZE(base_digs);
5418 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005419 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005420
5421 bc_num_truncate(&intp, intp.rdx);
5422 s = zbc_num_sub(n, &intp, &fracp, 0);
5423 if (s) goto err;
5424
5425 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005426 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005427 if (s) goto err;
5428 s = zbc_num_ulong(&digit, &dig);
5429 if (s) goto err;
5430 bc_vec_push(&stack, &dig);
5431 }
5432
5433 for (i = 0; i < stack.len; ++i) {
5434 ptr = bc_vec_item_rev(&stack, i);
5435 print(*ptr, width, false);
5436 }
5437
5438 if (!n->rdx) goto err;
5439
5440 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005441 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005442 if (s) goto err;
5443 s = zbc_num_ulong(&fracp, &dig);
5444 if (s) goto err;
5445 bc_num_ulong2num(&intp, dig);
5446 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5447 if (s) goto err;
5448 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005449 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005450 if (s) goto err;
5451 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005452 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005453 bc_num_free(&frac_len);
5454 bc_num_free(&digit);
5455 bc_num_free(&fracp);
5456 bc_num_free(&intp);
5457 bc_vec_free(&stack);
5458 RETURN_STATUS(s);
5459}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005460#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005461
5462static BC_STATUS zbc_num_printBase(BcNum *n)
5463{
5464 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005465 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005466 BcNumDigitOp print;
5467 bool neg = n->neg;
5468
5469 if (neg) {
5470 bb_putchar('-');
5471 G.prog.nchars++;
5472 }
5473
5474 n->neg = false;
5475
5476 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5477 width = 1;
5478 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005479 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005480 unsigned i = G.prog.ob_t - 1;
5481 width = 0;
5482 for (;;) {
5483 width++;
5484 i /= 10;
5485 if (i == 0)
5486 break;
5487 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005488 print = bc_num_printDigits;
5489 }
5490
Denys Vlasenkod3401432018-12-18 17:00:35 +01005491 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005492 n->neg = neg;
5493
5494 RETURN_STATUS(s);
5495}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005496#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005497
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005498static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5499{
5500 BcStatus s = BC_STATUS_SUCCESS;
5501
5502 bc_num_printNewline();
5503
5504 if (n->len == 0) {
5505 bb_putchar('0');
5506 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005507 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005508 bc_num_printDecimal(n);
5509 else
5510 s = zbc_num_printBase(n);
5511
5512 if (newline) {
5513 bb_putchar('\n');
5514 G.prog.nchars = 0;
5515 }
5516
5517 RETURN_STATUS(s);
5518}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005519#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005520
Denys Vlasenko29301232018-12-11 15:29:32 +01005521static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005522{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005523 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005524 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005525 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005526 bool pop = inst != BC_INST_PRINT;
5527
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005528 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005529 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005530
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005531 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005532 s = zbc_program_num(r, &num, false);
5533 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005534
5535 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005536 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005537#if ENABLE_BC
5538 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5539#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005540 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005541 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005542
5543 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005544 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005545
5546 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005547 for (;;) {
5548 char c = *str++;
5549 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005550 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005551 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005552 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005553 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005554 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005555 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005556 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005557 }
5558 }
5559
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005560 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005561
Denys Vlasenko29301232018-12-11 15:29:32 +01005562 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005563}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005564#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005565
Denys Vlasenko29301232018-12-11 15:29:32 +01005566static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005567{
5568 BcStatus s;
5569 BcResult res, *ptr;
5570 BcNum *num = NULL;
5571
Denys Vlasenko29301232018-12-11 15:29:32 +01005572 s = zbc_program_prep(&ptr, &num);
5573 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005574
5575 bc_num_init(&res.d.n, num->len);
5576 bc_num_copy(&res.d.n, num);
5577 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5578
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005579 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005580
Denys Vlasenko29301232018-12-11 15:29:32 +01005581 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005582}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005583#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005584
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005585static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005586{
5587 BcStatus s;
5588 BcResult *opd1, *opd2, res;
5589 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005590 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005591
Denys Vlasenko29301232018-12-11 15:29:32 +01005592 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005593 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005594
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005595 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005596
5597 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005598 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005599 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005600 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005601 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005602 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005603 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005604 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005605 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005606 break;
5607 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005608 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005609 break;
5610 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005611 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005612 break;
5613 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005614 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005615 break;
5616 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005617 cond = (cond > 0);
5618 break;
5619 default: // = case BC_INST_REL_NE:
5620 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005621 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005622 }
5623 }
5624
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005625 if (cond) bc_num_one(&res.d.n);
5626 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005627
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005628 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005629
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005630 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005631}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005632#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005633
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005634#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005635static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005636{
5637 BcNum n2;
5638 BcResult res;
5639
5640 memset(&n2, 0, sizeof(BcNum));
5641 n2.rdx = res.d.id.idx = r->d.id.idx;
5642 res.t = BC_RESULT_STR;
5643
5644 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005645 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005646 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005647 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005648 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005649 }
5650
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005651 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005652 bc_vec_push(v, &n2);
5653
Denys Vlasenko29301232018-12-11 15:29:32 +01005654 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005655}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005656#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005657#endif // ENABLE_DC
5658
Denys Vlasenko29301232018-12-11 15:29:32 +01005659static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005660{
5661 BcStatus s;
5662 BcResult *ptr, r;
5663 BcVec *v;
5664 BcNum *n;
5665
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005666 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005667 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005668
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005669 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005670 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005671 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005672 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005673
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005674#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005675 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005676 RETURN_STATUS(bc_error_variable_is_wrong_type());
5677 if (ptr->t == BC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005678 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005679#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005680
Denys Vlasenko29301232018-12-11 15:29:32 +01005681 s = zbc_program_num(ptr, &n, false);
5682 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005683
5684 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005685 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005686
5687 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005688 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005689 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005690 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005691 bc_array_init(&r.d.v, true);
5692 bc_array_copy(&r.d.v, (BcVec *) n);
5693 }
5694
5695 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005696 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005697
Denys Vlasenko29301232018-12-11 15:29:32 +01005698 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005699}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005700#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005701
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005702static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005703{
5704 BcStatus s;
5705 BcResult *left, *right, res;
5706 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005707 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5708
Denys Vlasenko29301232018-12-11 15:29:32 +01005709 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005710 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005711
5712 ib = left->t == BC_RESULT_IBASE;
5713 sc = left->t == BC_RESULT_SCALE;
5714
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005715#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005716 if (right->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005717 BcVec *v;
5718
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005719 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005720 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005721 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005722
Denys Vlasenkofa210792018-12-19 19:35:40 +01005723 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005724 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005725#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005726
5727 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005728 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005729 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005730 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005731 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005732
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005733#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005734 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005735 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005736
5737 if (assign)
5738 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005739 else {
5740 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005741 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 +01005742 }
5743 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005744#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005745 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005746#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005747
5748 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005749 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005750 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5751 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5752 NULL, //can't happen //BC_RESULT_LAST
5753 NULL, //can't happen //BC_RESULT_CONSTANT
5754 NULL, //can't happen //BC_RESULT_ONE
5755 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005756 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005757 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005758 size_t max;
5759 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005760
Denys Vlasenko29301232018-12-11 15:29:32 +01005761 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005762 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005763 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005764 if (sc) {
5765 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005766 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005767 } else {
5768 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005769 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005770 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005771 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005772 }
5773
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005774 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005775 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005776
5777 *ptr = (size_t) val;
5778 s = BC_STATUS_SUCCESS;
5779 }
5780
5781 bc_num_init(&res.d.n, l->len);
5782 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005783 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005784
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005785 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005786}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005787#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005788
Denys Vlasenko416ce762018-12-02 20:57:17 +01005789#if !ENABLE_DC
5790#define bc_program_pushVar(code, bgn, pop, copy) \
5791 bc_program_pushVar(code, bgn)
5792// for bc, 'pop' and 'copy' are always false
5793#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005794static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005795 bool pop, bool copy)
5796{
Gavin Howard01055ba2018-11-03 11:00:21 -06005797 BcResult r;
5798 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005799
5800 r.t = BC_RESULT_VAR;
5801 r.d.id.name = name;
5802
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005803#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005804 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005805 BcVec *v = bc_program_search(name, true);
5806 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005807
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005808 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005809 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005810 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005811 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005812
5813 if (!BC_PROG_STR(num)) {
5814 r.t = BC_RESULT_TEMP;
5815 bc_num_init_DEF_SIZE(&r.d.n);
5816 bc_num_copy(&r.d.n, num);
5817 } else {
5818 r.t = BC_RESULT_STR;
5819 r.d.id.idx = num->rdx;
5820 }
5821
5822 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005823 }
5824#endif // ENABLE_DC
5825
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005826 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005827
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005828 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005829}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005830#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005831
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005832static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005833{
5834 BcStatus s = BC_STATUS_SUCCESS;
5835 BcResult r;
5836 BcNum *num;
5837
5838 r.d.id.name = bc_program_name(code, bgn);
5839
5840 if (inst == BC_INST_ARRAY) {
5841 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005842 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005843 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005844 BcResult *operand;
5845 unsigned long temp;
5846
Denys Vlasenko29301232018-12-11 15:29:32 +01005847 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005848 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005849 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005850 if (s) goto err;
5851
5852 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005853 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005854 goto err;
5855 }
5856
5857 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005858 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005859 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005860 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005861 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005862 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005863}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005864#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005865
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005866#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005867static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005868{
5869 BcStatus s;
5870 BcResult *ptr, res, copy;
5871 BcNum *num = NULL;
5872 char inst2 = inst;
5873
Denys Vlasenko29301232018-12-11 15:29:32 +01005874 s = zbc_program_prep(&ptr, &num);
5875 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005876
5877 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5878 copy.t = BC_RESULT_TEMP;
5879 bc_num_init(&copy.d.n, num->len);
5880 bc_num_copy(&copy.d.n, num);
5881 }
5882
5883 res.t = BC_RESULT_ONE;
5884 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
5885 BC_INST_ASSIGN_PLUS :
5886 BC_INST_ASSIGN_MINUS;
5887
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005888 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005889 s = zbc_program_assign(inst);
5890 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005891
5892 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005893 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005894 }
5895
Denys Vlasenko29301232018-12-11 15:29:32 +01005896 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005897}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005898#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005899
Denys Vlasenko29301232018-12-11 15:29:32 +01005900static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005901{
Gavin Howard01055ba2018-11-03 11:00:21 -06005902 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005903 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005904 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005905 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005906 BcResult *arg;
5907
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005908 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005909 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005910 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005911 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005912
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005913 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005914 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005915 }
5916 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005917 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005918 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005919 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005920
5921 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005922 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005923
5924 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005925 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005926
5927 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005928 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005929
Denys Vlasenko29301232018-12-11 15:29:32 +01005930 s = zbc_program_copyToVar(a->name, a->idx);
5931 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005932 }
5933
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005934 a = bc_vec_item(&func->autos, i);
5935 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005936 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005937
Denys Vlasenkodf515392018-12-02 19:27:48 +01005938 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005939 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005940 BcNum n2;
5941 bc_num_init_DEF_SIZE(&n2);
5942 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005943 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005944 BcVec v2;
5945 bc_array_init(&v2, true);
5946 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005947 }
5948 }
5949
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005950 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005951
Denys Vlasenko29301232018-12-11 15:29:32 +01005952 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005953}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005954#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005955
Denys Vlasenko29301232018-12-11 15:29:32 +01005956static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005957{
Gavin Howard01055ba2018-11-03 11:00:21 -06005958 BcResult res;
5959 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005960 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005961 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005962 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06005963
Denys Vlasenko24e41942018-12-21 23:01:26 +01005964 if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->results_len_before_call + (inst == BC_INST_RET)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005965 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005966
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005967 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005968 res.t = BC_RESULT_TEMP;
5969
5970 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005971 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005972 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005973 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005974
Denys Vlasenko29301232018-12-11 15:29:32 +01005975 s = zbc_program_num(operand, &num, false);
5976 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005977 bc_num_init(&res.d.n, num->len);
5978 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005979 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005980 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01005981 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005982 }
5983
5984 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005985 a = (void*)f->autos.v;
5986 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005987 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01005988 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005989 bc_vec_pop(v);
5990 }
5991
Denys Vlasenko24e41942018-12-21 23:01:26 +01005992 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005993 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005994 bc_vec_pop(&G.prog.exestack);
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_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005999#endif // ENABLE_BC
6000
6001static unsigned long bc_program_scale(BcNum *n)
6002{
6003 return (unsigned long) n->rdx;
6004}
6005
6006static unsigned long bc_program_len(BcNum *n)
6007{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006008 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006009
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006010 if (n->rdx != len) return len;
6011 for (;;) {
6012 if (len == 0) break;
6013 len--;
6014 if (n->num[len] != 0) break;
6015 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006016 return len;
6017}
6018
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006019static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006020{
6021 BcStatus s;
6022 BcResult *opnd;
6023 BcNum *num = NULL;
6024 BcResult res;
6025 bool len = inst == BC_INST_LENGTH;
6026
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006027 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006028 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006029 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006030
Denys Vlasenko29301232018-12-11 15:29:32 +01006031 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006032 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006033
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006034#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006035 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006036 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006037#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006038
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006039 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006040
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006041 if (inst == BC_INST_SQRT)
6042 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006043#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006044 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006045 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006046 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006047#endif
6048#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006049 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006050 char **str;
6051 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6052
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006053 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006054 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006055 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006056#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006057 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006058 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006059 }
6060
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006061 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006062
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006063 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006064}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006065#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006066
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006067#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006068static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006069{
6070 BcStatus s;
6071 BcResult *opd1, *opd2, res, res2;
6072 BcNum *n1, *n2 = NULL;
6073
Denys Vlasenko29301232018-12-11 15:29:32 +01006074 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006075 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006076
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006077 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006078 bc_num_init(&res2.d.n, n2->len);
6079
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006080 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006081 if (s) goto err;
6082
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006083 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006084 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006085 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006086
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006087 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006088 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006089 bc_num_free(&res2.d.n);
6090 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006091 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006092}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006093#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006094
Denys Vlasenkofa210792018-12-19 19:35:40 +01006095static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006096{
6097 BcStatus s;
6098 BcResult *r1, *r2, *r3, res;
6099 BcNum *n1, *n2, *n3;
6100
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006101 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006102 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006103 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006104 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006105
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006106 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006107 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006108 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006109 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006110 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006111
6112 // Make sure that the values have their pointers updated, if necessary.
6113 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006114 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006115 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006116 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006117 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006118 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006119 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006120 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006121 }
6122 }
6123
6124 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006125 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006126 if (s) goto err;
6127
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006128 bc_vec_pop(&G.prog.results);
6129 bc_program_binOpRetire(&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(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006134 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006135}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006136#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006137
Denys Vlasenkofa210792018-12-19 19:35:40 +01006138static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006139{
Gavin Howard01055ba2018-11-03 11:00:21 -06006140 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006141 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006142
6143 res.t = BC_RESULT_TEMP;
6144
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006145 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006146 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006147 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006148}
6149
Denys Vlasenkofa210792018-12-19 19:35:40 +01006150static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006151{
6152 BcStatus s;
6153 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006154 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006155 char **strs;
6156 char *str;
6157 char c;
6158 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006159
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006160 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006161 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006162 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006163
Denys Vlasenko29301232018-12-11 15:29:32 +01006164 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006165 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006166
6167 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006168 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006169 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006170 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006171
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006172 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006173 bc_num_copy(&n, num);
6174 bc_num_truncate(&n, n.rdx);
6175
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006176 strmb.cap = ARRAY_SIZE(strmb_digs);
6177 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006178 bc_num_ulong2num(&strmb, 0x100);
6179 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006180
Gavin Howard01055ba2018-11-03 11:00:21 -06006181 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006182 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006183 if (s) goto num_err;
6184
6185 c = (char) val;
6186
6187 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006188 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006189 char *sp;
Gavin Howard01055ba2018-11-03 11:00:21 -06006190 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006191 sp = *bc_program_str(idx);
6192 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006193 }
6194
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006195 strs = (void*)G.prog.strs.v;
6196 for (idx = 0; idx < G.prog.strs.len; idx++) {
6197 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6198 goto dup;
6199 }
6200 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006201 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006202 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006203 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006204 bc_vec_push(&G.prog.strs, &str);
6205 dup:
Gavin Howard01055ba2018-11-03 11:00:21 -06006206 res.t = BC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006207 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006208 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006209
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006210 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006211 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006212 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006213 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006214}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006215#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006216
Denys Vlasenkofa210792018-12-19 19:35:40 +01006217static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006218{
6219 BcStatus s;
6220 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006221 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006222 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006223
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006224 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006225 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006226 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006227
Denys Vlasenko29301232018-12-11 15:29:32 +01006228 s = zbc_program_num(r, &n, false);
6229 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006230
Denys Vlasenko57734c92018-12-17 21:14:05 +01006231 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006232 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006233 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006234 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06006235 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006236 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006237 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006238 }
6239
Denys Vlasenko29301232018-12-11 15:29:32 +01006240 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006241}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006242#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006243
Denys Vlasenkofa210792018-12-19 19:35:40 +01006244static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006245{
6246 BcStatus s;
6247 BcResult *opnd;
6248 BcNum *num = NULL;
6249 unsigned long val;
6250
Denys Vlasenko29301232018-12-11 15:29:32 +01006251 s = zbc_program_prep(&opnd, &num);
6252 if (s) RETURN_STATUS(s);
6253 s = zbc_num_ulong(num, &val);
6254 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006255
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006256 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006257
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006258 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006259 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006260 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006261 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006262 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006263
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006264 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006265
Denys Vlasenko29301232018-12-11 15:29:32 +01006266 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006267}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006268#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006269
Denys Vlasenkofa210792018-12-19 19:35:40 +01006270static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006271{
6272 BcStatus s = BC_STATUS_SUCCESS;
6273 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006274 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006275 BcInstPtr ip;
6276 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006277
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006278 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006279 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006280
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006281 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006282
6283 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006284 BcNum *n = n; // for compiler
6285 bool exec;
6286 char *name;
6287 char *then_name = bc_program_name(code, bgn);
6288 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006289
6290 if (code[*bgn] == BC_PARSE_STREND)
6291 (*bgn) += 1;
6292 else
6293 else_name = bc_program_name(code, bgn);
6294
6295 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006296 name = then_name;
6297 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006298 exec = true;
6299 name = else_name;
6300 }
6301
6302 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006303 BcVec *v;
6304 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006305 n = bc_vec_top(v);
6306 }
6307
6308 free(then_name);
6309 free(else_name);
6310
6311 if (!exec) goto exit;
6312 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006313 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006314 goto exit;
6315 }
6316
6317 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006318 } else {
6319 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006320 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006321 } else if (r->t == BC_RESULT_VAR) {
6322 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006323 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006324 if (s || !BC_PROG_STR(n)) goto exit;
6325 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006326 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006327 goto exit;
6328 }
6329
6330 fidx = sidx + BC_PROG_REQ_FUNCS;
6331
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006332 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006333
6334 if (f->code.len == 0) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006335 FILE *sv_input_fp;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006336 BcParse prs;
6337 char *str;
6338
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006339 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006340 str = *bc_program_str(sidx);
6341 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006342 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006343
6344 sv_input_fp = G.input_fp;
6345 G.input_fp = NULL; // "do not read from input file when <EOL> reached"
6346 s = zdc_parse_exprs_until_eof(&prs);
6347 G.input_fp = sv_input_fp;
6348
Gavin Howard01055ba2018-11-03 11:00:21 -06006349 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06006350 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006351 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006352 err:
6353 bc_parse_free(&prs);
6354 bc_vec_pop_all(&f->code);
6355 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006356 }
Denys Vlasenko39287e02018-12-22 02:23:08 +01006357 bc_parse_push(&prs, BC_INST_POP_EXEC);
Gavin Howard01055ba2018-11-03 11:00:21 -06006358 bc_parse_free(&prs);
6359 }
6360
Denys Vlasenko24e41942018-12-21 23:01:26 +01006361 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006362 ip.func = fidx;
6363
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006364 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006365 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006366
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006367 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006368 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006369 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006370 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006371}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006372#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006373#endif // ENABLE_DC
6374
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006375static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006376{
Gavin Howard01055ba2018-11-03 11:00:21 -06006377 BcResult res;
6378 unsigned long val;
6379
6380 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6381 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006382 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006383 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006384 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006385 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006386 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006387
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006388 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006389 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006390 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006391}
6392
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006393static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006394{
Gavin Howard01055ba2018-11-03 11:00:21 -06006395 BcResult r, *ptr;
6396 BcNum *num;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006397 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006398 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006399 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006400
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006401 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006402 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6403 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006404 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006405 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006406
Denys Vlasenko24e41942018-12-21 23:01:26 +01006407 dbg_exec("inst at %zd:%d results.len:%d", ip->inst_idx - 1, inst, G.prog.results.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006408 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006409#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006410 case BC_INST_JUMP_ZERO: {
6411 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006412 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006413 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006414 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006415 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006416 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006417 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006418 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006419 break;
6420 }
6421 // else: fall through
6422 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006423 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006424 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006425 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006426 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006427 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006428 break;
6429 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006430 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006431 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006432 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006433 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006434 case BC_INST_INC_PRE:
6435 case BC_INST_DEC_PRE:
6436 case BC_INST_INC_POST:
6437 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006438 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006439 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006440 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006441 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006442 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006443 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006444 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006445 case BC_INST_RET:
6446 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006447 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006448 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006449 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006450 case BC_INST_BOOL_OR:
6451 case BC_INST_BOOL_AND:
6452#endif // ENABLE_BC
6453 case BC_INST_REL_EQ:
6454 case BC_INST_REL_LE:
6455 case BC_INST_REL_GE:
6456 case BC_INST_REL_NE:
6457 case BC_INST_REL_LT:
6458 case BC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006459 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006460 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006461 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006462 case BC_INST_READ:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006463 dbg_exec("BC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006464 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006465 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006466 case BC_INST_VAR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006467 dbg_exec("BC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006468 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006469 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006470 case BC_INST_ARRAY_ELEM:
6471 case BC_INST_ARRAY:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006472 dbg_exec("BC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006473 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006474 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006475 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006476//TODO: this can't happen on dc, right?
6477 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006478 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006479 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006480 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006481 case BC_INST_IBASE:
6482 case BC_INST_SCALE:
6483 case BC_INST_OBASE:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006484 dbg_exec("BC_INST_internalvar:");
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006485 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006486 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006487 case BC_INST_SCALE_FUNC:
6488 case BC_INST_LENGTH:
6489 case BC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006490 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006491 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006492 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006493 case BC_INST_NUM:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006494 dbg_exec("BC_INST_NUM:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006495 r.t = BC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006496 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006497 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006498 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006499 case BC_INST_POP:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006500 dbg_exec("BC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006501 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006502 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006503 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006504 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006505 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006506 case BC_INST_PRINT:
6507 case BC_INST_PRINT_POP:
6508 case BC_INST_PRINT_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006509 dbg_exec("BC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006510 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006511 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006512 case BC_INST_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006513 dbg_exec("BC_INST_STR:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006514 r.t = BC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006515 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006516 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006517 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006518 case BC_INST_POWER:
6519 case BC_INST_MULTIPLY:
6520 case BC_INST_DIVIDE:
6521 case BC_INST_MODULUS:
6522 case BC_INST_PLUS:
6523 case BC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006524 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006525 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006526 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006527 case BC_INST_BOOL_NOT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006528 dbg_exec("BC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006529 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006530 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006531 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006532 if (!bc_num_cmp(num, &G.prog.zero))
6533 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006534 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006535 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006536 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006537 case BC_INST_NEG:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006538 dbg_exec("BC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006539 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006540 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006541#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006542 case BC_INST_ASSIGN_POWER:
6543 case BC_INST_ASSIGN_MULTIPLY:
6544 case BC_INST_ASSIGN_DIVIDE:
6545 case BC_INST_ASSIGN_MODULUS:
6546 case BC_INST_ASSIGN_PLUS:
6547 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006548#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006549 case BC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006550 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006551 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006552 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006553#if ENABLE_DC
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006554 case BC_INST_POP_EXEC:
6555 dbg_exec("BC_INST_POP_EXEC:");
6556 bc_vec_pop(&G.prog.exestack);
6557 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006558 case BC_INST_MODEXP:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006559 dbg_exec("BC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006560 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006561 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006562 case BC_INST_DIVMOD:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006563 dbg_exec("BC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006564 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006565 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006566 case BC_INST_EXECUTE:
6567 case BC_INST_EXEC_COND:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006568 dbg_exec("BC_INST_EXEC[_COND]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006569 s = zdc_program_execStr(code, &ip->inst_idx, inst == BC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006570 goto read_updated_ip;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006571 case BC_INST_PRINT_STACK: {
6572 size_t idx;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006573 dbg_exec("BC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006574 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006575 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006576 if (s) break;
6577 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006578 break;
6579 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006580 case BC_INST_CLEAR_STACK:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006581 dbg_exec("BC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006582 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006583 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006584 case BC_INST_STACK_LEN:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006585 dbg_exec("BC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006586 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006587 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006588 case BC_INST_DUPLICATE:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006589 dbg_exec("BC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006590 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006591 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006592 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006593 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006594 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006595 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006596 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006597 BcResult *ptr2;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006598 dbg_exec("BC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006599 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006600 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006601 ptr = bc_vec_item_rev(&G.prog.results, 0);
6602 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006603 memcpy(&r, ptr, sizeof(BcResult));
6604 memcpy(ptr, ptr2, sizeof(BcResult));
6605 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006606 break;
6607 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006608 case BC_INST_ASCIIFY:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006609 dbg_exec("BC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006610 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006611 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006612 case BC_INST_PRINT_STREAM:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006613 dbg_exec("BC_INST_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006614 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006615 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006616 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006617 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006618 bool copy = inst == BC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006619 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006620 break;
6621 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006622 case BC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006623 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006624 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006625 free(name);
6626 break;
6627 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006628 case BC_INST_QUIT:
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006629 dbg_exec("BC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006630 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006631 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006632 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006633 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006634 case BC_INST_NQUIT:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006635 dbg_exec("BC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006636 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006637 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006638#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006639 read_updated_ip:
6640 // Instruction stack has changed, read new pointers
6641 ip = bc_vec_top(&G.prog.exestack);
6642 func = bc_program_func(ip->func);
6643 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006644 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006645 }
6646
Denys Vlasenkod38af482018-12-04 19:11:02 +01006647 if (s || G_interrupt) {
6648 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006649 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006650 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006651
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006652 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006653 }
6654
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006655 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006656}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006657#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006658
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006659static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006660{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006661 char *lenv;
6662 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006663
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006664 lenv = getenv(var);
6665 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006666 if (!lenv) return len;
6667
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006668 len = bb_strtou(lenv, NULL, 10) - 1;
6669 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006670 len = BC_NUM_PRINT_WIDTH;
6671
6672 return len;
6673}
6674
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006675static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006676{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006677 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006678
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006679 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006680 s = zbc_parse_text_init(&G.prs, text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006681 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006682
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006683 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006684 BcInstPtr *ip;
6685 BcFunc *f;
6686
Denys Vlasenko39287e02018-12-22 02:23:08 +01006687 dbg_lex("%s:%d G.prs.l.t.t:%d, parsing...", __func__, __LINE__, G.prs.l.t.t);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006688 if (IS_BC) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006689// FIXME: "eating" of stmt delimiters is coded inconsistently
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006690// (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't),
6691// which causes bugs such as "print 1 print 2" erroneously accepted,
6692// or "print 1 else 2" detecting parse error only after executing
6693// "print 1" part.
6694 IF_BC(s = zbc_parse_stmt_or_funcdef(&G.prs));
6695 } else {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006696 IF_DC(s = zdc_parse_expr(&G.prs));
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006697 }
6698 if (s || G_interrupt) {
6699 bc_parse_reset(&G.prs); // includes bc_program_reset()
6700 RETURN_STATUS(BC_STATUS_FAILURE);
6701 }
6702
Denys Vlasenko39287e02018-12-22 02:23:08 +01006703 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006704 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006705 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006706 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006707 break;
6708 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006709
6710 ip = (void*)G.prog.exestack.v;
6711#if SANITY_CHECKS
6712 if (G.prog.exestack.len != 1) // should have only main's IP
6713 bb_error_msg_and_die("BUG:call stack");
6714 if (ip->func != BC_PROG_MAIN)
6715 bb_error_msg_and_die("BUG:not MAIN");
6716#endif
6717 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006718 // bc discards strings, constants and code after each
6719 // top-level statement in the "main program".
6720 // This prevents "yes 1 | bc" from growing its memory
6721 // without bound. This can be done because data stack
6722 // is empty and thus can't hold any references to
6723 // strings or constants, there is no generated code
6724 // which can hold references (after we discard one
6725 // we just executed). Code of functions can have references,
6726 // but bc stores function strings/constants in per-function
6727 // storage.
6728 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006729#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006730 if (G.prog.results.len != 0) // should be empty
6731 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006732#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006733 IF_BC(bc_vec_pop_all(&f->strs);)
6734 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006735 } else {
6736 // Most of dc parsing assumes all whitespace,
6737 // including '\n', is eaten.
6738 if (G.prs.l.t.t == BC_LEX_NLINE) {
6739 s = zbc_lex_next(&G.prs.l);
6740 if (s) RETURN_STATUS(s);
6741 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006742
6743 if (G.prog.results.len == 0
6744 && G.prog.vars.len == 0
6745 ) {
6746 // If stack is empty and no registers exist (TODO: or they are all empty),
6747 // we can get rid of accumulated strings and constants.
6748 // In this example dc process should not grow
6749 // its memory consumption with time:
6750 // yes 1pc | dc
6751 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6752 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6753 }
6754 // The code is discarded always (below), thus this example
6755 // should also not grow its memory consumption with time,
6756 // even though its data stack is not empty:
6757 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006758 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006759 // We drop generated and executed code for both bc and dc:
6760 bc_vec_pop_all(&f->code);
6761 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006762 }
6763
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006764 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006765 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006766}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006767#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006768
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006769static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006770{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006771 // So far bc/dc have no way to include a file from another file,
6772 // therefore we know G.prog.file == NULL on entry
6773 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006774 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006775
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006776 G.prog.file = filename;
6777 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006778 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006779
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006780 do {
6781 s = zbc_vm_process("");
6782 // We do not stop looping on errors here if reading stdin.
6783 // Example: start interactive bc and enter "return".
6784 // It should say "'return' not in a function"
6785 // but should not exit.
6786 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006787 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006788 RETURN_STATUS(s);
6789}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006790#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006791
6792static BC_STATUS zbc_vm_file(const char *file)
6793{
6794 BcStatus s;
6795 FILE *fp;
6796
6797 fp = xfopen_for_read(file);
6798 s = zbc_vm_execute_FILE(fp, file);
6799 fclose(fp);
6800
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006801 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006802}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006803#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006804
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006805#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006806static void bc_vm_info(void)
6807{
6808 printf("%s "BB_VER"\n"
6809 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6810 , applet_name);
6811}
6812
6813static void bc_args(char **argv)
6814{
6815 unsigned opts;
6816 int i;
6817
6818 GETOPT_RESET();
6819#if ENABLE_FEATURE_BC_LONG_OPTIONS
6820 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6821 "warn\0" No_argument "w"
6822 "version\0" No_argument "v"
6823 "standard\0" No_argument "s"
6824 "quiet\0" No_argument "q"
6825 "mathlib\0" No_argument "l"
6826 "interactive\0" No_argument "i"
6827 );
6828#else
6829 opts = option_mask32 |= getopt32(argv, "wvsqli");
6830#endif
6831 if (getenv("POSIXLY_CORRECT"))
6832 option_mask32 |= BC_FLAG_S;
6833
6834 if (opts & BC_FLAG_V) {
6835 bc_vm_info();
6836 exit(0);
6837 }
6838
6839 for (i = optind; argv[i]; ++i)
6840 bc_vec_push(&G.files, argv + i);
6841}
6842
6843static void bc_vm_envArgs(void)
6844{
6845 BcVec v;
6846 char *buf;
6847 char *env_args = getenv("BC_ENV_ARGS");
6848
6849 if (!env_args) return;
6850
6851 G.env_args = xstrdup(env_args);
6852 buf = G.env_args;
6853
6854 bc_vec_init(&v, sizeof(char *), NULL);
6855
6856 while (*(buf = skip_whitespace(buf)) != '\0') {
6857 bc_vec_push(&v, &buf);
6858 buf = skip_non_whitespace(buf);
6859 if (!*buf)
6860 break;
6861 *buf++ = '\0';
6862 }
6863
6864 // NULL terminate, and pass argv[] so that first arg is argv[1]
6865 if (sizeof(int) == sizeof(char*)) {
6866 bc_vec_push(&v, &const_int_0);
6867 } else {
6868 static char *const nullptr = NULL;
6869 bc_vec_push(&v, &nullptr);
6870 }
6871 bc_args(((char **)v.v) - 1);
6872
6873 bc_vec_free(&v);
6874}
6875
6876static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006877 "scale=20"
6878"\n" "define e(x){"
6879"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006880////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6881//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6882//e(-.998896): GNU:.36828580434569428695
6883// above code:.36828580434569428696
6884// actual value:.3682858043456942869594...
6885// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006886"\n" "b=ibase"
6887"\n" "ibase=A"
6888"\n" "if(x<0){"
6889"\n" "n=1"
6890"\n" "x=-x"
6891"\n" "}"
6892"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006893"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006894"\n" "scale=scale(x)+1"
6895"\n" "while(x>1){"
6896"\n" "d+=1"
6897"\n" "x/=2"
6898"\n" "scale+=1"
6899"\n" "}"
6900"\n" "scale=r"
6901"\n" "r=x+1"
6902"\n" "p=x"
6903"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006904"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006905"\n" "p*=x"
6906"\n" "f*=i"
6907"\n" "v=p/f"
6908"\n" "r+=v"
6909"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006910"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006911"\n" "scale=s"
6912"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006913"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006914"\n" "return(r/1)"
6915"\n" "}"
6916"\n" "define l(x){"
6917"\n" "auto b,s,r,p,a,q,i,v"
6918"\n" "b=ibase"
6919"\n" "ibase=A"
6920"\n" "if(x<=0){"
6921"\n" "r=(1-10^scale)/1"
6922"\n" "ibase=b"
6923"\n" "return(r)"
6924"\n" "}"
6925"\n" "s=scale"
6926"\n" "scale+=6"
6927"\n" "p=2"
6928"\n" "while(x>=2){"
6929"\n" "p*=2"
6930"\n" "x=sqrt(x)"
6931"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006932"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006933"\n" "p*=2"
6934"\n" "x=sqrt(x)"
6935"\n" "}"
6936"\n" "r=a=(x-1)/(x+1)"
6937"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006938"\n" "v=1"
6939"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006940"\n" "a*=q"
6941"\n" "v=a/i"
6942"\n" "r+=v"
6943"\n" "}"
6944"\n" "r*=p"
6945"\n" "scale=s"
6946"\n" "ibase=b"
6947"\n" "return(r/1)"
6948"\n" "}"
6949"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006950"\n" "auto b,s,r,a,q,i"
6951"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006952"\n" "b=ibase"
6953"\n" "ibase=A"
6954"\n" "s=scale"
6955"\n" "scale=1.1*s+2"
6956"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006957"\n" "scale=0"
6958"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006959"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006960"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006961"\n" "scale=s+2"
6962"\n" "r=a=x"
6963"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006964"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006965"\n" "a*=q/(i*(i-1))"
6966"\n" "r+=a"
6967"\n" "}"
6968"\n" "scale=s"
6969"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006970"\n" "return(r/1)"
6971"\n" "}"
6972"\n" "define c(x){"
6973"\n" "auto b,s"
6974"\n" "b=ibase"
6975"\n" "ibase=A"
6976"\n" "s=scale"
6977"\n" "scale*=1.2"
6978"\n" "x=s(2*a(1)+x)"
6979"\n" "scale=s"
6980"\n" "ibase=b"
6981"\n" "return(x/1)"
6982"\n" "}"
6983"\n" "define a(x){"
6984"\n" "auto b,s,r,n,a,m,t,f,i,u"
6985"\n" "b=ibase"
6986"\n" "ibase=A"
6987"\n" "n=1"
6988"\n" "if(x<0){"
6989"\n" "n=-1"
6990"\n" "x=-x"
6991"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006992"\n" "if(scale<65){"
6993"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
6994"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006995"\n" "}"
6996"\n" "s=scale"
6997"\n" "if(x>.2){"
6998"\n" "scale+=5"
6999"\n" "a=a(.2)"
7000"\n" "}"
7001"\n" "scale=s+3"
7002"\n" "while(x>.2){"
7003"\n" "m+=1"
7004"\n" "x=(x-.2)/(1+.2*x)"
7005"\n" "}"
7006"\n" "r=u=x"
7007"\n" "f=-x*x"
7008"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007009"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007010"\n" "u*=f"
7011"\n" "t=u/i"
7012"\n" "r+=t"
7013"\n" "}"
7014"\n" "scale=s"
7015"\n" "ibase=b"
7016"\n" "return((m*a+r)/n)"
7017"\n" "}"
7018"\n" "define j(n,x){"
7019"\n" "auto b,s,o,a,i,v,f"
7020"\n" "b=ibase"
7021"\n" "ibase=A"
7022"\n" "s=scale"
7023"\n" "scale=0"
7024"\n" "n/=1"
7025"\n" "if(n<0){"
7026"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007027"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007028"\n" "}"
7029"\n" "a=1"
7030"\n" "for(i=2;i<=n;++i)a*=i"
7031"\n" "scale=1.5*s"
7032"\n" "a=(x^n)/2^n/a"
7033"\n" "r=v=1"
7034"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007035"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007036"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007037"\n" "v=v*f/i/(n+i)"
7038"\n" "r+=v"
7039"\n" "}"
7040"\n" "scale=s"
7041"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007042"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007043"\n" "return(a*r/1)"
7044"\n" "}"
7045};
7046#endif // ENABLE_BC
7047
Denys Vlasenko19f11072018-12-12 22:48:19 +01007048static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007049{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007050 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007051 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007052 size_t i;
7053
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007054#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007055 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007056 // We know that internal library is not buggy,
7057 // thus error checking is normally disabled.
7058# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007059 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007060 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007061 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007062 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007063#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007064
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007065 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007066 fname = (void*)G.files.v;
7067 for (i = 0; i < G.files.len; i++) {
7068 s = zbc_vm_file(*fname++);
7069 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7070 // Debug config, non-interactive mode:
7071 // return all the way back to main.
7072 // Non-debug builds do not come here
7073 // in non-interactive mode, they exit.
7074 RETURN_STATUS(s);
7075 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007076 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007077
Denys Vlasenko91cde952018-12-10 20:56:08 +01007078 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007079 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007080
Denys Vlasenko19f11072018-12-12 22:48:19 +01007081 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007082}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007083#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007084
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007085#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007086static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007087{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007088 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007089 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007090 bc_vec_free(&G.prog.vars);
7091 bc_vec_free(&G.prog.var_map);
7092 bc_vec_free(&G.prog.arrs);
7093 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007094 IF_DC(bc_vec_free(&G.prog.strs);)
7095 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007096 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007097 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007098 IF_BC(bc_num_free(&G.prog.last);)
7099 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007100 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007101 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007102}
7103
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007104static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007105{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007106 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007107 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007108 bc_parse_free(&G.prs);
7109 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007110}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007111#endif
7112
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007113static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007114{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007115 BcInstPtr ip;
7116
Denys Vlasenko82269122018-12-14 16:30:56 +01007117 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007118 memset(&ip, 0, sizeof(BcInstPtr));
7119
Denys Vlasenko82269122018-12-14 16:30:56 +01007120 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007121 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007122 G.prog.ob_t = 10;
7123
Denys Vlasenko503faf92018-12-20 16:24:18 +01007124 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7125 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007126
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007127 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007128 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007129
Denys Vlasenko503faf92018-12-20 16:24:18 +01007130 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7131 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007132
7133 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007134 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007135
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007136 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007137 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007138 // a valid function name (and be short)
7139 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007140 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007141 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007142 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007143 bc_program_add_fn();
7144 bc_program_add_fn();
7145 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007146
7147 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007148 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007149
7150 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007151 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007152
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007153 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7154 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007155 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007156 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7157 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007158
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007159 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007160}
Gavin Howard01055ba2018-11-03 11:00:21 -06007161
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007162static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007163{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007164#if ENABLE_FEATURE_EDITING
7165 G.line_input_state = new_line_input_t(DO_HISTORY);
7166#endif
7167 G.prog.len = bc_vm_envLen(env_len);
7168
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007169 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007170 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007171 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007172 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007173
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007174//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7175//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007176 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007177#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007178 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007179 // With SA_RESTART, most system calls will restart
7180 // (IOW: they won't fail with EINTR).
7181 // In particular, this means ^C won't cause
7182 // stdout to get into "error state" if SIGINT hits
7183 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007184 //
7185 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007186 // will only be handled after [Enter] since read()
7187 // from stdin is not interrupted by ^C either,
7188 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007189 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007190 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7191
7192 // Without SA_RESTART, this exhibits a bug:
7193 // "while (1) print 1" and try ^C-ing it.
7194 // Intermittently, instead of returning to input line,
7195 // you'll get "output error: Interrupted system call"
7196 // and exit.
7197 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007198#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007199 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007200 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007201 return 0; // "not a tty"
7202}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007203
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007204static BcStatus bc_vm_run(void)
7205{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007206 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007207#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007208 if (G_exiting) // it was actually "halt" or "quit"
7209 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007210 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007211# if ENABLE_FEATURE_EDITING
7212 free_line_input_t(G.line_input_state);
7213# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007214 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007215#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007216 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007217 return st;
7218}
7219
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007220#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007221int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007222int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007223{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007224 int is_tty;
7225
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007226 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007227
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007228 is_tty = bc_vm_init("BC_LINE_LENGTH");
7229
7230 bc_args(argv);
7231
7232 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7233 bc_vm_info();
7234
7235 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007236}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007237#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007238
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007239#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007240int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007241int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007242{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007243 int noscript;
7244
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007245 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007246
7247 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7248 // 1 char wider than bc from the same package.
7249 // Both default width, and xC_LINE_LENGTH=N are wider:
7250 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007251 // |1234\ |
7252 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007253 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007254 // |123\ |
7255 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007256 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007257 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007258
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007259 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7260 noscript = BC_FLAG_I;
7261 for (;;) {
7262 int n = getopt(argc, argv, "e:f:x");
7263 if (n <= 0)
7264 break;
7265 switch (n) {
7266 case 'e':
7267 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007268 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007269 if (n) return n;
7270 break;
7271 case 'f':
7272 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007273 n = zbc_vm_file(optarg);
7274 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007275 break;
7276 case 'x':
7277 option_mask32 |= DC_FLAG_X;
7278 break;
7279 default:
7280 bb_show_usage();
7281 }
7282 }
7283 argv += optind;
7284
7285 while (*argv) {
7286 noscript = 0;
7287 bc_vec_push(&G.files, argv++);
7288 }
7289
7290 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7291
7292 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007293}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007294#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007295
7296#endif // not DC_SMALL