blob: 25150a9ebb4baaeb17cc9bca0f30f9c534e67543 [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;
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002506 BcDig two_digs[2];
Gavin Howard01055ba2018-11-03 11:00:21 -06002507
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002508 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002509 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002510 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko1acac7f2018-12-22 23:14:48 +01002511 RETURN_STATUS(bc_error("not an integer"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002512 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002513 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002514
2515 bc_num_expand(d, c->len);
2516 bc_num_init(&base, c->len);
2517 bc_num_init(&exp, b->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002518 bc_num_init(&temp, b->len);
2519
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002520 two.cap = ARRAY_SIZE(two_digs);
2521 two.num = two_digs;
Gavin Howard01055ba2018-11-03 11:00:21 -06002522 bc_num_one(&two);
Denys Vlasenko01eb5e92018-12-22 23:59:21 +01002523 two_digs[0] = 2;
2524
Gavin Howard01055ba2018-11-03 11:00:21 -06002525 bc_num_one(d);
2526
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002527 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002528 if (s) goto err;
2529 bc_num_copy(&exp, b);
2530
2531 while (exp.len != 0) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002532 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002533 if (s) goto err;
2534
2535 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002536 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002537 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002538 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002539 if (s) goto err;
2540 }
2541
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002542 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002543 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002544 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002545 if (s) goto err;
2546 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002547 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06002548 bc_num_free(&temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06002549 bc_num_free(&exp);
2550 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002551 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002552}
Denys Vlasenkofa210792018-12-19 19:35:40 +01002553#define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002554#endif // ENABLE_DC
2555
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002556#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002557static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002558{
Denys Vlasenko87888ce2018-12-19 17:55:23 +01002559 BcId *autoid;
Gavin Howard01055ba2018-11-03 11:00:21 -06002560 BcId a;
2561 size_t i;
2562
Denys Vlasenko87888ce2018-12-19 17:55:23 +01002563 autoid = (void*)f->autos.v;
2564 for (i = 0; i < f->autos.len; i++, autoid++) {
2565 if (strcmp(name, autoid->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002566 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002567 }
2568
2569 a.idx = var;
2570 a.name = name;
2571
2572 bc_vec_push(&f->autos, &a);
2573
Denys Vlasenko29301232018-12-11 15:29:32 +01002574 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002575}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002576#define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002577#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002578
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002579static FAST_FUNC void bc_string_free(void *string)
2580{
2581 free(*(char**)string);
2582}
2583
Gavin Howard01055ba2018-11-03 11:00:21 -06002584static void bc_func_init(BcFunc *f)
2585{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002586 bc_char_vec_init(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002587 IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);)
2588 IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002589 IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);)
2590 IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01002591 IF_BC(f->nparams = 0;)
Gavin Howard01055ba2018-11-03 11:00:21 -06002592}
2593
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002594static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002595{
2596 BcFunc *f = (BcFunc *) func;
2597 bc_vec_free(&f->code);
Denys Vlasenko503faf92018-12-20 16:24:18 +01002598 IF_BC(bc_vec_free(&f->labels);)
2599 IF_BC(bc_vec_free(&f->autos);)
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01002600 IF_BC(bc_vec_free(&f->strs);)
2601 IF_BC(bc_vec_free(&f->consts);)
Gavin Howard01055ba2018-11-03 11:00:21 -06002602}
2603
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002604static void bc_array_expand(BcVec *a, size_t len);
2605
Gavin Howard01055ba2018-11-03 11:00:21 -06002606static void bc_array_init(BcVec *a, bool nums)
2607{
2608 if (nums)
2609 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2610 else
2611 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2612 bc_array_expand(a, 1);
2613}
2614
Gavin Howard01055ba2018-11-03 11:00:21 -06002615static void bc_array_expand(BcVec *a, size_t len)
2616{
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002617 if (a->dtor == bc_num_free
2618 // && a->size == sizeof(BcNum) - always true
2619 ) {
2620 BcNum n;
Gavin Howard01055ba2018-11-03 11:00:21 -06002621 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002622 bc_num_init_DEF_SIZE(&n);
2623 bc_vec_push(a, &n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002624 }
Denys Vlasenkoe2e6ffd2018-12-18 12:23:16 +01002625 } else {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002626 BcVec v;
Gavin Howard01055ba2018-11-03 11:00:21 -06002627 while (len > a->len) {
Denys Vlasenkod6e24bd2018-12-18 20:10:48 +01002628 bc_array_init(&v, true);
2629 bc_vec_push(a, &v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002630 }
2631 }
2632}
2633
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002634static void bc_array_copy(BcVec *d, const BcVec *s)
2635{
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002636 BcNum *dnum, *snum;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002637 size_t i;
2638
2639 bc_vec_pop_all(d);
2640 bc_vec_expand(d, s->cap);
2641 d->len = s->len;
2642
Denys Vlasenkoeac0de52018-12-19 17:59:30 +01002643 dnum = (void*)d->v;
2644 snum = (void*)s->v;
2645 for (i = 0; i < s->len; i++, dnum++, snum++) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002646 bc_num_init(dnum, snum->len);
2647 bc_num_copy(dnum, snum);
2648 }
2649}
2650
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002651#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01002652static void dc_result_copy(BcResult *d, BcResult *src)
Gavin Howard01055ba2018-11-03 11:00:21 -06002653{
2654 d->t = src->t;
2655
2656 switch (d->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002657 case BC_RESULT_TEMP:
2658 case BC_RESULT_IBASE:
2659 case BC_RESULT_SCALE:
2660 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002661 bc_num_init(&d->d.n, src->d.n.len);
2662 bc_num_copy(&d->d.n, &src->d.n);
2663 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002664 case BC_RESULT_VAR:
2665 case BC_RESULT_ARRAY:
2666 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002667 d->d.id.name = xstrdup(src->d.id.name);
2668 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002669 case BC_RESULT_CONSTANT:
2670 case BC_RESULT_LAST:
2671 case BC_RESULT_ONE:
2672 case BC_RESULT_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06002673 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2674 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002675 }
2676}
2677#endif // ENABLE_DC
2678
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002679static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002680{
2681 BcResult *r = (BcResult *) result;
2682
2683 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002684 case BC_RESULT_TEMP:
2685 case BC_RESULT_IBASE:
2686 case BC_RESULT_SCALE:
2687 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06002688 bc_num_free(&r->d.n);
2689 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002690 case BC_RESULT_VAR:
2691 case BC_RESULT_ARRAY:
2692 case BC_RESULT_ARRAY_ELEM:
Gavin Howard01055ba2018-11-03 11:00:21 -06002693 free(r->d.id.name);
2694 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002695 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06002696 // Do nothing.
2697 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06002698 }
2699}
2700
2701static void bc_lex_lineComment(BcLex *l)
2702{
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002703 // Try: echo -n '#foo' | bc
2704 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002705 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002706 i = l->i;
2707 while (i < l->len && l->buf[i] != '\n')
2708 i++;
2709 l->i = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002710}
2711
2712static void bc_lex_whitespace(BcLex *l)
2713{
Gavin Howard01055ba2018-11-03 11:00:21 -06002714 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko89198a92018-12-13 21:31:29 +01002715 for (;;) {
2716 char c = l->buf[l->i];
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002717 if (c == '\n') // this is BC_LEX_NLINE, not BC_LEX_WHITESPACE
2718 break;
2719 if (!isspace(c))
Denys Vlasenko89198a92018-12-13 21:31:29 +01002720 break;
2721 l->i++;
2722 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002723}
2724
Denys Vlasenko29301232018-12-11 15:29:32 +01002725static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002726{
2727 const char *buf = l->buf + l->i;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002728 size_t len, i, ccnt;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002729 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002730
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002731 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002732 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002733 ccnt = i = 0;
2734 for (;;) {
2735 char c = buf[i];
2736 if (c == '\0')
2737 break;
2738 if (c == '\\' && buf[i + 1] == '\n') {
2739 i += 2;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002740 //number_of_backslashes++ - see comment below
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002741 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002742 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002743 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2744 if (c != '.') break;
2745 // if '.' was already seen, stop on second one:
2746 if (pt) break;
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002747 pt = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06002748 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002749 // buf[i] is one of "0-9A-F."
2750 i++;
2751 if (c != '.')
2752 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002753 }
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002754 //ccnt is the number of chars in the number string, excluding possible
2755 //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions).
2756 //i is buf[i] index of the first not-yet-parsed char after that.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002757 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002758
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01002759 // This might overestimate the size, if there are "\<NL>"'s
2760 // in the number. Subtracting number_of_backslashes*2 correctly
2761 // is not that easy: consider that in the case of "NNN.\<NL>"
2762 // loop above will count "\<NL>" before it realizes it is not
2763 // in fact *inside* the number:
2764 len = ccnt + 1; // +1 byte for NUL termination
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002765
Denys Vlasenko64074a12018-12-07 15:50:14 +01002766 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2767 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2768 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002769 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002770 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002771
Denys Vlasenko7d628012018-12-04 21:46:47 +01002772 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002773 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002774 bc_vec_push(&l->t.v, &start);
2775
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002776 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002777 // If we have hit a backslash, skip it. We don't have
2778 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002779 if (*buf == '\\') {
2780 buf += 2;
2781 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002782 continue;
2783 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002784 bc_vec_push(&l->t.v, buf);
2785 buf++;
2786 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002787 }
2788
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002789 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002790
Denys Vlasenko29301232018-12-11 15:29:32 +01002791 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002792}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002793#define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002794
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002795static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002796{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002797 size_t i;
2798 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002799
2800 l->t.t = BC_LEX_NAME;
2801
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002802 i = 0;
2803 buf = l->buf + l->i - 1;
2804 for (;;) {
2805 char c = buf[i];
2806 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2807 i++;
2808 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002809
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002810#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002811 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2812 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2813 if (i > BC_MAX_STRING)
2814 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2815 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002816#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002817 bc_vec_string(&l->t.v, i, buf);
2818
2819 // Increment the index. We minus 1 because it has already been incremented.
2820 l->i += i - 1;
2821
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002822 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002823}
2824
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002825static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002826{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002827 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002828}
2829
2830static void bc_lex_free(BcLex *l)
2831{
2832 bc_vec_free(&l->t.v);
2833}
2834
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002835static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002836{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002837 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002838 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002839}
2840
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002841static bool bc_lex_more_input(BcLex *l)
2842{
2843 size_t str;
2844 bool comment;
2845
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002846 bc_vec_pop_all(&G.input_buffer);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002847
2848 // This loop is complex because the vm tries not to send any lines that end
2849 // with a backslash to the parser. The reason for that is because the parser
2850 // treats a backslash+newline combo as whitespace, per the bc spec. In that
2851 // case, and for strings and comments, the parser will expect more stuff.
2852 comment = false;
2853 str = 0;
2854 for (;;) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002855 size_t prevlen = G.input_buffer.len;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002856 char *string;
2857
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002858 bc_read_line(&G.input_buffer, G.input_fp);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002859 // No more input means EOF
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002860 if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002861 break;
2862
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002863 string = G.input_buffer.v + prevlen;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002864 while (*string) {
2865 char c = *string;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002866 if (string == G.input_buffer.v || string[-1] != '\\') {
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002867 if (IS_BC)
2868 str ^= (c == '"');
2869 else {
2870 if (c == ']')
2871 str -= 1;
2872 else if (c == '[')
2873 str += 1;
2874 }
2875 }
2876 string++;
2877 if (c == '/' && *string == '*') {
2878 comment = true;
2879 string++;
2880 continue;
2881 }
2882 if (c == '*' && *string == '/') {
2883 comment = false;
2884 string++;
2885 }
2886 }
2887 if (str != 0 || comment) {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002888 G.input_buffer.len--; // backstep over the trailing NUL byte
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002889 continue;
2890 }
2891
2892 // Check for backslash+newline.
2893 // we do not check that last char is '\n' -
2894 // if it is not, then it's EOF, and looping back
2895 // to bc_read_line() will detect it:
2896 string -= 2;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002897 if (string >= G.input_buffer.v && *string == '\\') {
2898 G.input_buffer.len--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002899 continue;
2900 }
2901
2902 break;
2903 }
2904
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002905 l->buf = G.input_buffer.v;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002906 l->i = 0;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002907// bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v);
2908 l->len = G.input_buffer.len - 1; // do not include NUL
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002909
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002910 return l->len != 0;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01002911}
2912
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002913IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);)
2914IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);)
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01002915#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
2916#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002917
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002918static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002919{
2920 BcStatus s;
2921
2922 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002923 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002924
2925 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002926 G.err_line = l->line;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002927 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002928
2929 // Loop until failure or we don't have whitespace. This
2930 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01002931 // Comments are also BC_LEX_WHITESPACE tokens and eaten here.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002932 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002933 do {
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002934 if (l->i == l->len) {
Denys Vlasenko55f3cab2018-12-18 14:37:16 +01002935 l->t.t = BC_LEX_EOF;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01002936 if (!G.input_fp)
2937 RETURN_STATUS(BC_STATUS_SUCCESS);
2938 if (!bc_lex_more_input(l)) {
2939 G.input_fp = NULL;
2940 RETURN_STATUS(BC_STATUS_SUCCESS);
2941 }
2942 // here it's guaranteed that l->i is below l->len
2943 }
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002944 dbg_lex("next string to parse:'%.*s'",
Denys Vlasenko0a238142018-12-14 16:48:34 +01002945 (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)),
2946 l->buf + l->i);
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01002947 if (IS_BC) {
2948 IF_BC(s = zbc_lex_token(l));
2949 } else {
2950 IF_DC(s = zdc_lex_token(l));
2951 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002952 } while (!s && l->t.t == BC_LEX_WHITESPACE);
Denys Vlasenko17df8822018-12-14 23:00:24 +01002953 dbg_lex("l->t.t from string:%d", l->t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06002954
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002955 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002956}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002957#define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002958
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002959#if ENABLE_BC
Denys Vlasenko202dd192018-12-16 17:30:35 +01002960static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l)
2961{
2962 if (l->t.t == BC_LEX_NLINE)
2963 RETURN_STATUS(zbc_lex_next(l));
2964 RETURN_STATUS(BC_STATUS_SUCCESS);
2965}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002966#define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko202dd192018-12-16 17:30:35 +01002967
2968static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l)
2969{
2970 BcStatus s;
2971 s = zbc_lex_next(l);
2972 if (s) RETURN_STATUS(s);
2973 // if(cond)<newline>stmt is accepted too (but not 2+ newlines)
2974 s = zbc_lex_skip_if_at_NLINE(l);
2975 RETURN_STATUS(s);
2976}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002977#define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01002978#endif
Denys Vlasenko202dd192018-12-16 17:30:35 +01002979
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01002980static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002981{
2982 l->buf = text;
2983 l->i = 0;
2984 l->len = strlen(text);
2985 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002986 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002987}
Denys Vlasenkoec603182018-12-17 10:34:02 +01002988#define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06002989
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002990#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002991static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002992{
2993 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002994 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002995 const char *buf = l->buf + l->i - 1;
2996
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002997 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2998 const char *keyword8 = bc_lex_kws[i].name8;
2999 unsigned j = 0;
3000 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3001 j++;
3002 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003003 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003004 if (keyword8[j] != '\0')
3005 continue;
3006 match:
3007 // buf starts with keyword bc_lex_kws[i]
Denys Vlasenko5acd14b2018-12-20 16:48:50 +01003008 if (isalnum(buf[j]) || buf[j]=='_')
3009 continue; // "ifz" does not match "if" keyword, "if." does
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003010 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003011 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003012 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003013 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003014 }
3015
3016 // We minus 1 because the index has already been incremented.
3017 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003018 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003019 }
3020
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003021 bc_lex_name(l);
Denys Vlasenko0f31a5c2018-12-18 03:16:48 +01003022 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003023
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003024 if (l->t.v.len > 2) {
3025 // Prevent this:
3026 // >>> qwe=1
3027 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3028 // '
3029 unsigned len = strchrnul(buf, '\n') - buf;
3030 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3031 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003032
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003033 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003034}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003035#define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003036
Denys Vlasenko26819db2018-12-12 16:08:46 +01003037static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003038{
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003039 size_t len, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003040
3041 l->t.t = BC_LEX_STR;
3042
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003043 nls = 0;
3044 i = l->i;
3045 for (;;) {
3046 char c = l->buf[i];
3047 if (c == '\0') {
3048 l->i = i;
3049 RETURN_STATUS(bc_error("string end could not be found"));
3050 }
3051 if (c == '"')
3052 break;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003053 nls += (c == '\n');
Denys Vlasenko07597cd2018-12-18 14:03:20 +01003054 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003055 }
3056
3057 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003058 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3059 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3060 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003061 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003062 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003063 bc_vec_string(&l->t.v, len, l->buf + l->i);
3064
3065 l->i = i + 1;
3066 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003067 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003068
Denys Vlasenko26819db2018-12-12 16:08:46 +01003069 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003070}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003071#define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003072
Denys Vlasenko0a238142018-12-14 16:48:34 +01003073static void bc_lex_assign(BcLex *l, unsigned with_and_without)
Gavin Howard01055ba2018-11-03 11:00:21 -06003074{
3075 if (l->buf[l->i] == '=') {
3076 ++l->i;
Denys Vlasenko0a238142018-12-14 16:48:34 +01003077 with_and_without >>= 8; // store "with" value
3078 } // else store "without" value
3079 l->t.t = (with_and_without & 0xff);
Gavin Howard01055ba2018-11-03 11:00:21 -06003080}
Denys Vlasenko0a238142018-12-14 16:48:34 +01003081#define bc_lex_assign(l, with, without) \
3082 bc_lex_assign(l, ((with)<<8)|(without))
Gavin Howard01055ba2018-11-03 11:00:21 -06003083
Denys Vlasenko29301232018-12-11 15:29:32 +01003084static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003085{
3086 size_t i, nls = 0;
3087 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003088
3089 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003090 i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003091 for (;;) {
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003092 char c = buf[++i];
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003093 check_star:
3094 if (c == '*') {
3095 c = buf[++i];
3096 if (c == '/')
3097 break;
3098 goto check_star;
3099 }
3100 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003101 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003102 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003103 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003104 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003105 }
3106
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003107 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003108 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003109 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003110
Denys Vlasenko29301232018-12-11 15:29:32 +01003111 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003112}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003113#define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003114
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003115#undef zbc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003116static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003117{
3118 BcStatus s = BC_STATUS_SUCCESS;
3119 char c = l->buf[l->i++], c2;
3120
3121 // This is the workhorse of the lexer.
3122 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003123// case '\0': // probably never reached
3124// l->i--;
3125// l->t.t = BC_LEX_EOF;
3126// l->newline = true;
3127// break;
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01003128 case '\n':
3129 l->t.t = BC_LEX_NLINE;
3130 l->newline = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06003131 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003132 case '\t':
3133 case '\v':
3134 case '\f':
3135 case '\r':
3136 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 bc_lex_whitespace(l);
3138 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003139 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003140 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003142 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003143 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003146 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003147 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003148 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003149 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003150 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003151 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003152 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003154 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003155 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3156 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003157 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003158 c2 = l->buf[l->i];
3159 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003160 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003161 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003162 ++l->i;
3163 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003164 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003165 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003166 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003167 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003168 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003169 case '(':
3170 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003171 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3172 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003173 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003174 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3175 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003176 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003177 c2 = l->buf[l->i];
3178 if (c2 == '+') {
3179 ++l->i;
3180 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003181 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003182 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3183 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003184 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003185 l->t.t = BC_LEX_COMMA;
3186 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003187 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003188 c2 = l->buf[l->i];
3189 if (c2 == '-') {
3190 ++l->i;
3191 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003192 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003193 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3194 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003195 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003196 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003197 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003198 else {
3199 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003200 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003201 }
3202 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003204 c2 = l->buf[l->i];
3205 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003206 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003207 else
3208 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3209 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003210 case '0':
3211 case '1':
3212 case '2':
3213 case '3':
3214 case '4':
3215 case '5':
3216 case '6':
3217 case '7':
3218 case '8':
3219 case '9':
3220 case 'A':
3221 case 'B':
3222 case 'C':
3223 case 'D':
3224 case 'E':
3225 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003226 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003227 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003228 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003229 l->t.t = BC_LEX_SCOLON;
3230 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003231 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003232 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3233 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003234 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003235 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3236 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003237 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003238 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3239 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003240 case '[':
3241 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003242 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3243 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003244 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003245 if (l->buf[l->i] == '\n') {
3246 l->t.t = BC_LEX_WHITESPACE;
3247 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003248 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003249 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003251 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3253 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003254 case 'a':
3255 case 'b':
3256 case 'c':
3257 case 'd':
3258 case 'e':
3259 case 'f':
3260 case 'g':
3261 case 'h':
3262 case 'i':
3263 case 'j':
3264 case 'k':
3265 case 'l':
3266 case 'm':
3267 case 'n':
3268 case 'o':
3269 case 'p':
3270 case 'q':
3271 case 'r':
3272 case 's':
3273 case 't':
3274 case 'u':
3275 case 'v':
3276 case 'w':
3277 case 'x':
3278 case 'y':
3279 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003280 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003281 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003282 case '{':
3283 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003284 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3285 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003286 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003287 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003288 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003289 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenkoec603182018-12-17 10:34:02 +01003290 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06003291 ++l->i;
3292 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003293 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003294 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003295 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003296 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003297 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003298 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003299 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003300 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003301 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003302 }
3303
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003304 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003305}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003306#define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003307#endif // ENABLE_BC
3308
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003309#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003310static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003311{
Gavin Howard01055ba2018-11-03 11:00:21 -06003312 if (isspace(l->buf[l->i - 1])) {
3313 bc_lex_whitespace(l);
3314 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003315 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003316 RETURN_STATUS(bc_error("extended register"));
3317 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003318 }
3319 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003320 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003321 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003322 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003323 l->t.t = BC_LEX_NAME;
3324 }
3325
Denys Vlasenko29301232018-12-11 15:29:32 +01003326 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003327}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003328#define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003329
Denys Vlasenko29301232018-12-11 15:29:32 +01003330static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003331{
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003332 size_t depth, nls, i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003333
3334 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003335 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003336
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003337 nls = 0;
3338 depth = 1;
3339 i = l->i;
3340 for (;;) {
3341 char c = l->buf[i];
3342 if (c == '\0') {
3343 l->i = i;
3344 RETURN_STATUS(bc_error("string end could not be found"));
3345 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003346 nls += (c == '\n');
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003347 if (i == l->i || l->buf[i - 1] != '\\') {
3348 if (c == '[') depth++;
3349 if (c == ']')
3350 if (--depth == 0)
3351 break;
3352 }
3353 bc_vec_push(&l->t.v, &l->buf[i]);
3354 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003355 }
Denys Vlasenkoef271da2018-12-18 13:48:37 +01003356 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003357
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003358 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003359 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3360 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3361 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003362 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003363 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003364
3365 l->i = i;
3366 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003367 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003368
Denys Vlasenko29301232018-12-11 15:29:32 +01003369 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003370}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003371#define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003372
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003373#undef zdc_lex_token
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003374static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003375{
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003376 static const //BcLexType - should be this type, but narrower type saves size:
3377 uint8_t
3378 dc_lex_regs[] = {
3379 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
3380 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
3381 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
3382 BC_LEX_STORE_PUSH,
3383 };
3384 static const //BcLexType - should be this type
3385 uint8_t
3386 dc_lex_tokens[] = {
3387 /* %&'( */
3388 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
3389 /* )*+, */
3390 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
3391 /* -./ */
3392 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
3393 /* 0123456789 */
3394 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3395 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3396 BC_LEX_INVALID, BC_LEX_INVALID,
3397 /* :;<=>?@ */
3398 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
3399 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
3400 /* ABCDEFGH */
3401 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3402 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
3403 /* IJKLMNOP */
3404 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
3405 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
3406 /* QRSTUVWXY */
3407 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
3408 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
3409 /* Z[\] */
3410 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
3411 /* ^_` */
3412 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
3413 /* abcdefgh */
3414 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
3415 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
3416 /* ijklmnop */
3417 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
3418 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
3419 /* qrstuvwx */
3420 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
3421 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
3422 /* yz */
3423 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
3424 /* {|}~ */
3425 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
3426 };
3427
Gavin Howard01055ba2018-11-03 11:00:21 -06003428 BcStatus s = BC_STATUS_SUCCESS;
3429 char c = l->buf[l->i++], c2;
3430 size_t i;
3431
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003432 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3433 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003434 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003435 }
3436
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003437 if (c >= '%' && c <= '~'
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003438 && (l->t.t = dc_lex_tokens[c - '%']) != BC_LEX_INVALID
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003439 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003440 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003441 }
3442
3443 // This is the workhorse of the lexer.
3444 switch (c) {
Denys Vlasenkob44a7f12018-12-17 11:58:20 +01003445// case '\0': // probably never reached
3446// l->t.t = BC_LEX_EOF;
3447// break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003448 case '\n':
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003449 // '\n' is BC_LEX_NLINE, not BC_LEX_WHITESPACE
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003450 // (and "case '\n':" is not just empty here)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003451 // only to allow interactive dc have a way to exit
3452 // "parse" stage of "parse,execute" loop
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01003453 // on <enter>, not on _next_ token (which would mean
3454 // commands are not executed on pressing <enter>).
Denys Vlasenkobadf6832018-12-22 18:04:08 +01003455 // IOW: typing "1p<enter>" should print "1" _at once_,
3456 // not after some more input.
3457 l->t.t = BC_LEX_NLINE;
3458 l->newline = true;
3459 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003460 case '\t':
3461 case '\v':
3462 case '\f':
3463 case '\r':
3464 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003465 l->newline = (c == '\n');
3466 bc_lex_whitespace(l);
3467 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003468 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003469 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003470 if (c2 == '=')
3471 l->t.t = BC_LEX_OP_REL_NE;
3472 else if (c2 == '<')
3473 l->t.t = BC_LEX_OP_REL_LE;
3474 else if (c2 == '>')
3475 l->t.t = BC_LEX_OP_REL_GE;
3476 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003477 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003478 ++l->i;
3479 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003480 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003481 bc_lex_lineComment(l);
3482 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003483 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003484 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003485 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003486 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003487 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003488 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003489 case '0':
3490 case '1':
3491 case '2':
3492 case '3':
3493 case '4':
3494 case '5':
3495 case '6':
3496 case '7':
3497 case '8':
3498 case '9':
3499 case 'A':
3500 case 'B':
3501 case 'C':
3502 case 'D':
3503 case 'E':
3504 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003505 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003506 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003507 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003508 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003509 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003510 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003511 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003512 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003513 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003514 }
3515
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003516 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003517}
Denys Vlasenko5cf0b2d2018-12-22 18:24:19 +01003518#define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003519#endif // ENABLE_DC
3520
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003521static void bc_parse_push(BcParse *p, char i)
3522{
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003523 dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003524 bc_vec_pushByte(&p->func->code, i);
3525}
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003526
Gavin Howard01055ba2018-11-03 11:00:21 -06003527static void bc_parse_pushName(BcParse *p, char *name)
3528{
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003529 while (*name)
3530 bc_parse_push(p, *name++);
Gavin Howard01055ba2018-11-03 11:00:21 -06003531 bc_parse_push(p, BC_PARSE_STREND);
Gavin Howard01055ba2018-11-03 11:00:21 -06003532}
3533
3534static void bc_parse_pushIndex(BcParse *p, size_t idx)
3535{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003536 size_t mask;
3537 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003538
Denys Vlasenkof4f10722018-12-18 02:23:53 +01003539 dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003540 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3541 amt = sizeof(idx);
3542 do {
3543 if (idx & mask) break;
3544 mask >>= 8;
3545 amt--;
3546 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003547
3548 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003549
3550 while (idx != 0) {
3551 bc_parse_push(p, (unsigned char)idx);
3552 idx >>= 8;
3553 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003554}
3555
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003556#if ENABLE_BC
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003557static void bc_parse_pushJUMP(BcParse *p, size_t idx)
3558{
3559 bc_parse_push(p, BC_INST_JUMP);
3560 bc_parse_pushIndex(p, idx);
3561}
3562
3563static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx)
3564{
3565 bc_parse_push(p, BC_INST_JUMP_ZERO);
3566 bc_parse_pushIndex(p, idx);
3567}
3568
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003569static BC_STATUS zbc_parse_pushSTR(BcParse *p)
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003570{
3571 char *str = xstrdup(p->l.t.v.v);
3572
3573 bc_parse_push(p, BC_INST_STR);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003574 bc_parse_pushIndex(p, p->func->strs.len);
3575 bc_vec_push(&p->func->strs, &str);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003576
3577 RETURN_STATUS(zbc_lex_next(&p->l));
3578}
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01003579#define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003580#endif
3581
3582static void bc_parse_pushNUM(BcParse *p)
3583{
3584 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003585#if ENABLE_BC && ENABLE_DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003586 size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003587#elif ENABLE_BC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003588 size_t idx = bc_vec_push(&p->func->consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003589#else // DC
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003590 size_t idx = bc_vec_push(&G.prog.consts, &num);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003591#endif
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003592 bc_parse_push(p, BC_INST_NUM);
3593 bc_parse_pushIndex(p, idx);
3594}
Denys Vlasenko44dbe672018-12-19 19:10:40 +01003595
Denys Vlasenkof86e9602018-12-14 17:01:56 +01003596static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003597{
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003598 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003599
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003600 RETURN_STATUS(zbc_lex_text_init(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003601}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003602#define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003603
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003604// Called when parsing or execution detects a failure,
3605// resets execution structures.
3606static void bc_program_reset(void)
3607{
3608 BcFunc *f;
3609 BcInstPtr *ip;
3610
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003611 bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003612 bc_vec_pop_all(&G.prog.results);
3613
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01003614 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01003615 ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko24e41942018-12-21 23:01:26 +01003616 ip->inst_idx = f->code.len;
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003617}
3618
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003619// Called when parsing code detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003620// resets parsing structures.
3621static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003622{
3623 if (p->fidx != BC_PROG_MAIN) {
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01003624 bc_func_free(p->func);
3625 bc_func_init(p->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06003626
Denys Vlasenko65e10462018-12-19 15:13:14 +01003627 p->fidx = BC_PROG_MAIN;
3628 p->func = bc_program_func_BC_PROG_MAIN();
Gavin Howard01055ba2018-11-03 11:00:21 -06003629 }
3630
3631 p->l.i = p->l.len;
3632 p->l.t.t = BC_LEX_EOF;
Gavin Howard01055ba2018-11-03 11:00:21 -06003633
Denys Vlasenko503faf92018-12-20 16:24:18 +01003634 IF_BC(bc_vec_pop_all(&p->exits);)
3635 IF_BC(bc_vec_pop_all(&p->conds);)
3636 IF_BC(bc_vec_pop_all(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003637
Denys Vlasenkod38af482018-12-04 19:11:02 +01003638 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003639}
3640
3641static void bc_parse_free(BcParse *p)
3642{
Denys Vlasenko503faf92018-12-20 16:24:18 +01003643 IF_BC(bc_vec_free(&p->exits);)
3644 IF_BC(bc_vec_free(&p->conds);)
3645 IF_BC(bc_vec_free(&p->ops);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003646 bc_lex_free(&p->l);
3647}
3648
Denys Vlasenkofa210792018-12-19 19:35:40 +01003649static void bc_parse_create(BcParse *p, size_t fidx)
Gavin Howard01055ba2018-11-03 11:00:21 -06003650{
3651 memset(p, 0, sizeof(BcParse));
3652
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003653 bc_lex_init(&p->l);
Denys Vlasenko503faf92018-12-20 16:24:18 +01003654 IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);)
3655 IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);)
3656 IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003657
Denys Vlasenkofa210792018-12-19 19:35:40 +01003658 p->fidx = fidx;
3659 p->func = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003660}
3661
Denys Vlasenko04715442018-12-21 00:10:26 +01003662static void bc_program_add_fn(void)
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003663{
Denys Vlasenko04715442018-12-21 00:10:26 +01003664 //size_t idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003665 BcFunc f;
3666 bc_func_init(&f);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01003667 //idx =
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003668 bc_vec_push(&G.prog.fns, &f);
Denys Vlasenko04715442018-12-21 00:10:26 +01003669 //return idx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003670}
3671
3672#if ENABLE_BC
3673
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003674// Note: takes ownership of 'name' (must be malloced)
3675static size_t bc_program_addFunc(char *name)
3676{
3677 size_t idx;
3678 BcId entry, *entry_ptr;
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003679 int inserted;
3680
3681 entry.name = name;
3682 entry.idx = G.prog.fns.len;
3683
3684 inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx);
3685 if (!inserted) free(name);
3686
3687 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
3688 idx = entry_ptr->idx;
3689
3690 if (!inserted) {
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003691 // There is already a function with this name.
3692 // It'll be redefined now, clear old definition.
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003693 BcFunc *func = bc_program_func(entry_ptr->idx);
Denys Vlasenkoeaa3b002018-12-19 20:05:50 +01003694 bc_func_free(func);
3695 bc_func_init(func);
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003696 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01003697 bc_program_add_fn();
Denys Vlasenko408b7d42018-12-19 19:43:03 +01003698 }
3699
3700 return idx;
3701}
3702
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003703#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3704#define BC_PARSE_LEAF(p, rparen) \
3705 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3706 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3707
3708// We can calculate the conversion between tokens and exprs by subtracting the
3709// position of the first operator in the lex enum and adding the position of the
3710// first in the expr enum. Note: This only works for binary operators.
Denys Vlasenko0154d782018-12-14 23:32:51 +01003711#define BC_TOKEN_2_INST(t) ((char) ((t) - BC_LEX_NEG + BC_INST_NEG))
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003712
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003713static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags);
3714
3715static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags)
3716{
3717 BcStatus s;
3718
3719 s = bc_parse_expr_empty_ok(p, flags);
3720 if (s == BC_STATUS_PARSE_EMPTY_EXP)
3721 RETURN_STATUS(bc_error("empty expression"));
3722 RETURN_STATUS(s);
3723}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003724#define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003725
3726static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed);
Denys Vlasenkoec603182018-12-17 10:34:02 +01003727#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01003728
3729static BC_STATUS zbc_parse_stmt(BcParse *p)
3730{
3731 RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false));
3732}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003733#define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003734
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003735static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003736{
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003737 // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines.
3738 // Same for "else", "while()", "for()".
3739 BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l);
3740 if (s) RETURN_STATUS(s);
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003741 if (p->l.t.t == BC_LEX_NLINE)
3742 RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X));
Denys Vlasenko94f72a32018-12-17 00:07:48 +01003743
3744 RETURN_STATUS(zbc_parse_stmt(p));
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003745}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003746#define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko2e8be022018-12-16 20:41:32 +01003747
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003748static void bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3749 size_t *nexprs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003750{
Denys Vlasenko65437582018-12-05 19:37:19 +01003751 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3752 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003753
3754 while (p->ops.len > start) {
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003755 BcLexType t = BC_PARSE_TOP_OP(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06003756 if (t == BC_LEX_LPAREN) break;
3757
Denys Vlasenko65437582018-12-05 19:37:19 +01003758 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003759 if (l >= r && (l != r || !left)) break;
3760
Denys Vlasenko0154d782018-12-14 23:32:51 +01003761 bc_parse_push(p, BC_TOKEN_2_INST(t));
Gavin Howard01055ba2018-11-03 11:00:21 -06003762 bc_vec_pop(&p->ops);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01003763 *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06003764 }
3765
3766 bc_vec_push(&p->ops, &type);
Gavin Howard01055ba2018-11-03 11:00:21 -06003767}
3768
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003769static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003770{
3771 BcLexType top;
3772
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003773 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003774 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003775 top = BC_PARSE_TOP_OP(p);
3776
3777 while (top != BC_LEX_LPAREN) {
Denys Vlasenko0154d782018-12-14 23:32:51 +01003778 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06003779
3780 bc_vec_pop(&p->ops);
3781 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3782
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003783 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003784 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003785 top = BC_PARSE_TOP_OP(p);
3786 }
3787
3788 bc_vec_pop(&p->ops);
3789
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003790 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003791}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003792#define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003793
Denys Vlasenko26819db2018-12-12 16:08:46 +01003794static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003795{
3796 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003797 size_t nparams;
3798
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01003799 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003800 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3801
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003802 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003803 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003804
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003805 nparams = 0;
3806 if (p->l.t.t != BC_LEX_RPAREN) {
3807 for (;;) {
3808 s = zbc_parse_expr(p, flags);
3809 if (s) RETURN_STATUS(s);
3810 nparams++;
3811 if (p->l.t.t != BC_LEX_COMMA) {
3812 if (p->l.t.t == BC_LEX_RPAREN)
3813 break;
3814 RETURN_STATUS(bc_error_bad_token());
3815 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003816 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003817 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003818 }
3819 }
3820
Gavin Howard01055ba2018-11-03 11:00:21 -06003821 bc_parse_push(p, BC_INST_CALL);
3822 bc_parse_pushIndex(p, nparams);
3823
Denys Vlasenko26819db2018-12-12 16:08:46 +01003824 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003825}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003826#define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003827
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01003828// Note: takes ownership of 'name' (must be malloced)
Denys Vlasenko26819db2018-12-12 16:08:46 +01003829static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003830{
3831 BcStatus s;
3832 BcId entry, *entry_ptr;
3833 size_t idx;
3834
3835 entry.name = name;
3836
Denys Vlasenko26819db2018-12-12 16:08:46 +01003837 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003838 if (s) goto err;
3839
3840 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003841 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003842 goto err;
3843 }
3844
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003845 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003846
3847 if (idx == BC_VEC_INVALID_IDX) {
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003848 // No such function exists, create an empty one
Denys Vlasenko684d4412018-12-19 14:57:23 +01003849 bc_program_addFunc(name);
Denys Vlasenko5aa54832018-12-19 13:55:53 +01003850 idx = bc_map_find_exact(&G.prog.fn_map, &entry);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003851 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003852 free(name);
3853
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003854 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003855 bc_parse_pushIndex(p, entry_ptr->idx);
3856
Denys Vlasenko26819db2018-12-12 16:08:46 +01003857 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003858 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003859 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003860 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003861}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003862#define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003863
Denys Vlasenko26819db2018-12-12 16:08:46 +01003864static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003865{
3866 BcStatus s;
3867 char *name;
3868
3869 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003870 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003871 if (s) goto err;
3872
3873 if (p->l.t.t == BC_LEX_LBRACKET) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003874 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003875 if (s) goto err;
3876
3877 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003878 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003879 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003880 goto err;
3881 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003882 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003883 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003884 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003886 s = zbc_parse_expr(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003887 if (s) goto err;
3888 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003889 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003890 if (s) goto err;
3891 bc_parse_push(p, *type);
3892 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003893 free(name);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003894 } else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003895 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003896 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 goto err;
3898 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003899 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003900 s = zbc_parse_call(p, name, flags);
3901 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003902 *type = BC_INST_VAR;
3903 bc_parse_push(p, BC_INST_VAR);
3904 bc_parse_pushName(p, name);
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01003905 free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06003906 }
3907
Denys Vlasenko26819db2018-12-12 16:08:46 +01003908 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003909 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06003910 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003911 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003912}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003913#define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003914
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003915static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003916{
3917 BcStatus s;
3918
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003919 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003920 if (s) RETURN_STATUS(s);
3921 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003922
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003923 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003924 if (s) RETURN_STATUS(s);
3925 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003926
3927 bc_parse_push(p, BC_INST_READ);
3928
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003929 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003930}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003931#define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003932
Denys Vlasenko26819db2018-12-12 16:08:46 +01003933static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003934 BcInst *prev)
3935{
3936 BcStatus s;
3937
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003938 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003939 if (s) RETURN_STATUS(s);
3940 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003941
3942 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3943
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003944 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003945 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003946
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003947 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003948 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003949
Denys Vlasenko26819db2018-12-12 16:08:46 +01003950 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003951
3952 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3953 bc_parse_push(p, *prev);
3954
Denys Vlasenko26819db2018-12-12 16:08:46 +01003955 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003956}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003957#define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003958
Denys Vlasenko26819db2018-12-12 16:08:46 +01003959static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003960{
3961 BcStatus s;
3962
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003963 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003964 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003965
3966 if (p->l.t.t != BC_LEX_LPAREN) {
3967 *type = BC_INST_SCALE;
3968 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003969 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003970 }
3971
3972 *type = BC_INST_SCALE_FUNC;
3973 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3974
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003975 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003976 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003977
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01003978 s = zbc_parse_expr(p, flags);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003979 if (s) RETURN_STATUS(s);
3980 if (p->l.t.t != BC_LEX_RPAREN)
3981 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003982 bc_parse_push(p, BC_INST_SCALE_FUNC);
3983
Denys Vlasenko26819db2018-12-12 16:08:46 +01003984 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003985}
Denys Vlasenkoec603182018-12-17 10:34:02 +01003986#define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06003987
Denys Vlasenko26819db2018-12-12 16:08:46 +01003988static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003989 size_t *nexprs, uint8_t flags)
3990{
3991 BcStatus s;
3992 BcLexType type;
3993 char inst;
3994 BcInst etype = *prev;
3995
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01003996 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM
3997 || etype == BC_INST_SCALE || etype == BC_INST_LAST
3998 || etype == BC_INST_IBASE || etype == BC_INST_OBASE
3999 ) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004000 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
4001 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004002 s = zbc_lex_next(&p->l);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004003 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004004 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
4005 *paren_expr = true;
4006
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004007 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004008 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004009 type = p->l.t.t;
4010
4011 // Because we parse the next part of the expression
4012 // right here, we need to increment this.
4013 *nexprs = *nexprs + 1;
4014
4015 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004016 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004017 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004018 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004019 case BC_LEX_KEY_IBASE:
4020 case BC_LEX_KEY_LAST:
4021 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004022 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004023 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004024 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004025 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004026 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004027 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004028 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004029 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004030 else
4031 bc_parse_push(p, BC_INST_SCALE);
4032 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004033 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004034 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004035 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004036 }
4037
4038 if (!s) bc_parse_push(p, inst);
4039 }
4040
Denys Vlasenko26819db2018-12-12 16:08:46 +01004041 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004042}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004043#define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004044
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004045static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06004046 bool rparen, size_t *nexprs)
4047{
4048 BcStatus s;
4049 BcLexType type;
4050 BcInst etype = *prev;
4051
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004052 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004053 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004054
4055 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4056 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4057 BC_LEX_OP_MINUS :
4058 BC_LEX_NEG;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004059 *prev = BC_TOKEN_2_INST(type);
Gavin Howard01055ba2018-11-03 11:00:21 -06004060
4061 // We can just push onto the op stack because this is the largest
4062 // precedence operator that gets pushed. Inc/dec does not.
4063 if (type != BC_LEX_OP_MINUS)
4064 bc_vec_push(&p->ops, &type);
4065 else
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004066 bc_parse_operator(p, type, ops_bgn, nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004067
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004068 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004069}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004070#define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004071
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004072static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004073{
4074 BcStatus s;
4075 BcLexType type;
Gavin Howard01055ba2018-11-03 11:00:21 -06004076
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004077 for (;;) {
4078 s = zbc_lex_next(&p->l);
4079 if (s) RETURN_STATUS(s);
4080 type = p->l.t.t;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004081 if (type == BC_LEX_STR) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004082 s = zbc_parse_pushSTR(p);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004083 } else {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004084 s = zbc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06004085 }
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004086 if (s) RETURN_STATUS(s);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004087 bc_parse_push(p, BC_INST_PRINT_POP);
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004088 if (p->l.t.t != BC_LEX_COMMA)
4089 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004090 }
4091
Denys Vlasenko5d18f6b2018-12-16 21:08:30 +01004092 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004093}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004094#define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004095
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004096static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004097{
4098 BcStatus s;
4099 BcLexType t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004100
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004101 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004102 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004103 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004104
4105 t = p->l.t.t;
Gavin Howard01055ba2018-11-03 11:00:21 -06004106 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4107 bc_parse_push(p, BC_INST_RET0);
4108 else {
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004109 bool paren = (t == BC_LEX_LPAREN);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004110 s = bc_parse_expr_empty_ok(p, 0);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004111 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004112 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004113 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004114 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004115 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004116
4117 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004118 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004119 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s));
Gavin Howard01055ba2018-11-03 11:00:21 -06004120 }
4121
4122 bc_parse_push(p, BC_INST_RET);
4123 }
4124
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004125 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004126 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004127}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004128#define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004129
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004130static void rewrite_label_to_current(BcParse *p, size_t idx)
4131{
4132 size_t *label = bc_vec_item(&p->func->labels, idx);
4133 *label = p->func->code.len;
4134}
4135
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004136static BC_STATUS zbc_parse_if(BcParse *p)
4137{
4138 BcStatus s;
Denys Vlasenko15850832018-12-16 21:40:54 +01004139 size_t ip_idx;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004140
4141 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
4142 s = zbc_lex_next(&p->l);
4143 if (s) RETURN_STATUS(s);
4144 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
4145
4146 s = zbc_lex_next(&p->l);
4147 if (s) RETURN_STATUS(s);
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004148 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004149 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004150 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004151
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004152 // Encode "if zero, jump to ..."
4153 // Pushed value (destination of the jump) is uninitialized,
4154 // will be rewritten to be address of "end of if()" or of "else".
4155 ip_idx = bc_vec_push(&p->func->labels, &ip_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004156 bc_parse_pushJUMP_ZERO(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004157
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004158 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004159 if (s) RETURN_STATUS(s);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004160
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004161 dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
4162 if (p->l.t.t == BC_LEX_KEY_ELSE) {
Denys Vlasenko15850832018-12-16 21:40:54 +01004163 size_t ip2_idx;
Denys Vlasenko74156332018-12-16 21:21:27 +01004164
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004165 // Encode "after then_stmt, jump to end of if()"
4166 ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx);
4167 dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004168 bc_parse_pushJUMP(p, ip2_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004169
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004170 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 +01004171 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004172
Denys Vlasenko15850832018-12-16 21:40:54 +01004173 ip_idx = ip2_idx;
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004174
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004175 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else);
Denys Vlasenkoa50576a2018-12-16 19:21:57 +01004176 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004177 }
4178
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004179 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 +01004180 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004181
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004182 dbg_lex_done("%s:%d done", __func__, __LINE__);
4183 RETURN_STATUS(s);
4184}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004185#define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004186
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004187static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004188{
4189 BcStatus s;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004190 size_t cond_idx;
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004191 size_t ip_idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06004192
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004193 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004194 if (s) RETURN_STATUS(s);
4195 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004196 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004197 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004198
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004199 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004200 ip_idx = cond_idx + 1;
Denys Vlasenkode24e9d2018-12-16 23:02:22 +01004201 bc_vec_push(&p->conds, &cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004202
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004203 bc_vec_push(&p->exits, &ip_idx);
4204 bc_vec_push(&p->func->labels, &ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004205
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004206 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004207 if (s) RETURN_STATUS(s);
4208 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko202dd192018-12-16 17:30:35 +01004209
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004210 bc_parse_pushJUMP_ZERO(p, ip_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004211
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004212 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004213 if (s) RETURN_STATUS(s);
4214
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004215 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004216 bc_parse_pushJUMP(p, cond_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004217
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004218 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004219 rewrite_label_to_current(p, ip_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004220
4221 bc_vec_pop(&p->exits);
4222 bc_vec_pop(&p->conds);
4223
4224 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004225}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004226#define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004227
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004228static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004229{
4230 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004231 size_t cond_idx, exit_idx, body_idx, update_idx;
4232
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004233 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004234 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004235 if (s) RETURN_STATUS(s);
4236 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004237 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004238 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004239
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004240 if (p->l.t.t != BC_LEX_SCOLON) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004241 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004242 bc_parse_push(p, BC_INST_POP);
4243 if (s) RETURN_STATUS(s);
4244 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004245 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004246 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4247 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004248
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004249 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004250 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004251 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004252
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004253 cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06004254 update_idx = cond_idx + 1;
4255 body_idx = update_idx + 1;
4256 exit_idx = body_idx + 1;
4257
Gavin Howard01055ba2018-11-03 11:00:21 -06004258 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004259 s = zbc_parse_expr(p, BC_PARSE_REL);
Denys Vlasenko52caa002018-12-21 00:35:22 +01004260 else {
Denys Vlasenko447dc022018-12-21 00:39:02 +01004261 // Set this for the next call to bc_parse_pushNUM().
Denys Vlasenko52caa002018-12-21 00:35:22 +01004262 // This is safe to set because the current token is a semicolon,
4263 // which has no string requirement.
4264 bc_vec_string(&p->l.t.v, 1, "1");
4265 bc_parse_pushNUM(p);
Denys Vlasenko00646792018-12-05 18:12:27 +01004266 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Denys Vlasenko52caa002018-12-21 00:35:22 +01004267 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004268 if (s) RETURN_STATUS(s);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004269
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004270 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004271
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004272 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004273 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004274
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004275 bc_parse_pushJUMP_ZERO(p, exit_idx);
4276 bc_parse_pushJUMP(p, body_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004277
Gavin Howard01055ba2018-11-03 11:00:21 -06004278 bc_vec_push(&p->conds, &update_idx);
4279 bc_vec_push(&p->func->labels, &p->func->code.len);
4280
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004281 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004282 s = zbc_parse_expr(p, 0);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004283 if (s) RETURN_STATUS(s);
Denys Vlasenko6ed7fb02018-12-21 22:16:17 +01004284 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
4285 bc_parse_push(p, BC_INST_POP);
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004286 } else {
Denys Vlasenko00646792018-12-05 18:12:27 +01004287 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01004288 IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);)
4289 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004290
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004291 bc_parse_pushJUMP(p, cond_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004292 bc_vec_push(&p->func->labels, &p->func->code.len);
4293
Denys Vlasenko5ebd2a62018-12-16 23:35:04 +01004294 bc_vec_push(&p->exits, &exit_idx);
4295 bc_vec_push(&p->func->labels, &exit_idx);
Denys Vlasenko202dd192018-12-16 17:30:35 +01004296
Denys Vlasenko59d4ce92018-12-17 10:42:31 +01004297 s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004298 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004299
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004300 dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004301 bc_parse_pushJUMP(p, update_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004302
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004303 dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004304 rewrite_label_to_current(p, exit_idx);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004305
4306 bc_vec_pop(&p->exits);
4307 bc_vec_pop(&p->conds);
Gavin Howard01055ba2018-11-03 11:00:21 -06004308
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004309 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004310}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004311#define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004312
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004313static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004314{
4315 BcStatus s;
4316 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06004317
Gavin Howard01055ba2018-11-03 11:00:21 -06004318 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004319 if (p->exits.len == 0) // none of the enclosing blocks is a loop
4320 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko266aa002018-12-16 23:24:25 +01004321 i = *(size_t*)bc_vec_top(&p->exits);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004322 } else {
Denys Vlasenko266aa002018-12-16 23:24:25 +01004323 i = *(size_t*)bc_vec_top(&p->conds);
Denys Vlasenko8e7686e2018-12-16 23:18:28 +01004324 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004325
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004326 bc_parse_pushJUMP(p, i);
Gavin Howard01055ba2018-11-03 11:00:21 -06004327
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004328 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004329 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004330
4331 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004332 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004333
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004334 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004335}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004336#define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004337
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004338static BC_STATUS zbc_parse_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004339{
4340 BcStatus s;
4341 bool var, comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004342 char *name;
4343
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004344 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004345 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004346 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004347 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004348 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004349
4350 name = xstrdup(p->l.t.v.v);
Denys Vlasenko684d4412018-12-19 14:57:23 +01004351 p->fidx = bc_program_addFunc(name);
4352 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004353
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004354 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004355 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004356 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004357 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004358 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004359 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004360
4361 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004362 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004363 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004364
4365 ++p->func->nparams;
4366
4367 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004368 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004369 if (s) goto err;
4370
4371 var = p->l.t.t != BC_LEX_LBRACKET;
4372
4373 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004374 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004375 if (s) goto err;
4376
4377 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004378 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004379 goto err;
4380 }
4381
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004382 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004383 if (s) goto err;
4384 }
4385
4386 comma = p->l.t.t == BC_LEX_COMMA;
4387 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004388 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004389 if (s) goto err;
4390 }
4391
Denys Vlasenko29301232018-12-11 15:29:32 +01004392 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004393 if (s) goto err;
4394 }
4395
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004396 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004397
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004398 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004399 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004400
4401 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004402 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004403
Denys Vlasenko202dd192018-12-16 17:30:35 +01004404 // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body
4405 s = zbc_lex_skip_if_at_NLINE(&p->l);
4406 if (s) RETURN_STATUS(s);
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004407 //GNU bc requires a {} block even if function body has single stmt, enforce this?
4408 if (p->l.t.t != BC_LEX_LBRACE)
4409 RETURN_STATUS(bc_error("function { body } expected"));
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004410
4411 p->in_funcdef++; // to determine whether "return" stmt is allowed, and such
Denys Vlasenko94f72a32018-12-17 00:07:48 +01004412 s = zbc_parse_stmt_possibly_auto(p, true);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004413 p->in_funcdef--;
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004414 if (s) RETURN_STATUS(s);
4415
4416 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko65e10462018-12-19 15:13:14 +01004417
4418 // Subsequent code generation is into main program
4419 p->fidx = BC_PROG_MAIN;
4420 p->func = bc_program_func_BC_PROG_MAIN();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004421
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004422 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004423 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004424 err:
Denys Vlasenkof4f10722018-12-18 02:23:53 +01004425 dbg_lex_done("%s:%d done (error)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004426 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004427 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004428}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004429#define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004430
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004431static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004432{
4433 BcStatus s;
4434 bool comma, var, one;
4435 char *name;
4436
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004437 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004438 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004439 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004440
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004441 comma = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004442 one = p->l.t.t == BC_LEX_NAME;
4443
4444 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004445 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004446 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004447 if (s) goto err;
4448
4449 var = p->l.t.t != BC_LEX_LBRACKET;
4450 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004451 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004452 if (s) goto err;
4453
4454 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004455 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004456 goto err;
4457 }
4458
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004459 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004460 if (s) goto err;
4461 }
4462
4463 comma = p->l.t.t == BC_LEX_COMMA;
4464 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004465 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004466 if (s) goto err;
4467 }
4468
Denys Vlasenko29301232018-12-11 15:29:32 +01004469 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004470 if (s) goto err;
4471 }
4472
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004473 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4474 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004475
4476 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004477 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004478
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004479 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004480 RETURN_STATUS(zbc_lex_next(&p->l));
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004481 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06004482 free(name);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004483 dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004484 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004485}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004486#define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004487
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004488#undef zbc_parse_stmt_possibly_auto
4489static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed)
Gavin Howard01055ba2018-11-03 11:00:21 -06004490{
4491 BcStatus s = BC_STATUS_SUCCESS;
4492
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004493 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004494
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004495 if (p->l.t.t == BC_LEX_NLINE) {
4496 dbg_lex_done("%s:%d done (seen BC_LEX_NLINE)", __func__, __LINE__);
4497 RETURN_STATUS(zbc_lex_next(&p->l));
4498 }
4499 if (p->l.t.t == BC_LEX_SCOLON) {
4500 dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__);
4501 RETURN_STATUS(zbc_lex_next(&p->l));
4502 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004503
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004504 if (p->l.t.t == BC_LEX_LBRACE) {
4505 dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed);
4506 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004507 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004508 if (s) RETURN_STATUS(s);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004509 } while (p->l.t.t == BC_LEX_NLINE);
4510 if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) {
4511 dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__);
4512 s = zbc_parse_auto(p);
4513 if (s) RETURN_STATUS(s);
4514 }
4515 while (p->l.t.t != BC_LEX_RBRACE) {
4516 dbg_lex("%s:%d block parsing loop", __func__, __LINE__);
4517 s = zbc_parse_stmt(p);
4518 if (s) RETURN_STATUS(s);
4519 }
4520 s = zbc_lex_next(&p->l);
4521 dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__);
4522 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004523 }
4524
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004525 dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004526 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004527 case BC_LEX_OP_INC:
4528 case BC_LEX_OP_DEC:
4529 case BC_LEX_OP_MINUS:
4530 case BC_LEX_OP_BOOL_NOT:
4531 case BC_LEX_LPAREN:
4532 case BC_LEX_NAME:
4533 case BC_LEX_NUMBER:
4534 case BC_LEX_KEY_IBASE:
4535 case BC_LEX_KEY_LAST:
4536 case BC_LEX_KEY_LENGTH:
4537 case BC_LEX_KEY_OBASE:
4538 case BC_LEX_KEY_READ:
4539 case BC_LEX_KEY_SCALE:
4540 case BC_LEX_KEY_SQRT:
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004541 s = zbc_parse_expr(p, BC_PARSE_PRINT);
Gavin Howard01055ba2018-11-03 11:00:21 -06004542 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004543 case BC_LEX_STR:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004544 s = zbc_parse_pushSTR(p);
Denys Vlasenko44dbe672018-12-19 19:10:40 +01004545 bc_parse_push(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004546 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004547 case BC_LEX_KEY_BREAK:
4548 case BC_LEX_KEY_CONTINUE:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004549 s = zbc_parse_break_or_continue(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004550 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004551 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004552 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004553 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004554 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004555 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004556 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004557 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004558 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004559 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004560 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004561 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004562 // "limits" is a compile-time command,
4563 // the output is produced at _parse time_.
Denys Vlasenko64074a12018-12-07 15:50:14 +01004564 printf(
4565 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4566 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4567 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4568 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4569 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4570 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4571 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4572 "Number of vars = "BC_MAX_VARS_STR "\n"
4573 );
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004574 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004577 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004578 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004579 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004580 // "quit" is a compile-time command. For example,
4581 // "if (0 == 1) quit" terminates when parsing the statement,
4582 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004583 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004584 case BC_LEX_KEY_RETURN:
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004585 if (!p->in_funcdef)
4586 RETURN_STATUS(bc_error("'return' not in a function"));
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004587 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004588 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004589 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004590 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004591 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004592 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004593 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004594 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004595 }
4596
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004597 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004598 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004599}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004600#define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004601
Denys Vlasenko99b37622018-12-15 20:06:59 +01004602static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004603{
4604 BcStatus s;
4605
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004606 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004607 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01004608 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004609 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004610 dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__);
Denys Vlasenkoe9519e42018-12-16 17:06:07 +01004611 s = zbc_parse_funcdef(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004612 } else {
4613 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 +01004614 s = zbc_parse_stmt(p);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004615 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004616
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01004617 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004618 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004619}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004620#define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004621
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004622// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko4b72aeb2018-12-17 16:54:37 +01004623static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004624{
4625 BcStatus s = BC_STATUS_SUCCESS;
4626 BcInst prev = BC_INST_PRINT;
4627 BcLexType top, t = p->l.t.t;
4628 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004629 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004630 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4631
Denys Vlasenko17df8822018-12-14 23:00:24 +01004632 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004633 paren_first = p->l.t.t == BC_LEX_LPAREN;
4634 nparens = nrelops = 0;
4635 paren_expr = rprn = done = get_token = assign = false;
4636 bin_last = true;
4637
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004638 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004639 dbg_lex("%s:%d t:%d", __func__, __LINE__, t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004640 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004641 case BC_LEX_OP_INC:
4642 case BC_LEX_OP_DEC:
Denys Vlasenko26819db2018-12-12 16:08:46 +01004643 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004644 rprn = get_token = bin_last = false;
4645 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004646 case BC_LEX_OP_MINUS:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004647 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004648 rprn = get_token = false;
4649 bin_last = prev == BC_INST_MINUS;
4650 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004651 case BC_LEX_OP_ASSIGN_POWER:
4652 case BC_LEX_OP_ASSIGN_MULTIPLY:
4653 case BC_LEX_OP_ASSIGN_DIVIDE:
4654 case BC_LEX_OP_ASSIGN_MODULUS:
4655 case BC_LEX_OP_ASSIGN_PLUS:
4656 case BC_LEX_OP_ASSIGN_MINUS:
4657 case BC_LEX_OP_ASSIGN:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004658 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM
4659 && prev != BC_INST_SCALE && prev != BC_INST_IBASE
4660 && prev != BC_INST_OBASE && prev != BC_INST_LAST
4661 ) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004662 s = bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004663 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004664 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01004665 ); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06004666 break;
4667 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004668 // Fallthrough.
4669 case BC_LEX_OP_POWER:
4670 case BC_LEX_OP_MULTIPLY:
4671 case BC_LEX_OP_DIVIDE:
4672 case BC_LEX_OP_MODULUS:
4673 case BC_LEX_OP_PLUS:
4674 case BC_LEX_OP_REL_EQ:
4675 case BC_LEX_OP_REL_LE:
4676 case BC_LEX_OP_REL_GE:
4677 case BC_LEX_OP_REL_NE:
4678 case BC_LEX_OP_REL_LT:
4679 case BC_LEX_OP_REL_GT:
4680 case BC_LEX_OP_BOOL_NOT:
4681 case BC_LEX_OP_BOOL_OR:
4682 case BC_LEX_OP_BOOL_AND:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004683 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4684 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4685 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004686 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004687 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004688 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
Denys Vlasenko0154d782018-12-14 23:32:51 +01004689 prev = BC_TOKEN_2_INST(t);
Denys Vlasenko7b1df3d2018-12-14 23:12:48 +01004690 bc_parse_operator(p, t, ops_bgn, &nexprs);
4691 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004692 rprn = get_token = false;
4693 bin_last = t != BC_LEX_OP_BOOL_NOT;
Gavin Howard01055ba2018-11-03 11:00:21 -06004694 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004695 case BC_LEX_LPAREN:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004696 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004697 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004698 ++nparens;
4699 paren_expr = rprn = bin_last = false;
4700 get_token = true;
4701 bc_vec_push(&p->ops, &t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004702 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004703 case BC_LEX_RPAREN:
Gavin Howard01055ba2018-11-03 11:00:21 -06004704 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004705 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004706 if (nparens == 0) {
4707 s = BC_STATUS_SUCCESS;
4708 done = true;
4709 get_token = false;
4710 break;
4711 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004712 if (!paren_expr) {
Denys Vlasenko17df8822018-12-14 23:00:24 +01004713 dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004714 return BC_STATUS_PARSE_EMPTY_EXP;
Denys Vlasenko17df8822018-12-14 23:00:24 +01004715 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004716 --nparens;
4717 paren_expr = rprn = true;
4718 get_token = bin_last = false;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004719 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004720 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004721 case BC_LEX_NAME:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004722 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004723 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004724 paren_expr = true;
4725 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004726 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004727 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004728 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004729 case BC_LEX_NUMBER:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004730 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004731 return bc_error_bad_expression();
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004732 bc_parse_pushNUM(p);
Denys Vlasenko9dc5d082018-12-16 18:43:51 +01004733 nexprs++;
4734 prev = BC_INST_NUM;
Gavin Howard01055ba2018-11-03 11:00:21 -06004735 paren_expr = get_token = true;
4736 rprn = bin_last = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004737 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004738 case BC_LEX_KEY_IBASE:
4739 case BC_LEX_KEY_LAST:
4740 case BC_LEX_KEY_OBASE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004741 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004742 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004743 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4744 bc_parse_push(p, (char) prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004745 paren_expr = get_token = true;
4746 rprn = bin_last = false;
4747 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004748 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004749 case BC_LEX_KEY_LENGTH:
4750 case BC_LEX_KEY_SQRT:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004751 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004752 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004753 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004754 paren_expr = true;
4755 rprn = get_token = bin_last = false;
4756 ++nexprs;
Gavin Howard01055ba2018-11-03 11:00:21 -06004757 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004758 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06004759 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004760 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004761 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004762 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004763 paren_expr = true;
4764 rprn = get_token = bin_last = false;
4765 ++nexprs;
4766 prev = BC_INST_READ;
Gavin Howard01055ba2018-11-03 11:00:21 -06004767 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004768 case BC_LEX_KEY_SCALE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004769 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004770 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004771 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004772 paren_expr = true;
4773 rprn = get_token = bin_last = false;
4774 ++nexprs;
4775 prev = BC_INST_SCALE;
Gavin Howard01055ba2018-11-03 11:00:21 -06004776 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004777 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004778 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004779 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004780 }
4781
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004782 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004783 }
4784
4785 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004786 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004787
4788 while (p->ops.len > ops_bgn) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004789 top = BC_PARSE_TOP_OP(p);
4790 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4791
4792 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004793 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004794
Denys Vlasenko0154d782018-12-14 23:32:51 +01004795 bc_parse_push(p, BC_TOKEN_2_INST(top));
Gavin Howard01055ba2018-11-03 11:00:21 -06004796
4797 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4798 bc_vec_pop(&p->ops);
4799 }
4800
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004801 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004802 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004803
Gavin Howard01055ba2018-11-03 11:00:21 -06004804 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004805 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004806 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01004807 } else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004808 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenkoec603182018-12-17 10:34:02 +01004809 IF_ERROR_RETURN_POSSIBLE(if (s) return s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004810 }
4811
4812 if (flags & BC_PARSE_PRINT) {
4813 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4814 bc_parse_push(p, BC_INST_POP);
4815 }
4816
Denys Vlasenko17df8822018-12-14 23:00:24 +01004817 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004818 return s;
4819}
4820
Gavin Howard01055ba2018-11-03 11:00:21 -06004821#endif // ENABLE_BC
4822
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004823#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004824
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004825static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004826{
4827 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004828
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004829 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004830 if (s) RETURN_STATUS(s);
4831 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004832
Denys Vlasenkoe6c40c42018-12-16 20:32:58 +01004833 bc_parse_pushName(p, p->l.t.v.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06004834
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004835 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004836}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004837#define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004838
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004839static void dc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004840{
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004841 char *str;
Denys Vlasenko684d4412018-12-19 14:57:23 +01004842 size_t len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004843
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004844 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004845
4846 str = xstrdup(p->l.t.v.v);
4847 bc_parse_push(p, BC_INST_STR);
4848 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004849 bc_vec_push(&G.prog.strs, &str);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004850
4851 // Explanation needed here
4852 bc_program_add_fn();
Denys Vlasenko684d4412018-12-19 14:57:23 +01004853 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06004854
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004855 dbg_lex_done("%s:%d done", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004856}
4857
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004858static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004859{
4860 BcStatus s;
4861
4862 bc_parse_push(p, inst);
4863 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004864 s = zdc_parse_register(p);
4865 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004866 }
4867
4868 if (store) {
4869 bc_parse_push(p, BC_INST_SWAP);
4870 bc_parse_push(p, BC_INST_ASSIGN);
4871 bc_parse_push(p, BC_INST_POP);
4872 }
4873
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004874 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004875}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004876#define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004877
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004878static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06004879{
4880 BcStatus s;
4881
4882 bc_parse_push(p, inst);
4883 bc_parse_push(p, BC_INST_EXEC_COND);
4884
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004885 s = zdc_parse_register(p);
4886 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004887
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004888 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004889 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004890
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004891 // Note that 'else' part can not be on the next line:
4892 // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2"
4893 // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error
Gavin Howard01055ba2018-11-03 11:00:21 -06004894 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004895 s = zdc_parse_register(p);
4896 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004897 s = zbc_lex_next(&p->l);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004898 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004899 bc_parse_push(p, BC_PARSE_STREND);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004900 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004901
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004902 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004903}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004904#define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004905
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004906static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06004907{
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004908 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004909 uint8_t inst;
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004910 bool assign, get_token;
Gavin Howard01055ba2018-11-03 11:00:21 -06004911
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004912 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004913 s = BC_STATUS_SUCCESS;
4914 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06004915 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004916 case BC_LEX_OP_REL_EQ:
4917 case BC_LEX_OP_REL_LE:
4918 case BC_LEX_OP_REL_GE:
4919 case BC_LEX_OP_REL_NE:
4920 case BC_LEX_OP_REL_LT:
4921 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004922 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004923 get_token = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004924 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004925 case BC_LEX_SCOLON:
4926 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004927 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06004928 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004929 case BC_LEX_STR:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004930 dbg_lex("%s:%d LEX_STR", __func__, __LINE__);
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004931 dc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004932 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004933 case BC_LEX_NEG:
4934 case BC_LEX_NUMBER:
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004935 dbg_lex("%s:%d LEX_NEG/NUMBER", __func__, __LINE__);
Gavin Howard01055ba2018-11-03 11:00:21 -06004936 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004937 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004938 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004939 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004940 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004941 }
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01004942 bc_parse_pushNUM(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004943 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
Gavin Howard01055ba2018-11-03 11:00:21 -06004944 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004945 case BC_LEX_KEY_READ:
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01004946 bc_parse_push(p, BC_INST_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06004947 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004948 case BC_LEX_OP_ASSIGN:
4949 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06004950 assign = t == BC_LEX_OP_ASSIGN;
4951 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004952 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06004953 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004954 case BC_LEX_LOAD:
4955 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06004956 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004957 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004958 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004959 case BC_LEX_STORE_IBASE:
4960 case BC_LEX_STORE_SCALE:
4961 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004962 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004963 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004964 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004965 default:
Denys Vlasenko5daa1a02018-12-22 16:40:38 +01004966 dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__);
4967 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004968 }
4969
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004970 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004971
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01004972 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004973 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004974}
Denys Vlasenkoec603182018-12-17 10:34:02 +01004975#define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06004976
Denys Vlasenko39287e02018-12-22 02:23:08 +01004977static BC_STATUS zdc_parse_expr(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004978{
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004979 BcInst inst;
4980 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004981
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004982 inst = dc_parse_insts[p->l.t.t];
4983 if (inst != BC_INST_INVALID) {
4984 bc_parse_push(p, inst);
4985 s = zbc_lex_next(&p->l);
4986 } else {
4987 s = zdc_parse_token(p, p->l.t.t);
4988 }
4989 RETURN_STATUS(s);
4990}
4991#define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS)
4992
4993static BC_STATUS zdc_parse_exprs_until_eof(BcParse *p)
4994{
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01004995 dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01004996 while (p->l.t.t != BC_LEX_EOF) {
4997 BcStatus s = zdc_parse_expr(p);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01004998 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004999 }
5000
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01005001 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkoa199cc92018-12-18 14:11:35 +01005002 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005003}
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005004#define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005005
Gavin Howard01055ba2018-11-03 11:00:21 -06005006#endif // ENABLE_DC
5007
Denys Vlasenkodf515392018-12-02 19:27:48 +01005008static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005009{
Gavin Howard01055ba2018-11-03 11:00:21 -06005010 BcId e, *ptr;
5011 BcVec *v, *map;
5012 size_t i;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005013 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005014
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005015 v = var ? &G.prog.vars : &G.prog.arrs;
5016 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005017
5018 e.name = id;
5019 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005020 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005021
5022 if (new) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005023 BcVec v2;
5024 bc_array_init(&v2, var);
5025 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005026 }
5027
5028 ptr = bc_vec_item(map, i);
5029 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005030 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005031}
5032
Denys Vlasenko8287b1c2018-12-21 22:43:53 +01005033// 'num' need not be initialized on entry
Denys Vlasenko29301232018-12-11 15:29:32 +01005034static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005035{
Gavin Howard01055ba2018-11-03 11:00:21 -06005036 switch (r->t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005037 case BC_RESULT_STR:
5038 case BC_RESULT_TEMP:
5039 case BC_RESULT_IBASE:
5040 case BC_RESULT_SCALE:
5041 case BC_RESULT_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005042 *num = &r->d.n;
5043 break;
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005044 case BC_RESULT_CONSTANT: {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005045 BcStatus s;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005046 char *str;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005047 unsigned base_t;
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01005048 size_t len;
5049
5050 str = *bc_program_const(r->d.id.idx);
5051 len = strlen(str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005052
5053 bc_num_init(&r->d.n, len);
5054
5055 hex = hex && len == 1;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005056 base_t = hex ? 16 : G.prog.ib_t;
Denys Vlasenkoe3d3d202018-12-19 13:19:44 +01005057 s = zbc_num_parse(&r->d.n, str, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005058 if (s) {
5059 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005060 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005061 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005062 *num = &r->d.n;
5063 r->t = BC_RESULT_TEMP;
Gavin Howard01055ba2018-11-03 11:00:21 -06005064 break;
5065 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005066 case BC_RESULT_VAR:
5067 case BC_RESULT_ARRAY:
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005068 case BC_RESULT_ARRAY_ELEM: {
Gavin Howard01055ba2018-11-03 11:00:21 -06005069 BcVec *v;
5070
Denys Vlasenkodf515392018-12-02 19:27:48 +01005071 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005072
5073 if (r->t == BC_RESULT_ARRAY_ELEM) {
5074 v = bc_vec_top(v);
5075 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5076 *num = bc_vec_item(v, r->d.id.idx);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005077 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06005078 *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005079 break;
5080 }
Denys Vlasenko503faf92018-12-20 16:24:18 +01005081#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06005082 case BC_RESULT_LAST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005083 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005084 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005085 case BC_RESULT_ONE:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005086 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005087 break;
Denys Vlasenko503faf92018-12-20 16:24:18 +01005088#endif
5089 default:
5090 // Testing the theory that dc does not reach LAST/ONE
5091 bb_error_msg_and_die("BUG:%d", r->t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005092 }
5093
Denys Vlasenko29301232018-12-11 15:29:32 +01005094 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005095}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005096#define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005097
Denys Vlasenko29301232018-12-11 15:29:32 +01005098static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005099 BcResult **r, BcNum **rn, bool assign)
5100{
5101 BcStatus s;
5102 bool hex;
5103 BcResultType lt, rt;
5104
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005105 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005106 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005107
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005108 *r = bc_vec_item_rev(&G.prog.results, 0);
5109 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005110
5111 lt = (*l)->t;
5112 rt = (*r)->t;
5113 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5114
Denys Vlasenko29301232018-12-11 15:29:32 +01005115 s = zbc_program_num(*l, ln, false);
5116 if (s) RETURN_STATUS(s);
5117 s = zbc_program_num(*r, rn, hex);
5118 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005119
5120 // We run this again under these conditions in case any vector has been
5121 // reallocated out from under the BcNums or arrays we had.
5122 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005123 s = zbc_program_num(*l, ln, false);
5124 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005125 }
5126
5127 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005128 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005129 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005130 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005131
Denys Vlasenko29301232018-12-11 15:29:32 +01005132 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005133}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005134#define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005135
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005136static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005137{
5138 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005139 bc_vec_pop(&G.prog.results);
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005140 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005141}
5142
Denys Vlasenko29301232018-12-11 15:29:32 +01005143static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005144{
5145 BcStatus s;
5146
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005147 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005148 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005149 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005150
Denys Vlasenko29301232018-12-11 15:29:32 +01005151 s = zbc_program_num(*r, n, false);
5152 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005153
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005154 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005155 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005156
Denys Vlasenko29301232018-12-11 15:29:32 +01005157 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005158}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005159#define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005160
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005161static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005162{
5163 r->t = t;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005164 bc_result_pop_and_push(r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005165}
5166
Denys Vlasenko259137d2018-12-11 19:42:05 +01005167static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005168{
5169 BcStatus s;
5170 BcResult *opd1, *opd2, res;
5171 BcNum *n1, *n2 = NULL;
5172
Denys Vlasenko29301232018-12-11 15:29:32 +01005173 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005174 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005175 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005176
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005177 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005178 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 -06005179 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005180 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005181
Denys Vlasenko259137d2018-12-11 19:42:05 +01005182 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005183 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005184 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005185 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005186}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005187#define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005188
Denys Vlasenko26819db2018-12-12 16:08:46 +01005189static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005190{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005191 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005192 BcStatus s;
5193 BcParse parse;
5194 BcVec buf;
5195 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005196 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005197
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005198 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005199 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06005200
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005201 sv_file = G.prog.file;
5202 G.prog.file = NULL;
5203
Denys Vlasenko82ea67f2018-12-13 19:23:45 +01005204 bc_char_vec_init(&buf);
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01005205 bc_read_line(&buf, stdin);
Gavin Howard01055ba2018-11-03 11:00:21 -06005206
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005207 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005208 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005209
Denys Vlasenkof86e9602018-12-14 17:01:56 +01005210 s = zbc_parse_text_init(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005211 if (s) goto exec_err;
Denys Vlasenko514967d2018-12-22 03:38:52 +01005212 if (IS_BC) {
5213 IF_BC(s = zbc_parse_expr(&parse, 0));
5214 } else {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01005215 IF_DC(s = zdc_parse_exprs_until_eof(&parse));
Denys Vlasenko514967d2018-12-22 03:38:52 +01005216 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005217 if (s) goto exec_err;
5218
5219 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005220 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005221 goto exec_err;
5222 }
5223
5224 ip.func = BC_PROG_READ;
Denys Vlasenko24e41942018-12-21 23:01:26 +01005225 ip.inst_idx = 0;
5226 IF_BC(ip.results_len_before_call = G.prog.results.len;)
Gavin Howard01055ba2018-11-03 11:00:21 -06005227
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01005228 bc_parse_push(&parse, BC_INST_RET);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005229 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005230 exec_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005231 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005232 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005233 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005234 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005235}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005236#define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005237
5238static size_t bc_program_index(char *code, size_t *bgn)
5239{
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005240 unsigned char *bytes = (void*)(code + *bgn);
5241 unsigned amt;
5242 unsigned i;
5243 size_t res;
Gavin Howard01055ba2018-11-03 11:00:21 -06005244
Denys Vlasenko3f940c92018-12-18 15:49:42 +01005245 amt = *bytes++;
5246 *bgn += amt + 1;
5247
5248 amt *= 8;
5249 res = 0;
5250 for (i = 0; i < amt; i += 8)
5251 res |= (size_t)(*bytes++) << i;
Gavin Howard01055ba2018-11-03 11:00:21 -06005252
5253 return res;
5254}
5255
5256static char *bc_program_name(char *code, size_t *bgn)
5257{
5258 size_t i;
Denys Vlasenko694d2982018-12-18 19:17:11 +01005259 char *s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005260
Denys Vlasenko694d2982018-12-18 19:17:11 +01005261 code += *bgn;
5262 s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005263 i = 0;
5264 for (;;) {
Denys Vlasenko694d2982018-12-18 19:17:11 +01005265 char c = *code++;
5266 if (c == BC_PARSE_STREND)
Denys Vlasenko6b0fbd12018-12-18 12:55:40 +01005267 break;
5268 s[i++] = c;
5269 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005270 s[i] = '\0';
Denys Vlasenko694d2982018-12-18 19:17:11 +01005271 *bgn += i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06005272
5273 return s;
5274}
5275
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005276static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005277{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005278#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005279 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005280 // Example: echo '[]ap' | dc
5281 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005282 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005283 return;
5284 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005285#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005286 while (*str) {
5287 int c = *str++;
5288 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005289 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005290 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005291 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005292 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005293 case 'a':
5294 bb_putchar('\a');
5295 break;
5296 case 'b':
5297 bb_putchar('\b');
5298 break;
5299 case '\\':
5300 case 'e':
5301 bb_putchar('\\');
5302 break;
5303 case 'f':
5304 bb_putchar('\f');
5305 break;
5306 case 'n':
5307 bb_putchar('\n');
5308 G.prog.nchars = SIZE_MAX;
5309 break;
5310 case 'r':
5311 bb_putchar('\r');
5312 break;
5313 case 'q':
5314 bb_putchar('"');
5315 break;
5316 case 't':
5317 bb_putchar('\t');
5318 break;
5319 default:
5320 // Just print the backslash and following character.
5321 bb_putchar('\\');
5322 ++G.prog.nchars;
5323 bb_putchar(c);
5324 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005325 }
5326 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005327 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005328 }
5329}
5330
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005331static void bc_num_printNewline(void)
5332{
5333 if (G.prog.nchars == G.prog.len - 1) {
5334 bb_putchar('\\');
5335 bb_putchar('\n');
5336 G.prog.nchars = 0;
5337 }
5338}
5339
5340#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005341static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005342{
5343 (void) radix;
5344 bb_putchar((char) num);
5345 G.prog.nchars += width;
5346}
5347#endif
5348
5349static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5350{
5351 size_t exp, pow;
5352
5353 bc_num_printNewline();
5354 bb_putchar(radix ? '.' : ' ');
5355 ++G.prog.nchars;
5356
5357 bc_num_printNewline();
5358 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5359 continue;
5360
5361 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5362 size_t dig;
5363 bc_num_printNewline();
5364 dig = num / pow;
5365 num -= dig * pow;
5366 bb_putchar(((char) dig) + '0');
5367 }
5368}
5369
5370static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5371{
5372 if (radix) {
5373 bc_num_printNewline();
5374 bb_putchar('.');
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005375 G.prog.nchars++;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005376 }
5377
5378 bc_num_printNewline();
5379 bb_putchar(bb_hexdigits_upcase[num]);
5380 G.prog.nchars += width;
5381}
5382
5383static void bc_num_printDecimal(BcNum *n)
5384{
5385 size_t i, rdx = n->rdx - 1;
5386
Denys Vlasenko30a8e0c2018-12-18 19:20:04 +01005387 if (n->neg) {
5388 bb_putchar('-');
5389 G.prog.nchars++;
5390 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005391
5392 for (i = n->len - 1; i < n->len; --i)
5393 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5394}
5395
Denys Vlasenkod3401432018-12-18 17:00:35 +01005396static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005397{
5398 BcStatus s;
5399 BcVec stack;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005400 BcNum base;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005401 BcDig base_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005402 BcNum intp, fracp, digit, frac_len;
5403 unsigned long dig, *ptr;
5404 size_t i;
5405 bool radix;
5406
5407 if (n->len == 0) {
5408 print(0, width, false);
5409 RETURN_STATUS(BC_STATUS_SUCCESS);
5410 }
5411
5412 bc_vec_init(&stack, sizeof(long), NULL);
5413 bc_num_init(&intp, n->len);
5414 bc_num_init(&fracp, n->rdx);
5415 bc_num_init(&digit, width);
5416 bc_num_init(&frac_len, BC_NUM_INT(n));
5417 bc_num_copy(&intp, n);
5418 bc_num_one(&frac_len);
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01005419 base.cap = ARRAY_SIZE(base_digs);
5420 base.num = base_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01005421 bc_num_ulong2num(&base, base_t);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005422
5423 bc_num_truncate(&intp, intp.rdx);
5424 s = zbc_num_sub(n, &intp, &fracp, 0);
5425 if (s) goto err;
5426
5427 while (intp.len != 0) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005428 s = zbc_num_divmod(&intp, &base, &intp, &digit, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005429 if (s) goto err;
5430 s = zbc_num_ulong(&digit, &dig);
5431 if (s) goto err;
5432 bc_vec_push(&stack, &dig);
5433 }
5434
5435 for (i = 0; i < stack.len; ++i) {
5436 ptr = bc_vec_item_rev(&stack, i);
5437 print(*ptr, width, false);
5438 }
5439
5440 if (!n->rdx) goto err;
5441
5442 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Denys Vlasenkod3401432018-12-18 17:00:35 +01005443 s = zbc_num_mul(&fracp, &base, &fracp, n->rdx);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005444 if (s) goto err;
5445 s = zbc_num_ulong(&fracp, &dig);
5446 if (s) goto err;
5447 bc_num_ulong2num(&intp, dig);
5448 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5449 if (s) goto err;
5450 print(dig, width, radix);
Denys Vlasenkod3401432018-12-18 17:00:35 +01005451 s = zbc_num_mul(&frac_len, &base, &frac_len, 0);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005452 if (s) goto err;
5453 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005454 err:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005455 bc_num_free(&frac_len);
5456 bc_num_free(&digit);
5457 bc_num_free(&fracp);
5458 bc_num_free(&intp);
5459 bc_vec_free(&stack);
5460 RETURN_STATUS(s);
5461}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005462#define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005463
5464static BC_STATUS zbc_num_printBase(BcNum *n)
5465{
5466 BcStatus s;
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005467 size_t width;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005468 BcNumDigitOp print;
5469 bool neg = n->neg;
5470
5471 if (neg) {
5472 bb_putchar('-');
5473 G.prog.nchars++;
5474 }
5475
5476 n->neg = false;
5477
5478 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5479 width = 1;
5480 print = bc_num_printHex;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005481 } else {
Denys Vlasenkod4258dd2018-12-18 13:22:23 +01005482 unsigned i = G.prog.ob_t - 1;
5483 width = 0;
5484 for (;;) {
5485 width++;
5486 i /= 10;
5487 if (i == 0)
5488 break;
5489 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005490 print = bc_num_printDigits;
5491 }
5492
Denys Vlasenkod3401432018-12-18 17:00:35 +01005493 s = zbc_num_printNum(n, G.prog.ob_t, width, print);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005494 n->neg = neg;
5495
5496 RETURN_STATUS(s);
5497}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005498#define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005499
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005500static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5501{
5502 BcStatus s = BC_STATUS_SUCCESS;
5503
5504 bc_num_printNewline();
5505
5506 if (n->len == 0) {
5507 bb_putchar('0');
5508 ++G.prog.nchars;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005509 } else if (G.prog.ob_t == 10)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005510 bc_num_printDecimal(n);
5511 else
5512 s = zbc_num_printBase(n);
5513
5514 if (newline) {
5515 bb_putchar('\n');
5516 G.prog.nchars = 0;
5517 }
5518
5519 RETURN_STATUS(s);
5520}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005521#define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005522
Denys Vlasenko29301232018-12-11 15:29:32 +01005523static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005524{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005525 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005526 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005527 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005528 bool pop = inst != BC_INST_PRINT;
5529
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005530 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
Denys Vlasenko29301232018-12-11 15:29:32 +01005531 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005532
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005533 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01005534 s = zbc_program_num(r, &num, false);
5535 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005536
5537 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005538 s = zbc_num_print(num, !pop);
Denys Vlasenko503faf92018-12-20 16:24:18 +01005539#if ENABLE_BC
5540 if (!s && IS_BC) bc_num_copy(&G.prog.last, num);
5541#endif
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005542 } else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005543 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005544
5545 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005546 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005547
5548 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005549 for (;;) {
5550 char c = *str++;
5551 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005552 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005553 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005554 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005555 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005556 } else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005557 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005558 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005559 }
5560 }
5561
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005562 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005563
Denys Vlasenko29301232018-12-11 15:29:32 +01005564 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005565}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005566#define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005567
Denys Vlasenko29301232018-12-11 15:29:32 +01005568static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005569{
5570 BcStatus s;
5571 BcResult res, *ptr;
5572 BcNum *num = NULL;
5573
Denys Vlasenko29301232018-12-11 15:29:32 +01005574 s = zbc_program_prep(&ptr, &num);
5575 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005576
5577 bc_num_init(&res.d.n, num->len);
5578 bc_num_copy(&res.d.n, num);
5579 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5580
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005581 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005582
Denys Vlasenko29301232018-12-11 15:29:32 +01005583 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005584}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005585#define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005586
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005587static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005588{
5589 BcStatus s;
5590 BcResult *opd1, *opd2, res;
5591 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005592 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005593
Denys Vlasenko29301232018-12-11 15:29:32 +01005594 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005595 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005596
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005597 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005598
5599 if (inst == BC_INST_BOOL_AND)
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 if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005602 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005603 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005604 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005605 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005606 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005607 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005608 break;
5609 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005610 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005611 break;
5612 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005613 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005614 break;
5615 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005616 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005617 break;
5618 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005619 cond = (cond > 0);
5620 break;
5621 default: // = case BC_INST_REL_NE:
5622 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005623 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005624 }
5625 }
5626
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005627 if (cond) bc_num_one(&res.d.n);
5628 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005629
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005630 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005631
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005632 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005633}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005634#define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005635
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005636#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01005637static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push)
Gavin Howard01055ba2018-11-03 11:00:21 -06005638{
5639 BcNum n2;
5640 BcResult res;
5641
5642 memset(&n2, 0, sizeof(BcNum));
5643 n2.rdx = res.d.id.idx = r->d.id.idx;
5644 res.t = BC_RESULT_STR;
5645
5646 if (!push) {
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005647 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005648 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005649 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005650 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005651 }
5652
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005653 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005654 bc_vec_push(v, &n2);
5655
Denys Vlasenko29301232018-12-11 15:29:32 +01005656 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005657}
Denys Vlasenkofa210792018-12-19 19:35:40 +01005658#define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005659#endif // ENABLE_DC
5660
Denys Vlasenko29301232018-12-11 15:29:32 +01005661static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005662{
5663 BcStatus s;
5664 BcResult *ptr, r;
5665 BcVec *v;
5666 BcNum *n;
5667
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005668 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01005669 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005670
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005671 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005672 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005673 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005674 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005675
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005676#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005677 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005678 RETURN_STATUS(bc_error_variable_is_wrong_type());
5679 if (ptr->t == BC_RESULT_STR)
Denys Vlasenkofa210792018-12-19 19:35:40 +01005680 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005681#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005682
Denys Vlasenko29301232018-12-11 15:29:32 +01005683 s = zbc_program_num(ptr, &n, false);
5684 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005685
5686 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005687 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005688
5689 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005690 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005691 bc_num_copy(&r.d.n, n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005692 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005693 bc_array_init(&r.d.v, true);
5694 bc_array_copy(&r.d.v, (BcVec *) n);
5695 }
5696
5697 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005698 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005699
Denys Vlasenko29301232018-12-11 15:29:32 +01005700 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005701}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005702#define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005703
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005704static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005705{
5706 BcStatus s;
5707 BcResult *left, *right, res;
5708 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005709 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5710
Denys Vlasenko29301232018-12-11 15:29:32 +01005711 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005712 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005713
5714 ib = left->t == BC_RESULT_IBASE;
5715 sc = left->t == BC_RESULT_SCALE;
5716
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005717#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005718 if (right->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005719 BcVec *v;
5720
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005721 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005722 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005723 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005724
Denys Vlasenkofa210792018-12-19 19:35:40 +01005725 RETURN_STATUS(zdc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005726 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005727#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005728
5729 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005730 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005731 " left side must be variable"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005732 " or array element"
Denys Vlasenko0154d782018-12-14 23:32:51 +01005733 )); // note: shared string
Gavin Howard01055ba2018-11-03 11:00:21 -06005734
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005735#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005736 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005737 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005738
5739 if (assign)
5740 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005741 else {
5742 s = BC_STATUS_SUCCESS;
Denys Vlasenkoec603182018-12-17 10:34:02 +01005743 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 +01005744 }
5745 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005746#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005747 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005748#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005749
5750 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005751 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005752 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5753 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5754 NULL, //can't happen //BC_RESULT_LAST
5755 NULL, //can't happen //BC_RESULT_CONSTANT
5756 NULL, //can't happen //BC_RESULT_ONE
5757 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005758 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005759 size_t *ptr;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005760 size_t max;
5761 unsigned long val;
Gavin Howard01055ba2018-11-03 11:00:21 -06005762
Denys Vlasenko29301232018-12-11 15:29:32 +01005763 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005764 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005765 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005766 if (sc) {
5767 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005768 ptr = &G.prog.scale;
Denys Vlasenkof6e3f852018-12-17 21:05:09 +01005769 } else {
5770 if (val < 2)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005771 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005772 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005773 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005774 }
5775
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005776 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005777 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005778
5779 *ptr = (size_t) val;
5780 s = BC_STATUS_SUCCESS;
5781 }
5782
5783 bc_num_init(&res.d.n, l->len);
5784 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005785 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005786
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005787 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005788}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005789#define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005790
Denys Vlasenko416ce762018-12-02 20:57:17 +01005791#if !ENABLE_DC
5792#define bc_program_pushVar(code, bgn, pop, copy) \
5793 bc_program_pushVar(code, bgn)
5794// for bc, 'pop' and 'copy' are always false
5795#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005796static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005797 bool pop, bool copy)
5798{
Gavin Howard01055ba2018-11-03 11:00:21 -06005799 BcResult r;
5800 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005801
5802 r.t = BC_RESULT_VAR;
5803 r.d.id.name = name;
5804
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005805#if ENABLE_DC
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005806 if (pop || copy) {
Denys Vlasenko416ce762018-12-02 20:57:17 +01005807 BcVec *v = bc_program_search(name, true);
5808 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005809
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005810 free(name);
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01005811 if (!STACK_HAS_MORE_THAN(v, 1 - copy)) {
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005812 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005813 }
Denys Vlasenko7b30bc02018-12-18 17:14:34 +01005814
5815 if (!BC_PROG_STR(num)) {
5816 r.t = BC_RESULT_TEMP;
5817 bc_num_init_DEF_SIZE(&r.d.n);
5818 bc_num_copy(&r.d.n, num);
5819 } else {
5820 r.t = BC_RESULT_STR;
5821 r.d.id.idx = num->rdx;
5822 }
5823
5824 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005825 }
5826#endif // ENABLE_DC
5827
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005828 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005829
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005830 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005831}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005832#define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005833
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005834static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005835{
5836 BcStatus s = BC_STATUS_SUCCESS;
5837 BcResult r;
5838 BcNum *num;
5839
5840 r.d.id.name = bc_program_name(code, bgn);
5841
5842 if (inst == BC_INST_ARRAY) {
5843 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005844 bc_vec_push(&G.prog.results, &r);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005845 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005846 BcResult *operand;
5847 unsigned long temp;
5848
Denys Vlasenko29301232018-12-11 15:29:32 +01005849 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005850 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01005851 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06005852 if (s) goto err;
5853
5854 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005855 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005856 goto err;
5857 }
5858
5859 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005860 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005861 }
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005862 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06005863 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01005864 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005865}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005866#define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005867
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005868#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01005869static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005870{
5871 BcStatus s;
5872 BcResult *ptr, res, copy;
5873 BcNum *num = NULL;
5874 char inst2 = inst;
5875
Denys Vlasenko29301232018-12-11 15:29:32 +01005876 s = zbc_program_prep(&ptr, &num);
5877 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005878
5879 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5880 copy.t = BC_RESULT_TEMP;
5881 bc_num_init(&copy.d.n, num->len);
5882 bc_num_copy(&copy.d.n, num);
5883 }
5884
5885 res.t = BC_RESULT_ONE;
5886 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
5887 BC_INST_ASSIGN_PLUS :
5888 BC_INST_ASSIGN_MINUS;
5889
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005890 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005891 s = zbc_program_assign(inst);
5892 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005893
5894 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01005895 bc_result_pop_and_push(&copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005896 }
5897
Denys Vlasenko29301232018-12-11 15:29:32 +01005898 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005899}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005900#define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005901
Denys Vlasenko29301232018-12-11 15:29:32 +01005902static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005903{
Gavin Howard01055ba2018-11-03 11:00:21 -06005904 BcInstPtr ip;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005905 size_t i, nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005906 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005907 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005908 BcResult *arg;
5909
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005910 nparams = bc_program_index(code, idx);
Denys Vlasenko24e41942018-12-21 23:01:26 +01005911 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005912 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005913 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005914
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005915 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005916 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005917 }
5918 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005919 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01005920 }
Denys Vlasenko24e41942018-12-21 23:01:26 +01005921 ip.results_len_before_call = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06005922
5923 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005924 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005925
5926 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005927 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005928
5929 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01005930 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005931
Denys Vlasenko29301232018-12-11 15:29:32 +01005932 s = zbc_program_copyToVar(a->name, a->idx);
5933 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005934 }
5935
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005936 a = bc_vec_item(&func->autos, i);
5937 for (; i < func->autos.len; i++, a++) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01005938 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06005939
Denys Vlasenkodf515392018-12-02 19:27:48 +01005940 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005941 if (a->idx) {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005942 BcNum n2;
5943 bc_num_init_DEF_SIZE(&n2);
5944 bc_vec_push(v, &n2);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005945 } else {
Denys Vlasenkof36a0ad2018-12-19 17:15:04 +01005946 BcVec v2;
5947 bc_array_init(&v2, true);
5948 bc_vec_push(v, &v2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005949 }
5950 }
5951
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005952 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005953
Denys Vlasenko29301232018-12-11 15:29:32 +01005954 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005955}
Denys Vlasenkoec603182018-12-17 10:34:02 +01005956#define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06005957
Denys Vlasenko29301232018-12-11 15:29:32 +01005958static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005959{
Gavin Howard01055ba2018-11-03 11:00:21 -06005960 BcResult res;
5961 BcFunc *f;
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005962 BcId *a;
Gavin Howard01055ba2018-11-03 11:00:21 -06005963 size_t i;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005964 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06005965
Denys Vlasenko24e41942018-12-21 23:01:26 +01005966 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 +01005967 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005968
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005969 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06005970 res.t = BC_RESULT_TEMP;
5971
5972 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005973 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005974 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005975 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005976
Denys Vlasenko29301232018-12-11 15:29:32 +01005977 s = zbc_program_num(operand, &num, false);
5978 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005979 bc_num_init(&res.d.n, num->len);
5980 bc_num_copy(&res.d.n, num);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01005981 } else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005982 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01005983 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005984 }
5985
5986 // We need to pop arguments as well, so this takes that into account.
Denys Vlasenko87888ce2018-12-19 17:55:23 +01005987 a = (void*)f->autos.v;
5988 for (i = 0; i < f->autos.len; i++, a++) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005989 BcVec *v;
Denys Vlasenkodf515392018-12-02 19:27:48 +01005990 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005991 bc_vec_pop(v);
5992 }
5993
Denys Vlasenko24e41942018-12-21 23:01:26 +01005994 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005995 bc_vec_push(&G.prog.results, &res);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01005996 bc_vec_pop(&G.prog.exestack);
Gavin Howard01055ba2018-11-03 11:00:21 -06005997
Denys Vlasenko29301232018-12-11 15:29:32 +01005998 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005999}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006000#define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006001#endif // ENABLE_BC
6002
6003static unsigned long bc_program_scale(BcNum *n)
6004{
6005 return (unsigned long) n->rdx;
6006}
6007
6008static unsigned long bc_program_len(BcNum *n)
6009{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006010 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006011
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006012 if (n->rdx != len) return len;
6013 for (;;) {
6014 if (len == 0) break;
6015 len--;
6016 if (n->num[len] != 0) break;
6017 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006018 return len;
6019}
6020
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006021static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006022{
6023 BcStatus s;
6024 BcResult *opnd;
6025 BcNum *num = NULL;
6026 BcResult res;
6027 bool len = inst == BC_INST_LENGTH;
6028
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006029 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006030 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006031 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006032
Denys Vlasenko29301232018-12-11 15:29:32 +01006033 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006034 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006035
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006036#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006037 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006038 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006039#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006040
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006041 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006042
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006043 if (inst == BC_INST_SQRT)
6044 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006045#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006046 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006047 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006048 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006049#endif
6050#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006051 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006052 char **str;
6053 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6054
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006055 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006056 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006057 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006058#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006059 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006060 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006061 }
6062
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006063 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006064
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006065 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006066}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006067#define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006068
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006069#if ENABLE_DC
Denys Vlasenkofa210792018-12-19 19:35:40 +01006070static BC_STATUS zdc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006071{
6072 BcStatus s;
6073 BcResult *opd1, *opd2, res, res2;
6074 BcNum *n1, *n2 = NULL;
6075
Denys Vlasenko29301232018-12-11 15:29:32 +01006076 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006077 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006078
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006079 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006080 bc_num_init(&res2.d.n, n2->len);
6081
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006082 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006083 if (s) goto err;
6084
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006085 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006086 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006087 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006088
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006089 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006090 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006091 bc_num_free(&res2.d.n);
6092 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006093 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006094}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006095#define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006096
Denys Vlasenkofa210792018-12-19 19:35:40 +01006097static BC_STATUS zdc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006098{
6099 BcStatus s;
6100 BcResult *r1, *r2, *r3, res;
6101 BcNum *n1, *n2, *n3;
6102
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006103 if (!STACK_HAS_MORE_THAN(&G.prog.results, 2))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006104 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006105 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006106 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006107
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006108 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006109 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006110 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006111 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006112 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006113
6114 // Make sure that the values have their pointers updated, if necessary.
6115 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006116 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006117 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006118 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006119 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006120 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006121 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006122 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006123 }
6124 }
6125
6126 bc_num_init(&res.d.n, n3->len);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006127 s = zdc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006128 if (s) goto err;
6129
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006130 bc_vec_pop(&G.prog.results);
6131 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006132
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006133 RETURN_STATUS(s);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006134 err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006135 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006136 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006137}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006138#define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006139
Denys Vlasenkofa210792018-12-19 19:35:40 +01006140static void dc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006141{
Gavin Howard01055ba2018-11-03 11:00:21 -06006142 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006143 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006144
6145 res.t = BC_RESULT_TEMP;
6146
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006147 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006148 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006149 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006150}
6151
Denys Vlasenkofa210792018-12-19 19:35:40 +01006152static BC_STATUS zdc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006153{
6154 BcStatus s;
6155 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006156 BcNum *num, n;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006157 char **strs;
6158 char *str;
6159 char c;
6160 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006161
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006162 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006163 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006164 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006165
Denys Vlasenko29301232018-12-11 15:29:32 +01006166 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006167 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006168
6169 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa9f59db2018-12-22 21:52:30 +01006170 unsigned long val;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006171 BcNum strmb;
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006172 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
Denys Vlasenkod3401432018-12-18 17:00:35 +01006173
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006174 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006175 bc_num_copy(&n, num);
6176 bc_num_truncate(&n, n.rdx);
6177
Denys Vlasenkoe8e7bda2018-12-21 22:36:04 +01006178 strmb.cap = ARRAY_SIZE(strmb_digs);
6179 strmb.num = strmb_digs;
Denys Vlasenkod3401432018-12-18 17:00:35 +01006180 bc_num_ulong2num(&strmb, 0x100);
6181 s = zbc_num_mod(&n, &strmb, &n, 0);
Denys Vlasenkod3401432018-12-18 17:00:35 +01006182
Gavin Howard01055ba2018-11-03 11:00:21 -06006183 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006184 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006185 if (s) goto num_err;
6186
6187 c = (char) val;
6188
6189 bc_num_free(&n);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006190 } else {
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006191 char *sp;
Gavin Howard01055ba2018-11-03 11:00:21 -06006192 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006193 sp = *bc_program_str(idx);
6194 c = sp[0];
Gavin Howard01055ba2018-11-03 11:00:21 -06006195 }
6196
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006197 strs = (void*)G.prog.strs.v;
6198 for (idx = 0; idx < G.prog.strs.len; idx++) {
6199 if (strs[idx][0] == c && strs[idx][1] == '\0') {
6200 goto dup;
6201 }
6202 }
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006203 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006204 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006205 //str[1] = '\0'; - already is
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006206 bc_vec_push(&G.prog.strs, &str);
6207 dup:
Gavin Howard01055ba2018-11-03 11:00:21 -06006208 res.t = BC_RESULT_STR;
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01006209 res.d.id.idx = idx;
Denys Vlasenko1dc4de92018-12-21 23:13:48 +01006210 bc_result_pop_and_push(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006211
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006212 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006213 num_err:
Gavin Howard01055ba2018-11-03 11:00:21 -06006214 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006215 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006216}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006217#define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006218
Denys Vlasenkofa210792018-12-19 19:35:40 +01006219static BC_STATUS zdc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006220{
6221 BcStatus s;
6222 BcResult *r;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006223 BcNum *n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006224 size_t idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006225
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006226 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko29301232018-12-11 15:29:32 +01006227 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006228 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006229
Denys Vlasenko29301232018-12-11 15:29:32 +01006230 s = zbc_program_num(r, &n, false);
6231 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006232
Denys Vlasenko57734c92018-12-17 21:14:05 +01006233 if (BC_PROG_NUM(r, n)) {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006234 s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar);
Denys Vlasenko57734c92018-12-17 21:14:05 +01006235 } else {
Denys Vlasenkofa210792018-12-19 19:35:40 +01006236 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06006237 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006238 str = *bc_program_str(idx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006239 fputs(str, stdout);
Gavin Howard01055ba2018-11-03 11:00:21 -06006240 }
6241
Denys Vlasenko29301232018-12-11 15:29:32 +01006242 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006243}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006244#define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006245
Denys Vlasenkofa210792018-12-19 19:35:40 +01006246static BC_STATUS zdc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006247{
6248 BcStatus s;
6249 BcResult *opnd;
6250 BcNum *num = NULL;
6251 unsigned long val;
6252
Denys Vlasenko29301232018-12-11 15:29:32 +01006253 s = zbc_program_prep(&opnd, &num);
6254 if (s) RETURN_STATUS(s);
6255 s = zbc_num_ulong(num, &val);
6256 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006257
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006258 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006259
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006260 if (G.prog.exestack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006261 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006262 if (G.prog.exestack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006263 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006264 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006265
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006266 bc_vec_npop(&G.prog.exestack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006267
Denys Vlasenko29301232018-12-11 15:29:32 +01006268 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006269}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006270#define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006271
Denys Vlasenkofa210792018-12-19 19:35:40 +01006272static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
Gavin Howard01055ba2018-11-03 11:00:21 -06006273{
6274 BcStatus s = BC_STATUS_SUCCESS;
6275 BcResult *r;
Gavin Howard01055ba2018-11-03 11:00:21 -06006276 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06006277 BcInstPtr ip;
6278 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006279
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006280 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006281 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006282
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006283 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006284
6285 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006286 BcNum *n = n; // for compiler
6287 bool exec;
6288 char *name;
6289 char *then_name = bc_program_name(code, bgn);
6290 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006291
6292 if (code[*bgn] == BC_PARSE_STREND)
6293 (*bgn) += 1;
6294 else
6295 else_name = bc_program_name(code, bgn);
6296
6297 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006298 name = then_name;
6299 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006300 exec = true;
6301 name = else_name;
6302 }
6303
6304 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006305 BcVec *v;
6306 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006307 n = bc_vec_top(v);
6308 }
6309
6310 free(then_name);
6311 free(else_name);
6312
6313 if (!exec) goto exit;
6314 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006315 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006316 goto exit;
6317 }
6318
6319 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006320 } else {
6321 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006322 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006323 } else if (r->t == BC_RESULT_VAR) {
6324 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006325 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006326 if (s || !BC_PROG_STR(n)) goto exit;
6327 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006328 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006329 goto exit;
6330 }
6331
6332 fidx = sidx + BC_PROG_REQ_FUNCS;
6333
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006334 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006335
6336 if (f->code.len == 0) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006337 FILE *sv_input_fp;
Denys Vlasenkofa210792018-12-19 19:35:40 +01006338 BcParse prs;
6339 char *str;
6340
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006341 bc_parse_create(&prs, fidx);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006342 str = *bc_program_str(sidx);
6343 s = zbc_parse_text_init(&prs, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006344 if (s) goto err;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006345
6346 sv_input_fp = G.input_fp;
6347 G.input_fp = NULL; // "do not read from input file when <EOL> reached"
6348 s = zdc_parse_exprs_until_eof(&prs);
6349 G.input_fp = sv_input_fp;
6350
Gavin Howard01055ba2018-11-03 11:00:21 -06006351 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06006352 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006353 s = bc_error_bad_expression();
Denys Vlasenkofa210792018-12-19 19:35:40 +01006354 err:
6355 bc_parse_free(&prs);
6356 bc_vec_pop_all(&f->code);
6357 goto exit;
Gavin Howard01055ba2018-11-03 11:00:21 -06006358 }
Denys Vlasenko39287e02018-12-22 02:23:08 +01006359 bc_parse_push(&prs, BC_INST_POP_EXEC);
Gavin Howard01055ba2018-11-03 11:00:21 -06006360 bc_parse_free(&prs);
6361 }
6362
Denys Vlasenko24e41942018-12-21 23:01:26 +01006363 ip.inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006364 ip.func = fidx;
6365
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006366 bc_vec_pop(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006367 bc_vec_push(&G.prog.exestack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006368
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006369 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko7f2d59c2018-12-18 16:24:07 +01006370 exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006371 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006372 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006373}
Denys Vlasenkofa210792018-12-19 19:35:40 +01006374#define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006375#endif // ENABLE_DC
6376
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006377static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006378{
Gavin Howard01055ba2018-11-03 11:00:21 -06006379 BcResult res;
6380 unsigned long val;
6381
6382 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6383 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006384 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006385 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006386 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006387 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006388 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006389
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006390 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006391 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006392 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006393}
6394
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006395static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006396{
Gavin Howard01055ba2018-11-03 11:00:21 -06006397 BcResult r, *ptr;
6398 BcNum *num;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006399 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006400 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006401 char *code = func->code.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006402
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006403 dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d",
Denys Vlasenko24e41942018-12-21 23:01:26 +01006404 ip->func, func->code.len, ip->inst_idx, G.prog.results.len);
6405 while (ip->inst_idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006406 BcStatus s = BC_STATUS_SUCCESS;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006407 char inst = code[ip->inst_idx++];
Gavin Howard01055ba2018-11-03 11:00:21 -06006408
Denys Vlasenko24e41942018-12-21 23:01:26 +01006409 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 -06006410 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006411#if ENABLE_BC
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006412 case BC_INST_JUMP_ZERO: {
6413 bool zero;
Denys Vlasenko99b37622018-12-15 20:06:59 +01006414 dbg_exec("BC_INST_JUMP_ZERO:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006415 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006416 if (s) RETURN_STATUS(s);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006417 zero = (bc_num_cmp(num, &G.prog.zero) == 0);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006418 bc_vec_pop(&G.prog.results);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006419 if (!zero) {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006420 bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006421 break;
6422 }
6423 // else: fall through
6424 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006425 case BC_INST_JUMP: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006426 size_t idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkofd51e0c2018-12-15 15:07:14 +01006427 size_t *addr = bc_vec_item(&func->labels, idx);
Denys Vlasenko99b37622018-12-15 20:06:59 +01006428 dbg_exec("BC_INST_JUMP: to %ld", (long)*addr);
Denys Vlasenko24e41942018-12-21 23:01:26 +01006429 ip->inst_idx = *addr;
Gavin Howard01055ba2018-11-03 11:00:21 -06006430 break;
6431 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006432 case BC_INST_CALL:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006433 dbg_exec("BC_INST_CALL:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006434 s = zbc_program_call(code, &ip->inst_idx);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006435 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006436 case BC_INST_INC_PRE:
6437 case BC_INST_DEC_PRE:
6438 case BC_INST_INC_POST:
6439 case BC_INST_DEC_POST:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006440 dbg_exec("BC_INST_INCDEC:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006441 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006442 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006443 case BC_INST_HALT:
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006444 dbg_exec("BC_INST_HALT:");
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006445 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006446 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006447 case BC_INST_RET:
6448 case BC_INST_RET0:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006449 dbg_exec("BC_INST_RET[0]:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006450 s = zbc_program_return(inst);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006451 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006452 case BC_INST_BOOL_OR:
6453 case BC_INST_BOOL_AND:
6454#endif // ENABLE_BC
6455 case BC_INST_REL_EQ:
6456 case BC_INST_REL_LE:
6457 case BC_INST_REL_GE:
6458 case BC_INST_REL_NE:
6459 case BC_INST_REL_LT:
6460 case BC_INST_REL_GT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006461 dbg_exec("BC_INST_BOOL:");
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006462 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006463 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006464 case BC_INST_READ:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006465 dbg_exec("BC_INST_READ:");
Denys Vlasenko26819db2018-12-12 16:08:46 +01006466 s = zbc_program_read();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006467 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006468 case BC_INST_VAR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006469 dbg_exec("BC_INST_VAR:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006470 s = zbc_program_pushVar(code, &ip->inst_idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006471 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006472 case BC_INST_ARRAY_ELEM:
6473 case BC_INST_ARRAY:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006474 dbg_exec("BC_INST_ARRAY[_ELEM]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006475 s = zbc_program_pushArray(code, &ip->inst_idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006476 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006477 case BC_INST_LAST:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006478//TODO: this can't happen on dc, right?
6479 dbg_exec("BC_INST_LAST:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006480 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006481 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006482 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006483 case BC_INST_IBASE:
6484 case BC_INST_SCALE:
6485 case BC_INST_OBASE:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006486 dbg_exec("BC_INST_internalvar:");
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006487 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006488 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006489 case BC_INST_SCALE_FUNC:
6490 case BC_INST_LENGTH:
6491 case BC_INST_SQRT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006492 dbg_exec("BC_INST_builtin:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006493 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006494 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006495 case BC_INST_NUM:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006496 dbg_exec("BC_INST_NUM:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006497 r.t = BC_RESULT_CONSTANT;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006498 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006499 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006500 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006501 case BC_INST_POP:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006502 dbg_exec("BC_INST_POP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006503 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006504 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006505 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006506 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006507 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006508 case BC_INST_PRINT:
6509 case BC_INST_PRINT_POP:
6510 case BC_INST_PRINT_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006511 dbg_exec("BC_INST_PRINTxyz:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006512 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006513 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006514 case BC_INST_STR:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006515 dbg_exec("BC_INST_STR:");
Gavin Howard01055ba2018-11-03 11:00:21 -06006516 r.t = BC_RESULT_STR;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006517 r.d.id.idx = bc_program_index(code, &ip->inst_idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006518 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006519 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006520 case BC_INST_POWER:
6521 case BC_INST_MULTIPLY:
6522 case BC_INST_DIVIDE:
6523 case BC_INST_MODULUS:
6524 case BC_INST_PLUS:
6525 case BC_INST_MINUS:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006526 dbg_exec("BC_INST_binaryop:");
Denys Vlasenko259137d2018-12-11 19:42:05 +01006527 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006528 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006529 case BC_INST_BOOL_NOT:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006530 dbg_exec("BC_INST_BOOL_NOT:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006531 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006532 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006533 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006534 if (!bc_num_cmp(num, &G.prog.zero))
6535 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006536 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006537 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006538 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006539 case BC_INST_NEG:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006540 dbg_exec("BC_INST_NEG:");
Denys Vlasenko29301232018-12-11 15:29:32 +01006541 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006542 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006543#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006544 case BC_INST_ASSIGN_POWER:
6545 case BC_INST_ASSIGN_MULTIPLY:
6546 case BC_INST_ASSIGN_DIVIDE:
6547 case BC_INST_ASSIGN_MODULUS:
6548 case BC_INST_ASSIGN_PLUS:
6549 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006550#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006551 case BC_INST_ASSIGN:
Denys Vlasenko99b37622018-12-15 20:06:59 +01006552 dbg_exec("BC_INST_ASSIGNxyz:");
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006553 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006554 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006555#if ENABLE_DC
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01006556 case BC_INST_POP_EXEC:
6557 dbg_exec("BC_INST_POP_EXEC:");
6558 bc_vec_pop(&G.prog.exestack);
6559 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006560 case BC_INST_MODEXP:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006561 dbg_exec("BC_INST_MODEXP:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006562 s = zdc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006563 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006564 case BC_INST_DIVMOD:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006565 dbg_exec("BC_INST_DIVMOD:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006566 s = zdc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006567 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006568 case BC_INST_EXECUTE:
6569 case BC_INST_EXEC_COND:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006570 dbg_exec("BC_INST_EXEC[_COND]:");
Denys Vlasenko24e41942018-12-21 23:01:26 +01006571 s = zdc_program_execStr(code, &ip->inst_idx, inst == BC_INST_EXEC_COND);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006572 goto read_updated_ip;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006573 case BC_INST_PRINT_STACK: {
6574 size_t idx;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006575 dbg_exec("BC_INST_PRINT_STACK:");
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006576 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006577 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006578 if (s) break;
6579 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006580 break;
6581 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006582 case BC_INST_CLEAR_STACK:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006583 dbg_exec("BC_INST_CLEAR_STACK:");
Denys Vlasenko7d628012018-12-04 21:46:47 +01006584 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006585 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006586 case BC_INST_STACK_LEN:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006587 dbg_exec("BC_INST_STACK_LEN:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006588 dc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006589 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006590 case BC_INST_DUPLICATE:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006591 dbg_exec("BC_INST_DUPLICATE:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006592 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006593 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006594 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenkofa210792018-12-19 19:35:40 +01006595 dc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006596 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006597 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006598 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006599 BcResult *ptr2;
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006600 dbg_exec("BC_INST_SWAP:");
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006601 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006602 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006603 ptr = bc_vec_item_rev(&G.prog.results, 0);
6604 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006605 memcpy(&r, ptr, sizeof(BcResult));
6606 memcpy(ptr, ptr2, sizeof(BcResult));
6607 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006608 break;
6609 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006610 case BC_INST_ASCIIFY:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006611 dbg_exec("BC_INST_ASCIIFY:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006612 s = zdc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006613 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006614 case BC_INST_PRINT_STREAM:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006615 dbg_exec("BC_INST_STREAM:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006616 s = zdc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006617 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006618 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006619 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006620 bool copy = inst == BC_INST_LOAD;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006621 s = zbc_program_pushVar(code, &ip->inst_idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006622 break;
6623 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006624 case BC_INST_PUSH_TO_VAR: {
Denys Vlasenko24e41942018-12-21 23:01:26 +01006625 char *name = bc_program_name(code, &ip->inst_idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006626 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006627 free(name);
6628 break;
6629 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006630 case BC_INST_QUIT:
Denys Vlasenkodfe1dd22018-12-19 17:09:01 +01006631 dbg_exec("BC_INST_QUIT:");
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006632 if (G.prog.exestack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006633 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006634 bc_vec_npop(&G.prog.exestack, 2);
Denys Vlasenko085b4202018-12-19 14:02:59 +01006635 goto read_updated_ip;
Gavin Howard01055ba2018-11-03 11:00:21 -06006636 case BC_INST_NQUIT:
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006637 dbg_exec("BC_INST_NQUIT:");
Denys Vlasenkofa210792018-12-19 19:35:40 +01006638 s = zdc_program_nquit();
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006639 //goto read_updated_ip; - just fall through to it
Gavin Howard01055ba2018-11-03 11:00:21 -06006640#endif // ENABLE_DC
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01006641 read_updated_ip:
6642 // Instruction stack has changed, read new pointers
6643 ip = bc_vec_top(&G.prog.exestack);
6644 func = bc_program_func(ip->func);
6645 code = func->code.v;
Denys Vlasenko24e41942018-12-21 23:01:26 +01006646 dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006647 }
6648
Denys Vlasenkod38af482018-12-04 19:11:02 +01006649 if (s || G_interrupt) {
6650 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006651 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006652 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006653
Denys Vlasenkoc5774a32018-12-17 01:22:53 +01006654 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06006655 }
6656
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006657 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006658}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006659#define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006660
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006661static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006662{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006663 char *lenv;
6664 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006665
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006666 lenv = getenv(var);
6667 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006668 if (!lenv) return len;
6669
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006670 len = bb_strtou(lenv, NULL, 10) - 1;
6671 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006672 len = BC_NUM_PRINT_WIDTH;
6673
6674 return len;
6675}
6676
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006677static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006678{
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006679 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006680
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006681 dbg_lex_enter("%s:%d entered", __func__, __LINE__);
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006682 s = zbc_parse_text_init(&G.prs, text); // does the first zbc_lex_next()
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006683 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006684
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006685 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006686 BcInstPtr *ip;
6687 BcFunc *f;
6688
Denys Vlasenko39287e02018-12-22 02:23:08 +01006689 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 +01006690 if (IS_BC) {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006691// FIXME: "eating" of stmt delimiters is coded inconsistently
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006692// (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't),
6693// which causes bugs such as "print 1 print 2" erroneously accepted,
6694// or "print 1 else 2" detecting parse error only after executing
6695// "print 1" part.
6696 IF_BC(s = zbc_parse_stmt_or_funcdef(&G.prs));
6697 } else {
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006698 IF_DC(s = zdc_parse_expr(&G.prs));
Denys Vlasenko88fcd5c2018-12-22 06:00:25 +01006699 }
6700 if (s || G_interrupt) {
6701 bc_parse_reset(&G.prs); // includes bc_program_reset()
6702 RETURN_STATUS(BC_STATUS_FAILURE);
6703 }
6704
Denys Vlasenko39287e02018-12-22 02:23:08 +01006705 dbg_lex("%s:%d executing...", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006706 s = zbc_program_exec();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006707 if (s) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01006708 bc_program_reset();
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01006709 break;
6710 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006711
6712 ip = (void*)G.prog.exestack.v;
6713#if SANITY_CHECKS
6714 if (G.prog.exestack.len != 1) // should have only main's IP
6715 bb_error_msg_and_die("BUG:call stack");
6716 if (ip->func != BC_PROG_MAIN)
6717 bb_error_msg_and_die("BUG:not MAIN");
6718#endif
6719 f = bc_program_func_BC_PROG_MAIN();
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006720 // bc discards strings, constants and code after each
6721 // top-level statement in the "main program".
6722 // This prevents "yes 1 | bc" from growing its memory
6723 // without bound. This can be done because data stack
6724 // is empty and thus can't hold any references to
6725 // strings or constants, there is no generated code
6726 // which can hold references (after we discard one
6727 // we just executed). Code of functions can have references,
6728 // but bc stores function strings/constants in per-function
6729 // storage.
6730 if (IS_BC) {
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006731#if SANITY_CHECKS
Denys Vlasenko7c1c9dc2018-12-22 14:18:47 +01006732 if (G.prog.results.len != 0) // should be empty
6733 bb_error_msg_and_die("BUG:data stack");
Denys Vlasenko19eee8e2018-12-21 20:29:34 +01006734#endif
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006735 IF_BC(bc_vec_pop_all(&f->strs);)
6736 IF_BC(bc_vec_pop_all(&f->consts);)
Denys Vlasenkobadf6832018-12-22 18:04:08 +01006737 } else {
6738 // Most of dc parsing assumes all whitespace,
6739 // including '\n', is eaten.
6740 if (G.prs.l.t.t == BC_LEX_NLINE) {
6741 s = zbc_lex_next(&G.prs.l);
6742 if (s) RETURN_STATUS(s);
6743 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006744
6745 if (G.prog.results.len == 0
6746 && G.prog.vars.len == 0
6747 ) {
6748 // If stack is empty and no registers exist (TODO: or they are all empty),
6749 // we can get rid of accumulated strings and constants.
6750 // In this example dc process should not grow
6751 // its memory consumption with time:
6752 // yes 1pc | dc
6753 IF_DC(bc_vec_pop_all(&G.prog.strs);)
6754 IF_DC(bc_vec_pop_all(&G.prog.consts);)
6755 }
6756 // The code is discarded always (below), thus this example
6757 // should also not grow its memory consumption with time,
6758 // even though its data stack is not empty:
6759 // { echo 1; yes dk; } | dc
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01006760 }
Denys Vlasenkoec74a9c2018-12-22 19:23:46 +01006761 // We drop generated and executed code for both bc and dc:
6762 bc_vec_pop_all(&f->code);
6763 ip->inst_idx = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06006764 }
6765
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01006766 dbg_lex_done("%s:%d done", __func__, __LINE__);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006767 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006768}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006769#define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006770
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006771static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename)
Gavin Howard01055ba2018-11-03 11:00:21 -06006772{
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006773 // So far bc/dc have no way to include a file from another file,
6774 // therefore we know G.prog.file == NULL on entry
6775 //const char *sv_file;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006776 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006777
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006778 G.prog.file = filename;
6779 G.input_fp = fp;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006780 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006781
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006782 do {
6783 s = zbc_vm_process("");
6784 // We do not stop looping on errors here if reading stdin.
6785 // Example: start interactive bc and enter "return".
6786 // It should say "'return' not in a function"
6787 // but should not exit.
6788 } while (G.input_fp == stdin);
Denys Vlasenko0fe270e2018-12-13 19:58:58 +01006789 G.prog.file = NULL;
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006790 RETURN_STATUS(s);
6791}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006792#define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01006793
6794static BC_STATUS zbc_vm_file(const char *file)
6795{
6796 BcStatus s;
6797 FILE *fp;
6798
6799 fp = xfopen_for_read(file);
6800 s = zbc_vm_execute_FILE(fp, file);
6801 fclose(fp);
6802
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006803 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006804}
Denys Vlasenkoec603182018-12-17 10:34:02 +01006805#define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06006806
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006807#if ENABLE_BC
Denys Vlasenko57b69182018-12-14 00:12:13 +01006808static void bc_vm_info(void)
6809{
6810 printf("%s "BB_VER"\n"
6811 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
6812 , applet_name);
6813}
6814
6815static void bc_args(char **argv)
6816{
6817 unsigned opts;
6818 int i;
6819
6820 GETOPT_RESET();
6821#if ENABLE_FEATURE_BC_LONG_OPTIONS
6822 opts = option_mask32 |= getopt32long(argv, "wvsqli",
6823 "warn\0" No_argument "w"
6824 "version\0" No_argument "v"
6825 "standard\0" No_argument "s"
6826 "quiet\0" No_argument "q"
6827 "mathlib\0" No_argument "l"
6828 "interactive\0" No_argument "i"
6829 );
6830#else
6831 opts = option_mask32 |= getopt32(argv, "wvsqli");
6832#endif
6833 if (getenv("POSIXLY_CORRECT"))
6834 option_mask32 |= BC_FLAG_S;
6835
6836 if (opts & BC_FLAG_V) {
6837 bc_vm_info();
6838 exit(0);
6839 }
6840
6841 for (i = optind; argv[i]; ++i)
6842 bc_vec_push(&G.files, argv + i);
6843}
6844
6845static void bc_vm_envArgs(void)
6846{
6847 BcVec v;
6848 char *buf;
6849 char *env_args = getenv("BC_ENV_ARGS");
6850
6851 if (!env_args) return;
6852
6853 G.env_args = xstrdup(env_args);
6854 buf = G.env_args;
6855
6856 bc_vec_init(&v, sizeof(char *), NULL);
6857
6858 while (*(buf = skip_whitespace(buf)) != '\0') {
6859 bc_vec_push(&v, &buf);
6860 buf = skip_non_whitespace(buf);
6861 if (!*buf)
6862 break;
6863 *buf++ = '\0';
6864 }
6865
6866 // NULL terminate, and pass argv[] so that first arg is argv[1]
6867 if (sizeof(int) == sizeof(char*)) {
6868 bc_vec_push(&v, &const_int_0);
6869 } else {
6870 static char *const nullptr = NULL;
6871 bc_vec_push(&v, &nullptr);
6872 }
6873 bc_args(((char **)v.v) - 1);
6874
6875 bc_vec_free(&v);
6876}
6877
6878static const char bc_lib[] ALIGN1 = {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006879 "scale=20"
6880"\n" "define e(x){"
6881"\n" "auto b,s,n,r,d,i,p,f,v"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006882////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below
6883//^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc:
6884//e(-.998896): GNU:.36828580434569428695
6885// above code:.36828580434569428696
6886// actual value:.3682858043456942869594...
6887// but for now let's be "GNU compatible"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006888"\n" "b=ibase"
6889"\n" "ibase=A"
6890"\n" "if(x<0){"
6891"\n" "n=1"
6892"\n" "x=-x"
6893"\n" "}"
6894"\n" "s=scale"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006895"\n" "r=6+s+.44*x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006896"\n" "scale=scale(x)+1"
6897"\n" "while(x>1){"
6898"\n" "d+=1"
6899"\n" "x/=2"
6900"\n" "scale+=1"
6901"\n" "}"
6902"\n" "scale=r"
6903"\n" "r=x+1"
6904"\n" "p=x"
6905"\n" "f=v=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006906"\n" "for(i=2;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006907"\n" "p*=x"
6908"\n" "f*=i"
6909"\n" "v=p/f"
6910"\n" "r+=v"
6911"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006912"\n" "while(d--)r*=r"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006913"\n" "scale=s"
6914"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006915"\n" "if(n)return(1/r)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006916"\n" "return(r/1)"
6917"\n" "}"
6918"\n" "define l(x){"
6919"\n" "auto b,s,r,p,a,q,i,v"
6920"\n" "b=ibase"
6921"\n" "ibase=A"
6922"\n" "if(x<=0){"
6923"\n" "r=(1-10^scale)/1"
6924"\n" "ibase=b"
6925"\n" "return(r)"
6926"\n" "}"
6927"\n" "s=scale"
6928"\n" "scale+=6"
6929"\n" "p=2"
6930"\n" "while(x>=2){"
6931"\n" "p*=2"
6932"\n" "x=sqrt(x)"
6933"\n" "}"
Denys Vlasenko146a79d2018-12-16 21:46:11 +01006934"\n" "while(x<=.5){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006935"\n" "p*=2"
6936"\n" "x=sqrt(x)"
6937"\n" "}"
6938"\n" "r=a=(x-1)/(x+1)"
6939"\n" "q=a*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006940"\n" "v=1"
6941"\n" "for(i=3;v;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006942"\n" "a*=q"
6943"\n" "v=a/i"
6944"\n" "r+=v"
6945"\n" "}"
6946"\n" "r*=p"
6947"\n" "scale=s"
6948"\n" "ibase=b"
6949"\n" "return(r/1)"
6950"\n" "}"
6951"\n" "define s(x){"
Denys Vlasenko240d7ee2018-12-14 11:27:09 +01006952"\n" "auto b,s,r,a,q,i"
6953"\n" "if(x<0)return(-s(-x))"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006954"\n" "b=ibase"
6955"\n" "ibase=A"
6956"\n" "s=scale"
6957"\n" "scale=1.1*s+2"
6958"\n" "a=a(1)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006959"\n" "scale=0"
6960"\n" "q=(x/a+2)/4"
Denys Vlasenko203210e2018-12-14 09:53:50 +01006961"\n" "x-=4*q*a"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006962"\n" "if(q%2)x=-x"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006963"\n" "scale=s+2"
6964"\n" "r=a=x"
6965"\n" "q=-x*x"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006966"\n" "for(i=3;a;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006967"\n" "a*=q/(i*(i-1))"
6968"\n" "r+=a"
6969"\n" "}"
6970"\n" "scale=s"
6971"\n" "ibase=b"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006972"\n" "return(r/1)"
6973"\n" "}"
6974"\n" "define c(x){"
6975"\n" "auto b,s"
6976"\n" "b=ibase"
6977"\n" "ibase=A"
6978"\n" "s=scale"
6979"\n" "scale*=1.2"
6980"\n" "x=s(2*a(1)+x)"
6981"\n" "scale=s"
6982"\n" "ibase=b"
6983"\n" "return(x/1)"
6984"\n" "}"
6985"\n" "define a(x){"
6986"\n" "auto b,s,r,n,a,m,t,f,i,u"
6987"\n" "b=ibase"
6988"\n" "ibase=A"
6989"\n" "n=1"
6990"\n" "if(x<0){"
6991"\n" "n=-1"
6992"\n" "x=-x"
6993"\n" "}"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01006994"\n" "if(scale<65){"
6995"\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
6996"\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01006997"\n" "}"
6998"\n" "s=scale"
6999"\n" "if(x>.2){"
7000"\n" "scale+=5"
7001"\n" "a=a(.2)"
7002"\n" "}"
7003"\n" "scale=s+3"
7004"\n" "while(x>.2){"
7005"\n" "m+=1"
7006"\n" "x=(x-.2)/(1+.2*x)"
7007"\n" "}"
7008"\n" "r=u=x"
7009"\n" "f=-x*x"
7010"\n" "t=1"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007011"\n" "for(i=3;t;i+=2){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007012"\n" "u*=f"
7013"\n" "t=u/i"
7014"\n" "r+=t"
7015"\n" "}"
7016"\n" "scale=s"
7017"\n" "ibase=b"
7018"\n" "return((m*a+r)/n)"
7019"\n" "}"
7020"\n" "define j(n,x){"
7021"\n" "auto b,s,o,a,i,v,f"
7022"\n" "b=ibase"
7023"\n" "ibase=A"
7024"\n" "s=scale"
7025"\n" "scale=0"
7026"\n" "n/=1"
7027"\n" "if(n<0){"
7028"\n" "n=-n"
Denys Vlasenko203210e2018-12-14 09:53:50 +01007029"\n" "o=n%2"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007030"\n" "}"
7031"\n" "a=1"
7032"\n" "for(i=2;i<=n;++i)a*=i"
7033"\n" "scale=1.5*s"
7034"\n" "a=(x^n)/2^n/a"
7035"\n" "r=v=1"
7036"\n" "f=-x*x/4"
Denys Vlasenkoc06537d2018-12-14 10:10:37 +01007037"\n" "scale+=length(a)-scale(a)"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007038"\n" "for(i=1;v;++i){"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007039"\n" "v=v*f/i/(n+i)"
7040"\n" "r+=v"
7041"\n" "}"
7042"\n" "scale=s"
7043"\n" "ibase=b"
Denys Vlasenko3ac0c212018-12-14 01:01:01 +01007044"\n" "if(o)a=-a"
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007045"\n" "return(a*r/1)"
7046"\n" "}"
7047};
7048#endif // ENABLE_BC
7049
Denys Vlasenko19f11072018-12-12 22:48:19 +01007050static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007051{
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007052 char **fname;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007053 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007054 size_t i;
7055
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007056#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007057 if (option_mask32 & BC_FLAG_L) {
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007058 // We know that internal library is not buggy,
7059 // thus error checking is normally disabled.
7060# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007061 bc_lex_file(&G.prs.l);
Denys Vlasenko2ea53a42018-12-14 17:51:17 +01007062 s = zbc_vm_process(bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007063 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007064 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007065#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007066
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007067 s = BC_STATUS_SUCCESS;
Denys Vlasenkoea5cad22018-12-19 18:09:31 +01007068 fname = (void*)G.files.v;
7069 for (i = 0; i < G.files.len; i++) {
7070 s = zbc_vm_file(*fname++);
7071 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) {
7072 // Debug config, non-interactive mode:
7073 // return all the way back to main.
7074 // Non-debug builds do not come here
7075 // in non-interactive mode, they exit.
7076 RETURN_STATUS(s);
7077 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007078 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007079
Denys Vlasenko91cde952018-12-10 20:56:08 +01007080 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007081 s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007082
Denys Vlasenko19f11072018-12-12 22:48:19 +01007083 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007084}
Denys Vlasenkoec603182018-12-17 10:34:02 +01007085#define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS)
Gavin Howard01055ba2018-11-03 11:00:21 -06007086
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007087#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007088static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007089{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007090 bc_vec_free(&G.prog.fns);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007091 IF_BC(bc_vec_free(&G.prog.fn_map);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007092 bc_vec_free(&G.prog.vars);
7093 bc_vec_free(&G.prog.var_map);
7094 bc_vec_free(&G.prog.arrs);
7095 bc_vec_free(&G.prog.arr_map);
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007096 IF_DC(bc_vec_free(&G.prog.strs);)
7097 IF_DC(bc_vec_free(&G.prog.consts);)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007098 bc_vec_free(&G.prog.results);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007099 bc_vec_free(&G.prog.exestack);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007100 IF_BC(bc_num_free(&G.prog.last);)
7101 IF_BC(bc_num_free(&G.prog.zero);)
Denys Vlasenko503faf92018-12-20 16:24:18 +01007102 IF_BC(bc_num_free(&G.prog.one);)
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007103 bc_vec_free(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007104}
7105
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007106static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007107{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007108 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007109 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007110 bc_parse_free(&G.prs);
7111 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007112}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007113#endif
7114
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007115static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007116{
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007117 BcInstPtr ip;
7118
Denys Vlasenko82269122018-12-14 16:30:56 +01007119 // memset(&G.prog, 0, sizeof(G.prog)); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007120 memset(&ip, 0, sizeof(BcInstPtr));
7121
Denys Vlasenko82269122018-12-14 16:30:56 +01007122 // G.prog.nchars = G.prog.scale = 0; - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007123 G.prog.ib_t = 10;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007124 G.prog.ob_t = 10;
7125
Denys Vlasenko503faf92018-12-20 16:24:18 +01007126 IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);)
7127 //IF_BC(bc_num_zero(&G.prog.last);) - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007128
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007129 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007130 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007131
Denys Vlasenko503faf92018-12-20 16:24:18 +01007132 IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);)
7133 IF_BC(bc_num_one(&G.prog.one);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007134
7135 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007136 IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007137
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007138 if (IS_BC) {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007139 // Names are chosen simply to be distinct and never match
Denys Vlasenko04715442018-12-21 00:10:26 +01007140 // a valid function name (and be short)
7141 IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007142 IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read()
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007143 } else {
Denys Vlasenko8c1e7232018-12-22 01:34:10 +01007144 // in dc, functions have no names
Denys Vlasenko44a99ca2018-12-20 20:34:09 +01007145 bc_program_add_fn();
7146 bc_program_add_fn();
7147 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007148
7149 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007150 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007151
7152 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007153 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007154
Denys Vlasenko5d57bc42018-12-21 16:22:26 +01007155 IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);)
7156 IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007157 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
Denys Vlasenkob80d7aa2018-12-19 12:35:27 +01007158 bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL);
7159 bc_vec_push(&G.prog.exestack, &ip);
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007160
Denys Vlasenkoe4ba4c42018-12-17 09:51:43 +01007161 bc_char_vec_init(&G.input_buffer);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007162}
Gavin Howard01055ba2018-11-03 11:00:21 -06007163
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007164static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007165{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007166#if ENABLE_FEATURE_EDITING
7167 G.line_input_state = new_line_input_t(DO_HISTORY);
7168#endif
7169 G.prog.len = bc_vm_envLen(env_len);
7170
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007171 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko5f263f42018-12-13 22:49:59 +01007172 IF_BC(if (IS_BC) bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007173 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007174 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007175
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007176//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7177//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007178 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007179#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007180 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007181 // With SA_RESTART, most system calls will restart
7182 // (IOW: they won't fail with EINTR).
7183 // In particular, this means ^C won't cause
7184 // stdout to get into "error state" if SIGINT hits
7185 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007186 //
7187 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007188 // will only be handled after [Enter] since read()
7189 // from stdin is not interrupted by ^C either,
7190 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007191 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007192 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7193
7194 // Without SA_RESTART, this exhibits a bug:
7195 // "while (1) print 1" and try ^C-ing it.
7196 // Intermittently, instead of returning to input line,
7197 // you'll get "output error: Interrupted system call"
7198 // and exit.
7199 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007200#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007201 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007202 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007203 return 0; // "not a tty"
7204}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007205
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007206static BcStatus bc_vm_run(void)
7207{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007208 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007209#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007210 if (G_exiting) // it was actually "halt" or "quit"
7211 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007212 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007213# if ENABLE_FEATURE_EDITING
7214 free_line_input_t(G.line_input_state);
7215# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007216 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007217#endif
Denys Vlasenkod1d29b42018-12-16 16:03:03 +01007218 dbg_exec("exiting with exitcode %d", st);
Gavin Howard01055ba2018-11-03 11:00:21 -06007219 return st;
7220}
7221
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007222#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007223int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007224int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007225{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007226 int is_tty;
7227
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007228 INIT_G();
Gavin Howard01055ba2018-11-03 11:00:21 -06007229
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007230 is_tty = bc_vm_init("BC_LINE_LENGTH");
7231
7232 bc_args(argv);
7233
7234 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7235 bc_vm_info();
7236
7237 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007238}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007239#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007240
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007241#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007242int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007243int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007244{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007245 int noscript;
7246
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007247 INIT_G();
Denys Vlasenko82269122018-12-14 16:30:56 +01007248
7249 // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7250 // 1 char wider than bc from the same package.
7251 // Both default width, and xC_LINE_LENGTH=N are wider:
7252 // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007253 // |1234\ |
7254 // |56 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007255 // "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
Denys Vlasenko0a238142018-12-14 16:48:34 +01007256 // |123\ |
7257 // |456 |
Denys Vlasenko82269122018-12-14 16:30:56 +01007258 // Do the same, or it's a bug?
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007259 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007260
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007261 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7262 noscript = BC_FLAG_I;
7263 for (;;) {
7264 int n = getopt(argc, argv, "e:f:x");
7265 if (n <= 0)
7266 break;
7267 switch (n) {
7268 case 'e':
7269 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007270 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007271 if (n) return n;
7272 break;
7273 case 'f':
7274 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007275 n = zbc_vm_file(optarg);
7276 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007277 break;
7278 case 'x':
7279 option_mask32 |= DC_FLAG_X;
7280 break;
7281 default:
7282 bb_show_usage();
7283 }
7284 }
7285 argv += optind;
7286
7287 while (*argv) {
7288 noscript = 0;
7289 bc_vec_push(&G.files, argv++);
7290 }
7291
7292 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7293
7294 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007295}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007296#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007297
7298#endif // not DC_SMALL