blob: 5a4c92a1c97da834d20e42abc8d57e97f09078f7 [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
13//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
14//config: for details.
15//config:
16//config: This bc has four differences to the GNU bc:
17//config:
18//config: 1) The period (.) can also be used as a shortcut for "last", as in
19//config: the BSD bc.
20//config: 2) Arrays are copied before being passed as arguments to
21//config: functions. This behavior is required by the bc spec.
22//config: 3) Arrays can be passed to the builtin "length" function to get
23//config: the number of elements currently in the array. The following
24//config: example prints "1":
25//config:
26//config: a[0] = 0
27//config: length(a[])
28//config:
29//config: 4) The precedence of the boolean "not" operator (!) is equal to
30//config: that of the unary minus (-), or negation, operator. This still
31//config: allows POSIX-compliant scripts to work while somewhat
32//config: preserving expected behavior (versus C) and making parsing
33//config: easier.
34//config:
35//config: Options:
36//config:
37//config: -i --interactive force interactive mode
38//config: -l --mathlib use predefined math routines:
39//config:
40//config: s(expr) = sine of expr in radians
41//config: c(expr) = cosine of expr in radians
42//config: a(expr) = arctangent of expr, returning
43//config: radians
44//config: l(expr) = natural log of expr
45//config: e(expr) = raises e to the power of expr
46//config: j(n, x) = Bessel function of integer order
47//config: n of x
48//config:
49//config: -q --quiet don't print version and copyright.
50//config: -s --standard error if any non-POSIX extensions are used.
51//config: -w --warn warn if any non-POSIX extensions are used.
52//config: -v --version print version and copyright and exit.
53//config:
54//config: Long options are only available if FEATURE_BC_LONG_OPTIONS is
55//config: enabled.
56//config:
57//config:config DC
58//config: bool "dc (38 kb; 49 kb when combined with bc)"
59//config: default y
60//config: help
61//config: dc is a reverse-polish notation command-line calculator which
62//config: supports unlimited precision arithmetic. See the FreeBSD man page
63//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
64//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html)
65//config: for details.
66//config:
67//config: This dc has a few differences from the two above:
68//config:
69//config: 1) When printing a byte stream (command "P"), this bc follows what
70//config: the FreeBSD dc does.
71//config: 2) This dc implements the GNU extensions for divmod ("~") and
72//config: modular exponentiation ("|").
73//config: 3) This dc implements all FreeBSD extensions, except for "J" and
74//config: "M".
75//config: 4) Like the FreeBSD dc, this dc supports extended registers.
76//config: However, they are implemented differently. When it encounters
77//config: whitespace where a register should be, it skips the whitespace.
78//config: If the character following is not a lowercase letter, an error
79//config: is issued. Otherwise, the register name is parsed by the
80//config: following regex:
81//config:
82//config: [a-z][a-z0-9_]*
83//config:
84//config: This generally means that register names will be surrounded by
85//config: whitespace.
86//config:
87//config: Examples:
88//config:
89//config: l idx s temp L index S temp2 < do_thing
90//config:
91//config: Also note that, like the FreeBSD dc, extended registers are not
92//config: allowed unless the "-x" option is given.
93//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010094//config:config FEATURE_DC_SMALL
95//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
96//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010097//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010098//config:
99//config:config FEATURE_DC_LIBM
100//config: bool "Enable power and exp functions (requires libm)"
101//config: default y
102//config: depends on FEATURE_DC_SMALL
103//config: help
104//config: Enable power and exp functions.
105//config: NOTE: This will require libm to be present for linking.
106//config:
Gavin Howard01055ba2018-11-03 11:00:21 -0600107//config:config FEATURE_BC_SIGNALS
108//config: bool "Enable bc/dc signal handling"
109//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100110//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600111//config: help
112//config: Enable signal handling for bc and dc.
113//config:
114//config:config FEATURE_BC_LONG_OPTIONS
115//config: bool "Enable bc/dc long options"
116//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100117//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600118//config: help
119//config: Enable long options for bc and dc.
120
121//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
122//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
123
124//kbuild:lib-$(CONFIG_BC) += bc.o
125//kbuild:lib-$(CONFIG_DC) += bc.o
126
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100127//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600128//usage:#define bc_trivial_usage
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100129//usage: "[-sqliw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600130//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100131//usage:#define bc_full_usage "\n"
132//usage: "\nArbitrary precision calculator"
133//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100134///////: "\n -i Interactive" - has no effect for now
135//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100136//usage: "\n -l Load standard math library"
137//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100138//usage: "\n -w Warn if extensions are used"
139///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100140//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100141//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600142//usage:
143//usage:#define bc_example_usage
144//usage: "3 + 4.129\n"
145//usage: "1903 - 2893\n"
146//usage: "-129 * 213.28935\n"
147//usage: "12 / -1932\n"
148//usage: "12 % 12\n"
149//usage: "34 ^ 189\n"
150//usage: "scale = 13\n"
151//usage: "ibase = 2\n"
152//usage: "obase = A\n"
153//usage:
154//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100155//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600156//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100157//usage:#define dc_full_usage "\n"
158//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100159//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "\np - print top of the stack (without popping)"
161//usage: "\nf - print entire stack"
162//usage: "\nk - pop the value and set the precision"
163//usage: "\ni - pop the value and set input radix"
164//usage: "\no - pop the value and set output radix"
165//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600166//usage:
167//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100168//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600169//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100170//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600171//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100172//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600173//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100174//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600175//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100176//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600177//usage: "64\n"
178
179#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100180#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600181
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100182#if ENABLE_FEATURE_DC_SMALL
183# include "dc.c"
184#else
185
Gavin Howard01055ba2018-11-03 11:00:21 -0600186typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100187 BC_STATUS_SUCCESS = 0,
188 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100189 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Denys Vlasenkof522dd92018-12-07 16:35:43 +0100190 BC_STATUS_EOF = 3, // bc_vm_stdin() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600191} BcStatus;
192
Gavin Howard01055ba2018-11-03 11:00:21 -0600193#define BC_VEC_INVALID_IDX ((size_t) -1)
194#define BC_VEC_START_CAP (1 << 5)
195
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100196typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600197
198typedef struct BcVec {
199 char *v;
200 size_t len;
201 size_t cap;
202 size_t size;
203 BcVecFree dtor;
204} BcVec;
205
Gavin Howard01055ba2018-11-03 11:00:21 -0600206typedef signed char BcDig;
207
208typedef struct BcNum {
209 BcDig *restrict num;
210 size_t rdx;
211 size_t len;
212 size_t cap;
213 bool neg;
214} BcNum;
215
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100216#define BC_NUM_MIN_BASE ((unsigned long) 2)
217#define BC_NUM_MAX_IBASE ((unsigned long) 16)
218// larger value might speed up BIGNUM calculations a bit:
219#define BC_NUM_DEF_SIZE (16)
220#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600221
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100222#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600223
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100224typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
Denys Vlasenko2a8ad482018-12-08 21:56:37 +0100225
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100226typedef BcStatus (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600227
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100228static BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC;
229static BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC;
230static BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC;
231static BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC;
232static BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC;
233static BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600234static BcStatus bc_num_sqrt(BcNum *a, BcNum *b, size_t scale);
235static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
236 size_t scale);
237
238typedef enum BcInst {
239
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100240#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600241 BC_INST_INC_PRE,
242 BC_INST_DEC_PRE,
243 BC_INST_INC_POST,
244 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100245#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600246
247 BC_INST_NEG,
248
249 BC_INST_POWER,
250 BC_INST_MULTIPLY,
251 BC_INST_DIVIDE,
252 BC_INST_MODULUS,
253 BC_INST_PLUS,
254 BC_INST_MINUS,
255
256 BC_INST_REL_EQ,
257 BC_INST_REL_LE,
258 BC_INST_REL_GE,
259 BC_INST_REL_NE,
260 BC_INST_REL_LT,
261 BC_INST_REL_GT,
262
263 BC_INST_BOOL_NOT,
264 BC_INST_BOOL_OR,
265 BC_INST_BOOL_AND,
266
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100267#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600268 BC_INST_ASSIGN_POWER,
269 BC_INST_ASSIGN_MULTIPLY,
270 BC_INST_ASSIGN_DIVIDE,
271 BC_INST_ASSIGN_MODULUS,
272 BC_INST_ASSIGN_PLUS,
273 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100274#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600275 BC_INST_ASSIGN,
276
277 BC_INST_NUM,
278 BC_INST_VAR,
279 BC_INST_ARRAY_ELEM,
280 BC_INST_ARRAY,
281
282 BC_INST_SCALE_FUNC,
283 BC_INST_IBASE,
284 BC_INST_SCALE,
285 BC_INST_LAST,
286 BC_INST_LENGTH,
287 BC_INST_READ,
288 BC_INST_OBASE,
289 BC_INST_SQRT,
290
291 BC_INST_PRINT,
292 BC_INST_PRINT_POP,
293 BC_INST_STR,
294 BC_INST_PRINT_STR,
295
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100296#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600297 BC_INST_JUMP,
298 BC_INST_JUMP_ZERO,
299
300 BC_INST_CALL,
301
302 BC_INST_RET,
303 BC_INST_RET0,
304
305 BC_INST_HALT,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100306#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600307
308 BC_INST_POP,
309 BC_INST_POP_EXEC,
310
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100311#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600312 BC_INST_MODEXP,
313 BC_INST_DIVMOD,
314
315 BC_INST_EXECUTE,
316 BC_INST_EXEC_COND,
317
318 BC_INST_ASCIIFY,
319 BC_INST_PRINT_STREAM,
320
321 BC_INST_PRINT_STACK,
322 BC_INST_CLEAR_STACK,
323 BC_INST_STACK_LEN,
324 BC_INST_DUPLICATE,
325 BC_INST_SWAP,
326
327 BC_INST_LOAD,
328 BC_INST_PUSH_VAR,
329 BC_INST_PUSH_TO_VAR,
330
331 BC_INST_QUIT,
332 BC_INST_NQUIT,
333
334 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100335#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600336
337} BcInst;
338
339typedef struct BcId {
340 char *name;
341 size_t idx;
342} BcId;
343
344typedef struct BcFunc {
345 BcVec code;
346 BcVec labels;
347 size_t nparams;
348 BcVec autos;
349} BcFunc;
350
351typedef enum BcResultType {
352
353 BC_RESULT_TEMP,
354
355 BC_RESULT_VAR,
356 BC_RESULT_ARRAY_ELEM,
357 BC_RESULT_ARRAY,
358
359 BC_RESULT_STR,
360
361 BC_RESULT_IBASE,
362 BC_RESULT_SCALE,
363 BC_RESULT_LAST,
364
365 // These are between to calculate ibase, obase, and last from instructions.
366 BC_RESULT_CONSTANT,
367 BC_RESULT_ONE,
368
369 BC_RESULT_OBASE,
370
371} BcResultType;
372
373typedef union BcResultData {
374 BcNum n;
375 BcVec v;
376 BcId id;
377} BcResultData;
378
379typedef struct BcResult {
380 BcResultType t;
381 BcResultData d;
382} BcResult;
383
384typedef struct BcInstPtr {
385 size_t func;
386 size_t idx;
387 size_t len;
388} BcInstPtr;
389
Gavin Howard01055ba2018-11-03 11:00:21 -0600390// BC_LEX_NEG is not used in lexing; it is only for parsing.
391typedef enum BcLexType {
392
393 BC_LEX_EOF,
394 BC_LEX_INVALID,
395
396 BC_LEX_OP_INC,
397 BC_LEX_OP_DEC,
398
399 BC_LEX_NEG,
400
401 BC_LEX_OP_POWER,
402 BC_LEX_OP_MULTIPLY,
403 BC_LEX_OP_DIVIDE,
404 BC_LEX_OP_MODULUS,
405 BC_LEX_OP_PLUS,
406 BC_LEX_OP_MINUS,
407
408 BC_LEX_OP_REL_EQ,
409 BC_LEX_OP_REL_LE,
410 BC_LEX_OP_REL_GE,
411 BC_LEX_OP_REL_NE,
412 BC_LEX_OP_REL_LT,
413 BC_LEX_OP_REL_GT,
414
415 BC_LEX_OP_BOOL_NOT,
416 BC_LEX_OP_BOOL_OR,
417 BC_LEX_OP_BOOL_AND,
418
419 BC_LEX_OP_ASSIGN_POWER,
420 BC_LEX_OP_ASSIGN_MULTIPLY,
421 BC_LEX_OP_ASSIGN_DIVIDE,
422 BC_LEX_OP_ASSIGN_MODULUS,
423 BC_LEX_OP_ASSIGN_PLUS,
424 BC_LEX_OP_ASSIGN_MINUS,
425 BC_LEX_OP_ASSIGN,
426
427 BC_LEX_NLINE,
428 BC_LEX_WHITESPACE,
429
430 BC_LEX_LPAREN,
431 BC_LEX_RPAREN,
432
433 BC_LEX_LBRACKET,
434 BC_LEX_COMMA,
435 BC_LEX_RBRACKET,
436
437 BC_LEX_LBRACE,
438 BC_LEX_SCOLON,
439 BC_LEX_RBRACE,
440
441 BC_LEX_STR,
442 BC_LEX_NAME,
443 BC_LEX_NUMBER,
444
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100445 BC_LEX_KEY_1st_keyword,
446 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600447 BC_LEX_KEY_BREAK,
448 BC_LEX_KEY_CONTINUE,
449 BC_LEX_KEY_DEFINE,
450 BC_LEX_KEY_ELSE,
451 BC_LEX_KEY_FOR,
452 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100453 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
454 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600455 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100456 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600457 BC_LEX_KEY_LENGTH,
458 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100459 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600460 BC_LEX_KEY_PRINT,
461 BC_LEX_KEY_QUIT,
462 BC_LEX_KEY_READ,
463 BC_LEX_KEY_RETURN,
464 BC_LEX_KEY_SCALE,
465 BC_LEX_KEY_SQRT,
466 BC_LEX_KEY_WHILE,
467
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100468#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600469 BC_LEX_EQ_NO_REG,
470 BC_LEX_OP_MODEXP,
471 BC_LEX_OP_DIVMOD,
472
473 BC_LEX_COLON,
474 BC_LEX_ELSE,
475 BC_LEX_EXECUTE,
476 BC_LEX_PRINT_STACK,
477 BC_LEX_CLEAR_STACK,
478 BC_LEX_STACK_LEVEL,
479 BC_LEX_DUPLICATE,
480 BC_LEX_SWAP,
481 BC_LEX_POP,
482
483 BC_LEX_ASCIIFY,
484 BC_LEX_PRINT_STREAM,
485
486 BC_LEX_STORE_IBASE,
487 BC_LEX_STORE_SCALE,
488 BC_LEX_LOAD,
489 BC_LEX_LOAD_POP,
490 BC_LEX_STORE_PUSH,
491 BC_LEX_STORE_OBASE,
492 BC_LEX_PRINT_POP,
493 BC_LEX_NQUIT,
494 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100495#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600496} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100497// must match order of BC_LEX_KEY_foo etc above
498#if ENABLE_BC
499struct BcLexKeyword {
500 char name8[8];
501};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100502#define BC_LEX_KW_ENTRY(a, b) \
503 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100504static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100505 BC_LEX_KW_ENTRY("auto" , 1), // 0
506 BC_LEX_KW_ENTRY("break" , 1), // 1
507 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
508 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100509
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100510 BC_LEX_KW_ENTRY("else" , 0), // 4
511 BC_LEX_KW_ENTRY("for" , 1), // 5
512 BC_LEX_KW_ENTRY("halt" , 0), // 6
513 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100514
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100515 BC_LEX_KW_ENTRY("if" , 1), // 8
516 BC_LEX_KW_ENTRY("last" , 0), // 9
517 BC_LEX_KW_ENTRY("length" , 1), // 10
518 BC_LEX_KW_ENTRY("limits" , 0), // 11
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100519
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 Vlasenko51fb8aa2018-12-05 00:22:34 +0100524
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100525 BC_LEX_KW_ENTRY("return" , 1), // 16
526 BC_LEX_KW_ENTRY("scale" , 1), // 17
527 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
528 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100529};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100530#undef BC_LEX_KW_ENTRY
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100531enum {
532 POSIX_KWORD_MASK = 0
533 | (1 << 0)
534 | (1 << 1)
535 | (0 << 2)
536 | (1 << 3)
537 \
538 | (0 << 4)
539 | (1 << 5)
540 | (0 << 6)
541 | (1 << 7)
542 \
543 | (1 << 8)
544 | (0 << 9)
545 | (1 << 10)
546 | (0 << 11)
547 \
548 | (1 << 12)
549 | (0 << 13)
550 | (1 << 14)
551 | (0 << 15)
552 \
553 | (1 << 16)
554 | (1 << 17)
555 | (1 << 18)
556 | (1 << 19)
557};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100558#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100559#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600560
561struct BcLex;
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100562typedef BcStatus (*BcLexNext)(struct BcLex *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600563
564typedef struct BcLex {
565
566 const char *buf;
567 size_t i;
568 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600569 size_t len;
570 bool newline;
571
572 struct {
573 BcLexType t;
574 BcLexType last;
575 BcVec v;
576 } t;
577
578 BcLexNext next;
579
580} BcLex;
581
582#define BC_PARSE_STREND ((char) UCHAR_MAX)
583
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100584#define BC_PARSE_REL (1 << 0)
585#define BC_PARSE_PRINT (1 << 1)
Gavin Howard01055ba2018-11-03 11:00:21 -0600586#define BC_PARSE_NOCALL (1 << 2)
587#define BC_PARSE_NOREAD (1 << 3)
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100588#define BC_PARSE_ARRAY (1 << 4)
Gavin Howard01055ba2018-11-03 11:00:21 -0600589
590#define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags))
591#define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse)))
592
593#define BC_PARSE_FLAG_FUNC_INNER (1 << 0)
594#define BC_PARSE_FUNC_INNER(parse) \
595 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER)
596
597#define BC_PARSE_FLAG_FUNC (1 << 1)
598#define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC)
599
600#define BC_PARSE_FLAG_BODY (1 << 2)
601#define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY)
602
603#define BC_PARSE_FLAG_LOOP (1 << 3)
604#define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP)
605
606#define BC_PARSE_FLAG_LOOP_INNER (1 << 4)
607#define BC_PARSE_LOOP_INNER(parse) \
608 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER)
609
610#define BC_PARSE_FLAG_IF (1 << 5)
611#define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF)
612
613#define BC_PARSE_FLAG_ELSE (1 << 6)
614#define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE)
615
616#define BC_PARSE_FLAG_IF_END (1 << 7)
617#define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END)
618
619#define BC_PARSE_CAN_EXEC(parse) \
620 (!(BC_PARSE_TOP_FLAG(parse) & \
621 (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \
622 BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \
623 BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END)))
624
Gavin Howard01055ba2018-11-03 11:00:21 -0600625struct BcParse;
626
627struct BcProgram;
628
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100629typedef BcStatus (*BcParseParse)(struct BcParse *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600630
631typedef struct BcParse {
632
633 BcParseParse parse;
634
635 BcLex l;
636
637 BcVec flags;
638
639 BcVec exits;
640 BcVec conds;
641
642 BcVec ops;
643
Gavin Howard01055ba2018-11-03 11:00:21 -0600644 BcFunc *func;
645 size_t fidx;
646
647 size_t nbraces;
648 bool auto_part;
649
650} BcParse;
651
Gavin Howard01055ba2018-11-03 11:00:21 -0600652typedef struct BcProgram {
653
654 size_t len;
655 size_t scale;
656
657 BcNum ib;
658 size_t ib_t;
659 BcNum ob;
660 size_t ob_t;
661
662 BcNum hexb;
663
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100664#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600665 BcNum strmb;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100666#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600667
668 BcVec results;
669 BcVec stack;
670
671 BcVec fns;
672 BcVec fn_map;
673
674 BcVec vars;
675 BcVec var_map;
676
677 BcVec arrs;
678 BcVec arr_map;
679
680 BcVec strs;
681 BcVec consts;
682
683 const char *file;
684
685 BcNum last;
686 BcNum zero;
687 BcNum one;
688
689 size_t nchars;
690
Gavin Howard01055ba2018-11-03 11:00:21 -0600691} BcProgram;
692
693#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
694
695#define BC_PROG_MAIN (0)
696#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100697#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600698#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100699#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600700
701#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
702#define BC_PROG_NUM(r, n) \
703 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
704
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100705#define BC_FLAG_W (1 << 0)
706#define BC_FLAG_V (1 << 1)
707#define BC_FLAG_S (1 << 2)
708#define BC_FLAG_Q (1 << 3)
709#define BC_FLAG_L (1 << 4)
710#define BC_FLAG_I (1 << 5)
711#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600712
713#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
714#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
715
Denys Vlasenko64074a12018-12-07 15:50:14 +0100716#define BC_MAX_OBASE ((unsigned) 999)
717#define BC_MAX_DIM ((unsigned) INT_MAX)
718#define BC_MAX_SCALE ((unsigned) UINT_MAX)
719#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
720#define BC_MAX_NUM BC_MAX_STRING
721// Unused apart from "limits" message. Just show a "biggish number" there.
722//#define BC_MAX_NAME BC_MAX_STRING
723//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
724//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
725#define BC_MAX_NAME_STR "999999999"
726#define BC_MAX_EXP_STR "999999999"
727#define BC_MAX_VARS_STR "999999999"
728
729#define BC_MAX_OBASE_STR "999"
730
731#if INT_MAX == 2147483647
732# define BC_MAX_DIM_STR "2147483647"
733#elif INT_MAX == 9223372036854775807
734# define BC_MAX_DIM_STR "9223372036854775807"
735#else
736# error Strange INT_MAX
737#endif
738
739#if UINT_MAX == 4294967295
740# define BC_MAX_SCALE_STR "4294967295"
741# define BC_MAX_STRING_STR "4294967294"
742#elif UINT_MAX == 18446744073709551615
743# define BC_MAX_SCALE_STR "18446744073709551615"
744# define BC_MAX_STRING_STR "18446744073709551614"
745#else
746# error Strange UINT_MAX
747#endif
748#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600749
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100750struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100751 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100752 IF_FEATURE_CLEAN_UP(smallint exiting;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600753 char sbgn;
754 char send;
Gavin Howard01055ba2018-11-03 11:00:21 -0600755
756 BcParse prs;
757 BcProgram prog;
758
Denys Vlasenko5318f812018-12-05 17:48:01 +0100759 // For error messages. Can be set to current parsed line,
760 // or [TODO] to current executing line (can be before last parsed one)
761 unsigned err_line;
762
Gavin Howard01055ba2018-11-03 11:00:21 -0600763 BcVec files;
764
765 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100766
767#if ENABLE_FEATURE_EDITING
768 line_input_t *line_input_state;
769#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100770} FIX_ALIASING;
771#define G (*ptr_to_globals)
772#define INIT_G() do { \
773 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
774} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100775#define FREE_G() do { \
776 FREE_PTR_TO_GLOBALS(); \
777} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100778#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
779#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100780#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100781#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100782# define G_interrupt bb_got_signal
783# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100784#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100785# define G_interrupt 0
786# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100787#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100788#if ENABLE_FEATURE_CLEAN_UP
789# define G_exiting G.exiting
790#else
791# define G_exiting 0
792#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100793#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
794
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100795#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600796
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100797// This is a bit array that corresponds to token types. An entry is
Gavin Howard01055ba2018-11-03 11:00:21 -0600798// true if the token is valid in an expression, false otherwise.
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100799enum {
800 BC_PARSE_EXPRS_BITS = 0
801 + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
802 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
803 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
804 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
805 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
806 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
807 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
808 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8))
Gavin Howard01055ba2018-11-03 11:00:21 -0600809};
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100810static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
811{
812#if ULONG_MAX > 0xffffffff
813 // 64-bit version (will not work correctly for 32-bit longs!)
814 return BC_PARSE_EXPRS_BITS & (1UL << i);
815#else
816 // 32-bit version
817 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
818 if (i >= 32) {
819 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
820 i &= 31;
821 }
822 return m & (1UL << i);
823#endif
824}
Gavin Howard01055ba2018-11-03 11:00:21 -0600825
826// This is an array of data for operators that correspond to token types.
Denys Vlasenko65437582018-12-05 19:37:19 +0100827static const uint8_t bc_parse_ops[] = {
828#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100829 OP(0, false), OP( 0, false ), // inc dec
830 OP(1, false), // neg
Denys Vlasenko65437582018-12-05 19:37:19 +0100831 OP(2, false),
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100832 OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div
833 OP(4, true ), OP( 4, true ), // mod + -
834 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
835 OP(1, false), // not
836 OP(7, true ), OP( 7, true ), // or and
837 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
838 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenko65437582018-12-05 19:37:19 +0100839#undef OP
Gavin Howard01055ba2018-11-03 11:00:21 -0600840};
Denys Vlasenko65437582018-12-05 19:37:19 +0100841#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
842#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
Gavin Howard01055ba2018-11-03 11:00:21 -0600843
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100844// Byte array of up to 4 BC_LEX's, packed into 32-bit word
845typedef uint32_t BcParseNext;
846
Gavin Howard01055ba2018-11-03 11:00:21 -0600847// These identify what tokens can come after expressions in certain cases.
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100848enum {
849#define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) )
850#define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff)
851#define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff)
852 bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF),
853 bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA),
854 bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF),
855 bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN),
856 bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET),
857 bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON),
858 bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF),
859#undef BC_PARSE_NEXT4
860#undef BC_PARSE_NEXT2
861#undef BC_PARSE_NEXT1
862};
Gavin Howard01055ba2018-11-03 11:00:21 -0600863#endif // ENABLE_BC
864
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100865#if ENABLE_DC
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100866static const //BcLexType - should be this type, but narrower type saves size:
867uint8_t
868dc_lex_regs[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600869 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
870 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
871 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
872 BC_LEX_STORE_PUSH,
873};
874
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100875static const //BcLexType - should be this type
876uint8_t
877dc_lex_tokens[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600878 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
879 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
880 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
881 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
882 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
883 BC_LEX_INVALID, BC_LEX_INVALID,
884 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
885 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
886 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
887 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
888 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
889 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
890 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
891 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
892 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
893 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
894 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
895 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
896 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
897 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
898 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
899 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
900 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
901 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
902 BC_LEX_INVALID
903};
904
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100905static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
906int8_t
907dc_parse_insts[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600908 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
909 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
910 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
911 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
912 BC_INST_INVALID, BC_INST_INVALID,
913 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
914 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
915 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
916 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
917 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
918 BC_INST_INVALID, BC_INST_INVALID,
919 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
920 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
921 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
922 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
923 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
924 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
925 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
926 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
927 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
928 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
929 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
930 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
931};
932#endif // ENABLE_DC
933
Gavin Howard01055ba2018-11-03 11:00:21 -0600934static const BcNumBinaryOp bc_program_ops[] = {
935 bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub,
936};
937
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100938static void fflush_and_check(void)
939{
940 fflush_all();
941 if (ferror(stdout) || ferror(stderr))
942 bb_perror_msg_and_die("output error");
943}
944
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100945#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100946#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100947do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100948 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100949 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100950 return BC_STATUS_FAILURE; \
951} while (0)
952#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100953#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100954#endif
955
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100956static void quit(void) NORETURN;
957static void quit(void)
958{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100959 if (ferror(stdin))
960 bb_perror_msg_and_die("input error");
961 fflush_and_check();
962 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100963}
964
Denys Vlasenko5318f812018-12-05 17:48:01 +0100965static void bc_verror_msg(const char *fmt, va_list p)
966{
967 const char *sv = sv; /* for compiler */
968 if (G.prog.file) {
969 sv = applet_name;
970 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
971 }
972 bb_verror_msg(fmt, p, NULL);
973 if (G.prog.file) {
974 free((char*)applet_name);
975 applet_name = sv;
976 }
977}
978
Denys Vlasenko29301232018-12-11 15:29:32 +0100979// In configurations where errors abort instead of propagating error
980// return code up the call chain, functions returning BC_STATUS
981// actually don't return anything, they always succeed and return "void".
982// A macro wrapper is provided, which makes this statement work:
983// s = zbc_func(...)
984// and makes it visible to the compiler that s is always zero,
985// allowing compiler to optimize dead code after the statement.
986//
987// To make code more readable, each such function has a "z"
988// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
989//
990#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkofa35e592018-12-10 20:17:24 +0100991# define ERRORFUNC /*nothing*/
992# define ERROR_RETURN(a) a
993# define ERRORS_ARE_FATAL 0
Denys Vlasenko29301232018-12-11 15:29:32 +0100994# define BC_STATUS BcStatus
995# define RETURN_STATUS(v) return (v)
Denys Vlasenko86e63cd2018-12-10 19:46:53 +0100996#else
Denys Vlasenko29301232018-12-11 15:29:32 +0100997# define ERRORFUNC NORETURN
998# define ERROR_RETURN(a) /*nothing*/
999# define ERRORS_ARE_FATAL 1
1000# define BC_STATUS void
1001# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001002#endif
1003
1004static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001005{
1006 va_list p;
1007
1008 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001009 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001010 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01001011
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001012 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001013 exit(1);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001014 ERROR_RETURN(return BC_STATUS_FAILURE;)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001015}
1016
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001017#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001018static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001019{
1020 va_list p;
1021
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001022 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001023 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001024 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001025
1026 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001027 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001028 va_end(p);
1029
1030 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001031 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001032 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001033 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001034 exit(1);
1035 return BC_STATUS_FAILURE;
1036}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001037#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001038
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001039// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1040// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001041// function must not have caller-cleaned parameters on stack.
1042// Unfortunately, vararg function API does exactly that on most arches.
1043// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001044static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001045{
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001046 ERROR_RETURN(return) bc_error_fmt("%s", msg);
1047}
1048static ERRORFUNC int bc_error_bad_character(char c)
1049{
1050 ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c);
1051}
1052static ERRORFUNC int bc_error_bad_expression(void)
1053{
1054 ERROR_RETURN(return) bc_error("bad expression");
1055}
1056static ERRORFUNC int bc_error_bad_token(void)
1057{
1058 ERROR_RETURN(return) bc_error("bad token");
1059}
1060static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1061{
1062 ERROR_RETURN(return) bc_error("stack has too few elements");
1063}
1064static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1065{
1066 ERROR_RETURN(return) bc_error("variable is wrong type");
1067}
1068static ERRORFUNC int bc_error_nested_read_call(void)
1069{
1070 ERROR_RETURN(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001071}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001072#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001073static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001074{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001075 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001076}
Denys Vlasenko00646792018-12-05 18:12:27 +01001077static int bc_POSIX_does_not_allow(const char *msg)
1078{
1079 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1080}
1081static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1082{
1083 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1084}
1085static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1086{
1087 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1088}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001089#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001090
Gavin Howard01055ba2018-11-03 11:00:21 -06001091static void bc_vec_grow(BcVec *v, size_t n)
1092{
1093 size_t cap = v->cap * 2;
1094 while (cap < v->len + n) cap *= 2;
1095 v->v = xrealloc(v->v, v->size * cap);
1096 v->cap = cap;
1097}
1098
1099static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1100{
1101 v->size = esize;
1102 v->cap = BC_VEC_START_CAP;
1103 v->len = 0;
1104 v->dtor = dtor;
1105 v->v = xmalloc(esize * BC_VEC_START_CAP);
1106}
1107
Denys Vlasenko7d628012018-12-04 21:46:47 +01001108static void bc_char_vec_init(BcVec *v)
1109{
1110 bc_vec_init(v, sizeof(char), NULL);
1111}
1112
Gavin Howard01055ba2018-11-03 11:00:21 -06001113static void bc_vec_expand(BcVec *v, size_t req)
1114{
1115 if (v->cap < req) {
1116 v->v = xrealloc(v->v, v->size * req);
1117 v->cap = req;
1118 }
1119}
1120
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001121static void bc_vec_pop(BcVec *v)
1122{
1123 v->len--;
1124 if (v->dtor)
1125 v->dtor(v->v + (v->size * v->len));
1126}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001127
Gavin Howard01055ba2018-11-03 11:00:21 -06001128static void bc_vec_npop(BcVec *v, size_t n)
1129{
1130 if (!v->dtor)
1131 v->len -= n;
1132 else {
1133 size_t len = v->len - n;
1134 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1135 }
1136}
1137
Denys Vlasenko7d628012018-12-04 21:46:47 +01001138static void bc_vec_pop_all(BcVec *v)
1139{
1140 bc_vec_npop(v, v->len);
1141}
1142
Gavin Howard01055ba2018-11-03 11:00:21 -06001143static void bc_vec_push(BcVec *v, const void *data)
1144{
1145 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1146 memmove(v->v + (v->size * v->len), data, v->size);
1147 v->len += 1;
1148}
1149
1150static void bc_vec_pushByte(BcVec *v, char data)
1151{
1152 bc_vec_push(v, &data);
1153}
1154
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001155static void bc_vec_pushZeroByte(BcVec *v)
1156{
1157 //bc_vec_pushByte(v, '\0');
1158 // better:
1159 bc_vec_push(v, &const_int_0);
1160}
1161
Gavin Howard01055ba2018-11-03 11:00:21 -06001162static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1163{
1164 if (idx == v->len)
1165 bc_vec_push(v, data);
1166 else {
1167
1168 char *ptr;
1169
1170 if (v->len == v->cap) bc_vec_grow(v, 1);
1171
1172 ptr = v->v + v->size * idx;
1173
1174 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1175 memmove(ptr, data, v->size);
1176 }
1177}
1178
1179static void bc_vec_string(BcVec *v, size_t len, const char *str)
1180{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001181 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001182 bc_vec_expand(v, len + 1);
1183 memcpy(v->v, str, len);
1184 v->len = len;
1185
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001186 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001187}
1188
1189static void bc_vec_concat(BcVec *v, const char *str)
1190{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001191 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001192
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001193 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001194
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001195 slen = strlen(str);
1196 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001197
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001198 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001199 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001200
1201 v->len = len;
1202}
1203
1204static void *bc_vec_item(const BcVec *v, size_t idx)
1205{
1206 return v->v + v->size * idx;
1207}
1208
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01001209static char** bc_program_str(size_t idx)
1210{
1211 return bc_vec_item(&G.prog.strs, idx);
1212}
1213
1214static BcFunc* bc_program_func(size_t idx)
1215{
1216 return bc_vec_item(&G.prog.fns, idx);
1217}
1218
Gavin Howard01055ba2018-11-03 11:00:21 -06001219static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1220{
1221 return v->v + v->size * (v->len - idx - 1);
1222}
1223
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001224static void *bc_vec_top(const BcVec *v)
1225{
1226 return v->v + v->size * (v->len - 1);
1227}
1228
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001229static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001230{
1231 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001232 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001233 free(v->v);
1234}
1235
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001236static int bc_id_cmp(const void *e1, const void *e2)
1237{
1238 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1239}
1240
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001241static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001242{
1243 free(((BcId *) id)->name);
1244}
1245
Gavin Howard01055ba2018-11-03 11:00:21 -06001246static size_t bc_map_find(const BcVec *v, const void *ptr)
1247{
1248 size_t low = 0, high = v->len;
1249
1250 while (low < high) {
1251
1252 size_t mid = (low + high) / 2;
1253 BcId *id = bc_vec_item(v, mid);
1254 int result = bc_id_cmp(ptr, id);
1255
1256 if (result == 0)
1257 return mid;
1258 else if (result < 0)
1259 high = mid;
1260 else
1261 low = mid + 1;
1262 }
1263
1264 return low;
1265}
1266
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001267static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001268{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001269 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001270
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001271 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001272 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001273 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1274 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001275 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001276 bc_vec_pushAt(v, ptr, n);
1277 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001278}
1279
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001280#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06001281static size_t bc_map_index(const BcVec *v, const void *ptr)
1282{
1283 size_t i = bc_map_find(v, ptr);
1284 if (i >= v->len) return BC_VEC_INVALID_IDX;
1285 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1286}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001287#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001288
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001289static int push_input_byte(BcVec *vec, char c)
1290{
1291 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1292 || c > 0x7e
1293 ) {
1294 // Bad chars on this line, ignore entire line
1295 bc_error_fmt("illegal character 0x%02x", c);
1296 return 1;
1297 }
1298 bc_vec_pushByte(vec, (char)c);
1299 return 0;
1300}
1301
Denys Vlasenkob402ff82018-12-11 15:45:15 +01001302// This is not a "z" function: can also return BC_STATUS_EOF
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001303static BcStatus bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001304{
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001305 BcStatus s;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001306 bool bad_chars;
Gavin Howard01055ba2018-11-03 11:00:21 -06001307
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001308 s = BC_STATUS_SUCCESS;
Denys Vlasenko00d77792018-11-30 23:13:42 +01001309 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001310 int c;
Gavin Howard01055ba2018-11-03 11:00:21 -06001311
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001312 bad_chars = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001313 bc_vec_pop_all(vec);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001314
1315 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001316
Gavin Howard01055ba2018-11-03 11:00:21 -06001317#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001318 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001319 intr:
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001320 G_interrupt = 0;
Denys Vlasenkoac6ed112018-12-08 21:39:10 +01001321 // GNU bc says "interrupted execution."
1322 // GNU dc says "Interrupt!"
1323 fputs("\ninterrupted execution\n", stderr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001324 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001325# if ENABLE_FEATURE_EDITING
1326 if (G_ttyin) {
1327 int n, i;
1328# define line_buf bb_common_bufsiz1
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001329 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001330 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1331 if (n == 0) // ^C
1332 goto intr;
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001333 s = BC_STATUS_EOF;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001334 break;
1335 }
1336 i = 0;
1337 for (;;) {
1338 c = line_buf[i++];
1339 if (!c) break;
1340 bad_chars |= push_input_byte(vec, c);
1341 }
1342# undef line_buf
1343 } else
1344# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001345#endif
Denys Vlasenkoed849352018-12-06 10:26:13 +01001346 {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001347 IF_FEATURE_BC_SIGNALS(errno = 0;)
1348 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001349 c = fgetc(stdin);
1350#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
1351 // Both conditions appear simultaneously, check both just in case
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001352 if (errno == EINTR || G_interrupt) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001353 // ^C was pressed
1354 clearerr(stdin);
1355 goto intr;
1356 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001357#endif
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001358 if (c == EOF) {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001359 if (ferror(stdin))
1360 quit(); // this emits error message
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001361 s = BC_STATUS_EOF;
Denys Vlasenkoed849352018-12-06 10:26:13 +01001362 // Note: EOF does not append '\n', therefore:
1363 // printf 'print 123\n' | bc - works
1364 // printf 'print 123' | bc - fails (syntax error)
1365 break;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001366 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001367 bad_chars |= push_input_byte(vec, c);
1368 } while (c != '\n');
Denys Vlasenkoed849352018-12-06 10:26:13 +01001369 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001370 } while (bad_chars);
Gavin Howard01055ba2018-11-03 11:00:21 -06001371
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001372 bc_vec_pushZeroByte(vec);
Gavin Howard01055ba2018-11-03 11:00:21 -06001373
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001374 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001375}
1376
Denys Vlasenkodf515392018-12-02 19:27:48 +01001377static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001378{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001379 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001380 size_t size = ((size_t) -1);
1381 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001382
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001383 // Never returns NULL (dies on errors)
1384 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001385
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001386 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001387 char c = buf[i];
1388 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1389 || c > 0x7e
1390 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001391 free(buf);
1392 buf = NULL;
1393 break;
1394 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001395 }
1396
Denys Vlasenkodf515392018-12-02 19:27:48 +01001397 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001398}
1399
Gavin Howard01055ba2018-11-03 11:00:21 -06001400static void bc_num_setToZero(BcNum *n, size_t scale)
1401{
1402 n->len = 0;
1403 n->neg = false;
1404 n->rdx = scale;
1405}
1406
1407static void bc_num_zero(BcNum *n)
1408{
1409 bc_num_setToZero(n, 0);
1410}
1411
1412static void bc_num_one(BcNum *n)
1413{
1414 bc_num_setToZero(n, 0);
1415 n->len = 1;
1416 n->num[0] = 1;
1417}
1418
1419static void bc_num_ten(BcNum *n)
1420{
1421 bc_num_setToZero(n, 0);
1422 n->len = 2;
1423 n->num[0] = 0;
1424 n->num[1] = 1;
1425}
1426
Denys Vlasenko3129f702018-12-09 12:04:44 +01001427// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001428static void bc_num_init(BcNum *n, size_t req)
1429{
1430 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001431 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001432 n->num = xmalloc(req);
1433 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001434 n->rdx = 0;
1435 n->len = 0;
1436 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001437}
1438
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001439static void bc_num_init_DEF_SIZE(BcNum *n)
1440{
1441 bc_num_init(n, BC_NUM_DEF_SIZE);
1442}
1443
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001444static void bc_num_expand(BcNum *n, size_t req)
1445{
1446 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1447 if (req > n->cap) {
1448 n->num = xrealloc(n->num, req);
1449 n->cap = req;
1450 }
1451}
1452
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001453static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001454{
1455 free(((BcNum *) num)->num);
1456}
1457
1458static void bc_num_copy(BcNum *d, BcNum *s)
1459{
1460 if (d != s) {
1461 bc_num_expand(d, s->cap);
1462 d->len = s->len;
1463 d->neg = s->neg;
1464 d->rdx = s->rdx;
1465 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1466 }
1467}
1468
Denys Vlasenko29301232018-12-11 15:29:32 +01001469static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001470{
1471 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001472 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001473
Denys Vlasenko29301232018-12-11 15:29:32 +01001474 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001475
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001476 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001477
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001478 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001479
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001480 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001481 pow *= 10;
1482
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001483 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001484 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001485 prev = result;
1486 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001487 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001488 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001489
Denys Vlasenko29301232018-12-11 15:29:32 +01001490 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001491}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001492#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001493# define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001494#endif
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001495
1496static void bc_num_ulong2num(BcNum *n, unsigned long val)
1497{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001498 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001499
1500 bc_num_zero(n);
1501
1502 if (val == 0) return;
1503
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001504 if (ULONG_MAX == 0xffffffffUL)
1505 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001506 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001507 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001508 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001509
1510 ptr = n->num;
1511 for (;;) {
1512 n->len++;
1513 *ptr++ = val % 10;
1514 val /= 10;
1515 if (val == 0) break;
1516 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001517}
1518
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001519static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001520 size_t len)
1521{
1522 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001523 for (i = 0; i < len; ++i) {
1524 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001525 a[i + j++] += 10;
1526 a[i + j] -= 1;
1527 }
1528 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001529}
1530
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01001531#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1532#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1533#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1534//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1535static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1536{
1537 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1538}
1539//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1540static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1541{
1542 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1543}
1544
Gavin Howard01055ba2018-11-03 11:00:21 -06001545static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1546{
1547 size_t i;
1548 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001549 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001550 return BC_NUM_NEG(i + 1, c < 0);
1551}
1552
1553static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1554{
1555 size_t i, min, a_int, b_int, diff;
1556 BcDig *max_num, *min_num;
1557 bool a_max, neg = false;
1558 ssize_t cmp;
1559
1560 if (a == b) return 0;
1561 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1562 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
1563 if (a->neg) {
1564 if (b->neg)
1565 neg = true;
1566 else
1567 return -1;
1568 }
1569 else if (b->neg)
1570 return 1;
1571
1572 a_int = BC_NUM_INT(a);
1573 b_int = BC_NUM_INT(b);
1574 a_int -= b_int;
1575 a_max = (a->rdx > b->rdx);
1576
1577 if (a_int != 0) return (ssize_t) a_int;
1578
1579 if (a_max) {
1580 min = b->rdx;
1581 diff = a->rdx - b->rdx;
1582 max_num = a->num + diff;
1583 min_num = b->num;
1584 }
1585 else {
1586 min = a->rdx;
1587 diff = b->rdx - a->rdx;
1588 max_num = b->num + diff;
1589 min_num = a->num;
1590 }
1591
1592 cmp = bc_num_compare(max_num, min_num, b_int + min);
1593 if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg);
1594
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001595 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001596 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
1597 }
1598
1599 return 0;
1600}
1601
1602static void bc_num_truncate(BcNum *n, size_t places)
1603{
1604 if (places == 0) return;
1605
1606 n->rdx -= places;
1607
1608 if (n->len != 0) {
1609 n->len -= places;
1610 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1611 }
1612}
1613
1614static void bc_num_extend(BcNum *n, size_t places)
1615{
1616 size_t len = n->len + places;
1617
1618 if (places != 0) {
1619
1620 if (n->cap < len) bc_num_expand(n, len);
1621
1622 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1623 memset(n->num, 0, sizeof(BcDig) * places);
1624
1625 n->len += places;
1626 n->rdx += places;
1627 }
1628}
1629
1630static void bc_num_clean(BcNum *n)
1631{
1632 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1633 if (n->len == 0)
1634 n->neg = false;
1635 else if (n->len < n->rdx)
1636 n->len = n->rdx;
1637}
1638
1639static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1640{
1641 if (n->rdx < scale)
1642 bc_num_extend(n, scale - n->rdx);
1643 else
1644 bc_num_truncate(n, n->rdx - scale);
1645
1646 bc_num_clean(n);
1647 if (n->len != 0) n->neg = !neg1 != !neg2;
1648}
1649
1650static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1651 BcNum *restrict b)
1652{
1653 if (idx < n->len) {
1654
1655 b->len = n->len - idx;
1656 a->len = idx;
1657 a->rdx = b->rdx = 0;
1658
1659 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1660 memcpy(a->num, n->num, idx * sizeof(BcDig));
1661 }
1662 else {
1663 bc_num_zero(b);
1664 bc_num_copy(a, n);
1665 }
1666
1667 bc_num_clean(a);
1668 bc_num_clean(b);
1669}
1670
Denys Vlasenko29301232018-12-11 15:29:32 +01001671static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001672{
Denys Vlasenko29301232018-12-11 15:29:32 +01001673 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001674
1675 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1676 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1677 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001678 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001679 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001680
1681 if (n->rdx >= places)
1682 n->rdx -= places;
1683 else {
1684 bc_num_extend(n, places - n->rdx);
1685 n->rdx = 0;
1686 }
1687
1688 bc_num_clean(n);
1689
Denys Vlasenko29301232018-12-11 15:29:32 +01001690 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001691}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001692#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001693# define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001694#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001695
1696static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale)
1697{
1698 BcNum one;
1699 BcDig num[2];
1700
1701 one.cap = 2;
1702 one.num = num;
1703 bc_num_one(&one);
1704
1705 return bc_num_div(&one, a, b, scale);
1706}
1707
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001708static FAST_FUNC BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001709{
1710 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1711 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1712 int carry, in;
1713
1714 // Because this function doesn't need to use scale (per the bc spec),
1715 // I am hijacking it to say whether it's doing an add or a subtract.
1716
1717 if (a->len == 0) {
1718 bc_num_copy(c, b);
1719 if (sub && c->len) c->neg = !c->neg;
1720 return BC_STATUS_SUCCESS;
1721 }
1722 else if (b->len == 0) {
1723 bc_num_copy(c, a);
1724 return BC_STATUS_SUCCESS;
1725 }
1726
1727 c->neg = a->neg;
1728 c->rdx = BC_MAX(a->rdx, b->rdx);
1729 min_rdx = BC_MIN(a->rdx, b->rdx);
1730 c->len = 0;
1731
1732 if (a->rdx > b->rdx) {
1733 diff = a->rdx - b->rdx;
1734 ptr = a->num;
1735 ptr_a = a->num + diff;
1736 ptr_b = b->num;
1737 }
1738 else {
1739 diff = b->rdx - a->rdx;
1740 ptr = b->num;
1741 ptr_a = a->num;
1742 ptr_b = b->num + diff;
1743 }
1744
1745 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1746
1747 ptr_c += diff;
1748 a_int = BC_NUM_INT(a);
1749 b_int = BC_NUM_INT(b);
1750
1751 if (a_int > b_int) {
1752 min_int = b_int;
1753 max = a_int;
1754 ptr = ptr_a;
1755 }
1756 else {
1757 min_int = a_int;
1758 max = b_int;
1759 ptr = ptr_b;
1760 }
1761
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001762 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001763 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1764 carry = in / 10;
1765 ptr_c[i] = (BcDig)(in % 10);
1766 }
1767
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001768 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001769 in = ((int) ptr[i]) + carry;
1770 carry = in / 10;
1771 ptr_c[i] = (BcDig)(in % 10);
1772 }
1773
1774 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1775
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001776 return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001777}
1778
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001779static FAST_FUNC BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001780{
Gavin Howard01055ba2018-11-03 11:00:21 -06001781 ssize_t cmp;
1782 BcNum *minuend, *subtrahend;
1783 size_t start;
1784 bool aneg, bneg, neg;
1785
1786 // Because this function doesn't need to use scale (per the bc spec),
1787 // I am hijacking it to say whether it's doing an add or a subtract.
1788
1789 if (a->len == 0) {
1790 bc_num_copy(c, b);
1791 if (sub && c->len) c->neg = !c->neg;
1792 return BC_STATUS_SUCCESS;
1793 }
1794 else if (b->len == 0) {
1795 bc_num_copy(c, a);
1796 return BC_STATUS_SUCCESS;
1797 }
1798
1799 aneg = a->neg;
1800 bneg = b->neg;
1801 a->neg = b->neg = false;
1802
1803 cmp = bc_num_cmp(a, b);
1804
1805 a->neg = aneg;
1806 b->neg = bneg;
1807
1808 if (cmp == 0) {
1809 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
1810 return BC_STATUS_SUCCESS;
1811 }
1812 else if (cmp > 0) {
1813 neg = a->neg;
1814 minuend = a;
1815 subtrahend = b;
1816 }
1817 else {
1818 neg = b->neg;
1819 if (sub) neg = !neg;
1820 minuend = b;
1821 subtrahend = a;
1822 }
1823
1824 bc_num_copy(c, minuend);
1825 c->neg = neg;
1826
1827 if (c->rdx < subtrahend->rdx) {
1828 bc_num_extend(c, subtrahend->rdx - c->rdx);
1829 start = 0;
1830 }
1831 else
1832 start = c->rdx - subtrahend->rdx;
1833
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001834 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001835
1836 bc_num_clean(c);
1837
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001838 return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001839}
1840
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001841static FAST_FUNC BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001842 BcNum *restrict c)
1843{
1844 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001845 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001846 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001847 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001848
Gavin Howard01055ba2018-11-03 11:00:21 -06001849 if (a->len == 0 || b->len == 0) {
1850 bc_num_zero(c);
1851 return BC_STATUS_SUCCESS;
1852 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001853 aone = BC_NUM_ONE(a);
1854 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001855 bc_num_copy(c, aone ? b : a);
1856 return BC_STATUS_SUCCESS;
1857 }
1858
1859 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1860 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1861 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001862 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001863 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001864
Gavin Howard01055ba2018-11-03 11:00:21 -06001865 bc_num_expand(c, a->len + b->len + 1);
1866
1867 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001868 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001869
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001870 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001871
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001872 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001873 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001874 unsigned in = c->num[i + j];
1875 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1876 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001877 carry = in / 10;
1878 c->num[i + j] = (BcDig)(in % 10);
1879 }
1880
1881 c->num[i + j] += (BcDig) carry;
1882 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001883
1884 // a=2^1000000
1885 // a*a <- without check below, this will not be interruptible
1886 if (G_interrupt) return BC_STATUS_FAILURE;
Gavin Howard01055ba2018-11-03 11:00:21 -06001887 }
1888
1889 c->len = len;
1890
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001891 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06001892 }
1893
1894 bc_num_init(&l1, max);
1895 bc_num_init(&h1, max);
1896 bc_num_init(&l2, max);
1897 bc_num_init(&h2, max);
1898 bc_num_init(&m1, max);
1899 bc_num_init(&m2, max);
1900 bc_num_init(&z0, max);
1901 bc_num_init(&z1, max);
1902 bc_num_init(&z2, max);
1903 bc_num_init(&temp, max + max);
1904
1905 bc_num_split(a, max2, &l1, &h1);
1906 bc_num_split(b, max2, &l2, &h2);
1907
1908 s = bc_num_add(&h1, &l1, &m1, 0);
1909 if (s) goto err;
1910 s = bc_num_add(&h2, &l2, &m2, 0);
1911 if (s) goto err;
1912
1913 s = bc_num_k(&h1, &h2, &z0);
1914 if (s) goto err;
1915 s = bc_num_k(&m1, &m2, &z1);
1916 if (s) goto err;
1917 s = bc_num_k(&l1, &l2, &z2);
1918 if (s) goto err;
1919
1920 s = bc_num_sub(&z1, &z0, &temp, 0);
1921 if (s) goto err;
1922 s = bc_num_sub(&temp, &z2, &z1, 0);
1923 if (s) goto err;
1924
Denys Vlasenko29301232018-12-11 15:29:32 +01001925 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001926 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001927 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001928 if (s) goto err;
1929 s = bc_num_add(&z0, &z1, &temp, 0);
1930 if (s) goto err;
1931 s = bc_num_add(&temp, &z2, c, 0);
1932
1933err:
1934 bc_num_free(&temp);
1935 bc_num_free(&z2);
1936 bc_num_free(&z1);
1937 bc_num_free(&z0);
1938 bc_num_free(&m2);
1939 bc_num_free(&m1);
1940 bc_num_free(&h2);
1941 bc_num_free(&l2);
1942 bc_num_free(&h1);
1943 bc_num_free(&l1);
1944 return s;
1945}
1946
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001947static FAST_FUNC BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001948{
1949 BcStatus s;
1950 BcNum cpa, cpb;
1951 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1952
1953 scale = BC_MAX(scale, a->rdx);
1954 scale = BC_MAX(scale, b->rdx);
1955 scale = BC_MIN(a->rdx + b->rdx, scale);
1956 maxrdx = BC_MAX(maxrdx, scale);
1957
1958 bc_num_init(&cpa, a->len);
1959 bc_num_init(&cpb, b->len);
1960
1961 bc_num_copy(&cpa, a);
1962 bc_num_copy(&cpb, b);
1963 cpa.neg = cpb.neg = false;
1964
Denys Vlasenko29301232018-12-11 15:29:32 +01001965 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001966 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001967 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001968 if (s) goto err;
1969 s = bc_num_k(&cpa, &cpb, c);
1970 if (s) goto err;
1971
1972 maxrdx += scale;
1973 bc_num_expand(c, c->len + maxrdx);
1974
1975 if (c->len < maxrdx) {
1976 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1977 c->len += maxrdx;
1978 }
1979
1980 c->rdx = maxrdx;
1981 bc_num_retireMul(c, scale, a->neg, b->neg);
1982
1983err:
1984 bc_num_free(&cpb);
1985 bc_num_free(&cpa);
1986 return s;
1987}
1988
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001989static FAST_FUNC BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001990{
1991 BcStatus s = BC_STATUS_SUCCESS;
1992 BcDig *n, *p, q;
1993 size_t len, end, i;
1994 BcNum cp;
1995 bool zero = true;
1996
1997 if (b->len == 0)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01001998 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06001999 else if (a->len == 0) {
2000 bc_num_setToZero(c, scale);
2001 return BC_STATUS_SUCCESS;
2002 }
2003 else if (BC_NUM_ONE(b)) {
2004 bc_num_copy(c, a);
2005 bc_num_retireMul(c, scale, a->neg, b->neg);
2006 return BC_STATUS_SUCCESS;
2007 }
2008
2009 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2010 bc_num_copy(&cp, a);
2011 len = b->len;
2012
2013 if (len > cp.len) {
2014 bc_num_expand(&cp, len + 2);
2015 bc_num_extend(&cp, len - cp.len);
2016 }
2017
2018 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2019 cp.rdx -= b->rdx;
2020 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2021
2022 if (b->rdx == b->len) {
2023 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
2024 len -= i - 1;
2025 }
2026
2027 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2028
2029 // We want an extra zero in front to make things simpler.
2030 cp.num[cp.len++] = 0;
2031 end = cp.len - len;
2032
2033 bc_num_expand(c, cp.len);
2034
2035 bc_num_zero(c);
2036 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2037 c->rdx = cp.rdx;
2038 c->len = cp.len;
2039 p = b->num;
2040
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002041 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002042 n = cp.num + i;
2043 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002044 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002045 c->num[i] = q;
Denys Vlasenkof381a882018-12-05 19:21:34 +01002046 // a=2^100000
2047 // scale=40000
2048 // 1/a <- without check below, this will not be interruptible
2049 if (G_interrupt) {
2050 s = BC_STATUS_FAILURE;
2051 break;
2052 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002053 }
2054
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002055 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002056 bc_num_free(&cp);
2057
Denys Vlasenkof381a882018-12-05 19:21:34 +01002058 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06002059}
2060
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002061static FAST_FUNC BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002062 BcNum *restrict d, size_t scale, size_t ts)
2063{
2064 BcStatus s;
2065 BcNum temp;
2066 bool neg;
2067
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002068 if (b->len == 0)
2069 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06002070
2071 if (a->len == 0) {
2072 bc_num_setToZero(d, ts);
2073 return BC_STATUS_SUCCESS;
2074 }
2075
2076 bc_num_init(&temp, d->cap);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002077 s = bc_num_d(a, b, c, scale);
2078 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002079
2080 if (scale != 0) scale = ts;
2081
2082 s = bc_num_m(c, b, &temp, scale);
2083 if (s) goto err;
2084 s = bc_num_sub(a, &temp, d, scale);
2085 if (s) goto err;
2086
2087 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2088
2089 neg = d->neg;
2090 bc_num_retireMul(d, ts, a->neg, b->neg);
2091 d->neg = neg;
2092
2093err:
2094 bc_num_free(&temp);
2095 return s;
2096}
2097
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002098static FAST_FUNC BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002099{
2100 BcStatus s;
2101 BcNum c1;
2102 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2103
2104 bc_num_init(&c1, len);
2105 s = bc_num_r(a, b, &c1, c, scale, ts);
2106 bc_num_free(&c1);
2107
2108 return s;
2109}
2110
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002111static FAST_FUNC BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002112{
2113 BcStatus s = BC_STATUS_SUCCESS;
2114 BcNum copy;
2115 unsigned long pow;
2116 size_t i, powrdx, resrdx;
2117 bool neg, zero;
2118
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002119 if (b->rdx) return bc_error("non integer number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002120
2121 if (b->len == 0) {
2122 bc_num_one(c);
2123 return BC_STATUS_SUCCESS;
2124 }
2125 else if (a->len == 0) {
2126 bc_num_setToZero(c, scale);
2127 return BC_STATUS_SUCCESS;
2128 }
2129 else if (BC_NUM_ONE(b)) {
2130 if (!b->neg)
2131 bc_num_copy(c, a);
2132 else
2133 s = bc_num_inv(a, c, scale);
2134 return s;
2135 }
2136
2137 neg = b->neg;
2138 b->neg = false;
2139
Denys Vlasenko29301232018-12-11 15:29:32 +01002140 s = zbc_num_ulong(b, &pow);
Gavin Howard01055ba2018-11-03 11:00:21 -06002141 if (s) return s;
2142
2143 bc_num_init(&copy, a->len);
2144 bc_num_copy(&copy, a);
2145
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002146 if (!neg) {
2147 if (a->rdx > scale)
2148 scale = a->rdx;
2149 if (a->rdx * pow < scale)
2150 scale = a->rdx * pow;
2151 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002152
2153 b->neg = neg;
2154
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002155 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002156 powrdx <<= 1;
2157 s = bc_num_mul(&copy, &copy, &copy, powrdx);
2158 if (s) goto err;
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002159 // Not needed: bc_num_mul() has a check for ^C:
2160 //if (G_interrupt) {
2161 // s = BC_STATUS_FAILURE;
2162 // goto err;
2163 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002164 }
2165
Gavin Howard01055ba2018-11-03 11:00:21 -06002166 bc_num_copy(c, &copy);
2167
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002168 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002169
2170 powrdx <<= 1;
2171 s = bc_num_mul(&copy, &copy, &copy, powrdx);
2172 if (s) goto err;
2173
2174 if (pow & 1) {
2175 resrdx += powrdx;
2176 s = bc_num_mul(c, &copy, c, resrdx);
2177 if (s) goto err;
2178 }
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002179 // Not needed: bc_num_mul() has a check for ^C:
2180 //if (G_interrupt) {
2181 // s = BC_STATUS_FAILURE;
2182 // goto err;
2183 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002184 }
2185
2186 if (neg) {
2187 s = bc_num_inv(c, c, scale);
2188 if (s) goto err;
2189 }
2190
Gavin Howard01055ba2018-11-03 11:00:21 -06002191 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2192
2193 // We can't use bc_num_clean() here.
2194 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2195 if (zero) bc_num_setToZero(c, scale);
2196
2197err:
2198 bc_num_free(&copy);
2199 return s;
2200}
2201
2202static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
2203 BcNumBinaryOp op, size_t req)
2204{
2205 BcStatus s;
2206 BcNum num2, *ptr_a, *ptr_b;
2207 bool init = false;
2208
2209 if (c == a) {
2210 ptr_a = &num2;
2211 memcpy(ptr_a, c, sizeof(BcNum));
2212 init = true;
2213 }
2214 else
2215 ptr_a = a;
2216
2217 if (c == b) {
2218 ptr_b = &num2;
2219 if (c != a) {
2220 memcpy(ptr_b, c, sizeof(BcNum));
2221 init = true;
2222 }
2223 }
2224 else
2225 ptr_b = b;
2226
2227 if (init)
2228 bc_num_init(c, req);
2229 else
2230 bc_num_expand(c, req);
2231
2232 s = op(ptr_a, ptr_b, c, scale);
2233
2234 if (init) bc_num_free(&num2);
2235
2236 return s;
2237}
2238
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002239static void bc_num_printNewline(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002240{
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002241 if (G.prog.nchars == G.prog.len - 1) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002242 bb_putchar('\\');
2243 bb_putchar('\n');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002244 G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06002245 }
2246}
2247
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002248#if ENABLE_DC
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002249static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
Gavin Howard01055ba2018-11-03 11:00:21 -06002250{
Denys Vlasenko2a8ad482018-12-08 21:56:37 +01002251 (void) radix;
Denys Vlasenko00d77792018-11-30 23:13:42 +01002252 bb_putchar((char) num);
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002253 G.prog.nchars += width;
Gavin Howard01055ba2018-11-03 11:00:21 -06002254}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002255#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002256
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002257static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
Gavin Howard01055ba2018-11-03 11:00:21 -06002258{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002259 size_t exp, pow;
Gavin Howard01055ba2018-11-03 11:00:21 -06002260
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002261 bc_num_printNewline();
Denys Vlasenko00d77792018-11-30 23:13:42 +01002262 bb_putchar(radix ? '.' : ' ');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002263 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06002264
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002265 bc_num_printNewline();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002266 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
2267 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002268
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002269 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002270 size_t dig;
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002271 bc_num_printNewline();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002272 dig = num / pow;
2273 num -= dig * pow;
Denys Vlasenko00d77792018-11-30 23:13:42 +01002274 bb_putchar(((char) dig) + '0');
Gavin Howard01055ba2018-11-03 11:00:21 -06002275 }
2276}
2277
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002278static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
Gavin Howard01055ba2018-11-03 11:00:21 -06002279{
2280 if (radix) {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002281 bc_num_printNewline();
Denys Vlasenko00d77792018-11-30 23:13:42 +01002282 bb_putchar('.');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002283 G.prog.nchars += 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002284 }
2285
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002286 bc_num_printNewline();
Denys Vlasenko00d77792018-11-30 23:13:42 +01002287 bb_putchar(bb_hexdigits_upcase[num]);
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002288 G.prog.nchars += width;
Gavin Howard01055ba2018-11-03 11:00:21 -06002289}
2290
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002291static void bc_num_printDecimal(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06002292{
2293 size_t i, rdx = n->rdx - 1;
2294
Denys Vlasenko00d77792018-11-30 23:13:42 +01002295 if (n->neg) bb_putchar('-');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002296 G.prog.nchars += n->neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002297
2298 for (i = n->len - 1; i < n->len; --i)
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002299 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002300}
2301
Denys Vlasenko29301232018-12-11 15:29:32 +01002302static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
Gavin Howard01055ba2018-11-03 11:00:21 -06002303{
2304 BcStatus s;
2305 BcVec stack;
2306 BcNum intp, fracp, digit, frac_len;
2307 unsigned long dig, *ptr;
2308 size_t i;
2309 bool radix;
2310
2311 if (n->len == 0) {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002312 print(0, width, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01002313 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002314 }
2315
2316 bc_vec_init(&stack, sizeof(long), NULL);
2317 bc_num_init(&intp, n->len);
2318 bc_num_init(&fracp, n->rdx);
2319 bc_num_init(&digit, width);
2320 bc_num_init(&frac_len, BC_NUM_INT(n));
2321 bc_num_copy(&intp, n);
2322 bc_num_one(&frac_len);
2323
2324 bc_num_truncate(&intp, intp.rdx);
2325 s = bc_num_sub(n, &intp, &fracp, 0);
2326 if (s) goto err;
2327
2328 while (intp.len != 0) {
2329 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
2330 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01002331 s = zbc_num_ulong(&digit, &dig);
Gavin Howard01055ba2018-11-03 11:00:21 -06002332 if (s) goto err;
2333 bc_vec_push(&stack, &dig);
2334 }
2335
2336 for (i = 0; i < stack.len; ++i) {
2337 ptr = bc_vec_item_rev(&stack, i);
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002338 print(*ptr, width, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06002339 }
2340
2341 if (!n->rdx) goto err;
2342
2343 for (radix = true; frac_len.len <= n->rdx; radix = false) {
2344 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
2345 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01002346 s = zbc_num_ulong(&fracp, &dig);
Gavin Howard01055ba2018-11-03 11:00:21 -06002347 if (s) goto err;
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01002348 bc_num_ulong2num(&intp, dig);
Gavin Howard01055ba2018-11-03 11:00:21 -06002349 s = bc_num_sub(&fracp, &intp, &fracp, 0);
2350 if (s) goto err;
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002351 print(dig, width, radix);
Gavin Howard01055ba2018-11-03 11:00:21 -06002352 s = bc_num_mul(&frac_len, base, &frac_len, 0);
2353 if (s) goto err;
2354 }
2355
2356err:
2357 bc_num_free(&frac_len);
2358 bc_num_free(&digit);
2359 bc_num_free(&fracp);
2360 bc_num_free(&intp);
2361 bc_vec_free(&stack);
Denys Vlasenko29301232018-12-11 15:29:32 +01002362 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002363}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002364#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002365# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002366#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002367
Denys Vlasenko29301232018-12-11 15:29:32 +01002368static BC_STATUS zbc_num_printBase(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06002369{
2370 BcStatus s;
2371 size_t width, i;
2372 BcNumDigitOp print;
2373 bool neg = n->neg;
2374
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002375 if (neg) {
2376 bb_putchar('-');
2377 G.prog.nchars++;
2378 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002379
2380 n->neg = false;
2381
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002382 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002383 width = 1;
2384 print = bc_num_printHex;
2385 }
2386 else {
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002387 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
2388 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002389 print = bc_num_printDigits;
2390 }
2391
Denys Vlasenko29301232018-12-11 15:29:32 +01002392 s = zbc_num_printNum(n, &G.prog.ob, width, print);
Gavin Howard01055ba2018-11-03 11:00:21 -06002393 n->neg = neg;
2394
Denys Vlasenko29301232018-12-11 15:29:32 +01002395 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002396}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002397#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002398# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002399#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002400
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002401#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01002402static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
Gavin Howard01055ba2018-11-03 11:00:21 -06002403{
Denys Vlasenko29301232018-12-11 15:29:32 +01002404 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
Gavin Howard01055ba2018-11-03 11:00:21 -06002405}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01002406#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002407# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01002408#endif
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002409#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002410
Denys Vlasenko9311e012018-12-10 11:54:18 +01002411static bool bc_num_strValid(const char *val, size_t base)
2412{
2413 BcDig b;
2414 bool radix;
2415
2416 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2417 radix = false;
2418 for (;;) {
2419 BcDig c = *val++;
2420 if (c == '\0')
2421 break;
2422 if (c == '.') {
2423 if (radix) return false;
2424 radix = true;
2425 continue;
2426 }
2427 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2428 return false;
2429 }
2430 return true;
2431}
2432
2433// Note: n is already "bc_num_zero()"ed,
2434// leading zeroes in "val" are removed
2435static void bc_num_parseDecimal(BcNum *n, const char *val)
2436{
2437 size_t len, i;
2438 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002439
2440 len = strlen(val);
2441 if (len == 0)
2442 return;
2443
Denys Vlasenko9311e012018-12-10 11:54:18 +01002444 bc_num_expand(n, len);
2445
2446 ptr = strchr(val, '.');
2447
2448 n->rdx = 0;
2449 if (ptr != NULL)
2450 n->rdx = (size_t)((val + len) - (ptr + 1));
2451
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002452 for (i = 0; val[i]; ++i) {
2453 if (val[i] != '0' && val[i] != '.') {
2454 // Not entirely zero value - convert it, and exit
2455 i = len - 1;
2456 for (;;) {
2457 n->num[n->len] = val[i] - '0';
2458 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002459 skip_dot:
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002460 if ((ssize_t)--i == (ssize_t)-1) break;
2461 if (val[i] == '.') goto skip_dot;
2462 }
2463 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002464 }
2465 }
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002466 // if this is reached, the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002467}
2468
2469// Note: n is already "bc_num_zero()"ed,
2470// leading zeroes in "val" are removed
2471static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2472{
2473 BcStatus s;
2474 BcNum temp, mult, result;
2475 BcDig c = '\0';
2476 unsigned long v;
2477 size_t i, digits;
2478
2479 for (i = 0; ; ++i) {
2480 if (val[i] == '\0')
2481 return;
2482 if (val[i] != '.' && val[i] != '0')
2483 break;
2484 }
2485
2486 bc_num_init_DEF_SIZE(&temp);
2487 bc_num_init_DEF_SIZE(&mult);
2488
2489 for (;;) {
2490 c = *val++;
2491 if (c == '\0') goto int_err;
2492 if (c == '.') break;
2493
2494 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2495
2496 s = bc_num_mul(n, base, &mult, 0);
2497 if (s) goto int_err;
2498 bc_num_ulong2num(&temp, v);
2499 s = bc_num_add(&mult, &temp, n, 0);
2500 if (s) goto int_err;
2501 }
2502
2503 bc_num_init(&result, base->len);
2504 //bc_num_zero(&result); - already is
2505 bc_num_one(&mult);
2506
2507 digits = 0;
2508 for (;;) {
2509 c = *val++;
2510 if (c == '\0') break;
2511 digits++;
2512
2513 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2514
2515 s = bc_num_mul(&result, base, &result, 0);
2516 if (s) goto err;
2517 bc_num_ulong2num(&temp, v);
2518 s = bc_num_add(&result, &temp, &result, 0);
2519 if (s) goto err;
2520 s = bc_num_mul(&mult, base, &mult, 0);
2521 if (s) goto err;
2522 }
2523
2524 s = bc_num_div(&result, &mult, &result, digits);
2525 if (s) goto err;
2526 s = bc_num_add(n, &result, n, digits);
2527 if (s) goto err;
2528
2529 if (n->len != 0) {
2530 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2531 } else
2532 bc_num_zero(n);
2533
2534err:
2535 bc_num_free(&result);
2536int_err:
2537 bc_num_free(&mult);
2538 bc_num_free(&temp);
2539}
2540
Denys Vlasenko29301232018-12-11 15:29:32 +01002541static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002542 size_t base_t)
2543{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002544 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002545 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002546
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002547 bc_num_zero(n);
2548 while (*val == '0') val++;
2549
Gavin Howard01055ba2018-11-03 11:00:21 -06002550 if (base_t == 10)
2551 bc_num_parseDecimal(n, val);
2552 else
2553 bc_num_parseBase(n, val, base);
2554
Denys Vlasenko29301232018-12-11 15:29:32 +01002555 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002556}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002557#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002558# define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002559#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002560
Denys Vlasenko29301232018-12-11 15:29:32 +01002561static BC_STATUS zbc_num_print(BcNum *n, bool newline)
Gavin Howard01055ba2018-11-03 11:00:21 -06002562{
2563 BcStatus s = BC_STATUS_SUCCESS;
2564
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002565 bc_num_printNewline();
Gavin Howard01055ba2018-11-03 11:00:21 -06002566
2567 if (n->len == 0) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002568 bb_putchar('0');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002569 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06002570 }
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002571 else if (G.prog.ob_t == 10)
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002572 bc_num_printDecimal(n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002573 else
Denys Vlasenko29301232018-12-11 15:29:32 +01002574 s = zbc_num_printBase(n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002575
2576 if (newline) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002577 bb_putchar('\n');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002578 G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06002579 }
2580
Denys Vlasenko29301232018-12-11 15:29:32 +01002581 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002582}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002583#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002584# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002585#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002586
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002587static FAST_FUNC BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002588{
2589 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
2590 (void) scale;
2591 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
2592}
2593
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002594static FAST_FUNC BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002595{
2596 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
2597 (void) scale;
2598 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
2599}
2600
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002601static FAST_FUNC BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002602{
2603 size_t req = BC_NUM_MREQ(a, b, scale);
2604 return bc_num_binary(a, b, c, scale, bc_num_m, req);
2605}
2606
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002607static FAST_FUNC BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002608{
2609 size_t req = BC_NUM_MREQ(a, b, scale);
2610 return bc_num_binary(a, b, c, scale, bc_num_d, req);
2611}
2612
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002613static FAST_FUNC BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002614{
2615 size_t req = BC_NUM_MREQ(a, b, scale);
2616 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
2617}
2618
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002619static FAST_FUNC BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002620{
2621 return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1);
2622}
2623
2624static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
2625{
2626 BcStatus s;
2627 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2628 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2629 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2630
2631 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2632 bc_num_expand(b, req);
2633
2634 if (a->len == 0) {
2635 bc_num_setToZero(b, scale);
2636 return BC_STATUS_SUCCESS;
2637 }
2638 else if (a->neg)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002639 return bc_error("negative number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002640 else if (BC_NUM_ONE(a)) {
2641 bc_num_one(b);
2642 bc_num_extend(b, scale);
2643 return BC_STATUS_SUCCESS;
2644 }
2645
2646 scale = BC_MAX(scale, a->rdx) + 1;
2647 len = a->len + scale;
2648
2649 bc_num_init(&num1, len);
2650 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002651 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002652
2653 bc_num_one(&half);
2654 half.num[0] = 5;
2655 half.rdx = 1;
2656
2657 bc_num_init(&f, len);
2658 bc_num_init(&fprime, len);
2659
2660 x0 = &num1;
2661 x1 = &num2;
2662
2663 bc_num_one(x0);
2664 pow = BC_NUM_INT(a);
2665
2666 if (pow) {
2667
2668 if (pow & 1)
2669 x0->num[0] = 2;
2670 else
2671 x0->num[0] = 6;
2672
2673 pow -= 2 - (pow & 1);
2674
2675 bc_num_extend(x0, pow);
2676
2677 // Make sure to move the radix back.
2678 x0->rdx -= pow;
2679 }
2680
2681 x0->rdx = digs = digs1 = 0;
2682 resrdx = scale + 2;
2683 len = BC_NUM_INT(x0) + resrdx - 1;
2684
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002685 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002686
2687 s = bc_num_div(a, x0, &f, resrdx);
2688 if (s) goto err;
2689 s = bc_num_add(x0, &f, &fprime, resrdx);
2690 if (s) goto err;
2691 s = bc_num_mul(&fprime, &half, x1, resrdx);
2692 if (s) goto err;
2693
2694 cmp = bc_num_cmp(x1, x0);
2695 digs = x1->len - (unsigned long long) llabs(cmp);
2696
2697 if (cmp == cmp2 && digs == digs1)
2698 times += 1;
2699 else
2700 times = 0;
2701
2702 resrdx += times > 4;
2703
2704 cmp2 = cmp1;
2705 cmp1 = cmp;
2706 digs1 = digs;
2707
2708 temp = x0;
2709 x0 = x1;
2710 x1 = temp;
2711 }
2712
Gavin Howard01055ba2018-11-03 11:00:21 -06002713 bc_num_copy(b, x0);
2714 scale -= 1;
2715 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2716
2717err:
2718 bc_num_free(&fprime);
2719 bc_num_free(&f);
2720 bc_num_free(&half);
2721 bc_num_free(&num2);
2722 bc_num_free(&num1);
2723 return s;
2724}
2725
2726static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
2727 size_t scale)
2728{
2729 BcStatus s;
2730 BcNum num2, *ptr_a;
2731 bool init = false;
2732 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2733
2734 if (c == a) {
2735 memcpy(&num2, c, sizeof(BcNum));
2736 ptr_a = &num2;
2737 bc_num_init(c, len);
2738 init = true;
2739 }
2740 else {
2741 ptr_a = a;
2742 bc_num_expand(c, len);
2743 }
2744
2745 s = bc_num_r(ptr_a, b, c, d, scale, ts);
2746
2747 if (init) bc_num_free(&num2);
2748
2749 return s;
2750}
2751
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002752#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002753static BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
2754{
2755 BcStatus s;
2756 BcNum base, exp, two, temp;
2757
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002758 if (c->len == 0)
2759 return bc_error("divide by zero");
2760 if (a->rdx || b->rdx || c->rdx)
2761 return bc_error("non integer number");
2762 if (b->neg)
2763 return bc_error("negative number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002764
2765 bc_num_expand(d, c->len);
2766 bc_num_init(&base, c->len);
2767 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002768 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002769 bc_num_init(&temp, b->len);
2770
2771 bc_num_one(&two);
2772 two.num[0] = 2;
2773 bc_num_one(d);
2774
2775 s = bc_num_rem(a, c, &base, 0);
2776 if (s) goto err;
2777 bc_num_copy(&exp, b);
2778
2779 while (exp.len != 0) {
2780
2781 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
2782 if (s) goto err;
2783
2784 if (BC_NUM_ONE(&temp)) {
2785 s = bc_num_mul(d, &base, &temp, 0);
2786 if (s) goto err;
2787 s = bc_num_rem(&temp, c, d, 0);
2788 if (s) goto err;
2789 }
2790
2791 s = bc_num_mul(&base, &base, &temp, 0);
2792 if (s) goto err;
2793 s = bc_num_rem(&temp, c, &base, 0);
2794 if (s) goto err;
2795 }
2796
2797err:
2798 bc_num_free(&temp);
2799 bc_num_free(&two);
2800 bc_num_free(&exp);
2801 bc_num_free(&base);
2802 return s;
2803}
2804#endif // ENABLE_DC
2805
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002806#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002807static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002808{
2809 BcId a;
2810 size_t i;
2811
2812 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002813 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002814 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002815 }
2816
2817 a.idx = var;
2818 a.name = name;
2819
2820 bc_vec_push(&f->autos, &a);
2821
Denys Vlasenko29301232018-12-11 15:29:32 +01002822 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002823}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002824#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002825# define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002826#endif
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002827#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002828
2829static void bc_func_init(BcFunc *f)
2830{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002831 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002832 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2833 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2834 f->nparams = 0;
2835}
2836
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002837static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002838{
2839 BcFunc *f = (BcFunc *) func;
2840 bc_vec_free(&f->code);
2841 bc_vec_free(&f->autos);
2842 bc_vec_free(&f->labels);
2843}
2844
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002845static void bc_array_expand(BcVec *a, size_t len);
2846
Gavin Howard01055ba2018-11-03 11:00:21 -06002847static void bc_array_init(BcVec *a, bool nums)
2848{
2849 if (nums)
2850 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2851 else
2852 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2853 bc_array_expand(a, 1);
2854}
2855
Gavin Howard01055ba2018-11-03 11:00:21 -06002856static void bc_array_expand(BcVec *a, size_t len)
2857{
2858 BcResultData data;
2859
2860 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2861 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002862 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002863 bc_vec_push(a, &data.n);
2864 }
2865 }
2866 else {
2867 while (len > a->len) {
2868 bc_array_init(&data.v, true);
2869 bc_vec_push(a, &data.v);
2870 }
2871 }
2872}
2873
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002874static void bc_array_copy(BcVec *d, const BcVec *s)
2875{
2876 size_t i;
2877
2878 bc_vec_pop_all(d);
2879 bc_vec_expand(d, s->cap);
2880 d->len = s->len;
2881
2882 for (i = 0; i < s->len; ++i) {
2883 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2884 bc_num_init(dnum, snum->len);
2885 bc_num_copy(dnum, snum);
2886 }
2887}
2888
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002889static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002890{
2891 free(*((char **) string));
2892}
2893
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002894#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002895static void bc_result_copy(BcResult *d, BcResult *src)
2896{
2897 d->t = src->t;
2898
2899 switch (d->t) {
2900
2901 case BC_RESULT_TEMP:
2902 case BC_RESULT_IBASE:
2903 case BC_RESULT_SCALE:
2904 case BC_RESULT_OBASE:
2905 {
2906 bc_num_init(&d->d.n, src->d.n.len);
2907 bc_num_copy(&d->d.n, &src->d.n);
2908 break;
2909 }
2910
2911 case BC_RESULT_VAR:
2912 case BC_RESULT_ARRAY:
2913 case BC_RESULT_ARRAY_ELEM:
2914 {
2915 d->d.id.name = xstrdup(src->d.id.name);
2916 break;
2917 }
2918
2919 case BC_RESULT_CONSTANT:
2920 case BC_RESULT_LAST:
2921 case BC_RESULT_ONE:
2922 case BC_RESULT_STR:
2923 {
2924 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2925 break;
2926 }
2927 }
2928}
2929#endif // ENABLE_DC
2930
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002931static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002932{
2933 BcResult *r = (BcResult *) result;
2934
2935 switch (r->t) {
2936
2937 case BC_RESULT_TEMP:
2938 case BC_RESULT_IBASE:
2939 case BC_RESULT_SCALE:
2940 case BC_RESULT_OBASE:
2941 {
2942 bc_num_free(&r->d.n);
2943 break;
2944 }
2945
2946 case BC_RESULT_VAR:
2947 case BC_RESULT_ARRAY:
2948 case BC_RESULT_ARRAY_ELEM:
2949 {
2950 free(r->d.id.name);
2951 break;
2952 }
2953
2954 default:
2955 {
2956 // Do nothing.
2957 break;
2958 }
2959 }
2960}
2961
2962static void bc_lex_lineComment(BcLex *l)
2963{
2964 l->t.t = BC_LEX_WHITESPACE;
2965 while (l->i < l->len && l->buf[l->i++] != '\n');
2966 --l->i;
2967}
2968
2969static void bc_lex_whitespace(BcLex *l)
2970{
2971 char c;
2972 l->t.t = BC_LEX_WHITESPACE;
2973 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
2974}
2975
Denys Vlasenko29301232018-12-11 15:29:32 +01002976static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002977{
2978 const char *buf = l->buf + l->i;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002979 size_t len, bslashes, i, ccnt;
2980 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002981
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002982 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002983 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002984 bslashes = 0;
2985 ccnt = i = 0;
2986 for (;;) {
2987 char c = buf[i];
2988 if (c == '\0')
2989 break;
2990 if (c == '\\' && buf[i + 1] == '\n') {
2991 i += 2;
2992 bslashes++;
2993 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002994 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002995 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2996 if (c != '.') break;
2997 // if '.' was already seen, stop on second one:
2998 if (pt) break;
2999 pt = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003000 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01003001 // buf[i] is one of "0-9A-F."
3002 i++;
3003 if (c != '.')
3004 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003005 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01003006 //i is buf[i] index of the first not-yet-parsed char
3007 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003008
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01003009 //ccnt is the number of chars in the number string, excluding possible
3010 //trailing "." and possible following trailing "\<newline>"(s).
3011 len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination
3012
Denys Vlasenko64074a12018-12-07 15:50:14 +01003013 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
3014 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
3015 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01003016 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003017 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003018
Denys Vlasenko7d628012018-12-04 21:46:47 +01003019 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01003020 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06003021 bc_vec_push(&l->t.v, &start);
3022
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01003023 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003024 // If we have hit a backslash, skip it. We don't have
3025 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01003026 if (*buf == '\\') {
3027 buf += 2;
3028 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06003029 continue;
3030 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01003031 bc_vec_push(&l->t.v, buf);
3032 buf++;
3033 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06003034 }
3035
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003036 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003037
Denys Vlasenko29301232018-12-11 15:29:32 +01003038 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003039}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01003040#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003041# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01003042#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003043
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003044static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003045{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003046 size_t i;
3047 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003048
3049 l->t.t = BC_LEX_NAME;
3050
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003051 i = 0;
3052 buf = l->buf + l->i - 1;
3053 for (;;) {
3054 char c = buf[i];
3055 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
3056 i++;
3057 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003058
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003059#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01003060 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3061 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3062 if (i > BC_MAX_STRING)
3063 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
3064 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003065#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003066 bc_vec_string(&l->t.v, i, buf);
3067
3068 // Increment the index. We minus 1 because it has already been incremented.
3069 l->i += i - 1;
3070
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003071 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003072}
3073
3074static void bc_lex_init(BcLex *l, BcLexNext next)
3075{
3076 l->next = next;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003077 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003078}
3079
3080static void bc_lex_free(BcLex *l)
3081{
3082 bc_vec_free(&l->t.v);
3083}
3084
Denys Vlasenko0409ad32018-12-05 16:39:22 +01003085static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003086{
Denys Vlasenko5318f812018-12-05 17:48:01 +01003087 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003088 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06003089}
3090
3091static BcStatus bc_lex_next(BcLex *l)
3092{
3093 BcStatus s;
3094
3095 l->t.last = l->t.t;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003096 if (l->t.last == BC_LEX_EOF) return bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06003097
3098 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003099 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003100 l->t.t = BC_LEX_EOF;
3101
3102 l->newline = (l->i == l->len);
3103 if (l->newline) return BC_STATUS_SUCCESS;
3104
3105 // Loop until failure or we don't have whitespace. This
3106 // is so the parser doesn't get inundated with whitespace.
3107 do {
3108 s = l->next(l);
3109 } while (!s && l->t.t == BC_LEX_WHITESPACE);
3110
3111 return s;
3112}
3113
3114static BcStatus bc_lex_text(BcLex *l, const char *text)
3115{
3116 l->buf = text;
3117 l->i = 0;
3118 l->len = strlen(text);
3119 l->t.t = l->t.last = BC_LEX_INVALID;
3120 return bc_lex_next(l);
3121}
3122
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003123#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06003124static BcStatus bc_lex_identifier(BcLex *l)
3125{
3126 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003127 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003128 const char *buf = l->buf + l->i - 1;
3129
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003130 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3131 const char *keyword8 = bc_lex_kws[i].name8;
3132 unsigned j = 0;
3133 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3134 j++;
3135 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003137 if (keyword8[j] != '\0')
3138 continue;
3139 match:
3140 // buf starts with keyword bc_lex_kws[i]
3141 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003142 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003143 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003144 if (s) return s;
3145 }
3146
3147 // We minus 1 because the index has already been incremented.
3148 l->i += j - 1;
3149 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 }
3151
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003152 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003153
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003154 if (l->t.v.len > 2) {
3155 // Prevent this:
3156 // >>> qwe=1
3157 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3158 // '
3159 unsigned len = strchrnul(buf, '\n') - buf;
3160 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3161 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003162
3163 return s;
3164}
3165
3166static BcStatus bc_lex_string(BcLex *l)
3167{
3168 size_t len, nls = 0, i = l->i;
3169 char c;
3170
3171 l->t.t = BC_LEX_STR;
3172
3173 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n');
3174
3175 if (c == '\0') {
3176 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003177 return bc_error("string end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003178 }
3179
3180 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003181 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3182 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3183 if (len > BC_MAX_STRING)
3184 return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]");
3185 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003186 bc_vec_string(&l->t.v, len, l->buf + l->i);
3187
3188 l->i = i + 1;
3189 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003190 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003191
3192 return BC_STATUS_SUCCESS;
3193}
3194
3195static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3196{
3197 if (l->buf[l->i] == '=') {
3198 ++l->i;
3199 l->t.t = with;
3200 }
3201 else
3202 l->t.t = without;
3203}
3204
Denys Vlasenko29301232018-12-11 15:29:32 +01003205static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003206{
3207 size_t i, nls = 0;
3208 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003209
3210 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003211 i = ++l->i;
3212 for (;;) {
3213 char c = buf[i];
3214 check_star:
3215 if (c == '*') {
3216 c = buf[++i];
3217 if (c == '/')
3218 break;
3219 goto check_star;
3220 }
3221 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003223 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003224 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003225 nls += (c == '\n');
3226 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003227 }
3228
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003229 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003230 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003231 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003232
Denys Vlasenko29301232018-12-11 15:29:32 +01003233 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003234}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003235#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003236# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003237#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003238
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003239static FAST_FUNC BcStatus bc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003240{
3241 BcStatus s = BC_STATUS_SUCCESS;
3242 char c = l->buf[l->i++], c2;
3243
3244 // This is the workhorse of the lexer.
3245 switch (c) {
3246
3247 case '\0':
3248 case '\n':
3249 {
3250 l->newline = true;
3251 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3252 break;
3253 }
3254
3255 case '\t':
3256 case '\v':
3257 case '\f':
3258 case '\r':
3259 case ' ':
3260 {
3261 bc_lex_whitespace(l);
3262 break;
3263 }
3264
3265 case '!':
3266 {
3267 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3268
3269 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003270 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Gavin Howard01055ba2018-11-03 11:00:21 -06003271 if (s) return s;
3272 }
3273
3274 break;
3275 }
3276
3277 case '"':
3278 {
3279 s = bc_lex_string(l);
3280 break;
3281 }
3282
3283 case '#':
3284 {
Denys Vlasenko00646792018-12-05 18:12:27 +01003285 s = bc_POSIX_does_not_allow("'#' script comments");
Gavin Howard01055ba2018-11-03 11:00:21 -06003286 if (s) return s;
3287
3288 bc_lex_lineComment(l);
3289
3290 break;
3291 }
3292
3293 case '%':
3294 {
3295 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3296 break;
3297 }
3298
3299 case '&':
3300 {
3301 c2 = l->buf[l->i];
3302 if (c2 == '&') {
3303
Denys Vlasenko00646792018-12-05 18:12:27 +01003304 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Gavin Howard01055ba2018-11-03 11:00:21 -06003305 if (s) return s;
3306
3307 ++l->i;
3308 l->t.t = BC_LEX_OP_BOOL_AND;
3309 }
3310 else {
3311 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003312 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003313 }
3314
3315 break;
3316 }
3317
3318 case '(':
3319 case ')':
3320 {
3321 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3322 break;
3323 }
3324
3325 case '*':
3326 {
3327 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3328 break;
3329 }
3330
3331 case '+':
3332 {
3333 c2 = l->buf[l->i];
3334 if (c2 == '+') {
3335 ++l->i;
3336 l->t.t = BC_LEX_OP_INC;
3337 }
3338 else
3339 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3340 break;
3341 }
3342
3343 case ',':
3344 {
3345 l->t.t = BC_LEX_COMMA;
3346 break;
3347 }
3348
3349 case '-':
3350 {
3351 c2 = l->buf[l->i];
3352 if (c2 == '-') {
3353 ++l->i;
3354 l->t.t = BC_LEX_OP_DEC;
3355 }
3356 else
3357 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3358 break;
3359 }
3360
3361 case '.':
3362 {
3363 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003364 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003365 else {
3366 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003367 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003368 }
3369 break;
3370 }
3371
3372 case '/':
3373 {
3374 c2 = l->buf[l->i];
3375 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003376 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003377 else
3378 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3379 break;
3380 }
3381
3382 case '0':
3383 case '1':
3384 case '2':
3385 case '3':
3386 case '4':
3387 case '5':
3388 case '6':
3389 case '7':
3390 case '8':
3391 case '9':
3392 case 'A':
3393 case 'B':
3394 case 'C':
3395 case 'D':
3396 case 'E':
3397 case 'F':
3398 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003399 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003400 break;
3401 }
3402
3403 case ';':
3404 {
3405 l->t.t = BC_LEX_SCOLON;
3406 break;
3407 }
3408
3409 case '<':
3410 {
3411 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3412 break;
3413 }
3414
3415 case '=':
3416 {
3417 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3418 break;
3419 }
3420
3421 case '>':
3422 {
3423 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3424 break;
3425 }
3426
3427 case '[':
3428 case ']':
3429 {
3430 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3431 break;
3432 }
3433
3434 case '\\':
3435 {
3436 if (l->buf[l->i] == '\n') {
3437 l->t.t = BC_LEX_WHITESPACE;
3438 ++l->i;
3439 }
3440 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003441 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003442 break;
3443 }
3444
3445 case '^':
3446 {
3447 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3448 break;
3449 }
3450
3451 case 'a':
3452 case 'b':
3453 case 'c':
3454 case 'd':
3455 case 'e':
3456 case 'f':
3457 case 'g':
3458 case 'h':
3459 case 'i':
3460 case 'j':
3461 case 'k':
3462 case 'l':
3463 case 'm':
3464 case 'n':
3465 case 'o':
3466 case 'p':
3467 case 'q':
3468 case 'r':
3469 case 's':
3470 case 't':
3471 case 'u':
3472 case 'v':
3473 case 'w':
3474 case 'x':
3475 case 'y':
3476 case 'z':
3477 {
3478 s = bc_lex_identifier(l);
3479 break;
3480 }
3481
3482 case '{':
3483 case '}':
3484 {
3485 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3486 break;
3487 }
3488
3489 case '|':
3490 {
3491 c2 = l->buf[l->i];
3492
3493 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003494 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Gavin Howard01055ba2018-11-03 11:00:21 -06003495 if (s) return s;
3496
3497 ++l->i;
3498 l->t.t = BC_LEX_OP_BOOL_OR;
3499 }
3500 else {
3501 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003502 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003503 }
3504
3505 break;
3506 }
3507
3508 default:
3509 {
3510 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003511 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003512 break;
3513 }
3514 }
3515
3516 return s;
3517}
3518#endif // ENABLE_BC
3519
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003520#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003521static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003522{
Gavin Howard01055ba2018-11-03 11:00:21 -06003523 if (isspace(l->buf[l->i - 1])) {
3524 bc_lex_whitespace(l);
3525 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003526 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003527 RETURN_STATUS(bc_error("extended register"));
3528 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003529 }
3530 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003531 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003532 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003533 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003534 l->t.t = BC_LEX_NAME;
3535 }
3536
Denys Vlasenko29301232018-12-11 15:29:32 +01003537 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003538}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003539#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003540# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003541#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003542
Denys Vlasenko29301232018-12-11 15:29:32 +01003543static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003544{
3545 size_t depth = 1, nls = 0, i = l->i;
3546 char c;
3547
3548 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003549 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003550
3551 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3552
3553 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3554 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3555 nls += (c == '\n');
3556
3557 if (depth) bc_vec_push(&l->t.v, &c);
3558 }
3559
3560 if (c == '\0') {
3561 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003562 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003563 }
3564
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003565 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003566 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3567 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3568 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003569 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003570 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003571
3572 l->i = i;
3573 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003574 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003575
Denys Vlasenko29301232018-12-11 15:29:32 +01003576 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003577}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003578#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003579# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003580#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003581
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003582static FAST_FUNC BcStatus dc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003583{
3584 BcStatus s = BC_STATUS_SUCCESS;
3585 char c = l->buf[l->i++], c2;
3586 size_t i;
3587
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003588 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3589 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko29301232018-12-11 15:29:32 +01003590 return zdc_lex_register(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003591 }
3592
3593 if (c >= '%' && c <= '~' &&
3594 (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID)
3595 {
3596 return s;
3597 }
3598
3599 // This is the workhorse of the lexer.
3600 switch (c) {
3601
3602 case '\0':
3603 {
3604 l->t.t = BC_LEX_EOF;
3605 break;
3606 }
3607
3608 case '\n':
3609 case '\t':
3610 case '\v':
3611 case '\f':
3612 case '\r':
3613 case ' ':
3614 {
3615 l->newline = (c == '\n');
3616 bc_lex_whitespace(l);
3617 break;
3618 }
3619
3620 case '!':
3621 {
3622 c2 = l->buf[l->i];
3623
3624 if (c2 == '=')
3625 l->t.t = BC_LEX_OP_REL_NE;
3626 else if (c2 == '<')
3627 l->t.t = BC_LEX_OP_REL_LE;
3628 else if (c2 == '>')
3629 l->t.t = BC_LEX_OP_REL_GE;
3630 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003631 return bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003632
3633 ++l->i;
3634 break;
3635 }
3636
3637 case '#':
3638 {
3639 bc_lex_lineComment(l);
3640 break;
3641 }
3642
3643 case '.':
3644 {
3645 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003646 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003647 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003648 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003649 break;
3650 }
3651
3652 case '0':
3653 case '1':
3654 case '2':
3655 case '3':
3656 case '4':
3657 case '5':
3658 case '6':
3659 case '7':
3660 case '8':
3661 case '9':
3662 case 'A':
3663 case 'B':
3664 case 'C':
3665 case 'D':
3666 case 'E':
3667 case 'F':
3668 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003669 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003670 break;
3671 }
3672
3673 case '[':
3674 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003675 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003676 break;
3677 }
3678
3679 default:
3680 {
3681 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003682 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003683 break;
3684 }
3685 }
3686
3687 return s;
3688}
3689#endif // ENABLE_DC
3690
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003691static void bc_program_addFunc(char *name, size_t *idx);
3692
Gavin Howard01055ba2018-11-03 11:00:21 -06003693static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3694{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003695 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003696 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003697}
3698
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003699#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3700
Gavin Howard01055ba2018-11-03 11:00:21 -06003701static void bc_parse_pushName(BcParse *p, char *name)
3702{
3703 size_t i = 0, len = strlen(name);
3704
3705 for (; i < len; ++i) bc_parse_push(p, name[i]);
3706 bc_parse_push(p, BC_PARSE_STREND);
3707
3708 free(name);
3709}
3710
3711static void bc_parse_pushIndex(BcParse *p, size_t idx)
3712{
3713 unsigned char amt, i, nums[sizeof(size_t)];
3714
3715 for (amt = 0; idx; ++amt) {
3716 nums[amt] = (char) idx;
3717 idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT;
3718 }
3719
3720 bc_parse_push(p, amt);
3721 for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]);
3722}
3723
3724static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3725{
3726 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003727 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003728
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003729 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003730
3731 bc_parse_push(p, BC_INST_NUM);
3732 bc_parse_pushIndex(p, idx);
3733
3734 ++(*nexs);
3735 (*prev) = BC_INST_NUM;
3736}
3737
3738static BcStatus bc_parse_text(BcParse *p, const char *text)
3739{
3740 BcStatus s;
3741
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003742 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003743
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003744 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003745 p->l.t.t = BC_LEX_INVALID;
3746 s = p->parse(p);
3747 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003748 if (!BC_PARSE_CAN_EXEC(p))
3749 return bc_error("file is not executable");
Gavin Howard01055ba2018-11-03 11:00:21 -06003750 }
3751
3752 return bc_lex_text(&p->l, text);
3753}
3754
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003755// Called when parsing or execution detects a failure,
3756// resets execution structures.
3757static void bc_program_reset(void)
3758{
3759 BcFunc *f;
3760 BcInstPtr *ip;
3761
3762 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3763 bc_vec_pop_all(&G.prog.results);
3764
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003765 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003766 ip = bc_vec_top(&G.prog.stack);
3767 ip->idx = f->code.len;
3768}
3769
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003770#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003771 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003772
Denys Vlasenkod38af482018-12-04 19:11:02 +01003773// Called when bc/dc_parse_parse() detects a failure,
3774// resets parsing structures.
3775static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003776{
3777 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003778 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003779 bc_vec_pop_all(&p->func->code);
3780 bc_vec_pop_all(&p->func->autos);
3781 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003782
3783 bc_parse_updateFunc(p, BC_PROG_MAIN);
3784 }
3785
3786 p->l.i = p->l.len;
3787 p->l.t.t = BC_LEX_EOF;
3788 p->auto_part = (p->nbraces = 0);
3789
3790 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003791 bc_vec_pop_all(&p->exits);
3792 bc_vec_pop_all(&p->conds);
3793 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003794
Denys Vlasenkod38af482018-12-04 19:11:02 +01003795 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003796}
3797
3798static void bc_parse_free(BcParse *p)
3799{
3800 bc_vec_free(&p->flags);
3801 bc_vec_free(&p->exits);
3802 bc_vec_free(&p->conds);
3803 bc_vec_free(&p->ops);
3804 bc_lex_free(&p->l);
3805}
3806
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003807static void bc_parse_create(BcParse *p, size_t func,
Gavin Howard01055ba2018-11-03 11:00:21 -06003808 BcParseParse parse, BcLexNext next)
3809{
3810 memset(p, 0, sizeof(BcParse));
3811
3812 bc_lex_init(&p->l, next);
3813 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3814 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3815 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003816 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003817 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3818
3819 p->parse = parse;
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003820 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003821 bc_parse_updateFunc(p, func);
3822}
3823
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003824#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003825
3826#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3827#define BC_PARSE_LEAF(p, rparen) \
3828 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3829 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3830
3831// We can calculate the conversion between tokens and exprs by subtracting the
3832// position of the first operator in the lex enum and adding the position of the
3833// first in the expr enum. Note: This only works for binary operators.
3834#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3835
Gavin Howard01055ba2018-11-03 11:00:21 -06003836static BcStatus bc_parse_else(BcParse *p);
3837static BcStatus bc_parse_stmt(BcParse *p);
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003838static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003839static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Gavin Howard01055ba2018-11-03 11:00:21 -06003840
3841static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3842 size_t *nexprs, bool next)
3843{
3844 BcStatus s = BC_STATUS_SUCCESS;
3845 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003846 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3847 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003848
3849 while (p->ops.len > start) {
3850
3851 t = BC_PARSE_TOP_OP(p);
3852 if (t == BC_LEX_LPAREN) break;
3853
Denys Vlasenko65437582018-12-05 19:37:19 +01003854 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003855 if (l >= r && (l != r || !left)) break;
3856
3857 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3858 bc_vec_pop(&p->ops);
3859 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3860 }
3861
3862 bc_vec_push(&p->ops, &type);
3863 if (next) s = bc_lex_next(&p->l);
3864
3865 return s;
3866}
3867
3868static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
3869{
3870 BcLexType top;
3871
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003872 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003873 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003874 top = BC_PARSE_TOP_OP(p);
3875
3876 while (top != BC_LEX_LPAREN) {
3877
3878 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3879
3880 bc_vec_pop(&p->ops);
3881 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3882
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003883 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003884 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003885 top = BC_PARSE_TOP_OP(p);
3886 }
3887
3888 bc_vec_pop(&p->ops);
3889
3890 return bc_lex_next(&p->l);
3891}
3892
3893static BcStatus bc_parse_params(BcParse *p, uint8_t flags)
3894{
3895 BcStatus s;
3896 bool comma = false;
3897 size_t nparams;
3898
3899 s = bc_lex_next(&p->l);
3900 if (s) return s;
3901
3902 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
3903
3904 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3905 s = bc_parse_expr(p, flags, bc_parse_next_param);
3906 if (s) return s;
3907
3908 comma = p->l.t.t == BC_LEX_COMMA;
3909 if (comma) {
3910 s = bc_lex_next(&p->l);
3911 if (s) return s;
3912 }
3913 }
3914
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003915 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003916 bc_parse_push(p, BC_INST_CALL);
3917 bc_parse_pushIndex(p, nparams);
3918
3919 return BC_STATUS_SUCCESS;
3920}
3921
3922static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags)
3923{
3924 BcStatus s;
3925 BcId entry, *entry_ptr;
3926 size_t idx;
3927
3928 entry.name = name;
3929
3930 s = bc_parse_params(p, flags);
3931 if (s) goto err;
3932
3933 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003934 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003935 goto err;
3936 }
3937
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003938 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003939
3940 if (idx == BC_VEC_INVALID_IDX) {
3941 name = xstrdup(entry.name);
3942 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003943 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003944 free(entry.name);
3945 }
3946 else
3947 free(name);
3948
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003949 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003950 bc_parse_pushIndex(p, entry_ptr->idx);
3951
3952 return bc_lex_next(&p->l);
3953
3954err:
3955 free(name);
3956 return s;
3957}
3958
3959static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
3960{
3961 BcStatus s;
3962 char *name;
3963
3964 name = xstrdup(p->l.t.v.v);
3965 s = bc_lex_next(&p->l);
3966 if (s) goto err;
3967
3968 if (p->l.t.t == BC_LEX_LBRACKET) {
3969
3970 s = bc_lex_next(&p->l);
3971 if (s) goto err;
3972
3973 if (p->l.t.t == BC_LEX_RBRACKET) {
3974
3975 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003976 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003977 goto err;
3978 }
3979
3980 *type = BC_INST_ARRAY;
3981 }
3982 else {
3983
3984 *type = BC_INST_ARRAY_ELEM;
3985
3986 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3987 s = bc_parse_expr(p, flags, bc_parse_next_elem);
3988 if (s) goto err;
3989 }
3990
3991 s = bc_lex_next(&p->l);
3992 if (s) goto err;
3993 bc_parse_push(p, *type);
3994 bc_parse_pushName(p, name);
3995 }
3996 else if (p->l.t.t == BC_LEX_LPAREN) {
3997
3998 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003999 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004000 goto err;
4001 }
4002
4003 *type = BC_INST_CALL;
4004 s = bc_parse_call(p, name, flags);
4005 }
4006 else {
4007 *type = BC_INST_VAR;
4008 bc_parse_push(p, BC_INST_VAR);
4009 bc_parse_pushName(p, name);
4010 }
4011
4012 return s;
4013
4014err:
4015 free(name);
4016 return s;
4017}
4018
4019static BcStatus bc_parse_read(BcParse *p)
4020{
4021 BcStatus s;
4022
4023 s = bc_lex_next(&p->l);
4024 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004025 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004026
4027 s = bc_lex_next(&p->l);
4028 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004029 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004030
4031 bc_parse_push(p, BC_INST_READ);
4032
4033 return bc_lex_next(&p->l);
4034}
4035
4036static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
4037 BcInst *prev)
4038{
4039 BcStatus s;
4040
4041 s = bc_lex_next(&p->l);
4042 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004043 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004044
4045 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
4046
4047 s = bc_lex_next(&p->l);
4048 if (s) return s;
4049
4050 s = bc_parse_expr(p, flags, bc_parse_next_rel);
4051 if (s) return s;
4052
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004053 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004054
4055 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
4056 bc_parse_push(p, *prev);
4057
4058 return bc_lex_next(&p->l);
4059}
4060
4061static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
4062{
4063 BcStatus s;
4064
4065 s = bc_lex_next(&p->l);
4066 if (s) return s;
4067
4068 if (p->l.t.t != BC_LEX_LPAREN) {
4069 *type = BC_INST_SCALE;
4070 bc_parse_push(p, BC_INST_SCALE);
4071 return BC_STATUS_SUCCESS;
4072 }
4073
4074 *type = BC_INST_SCALE_FUNC;
4075 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
4076
4077 s = bc_lex_next(&p->l);
4078 if (s) return s;
4079
4080 s = bc_parse_expr(p, flags, bc_parse_next_rel);
4081 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004082 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004083 bc_parse_push(p, BC_INST_SCALE_FUNC);
4084
4085 return bc_lex_next(&p->l);
4086}
4087
4088static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
4089 size_t *nexprs, uint8_t flags)
4090{
4091 BcStatus s;
4092 BcLexType type;
4093 char inst;
4094 BcInst etype = *prev;
4095
4096 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
4097 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
4098 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
4099 {
4100 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
4101 bc_parse_push(p, inst);
4102 s = bc_lex_next(&p->l);
4103 }
4104 else {
4105
4106 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
4107 *paren_expr = true;
4108
4109 s = bc_lex_next(&p->l);
4110 if (s) return s;
4111 type = p->l.t.t;
4112
4113 // Because we parse the next part of the expression
4114 // right here, we need to increment this.
4115 *nexprs = *nexprs + 1;
4116
4117 switch (type) {
4118
4119 case BC_LEX_NAME:
4120 {
4121 s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
4122 break;
4123 }
4124
4125 case BC_LEX_KEY_IBASE:
4126 case BC_LEX_KEY_LAST:
4127 case BC_LEX_KEY_OBASE:
4128 {
4129 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4130 s = bc_lex_next(&p->l);
4131 break;
4132 }
4133
4134 case BC_LEX_KEY_SCALE:
4135 {
4136 s = bc_lex_next(&p->l);
4137 if (s) return s;
4138 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004139 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004140 else
4141 bc_parse_push(p, BC_INST_SCALE);
4142 break;
4143 }
4144
4145 default:
4146 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004147 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004148 break;
4149 }
4150 }
4151
4152 if (!s) bc_parse_push(p, inst);
4153 }
4154
4155 return s;
4156}
4157
4158static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
4159 bool rparen, size_t *nexprs)
4160{
4161 BcStatus s;
4162 BcLexType type;
4163 BcInst etype = *prev;
4164
4165 s = bc_lex_next(&p->l);
4166 if (s) return s;
4167
4168 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4169 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4170 BC_LEX_OP_MINUS :
4171 BC_LEX_NEG;
4172 *prev = BC_PARSE_TOKEN_INST(type);
4173
4174 // We can just push onto the op stack because this is the largest
4175 // precedence operator that gets pushed. Inc/dec does not.
4176 if (type != BC_LEX_OP_MINUS)
4177 bc_vec_push(&p->ops, &type);
4178 else
4179 s = bc_parse_operator(p, type, ops_bgn, nexprs, false);
4180
4181 return s;
4182}
4183
4184static BcStatus bc_parse_string(BcParse *p, char inst)
4185{
4186 char *str = xstrdup(p->l.t.v.v);
4187
4188 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004189 bc_parse_pushIndex(p, G.prog.strs.len);
4190 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004191 bc_parse_push(p, inst);
4192
4193 return bc_lex_next(&p->l);
4194}
4195
4196static BcStatus bc_parse_print(BcParse *p)
4197{
4198 BcStatus s;
4199 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004200 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06004201
4202 s = bc_lex_next(&p->l);
4203 if (s) return s;
4204
4205 type = p->l.t.t;
4206
4207 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004208 return bc_error("bad print statement");
Gavin Howard01055ba2018-11-03 11:00:21 -06004209
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004210 comma = false;
4211 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004212
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004213 if (type == BC_LEX_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004214 s = bc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004215 if (s) return s;
4216 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004217 s = bc_parse_expr(p, 0, bc_parse_next_print);
4218 if (s) return s;
4219 bc_parse_push(p, BC_INST_PRINT_POP);
4220 }
4221
Gavin Howard01055ba2018-11-03 11:00:21 -06004222 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004223 if (comma) {
4224 s = bc_lex_next(&p->l);
4225 if (s) return s;
4226 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004227 type = p->l.t.t;
4228 }
4229
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004230 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004231
4232 return bc_lex_next(&p->l);
4233}
4234
4235static BcStatus bc_parse_return(BcParse *p)
4236{
4237 BcStatus s;
4238 BcLexType t;
4239 bool paren;
4240
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004241 if (!BC_PARSE_FUNC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004242
4243 s = bc_lex_next(&p->l);
4244 if (s) return s;
4245
4246 t = p->l.t.t;
4247 paren = t == BC_LEX_LPAREN;
4248
4249 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4250 bc_parse_push(p, BC_INST_RET0);
4251 else {
4252
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004253 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004254 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004255 bc_parse_push(p, BC_INST_RET0);
4256 s = bc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004257 }
Denys Vlasenko452df922018-12-05 20:28:26 +01004258 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004259
4260 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004261 s = bc_POSIX_requires("parentheses around return expressions");
Gavin Howard01055ba2018-11-03 11:00:21 -06004262 if (s) return s;
4263 }
4264
4265 bc_parse_push(p, BC_INST_RET);
4266 }
4267
4268 return s;
4269}
4270
4271static BcStatus bc_parse_endBody(BcParse *p, bool brace)
4272{
4273 BcStatus s = BC_STATUS_SUCCESS;
4274
4275 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004276 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004277
4278 if (brace) {
4279
4280 if (p->l.t.t == BC_LEX_RBRACE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004281 if (!p->nbraces) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004282 --p->nbraces;
4283 s = bc_lex_next(&p->l);
4284 if (s) return s;
4285 }
4286 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004287 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004288 }
4289
4290 if (BC_PARSE_IF(p)) {
4291
4292 uint8_t *flag_ptr;
4293
4294 while (p->l.t.t == BC_LEX_NLINE) {
4295 s = bc_lex_next(&p->l);
4296 if (s) return s;
4297 }
4298
4299 bc_vec_pop(&p->flags);
4300
4301 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4302 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4303
4304 if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p);
4305 }
4306 else if (BC_PARSE_ELSE(p)) {
4307
4308 BcInstPtr *ip;
4309 size_t *label;
4310
4311 bc_vec_pop(&p->flags);
4312
4313 ip = bc_vec_top(&p->exits);
4314 label = bc_vec_item(&p->func->labels, ip->idx);
4315 *label = p->func->code.len;
4316
4317 bc_vec_pop(&p->exits);
4318 }
4319 else if (BC_PARSE_FUNC_INNER(p)) {
4320 bc_parse_push(p, BC_INST_RET0);
4321 bc_parse_updateFunc(p, BC_PROG_MAIN);
4322 bc_vec_pop(&p->flags);
4323 }
4324 else {
4325
4326 BcInstPtr *ip = bc_vec_top(&p->exits);
4327 size_t *label = bc_vec_top(&p->conds);
4328
4329 bc_parse_push(p, BC_INST_JUMP);
4330 bc_parse_pushIndex(p, *label);
4331
4332 label = bc_vec_item(&p->func->labels, ip->idx);
4333 *label = p->func->code.len;
4334
4335 bc_vec_pop(&p->flags);
4336 bc_vec_pop(&p->exits);
4337 bc_vec_pop(&p->conds);
4338 }
4339
4340 return s;
4341}
4342
4343static void bc_parse_startBody(BcParse *p, uint8_t flags)
4344{
4345 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4346 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4347 flags |= BC_PARSE_FLAG_BODY;
4348 bc_vec_push(&p->flags, &flags);
4349}
4350
4351static void bc_parse_noElse(BcParse *p)
4352{
4353 BcInstPtr *ip;
4354 size_t *label;
4355 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4356
4357 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4358
4359 ip = bc_vec_top(&p->exits);
4360 label = bc_vec_item(&p->func->labels, ip->idx);
4361 *label = p->func->code.len;
4362
4363 bc_vec_pop(&p->exits);
4364}
4365
4366static BcStatus bc_parse_if(BcParse *p)
4367{
4368 BcStatus s;
4369 BcInstPtr ip;
4370
4371 s = bc_lex_next(&p->l);
4372 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004373 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004374
4375 s = bc_lex_next(&p->l);
4376 if (s) return s;
4377 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4378 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004379 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004380
4381 s = bc_lex_next(&p->l);
4382 if (s) return s;
4383 bc_parse_push(p, BC_INST_JUMP_ZERO);
4384
4385 ip.idx = p->func->labels.len;
4386 ip.func = ip.len = 0;
4387
4388 bc_parse_pushIndex(p, ip.idx);
4389 bc_vec_push(&p->exits, &ip);
4390 bc_vec_push(&p->func->labels, &ip.idx);
4391 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4392
4393 return BC_STATUS_SUCCESS;
4394}
4395
4396static BcStatus bc_parse_else(BcParse *p)
4397{
4398 BcInstPtr ip;
4399
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004400 if (!BC_PARSE_IF_END(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004401
4402 ip.idx = p->func->labels.len;
4403 ip.func = ip.len = 0;
4404
4405 bc_parse_push(p, BC_INST_JUMP);
4406 bc_parse_pushIndex(p, ip.idx);
4407
4408 bc_parse_noElse(p);
4409
4410 bc_vec_push(&p->exits, &ip);
4411 bc_vec_push(&p->func->labels, &ip.idx);
4412 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4413
4414 return bc_lex_next(&p->l);
4415}
4416
4417static BcStatus bc_parse_while(BcParse *p)
4418{
4419 BcStatus s;
4420 BcInstPtr ip;
4421
4422 s = bc_lex_next(&p->l);
4423 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004424 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004425 s = bc_lex_next(&p->l);
4426 if (s) return s;
4427
4428 ip.idx = p->func->labels.len;
4429
4430 bc_vec_push(&p->func->labels, &p->func->code.len);
4431 bc_vec_push(&p->conds, &ip.idx);
4432
4433 ip.idx = p->func->labels.len;
4434 ip.func = 1;
4435 ip.len = 0;
4436
4437 bc_vec_push(&p->exits, &ip);
4438 bc_vec_push(&p->func->labels, &ip.idx);
4439
4440 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4441 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004442 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004443 s = bc_lex_next(&p->l);
4444 if (s) return s;
4445
4446 bc_parse_push(p, BC_INST_JUMP_ZERO);
4447 bc_parse_pushIndex(p, ip.idx);
4448 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4449
4450 return BC_STATUS_SUCCESS;
4451}
4452
4453static BcStatus bc_parse_for(BcParse *p)
4454{
4455 BcStatus s;
4456 BcInstPtr ip;
4457 size_t cond_idx, exit_idx, body_idx, update_idx;
4458
4459 s = bc_lex_next(&p->l);
4460 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004461 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004462 s = bc_lex_next(&p->l);
4463 if (s) return s;
4464
4465 if (p->l.t.t != BC_LEX_SCOLON)
4466 s = bc_parse_expr(p, 0, bc_parse_next_for);
4467 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004468 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004469
4470 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004471 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004472 s = bc_lex_next(&p->l);
4473 if (s) return s;
4474
4475 cond_idx = p->func->labels.len;
4476 update_idx = cond_idx + 1;
4477 body_idx = update_idx + 1;
4478 exit_idx = body_idx + 1;
4479
4480 bc_vec_push(&p->func->labels, &p->func->code.len);
4481
4482 if (p->l.t.t != BC_LEX_SCOLON)
4483 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
4484 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004485 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004486
4487 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004488 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004489
4490 s = bc_lex_next(&p->l);
4491 if (s) return s;
4492
4493 bc_parse_push(p, BC_INST_JUMP_ZERO);
4494 bc_parse_pushIndex(p, exit_idx);
4495 bc_parse_push(p, BC_INST_JUMP);
4496 bc_parse_pushIndex(p, body_idx);
4497
4498 ip.idx = p->func->labels.len;
4499
4500 bc_vec_push(&p->conds, &update_idx);
4501 bc_vec_push(&p->func->labels, &p->func->code.len);
4502
4503 if (p->l.t.t != BC_LEX_RPAREN)
4504 s = bc_parse_expr(p, 0, bc_parse_next_rel);
4505 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004506 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004507
4508 if (s) return s;
4509
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004510 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004511 bc_parse_push(p, BC_INST_JUMP);
4512 bc_parse_pushIndex(p, cond_idx);
4513 bc_vec_push(&p->func->labels, &p->func->code.len);
4514
4515 ip.idx = exit_idx;
4516 ip.func = 1;
4517 ip.len = 0;
4518
4519 bc_vec_push(&p->exits, &ip);
4520 bc_vec_push(&p->func->labels, &ip.idx);
4521 bc_lex_next(&p->l);
4522 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4523
4524 return BC_STATUS_SUCCESS;
4525}
4526
4527static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type)
4528{
4529 BcStatus s;
4530 size_t i;
4531 BcInstPtr *ip;
4532
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004533 if (!BC_PARSE_LOOP(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004534
4535 if (type == BC_LEX_KEY_BREAK) {
4536
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004537 if (p->exits.len == 0) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004538
4539 i = p->exits.len - 1;
4540 ip = bc_vec_item(&p->exits, i);
4541
4542 while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004543 if (i >= p->exits.len && !ip->func) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004544
4545 i = ip->idx;
4546 }
4547 else
4548 i = *((size_t *) bc_vec_top(&p->conds));
4549
4550 bc_parse_push(p, BC_INST_JUMP);
4551 bc_parse_pushIndex(p, i);
4552
4553 s = bc_lex_next(&p->l);
4554 if (s) return s;
4555
4556 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004557 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004558
4559 return bc_lex_next(&p->l);
4560}
4561
4562static BcStatus bc_parse_func(BcParse *p)
4563{
4564 BcStatus s;
4565 bool var, comma = false;
4566 uint8_t flags;
4567 char *name;
4568
4569 s = bc_lex_next(&p->l);
4570 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004571 if (p->l.t.t != BC_LEX_NAME)
4572 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004573
4574 name = xstrdup(p->l.t.v.v);
4575 bc_parse_addFunc(p, name, &p->fidx);
4576
4577 s = bc_lex_next(&p->l);
4578 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004579 if (p->l.t.t != BC_LEX_LPAREN)
4580 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004581 s = bc_lex_next(&p->l);
4582 if (s) return s;
4583
4584 while (p->l.t.t != BC_LEX_RPAREN) {
4585
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004586 if (p->l.t.t != BC_LEX_NAME)
4587 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004588
4589 ++p->func->nparams;
4590
4591 name = xstrdup(p->l.t.v.v);
4592 s = bc_lex_next(&p->l);
4593 if (s) goto err;
4594
4595 var = p->l.t.t != BC_LEX_LBRACKET;
4596
4597 if (!var) {
4598
4599 s = bc_lex_next(&p->l);
4600 if (s) goto err;
4601
4602 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004603 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004604 goto err;
4605 }
4606
4607 s = bc_lex_next(&p->l);
4608 if (s) goto err;
4609 }
4610
4611 comma = p->l.t.t == BC_LEX_COMMA;
4612 if (comma) {
4613 s = bc_lex_next(&p->l);
4614 if (s) goto err;
4615 }
4616
Denys Vlasenko29301232018-12-11 15:29:32 +01004617 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004618 if (s) goto err;
4619 }
4620
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004621 if (comma) return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004622
4623 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4624 bc_parse_startBody(p, flags);
4625
4626 s = bc_lex_next(&p->l);
4627 if (s) return s;
4628
4629 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004630 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004631
4632 return s;
4633
4634err:
4635 free(name);
4636 return s;
4637}
4638
4639static BcStatus bc_parse_auto(BcParse *p)
4640{
4641 BcStatus s;
4642 bool comma, var, one;
4643 char *name;
4644
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004645 if (!p->auto_part) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004646 s = bc_lex_next(&p->l);
4647 if (s) return s;
4648
4649 p->auto_part = comma = false;
4650 one = p->l.t.t == BC_LEX_NAME;
4651
4652 while (p->l.t.t == BC_LEX_NAME) {
4653
4654 name = xstrdup(p->l.t.v.v);
4655 s = bc_lex_next(&p->l);
4656 if (s) goto err;
4657
4658 var = p->l.t.t != BC_LEX_LBRACKET;
4659 if (!var) {
4660
4661 s = bc_lex_next(&p->l);
4662 if (s) goto err;
4663
4664 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004665 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004666 goto err;
4667 }
4668
4669 s = bc_lex_next(&p->l);
4670 if (s) goto err;
4671 }
4672
4673 comma = p->l.t.t == BC_LEX_COMMA;
4674 if (comma) {
4675 s = bc_lex_next(&p->l);
4676 if (s) goto err;
4677 }
4678
Denys Vlasenko29301232018-12-11 15:29:32 +01004679 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004680 if (s) goto err;
4681 }
4682
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004683 if (comma) return bc_error("bad function definition");
Denys Vlasenkoabbc4332018-12-03 21:46:41 +01004684 if (!one) return bc_error("no auto variable found");
Gavin Howard01055ba2018-11-03 11:00:21 -06004685
4686 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004687 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004688
4689 return bc_lex_next(&p->l);
4690
4691err:
4692 free(name);
4693 return s;
4694}
4695
4696static BcStatus bc_parse_body(BcParse *p, bool brace)
4697{
4698 BcStatus s = BC_STATUS_SUCCESS;
4699 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4700
4701 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4702
4703 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
4704
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004705 if (!brace) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004706 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4707
4708 if (!p->auto_part) {
4709 s = bc_parse_auto(p);
4710 if (s) return s;
4711 }
4712
4713 if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l);
4714 }
4715 else {
4716 s = bc_parse_stmt(p);
4717 if (!s && !brace) s = bc_parse_endBody(p, false);
4718 }
4719
4720 return s;
4721}
4722
4723static BcStatus bc_parse_stmt(BcParse *p)
4724{
4725 BcStatus s = BC_STATUS_SUCCESS;
4726
4727 switch (p->l.t.t) {
4728
4729 case BC_LEX_NLINE:
4730 {
4731 return bc_lex_next(&p->l);
4732 }
4733
4734 case BC_LEX_KEY_ELSE:
4735 {
4736 p->auto_part = false;
4737 break;
4738 }
4739
4740 case BC_LEX_LBRACE:
4741 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004742 if (!BC_PARSE_BODY(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004743
4744 ++p->nbraces;
4745 s = bc_lex_next(&p->l);
4746 if (s) return s;
4747
4748 return bc_parse_body(p, true);
4749 }
4750
4751 case BC_LEX_KEY_AUTO:
4752 {
4753 return bc_parse_auto(p);
4754 }
4755
4756 default:
4757 {
4758 p->auto_part = false;
4759
4760 if (BC_PARSE_IF_END(p)) {
4761 bc_parse_noElse(p);
4762 return BC_STATUS_SUCCESS;
4763 }
4764 else if (BC_PARSE_BODY(p))
4765 return bc_parse_body(p, false);
4766
4767 break;
4768 }
4769 }
4770
4771 switch (p->l.t.t) {
4772
4773 case BC_LEX_OP_INC:
4774 case BC_LEX_OP_DEC:
4775 case BC_LEX_OP_MINUS:
4776 case BC_LEX_OP_BOOL_NOT:
4777 case BC_LEX_LPAREN:
4778 case BC_LEX_NAME:
4779 case BC_LEX_NUMBER:
4780 case BC_LEX_KEY_IBASE:
4781 case BC_LEX_KEY_LAST:
4782 case BC_LEX_KEY_LENGTH:
4783 case BC_LEX_KEY_OBASE:
4784 case BC_LEX_KEY_READ:
4785 case BC_LEX_KEY_SCALE:
4786 case BC_LEX_KEY_SQRT:
4787 {
4788 s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
4789 break;
4790 }
4791
4792 case BC_LEX_KEY_ELSE:
4793 {
4794 s = bc_parse_else(p);
4795 break;
4796 }
4797
4798 case BC_LEX_SCOLON:
4799 {
4800 while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l);
4801 break;
4802 }
4803
4804 case BC_LEX_RBRACE:
4805 {
4806 s = bc_parse_endBody(p, true);
4807 break;
4808 }
4809
4810 case BC_LEX_STR:
4811 {
4812 s = bc_parse_string(p, BC_INST_PRINT_STR);
4813 break;
4814 }
4815
4816 case BC_LEX_KEY_BREAK:
4817 case BC_LEX_KEY_CONTINUE:
4818 {
4819 s = bc_parse_loopExit(p, p->l.t.t);
4820 break;
4821 }
4822
4823 case BC_LEX_KEY_FOR:
4824 {
4825 s = bc_parse_for(p);
4826 break;
4827 }
4828
4829 case BC_LEX_KEY_HALT:
4830 {
4831 bc_parse_push(p, BC_INST_HALT);
4832 s = bc_lex_next(&p->l);
4833 break;
4834 }
4835
4836 case BC_LEX_KEY_IF:
4837 {
4838 s = bc_parse_if(p);
4839 break;
4840 }
4841
4842 case BC_LEX_KEY_LIMITS:
4843 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004844 // "limits" is a compile-time command,
4845 // the output is produced at _parse time_.
Gavin Howard01055ba2018-11-03 11:00:21 -06004846 s = bc_lex_next(&p->l);
4847 if (s) return s;
Denys Vlasenko64074a12018-12-07 15:50:14 +01004848 printf(
4849 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4850 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4851 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4852 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4853 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4854 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4855 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4856 "Number of vars = "BC_MAX_VARS_STR "\n"
4857 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004858 break;
4859 }
4860
4861 case BC_LEX_KEY_PRINT:
4862 {
4863 s = bc_parse_print(p);
4864 break;
4865 }
4866
4867 case BC_LEX_KEY_QUIT:
4868 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004869 // "quit" is a compile-time command. For example,
4870 // "if (0 == 1) quit" terminates when parsing the statement,
4871 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004872 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004873 }
4874
4875 case BC_LEX_KEY_RETURN:
4876 {
4877 s = bc_parse_return(p);
4878 break;
4879 }
4880
4881 case BC_LEX_KEY_WHILE:
4882 {
4883 s = bc_parse_while(p);
4884 break;
4885 }
4886
4887 default:
4888 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004889 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004890 break;
4891 }
4892 }
4893
4894 return s;
4895}
4896
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01004897static FAST_FUNC BcStatus bc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004898{
4899 BcStatus s;
4900
4901 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004902 s = p->flags.len > 0 ? bc_error("block end could not be found") : bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06004903 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004904 if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004905 s = bc_parse_func(p);
4906 }
4907 else
4908 s = bc_parse_stmt(p);
4909
Denys Vlasenkod38af482018-12-04 19:11:02 +01004910 if (s || G_interrupt) {
4911 bc_parse_reset(p);
4912 s = BC_STATUS_FAILURE;
4913 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004914
4915 return s;
4916}
4917
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004918// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004919static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004920{
4921 BcStatus s = BC_STATUS_SUCCESS;
4922 BcInst prev = BC_INST_PRINT;
4923 BcLexType top, t = p->l.t.t;
4924 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004925 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004926 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4927
4928 paren_first = p->l.t.t == BC_LEX_LPAREN;
4929 nparens = nrelops = 0;
4930 paren_expr = rprn = done = get_token = assign = false;
4931 bin_last = true;
4932
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004933 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004934 switch (t) {
4935
4936 case BC_LEX_OP_INC:
4937 case BC_LEX_OP_DEC:
4938 {
4939 s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
4940 rprn = get_token = bin_last = false;
4941 break;
4942 }
4943
4944 case BC_LEX_OP_MINUS:
4945 {
4946 s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
4947 rprn = get_token = false;
4948 bin_last = prev == BC_INST_MINUS;
4949 break;
4950 }
4951
4952 case BC_LEX_OP_ASSIGN_POWER:
4953 case BC_LEX_OP_ASSIGN_MULTIPLY:
4954 case BC_LEX_OP_ASSIGN_DIVIDE:
4955 case BC_LEX_OP_ASSIGN_MODULUS:
4956 case BC_LEX_OP_ASSIGN_PLUS:
4957 case BC_LEX_OP_ASSIGN_MINUS:
4958 case BC_LEX_OP_ASSIGN:
4959 {
4960 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4961 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4962 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4963 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004964 s = bc_error("bad assignment:"
4965 " left side must be scale,"
4966 " ibase, obase, last, var,"
4967 " or array element"
4968 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004969 break;
4970 }
4971 }
4972 // Fallthrough.
4973 case BC_LEX_OP_POWER:
4974 case BC_LEX_OP_MULTIPLY:
4975 case BC_LEX_OP_DIVIDE:
4976 case BC_LEX_OP_MODULUS:
4977 case BC_LEX_OP_PLUS:
4978 case BC_LEX_OP_REL_EQ:
4979 case BC_LEX_OP_REL_LE:
4980 case BC_LEX_OP_REL_GE:
4981 case BC_LEX_OP_REL_NE:
4982 case BC_LEX_OP_REL_LT:
4983 case BC_LEX_OP_REL_GT:
4984 case BC_LEX_OP_BOOL_NOT:
4985 case BC_LEX_OP_BOOL_OR:
4986 case BC_LEX_OP_BOOL_AND:
4987 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004988 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4989 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4990 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004991 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004992 }
4993
4994 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4995 prev = BC_PARSE_TOKEN_INST(t);
4996 s = bc_parse_operator(p, t, ops_bgn, &nexprs, true);
4997 rprn = get_token = false;
4998 bin_last = t != BC_LEX_OP_BOOL_NOT;
4999
5000 break;
5001 }
5002
5003 case BC_LEX_LPAREN:
5004 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005005 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005006 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005007 ++nparens;
5008 paren_expr = rprn = bin_last = false;
5009 get_token = true;
5010 bc_vec_push(&p->ops, &t);
5011
5012 break;
5013 }
5014
5015 case BC_LEX_RPAREN:
5016 {
5017 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005018 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005019
5020 if (nparens == 0) {
5021 s = BC_STATUS_SUCCESS;
5022 done = true;
5023 get_token = false;
5024 break;
5025 }
5026 else if (!paren_expr)
5027 return BC_STATUS_PARSE_EMPTY_EXP;
5028
5029 --nparens;
5030 paren_expr = rprn = true;
5031 get_token = bin_last = false;
5032
5033 s = bc_parse_rightParen(p, ops_bgn, &nexprs);
5034
5035 break;
5036 }
5037
5038 case BC_LEX_NAME:
5039 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005040 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005041 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005042 paren_expr = true;
5043 rprn = get_token = bin_last = false;
5044 s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
5045 ++nexprs;
5046
5047 break;
5048 }
5049
5050 case BC_LEX_NUMBER:
5051 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005052 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005053 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005054 bc_parse_number(p, &prev, &nexprs);
5055 paren_expr = get_token = true;
5056 rprn = bin_last = false;
5057
5058 break;
5059 }
5060
5061 case BC_LEX_KEY_IBASE:
5062 case BC_LEX_KEY_LAST:
5063 case BC_LEX_KEY_OBASE:
5064 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005065 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005066 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005067 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
5068 bc_parse_push(p, (char) prev);
5069
5070 paren_expr = get_token = true;
5071 rprn = bin_last = false;
5072 ++nexprs;
5073
5074 break;
5075 }
5076
5077 case BC_LEX_KEY_LENGTH:
5078 case BC_LEX_KEY_SQRT:
5079 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005080 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005081 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005082 s = bc_parse_builtin(p, t, flags, &prev);
5083 paren_expr = true;
5084 rprn = get_token = bin_last = false;
5085 ++nexprs;
5086
5087 break;
5088 }
5089
5090 case BC_LEX_KEY_READ:
5091 {
5092 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005093 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005094 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005095 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005096 else
5097 s = bc_parse_read(p);
5098
5099 paren_expr = true;
5100 rprn = get_token = bin_last = false;
5101 ++nexprs;
5102 prev = BC_INST_READ;
5103
5104 break;
5105 }
5106
5107 case BC_LEX_KEY_SCALE:
5108 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005109 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005110 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005111 s = bc_parse_scale(p, &prev, flags);
5112 paren_expr = true;
5113 rprn = get_token = bin_last = false;
5114 ++nexprs;
5115 prev = BC_INST_SCALE;
5116
5117 break;
5118 }
5119
5120 default:
5121 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005122 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005123 break;
5124 }
5125 }
5126
5127 if (!s && get_token) s = bc_lex_next(&p->l);
5128 }
5129
5130 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01005131 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06005132
5133 while (p->ops.len > ops_bgn) {
5134
5135 top = BC_PARSE_TOP_OP(p);
5136 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
5137
5138 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005139 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005140
5141 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
5142
5143 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
5144 bc_vec_pop(&p->ops);
5145 }
5146
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005147 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005148 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005149
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005150 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
5151 for (;;) {
5152 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005153 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005154 if (next & 0x80) // last element?
5155 break;
5156 next >>= 8;
5157 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005158 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005159 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06005160
5161 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01005162 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Gavin Howard01055ba2018-11-03 11:00:21 -06005163 if (s) return s;
5164 }
5165 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01005166 s = bc_POSIX_requires("exactly one comparison operator per condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06005167 if (s) return s;
5168 }
5169
5170 if (flags & BC_PARSE_PRINT) {
5171 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
5172 bc_parse_push(p, BC_INST_POP);
5173 }
5174
5175 return s;
5176}
5177
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01005178static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
5179{
5180 BcStatus s;
5181
5182 s = bc_parse_expr_empty_ok(p, flags, next);
5183 if (s == BC_STATUS_PARSE_EMPTY_EXP)
5184 return bc_error("empty expression");
5185 return s;
5186}
5187
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005188static void bc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005189{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005190 bc_parse_create(p, func, bc_parse_parse, bc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005191}
5192
5193static BcStatus bc_parse_expression(BcParse *p, uint8_t flags)
5194{
5195 return bc_parse_expr(p, flags, bc_parse_next_read);
5196}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005197
Gavin Howard01055ba2018-11-03 11:00:21 -06005198#endif // ENABLE_BC
5199
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005200#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005201
5202#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
5203
Gavin Howard01055ba2018-11-03 11:00:21 -06005204static BcStatus dc_parse_register(BcParse *p)
5205{
5206 BcStatus s;
5207 char *name;
5208
5209 s = bc_lex_next(&p->l);
5210 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005211 if (p->l.t.t != BC_LEX_NAME) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005212
5213 name = xstrdup(p->l.t.v.v);
5214 bc_parse_pushName(p, name);
5215
5216 return s;
5217}
5218
5219static BcStatus dc_parse_string(BcParse *p)
5220{
5221 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005222 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005223
5224 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
5225 name = xstrdup(b);
5226
5227 str = xstrdup(p->l.t.v.v);
5228 bc_parse_push(p, BC_INST_STR);
5229 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005230 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005231 bc_parse_addFunc(p, name, &idx);
5232
5233 return bc_lex_next(&p->l);
5234}
5235
5236static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
5237{
5238 BcStatus s;
5239
5240 bc_parse_push(p, inst);
5241 if (name) {
5242 s = dc_parse_register(p);
5243 if (s) return s;
5244 }
5245
5246 if (store) {
5247 bc_parse_push(p, BC_INST_SWAP);
5248 bc_parse_push(p, BC_INST_ASSIGN);
5249 bc_parse_push(p, BC_INST_POP);
5250 }
5251
5252 return bc_lex_next(&p->l);
5253}
5254
5255static BcStatus dc_parse_cond(BcParse *p, uint8_t inst)
5256{
5257 BcStatus s;
5258
5259 bc_parse_push(p, inst);
5260 bc_parse_push(p, BC_INST_EXEC_COND);
5261
5262 s = dc_parse_register(p);
5263 if (s) return s;
5264
5265 s = bc_lex_next(&p->l);
5266 if (s) return s;
5267
5268 if (p->l.t.t == BC_LEX_ELSE) {
5269 s = dc_parse_register(p);
5270 if (s) return s;
5271 s = bc_lex_next(&p->l);
5272 }
5273 else
5274 bc_parse_push(p, BC_PARSE_STREND);
5275
5276 return s;
5277}
5278
5279static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
5280{
5281 BcStatus s = BC_STATUS_SUCCESS;
5282 BcInst prev;
5283 uint8_t inst;
5284 bool assign, get_token = false;
5285
5286 switch (t) {
5287
5288 case BC_LEX_OP_REL_EQ:
5289 case BC_LEX_OP_REL_LE:
5290 case BC_LEX_OP_REL_GE:
5291 case BC_LEX_OP_REL_NE:
5292 case BC_LEX_OP_REL_LT:
5293 case BC_LEX_OP_REL_GT:
5294 {
5295 s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
5296 break;
5297 }
5298
5299 case BC_LEX_SCOLON:
5300 case BC_LEX_COLON:
5301 {
5302 s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
5303 break;
5304 }
5305
5306 case BC_LEX_STR:
5307 {
5308 s = dc_parse_string(p);
5309 break;
5310 }
5311
5312 case BC_LEX_NEG:
5313 case BC_LEX_NUMBER:
5314 {
5315 if (t == BC_LEX_NEG) {
5316 s = bc_lex_next(&p->l);
5317 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005318 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005319 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005320 }
5321
5322 bc_parse_number(p, &prev, &p->nbraces);
5323
5324 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5325 get_token = true;
5326
5327 break;
5328 }
5329
5330 case BC_LEX_KEY_READ:
5331 {
5332 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005333 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005334 else
5335 bc_parse_push(p, BC_INST_READ);
5336 get_token = true;
5337 break;
5338 }
5339
5340 case BC_LEX_OP_ASSIGN:
5341 case BC_LEX_STORE_PUSH:
5342 {
5343 assign = t == BC_LEX_OP_ASSIGN;
5344 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
5345 s = dc_parse_mem(p, inst, true, assign);
5346 break;
5347 }
5348
5349 case BC_LEX_LOAD:
5350 case BC_LEX_LOAD_POP:
5351 {
5352 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
5353 s = dc_parse_mem(p, inst, true, false);
5354 break;
5355 }
5356
5357 case BC_LEX_STORE_IBASE:
5358 case BC_LEX_STORE_SCALE:
5359 case BC_LEX_STORE_OBASE:
5360 {
5361 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
5362 s = dc_parse_mem(p, inst, false, true);
5363 break;
5364 }
5365
5366 default:
5367 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005368 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005369 get_token = true;
5370 break;
5371 }
5372 }
5373
5374 if (!s && get_token) s = bc_lex_next(&p->l);
5375
5376 return s;
5377}
5378
5379static BcStatus dc_parse_expr(BcParse *p, uint8_t flags)
5380{
5381 BcStatus s = BC_STATUS_SUCCESS;
5382 BcInst inst;
5383 BcLexType t;
5384
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005385 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005386
5387 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
5388
5389 inst = dc_parse_insts[t];
5390
5391 if (inst != BC_INST_INVALID) {
5392 bc_parse_push(p, inst);
5393 s = bc_lex_next(&p->l);
5394 }
5395 else
5396 s = dc_parse_token(p, t, flags);
5397 }
5398
5399 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5400 bc_parse_push(p, BC_INST_POP_EXEC);
5401
5402 return s;
5403}
5404
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01005405static FAST_FUNC BcStatus dc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005406{
5407 BcStatus s;
5408
5409 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005410 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005411 else
5412 s = dc_parse_expr(p, 0);
5413
Denys Vlasenkod38af482018-12-04 19:11:02 +01005414 if (s || G_interrupt) {
5415 bc_parse_reset(p);
5416 s = BC_STATUS_FAILURE;
5417 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005418
5419 return s;
5420}
5421
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005422static void dc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005423{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005424 bc_parse_create(p, func, dc_parse_parse, dc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005425}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005426
Gavin Howard01055ba2018-11-03 11:00:21 -06005427#endif // ENABLE_DC
5428
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005429static void common_parse_init(BcParse *p, size_t func)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005430{
5431 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005432 IF_BC(bc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005433 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005434 IF_DC(dc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005435 }
5436}
5437
5438static BcStatus common_parse_expr(BcParse *p, uint8_t flags)
5439{
5440 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005441 IF_BC(return bc_parse_expression(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005442 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005443 IF_DC(return dc_parse_expr(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005444 }
5445}
5446
Denys Vlasenkodf515392018-12-02 19:27:48 +01005447static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005448{
Gavin Howard01055ba2018-11-03 11:00:21 -06005449 BcId e, *ptr;
5450 BcVec *v, *map;
5451 size_t i;
5452 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005453 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005454
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005455 v = var ? &G.prog.vars : &G.prog.arrs;
5456 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005457
5458 e.name = id;
5459 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005460 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005461
5462 if (new) {
5463 bc_array_init(&data.v, var);
5464 bc_vec_push(v, &data.v);
5465 }
5466
5467 ptr = bc_vec_item(map, i);
5468 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005469 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005470}
5471
Denys Vlasenko29301232018-12-11 15:29:32 +01005472static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005473{
Gavin Howard01055ba2018-11-03 11:00:21 -06005474 switch (r->t) {
5475
5476 case BC_RESULT_STR:
5477 case BC_RESULT_TEMP:
5478 case BC_RESULT_IBASE:
5479 case BC_RESULT_SCALE:
5480 case BC_RESULT_OBASE:
5481 {
5482 *num = &r->d.n;
5483 break;
5484 }
5485
5486 case BC_RESULT_CONSTANT:
5487 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005488 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005489 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005490 size_t base_t, len = strlen(*str);
5491 BcNum *base;
5492
5493 bc_num_init(&r->d.n, len);
5494
5495 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005496 base = hex ? &G.prog.hexb : &G.prog.ib;
5497 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005498 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005499
5500 if (s) {
5501 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005502 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005503 }
5504
5505 *num = &r->d.n;
5506 r->t = BC_RESULT_TEMP;
5507
5508 break;
5509 }
5510
5511 case BC_RESULT_VAR:
5512 case BC_RESULT_ARRAY:
5513 case BC_RESULT_ARRAY_ELEM:
5514 {
5515 BcVec *v;
5516
Denys Vlasenkodf515392018-12-02 19:27:48 +01005517 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005518
5519 if (r->t == BC_RESULT_ARRAY_ELEM) {
5520 v = bc_vec_top(v);
5521 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5522 *num = bc_vec_item(v, r->d.id.idx);
5523 }
5524 else
5525 *num = bc_vec_top(v);
5526
5527 break;
5528 }
5529
5530 case BC_RESULT_LAST:
5531 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005532 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005533 break;
5534 }
5535
5536 case BC_RESULT_ONE:
5537 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005538 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005539 break;
5540 }
5541 }
5542
Denys Vlasenko29301232018-12-11 15:29:32 +01005543 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005544}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005545#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005546# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005547#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005548
Denys Vlasenko29301232018-12-11 15:29:32 +01005549static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005550 BcResult **r, BcNum **rn, bool assign)
5551{
5552 BcStatus s;
5553 bool hex;
5554 BcResultType lt, rt;
5555
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005556 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005557 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005558
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005559 *r = bc_vec_item_rev(&G.prog.results, 0);
5560 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005561
5562 lt = (*l)->t;
5563 rt = (*r)->t;
5564 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5565
Denys Vlasenko29301232018-12-11 15:29:32 +01005566 s = zbc_program_num(*l, ln, false);
5567 if (s) RETURN_STATUS(s);
5568 s = zbc_program_num(*r, rn, hex);
5569 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005570
5571 // We run this again under these conditions in case any vector has been
5572 // reallocated out from under the BcNums or arrays we had.
5573 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005574 s = zbc_program_num(*l, ln, false);
5575 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005576 }
5577
5578 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005579 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005580 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005581 RETURN_STATUS(bc_error_variable_is_wrong_type());
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 Vlasenko628bf1b2018-12-10 20:41:05 +01005585#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005586# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005587#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005588
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005589static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005590{
5591 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005592 bc_vec_pop(&G.prog.results);
5593 bc_vec_pop(&G.prog.results);
5594 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005595}
5596
Denys Vlasenko29301232018-12-11 15:29:32 +01005597static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005598{
5599 BcStatus s;
5600
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005601 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005602 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005603 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005604
Denys Vlasenko29301232018-12-11 15:29:32 +01005605 s = zbc_program_num(*r, n, false);
5606 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005607
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005608 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005609 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005610
Denys Vlasenko29301232018-12-11 15:29:32 +01005611 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005612}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005613#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005614# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005615#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005616
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005617static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005618{
5619 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005620 bc_vec_pop(&G.prog.results);
5621 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005622}
5623
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005624static BcStatus bc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005625{
5626 BcStatus s;
5627 BcResult *opd1, *opd2, res;
5628 BcNum *n1, *n2 = NULL;
5629
Denys Vlasenko29301232018-12-11 15:29:32 +01005630 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005631 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005632 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005633
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005634 s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005635 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005636 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005637
5638 return s;
5639
5640err:
5641 bc_num_free(&res.d.n);
5642 return s;
5643}
5644
Denys Vlasenko785e4b32018-12-02 17:18:52 +01005645static BcStatus bc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005646{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005647 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005648 BcStatus s;
5649 BcParse parse;
5650 BcVec buf;
5651 BcInstPtr ip;
5652 size_t i;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005653 BcFunc *f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005654
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005655 for (i = 0; i < G.prog.stack.len; ++i) {
5656 BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005657 if (ip_ptr->func == BC_PROG_READ)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005658 return bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005659 }
5660
Denys Vlasenko7d628012018-12-04 21:46:47 +01005661 bc_vec_pop_all(&f->code);
5662 bc_char_vec_init(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005663
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005664 sv_file = G.prog.file;
5665 G.prog.file = NULL;
5666
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01005667 s = bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005668 if (s) goto io_err;
5669
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005670 common_parse_init(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005671 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005672
5673 s = bc_parse_text(&parse, buf.v);
5674 if (s) goto exec_err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005675 s = common_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005676 if (s) goto exec_err;
5677
5678 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005679 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005680 goto exec_err;
5681 }
5682
5683 ip.func = BC_PROG_READ;
5684 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005685 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005686
5687 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005688 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005689
5690 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005691 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005692
5693exec_err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005694 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005695 bc_parse_free(&parse);
5696io_err:
5697 bc_vec_free(&buf);
5698 return s;
5699}
5700
5701static size_t bc_program_index(char *code, size_t *bgn)
5702{
5703 char amt = code[(*bgn)++], i = 0;
5704 size_t res = 0;
5705
5706 for (; i < amt; ++i, ++(*bgn))
5707 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5708
5709 return res;
5710}
5711
5712static char *bc_program_name(char *code, size_t *bgn)
5713{
5714 size_t i;
5715 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5716
5717 s = xmalloc(ptr - str + 1);
5718 c = code[(*bgn)++];
5719
5720 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5721 s[i] = c;
5722
5723 s[i] = '\0';
5724
5725 return s;
5726}
5727
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005728static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005729{
5730 size_t i, len = strlen(str);
5731
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005732#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005733 if (len == 0) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005734 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005735 return;
5736 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005737#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005738
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005739 for (i = 0; i < len; ++i, ++G.prog.nchars) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005740
5741 int c = str[i];
5742
5743 if (c != '\\' || i == len - 1)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005744 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005745 else {
5746
5747 c = str[++i];
5748
5749 switch (c) {
5750
5751 case 'a':
5752 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005753 bb_putchar('\a');
Gavin Howard01055ba2018-11-03 11:00:21 -06005754 break;
5755 }
5756
5757 case 'b':
5758 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005759 bb_putchar('\b');
Gavin Howard01055ba2018-11-03 11:00:21 -06005760 break;
5761 }
5762
5763 case '\\':
5764 case 'e':
5765 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005766 bb_putchar('\\');
Gavin Howard01055ba2018-11-03 11:00:21 -06005767 break;
5768 }
5769
5770 case 'f':
5771 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005772 bb_putchar('\f');
Gavin Howard01055ba2018-11-03 11:00:21 -06005773 break;
5774 }
5775
5776 case 'n':
5777 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005778 bb_putchar('\n');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005779 G.prog.nchars = SIZE_MAX;
Gavin Howard01055ba2018-11-03 11:00:21 -06005780 break;
5781 }
5782
5783 case 'r':
5784 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005785 bb_putchar('\r');
Gavin Howard01055ba2018-11-03 11:00:21 -06005786 break;
5787 }
5788
5789 case 'q':
5790 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005791 bb_putchar('"');
Gavin Howard01055ba2018-11-03 11:00:21 -06005792 break;
5793 }
5794
5795 case 't':
5796 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005797 bb_putchar('\t');
Gavin Howard01055ba2018-11-03 11:00:21 -06005798 break;
5799 }
5800
5801 default:
5802 {
5803 // Just print the backslash and following character.
Denys Vlasenko00d77792018-11-30 23:13:42 +01005804 bb_putchar('\\');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005805 ++G.prog.nchars;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005806 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005807 break;
5808 }
5809 }
5810 }
5811 }
5812}
5813
Denys Vlasenko29301232018-12-11 15:29:32 +01005814static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005815{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005816 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005817 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005818 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005819 bool pop = inst != BC_INST_PRINT;
5820
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005821 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005822 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005823
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005824 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005825 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005826 s = zbc_program_num(r, &num, false);
5827 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005828
5829 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005830 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005831 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005832 }
5833 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005834 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005835
5836 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005837 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005838
5839 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005840 for (;;) {
5841 char c = *str++;
5842 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005843 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005844 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005845 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005846 }
5847 }
5848 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005849 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005850 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005851 }
5852 }
5853
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005854 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005855
Denys Vlasenko29301232018-12-11 15:29:32 +01005856 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005857}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005858#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005859# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005860#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005861
Denys Vlasenko29301232018-12-11 15:29:32 +01005862static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005863{
5864 BcStatus s;
5865 BcResult res, *ptr;
5866 BcNum *num = NULL;
5867
Denys Vlasenko29301232018-12-11 15:29:32 +01005868 s = zbc_program_prep(&ptr, &num);
5869 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005870
5871 bc_num_init(&res.d.n, num->len);
5872 bc_num_copy(&res.d.n, num);
5873 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5874
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005875 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005876
Denys Vlasenko29301232018-12-11 15:29:32 +01005877 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005878}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005879#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005880# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005881#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005882
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005883static BcStatus bc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005884{
5885 BcStatus s;
5886 BcResult *opd1, *opd2, res;
5887 BcNum *n1, *n2;
5888 bool cond = 0;
5889 ssize_t cmp;
5890
Denys Vlasenko29301232018-12-11 15:29:32 +01005891 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005892 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005893 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005894
5895 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005896 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005897 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005898 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005899 else {
5900
5901 cmp = bc_num_cmp(n1, n2);
5902
5903 switch (inst) {
5904
5905 case BC_INST_REL_EQ:
5906 {
5907 cond = cmp == 0;
5908 break;
5909 }
5910
5911 case BC_INST_REL_LE:
5912 {
5913 cond = cmp <= 0;
5914 break;
5915 }
5916
5917 case BC_INST_REL_GE:
5918 {
5919 cond = cmp >= 0;
5920 break;
5921 }
5922
5923 case BC_INST_REL_NE:
5924 {
5925 cond = cmp != 0;
5926 break;
5927 }
5928
5929 case BC_INST_REL_LT:
5930 {
5931 cond = cmp < 0;
5932 break;
5933 }
5934
5935 case BC_INST_REL_GT:
5936 {
5937 cond = cmp > 0;
5938 break;
5939 }
5940 }
5941 }
5942
5943 (cond ? bc_num_one : bc_num_zero)(&res.d.n);
5944
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005945 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005946
5947 return s;
5948}
5949
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005950#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005951static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005952 bool push)
5953{
5954 BcNum n2;
5955 BcResult res;
5956
5957 memset(&n2, 0, sizeof(BcNum));
5958 n2.rdx = res.d.id.idx = r->d.id.idx;
5959 res.t = BC_RESULT_STR;
5960
5961 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005962 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005963 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005964 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005965 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005966 }
5967
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005968 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005969
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005970 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005971 bc_vec_push(v, &n2);
5972
Denys Vlasenko29301232018-12-11 15:29:32 +01005973 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005974}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005975#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005976# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005977#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005978#endif // ENABLE_DC
5979
Denys Vlasenko29301232018-12-11 15:29:32 +01005980static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005981{
5982 BcStatus s;
5983 BcResult *ptr, r;
5984 BcVec *v;
5985 BcNum *n;
5986
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005987 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005988 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005989
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005990 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005991 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005992 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005993 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005994
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005995#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005996 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005997 RETURN_STATUS(bc_error_variable_is_wrong_type());
5998 if (ptr->t == BC_RESULT_STR)
5999 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006000#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006001
Denys Vlasenko29301232018-12-11 15:29:32 +01006002 s = zbc_program_num(ptr, &n, false);
6003 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006004
6005 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01006006 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06006007
6008 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006009 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006010 bc_num_copy(&r.d.n, n);
6011 }
6012 else {
6013 bc_array_init(&r.d.v, true);
6014 bc_array_copy(&r.d.v, (BcVec *) n);
6015 }
6016
6017 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006018 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006019
Denys Vlasenko29301232018-12-11 15:29:32 +01006020 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006021}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006022#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006023# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006024#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006025
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006026static BcStatus bc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006027{
6028 BcStatus s;
6029 BcResult *left, *right, res;
6030 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006031 bool assign = inst == BC_INST_ASSIGN, ib, sc;
6032
Denys Vlasenko29301232018-12-11 15:29:32 +01006033 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06006034 if (s) return s;
6035
6036 ib = left->t == BC_RESULT_IBASE;
6037 sc = left->t == BC_RESULT_SCALE;
6038
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006039#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006040
6041 if (right->t == BC_RESULT_STR) {
6042
6043 BcVec *v;
6044
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006045 if (left->t != BC_RESULT_VAR)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006046 return bc_error_variable_is_wrong_type();
Denys Vlasenkodf515392018-12-02 19:27:48 +01006047 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006048
Denys Vlasenko29301232018-12-11 15:29:32 +01006049 return zbc_program_assignStr(right, v, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006050 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006051#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006052
6053 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006054 return bc_error("bad assignment:"
6055 " left side must be scale,"
6056 " ibase, obase, last, var,"
6057 " or array element"
6058 );
Gavin Howard01055ba2018-11-03 11:00:21 -06006059
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006060#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006061 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006062 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06006063
6064 if (assign)
6065 bc_num_copy(l, r);
6066 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006067 s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006068
6069 if (s) return s;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006070#else
Gavin Howard01055ba2018-11-03 11:00:21 -06006071 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006072#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006073
6074 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006075 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006076 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
6077 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
6078 NULL, //can't happen //BC_RESULT_LAST
6079 NULL, //can't happen //BC_RESULT_CONSTANT
6080 NULL, //can't happen //BC_RESULT_ONE
6081 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006082 };
Gavin Howard01055ba2018-11-03 11:00:21 -06006083 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01006084 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06006085
Denys Vlasenko29301232018-12-11 15:29:32 +01006086 s = zbc_num_ulong(l, &val);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006087 if (s)
6088 return s;
6089 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06006090 if (sc) {
6091 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006092 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006093 }
6094 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006095 if (val < BC_NUM_MIN_BASE)
6096 return bc_error(msg[s]);
Gavin Howard01055ba2018-11-03 11:00:21 -06006097 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006098 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006099 }
6100
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006101 if (val > max)
6102 return bc_error(msg[s]);
6103 if (!sc)
6104 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006105
6106 *ptr = (size_t) val;
6107 s = BC_STATUS_SUCCESS;
6108 }
6109
6110 bc_num_init(&res.d.n, l->len);
6111 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006112 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006113
6114 return s;
6115}
6116
Denys Vlasenko416ce762018-12-02 20:57:17 +01006117#if !ENABLE_DC
6118#define bc_program_pushVar(code, bgn, pop, copy) \
6119 bc_program_pushVar(code, bgn)
6120// for bc, 'pop' and 'copy' are always false
6121#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006122static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006123 bool pop, bool copy)
6124{
Gavin Howard01055ba2018-11-03 11:00:21 -06006125 BcResult r;
6126 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006127
6128 r.t = BC_RESULT_VAR;
6129 r.d.id.name = name;
6130
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006131#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006132 {
6133 BcVec *v = bc_program_search(name, true);
6134 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006135
Denys Vlasenko416ce762018-12-02 20:57:17 +01006136 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006137
Denys Vlasenko416ce762018-12-02 20:57:17 +01006138 if (!BC_PROG_STACK(v, 2 - copy)) {
6139 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006140 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006141 }
6142
Gavin Howard01055ba2018-11-03 11:00:21 -06006143 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006144 name = NULL;
6145
6146 if (!BC_PROG_STR(num)) {
6147
6148 r.t = BC_RESULT_TEMP;
6149
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006150 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006151 bc_num_copy(&r.d.n, num);
6152 }
6153 else {
6154 r.t = BC_RESULT_STR;
6155 r.d.id.idx = num->rdx;
6156 }
6157
6158 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006159 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006160 }
6161#endif // ENABLE_DC
6162
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006163 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006164
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006165 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006166}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006167#if ERRORS_ARE_FATAL
6168# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6169#else
6170# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6171#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006172
Denys Vlasenko29301232018-12-11 15:29:32 +01006173static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006174 char inst)
6175{
6176 BcStatus s = BC_STATUS_SUCCESS;
6177 BcResult r;
6178 BcNum *num;
6179
6180 r.d.id.name = bc_program_name(code, bgn);
6181
6182 if (inst == BC_INST_ARRAY) {
6183 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006184 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006185 }
6186 else {
6187
6188 BcResult *operand;
6189 unsigned long temp;
6190
Denys Vlasenko29301232018-12-11 15:29:32 +01006191 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006192 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006193 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006194 if (s) goto err;
6195
6196 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006197 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006198 goto err;
6199 }
6200
6201 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006202 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203 }
6204
6205err:
6206 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006207 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006208}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006209#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006210# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006211#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006212
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006213#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006214static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006215{
6216 BcStatus s;
6217 BcResult *ptr, res, copy;
6218 BcNum *num = NULL;
6219 char inst2 = inst;
6220
Denys Vlasenko29301232018-12-11 15:29:32 +01006221 s = zbc_program_prep(&ptr, &num);
6222 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006223
6224 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6225 copy.t = BC_RESULT_TEMP;
6226 bc_num_init(&copy.d.n, num->len);
6227 bc_num_copy(&copy.d.n, num);
6228 }
6229
6230 res.t = BC_RESULT_ONE;
6231 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6232 BC_INST_ASSIGN_PLUS :
6233 BC_INST_ASSIGN_MINUS;
6234
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006235 bc_vec_push(&G.prog.results, &res);
6236 bc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006237
6238 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006239 bc_vec_pop(&G.prog.results);
6240 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006241 }
6242
Denys Vlasenko29301232018-12-11 15:29:32 +01006243 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006244}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006245#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006246# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006247#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006248
Denys Vlasenko29301232018-12-11 15:29:32 +01006249static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006250{
Gavin Howard01055ba2018-11-03 11:00:21 -06006251 BcInstPtr ip;
6252 size_t i, nparams = bc_program_index(code, idx);
6253 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006254 BcId *a;
6255 BcResultData param;
6256 BcResult *arg;
6257
6258 ip.idx = 0;
6259 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006260 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006261
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006262 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006263 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006264 }
6265 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006266 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006267 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006268 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006269
6270 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006271 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006272
6273 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006274 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006275
6276 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006277 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006278
Denys Vlasenko29301232018-12-11 15:29:32 +01006279 s = zbc_program_copyToVar(a->name, a->idx);
6280 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006281 }
6282
6283 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006284 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006285
6286 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006287 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006288
6289 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006290 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006291 bc_vec_push(v, &param.n);
6292 }
6293 else {
6294 bc_array_init(&param.v, true);
6295 bc_vec_push(v, &param.v);
6296 }
6297 }
6298
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006299 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006300
Denys Vlasenko29301232018-12-11 15:29:32 +01006301 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006302}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006303#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006304# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006305#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006306
Denys Vlasenko29301232018-12-11 15:29:32 +01006307static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006308{
Gavin Howard01055ba2018-11-03 11:00:21 -06006309 BcResult res;
6310 BcFunc *f;
6311 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006312 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006313
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006314 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006315 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006316
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006317 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006318 res.t = BC_RESULT_TEMP;
6319
6320 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006321 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006322 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006323 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006324
Denys Vlasenko29301232018-12-11 15:29:32 +01006325 s = zbc_program_num(operand, &num, false);
6326 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006327 bc_num_init(&res.d.n, num->len);
6328 bc_num_copy(&res.d.n, num);
6329 }
6330 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006331 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006332 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006333 }
6334
6335 // We need to pop arguments as well, so this takes that into account.
6336 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006337 BcVec *v;
6338 BcId *a = bc_vec_item(&f->autos, i);
6339
Denys Vlasenkodf515392018-12-02 19:27:48 +01006340 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006341 bc_vec_pop(v);
6342 }
6343
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006344 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6345 bc_vec_push(&G.prog.results, &res);
6346 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006347
Denys Vlasenko29301232018-12-11 15:29:32 +01006348 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006349}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006350#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006351# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006352#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006353#endif // ENABLE_BC
6354
6355static unsigned long bc_program_scale(BcNum *n)
6356{
6357 return (unsigned long) n->rdx;
6358}
6359
6360static unsigned long bc_program_len(BcNum *n)
6361{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006362 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006363
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006364 if (n->rdx != len) return len;
6365 for (;;) {
6366 if (len == 0) break;
6367 len--;
6368 if (n->num[len] != 0) break;
6369 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006370 return len;
6371}
6372
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006373static BcStatus bc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006374{
6375 BcStatus s;
6376 BcResult *opnd;
6377 BcNum *num = NULL;
6378 BcResult res;
6379 bool len = inst == BC_INST_LENGTH;
6380
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006381 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006382 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006383 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006384
Denys Vlasenko29301232018-12-11 15:29:32 +01006385 s = zbc_program_num(opnd, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006386 if (s) return s;
6387
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006388#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006389 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006390 return bc_error_variable_is_wrong_type();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006391#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006392
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006393 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006394
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006395 if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006396#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006397 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006398 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006399 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006400#endif
6401#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006402 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
6403
6404 char **str;
6405 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6406
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006407 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006408 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006409 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006410#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006411 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006412 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006413 }
6414
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006415 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006416
6417 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006418}
6419
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006420#if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006421static BcStatus bc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006422{
6423 BcStatus s;
6424 BcResult *opd1, *opd2, res, res2;
6425 BcNum *n1, *n2 = NULL;
6426
Denys Vlasenko29301232018-12-11 15:29:32 +01006427 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006428 if (s) return s;
6429
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006430 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006431 bc_num_init(&res2.d.n, n2->len);
6432
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006433 s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006434 if (s) goto err;
6435
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006436 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006437 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006438 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006439
6440 return s;
6441
6442err:
6443 bc_num_free(&res2.d.n);
6444 bc_num_free(&res.d.n);
6445 return s;
6446}
6447
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006448static BcStatus bc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006449{
6450 BcStatus s;
6451 BcResult *r1, *r2, *r3, res;
6452 BcNum *n1, *n2, *n3;
6453
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006454 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006455 return bc_error_stack_has_too_few_elements();
Denys Vlasenko29301232018-12-11 15:29:32 +01006456 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006457 if (s) return s;
6458
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006459 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006460 s = zbc_program_num(r1, &n1, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006461 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006462 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006463 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006464
6465 // Make sure that the values have their pointers updated, if necessary.
6466 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6467
6468 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006469 s = zbc_program_num(r2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006470 if (s) return s;
6471 }
6472
6473 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006474 s = zbc_program_num(r3, &n3, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006475 if (s) return s;
6476 }
6477 }
6478
6479 bc_num_init(&res.d.n, n3->len);
6480 s = bc_num_modexp(n1, n2, n3, &res.d.n);
6481 if (s) goto err;
6482
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006483 bc_vec_pop(&G.prog.results);
6484 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006485
6486 return s;
6487
6488err:
6489 bc_num_free(&res.d.n);
6490 return s;
6491}
6492
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006493static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006494{
Gavin Howard01055ba2018-11-03 11:00:21 -06006495 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006496 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006497
6498 res.t = BC_RESULT_TEMP;
6499
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006500 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006501 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006502 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006503}
6504
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006505static BcStatus bc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006506{
6507 BcStatus s;
6508 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006509 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006510 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006511 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006512 unsigned long val;
6513
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006514 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006515 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006516 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006517
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006518 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006519 s = zbc_program_num(r, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006520 if (s) return s;
6521
6522 if (BC_PROG_NUM(r, num)) {
6523
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006524 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006525 bc_num_copy(&n, num);
6526 bc_num_truncate(&n, n.rdx);
6527
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006528 s = bc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006529 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006530 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006531 if (s) goto num_err;
6532
6533 c = (char) val;
6534
6535 bc_num_free(&n);
6536 }
6537 else {
6538 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006539 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006540 c = str2[0];
6541 }
6542
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006543 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006544 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006545 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006546
6547 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006548 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006549
6550 if (idx != len + BC_PROG_REQ_FUNCS) {
6551
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006552 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006553 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006554 len = idx;
6555 break;
6556 }
6557 }
6558
6559 free(str);
6560 }
6561 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006562 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006563
6564 res.t = BC_RESULT_STR;
6565 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006566 bc_vec_pop(&G.prog.results);
6567 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006568
6569 return BC_STATUS_SUCCESS;
6570
6571num_err:
6572 bc_num_free(&n);
6573 return s;
6574}
6575
Denys Vlasenko29301232018-12-11 15:29:32 +01006576static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006577{
6578 BcStatus s;
6579 BcResult *r;
6580 BcNum *n = NULL;
6581 size_t idx;
6582 char *str;
6583
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006584 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006585 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006586 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006587
Denys Vlasenko29301232018-12-11 15:29:32 +01006588 s = zbc_program_num(r, &n, false);
6589 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006590
6591 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006592 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006593 else {
6594 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006595 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006596 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006597 }
6598
Denys Vlasenko29301232018-12-11 15:29:32 +01006599 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006600}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006601#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006602# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006603#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006604
Denys Vlasenko29301232018-12-11 15:29:32 +01006605static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006606{
6607 BcStatus s;
6608 BcResult *opnd;
6609 BcNum *num = NULL;
6610 unsigned long val;
6611
Denys Vlasenko29301232018-12-11 15:29:32 +01006612 s = zbc_program_prep(&opnd, &num);
6613 if (s) RETURN_STATUS(s);
6614 s = zbc_num_ulong(num, &val);
6615 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006616
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006617 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006618
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006619 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006620 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006621 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006622 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006623 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006624
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006625 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006626
Denys Vlasenko29301232018-12-11 15:29:32 +01006627 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006628}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006629#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006630# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006631#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006632
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006633static BcStatus bc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006634 bool cond)
6635{
6636 BcStatus s = BC_STATUS_SUCCESS;
6637 BcResult *r;
6638 char **str;
6639 BcFunc *f;
6640 BcParse prs;
6641 BcInstPtr ip;
6642 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006643
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006644 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006645 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006646
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006647 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006648
6649 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006650 BcNum *n = n; // for compiler
6651 bool exec;
6652 char *name;
6653 char *then_name = bc_program_name(code, bgn);
6654 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006655
6656 if (code[*bgn] == BC_PARSE_STREND)
6657 (*bgn) += 1;
6658 else
6659 else_name = bc_program_name(code, bgn);
6660
6661 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006662 name = then_name;
6663 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006664 exec = true;
6665 name = else_name;
6666 }
6667
6668 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006669 BcVec *v;
6670 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006671 n = bc_vec_top(v);
6672 }
6673
6674 free(then_name);
6675 free(else_name);
6676
6677 if (!exec) goto exit;
6678 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006679 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006680 goto exit;
6681 }
6682
6683 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006684 } else {
6685 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006686 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006687 } else if (r->t == BC_RESULT_VAR) {
6688 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006689 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006690 if (s || !BC_PROG_STR(n)) goto exit;
6691 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006692 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006693 goto exit;
6694 }
6695
6696 fidx = sidx + BC_PROG_REQ_FUNCS;
6697
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006698 str = bc_program_str(sidx);
6699 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006700
6701 if (f->code.len == 0) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006702 common_parse_init(&prs, fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006703 s = bc_parse_text(&prs, *str);
6704 if (s) goto err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01006705 s = common_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006706 if (s) goto err;
6707
6708 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006709 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006710 goto err;
6711 }
6712
6713 bc_parse_free(&prs);
6714 }
6715
6716 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006717 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006718 ip.func = fidx;
6719
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006720 bc_vec_pop(&G.prog.results);
6721 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006722
6723 return BC_STATUS_SUCCESS;
6724
6725err:
6726 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006727 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006728 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006729exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006730 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006731 return s;
6732}
6733#endif // ENABLE_DC
6734
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006735static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006736{
Gavin Howard01055ba2018-11-03 11:00:21 -06006737 BcResult res;
6738 unsigned long val;
6739
6740 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6741 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006742 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006743 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006744 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006745 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006746 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006747
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006748 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006749 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006750 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006751}
6752
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006753static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006754{
Gavin Howard01055ba2018-11-03 11:00:21 -06006755 BcId entry, *entry_ptr;
6756 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006757 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006758
6759 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006760 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006761
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006762 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6763 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006764
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006765 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006766 *idx = entry_ptr->idx;
6767
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006768 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006769
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006770 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006771
6772 // We need to reset these, so the function can be repopulated.
6773 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006774 bc_vec_pop_all(&func->autos);
6775 bc_vec_pop_all(&func->code);
6776 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006777 }
6778 else {
6779 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006780 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006781 }
6782}
6783
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006784static BcStatus bc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006785{
Gavin Howard01055ba2018-11-03 11:00:21 -06006786 BcResult r, *ptr;
6787 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006788 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006789 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006790 char *code = func->code.v;
6791 bool cond = false;
6792
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006793 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006794 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006795 char inst = code[(ip->idx)++];
6796
6797 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006798#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006799 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006800 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006801 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006802 cond = !bc_num_cmp(num, &G.prog.zero);
6803 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006804 // Fallthrough.
6805 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006806 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006807 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006808 addr = bc_vec_item(&func->labels, idx);
6809 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6810 break;
6811 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006812 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006813 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006814 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006815 case BC_INST_INC_PRE:
6816 case BC_INST_DEC_PRE:
6817 case BC_INST_INC_POST:
6818 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006819 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006820 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006821 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006822 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006823 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006824 case BC_INST_RET:
6825 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006826 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006827 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006828 case BC_INST_BOOL_OR:
6829 case BC_INST_BOOL_AND:
6830#endif // ENABLE_BC
6831 case BC_INST_REL_EQ:
6832 case BC_INST_REL_LE:
6833 case BC_INST_REL_GE:
6834 case BC_INST_REL_NE:
6835 case BC_INST_REL_LT:
6836 case BC_INST_REL_GT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006837 s = bc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006838 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006839 case BC_INST_READ:
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006840 s = bc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006841 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006842 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006843 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006844 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006845 case BC_INST_ARRAY_ELEM:
6846 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006847 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006848 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006849 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006850 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006851 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006852 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006853 case BC_INST_IBASE:
6854 case BC_INST_SCALE:
6855 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006856 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006857 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006858 case BC_INST_SCALE_FUNC:
6859 case BC_INST_LENGTH:
6860 case BC_INST_SQRT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006861 s = bc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006862 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006863 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006864 r.t = BC_RESULT_CONSTANT;
6865 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006866 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006867 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006868 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006869 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006870 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006871 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006872 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006873 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006874 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006875 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006876 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006877 case BC_INST_PRINT:
6878 case BC_INST_PRINT_POP:
6879 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006880 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006881 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006882 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006883 r.t = BC_RESULT_STR;
6884 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006885 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006886 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006887 case BC_INST_POWER:
6888 case BC_INST_MULTIPLY:
6889 case BC_INST_DIVIDE:
6890 case BC_INST_MODULUS:
6891 case BC_INST_PLUS:
6892 case BC_INST_MINUS:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006893 s = bc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006894 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006895 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006896 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006897 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006898 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006899 if (!bc_num_cmp(num, &G.prog.zero))
6900 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006901 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006902 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006903 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006904 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006905 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006906 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006907#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006908 case BC_INST_ASSIGN_POWER:
6909 case BC_INST_ASSIGN_MULTIPLY:
6910 case BC_INST_ASSIGN_DIVIDE:
6911 case BC_INST_ASSIGN_MODULUS:
6912 case BC_INST_ASSIGN_PLUS:
6913 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006914#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006915 case BC_INST_ASSIGN:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006916 s = bc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006917 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006918#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006919 case BC_INST_MODEXP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006920 s = bc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006921 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006922 case BC_INST_DIVMOD:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006923 s = bc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006924 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006925 case BC_INST_EXECUTE:
6926 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006927 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006928 s = bc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006929 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006930 case BC_INST_PRINT_STACK: {
6931 size_t idx;
6932 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006933 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006934 if (s) break;
6935 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006936 break;
6937 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006938 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006939 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006940 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006941 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006942 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006943 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006944 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006945 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006946 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006947 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006948 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006949 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006950 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006951 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006952 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006953 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006954 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006955 ptr = bc_vec_item_rev(&G.prog.results, 0);
6956 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006957 memcpy(&r, ptr, sizeof(BcResult));
6958 memcpy(ptr, ptr2, sizeof(BcResult));
6959 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006960 break;
6961 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006962 case BC_INST_ASCIIFY:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006963 s = bc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006964 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006965 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006966 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006967 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006968 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006969 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006970 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006971 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006972 break;
6973 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006974 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006975 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006976 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006977 free(name);
6978 break;
6979 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006980 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006981 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006982 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006983 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006984 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006985 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006986 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006987 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006988#endif // ENABLE_DC
6989 }
6990
Denys Vlasenkod38af482018-12-04 19:11:02 +01006991 if (s || G_interrupt) {
6992 bc_program_reset();
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006993 return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01006994 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006995
6996 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006997 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006998 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006999 code = func->code.v;
7000 }
7001
Denys Vlasenko927a7d62018-12-09 02:24:14 +01007002 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007003}
7004
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007005#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01007006static void bc_vm_info(void)
7007{
7008 printf("%s "BB_VER"\n"
7009 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01007010 , applet_name);
7011}
7012
7013static void bc_args(char **argv)
7014{
7015 unsigned opts;
7016 int i;
7017
7018 GETOPT_RESET();
7019#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007020 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01007021 "warn\0" No_argument "w"
7022 "version\0" No_argument "v"
7023 "standard\0" No_argument "s"
7024 "quiet\0" No_argument "q"
7025 "mathlib\0" No_argument "l"
7026 "interactive\0" No_argument "i"
7027 );
7028#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007029 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01007030#endif
7031 if (getenv("POSIXLY_CORRECT"))
7032 option_mask32 |= BC_FLAG_S;
7033
Denys Vlasenko54214c32018-12-06 09:07:06 +01007034 if (opts & BC_FLAG_V) {
7035 bc_vm_info();
7036 exit(0);
7037 }
7038
7039 for (i = optind; argv[i]; ++i)
7040 bc_vec_push(&G.files, argv + i);
7041}
7042
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007043static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007044{
Gavin Howard01055ba2018-11-03 11:00:21 -06007045 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007046 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01007047 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06007048
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007049 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06007050
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007051 G.env_args = xstrdup(env_args);
7052 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06007053
7054 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06007055
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007056 while (*(buf = skip_whitespace(buf)) != '\0') {
7057 bc_vec_push(&v, &buf);
7058 buf = skip_non_whitespace(buf);
7059 if (!*buf)
7060 break;
7061 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06007062 }
7063
Denys Vlasenko54214c32018-12-06 09:07:06 +01007064 // NULL terminate, and pass argv[] so that first arg is argv[1]
7065 if (sizeof(int) == sizeof(char*)) {
7066 bc_vec_push(&v, &const_int_0);
7067 } else {
7068 static char *const nullptr = NULL;
7069 bc_vec_push(&v, &nullptr);
7070 }
7071 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06007072
7073 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06007074}
7075#endif // ENABLE_BC
7076
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007077static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06007078{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007079 char *lenv;
7080 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06007081
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007082 lenv = getenv(var);
7083 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06007084 if (!lenv) return len;
7085
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007086 len = bb_strtou(lenv, NULL, 10) - 1;
7087 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06007088 len = BC_NUM_PRINT_WIDTH;
7089
7090 return len;
7091}
7092
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007093static BcStatus bc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06007094{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007095 BcStatus s = bc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06007096
Gavin Howard01055ba2018-11-03 11:00:21 -06007097 if (s) return s;
7098
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007099 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007100 s = G.prs.parse(&G.prs);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01007101 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007102 }
7103
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007104 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007105 s = bc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01007106 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007107 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01007108 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06007109 }
7110
7111 return s;
7112}
7113
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007114static BcStatus bc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007115{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007116 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007117 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007118 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007119 BcFunc *main_func;
7120 BcInstPtr *ip;
7121
Denys Vlasenkodf515392018-12-02 19:27:48 +01007122 data = bc_read_file(file);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007123 if (!data) return bc_error_fmt("file '%s' is not text", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007124
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007125 sv_file = G.prog.file;
7126 G.prog.file = file;
7127 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007128 s = bc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007129 if (s) goto err;
7130
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007131 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007132 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007133
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007134 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007135 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007136
7137err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007138 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007139 free(data);
7140 return s;
7141}
7142
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007143static BcStatus bc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007144{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007145 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007146 BcVec buf, buffer;
Gavin Howard01055ba2018-11-03 11:00:21 -06007147 size_t len, i, str = 0;
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007148 bool comment = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06007149
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007150 G.prog.file = NULL;
7151 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007152
Denys Vlasenko7d628012018-12-04 21:46:47 +01007153 bc_char_vec_init(&buffer);
7154 bc_char_vec_init(&buf);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01007155 bc_vec_pushZeroByte(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007156
7157 // This loop is complex because the vm tries not to send any lines that end
7158 // with a backslash to the parser. The reason for that is because the parser
7159 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7160 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007161 while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007162
7163 char *string = buf.v;
7164
7165 len = buf.len - 1;
7166
7167 if (len == 1) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007168 if (str && buf.v[0] == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007169 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007170 else if (buf.v[0] == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007171 str += 1;
7172 }
7173 else if (len > 1 || comment) {
7174
7175 for (i = 0; i < len; ++i) {
7176
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007177 bool notend = len > i + 1;
7178 char c = string[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06007179
7180 if (i - 1 > len || string[i - 1] != '\\') {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007181 if (G.sbgn == G.send)
7182 str ^= c == G.sbgn;
7183 else if (c == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007184 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007185 else if (c == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007186 str += 1;
7187 }
7188
7189 if (c == '/' && notend && !comment && string[i + 1] == '*') {
7190 comment = true;
7191 break;
7192 }
7193 else if (c == '*' && notend && comment && string[i + 1] == '/')
7194 comment = false;
7195 }
7196
7197 if (str || comment || string[len - 2] == '\\') {
7198 bc_vec_concat(&buffer, buf.v);
7199 continue;
7200 }
7201 }
7202
7203 bc_vec_concat(&buffer, buf.v);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007204 s = bc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007205 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007206 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007207 // Debug config, non-interactive mode:
7208 // return all the way back to main.
7209 // Non-debug builds do not come here, they exit.
7210 break;
7211 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007212 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007213
Denys Vlasenko7d628012018-12-04 21:46:47 +01007214 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007215 }
Denys Vlasenkof522dd92018-12-07 16:35:43 +01007216 if (s == BC_STATUS_EOF) // input EOF (^D) is not an error
7217 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007218
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007219 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007220 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007221 }
7222 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007223 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007224 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007225
Gavin Howard01055ba2018-11-03 11:00:21 -06007226 bc_vec_free(&buf);
7227 bc_vec_free(&buffer);
7228 return s;
7229}
7230
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007231#if ENABLE_BC
7232static const char bc_lib[] = {
7233 "scale=20"
7234"\n" "define e(x){"
7235"\n" "auto b,s,n,r,d,i,p,f,v"
7236"\n" "b=ibase"
7237"\n" "ibase=A"
7238"\n" "if(x<0){"
7239"\n" "n=1"
7240"\n" "x=-x"
7241"\n" "}"
7242"\n" "s=scale"
7243"\n" "r=6+s+0.44*x"
7244"\n" "scale=scale(x)+1"
7245"\n" "while(x>1){"
7246"\n" "d+=1"
7247"\n" "x/=2"
7248"\n" "scale+=1"
7249"\n" "}"
7250"\n" "scale=r"
7251"\n" "r=x+1"
7252"\n" "p=x"
7253"\n" "f=v=1"
7254"\n" "for(i=2;v!=0;++i){"
7255"\n" "p*=x"
7256"\n" "f*=i"
7257"\n" "v=p/f"
7258"\n" "r+=v"
7259"\n" "}"
7260"\n" "while((d--)!=0)r*=r"
7261"\n" "scale=s"
7262"\n" "ibase=b"
7263"\n" "if(n!=0)return(1/r)"
7264"\n" "return(r/1)"
7265"\n" "}"
7266"\n" "define l(x){"
7267"\n" "auto b,s,r,p,a,q,i,v"
7268"\n" "b=ibase"
7269"\n" "ibase=A"
7270"\n" "if(x<=0){"
7271"\n" "r=(1-10^scale)/1"
7272"\n" "ibase=b"
7273"\n" "return(r)"
7274"\n" "}"
7275"\n" "s=scale"
7276"\n" "scale+=6"
7277"\n" "p=2"
7278"\n" "while(x>=2){"
7279"\n" "p*=2"
7280"\n" "x=sqrt(x)"
7281"\n" "}"
7282"\n" "while(x<=0.5){"
7283"\n" "p*=2"
7284"\n" "x=sqrt(x)"
7285"\n" "}"
7286"\n" "r=a=(x-1)/(x+1)"
7287"\n" "q=a*a"
7288"\n" "v=1"
7289"\n" "for(i=3;v!=0;i+=2){"
7290"\n" "a*=q"
7291"\n" "v=a/i"
7292"\n" "r+=v"
7293"\n" "}"
7294"\n" "r*=p"
7295"\n" "scale=s"
7296"\n" "ibase=b"
7297"\n" "return(r/1)"
7298"\n" "}"
7299"\n" "define s(x){"
7300"\n" "auto b,s,r,n,a,q,i"
7301"\n" "b=ibase"
7302"\n" "ibase=A"
7303"\n" "s=scale"
7304"\n" "scale=1.1*s+2"
7305"\n" "a=a(1)"
7306"\n" "if(x<0){"
7307"\n" "n=1"
7308"\n" "x=-x"
7309"\n" "}"
7310"\n" "scale=0"
7311"\n" "q=(x/a+2)/4"
7312"\n" "x=x-4*q*a"
7313"\n" "if(q%2!=0)x=-x"
7314"\n" "scale=s+2"
7315"\n" "r=a=x"
7316"\n" "q=-x*x"
7317"\n" "for(i=3;a!=0;i+=2){"
7318"\n" "a*=q/(i*(i-1))"
7319"\n" "r+=a"
7320"\n" "}"
7321"\n" "scale=s"
7322"\n" "ibase=b"
7323"\n" "if(n!=0)return(-r/1)"
7324"\n" "return(r/1)"
7325"\n" "}"
7326"\n" "define c(x){"
7327"\n" "auto b,s"
7328"\n" "b=ibase"
7329"\n" "ibase=A"
7330"\n" "s=scale"
7331"\n" "scale*=1.2"
7332"\n" "x=s(2*a(1)+x)"
7333"\n" "scale=s"
7334"\n" "ibase=b"
7335"\n" "return(x/1)"
7336"\n" "}"
7337"\n" "define a(x){"
7338"\n" "auto b,s,r,n,a,m,t,f,i,u"
7339"\n" "b=ibase"
7340"\n" "ibase=A"
7341"\n" "n=1"
7342"\n" "if(x<0){"
7343"\n" "n=-1"
7344"\n" "x=-x"
7345"\n" "}"
7346"\n" "if(x==1){"
7347"\n" "if(scale<65){"
7348"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7349"\n" "}"
7350"\n" "}"
7351"\n" "if(x==.2){"
7352"\n" "if(scale<65){"
7353"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7354"\n" "}"
7355"\n" "}"
7356"\n" "s=scale"
7357"\n" "if(x>.2){"
7358"\n" "scale+=5"
7359"\n" "a=a(.2)"
7360"\n" "}"
7361"\n" "scale=s+3"
7362"\n" "while(x>.2){"
7363"\n" "m+=1"
7364"\n" "x=(x-.2)/(1+.2*x)"
7365"\n" "}"
7366"\n" "r=u=x"
7367"\n" "f=-x*x"
7368"\n" "t=1"
7369"\n" "for(i=3;t!=0;i+=2){"
7370"\n" "u*=f"
7371"\n" "t=u/i"
7372"\n" "r+=t"
7373"\n" "}"
7374"\n" "scale=s"
7375"\n" "ibase=b"
7376"\n" "return((m*a+r)/n)"
7377"\n" "}"
7378"\n" "define j(n,x){"
7379"\n" "auto b,s,o,a,i,v,f"
7380"\n" "b=ibase"
7381"\n" "ibase=A"
7382"\n" "s=scale"
7383"\n" "scale=0"
7384"\n" "n/=1"
7385"\n" "if(n<0){"
7386"\n" "n=-n"
7387"\n" "if(n%2==1)o=1"
7388"\n" "}"
7389"\n" "a=1"
7390"\n" "for(i=2;i<=n;++i)a*=i"
7391"\n" "scale=1.5*s"
7392"\n" "a=(x^n)/2^n/a"
7393"\n" "r=v=1"
7394"\n" "f=-x*x/4"
7395"\n" "scale=scale+length(a)-scale(a)"
7396"\n" "for(i=1;v!=0;++i){"
7397"\n" "v=v*f/i/(n+i)"
7398"\n" "r+=v"
7399"\n" "}"
7400"\n" "scale=s"
7401"\n" "ibase=b"
7402"\n" "if(o!=0)a=-a"
7403"\n" "return(a*r/1)"
7404"\n" "}"
7405};
7406#endif // ENABLE_BC
7407
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007408static BcStatus bc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007409{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007410 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007411 size_t i;
7412
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007413#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007414 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007415
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007416 // We know that internal library is not buggy,
7417 // thus error checking is normally disabled.
7418# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007419 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007420 s = bc_parse_text(&G.prs, bc_lib);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007421 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007422
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007423 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007424 s = G.prs.parse(&G.prs);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007425 if (DEBUG_LIB && s) return s;
7426 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007427 s = bc_program_exec();
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007428 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007429 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007430#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007431
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007432 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007433 for (i = 0; !s && i < G.files.len; ++i)
7434 s = bc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007435 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7436 // Debug config, non-interactive mode:
7437 // return all the way back to main.
7438 // Non-debug builds do not come here, they exit.
7439 return s;
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007440 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007441
Denys Vlasenko91cde952018-12-10 20:56:08 +01007442 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007443 s = bc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007444
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007445 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
7446 s = bc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007447
Denys Vlasenko00d77792018-11-30 23:13:42 +01007448 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007449}
7450
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007451#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007452static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007453{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007454 bc_num_free(&G.prog.ib);
7455 bc_num_free(&G.prog.ob);
7456 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007457# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007458 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007459# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007460 bc_vec_free(&G.prog.fns);
7461 bc_vec_free(&G.prog.fn_map);
7462 bc_vec_free(&G.prog.vars);
7463 bc_vec_free(&G.prog.var_map);
7464 bc_vec_free(&G.prog.arrs);
7465 bc_vec_free(&G.prog.arr_map);
7466 bc_vec_free(&G.prog.strs);
7467 bc_vec_free(&G.prog.consts);
7468 bc_vec_free(&G.prog.results);
7469 bc_vec_free(&G.prog.stack);
7470 bc_num_free(&G.prog.last);
7471 bc_num_free(&G.prog.zero);
7472 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007473}
7474
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007475static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007476{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007477 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007478 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007479 bc_parse_free(&G.prs);
7480 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007481}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007482#endif
7483
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007484static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007485{
7486 size_t idx;
7487 BcInstPtr ip;
7488
7489 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7490 memset(&ip, 0, sizeof(BcInstPtr));
7491
7492 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007493 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007494 bc_num_ten(&G.prog.ib);
7495 G.prog.ib_t = 10;
7496
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007497 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007498 bc_num_ten(&G.prog.ob);
7499 G.prog.ob_t = 10;
7500
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007501 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007502 bc_num_ten(&G.prog.hexb);
7503 G.prog.hexb.num[0] = 6;
7504
7505#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007506 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007507 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7508#endif
7509
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007510 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007511 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007512
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007513 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007514 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007515
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007516 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007517 bc_num_one(&G.prog.one);
7518
7519 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007520 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007521
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007522 bc_program_addFunc(xstrdup("(main)"), &idx);
7523 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007524
7525 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007526 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007527
7528 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007529 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007530
7531 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7532 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7533 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7534 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7535 bc_vec_push(&G.prog.stack, &ip);
7536}
Gavin Howard01055ba2018-11-03 11:00:21 -06007537
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007538static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007539{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007540#if ENABLE_FEATURE_EDITING
7541 G.line_input_state = new_line_input_t(DO_HISTORY);
7542#endif
7543 G.prog.len = bc_vm_envLen(env_len);
7544
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007545 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007546 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007547 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007548 bc_program_init();
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007549 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007550 IF_BC(bc_parse_init(&G.prs, BC_PROG_MAIN);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007551 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007552 IF_DC(dc_parse_init(&G.prs, BC_PROG_MAIN);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007553 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007554
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007555 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007556#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007557 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007558 // With SA_RESTART, most system calls will restart
7559 // (IOW: they won't fail with EINTR).
7560 // In particular, this means ^C won't cause
7561 // stdout to get into "error state" if SIGINT hits
7562 // within write() syscall.
7563 // The downside is that ^C while line input is taken
7564 // will only be handled after [Enter] since read()
7565 // from stdin is not interrupted by ^C either,
7566 // it restarts, thus fgetc() does not return on ^C.
7567 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7568
7569 // Without SA_RESTART, this exhibits a bug:
7570 // "while (1) print 1" and try ^C-ing it.
7571 // Intermittently, instead of returning to input line,
7572 // you'll get "output error: Interrupted system call"
7573 // and exit.
7574 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007575#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007576 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007577 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007578 return 0; // "not a tty"
7579}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007580
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007581static BcStatus bc_vm_run(void)
7582{
7583 BcStatus st = bc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007584#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007585 if (G_exiting) // it was actually "halt" or "quit"
7586 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007587 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007588# if ENABLE_FEATURE_EDITING
7589 free_line_input_t(G.line_input_state);
7590# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007591 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007592#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007593 return st;
7594}
7595
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007596#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007597int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007598int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007599{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007600 int is_tty;
7601
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007602 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007603 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007604
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007605 is_tty = bc_vm_init("BC_LINE_LENGTH");
7606
7607 bc_args(argv);
7608
7609 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7610 bc_vm_info();
7611
7612 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007613}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007614#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007615
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007616#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007617int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007618int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007619{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007620 int noscript;
7621
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007622 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007623 G.sbgn = '[';
7624 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007625 /*
7626 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7627 * 1 char wider than bc from the same package.
7628 * Both default width, and xC_LINE_LENGTH=N are wider:
7629 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7630 * 1234\
7631 * 56
7632 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7633 * 123\
7634 * 456
7635 * Do the same, or it's a bug?
7636 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007637 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007638
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007639 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7640 noscript = BC_FLAG_I;
7641 for (;;) {
7642 int n = getopt(argc, argv, "e:f:x");
7643 if (n <= 0)
7644 break;
7645 switch (n) {
7646 case 'e':
7647 noscript = 0;
7648 n = bc_vm_process(optarg);
7649 if (n) return n;
7650 break;
7651 case 'f':
7652 noscript = 0;
7653 bc_vm_file(optarg);
7654 break;
7655 case 'x':
7656 option_mask32 |= DC_FLAG_X;
7657 break;
7658 default:
7659 bb_show_usage();
7660 }
7661 }
7662 argv += optind;
7663
7664 while (*argv) {
7665 noscript = 0;
7666 bc_vec_push(&G.files, argv++);
7667 }
7668
7669 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7670
7671 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007672}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007673#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007674
7675#endif // not DC_SMALL