blob: 6ab3a42524119f97a17653e92ea5a7b4109b23ab [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;
2979 size_t len, hits = 0, bslashes = 0, i = 0, j;
2980 char c = buf[i];
2981 bool last_pt, pt = start == '.';
2982
2983 last_pt = pt;
2984 l->t.t = BC_LEX_NUMBER;
2985
2986 while (c != 0 && (isdigit(c) || (c >= 'A' && c <= 'F') ||
2987 (c == '.' && !pt) || (c == '\\' && buf[i + 1] == '\n')))
2988 {
2989 if (c != '\\') {
2990 last_pt = c == '.';
2991 pt = pt || last_pt;
2992 }
2993 else {
2994 ++i;
2995 bslashes += 1;
2996 }
2997
2998 c = buf[++i];
2999 }
3000
Denys Vlasenkod5f77032018-12-04 21:37:56 +01003001 len = i + !last_pt - bslashes * 2;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003002 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
3003 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
3004 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01003005 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003006 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003007
Denys Vlasenko7d628012018-12-04 21:46:47 +01003008 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003009 bc_vec_expand(&l->t.v, len + 1);
3010 bc_vec_push(&l->t.v, &start);
3011
3012 for (buf -= 1, j = 1; j < len + hits * 2; ++j) {
3013
3014 c = buf[j];
3015
3016 // If we have hit a backslash, skip it. We don't have
3017 // to check for a newline because it's guaranteed.
3018 if (hits < bslashes && c == '\\') {
3019 ++hits;
3020 ++j;
3021 continue;
3022 }
3023
3024 bc_vec_push(&l->t.v, &c);
3025 }
3026
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003027 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003028 l->i += i;
3029
Denys Vlasenko29301232018-12-11 15:29:32 +01003030 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003031}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01003032#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003033# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01003034#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003035
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003036static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003037{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003038 size_t i;
3039 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003040
3041 l->t.t = BC_LEX_NAME;
3042
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003043 i = 0;
3044 buf = l->buf + l->i - 1;
3045 for (;;) {
3046 char c = buf[i];
3047 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
3048 i++;
3049 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003050
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003051#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01003052 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3053 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3054 if (i > BC_MAX_STRING)
3055 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
3056 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003057#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003058 bc_vec_string(&l->t.v, i, buf);
3059
3060 // Increment the index. We minus 1 because it has already been incremented.
3061 l->i += i - 1;
3062
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003063 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003064}
3065
3066static void bc_lex_init(BcLex *l, BcLexNext next)
3067{
3068 l->next = next;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003069 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003070}
3071
3072static void bc_lex_free(BcLex *l)
3073{
3074 bc_vec_free(&l->t.v);
3075}
3076
Denys Vlasenko0409ad32018-12-05 16:39:22 +01003077static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003078{
Denys Vlasenko5318f812018-12-05 17:48:01 +01003079 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003080 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06003081}
3082
3083static BcStatus bc_lex_next(BcLex *l)
3084{
3085 BcStatus s;
3086
3087 l->t.last = l->t.t;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003088 if (l->t.last == BC_LEX_EOF) return bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06003089
3090 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003091 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003092 l->t.t = BC_LEX_EOF;
3093
3094 l->newline = (l->i == l->len);
3095 if (l->newline) return BC_STATUS_SUCCESS;
3096
3097 // Loop until failure or we don't have whitespace. This
3098 // is so the parser doesn't get inundated with whitespace.
3099 do {
3100 s = l->next(l);
3101 } while (!s && l->t.t == BC_LEX_WHITESPACE);
3102
3103 return s;
3104}
3105
3106static BcStatus bc_lex_text(BcLex *l, const char *text)
3107{
3108 l->buf = text;
3109 l->i = 0;
3110 l->len = strlen(text);
3111 l->t.t = l->t.last = BC_LEX_INVALID;
3112 return bc_lex_next(l);
3113}
3114
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003115#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06003116static BcStatus bc_lex_identifier(BcLex *l)
3117{
3118 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003119 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003120 const char *buf = l->buf + l->i - 1;
3121
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003122 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3123 const char *keyword8 = bc_lex_kws[i].name8;
3124 unsigned j = 0;
3125 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3126 j++;
3127 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003128 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003129 if (keyword8[j] != '\0')
3130 continue;
3131 match:
3132 // buf starts with keyword bc_lex_kws[i]
3133 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003134 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003135 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003136 if (s) return s;
3137 }
3138
3139 // We minus 1 because the index has already been incremented.
3140 l->i += j - 1;
3141 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003142 }
3143
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003144 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003145
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003146 if (l->t.v.len > 2) {
3147 // Prevent this:
3148 // >>> qwe=1
3149 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3150 // '
3151 unsigned len = strchrnul(buf, '\n') - buf;
3152 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3153 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003154
3155 return s;
3156}
3157
3158static BcStatus bc_lex_string(BcLex *l)
3159{
3160 size_t len, nls = 0, i = l->i;
3161 char c;
3162
3163 l->t.t = BC_LEX_STR;
3164
3165 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n');
3166
3167 if (c == '\0') {
3168 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003169 return bc_error("string end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003170 }
3171
3172 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003173 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3174 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3175 if (len > BC_MAX_STRING)
3176 return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]");
3177 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003178 bc_vec_string(&l->t.v, len, l->buf + l->i);
3179
3180 l->i = i + 1;
3181 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003182 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003183
3184 return BC_STATUS_SUCCESS;
3185}
3186
3187static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3188{
3189 if (l->buf[l->i] == '=') {
3190 ++l->i;
3191 l->t.t = with;
3192 }
3193 else
3194 l->t.t = without;
3195}
3196
Denys Vlasenko29301232018-12-11 15:29:32 +01003197static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003198{
3199 size_t i, nls = 0;
3200 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003201
3202 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003203 i = ++l->i;
3204 for (;;) {
3205 char c = buf[i];
3206 check_star:
3207 if (c == '*') {
3208 c = buf[++i];
3209 if (c == '/')
3210 break;
3211 goto check_star;
3212 }
3213 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003214 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003215 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003217 nls += (c == '\n');
3218 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 }
3220
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003221 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003223 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003224
Denys Vlasenko29301232018-12-11 15:29:32 +01003225 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003226}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003227#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003228# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003229#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003230
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003231static FAST_FUNC BcStatus bc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003232{
3233 BcStatus s = BC_STATUS_SUCCESS;
3234 char c = l->buf[l->i++], c2;
3235
3236 // This is the workhorse of the lexer.
3237 switch (c) {
3238
3239 case '\0':
3240 case '\n':
3241 {
3242 l->newline = true;
3243 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3244 break;
3245 }
3246
3247 case '\t':
3248 case '\v':
3249 case '\f':
3250 case '\r':
3251 case ' ':
3252 {
3253 bc_lex_whitespace(l);
3254 break;
3255 }
3256
3257 case '!':
3258 {
3259 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3260
3261 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003262 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Gavin Howard01055ba2018-11-03 11:00:21 -06003263 if (s) return s;
3264 }
3265
3266 break;
3267 }
3268
3269 case '"':
3270 {
3271 s = bc_lex_string(l);
3272 break;
3273 }
3274
3275 case '#':
3276 {
Denys Vlasenko00646792018-12-05 18:12:27 +01003277 s = bc_POSIX_does_not_allow("'#' script comments");
Gavin Howard01055ba2018-11-03 11:00:21 -06003278 if (s) return s;
3279
3280 bc_lex_lineComment(l);
3281
3282 break;
3283 }
3284
3285 case '%':
3286 {
3287 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3288 break;
3289 }
3290
3291 case '&':
3292 {
3293 c2 = l->buf[l->i];
3294 if (c2 == '&') {
3295
Denys Vlasenko00646792018-12-05 18:12:27 +01003296 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Gavin Howard01055ba2018-11-03 11:00:21 -06003297 if (s) return s;
3298
3299 ++l->i;
3300 l->t.t = BC_LEX_OP_BOOL_AND;
3301 }
3302 else {
3303 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003304 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003305 }
3306
3307 break;
3308 }
3309
3310 case '(':
3311 case ')':
3312 {
3313 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3314 break;
3315 }
3316
3317 case '*':
3318 {
3319 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3320 break;
3321 }
3322
3323 case '+':
3324 {
3325 c2 = l->buf[l->i];
3326 if (c2 == '+') {
3327 ++l->i;
3328 l->t.t = BC_LEX_OP_INC;
3329 }
3330 else
3331 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3332 break;
3333 }
3334
3335 case ',':
3336 {
3337 l->t.t = BC_LEX_COMMA;
3338 break;
3339 }
3340
3341 case '-':
3342 {
3343 c2 = l->buf[l->i];
3344 if (c2 == '-') {
3345 ++l->i;
3346 l->t.t = BC_LEX_OP_DEC;
3347 }
3348 else
3349 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3350 break;
3351 }
3352
3353 case '.':
3354 {
3355 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003356 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003357 else {
3358 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003359 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003360 }
3361 break;
3362 }
3363
3364 case '/':
3365 {
3366 c2 = l->buf[l->i];
3367 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003368 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003369 else
3370 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3371 break;
3372 }
3373
3374 case '0':
3375 case '1':
3376 case '2':
3377 case '3':
3378 case '4':
3379 case '5':
3380 case '6':
3381 case '7':
3382 case '8':
3383 case '9':
3384 case 'A':
3385 case 'B':
3386 case 'C':
3387 case 'D':
3388 case 'E':
3389 case 'F':
3390 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003391 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003392 break;
3393 }
3394
3395 case ';':
3396 {
3397 l->t.t = BC_LEX_SCOLON;
3398 break;
3399 }
3400
3401 case '<':
3402 {
3403 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3404 break;
3405 }
3406
3407 case '=':
3408 {
3409 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3410 break;
3411 }
3412
3413 case '>':
3414 {
3415 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3416 break;
3417 }
3418
3419 case '[':
3420 case ']':
3421 {
3422 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3423 break;
3424 }
3425
3426 case '\\':
3427 {
3428 if (l->buf[l->i] == '\n') {
3429 l->t.t = BC_LEX_WHITESPACE;
3430 ++l->i;
3431 }
3432 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003433 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003434 break;
3435 }
3436
3437 case '^':
3438 {
3439 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3440 break;
3441 }
3442
3443 case 'a':
3444 case 'b':
3445 case 'c':
3446 case 'd':
3447 case 'e':
3448 case 'f':
3449 case 'g':
3450 case 'h':
3451 case 'i':
3452 case 'j':
3453 case 'k':
3454 case 'l':
3455 case 'm':
3456 case 'n':
3457 case 'o':
3458 case 'p':
3459 case 'q':
3460 case 'r':
3461 case 's':
3462 case 't':
3463 case 'u':
3464 case 'v':
3465 case 'w':
3466 case 'x':
3467 case 'y':
3468 case 'z':
3469 {
3470 s = bc_lex_identifier(l);
3471 break;
3472 }
3473
3474 case '{':
3475 case '}':
3476 {
3477 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3478 break;
3479 }
3480
3481 case '|':
3482 {
3483 c2 = l->buf[l->i];
3484
3485 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003486 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Gavin Howard01055ba2018-11-03 11:00:21 -06003487 if (s) return s;
3488
3489 ++l->i;
3490 l->t.t = BC_LEX_OP_BOOL_OR;
3491 }
3492 else {
3493 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003494 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003495 }
3496
3497 break;
3498 }
3499
3500 default:
3501 {
3502 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003503 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003504 break;
3505 }
3506 }
3507
3508 return s;
3509}
3510#endif // ENABLE_BC
3511
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003512#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003513static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003514{
Gavin Howard01055ba2018-11-03 11:00:21 -06003515 if (isspace(l->buf[l->i - 1])) {
3516 bc_lex_whitespace(l);
3517 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003518 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003519 RETURN_STATUS(bc_error("extended register"));
3520 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003521 }
3522 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003523 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003524 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003525 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003526 l->t.t = BC_LEX_NAME;
3527 }
3528
Denys Vlasenko29301232018-12-11 15:29:32 +01003529 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003530}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003531#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003532# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003533#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003534
Denys Vlasenko29301232018-12-11 15:29:32 +01003535static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003536{
3537 size_t depth = 1, nls = 0, i = l->i;
3538 char c;
3539
3540 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003541 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003542
3543 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3544
3545 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3546 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3547 nls += (c == '\n');
3548
3549 if (depth) bc_vec_push(&l->t.v, &c);
3550 }
3551
3552 if (c == '\0') {
3553 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003554 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003555 }
3556
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003557 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003558 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3559 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3560 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003561 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003562 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003563
3564 l->i = i;
3565 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003566 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003567
Denys Vlasenko29301232018-12-11 15:29:32 +01003568 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003569}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003570#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003571# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003572#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003573
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003574static FAST_FUNC BcStatus dc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003575{
3576 BcStatus s = BC_STATUS_SUCCESS;
3577 char c = l->buf[l->i++], c2;
3578 size_t i;
3579
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003580 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3581 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko29301232018-12-11 15:29:32 +01003582 return zdc_lex_register(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003583 }
3584
3585 if (c >= '%' && c <= '~' &&
3586 (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID)
3587 {
3588 return s;
3589 }
3590
3591 // This is the workhorse of the lexer.
3592 switch (c) {
3593
3594 case '\0':
3595 {
3596 l->t.t = BC_LEX_EOF;
3597 break;
3598 }
3599
3600 case '\n':
3601 case '\t':
3602 case '\v':
3603 case '\f':
3604 case '\r':
3605 case ' ':
3606 {
3607 l->newline = (c == '\n');
3608 bc_lex_whitespace(l);
3609 break;
3610 }
3611
3612 case '!':
3613 {
3614 c2 = l->buf[l->i];
3615
3616 if (c2 == '=')
3617 l->t.t = BC_LEX_OP_REL_NE;
3618 else if (c2 == '<')
3619 l->t.t = BC_LEX_OP_REL_LE;
3620 else if (c2 == '>')
3621 l->t.t = BC_LEX_OP_REL_GE;
3622 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003623 return bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003624
3625 ++l->i;
3626 break;
3627 }
3628
3629 case '#':
3630 {
3631 bc_lex_lineComment(l);
3632 break;
3633 }
3634
3635 case '.':
3636 {
3637 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003638 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003639 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003640 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003641 break;
3642 }
3643
3644 case '0':
3645 case '1':
3646 case '2':
3647 case '3':
3648 case '4':
3649 case '5':
3650 case '6':
3651 case '7':
3652 case '8':
3653 case '9':
3654 case 'A':
3655 case 'B':
3656 case 'C':
3657 case 'D':
3658 case 'E':
3659 case 'F':
3660 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003661 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003662 break;
3663 }
3664
3665 case '[':
3666 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003667 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003668 break;
3669 }
3670
3671 default:
3672 {
3673 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003674 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003675 break;
3676 }
3677 }
3678
3679 return s;
3680}
3681#endif // ENABLE_DC
3682
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003683static void bc_program_addFunc(char *name, size_t *idx);
3684
Gavin Howard01055ba2018-11-03 11:00:21 -06003685static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3686{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003687 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003688 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003689}
3690
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003691#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3692
Gavin Howard01055ba2018-11-03 11:00:21 -06003693static void bc_parse_pushName(BcParse *p, char *name)
3694{
3695 size_t i = 0, len = strlen(name);
3696
3697 for (; i < len; ++i) bc_parse_push(p, name[i]);
3698 bc_parse_push(p, BC_PARSE_STREND);
3699
3700 free(name);
3701}
3702
3703static void bc_parse_pushIndex(BcParse *p, size_t idx)
3704{
3705 unsigned char amt, i, nums[sizeof(size_t)];
3706
3707 for (amt = 0; idx; ++amt) {
3708 nums[amt] = (char) idx;
3709 idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT;
3710 }
3711
3712 bc_parse_push(p, amt);
3713 for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]);
3714}
3715
3716static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3717{
3718 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003719 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003720
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003721 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003722
3723 bc_parse_push(p, BC_INST_NUM);
3724 bc_parse_pushIndex(p, idx);
3725
3726 ++(*nexs);
3727 (*prev) = BC_INST_NUM;
3728}
3729
3730static BcStatus bc_parse_text(BcParse *p, const char *text)
3731{
3732 BcStatus s;
3733
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003734 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003735
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003736 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003737 p->l.t.t = BC_LEX_INVALID;
3738 s = p->parse(p);
3739 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003740 if (!BC_PARSE_CAN_EXEC(p))
3741 return bc_error("file is not executable");
Gavin Howard01055ba2018-11-03 11:00:21 -06003742 }
3743
3744 return bc_lex_text(&p->l, text);
3745}
3746
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003747// Called when parsing or execution detects a failure,
3748// resets execution structures.
3749static void bc_program_reset(void)
3750{
3751 BcFunc *f;
3752 BcInstPtr *ip;
3753
3754 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3755 bc_vec_pop_all(&G.prog.results);
3756
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003757 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003758 ip = bc_vec_top(&G.prog.stack);
3759 ip->idx = f->code.len;
3760}
3761
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003762#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003763 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003764
Denys Vlasenkod38af482018-12-04 19:11:02 +01003765// Called when bc/dc_parse_parse() detects a failure,
3766// resets parsing structures.
3767static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003768{
3769 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003770 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003771 bc_vec_pop_all(&p->func->code);
3772 bc_vec_pop_all(&p->func->autos);
3773 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003774
3775 bc_parse_updateFunc(p, BC_PROG_MAIN);
3776 }
3777
3778 p->l.i = p->l.len;
3779 p->l.t.t = BC_LEX_EOF;
3780 p->auto_part = (p->nbraces = 0);
3781
3782 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003783 bc_vec_pop_all(&p->exits);
3784 bc_vec_pop_all(&p->conds);
3785 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003786
Denys Vlasenkod38af482018-12-04 19:11:02 +01003787 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003788}
3789
3790static void bc_parse_free(BcParse *p)
3791{
3792 bc_vec_free(&p->flags);
3793 bc_vec_free(&p->exits);
3794 bc_vec_free(&p->conds);
3795 bc_vec_free(&p->ops);
3796 bc_lex_free(&p->l);
3797}
3798
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003799static void bc_parse_create(BcParse *p, size_t func,
Gavin Howard01055ba2018-11-03 11:00:21 -06003800 BcParseParse parse, BcLexNext next)
3801{
3802 memset(p, 0, sizeof(BcParse));
3803
3804 bc_lex_init(&p->l, next);
3805 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3806 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3807 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003808 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003809 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3810
3811 p->parse = parse;
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003812 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003813 bc_parse_updateFunc(p, func);
3814}
3815
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003816#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003817
3818#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3819#define BC_PARSE_LEAF(p, rparen) \
3820 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3821 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3822
3823// We can calculate the conversion between tokens and exprs by subtracting the
3824// position of the first operator in the lex enum and adding the position of the
3825// first in the expr enum. Note: This only works for binary operators.
3826#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3827
Gavin Howard01055ba2018-11-03 11:00:21 -06003828static BcStatus bc_parse_else(BcParse *p);
3829static BcStatus bc_parse_stmt(BcParse *p);
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003830static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003831static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Gavin Howard01055ba2018-11-03 11:00:21 -06003832
3833static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3834 size_t *nexprs, bool next)
3835{
3836 BcStatus s = BC_STATUS_SUCCESS;
3837 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003838 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3839 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003840
3841 while (p->ops.len > start) {
3842
3843 t = BC_PARSE_TOP_OP(p);
3844 if (t == BC_LEX_LPAREN) break;
3845
Denys Vlasenko65437582018-12-05 19:37:19 +01003846 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003847 if (l >= r && (l != r || !left)) break;
3848
3849 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3850 bc_vec_pop(&p->ops);
3851 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3852 }
3853
3854 bc_vec_push(&p->ops, &type);
3855 if (next) s = bc_lex_next(&p->l);
3856
3857 return s;
3858}
3859
3860static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
3861{
3862 BcLexType top;
3863
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003864 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003865 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003866 top = BC_PARSE_TOP_OP(p);
3867
3868 while (top != BC_LEX_LPAREN) {
3869
3870 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3871
3872 bc_vec_pop(&p->ops);
3873 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3874
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003875 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003876 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003877 top = BC_PARSE_TOP_OP(p);
3878 }
3879
3880 bc_vec_pop(&p->ops);
3881
3882 return bc_lex_next(&p->l);
3883}
3884
3885static BcStatus bc_parse_params(BcParse *p, uint8_t flags)
3886{
3887 BcStatus s;
3888 bool comma = false;
3889 size_t nparams;
3890
3891 s = bc_lex_next(&p->l);
3892 if (s) return s;
3893
3894 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
3895
3896 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3897 s = bc_parse_expr(p, flags, bc_parse_next_param);
3898 if (s) return s;
3899
3900 comma = p->l.t.t == BC_LEX_COMMA;
3901 if (comma) {
3902 s = bc_lex_next(&p->l);
3903 if (s) return s;
3904 }
3905 }
3906
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003907 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003908 bc_parse_push(p, BC_INST_CALL);
3909 bc_parse_pushIndex(p, nparams);
3910
3911 return BC_STATUS_SUCCESS;
3912}
3913
3914static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags)
3915{
3916 BcStatus s;
3917 BcId entry, *entry_ptr;
3918 size_t idx;
3919
3920 entry.name = name;
3921
3922 s = bc_parse_params(p, flags);
3923 if (s) goto err;
3924
3925 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003926 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003927 goto err;
3928 }
3929
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003930 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003931
3932 if (idx == BC_VEC_INVALID_IDX) {
3933 name = xstrdup(entry.name);
3934 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003935 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003936 free(entry.name);
3937 }
3938 else
3939 free(name);
3940
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003941 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003942 bc_parse_pushIndex(p, entry_ptr->idx);
3943
3944 return bc_lex_next(&p->l);
3945
3946err:
3947 free(name);
3948 return s;
3949}
3950
3951static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
3952{
3953 BcStatus s;
3954 char *name;
3955
3956 name = xstrdup(p->l.t.v.v);
3957 s = bc_lex_next(&p->l);
3958 if (s) goto err;
3959
3960 if (p->l.t.t == BC_LEX_LBRACKET) {
3961
3962 s = bc_lex_next(&p->l);
3963 if (s) goto err;
3964
3965 if (p->l.t.t == BC_LEX_RBRACKET) {
3966
3967 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003968 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003969 goto err;
3970 }
3971
3972 *type = BC_INST_ARRAY;
3973 }
3974 else {
3975
3976 *type = BC_INST_ARRAY_ELEM;
3977
3978 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3979 s = bc_parse_expr(p, flags, bc_parse_next_elem);
3980 if (s) goto err;
3981 }
3982
3983 s = bc_lex_next(&p->l);
3984 if (s) goto err;
3985 bc_parse_push(p, *type);
3986 bc_parse_pushName(p, name);
3987 }
3988 else if (p->l.t.t == BC_LEX_LPAREN) {
3989
3990 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003991 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003992 goto err;
3993 }
3994
3995 *type = BC_INST_CALL;
3996 s = bc_parse_call(p, name, flags);
3997 }
3998 else {
3999 *type = BC_INST_VAR;
4000 bc_parse_push(p, BC_INST_VAR);
4001 bc_parse_pushName(p, name);
4002 }
4003
4004 return s;
4005
4006err:
4007 free(name);
4008 return s;
4009}
4010
4011static BcStatus bc_parse_read(BcParse *p)
4012{
4013 BcStatus s;
4014
4015 s = bc_lex_next(&p->l);
4016 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004017 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004018
4019 s = bc_lex_next(&p->l);
4020 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004021 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004022
4023 bc_parse_push(p, BC_INST_READ);
4024
4025 return bc_lex_next(&p->l);
4026}
4027
4028static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
4029 BcInst *prev)
4030{
4031 BcStatus s;
4032
4033 s = bc_lex_next(&p->l);
4034 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004035 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004036
4037 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
4038
4039 s = bc_lex_next(&p->l);
4040 if (s) return s;
4041
4042 s = bc_parse_expr(p, flags, bc_parse_next_rel);
4043 if (s) return s;
4044
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004045 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004046
4047 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
4048 bc_parse_push(p, *prev);
4049
4050 return bc_lex_next(&p->l);
4051}
4052
4053static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
4054{
4055 BcStatus s;
4056
4057 s = bc_lex_next(&p->l);
4058 if (s) return s;
4059
4060 if (p->l.t.t != BC_LEX_LPAREN) {
4061 *type = BC_INST_SCALE;
4062 bc_parse_push(p, BC_INST_SCALE);
4063 return BC_STATUS_SUCCESS;
4064 }
4065
4066 *type = BC_INST_SCALE_FUNC;
4067 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
4068
4069 s = bc_lex_next(&p->l);
4070 if (s) return s;
4071
4072 s = bc_parse_expr(p, flags, bc_parse_next_rel);
4073 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004074 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004075 bc_parse_push(p, BC_INST_SCALE_FUNC);
4076
4077 return bc_lex_next(&p->l);
4078}
4079
4080static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
4081 size_t *nexprs, uint8_t flags)
4082{
4083 BcStatus s;
4084 BcLexType type;
4085 char inst;
4086 BcInst etype = *prev;
4087
4088 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
4089 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
4090 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
4091 {
4092 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
4093 bc_parse_push(p, inst);
4094 s = bc_lex_next(&p->l);
4095 }
4096 else {
4097
4098 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
4099 *paren_expr = true;
4100
4101 s = bc_lex_next(&p->l);
4102 if (s) return s;
4103 type = p->l.t.t;
4104
4105 // Because we parse the next part of the expression
4106 // right here, we need to increment this.
4107 *nexprs = *nexprs + 1;
4108
4109 switch (type) {
4110
4111 case BC_LEX_NAME:
4112 {
4113 s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
4114 break;
4115 }
4116
4117 case BC_LEX_KEY_IBASE:
4118 case BC_LEX_KEY_LAST:
4119 case BC_LEX_KEY_OBASE:
4120 {
4121 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4122 s = bc_lex_next(&p->l);
4123 break;
4124 }
4125
4126 case BC_LEX_KEY_SCALE:
4127 {
4128 s = bc_lex_next(&p->l);
4129 if (s) return s;
4130 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004131 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004132 else
4133 bc_parse_push(p, BC_INST_SCALE);
4134 break;
4135 }
4136
4137 default:
4138 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004139 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004140 break;
4141 }
4142 }
4143
4144 if (!s) bc_parse_push(p, inst);
4145 }
4146
4147 return s;
4148}
4149
4150static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
4151 bool rparen, size_t *nexprs)
4152{
4153 BcStatus s;
4154 BcLexType type;
4155 BcInst etype = *prev;
4156
4157 s = bc_lex_next(&p->l);
4158 if (s) return s;
4159
4160 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4161 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4162 BC_LEX_OP_MINUS :
4163 BC_LEX_NEG;
4164 *prev = BC_PARSE_TOKEN_INST(type);
4165
4166 // We can just push onto the op stack because this is the largest
4167 // precedence operator that gets pushed. Inc/dec does not.
4168 if (type != BC_LEX_OP_MINUS)
4169 bc_vec_push(&p->ops, &type);
4170 else
4171 s = bc_parse_operator(p, type, ops_bgn, nexprs, false);
4172
4173 return s;
4174}
4175
4176static BcStatus bc_parse_string(BcParse *p, char inst)
4177{
4178 char *str = xstrdup(p->l.t.v.v);
4179
4180 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004181 bc_parse_pushIndex(p, G.prog.strs.len);
4182 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004183 bc_parse_push(p, inst);
4184
4185 return bc_lex_next(&p->l);
4186}
4187
4188static BcStatus bc_parse_print(BcParse *p)
4189{
4190 BcStatus s;
4191 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004192 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06004193
4194 s = bc_lex_next(&p->l);
4195 if (s) return s;
4196
4197 type = p->l.t.t;
4198
4199 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004200 return bc_error("bad print statement");
Gavin Howard01055ba2018-11-03 11:00:21 -06004201
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004202 comma = false;
4203 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004204
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004205 if (type == BC_LEX_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004206 s = bc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004207 if (s) return s;
4208 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004209 s = bc_parse_expr(p, 0, bc_parse_next_print);
4210 if (s) return s;
4211 bc_parse_push(p, BC_INST_PRINT_POP);
4212 }
4213
Gavin Howard01055ba2018-11-03 11:00:21 -06004214 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004215 if (comma) {
4216 s = bc_lex_next(&p->l);
4217 if (s) return s;
4218 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004219 type = p->l.t.t;
4220 }
4221
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004222 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004223
4224 return bc_lex_next(&p->l);
4225}
4226
4227static BcStatus bc_parse_return(BcParse *p)
4228{
4229 BcStatus s;
4230 BcLexType t;
4231 bool paren;
4232
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004233 if (!BC_PARSE_FUNC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004234
4235 s = bc_lex_next(&p->l);
4236 if (s) return s;
4237
4238 t = p->l.t.t;
4239 paren = t == BC_LEX_LPAREN;
4240
4241 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4242 bc_parse_push(p, BC_INST_RET0);
4243 else {
4244
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004245 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004246 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004247 bc_parse_push(p, BC_INST_RET0);
4248 s = bc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004249 }
Denys Vlasenko452df922018-12-05 20:28:26 +01004250 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004251
4252 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004253 s = bc_POSIX_requires("parentheses around return expressions");
Gavin Howard01055ba2018-11-03 11:00:21 -06004254 if (s) return s;
4255 }
4256
4257 bc_parse_push(p, BC_INST_RET);
4258 }
4259
4260 return s;
4261}
4262
4263static BcStatus bc_parse_endBody(BcParse *p, bool brace)
4264{
4265 BcStatus s = BC_STATUS_SUCCESS;
4266
4267 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004268 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004269
4270 if (brace) {
4271
4272 if (p->l.t.t == BC_LEX_RBRACE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004273 if (!p->nbraces) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004274 --p->nbraces;
4275 s = bc_lex_next(&p->l);
4276 if (s) return s;
4277 }
4278 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004279 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004280 }
4281
4282 if (BC_PARSE_IF(p)) {
4283
4284 uint8_t *flag_ptr;
4285
4286 while (p->l.t.t == BC_LEX_NLINE) {
4287 s = bc_lex_next(&p->l);
4288 if (s) return s;
4289 }
4290
4291 bc_vec_pop(&p->flags);
4292
4293 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4294 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4295
4296 if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p);
4297 }
4298 else if (BC_PARSE_ELSE(p)) {
4299
4300 BcInstPtr *ip;
4301 size_t *label;
4302
4303 bc_vec_pop(&p->flags);
4304
4305 ip = bc_vec_top(&p->exits);
4306 label = bc_vec_item(&p->func->labels, ip->idx);
4307 *label = p->func->code.len;
4308
4309 bc_vec_pop(&p->exits);
4310 }
4311 else if (BC_PARSE_FUNC_INNER(p)) {
4312 bc_parse_push(p, BC_INST_RET0);
4313 bc_parse_updateFunc(p, BC_PROG_MAIN);
4314 bc_vec_pop(&p->flags);
4315 }
4316 else {
4317
4318 BcInstPtr *ip = bc_vec_top(&p->exits);
4319 size_t *label = bc_vec_top(&p->conds);
4320
4321 bc_parse_push(p, BC_INST_JUMP);
4322 bc_parse_pushIndex(p, *label);
4323
4324 label = bc_vec_item(&p->func->labels, ip->idx);
4325 *label = p->func->code.len;
4326
4327 bc_vec_pop(&p->flags);
4328 bc_vec_pop(&p->exits);
4329 bc_vec_pop(&p->conds);
4330 }
4331
4332 return s;
4333}
4334
4335static void bc_parse_startBody(BcParse *p, uint8_t flags)
4336{
4337 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4338 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4339 flags |= BC_PARSE_FLAG_BODY;
4340 bc_vec_push(&p->flags, &flags);
4341}
4342
4343static void bc_parse_noElse(BcParse *p)
4344{
4345 BcInstPtr *ip;
4346 size_t *label;
4347 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4348
4349 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4350
4351 ip = bc_vec_top(&p->exits);
4352 label = bc_vec_item(&p->func->labels, ip->idx);
4353 *label = p->func->code.len;
4354
4355 bc_vec_pop(&p->exits);
4356}
4357
4358static BcStatus bc_parse_if(BcParse *p)
4359{
4360 BcStatus s;
4361 BcInstPtr ip;
4362
4363 s = bc_lex_next(&p->l);
4364 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004365 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004366
4367 s = bc_lex_next(&p->l);
4368 if (s) return s;
4369 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4370 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004371 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004372
4373 s = bc_lex_next(&p->l);
4374 if (s) return s;
4375 bc_parse_push(p, BC_INST_JUMP_ZERO);
4376
4377 ip.idx = p->func->labels.len;
4378 ip.func = ip.len = 0;
4379
4380 bc_parse_pushIndex(p, ip.idx);
4381 bc_vec_push(&p->exits, &ip);
4382 bc_vec_push(&p->func->labels, &ip.idx);
4383 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4384
4385 return BC_STATUS_SUCCESS;
4386}
4387
4388static BcStatus bc_parse_else(BcParse *p)
4389{
4390 BcInstPtr ip;
4391
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004392 if (!BC_PARSE_IF_END(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004393
4394 ip.idx = p->func->labels.len;
4395 ip.func = ip.len = 0;
4396
4397 bc_parse_push(p, BC_INST_JUMP);
4398 bc_parse_pushIndex(p, ip.idx);
4399
4400 bc_parse_noElse(p);
4401
4402 bc_vec_push(&p->exits, &ip);
4403 bc_vec_push(&p->func->labels, &ip.idx);
4404 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4405
4406 return bc_lex_next(&p->l);
4407}
4408
4409static BcStatus bc_parse_while(BcParse *p)
4410{
4411 BcStatus s;
4412 BcInstPtr ip;
4413
4414 s = bc_lex_next(&p->l);
4415 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004416 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004417 s = bc_lex_next(&p->l);
4418 if (s) return s;
4419
4420 ip.idx = p->func->labels.len;
4421
4422 bc_vec_push(&p->func->labels, &p->func->code.len);
4423 bc_vec_push(&p->conds, &ip.idx);
4424
4425 ip.idx = p->func->labels.len;
4426 ip.func = 1;
4427 ip.len = 0;
4428
4429 bc_vec_push(&p->exits, &ip);
4430 bc_vec_push(&p->func->labels, &ip.idx);
4431
4432 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4433 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004434 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004435 s = bc_lex_next(&p->l);
4436 if (s) return s;
4437
4438 bc_parse_push(p, BC_INST_JUMP_ZERO);
4439 bc_parse_pushIndex(p, ip.idx);
4440 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4441
4442 return BC_STATUS_SUCCESS;
4443}
4444
4445static BcStatus bc_parse_for(BcParse *p)
4446{
4447 BcStatus s;
4448 BcInstPtr ip;
4449 size_t cond_idx, exit_idx, body_idx, update_idx;
4450
4451 s = bc_lex_next(&p->l);
4452 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004453 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004454 s = bc_lex_next(&p->l);
4455 if (s) return s;
4456
4457 if (p->l.t.t != BC_LEX_SCOLON)
4458 s = bc_parse_expr(p, 0, bc_parse_next_for);
4459 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004460 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004461
4462 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004463 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004464 s = bc_lex_next(&p->l);
4465 if (s) return s;
4466
4467 cond_idx = p->func->labels.len;
4468 update_idx = cond_idx + 1;
4469 body_idx = update_idx + 1;
4470 exit_idx = body_idx + 1;
4471
4472 bc_vec_push(&p->func->labels, &p->func->code.len);
4473
4474 if (p->l.t.t != BC_LEX_SCOLON)
4475 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
4476 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004477 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004478
4479 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004480 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004481
4482 s = bc_lex_next(&p->l);
4483 if (s) return s;
4484
4485 bc_parse_push(p, BC_INST_JUMP_ZERO);
4486 bc_parse_pushIndex(p, exit_idx);
4487 bc_parse_push(p, BC_INST_JUMP);
4488 bc_parse_pushIndex(p, body_idx);
4489
4490 ip.idx = p->func->labels.len;
4491
4492 bc_vec_push(&p->conds, &update_idx);
4493 bc_vec_push(&p->func->labels, &p->func->code.len);
4494
4495 if (p->l.t.t != BC_LEX_RPAREN)
4496 s = bc_parse_expr(p, 0, bc_parse_next_rel);
4497 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004498 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004499
4500 if (s) return s;
4501
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004502 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004503 bc_parse_push(p, BC_INST_JUMP);
4504 bc_parse_pushIndex(p, cond_idx);
4505 bc_vec_push(&p->func->labels, &p->func->code.len);
4506
4507 ip.idx = exit_idx;
4508 ip.func = 1;
4509 ip.len = 0;
4510
4511 bc_vec_push(&p->exits, &ip);
4512 bc_vec_push(&p->func->labels, &ip.idx);
4513 bc_lex_next(&p->l);
4514 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4515
4516 return BC_STATUS_SUCCESS;
4517}
4518
4519static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type)
4520{
4521 BcStatus s;
4522 size_t i;
4523 BcInstPtr *ip;
4524
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004525 if (!BC_PARSE_LOOP(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004526
4527 if (type == BC_LEX_KEY_BREAK) {
4528
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004529 if (p->exits.len == 0) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004530
4531 i = p->exits.len - 1;
4532 ip = bc_vec_item(&p->exits, i);
4533
4534 while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004535 if (i >= p->exits.len && !ip->func) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004536
4537 i = ip->idx;
4538 }
4539 else
4540 i = *((size_t *) bc_vec_top(&p->conds));
4541
4542 bc_parse_push(p, BC_INST_JUMP);
4543 bc_parse_pushIndex(p, i);
4544
4545 s = bc_lex_next(&p->l);
4546 if (s) return s;
4547
4548 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004549 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004550
4551 return bc_lex_next(&p->l);
4552}
4553
4554static BcStatus bc_parse_func(BcParse *p)
4555{
4556 BcStatus s;
4557 bool var, comma = false;
4558 uint8_t flags;
4559 char *name;
4560
4561 s = bc_lex_next(&p->l);
4562 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004563 if (p->l.t.t != BC_LEX_NAME)
4564 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004565
4566 name = xstrdup(p->l.t.v.v);
4567 bc_parse_addFunc(p, name, &p->fidx);
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_LPAREN)
4572 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004573 s = bc_lex_next(&p->l);
4574 if (s) return s;
4575
4576 while (p->l.t.t != BC_LEX_RPAREN) {
4577
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004578 if (p->l.t.t != BC_LEX_NAME)
4579 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004580
4581 ++p->func->nparams;
4582
4583 name = xstrdup(p->l.t.v.v);
4584 s = bc_lex_next(&p->l);
4585 if (s) goto err;
4586
4587 var = p->l.t.t != BC_LEX_LBRACKET;
4588
4589 if (!var) {
4590
4591 s = bc_lex_next(&p->l);
4592 if (s) goto err;
4593
4594 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004595 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004596 goto err;
4597 }
4598
4599 s = bc_lex_next(&p->l);
4600 if (s) goto err;
4601 }
4602
4603 comma = p->l.t.t == BC_LEX_COMMA;
4604 if (comma) {
4605 s = bc_lex_next(&p->l);
4606 if (s) goto err;
4607 }
4608
Denys Vlasenko29301232018-12-11 15:29:32 +01004609 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004610 if (s) goto err;
4611 }
4612
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004613 if (comma) return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004614
4615 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4616 bc_parse_startBody(p, flags);
4617
4618 s = bc_lex_next(&p->l);
4619 if (s) return s;
4620
4621 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004622 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004623
4624 return s;
4625
4626err:
4627 free(name);
4628 return s;
4629}
4630
4631static BcStatus bc_parse_auto(BcParse *p)
4632{
4633 BcStatus s;
4634 bool comma, var, one;
4635 char *name;
4636
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004637 if (!p->auto_part) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004638 s = bc_lex_next(&p->l);
4639 if (s) return s;
4640
4641 p->auto_part = comma = false;
4642 one = p->l.t.t == BC_LEX_NAME;
4643
4644 while (p->l.t.t == BC_LEX_NAME) {
4645
4646 name = xstrdup(p->l.t.v.v);
4647 s = bc_lex_next(&p->l);
4648 if (s) goto err;
4649
4650 var = p->l.t.t != BC_LEX_LBRACKET;
4651 if (!var) {
4652
4653 s = bc_lex_next(&p->l);
4654 if (s) goto err;
4655
4656 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004657 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004658 goto err;
4659 }
4660
4661 s = bc_lex_next(&p->l);
4662 if (s) goto err;
4663 }
4664
4665 comma = p->l.t.t == BC_LEX_COMMA;
4666 if (comma) {
4667 s = bc_lex_next(&p->l);
4668 if (s) goto err;
4669 }
4670
Denys Vlasenko29301232018-12-11 15:29:32 +01004671 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004672 if (s) goto err;
4673 }
4674
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004675 if (comma) return bc_error("bad function definition");
Denys Vlasenkoabbc4332018-12-03 21:46:41 +01004676 if (!one) return bc_error("no auto variable found");
Gavin Howard01055ba2018-11-03 11:00:21 -06004677
4678 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004679 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004680
4681 return bc_lex_next(&p->l);
4682
4683err:
4684 free(name);
4685 return s;
4686}
4687
4688static BcStatus bc_parse_body(BcParse *p, bool brace)
4689{
4690 BcStatus s = BC_STATUS_SUCCESS;
4691 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4692
4693 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4694
4695 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
4696
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004697 if (!brace) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004698 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4699
4700 if (!p->auto_part) {
4701 s = bc_parse_auto(p);
4702 if (s) return s;
4703 }
4704
4705 if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l);
4706 }
4707 else {
4708 s = bc_parse_stmt(p);
4709 if (!s && !brace) s = bc_parse_endBody(p, false);
4710 }
4711
4712 return s;
4713}
4714
4715static BcStatus bc_parse_stmt(BcParse *p)
4716{
4717 BcStatus s = BC_STATUS_SUCCESS;
4718
4719 switch (p->l.t.t) {
4720
4721 case BC_LEX_NLINE:
4722 {
4723 return bc_lex_next(&p->l);
4724 }
4725
4726 case BC_LEX_KEY_ELSE:
4727 {
4728 p->auto_part = false;
4729 break;
4730 }
4731
4732 case BC_LEX_LBRACE:
4733 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004734 if (!BC_PARSE_BODY(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004735
4736 ++p->nbraces;
4737 s = bc_lex_next(&p->l);
4738 if (s) return s;
4739
4740 return bc_parse_body(p, true);
4741 }
4742
4743 case BC_LEX_KEY_AUTO:
4744 {
4745 return bc_parse_auto(p);
4746 }
4747
4748 default:
4749 {
4750 p->auto_part = false;
4751
4752 if (BC_PARSE_IF_END(p)) {
4753 bc_parse_noElse(p);
4754 return BC_STATUS_SUCCESS;
4755 }
4756 else if (BC_PARSE_BODY(p))
4757 return bc_parse_body(p, false);
4758
4759 break;
4760 }
4761 }
4762
4763 switch (p->l.t.t) {
4764
4765 case BC_LEX_OP_INC:
4766 case BC_LEX_OP_DEC:
4767 case BC_LEX_OP_MINUS:
4768 case BC_LEX_OP_BOOL_NOT:
4769 case BC_LEX_LPAREN:
4770 case BC_LEX_NAME:
4771 case BC_LEX_NUMBER:
4772 case BC_LEX_KEY_IBASE:
4773 case BC_LEX_KEY_LAST:
4774 case BC_LEX_KEY_LENGTH:
4775 case BC_LEX_KEY_OBASE:
4776 case BC_LEX_KEY_READ:
4777 case BC_LEX_KEY_SCALE:
4778 case BC_LEX_KEY_SQRT:
4779 {
4780 s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
4781 break;
4782 }
4783
4784 case BC_LEX_KEY_ELSE:
4785 {
4786 s = bc_parse_else(p);
4787 break;
4788 }
4789
4790 case BC_LEX_SCOLON:
4791 {
4792 while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l);
4793 break;
4794 }
4795
4796 case BC_LEX_RBRACE:
4797 {
4798 s = bc_parse_endBody(p, true);
4799 break;
4800 }
4801
4802 case BC_LEX_STR:
4803 {
4804 s = bc_parse_string(p, BC_INST_PRINT_STR);
4805 break;
4806 }
4807
4808 case BC_LEX_KEY_BREAK:
4809 case BC_LEX_KEY_CONTINUE:
4810 {
4811 s = bc_parse_loopExit(p, p->l.t.t);
4812 break;
4813 }
4814
4815 case BC_LEX_KEY_FOR:
4816 {
4817 s = bc_parse_for(p);
4818 break;
4819 }
4820
4821 case BC_LEX_KEY_HALT:
4822 {
4823 bc_parse_push(p, BC_INST_HALT);
4824 s = bc_lex_next(&p->l);
4825 break;
4826 }
4827
4828 case BC_LEX_KEY_IF:
4829 {
4830 s = bc_parse_if(p);
4831 break;
4832 }
4833
4834 case BC_LEX_KEY_LIMITS:
4835 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004836 // "limits" is a compile-time command,
4837 // the output is produced at _parse time_.
Gavin Howard01055ba2018-11-03 11:00:21 -06004838 s = bc_lex_next(&p->l);
4839 if (s) return s;
Denys Vlasenko64074a12018-12-07 15:50:14 +01004840 printf(
4841 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4842 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4843 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4844 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4845 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4846 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4847 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4848 "Number of vars = "BC_MAX_VARS_STR "\n"
4849 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004850 break;
4851 }
4852
4853 case BC_LEX_KEY_PRINT:
4854 {
4855 s = bc_parse_print(p);
4856 break;
4857 }
4858
4859 case BC_LEX_KEY_QUIT:
4860 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004861 // "quit" is a compile-time command. For example,
4862 // "if (0 == 1) quit" terminates when parsing the statement,
4863 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004864 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004865 }
4866
4867 case BC_LEX_KEY_RETURN:
4868 {
4869 s = bc_parse_return(p);
4870 break;
4871 }
4872
4873 case BC_LEX_KEY_WHILE:
4874 {
4875 s = bc_parse_while(p);
4876 break;
4877 }
4878
4879 default:
4880 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004881 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004882 break;
4883 }
4884 }
4885
4886 return s;
4887}
4888
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01004889static FAST_FUNC BcStatus bc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004890{
4891 BcStatus s;
4892
4893 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004894 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 -06004895 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004896 if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004897 s = bc_parse_func(p);
4898 }
4899 else
4900 s = bc_parse_stmt(p);
4901
Denys Vlasenkod38af482018-12-04 19:11:02 +01004902 if (s || G_interrupt) {
4903 bc_parse_reset(p);
4904 s = BC_STATUS_FAILURE;
4905 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004906
4907 return s;
4908}
4909
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004910// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004911static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004912{
4913 BcStatus s = BC_STATUS_SUCCESS;
4914 BcInst prev = BC_INST_PRINT;
4915 BcLexType top, t = p->l.t.t;
4916 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004917 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004918 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4919
4920 paren_first = p->l.t.t == BC_LEX_LPAREN;
4921 nparens = nrelops = 0;
4922 paren_expr = rprn = done = get_token = assign = false;
4923 bin_last = true;
4924
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004925 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004926 switch (t) {
4927
4928 case BC_LEX_OP_INC:
4929 case BC_LEX_OP_DEC:
4930 {
4931 s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
4932 rprn = get_token = bin_last = false;
4933 break;
4934 }
4935
4936 case BC_LEX_OP_MINUS:
4937 {
4938 s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
4939 rprn = get_token = false;
4940 bin_last = prev == BC_INST_MINUS;
4941 break;
4942 }
4943
4944 case BC_LEX_OP_ASSIGN_POWER:
4945 case BC_LEX_OP_ASSIGN_MULTIPLY:
4946 case BC_LEX_OP_ASSIGN_DIVIDE:
4947 case BC_LEX_OP_ASSIGN_MODULUS:
4948 case BC_LEX_OP_ASSIGN_PLUS:
4949 case BC_LEX_OP_ASSIGN_MINUS:
4950 case BC_LEX_OP_ASSIGN:
4951 {
4952 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4953 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4954 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4955 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004956 s = bc_error("bad assignment:"
4957 " left side must be scale,"
4958 " ibase, obase, last, var,"
4959 " or array element"
4960 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004961 break;
4962 }
4963 }
4964 // Fallthrough.
4965 case BC_LEX_OP_POWER:
4966 case BC_LEX_OP_MULTIPLY:
4967 case BC_LEX_OP_DIVIDE:
4968 case BC_LEX_OP_MODULUS:
4969 case BC_LEX_OP_PLUS:
4970 case BC_LEX_OP_REL_EQ:
4971 case BC_LEX_OP_REL_LE:
4972 case BC_LEX_OP_REL_GE:
4973 case BC_LEX_OP_REL_NE:
4974 case BC_LEX_OP_REL_LT:
4975 case BC_LEX_OP_REL_GT:
4976 case BC_LEX_OP_BOOL_NOT:
4977 case BC_LEX_OP_BOOL_OR:
4978 case BC_LEX_OP_BOOL_AND:
4979 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004980 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4981 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4982 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004983 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004984 }
4985
4986 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4987 prev = BC_PARSE_TOKEN_INST(t);
4988 s = bc_parse_operator(p, t, ops_bgn, &nexprs, true);
4989 rprn = get_token = false;
4990 bin_last = t != BC_LEX_OP_BOOL_NOT;
4991
4992 break;
4993 }
4994
4995 case BC_LEX_LPAREN:
4996 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004997 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004998 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004999 ++nparens;
5000 paren_expr = rprn = bin_last = false;
5001 get_token = true;
5002 bc_vec_push(&p->ops, &t);
5003
5004 break;
5005 }
5006
5007 case BC_LEX_RPAREN:
5008 {
5009 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005010 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005011
5012 if (nparens == 0) {
5013 s = BC_STATUS_SUCCESS;
5014 done = true;
5015 get_token = false;
5016 break;
5017 }
5018 else if (!paren_expr)
5019 return BC_STATUS_PARSE_EMPTY_EXP;
5020
5021 --nparens;
5022 paren_expr = rprn = true;
5023 get_token = bin_last = false;
5024
5025 s = bc_parse_rightParen(p, ops_bgn, &nexprs);
5026
5027 break;
5028 }
5029
5030 case BC_LEX_NAME:
5031 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005032 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005033 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005034 paren_expr = true;
5035 rprn = get_token = bin_last = false;
5036 s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
5037 ++nexprs;
5038
5039 break;
5040 }
5041
5042 case BC_LEX_NUMBER:
5043 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005044 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005045 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005046 bc_parse_number(p, &prev, &nexprs);
5047 paren_expr = get_token = true;
5048 rprn = bin_last = false;
5049
5050 break;
5051 }
5052
5053 case BC_LEX_KEY_IBASE:
5054 case BC_LEX_KEY_LAST:
5055 case BC_LEX_KEY_OBASE:
5056 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005057 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005058 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005059 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
5060 bc_parse_push(p, (char) prev);
5061
5062 paren_expr = get_token = true;
5063 rprn = bin_last = false;
5064 ++nexprs;
5065
5066 break;
5067 }
5068
5069 case BC_LEX_KEY_LENGTH:
5070 case BC_LEX_KEY_SQRT:
5071 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005072 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005073 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005074 s = bc_parse_builtin(p, t, flags, &prev);
5075 paren_expr = true;
5076 rprn = get_token = bin_last = false;
5077 ++nexprs;
5078
5079 break;
5080 }
5081
5082 case BC_LEX_KEY_READ:
5083 {
5084 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005085 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005086 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005087 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005088 else
5089 s = bc_parse_read(p);
5090
5091 paren_expr = true;
5092 rprn = get_token = bin_last = false;
5093 ++nexprs;
5094 prev = BC_INST_READ;
5095
5096 break;
5097 }
5098
5099 case BC_LEX_KEY_SCALE:
5100 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005101 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005102 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005103 s = bc_parse_scale(p, &prev, flags);
5104 paren_expr = true;
5105 rprn = get_token = bin_last = false;
5106 ++nexprs;
5107 prev = BC_INST_SCALE;
5108
5109 break;
5110 }
5111
5112 default:
5113 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005114 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005115 break;
5116 }
5117 }
5118
5119 if (!s && get_token) s = bc_lex_next(&p->l);
5120 }
5121
5122 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01005123 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06005124
5125 while (p->ops.len > ops_bgn) {
5126
5127 top = BC_PARSE_TOP_OP(p);
5128 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
5129
5130 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005131 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005132
5133 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
5134
5135 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
5136 bc_vec_pop(&p->ops);
5137 }
5138
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005139 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005140 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005141
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005142 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
5143 for (;;) {
5144 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005145 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005146 if (next & 0x80) // last element?
5147 break;
5148 next >>= 8;
5149 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005150 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005151 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06005152
5153 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01005154 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Gavin Howard01055ba2018-11-03 11:00:21 -06005155 if (s) return s;
5156 }
5157 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01005158 s = bc_POSIX_requires("exactly one comparison operator per condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06005159 if (s) return s;
5160 }
5161
5162 if (flags & BC_PARSE_PRINT) {
5163 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
5164 bc_parse_push(p, BC_INST_POP);
5165 }
5166
5167 return s;
5168}
5169
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01005170static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
5171{
5172 BcStatus s;
5173
5174 s = bc_parse_expr_empty_ok(p, flags, next);
5175 if (s == BC_STATUS_PARSE_EMPTY_EXP)
5176 return bc_error("empty expression");
5177 return s;
5178}
5179
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005180static void bc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005181{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005182 bc_parse_create(p, func, bc_parse_parse, bc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005183}
5184
5185static BcStatus bc_parse_expression(BcParse *p, uint8_t flags)
5186{
5187 return bc_parse_expr(p, flags, bc_parse_next_read);
5188}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005189
Gavin Howard01055ba2018-11-03 11:00:21 -06005190#endif // ENABLE_BC
5191
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005192#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005193
5194#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
5195
Gavin Howard01055ba2018-11-03 11:00:21 -06005196static BcStatus dc_parse_register(BcParse *p)
5197{
5198 BcStatus s;
5199 char *name;
5200
5201 s = bc_lex_next(&p->l);
5202 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005203 if (p->l.t.t != BC_LEX_NAME) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005204
5205 name = xstrdup(p->l.t.v.v);
5206 bc_parse_pushName(p, name);
5207
5208 return s;
5209}
5210
5211static BcStatus dc_parse_string(BcParse *p)
5212{
5213 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005214 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005215
5216 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
5217 name = xstrdup(b);
5218
5219 str = xstrdup(p->l.t.v.v);
5220 bc_parse_push(p, BC_INST_STR);
5221 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005222 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005223 bc_parse_addFunc(p, name, &idx);
5224
5225 return bc_lex_next(&p->l);
5226}
5227
5228static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
5229{
5230 BcStatus s;
5231
5232 bc_parse_push(p, inst);
5233 if (name) {
5234 s = dc_parse_register(p);
5235 if (s) return s;
5236 }
5237
5238 if (store) {
5239 bc_parse_push(p, BC_INST_SWAP);
5240 bc_parse_push(p, BC_INST_ASSIGN);
5241 bc_parse_push(p, BC_INST_POP);
5242 }
5243
5244 return bc_lex_next(&p->l);
5245}
5246
5247static BcStatus dc_parse_cond(BcParse *p, uint8_t inst)
5248{
5249 BcStatus s;
5250
5251 bc_parse_push(p, inst);
5252 bc_parse_push(p, BC_INST_EXEC_COND);
5253
5254 s = dc_parse_register(p);
5255 if (s) return s;
5256
5257 s = bc_lex_next(&p->l);
5258 if (s) return s;
5259
5260 if (p->l.t.t == BC_LEX_ELSE) {
5261 s = dc_parse_register(p);
5262 if (s) return s;
5263 s = bc_lex_next(&p->l);
5264 }
5265 else
5266 bc_parse_push(p, BC_PARSE_STREND);
5267
5268 return s;
5269}
5270
5271static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
5272{
5273 BcStatus s = BC_STATUS_SUCCESS;
5274 BcInst prev;
5275 uint8_t inst;
5276 bool assign, get_token = false;
5277
5278 switch (t) {
5279
5280 case BC_LEX_OP_REL_EQ:
5281 case BC_LEX_OP_REL_LE:
5282 case BC_LEX_OP_REL_GE:
5283 case BC_LEX_OP_REL_NE:
5284 case BC_LEX_OP_REL_LT:
5285 case BC_LEX_OP_REL_GT:
5286 {
5287 s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
5288 break;
5289 }
5290
5291 case BC_LEX_SCOLON:
5292 case BC_LEX_COLON:
5293 {
5294 s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
5295 break;
5296 }
5297
5298 case BC_LEX_STR:
5299 {
5300 s = dc_parse_string(p);
5301 break;
5302 }
5303
5304 case BC_LEX_NEG:
5305 case BC_LEX_NUMBER:
5306 {
5307 if (t == BC_LEX_NEG) {
5308 s = bc_lex_next(&p->l);
5309 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005310 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005311 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005312 }
5313
5314 bc_parse_number(p, &prev, &p->nbraces);
5315
5316 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5317 get_token = true;
5318
5319 break;
5320 }
5321
5322 case BC_LEX_KEY_READ:
5323 {
5324 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005325 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005326 else
5327 bc_parse_push(p, BC_INST_READ);
5328 get_token = true;
5329 break;
5330 }
5331
5332 case BC_LEX_OP_ASSIGN:
5333 case BC_LEX_STORE_PUSH:
5334 {
5335 assign = t == BC_LEX_OP_ASSIGN;
5336 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
5337 s = dc_parse_mem(p, inst, true, assign);
5338 break;
5339 }
5340
5341 case BC_LEX_LOAD:
5342 case BC_LEX_LOAD_POP:
5343 {
5344 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
5345 s = dc_parse_mem(p, inst, true, false);
5346 break;
5347 }
5348
5349 case BC_LEX_STORE_IBASE:
5350 case BC_LEX_STORE_SCALE:
5351 case BC_LEX_STORE_OBASE:
5352 {
5353 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
5354 s = dc_parse_mem(p, inst, false, true);
5355 break;
5356 }
5357
5358 default:
5359 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005360 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005361 get_token = true;
5362 break;
5363 }
5364 }
5365
5366 if (!s && get_token) s = bc_lex_next(&p->l);
5367
5368 return s;
5369}
5370
5371static BcStatus dc_parse_expr(BcParse *p, uint8_t flags)
5372{
5373 BcStatus s = BC_STATUS_SUCCESS;
5374 BcInst inst;
5375 BcLexType t;
5376
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005377 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005378
5379 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
5380
5381 inst = dc_parse_insts[t];
5382
5383 if (inst != BC_INST_INVALID) {
5384 bc_parse_push(p, inst);
5385 s = bc_lex_next(&p->l);
5386 }
5387 else
5388 s = dc_parse_token(p, t, flags);
5389 }
5390
5391 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5392 bc_parse_push(p, BC_INST_POP_EXEC);
5393
5394 return s;
5395}
5396
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01005397static FAST_FUNC BcStatus dc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005398{
5399 BcStatus s;
5400
5401 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005402 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005403 else
5404 s = dc_parse_expr(p, 0);
5405
Denys Vlasenkod38af482018-12-04 19:11:02 +01005406 if (s || G_interrupt) {
5407 bc_parse_reset(p);
5408 s = BC_STATUS_FAILURE;
5409 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005410
5411 return s;
5412}
5413
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005414static void dc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005415{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005416 bc_parse_create(p, func, dc_parse_parse, dc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005417}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005418
Gavin Howard01055ba2018-11-03 11:00:21 -06005419#endif // ENABLE_DC
5420
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005421static void common_parse_init(BcParse *p, size_t func)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005422{
5423 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005424 IF_BC(bc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005425 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005426 IF_DC(dc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005427 }
5428}
5429
5430static BcStatus common_parse_expr(BcParse *p, uint8_t flags)
5431{
5432 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005433 IF_BC(return bc_parse_expression(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005434 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005435 IF_DC(return dc_parse_expr(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005436 }
5437}
5438
Denys Vlasenkodf515392018-12-02 19:27:48 +01005439static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005440{
Gavin Howard01055ba2018-11-03 11:00:21 -06005441 BcId e, *ptr;
5442 BcVec *v, *map;
5443 size_t i;
5444 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005445 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005446
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005447 v = var ? &G.prog.vars : &G.prog.arrs;
5448 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005449
5450 e.name = id;
5451 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005452 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005453
5454 if (new) {
5455 bc_array_init(&data.v, var);
5456 bc_vec_push(v, &data.v);
5457 }
5458
5459 ptr = bc_vec_item(map, i);
5460 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005461 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005462}
5463
Denys Vlasenko29301232018-12-11 15:29:32 +01005464static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005465{
Gavin Howard01055ba2018-11-03 11:00:21 -06005466 switch (r->t) {
5467
5468 case BC_RESULT_STR:
5469 case BC_RESULT_TEMP:
5470 case BC_RESULT_IBASE:
5471 case BC_RESULT_SCALE:
5472 case BC_RESULT_OBASE:
5473 {
5474 *num = &r->d.n;
5475 break;
5476 }
5477
5478 case BC_RESULT_CONSTANT:
5479 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005480 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005481 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005482 size_t base_t, len = strlen(*str);
5483 BcNum *base;
5484
5485 bc_num_init(&r->d.n, len);
5486
5487 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005488 base = hex ? &G.prog.hexb : &G.prog.ib;
5489 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005490 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005491
5492 if (s) {
5493 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005494 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005495 }
5496
5497 *num = &r->d.n;
5498 r->t = BC_RESULT_TEMP;
5499
5500 break;
5501 }
5502
5503 case BC_RESULT_VAR:
5504 case BC_RESULT_ARRAY:
5505 case BC_RESULT_ARRAY_ELEM:
5506 {
5507 BcVec *v;
5508
Denys Vlasenkodf515392018-12-02 19:27:48 +01005509 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005510
5511 if (r->t == BC_RESULT_ARRAY_ELEM) {
5512 v = bc_vec_top(v);
5513 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5514 *num = bc_vec_item(v, r->d.id.idx);
5515 }
5516 else
5517 *num = bc_vec_top(v);
5518
5519 break;
5520 }
5521
5522 case BC_RESULT_LAST:
5523 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005524 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005525 break;
5526 }
5527
5528 case BC_RESULT_ONE:
5529 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005530 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005531 break;
5532 }
5533 }
5534
Denys Vlasenko29301232018-12-11 15:29:32 +01005535 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005536}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005537#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005538# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005539#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005540
Denys Vlasenko29301232018-12-11 15:29:32 +01005541static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005542 BcResult **r, BcNum **rn, bool assign)
5543{
5544 BcStatus s;
5545 bool hex;
5546 BcResultType lt, rt;
5547
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005548 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005549 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005550
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005551 *r = bc_vec_item_rev(&G.prog.results, 0);
5552 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005553
5554 lt = (*l)->t;
5555 rt = (*r)->t;
5556 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5557
Denys Vlasenko29301232018-12-11 15:29:32 +01005558 s = zbc_program_num(*l, ln, false);
5559 if (s) RETURN_STATUS(s);
5560 s = zbc_program_num(*r, rn, hex);
5561 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005562
5563 // We run this again under these conditions in case any vector has been
5564 // reallocated out from under the BcNums or arrays we had.
5565 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005566 s = zbc_program_num(*l, ln, false);
5567 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005568 }
5569
5570 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005571 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005572 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005573 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005574
Denys Vlasenko29301232018-12-11 15:29:32 +01005575 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005576}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005577#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005578# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005579#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005580
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005581static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005582{
5583 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005584 bc_vec_pop(&G.prog.results);
5585 bc_vec_pop(&G.prog.results);
5586 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005587}
5588
Denys Vlasenko29301232018-12-11 15:29:32 +01005589static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005590{
5591 BcStatus s;
5592
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005593 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005594 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005595 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005596
Denys Vlasenko29301232018-12-11 15:29:32 +01005597 s = zbc_program_num(*r, n, false);
5598 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005599
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005600 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005601 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005602
Denys Vlasenko29301232018-12-11 15:29:32 +01005603 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005604}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005605#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005606# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005607#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005608
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005609static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005610{
5611 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005612 bc_vec_pop(&G.prog.results);
5613 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005614}
5615
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005616static BcStatus bc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005617{
5618 BcStatus s;
5619 BcResult *opd1, *opd2, res;
5620 BcNum *n1, *n2 = NULL;
5621
Denys Vlasenko29301232018-12-11 15:29:32 +01005622 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005623 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005624 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005625
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005626 s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005627 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005628 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005629
5630 return s;
5631
5632err:
5633 bc_num_free(&res.d.n);
5634 return s;
5635}
5636
Denys Vlasenko785e4b32018-12-02 17:18:52 +01005637static BcStatus bc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005638{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005639 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005640 BcStatus s;
5641 BcParse parse;
5642 BcVec buf;
5643 BcInstPtr ip;
5644 size_t i;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005645 BcFunc *f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005646
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005647 for (i = 0; i < G.prog.stack.len; ++i) {
5648 BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005649 if (ip_ptr->func == BC_PROG_READ)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005650 return bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005651 }
5652
Denys Vlasenko7d628012018-12-04 21:46:47 +01005653 bc_vec_pop_all(&f->code);
5654 bc_char_vec_init(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005655
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005656 sv_file = G.prog.file;
5657 G.prog.file = NULL;
5658
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01005659 s = bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005660 if (s) goto io_err;
5661
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005662 common_parse_init(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005663 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005664
5665 s = bc_parse_text(&parse, buf.v);
5666 if (s) goto exec_err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005667 s = common_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005668 if (s) goto exec_err;
5669
5670 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005671 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005672 goto exec_err;
5673 }
5674
5675 ip.func = BC_PROG_READ;
5676 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005677 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005678
5679 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005680 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005681
5682 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005683 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005684
5685exec_err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005686 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005687 bc_parse_free(&parse);
5688io_err:
5689 bc_vec_free(&buf);
5690 return s;
5691}
5692
5693static size_t bc_program_index(char *code, size_t *bgn)
5694{
5695 char amt = code[(*bgn)++], i = 0;
5696 size_t res = 0;
5697
5698 for (; i < amt; ++i, ++(*bgn))
5699 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5700
5701 return res;
5702}
5703
5704static char *bc_program_name(char *code, size_t *bgn)
5705{
5706 size_t i;
5707 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5708
5709 s = xmalloc(ptr - str + 1);
5710 c = code[(*bgn)++];
5711
5712 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5713 s[i] = c;
5714
5715 s[i] = '\0';
5716
5717 return s;
5718}
5719
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005720static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005721{
5722 size_t i, len = strlen(str);
5723
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005724#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005725 if (len == 0) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005726 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005727 return;
5728 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005729#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005730
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005731 for (i = 0; i < len; ++i, ++G.prog.nchars) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005732
5733 int c = str[i];
5734
5735 if (c != '\\' || i == len - 1)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005736 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005737 else {
5738
5739 c = str[++i];
5740
5741 switch (c) {
5742
5743 case 'a':
5744 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005745 bb_putchar('\a');
Gavin Howard01055ba2018-11-03 11:00:21 -06005746 break;
5747 }
5748
5749 case 'b':
5750 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005751 bb_putchar('\b');
Gavin Howard01055ba2018-11-03 11:00:21 -06005752 break;
5753 }
5754
5755 case '\\':
5756 case 'e':
5757 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005758 bb_putchar('\\');
Gavin Howard01055ba2018-11-03 11:00:21 -06005759 break;
5760 }
5761
5762 case 'f':
5763 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005764 bb_putchar('\f');
Gavin Howard01055ba2018-11-03 11:00:21 -06005765 break;
5766 }
5767
5768 case 'n':
5769 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005770 bb_putchar('\n');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005771 G.prog.nchars = SIZE_MAX;
Gavin Howard01055ba2018-11-03 11:00:21 -06005772 break;
5773 }
5774
5775 case 'r':
5776 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005777 bb_putchar('\r');
Gavin Howard01055ba2018-11-03 11:00:21 -06005778 break;
5779 }
5780
5781 case 'q':
5782 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005783 bb_putchar('"');
Gavin Howard01055ba2018-11-03 11:00:21 -06005784 break;
5785 }
5786
5787 case 't':
5788 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005789 bb_putchar('\t');
Gavin Howard01055ba2018-11-03 11:00:21 -06005790 break;
5791 }
5792
5793 default:
5794 {
5795 // Just print the backslash and following character.
Denys Vlasenko00d77792018-11-30 23:13:42 +01005796 bb_putchar('\\');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005797 ++G.prog.nchars;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005798 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005799 break;
5800 }
5801 }
5802 }
5803 }
5804}
5805
Denys Vlasenko29301232018-12-11 15:29:32 +01005806static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005807{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005808 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005809 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005810 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005811 bool pop = inst != BC_INST_PRINT;
5812
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005813 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005814 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005815
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005816 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005817 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005818 s = zbc_program_num(r, &num, false);
5819 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005820
5821 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005822 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005823 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005824 }
5825 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005826 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005827
5828 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005829 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005830
5831 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005832 for (;;) {
5833 char c = *str++;
5834 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005835 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005836 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005837 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005838 }
5839 }
5840 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005841 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005842 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005843 }
5844 }
5845
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005846 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005847
Denys Vlasenko29301232018-12-11 15:29:32 +01005848 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005849}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005850#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005851# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005852#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005853
Denys Vlasenko29301232018-12-11 15:29:32 +01005854static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005855{
5856 BcStatus s;
5857 BcResult res, *ptr;
5858 BcNum *num = NULL;
5859
Denys Vlasenko29301232018-12-11 15:29:32 +01005860 s = zbc_program_prep(&ptr, &num);
5861 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005862
5863 bc_num_init(&res.d.n, num->len);
5864 bc_num_copy(&res.d.n, num);
5865 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5866
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005867 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005868
Denys Vlasenko29301232018-12-11 15:29:32 +01005869 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005870}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005871#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005872# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005873#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005874
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005875static BcStatus bc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005876{
5877 BcStatus s;
5878 BcResult *opd1, *opd2, res;
5879 BcNum *n1, *n2;
5880 bool cond = 0;
5881 ssize_t cmp;
5882
Denys Vlasenko29301232018-12-11 15:29:32 +01005883 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005884 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005885 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005886
5887 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005888 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005889 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005890 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005891 else {
5892
5893 cmp = bc_num_cmp(n1, n2);
5894
5895 switch (inst) {
5896
5897 case BC_INST_REL_EQ:
5898 {
5899 cond = cmp == 0;
5900 break;
5901 }
5902
5903 case BC_INST_REL_LE:
5904 {
5905 cond = cmp <= 0;
5906 break;
5907 }
5908
5909 case BC_INST_REL_GE:
5910 {
5911 cond = cmp >= 0;
5912 break;
5913 }
5914
5915 case BC_INST_REL_NE:
5916 {
5917 cond = cmp != 0;
5918 break;
5919 }
5920
5921 case BC_INST_REL_LT:
5922 {
5923 cond = cmp < 0;
5924 break;
5925 }
5926
5927 case BC_INST_REL_GT:
5928 {
5929 cond = cmp > 0;
5930 break;
5931 }
5932 }
5933 }
5934
5935 (cond ? bc_num_one : bc_num_zero)(&res.d.n);
5936
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005937 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005938
5939 return s;
5940}
5941
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005942#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005943static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005944 bool push)
5945{
5946 BcNum n2;
5947 BcResult res;
5948
5949 memset(&n2, 0, sizeof(BcNum));
5950 n2.rdx = res.d.id.idx = r->d.id.idx;
5951 res.t = BC_RESULT_STR;
5952
5953 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005954 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005955 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005956 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005957 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005958 }
5959
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005960 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005961
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005962 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005963 bc_vec_push(v, &n2);
5964
Denys Vlasenko29301232018-12-11 15:29:32 +01005965 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005966}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005967#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005968# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005969#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005970#endif // ENABLE_DC
5971
Denys Vlasenko29301232018-12-11 15:29:32 +01005972static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005973{
5974 BcStatus s;
5975 BcResult *ptr, r;
5976 BcVec *v;
5977 BcNum *n;
5978
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005979 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005980 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005981
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005982 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005983 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005984 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005985 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005986
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005987#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005988 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005989 RETURN_STATUS(bc_error_variable_is_wrong_type());
5990 if (ptr->t == BC_RESULT_STR)
5991 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005992#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005993
Denys Vlasenko29301232018-12-11 15:29:32 +01005994 s = zbc_program_num(ptr, &n, false);
5995 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005996
5997 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005998 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005999
6000 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006001 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006002 bc_num_copy(&r.d.n, n);
6003 }
6004 else {
6005 bc_array_init(&r.d.v, true);
6006 bc_array_copy(&r.d.v, (BcVec *) n);
6007 }
6008
6009 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006010 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006011
Denys Vlasenko29301232018-12-11 15:29:32 +01006012 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006013}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006014#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006015# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006016#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006017
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006018static BcStatus bc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006019{
6020 BcStatus s;
6021 BcResult *left, *right, res;
6022 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006023 bool assign = inst == BC_INST_ASSIGN, ib, sc;
6024
Denys Vlasenko29301232018-12-11 15:29:32 +01006025 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06006026 if (s) return s;
6027
6028 ib = left->t == BC_RESULT_IBASE;
6029 sc = left->t == BC_RESULT_SCALE;
6030
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006031#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006032
6033 if (right->t == BC_RESULT_STR) {
6034
6035 BcVec *v;
6036
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006037 if (left->t != BC_RESULT_VAR)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006038 return bc_error_variable_is_wrong_type();
Denys Vlasenkodf515392018-12-02 19:27:48 +01006039 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006040
Denys Vlasenko29301232018-12-11 15:29:32 +01006041 return zbc_program_assignStr(right, v, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006042 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006043#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006044
6045 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006046 return bc_error("bad assignment:"
6047 " left side must be scale,"
6048 " ibase, obase, last, var,"
6049 " or array element"
6050 );
Gavin Howard01055ba2018-11-03 11:00:21 -06006051
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006052#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006053 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006054 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06006055
6056 if (assign)
6057 bc_num_copy(l, r);
6058 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006059 s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006060
6061 if (s) return s;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006062#else
Gavin Howard01055ba2018-11-03 11:00:21 -06006063 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006064#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006065
6066 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006067 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006068 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
6069 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
6070 NULL, //can't happen //BC_RESULT_LAST
6071 NULL, //can't happen //BC_RESULT_CONSTANT
6072 NULL, //can't happen //BC_RESULT_ONE
6073 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006074 };
Gavin Howard01055ba2018-11-03 11:00:21 -06006075 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01006076 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06006077
Denys Vlasenko29301232018-12-11 15:29:32 +01006078 s = zbc_num_ulong(l, &val);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006079 if (s)
6080 return s;
6081 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06006082 if (sc) {
6083 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006084 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006085 }
6086 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006087 if (val < BC_NUM_MIN_BASE)
6088 return bc_error(msg[s]);
Gavin Howard01055ba2018-11-03 11:00:21 -06006089 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006090 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006091 }
6092
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006093 if (val > max)
6094 return bc_error(msg[s]);
6095 if (!sc)
6096 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006097
6098 *ptr = (size_t) val;
6099 s = BC_STATUS_SUCCESS;
6100 }
6101
6102 bc_num_init(&res.d.n, l->len);
6103 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006104 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006105
6106 return s;
6107}
6108
Denys Vlasenko416ce762018-12-02 20:57:17 +01006109#if !ENABLE_DC
6110#define bc_program_pushVar(code, bgn, pop, copy) \
6111 bc_program_pushVar(code, bgn)
6112// for bc, 'pop' and 'copy' are always false
6113#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006114static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006115 bool pop, bool copy)
6116{
Gavin Howard01055ba2018-11-03 11:00:21 -06006117 BcResult r;
6118 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006119
6120 r.t = BC_RESULT_VAR;
6121 r.d.id.name = name;
6122
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006123#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006124 {
6125 BcVec *v = bc_program_search(name, true);
6126 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006127
Denys Vlasenko416ce762018-12-02 20:57:17 +01006128 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006129
Denys Vlasenko416ce762018-12-02 20:57:17 +01006130 if (!BC_PROG_STACK(v, 2 - copy)) {
6131 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006132 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006133 }
6134
Gavin Howard01055ba2018-11-03 11:00:21 -06006135 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006136 name = NULL;
6137
6138 if (!BC_PROG_STR(num)) {
6139
6140 r.t = BC_RESULT_TEMP;
6141
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006142 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006143 bc_num_copy(&r.d.n, num);
6144 }
6145 else {
6146 r.t = BC_RESULT_STR;
6147 r.d.id.idx = num->rdx;
6148 }
6149
6150 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006151 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006152 }
6153#endif // ENABLE_DC
6154
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006155 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006156
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006157 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006158}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006159#if ERRORS_ARE_FATAL
6160# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6161#else
6162# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6163#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006164
Denys Vlasenko29301232018-12-11 15:29:32 +01006165static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006166 char inst)
6167{
6168 BcStatus s = BC_STATUS_SUCCESS;
6169 BcResult r;
6170 BcNum *num;
6171
6172 r.d.id.name = bc_program_name(code, bgn);
6173
6174 if (inst == BC_INST_ARRAY) {
6175 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006176 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006177 }
6178 else {
6179
6180 BcResult *operand;
6181 unsigned long temp;
6182
Denys Vlasenko29301232018-12-11 15:29:32 +01006183 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006184 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006185 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006186 if (s) goto err;
6187
6188 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006189 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006190 goto err;
6191 }
6192
6193 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006194 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006195 }
6196
6197err:
6198 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006199 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006200}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006201#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006202# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006203#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006204
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006205#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006206static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006207{
6208 BcStatus s;
6209 BcResult *ptr, res, copy;
6210 BcNum *num = NULL;
6211 char inst2 = inst;
6212
Denys Vlasenko29301232018-12-11 15:29:32 +01006213 s = zbc_program_prep(&ptr, &num);
6214 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006215
6216 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6217 copy.t = BC_RESULT_TEMP;
6218 bc_num_init(&copy.d.n, num->len);
6219 bc_num_copy(&copy.d.n, num);
6220 }
6221
6222 res.t = BC_RESULT_ONE;
6223 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6224 BC_INST_ASSIGN_PLUS :
6225 BC_INST_ASSIGN_MINUS;
6226
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006227 bc_vec_push(&G.prog.results, &res);
6228 bc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006229
6230 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006231 bc_vec_pop(&G.prog.results);
6232 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006233 }
6234
Denys Vlasenko29301232018-12-11 15:29:32 +01006235 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006236}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006237#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006238# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006239#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006240
Denys Vlasenko29301232018-12-11 15:29:32 +01006241static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006242{
Gavin Howard01055ba2018-11-03 11:00:21 -06006243 BcInstPtr ip;
6244 size_t i, nparams = bc_program_index(code, idx);
6245 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006246 BcId *a;
6247 BcResultData param;
6248 BcResult *arg;
6249
6250 ip.idx = 0;
6251 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006252 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006253
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006254 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006255 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006256 }
6257 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006258 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006259 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006260 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006261
6262 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006263 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006264
6265 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006266 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006267
6268 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006269 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006270
Denys Vlasenko29301232018-12-11 15:29:32 +01006271 s = zbc_program_copyToVar(a->name, a->idx);
6272 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006273 }
6274
6275 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006276 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006277
6278 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006279 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006280
6281 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006282 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006283 bc_vec_push(v, &param.n);
6284 }
6285 else {
6286 bc_array_init(&param.v, true);
6287 bc_vec_push(v, &param.v);
6288 }
6289 }
6290
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006291 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006292
Denys Vlasenko29301232018-12-11 15:29:32 +01006293 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006294}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006295#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006296# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006297#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006298
Denys Vlasenko29301232018-12-11 15:29:32 +01006299static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006300{
Gavin Howard01055ba2018-11-03 11:00:21 -06006301 BcResult res;
6302 BcFunc *f;
6303 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006304 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006305
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006306 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006307 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006308
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006309 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006310 res.t = BC_RESULT_TEMP;
6311
6312 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006313 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006314 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006315 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006316
Denys Vlasenko29301232018-12-11 15:29:32 +01006317 s = zbc_program_num(operand, &num, false);
6318 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006319 bc_num_init(&res.d.n, num->len);
6320 bc_num_copy(&res.d.n, num);
6321 }
6322 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006323 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006324 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006325 }
6326
6327 // We need to pop arguments as well, so this takes that into account.
6328 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006329 BcVec *v;
6330 BcId *a = bc_vec_item(&f->autos, i);
6331
Denys Vlasenkodf515392018-12-02 19:27:48 +01006332 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006333 bc_vec_pop(v);
6334 }
6335
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006336 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6337 bc_vec_push(&G.prog.results, &res);
6338 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006339
Denys Vlasenko29301232018-12-11 15:29:32 +01006340 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006341}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006342#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006343# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006344#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006345#endif // ENABLE_BC
6346
6347static unsigned long bc_program_scale(BcNum *n)
6348{
6349 return (unsigned long) n->rdx;
6350}
6351
6352static unsigned long bc_program_len(BcNum *n)
6353{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006354 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006355
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006356 if (n->rdx != len) return len;
6357 for (;;) {
6358 if (len == 0) break;
6359 len--;
6360 if (n->num[len] != 0) break;
6361 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006362 return len;
6363}
6364
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006365static BcStatus bc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006366{
6367 BcStatus s;
6368 BcResult *opnd;
6369 BcNum *num = NULL;
6370 BcResult res;
6371 bool len = inst == BC_INST_LENGTH;
6372
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006373 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006374 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006375 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006376
Denys Vlasenko29301232018-12-11 15:29:32 +01006377 s = zbc_program_num(opnd, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006378 if (s) return s;
6379
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006380#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006381 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006382 return bc_error_variable_is_wrong_type();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006383#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006384
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006385 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006386
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006387 if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006388#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006389 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006390 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006391 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006392#endif
6393#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006394 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
6395
6396 char **str;
6397 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6398
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006399 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006400 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006401 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006402#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006404 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006405 }
6406
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006407 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006408
6409 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006410}
6411
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006412#if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006413static BcStatus bc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006414{
6415 BcStatus s;
6416 BcResult *opd1, *opd2, res, res2;
6417 BcNum *n1, *n2 = NULL;
6418
Denys Vlasenko29301232018-12-11 15:29:32 +01006419 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006420 if (s) return s;
6421
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006422 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006423 bc_num_init(&res2.d.n, n2->len);
6424
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006425 s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006426 if (s) goto err;
6427
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006428 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006429 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006430 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006431
6432 return s;
6433
6434err:
6435 bc_num_free(&res2.d.n);
6436 bc_num_free(&res.d.n);
6437 return s;
6438}
6439
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006440static BcStatus bc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006441{
6442 BcStatus s;
6443 BcResult *r1, *r2, *r3, res;
6444 BcNum *n1, *n2, *n3;
6445
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006446 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006447 return bc_error_stack_has_too_few_elements();
Denys Vlasenko29301232018-12-11 15:29:32 +01006448 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006449 if (s) return s;
6450
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006451 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006452 s = zbc_program_num(r1, &n1, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006453 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006454 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006455 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006456
6457 // Make sure that the values have their pointers updated, if necessary.
6458 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6459
6460 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006461 s = zbc_program_num(r2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006462 if (s) return s;
6463 }
6464
6465 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006466 s = zbc_program_num(r3, &n3, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006467 if (s) return s;
6468 }
6469 }
6470
6471 bc_num_init(&res.d.n, n3->len);
6472 s = bc_num_modexp(n1, n2, n3, &res.d.n);
6473 if (s) goto err;
6474
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006475 bc_vec_pop(&G.prog.results);
6476 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006477
6478 return s;
6479
6480err:
6481 bc_num_free(&res.d.n);
6482 return s;
6483}
6484
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006485static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006486{
Gavin Howard01055ba2018-11-03 11:00:21 -06006487 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006488 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006489
6490 res.t = BC_RESULT_TEMP;
6491
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006492 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006493 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006494 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006495}
6496
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006497static BcStatus bc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006498{
6499 BcStatus s;
6500 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006501 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006502 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006503 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006504 unsigned long val;
6505
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006506 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006507 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006508 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006509
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006510 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006511 s = zbc_program_num(r, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006512 if (s) return s;
6513
6514 if (BC_PROG_NUM(r, num)) {
6515
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006516 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006517 bc_num_copy(&n, num);
6518 bc_num_truncate(&n, n.rdx);
6519
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006520 s = bc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006521 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006522 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006523 if (s) goto num_err;
6524
6525 c = (char) val;
6526
6527 bc_num_free(&n);
6528 }
6529 else {
6530 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006531 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006532 c = str2[0];
6533 }
6534
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006535 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006536 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006537 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006538
6539 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006540 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006541
6542 if (idx != len + BC_PROG_REQ_FUNCS) {
6543
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006544 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006545 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006546 len = idx;
6547 break;
6548 }
6549 }
6550
6551 free(str);
6552 }
6553 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006554 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006555
6556 res.t = BC_RESULT_STR;
6557 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006558 bc_vec_pop(&G.prog.results);
6559 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006560
6561 return BC_STATUS_SUCCESS;
6562
6563num_err:
6564 bc_num_free(&n);
6565 return s;
6566}
6567
Denys Vlasenko29301232018-12-11 15:29:32 +01006568static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006569{
6570 BcStatus s;
6571 BcResult *r;
6572 BcNum *n = NULL;
6573 size_t idx;
6574 char *str;
6575
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006576 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006577 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006578 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006579
Denys Vlasenko29301232018-12-11 15:29:32 +01006580 s = zbc_program_num(r, &n, false);
6581 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006582
6583 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006584 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006585 else {
6586 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006587 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006588 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006589 }
6590
Denys Vlasenko29301232018-12-11 15:29:32 +01006591 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006592}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006593#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006594# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006595#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006596
Denys Vlasenko29301232018-12-11 15:29:32 +01006597static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006598{
6599 BcStatus s;
6600 BcResult *opnd;
6601 BcNum *num = NULL;
6602 unsigned long val;
6603
Denys Vlasenko29301232018-12-11 15:29:32 +01006604 s = zbc_program_prep(&opnd, &num);
6605 if (s) RETURN_STATUS(s);
6606 s = zbc_num_ulong(num, &val);
6607 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006608
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006609 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006610
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006611 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006612 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006613 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006614 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006615 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006616
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006617 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006618
Denys Vlasenko29301232018-12-11 15:29:32 +01006619 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006620}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006621#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006622# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006623#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006624
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006625static BcStatus bc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006626 bool cond)
6627{
6628 BcStatus s = BC_STATUS_SUCCESS;
6629 BcResult *r;
6630 char **str;
6631 BcFunc *f;
6632 BcParse prs;
6633 BcInstPtr ip;
6634 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006635
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006636 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006637 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006638
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006639 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006640
6641 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006642 BcNum *n = n; // for compiler
6643 bool exec;
6644 char *name;
6645 char *then_name = bc_program_name(code, bgn);
6646 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006647
6648 if (code[*bgn] == BC_PARSE_STREND)
6649 (*bgn) += 1;
6650 else
6651 else_name = bc_program_name(code, bgn);
6652
6653 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006654 name = then_name;
6655 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006656 exec = true;
6657 name = else_name;
6658 }
6659
6660 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006661 BcVec *v;
6662 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006663 n = bc_vec_top(v);
6664 }
6665
6666 free(then_name);
6667 free(else_name);
6668
6669 if (!exec) goto exit;
6670 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006671 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006672 goto exit;
6673 }
6674
6675 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006676 } else {
6677 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006678 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006679 } else if (r->t == BC_RESULT_VAR) {
6680 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006681 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006682 if (s || !BC_PROG_STR(n)) goto exit;
6683 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006684 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006685 goto exit;
6686 }
6687
6688 fidx = sidx + BC_PROG_REQ_FUNCS;
6689
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006690 str = bc_program_str(sidx);
6691 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006692
6693 if (f->code.len == 0) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006694 common_parse_init(&prs, fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006695 s = bc_parse_text(&prs, *str);
6696 if (s) goto err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01006697 s = common_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006698 if (s) goto err;
6699
6700 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006701 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006702 goto err;
6703 }
6704
6705 bc_parse_free(&prs);
6706 }
6707
6708 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006709 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006710 ip.func = fidx;
6711
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006712 bc_vec_pop(&G.prog.results);
6713 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006714
6715 return BC_STATUS_SUCCESS;
6716
6717err:
6718 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006719 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006720 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006721exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006722 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006723 return s;
6724}
6725#endif // ENABLE_DC
6726
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006727static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006728{
Gavin Howard01055ba2018-11-03 11:00:21 -06006729 BcResult res;
6730 unsigned long val;
6731
6732 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6733 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006734 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006735 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006736 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006737 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006738 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006739
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006740 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006741 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006742 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006743}
6744
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006745static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006746{
Gavin Howard01055ba2018-11-03 11:00:21 -06006747 BcId entry, *entry_ptr;
6748 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006749 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006750
6751 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006752 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006753
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006754 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6755 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006756
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006757 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006758 *idx = entry_ptr->idx;
6759
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006760 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006761
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006762 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006763
6764 // We need to reset these, so the function can be repopulated.
6765 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006766 bc_vec_pop_all(&func->autos);
6767 bc_vec_pop_all(&func->code);
6768 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006769 }
6770 else {
6771 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006772 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006773 }
6774}
6775
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006776static BcStatus bc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006777{
Gavin Howard01055ba2018-11-03 11:00:21 -06006778 BcResult r, *ptr;
6779 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006780 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006781 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006782 char *code = func->code.v;
6783 bool cond = false;
6784
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006785 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006786 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006787 char inst = code[(ip->idx)++];
6788
6789 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006790#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006791 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006792 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006793 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006794 cond = !bc_num_cmp(num, &G.prog.zero);
6795 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006796 // Fallthrough.
6797 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006798 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006799 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006800 addr = bc_vec_item(&func->labels, idx);
6801 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6802 break;
6803 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006804 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006805 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006806 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006807 case BC_INST_INC_PRE:
6808 case BC_INST_DEC_PRE:
6809 case BC_INST_INC_POST:
6810 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006811 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006812 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006813 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006814 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006815 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006816 case BC_INST_RET:
6817 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006818 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006819 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006820 case BC_INST_BOOL_OR:
6821 case BC_INST_BOOL_AND:
6822#endif // ENABLE_BC
6823 case BC_INST_REL_EQ:
6824 case BC_INST_REL_LE:
6825 case BC_INST_REL_GE:
6826 case BC_INST_REL_NE:
6827 case BC_INST_REL_LT:
6828 case BC_INST_REL_GT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006829 s = bc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006830 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006831 case BC_INST_READ:
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006832 s = bc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006833 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006834 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006835 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006836 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006837 case BC_INST_ARRAY_ELEM:
6838 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006839 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006840 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006841 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006842 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006843 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006844 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006845 case BC_INST_IBASE:
6846 case BC_INST_SCALE:
6847 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006848 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006849 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006850 case BC_INST_SCALE_FUNC:
6851 case BC_INST_LENGTH:
6852 case BC_INST_SQRT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006853 s = bc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006854 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006855 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006856 r.t = BC_RESULT_CONSTANT;
6857 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006858 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006859 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006860 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006861 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006862 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006863 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006864 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006865 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006866 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006867 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006868 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006869 case BC_INST_PRINT:
6870 case BC_INST_PRINT_POP:
6871 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006872 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006873 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006874 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006875 r.t = BC_RESULT_STR;
6876 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006877 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006878 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006879 case BC_INST_POWER:
6880 case BC_INST_MULTIPLY:
6881 case BC_INST_DIVIDE:
6882 case BC_INST_MODULUS:
6883 case BC_INST_PLUS:
6884 case BC_INST_MINUS:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006885 s = bc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006886 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006887 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006888 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006889 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006890 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006891 if (!bc_num_cmp(num, &G.prog.zero))
6892 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006893 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006894 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006895 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006896 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006897 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006898 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006899#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006900 case BC_INST_ASSIGN_POWER:
6901 case BC_INST_ASSIGN_MULTIPLY:
6902 case BC_INST_ASSIGN_DIVIDE:
6903 case BC_INST_ASSIGN_MODULUS:
6904 case BC_INST_ASSIGN_PLUS:
6905 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006906#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006907 case BC_INST_ASSIGN:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006908 s = bc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006909 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006910#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006911 case BC_INST_MODEXP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006912 s = bc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006913 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006914 case BC_INST_DIVMOD:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006915 s = bc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006916 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006917 case BC_INST_EXECUTE:
6918 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006919 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006920 s = bc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006921 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006922 case BC_INST_PRINT_STACK: {
6923 size_t idx;
6924 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006925 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006926 if (s) break;
6927 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006928 break;
6929 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006930 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006931 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006932 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006933 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006934 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006935 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006936 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006937 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006938 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006939 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006940 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006941 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006942 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006943 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006944 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006945 if (!BC_PROG_STACK(&G.prog.results, 2))
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_item_rev(&G.prog.results, 0);
6948 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006949 memcpy(&r, ptr, sizeof(BcResult));
6950 memcpy(ptr, ptr2, sizeof(BcResult));
6951 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006952 break;
6953 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006954 case BC_INST_ASCIIFY:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006955 s = bc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006956 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006957 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006958 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006959 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006960 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006961 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006962 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006963 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006964 break;
6965 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006966 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006967 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006968 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006969 free(name);
6970 break;
6971 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006972 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006973 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006974 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006975 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006976 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006977 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006978 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006979 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006980#endif // ENABLE_DC
6981 }
6982
Denys Vlasenkod38af482018-12-04 19:11:02 +01006983 if (s || G_interrupt) {
6984 bc_program_reset();
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006985 return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01006986 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006987
6988 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006989 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006990 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006991 code = func->code.v;
6992 }
6993
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006994 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006995}
6996
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006997#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01006998static void bc_vm_info(void)
6999{
7000 printf("%s "BB_VER"\n"
7001 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01007002 , applet_name);
7003}
7004
7005static void bc_args(char **argv)
7006{
7007 unsigned opts;
7008 int i;
7009
7010 GETOPT_RESET();
7011#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007012 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01007013 "warn\0" No_argument "w"
7014 "version\0" No_argument "v"
7015 "standard\0" No_argument "s"
7016 "quiet\0" No_argument "q"
7017 "mathlib\0" No_argument "l"
7018 "interactive\0" No_argument "i"
7019 );
7020#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007021 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01007022#endif
7023 if (getenv("POSIXLY_CORRECT"))
7024 option_mask32 |= BC_FLAG_S;
7025
Denys Vlasenko54214c32018-12-06 09:07:06 +01007026 if (opts & BC_FLAG_V) {
7027 bc_vm_info();
7028 exit(0);
7029 }
7030
7031 for (i = optind; argv[i]; ++i)
7032 bc_vec_push(&G.files, argv + i);
7033}
7034
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007035static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007036{
Gavin Howard01055ba2018-11-03 11:00:21 -06007037 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007038 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01007039 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06007040
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007041 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06007042
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007043 G.env_args = xstrdup(env_args);
7044 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06007045
7046 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06007047
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007048 while (*(buf = skip_whitespace(buf)) != '\0') {
7049 bc_vec_push(&v, &buf);
7050 buf = skip_non_whitespace(buf);
7051 if (!*buf)
7052 break;
7053 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06007054 }
7055
Denys Vlasenko54214c32018-12-06 09:07:06 +01007056 // NULL terminate, and pass argv[] so that first arg is argv[1]
7057 if (sizeof(int) == sizeof(char*)) {
7058 bc_vec_push(&v, &const_int_0);
7059 } else {
7060 static char *const nullptr = NULL;
7061 bc_vec_push(&v, &nullptr);
7062 }
7063 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06007064
7065 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06007066}
7067#endif // ENABLE_BC
7068
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007069static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06007070{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007071 char *lenv;
7072 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06007073
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007074 lenv = getenv(var);
7075 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06007076 if (!lenv) return len;
7077
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007078 len = bb_strtou(lenv, NULL, 10) - 1;
7079 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06007080 len = BC_NUM_PRINT_WIDTH;
7081
7082 return len;
7083}
7084
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007085static BcStatus bc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06007086{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007087 BcStatus s = bc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06007088
Gavin Howard01055ba2018-11-03 11:00:21 -06007089 if (s) return s;
7090
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007091 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007092 s = G.prs.parse(&G.prs);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01007093 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007094 }
7095
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007096 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007097 s = bc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01007098 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007099 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01007100 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06007101 }
7102
7103 return s;
7104}
7105
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007106static BcStatus bc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007107{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007108 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007109 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007110 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007111 BcFunc *main_func;
7112 BcInstPtr *ip;
7113
Denys Vlasenkodf515392018-12-02 19:27:48 +01007114 data = bc_read_file(file);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007115 if (!data) return bc_error_fmt("file '%s' is not text", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007116
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007117 sv_file = G.prog.file;
7118 G.prog.file = file;
7119 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007120 s = bc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007121 if (s) goto err;
7122
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007123 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007124 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007125
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007126 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007127 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007128
7129err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007130 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007131 free(data);
7132 return s;
7133}
7134
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007135static BcStatus bc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007136{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007137 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007138 BcVec buf, buffer;
Gavin Howard01055ba2018-11-03 11:00:21 -06007139 size_t len, i, str = 0;
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007140 bool comment = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06007141
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007142 G.prog.file = NULL;
7143 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007144
Denys Vlasenko7d628012018-12-04 21:46:47 +01007145 bc_char_vec_init(&buffer);
7146 bc_char_vec_init(&buf);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01007147 bc_vec_pushZeroByte(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007148
7149 // This loop is complex because the vm tries not to send any lines that end
7150 // with a backslash to the parser. The reason for that is because the parser
7151 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7152 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007153 while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007154
7155 char *string = buf.v;
7156
7157 len = buf.len - 1;
7158
7159 if (len == 1) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007160 if (str && buf.v[0] == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007161 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007162 else if (buf.v[0] == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007163 str += 1;
7164 }
7165 else if (len > 1 || comment) {
7166
7167 for (i = 0; i < len; ++i) {
7168
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007169 bool notend = len > i + 1;
7170 char c = string[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06007171
7172 if (i - 1 > len || string[i - 1] != '\\') {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007173 if (G.sbgn == G.send)
7174 str ^= c == G.sbgn;
7175 else if (c == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007176 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007177 else if (c == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007178 str += 1;
7179 }
7180
7181 if (c == '/' && notend && !comment && string[i + 1] == '*') {
7182 comment = true;
7183 break;
7184 }
7185 else if (c == '*' && notend && comment && string[i + 1] == '/')
7186 comment = false;
7187 }
7188
7189 if (str || comment || string[len - 2] == '\\') {
7190 bc_vec_concat(&buffer, buf.v);
7191 continue;
7192 }
7193 }
7194
7195 bc_vec_concat(&buffer, buf.v);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007196 s = bc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007197 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007198 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007199 // Debug config, non-interactive mode:
7200 // return all the way back to main.
7201 // Non-debug builds do not come here, they exit.
7202 break;
7203 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007204 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007205
Denys Vlasenko7d628012018-12-04 21:46:47 +01007206 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007207 }
Denys Vlasenkof522dd92018-12-07 16:35:43 +01007208 if (s == BC_STATUS_EOF) // input EOF (^D) is not an error
7209 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007210
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007211 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007212 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007213 }
7214 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007215 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007216 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007217
Gavin Howard01055ba2018-11-03 11:00:21 -06007218 bc_vec_free(&buf);
7219 bc_vec_free(&buffer);
7220 return s;
7221}
7222
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007223#if ENABLE_BC
7224static const char bc_lib[] = {
7225 "scale=20"
7226"\n" "define e(x){"
7227"\n" "auto b,s,n,r,d,i,p,f,v"
7228"\n" "b=ibase"
7229"\n" "ibase=A"
7230"\n" "if(x<0){"
7231"\n" "n=1"
7232"\n" "x=-x"
7233"\n" "}"
7234"\n" "s=scale"
7235"\n" "r=6+s+0.44*x"
7236"\n" "scale=scale(x)+1"
7237"\n" "while(x>1){"
7238"\n" "d+=1"
7239"\n" "x/=2"
7240"\n" "scale+=1"
7241"\n" "}"
7242"\n" "scale=r"
7243"\n" "r=x+1"
7244"\n" "p=x"
7245"\n" "f=v=1"
7246"\n" "for(i=2;v!=0;++i){"
7247"\n" "p*=x"
7248"\n" "f*=i"
7249"\n" "v=p/f"
7250"\n" "r+=v"
7251"\n" "}"
7252"\n" "while((d--)!=0)r*=r"
7253"\n" "scale=s"
7254"\n" "ibase=b"
7255"\n" "if(n!=0)return(1/r)"
7256"\n" "return(r/1)"
7257"\n" "}"
7258"\n" "define l(x){"
7259"\n" "auto b,s,r,p,a,q,i,v"
7260"\n" "b=ibase"
7261"\n" "ibase=A"
7262"\n" "if(x<=0){"
7263"\n" "r=(1-10^scale)/1"
7264"\n" "ibase=b"
7265"\n" "return(r)"
7266"\n" "}"
7267"\n" "s=scale"
7268"\n" "scale+=6"
7269"\n" "p=2"
7270"\n" "while(x>=2){"
7271"\n" "p*=2"
7272"\n" "x=sqrt(x)"
7273"\n" "}"
7274"\n" "while(x<=0.5){"
7275"\n" "p*=2"
7276"\n" "x=sqrt(x)"
7277"\n" "}"
7278"\n" "r=a=(x-1)/(x+1)"
7279"\n" "q=a*a"
7280"\n" "v=1"
7281"\n" "for(i=3;v!=0;i+=2){"
7282"\n" "a*=q"
7283"\n" "v=a/i"
7284"\n" "r+=v"
7285"\n" "}"
7286"\n" "r*=p"
7287"\n" "scale=s"
7288"\n" "ibase=b"
7289"\n" "return(r/1)"
7290"\n" "}"
7291"\n" "define s(x){"
7292"\n" "auto b,s,r,n,a,q,i"
7293"\n" "b=ibase"
7294"\n" "ibase=A"
7295"\n" "s=scale"
7296"\n" "scale=1.1*s+2"
7297"\n" "a=a(1)"
7298"\n" "if(x<0){"
7299"\n" "n=1"
7300"\n" "x=-x"
7301"\n" "}"
7302"\n" "scale=0"
7303"\n" "q=(x/a+2)/4"
7304"\n" "x=x-4*q*a"
7305"\n" "if(q%2!=0)x=-x"
7306"\n" "scale=s+2"
7307"\n" "r=a=x"
7308"\n" "q=-x*x"
7309"\n" "for(i=3;a!=0;i+=2){"
7310"\n" "a*=q/(i*(i-1))"
7311"\n" "r+=a"
7312"\n" "}"
7313"\n" "scale=s"
7314"\n" "ibase=b"
7315"\n" "if(n!=0)return(-r/1)"
7316"\n" "return(r/1)"
7317"\n" "}"
7318"\n" "define c(x){"
7319"\n" "auto b,s"
7320"\n" "b=ibase"
7321"\n" "ibase=A"
7322"\n" "s=scale"
7323"\n" "scale*=1.2"
7324"\n" "x=s(2*a(1)+x)"
7325"\n" "scale=s"
7326"\n" "ibase=b"
7327"\n" "return(x/1)"
7328"\n" "}"
7329"\n" "define a(x){"
7330"\n" "auto b,s,r,n,a,m,t,f,i,u"
7331"\n" "b=ibase"
7332"\n" "ibase=A"
7333"\n" "n=1"
7334"\n" "if(x<0){"
7335"\n" "n=-1"
7336"\n" "x=-x"
7337"\n" "}"
7338"\n" "if(x==1){"
7339"\n" "if(scale<65){"
7340"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7341"\n" "}"
7342"\n" "}"
7343"\n" "if(x==.2){"
7344"\n" "if(scale<65){"
7345"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7346"\n" "}"
7347"\n" "}"
7348"\n" "s=scale"
7349"\n" "if(x>.2){"
7350"\n" "scale+=5"
7351"\n" "a=a(.2)"
7352"\n" "}"
7353"\n" "scale=s+3"
7354"\n" "while(x>.2){"
7355"\n" "m+=1"
7356"\n" "x=(x-.2)/(1+.2*x)"
7357"\n" "}"
7358"\n" "r=u=x"
7359"\n" "f=-x*x"
7360"\n" "t=1"
7361"\n" "for(i=3;t!=0;i+=2){"
7362"\n" "u*=f"
7363"\n" "t=u/i"
7364"\n" "r+=t"
7365"\n" "}"
7366"\n" "scale=s"
7367"\n" "ibase=b"
7368"\n" "return((m*a+r)/n)"
7369"\n" "}"
7370"\n" "define j(n,x){"
7371"\n" "auto b,s,o,a,i,v,f"
7372"\n" "b=ibase"
7373"\n" "ibase=A"
7374"\n" "s=scale"
7375"\n" "scale=0"
7376"\n" "n/=1"
7377"\n" "if(n<0){"
7378"\n" "n=-n"
7379"\n" "if(n%2==1)o=1"
7380"\n" "}"
7381"\n" "a=1"
7382"\n" "for(i=2;i<=n;++i)a*=i"
7383"\n" "scale=1.5*s"
7384"\n" "a=(x^n)/2^n/a"
7385"\n" "r=v=1"
7386"\n" "f=-x*x/4"
7387"\n" "scale=scale+length(a)-scale(a)"
7388"\n" "for(i=1;v!=0;++i){"
7389"\n" "v=v*f/i/(n+i)"
7390"\n" "r+=v"
7391"\n" "}"
7392"\n" "scale=s"
7393"\n" "ibase=b"
7394"\n" "if(o!=0)a=-a"
7395"\n" "return(a*r/1)"
7396"\n" "}"
7397};
7398#endif // ENABLE_BC
7399
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007400static BcStatus bc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007401{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007402 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007403 size_t i;
7404
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007405#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007406 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007407
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007408 // We know that internal library is not buggy,
7409 // thus error checking is normally disabled.
7410# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007411 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007412 s = bc_parse_text(&G.prs, bc_lib);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007413 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007414
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007415 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007416 s = G.prs.parse(&G.prs);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007417 if (DEBUG_LIB && s) return s;
7418 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007419 s = bc_program_exec();
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007420 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007421 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007422#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007423
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007424 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007425 for (i = 0; !s && i < G.files.len; ++i)
7426 s = bc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007427 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7428 // Debug config, non-interactive mode:
7429 // return all the way back to main.
7430 // Non-debug builds do not come here, they exit.
7431 return s;
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007432 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007433
Denys Vlasenko91cde952018-12-10 20:56:08 +01007434 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007435 s = bc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007436
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007437 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
7438 s = bc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007439
Denys Vlasenko00d77792018-11-30 23:13:42 +01007440 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007441}
7442
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007443#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007444static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007445{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007446 bc_num_free(&G.prog.ib);
7447 bc_num_free(&G.prog.ob);
7448 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007449# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007450 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007451# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007452 bc_vec_free(&G.prog.fns);
7453 bc_vec_free(&G.prog.fn_map);
7454 bc_vec_free(&G.prog.vars);
7455 bc_vec_free(&G.prog.var_map);
7456 bc_vec_free(&G.prog.arrs);
7457 bc_vec_free(&G.prog.arr_map);
7458 bc_vec_free(&G.prog.strs);
7459 bc_vec_free(&G.prog.consts);
7460 bc_vec_free(&G.prog.results);
7461 bc_vec_free(&G.prog.stack);
7462 bc_num_free(&G.prog.last);
7463 bc_num_free(&G.prog.zero);
7464 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007465}
7466
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007467static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007468{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007469 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007470 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007471 bc_parse_free(&G.prs);
7472 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007473}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007474#endif
7475
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007476static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007477{
7478 size_t idx;
7479 BcInstPtr ip;
7480
7481 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7482 memset(&ip, 0, sizeof(BcInstPtr));
7483
7484 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007485 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007486 bc_num_ten(&G.prog.ib);
7487 G.prog.ib_t = 10;
7488
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007489 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007490 bc_num_ten(&G.prog.ob);
7491 G.prog.ob_t = 10;
7492
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007493 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007494 bc_num_ten(&G.prog.hexb);
7495 G.prog.hexb.num[0] = 6;
7496
7497#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007498 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007499 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7500#endif
7501
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007502 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007503 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007504
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007505 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007506 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007507
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007508 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007509 bc_num_one(&G.prog.one);
7510
7511 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007512 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007513
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007514 bc_program_addFunc(xstrdup("(main)"), &idx);
7515 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007516
7517 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007518 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007519
7520 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007521 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007522
7523 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7524 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7525 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7526 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7527 bc_vec_push(&G.prog.stack, &ip);
7528}
Gavin Howard01055ba2018-11-03 11:00:21 -06007529
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007530static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007531{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007532#if ENABLE_FEATURE_EDITING
7533 G.line_input_state = new_line_input_t(DO_HISTORY);
7534#endif
7535 G.prog.len = bc_vm_envLen(env_len);
7536
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007537 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007538 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007539 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007540 bc_program_init();
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007541 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007542 IF_BC(bc_parse_init(&G.prs, BC_PROG_MAIN);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007543 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007544 IF_DC(dc_parse_init(&G.prs, BC_PROG_MAIN);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007545 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007546
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007547 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007548#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007549 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007550 // With SA_RESTART, most system calls will restart
7551 // (IOW: they won't fail with EINTR).
7552 // In particular, this means ^C won't cause
7553 // stdout to get into "error state" if SIGINT hits
7554 // within write() syscall.
7555 // The downside is that ^C while line input is taken
7556 // will only be handled after [Enter] since read()
7557 // from stdin is not interrupted by ^C either,
7558 // it restarts, thus fgetc() does not return on ^C.
7559 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7560
7561 // Without SA_RESTART, this exhibits a bug:
7562 // "while (1) print 1" and try ^C-ing it.
7563 // Intermittently, instead of returning to input line,
7564 // you'll get "output error: Interrupted system call"
7565 // and exit.
7566 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007567#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007568 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007569 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007570 return 0; // "not a tty"
7571}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007572
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007573static BcStatus bc_vm_run(void)
7574{
7575 BcStatus st = bc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007576#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007577 if (G_exiting) // it was actually "halt" or "quit"
7578 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007579 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007580# if ENABLE_FEATURE_EDITING
7581 free_line_input_t(G.line_input_state);
7582# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007583 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007584#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007585 return st;
7586}
7587
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007588#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007589int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007590int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007591{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007592 int is_tty;
7593
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007594 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007595 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007596
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007597 is_tty = bc_vm_init("BC_LINE_LENGTH");
7598
7599 bc_args(argv);
7600
7601 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7602 bc_vm_info();
7603
7604 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007605}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007606#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007607
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007608#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007609int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007610int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007611{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007612 int noscript;
7613
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007614 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007615 G.sbgn = '[';
7616 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007617 /*
7618 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7619 * 1 char wider than bc from the same package.
7620 * Both default width, and xC_LINE_LENGTH=N are wider:
7621 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7622 * 1234\
7623 * 56
7624 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7625 * 123\
7626 * 456
7627 * Do the same, or it's a bug?
7628 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007629 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007630
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007631 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7632 noscript = BC_FLAG_I;
7633 for (;;) {
7634 int n = getopt(argc, argv, "e:f:x");
7635 if (n <= 0)
7636 break;
7637 switch (n) {
7638 case 'e':
7639 noscript = 0;
7640 n = bc_vm_process(optarg);
7641 if (n) return n;
7642 break;
7643 case 'f':
7644 noscript = 0;
7645 bc_vm_file(optarg);
7646 break;
7647 case 'x':
7648 option_mask32 |= DC_FLAG_X;
7649 break;
7650 default:
7651 bb_show_usage();
7652 }
7653 }
7654 argv += optind;
7655
7656 while (*argv) {
7657 noscript = 0;
7658 bc_vec_push(&G.files, argv++);
7659 }
7660
7661 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7662
7663 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007664}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007665#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007666
7667#endif // not DC_SMALL