blob: 4e5ba804fda070c2e2ceae65b2aab36a2ead35b8 [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,
189 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr() 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 Vlasenko6e7c65f2018-12-08 19:34:35 +01001302static BcStatus bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001303{
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001304 BcStatus s;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001305 bool bad_chars;
Gavin Howard01055ba2018-11-03 11:00:21 -06001306
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001307 s = BC_STATUS_SUCCESS;
Denys Vlasenko00d77792018-11-30 23:13:42 +01001308 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001309 int c;
Gavin Howard01055ba2018-11-03 11:00:21 -06001310
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001311 bad_chars = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001312 bc_vec_pop_all(vec);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001313
1314 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001315
Gavin Howard01055ba2018-11-03 11:00:21 -06001316#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001317 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001318 intr:
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001319 G_interrupt = 0;
Denys Vlasenkoac6ed112018-12-08 21:39:10 +01001320 // GNU bc says "interrupted execution."
1321 // GNU dc says "Interrupt!"
1322 fputs("\ninterrupted execution\n", stderr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001323 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001324# if ENABLE_FEATURE_EDITING
1325 if (G_ttyin) {
1326 int n, i;
1327# define line_buf bb_common_bufsiz1
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001328 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001329 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1330 if (n == 0) // ^C
1331 goto intr;
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001332 s = BC_STATUS_EOF;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001333 break;
1334 }
1335 i = 0;
1336 for (;;) {
1337 c = line_buf[i++];
1338 if (!c) break;
1339 bad_chars |= push_input_byte(vec, c);
1340 }
1341# undef line_buf
1342 } else
1343# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001344#endif
Denys Vlasenkoed849352018-12-06 10:26:13 +01001345 {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001346 IF_FEATURE_BC_SIGNALS(errno = 0;)
1347 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001348 c = fgetc(stdin);
1349#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
1350 // Both conditions appear simultaneously, check both just in case
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001351 if (errno == EINTR || G_interrupt) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001352 // ^C was pressed
1353 clearerr(stdin);
1354 goto intr;
1355 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001356#endif
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001357 if (c == EOF) {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001358 if (ferror(stdin))
1359 quit(); // this emits error message
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001360 s = BC_STATUS_EOF;
Denys Vlasenkoed849352018-12-06 10:26:13 +01001361 // Note: EOF does not append '\n', therefore:
1362 // printf 'print 123\n' | bc - works
1363 // printf 'print 123' | bc - fails (syntax error)
1364 break;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001365 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001366 bad_chars |= push_input_byte(vec, c);
1367 } while (c != '\n');
Denys Vlasenkoed849352018-12-06 10:26:13 +01001368 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001369 } while (bad_chars);
Gavin Howard01055ba2018-11-03 11:00:21 -06001370
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001371 bc_vec_pushZeroByte(vec);
Gavin Howard01055ba2018-11-03 11:00:21 -06001372
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001373 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001374}
1375
Denys Vlasenkodf515392018-12-02 19:27:48 +01001376static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001377{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001378 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001379 size_t size = ((size_t) -1);
1380 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001381
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001382 // Never returns NULL (dies on errors)
1383 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001384
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001385 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001386 char c = buf[i];
1387 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1388 || c > 0x7e
1389 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001390 free(buf);
1391 buf = NULL;
1392 break;
1393 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001394 }
1395
Denys Vlasenkodf515392018-12-02 19:27:48 +01001396 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001397}
1398
Gavin Howard01055ba2018-11-03 11:00:21 -06001399static void bc_num_setToZero(BcNum *n, size_t scale)
1400{
1401 n->len = 0;
1402 n->neg = false;
1403 n->rdx = scale;
1404}
1405
1406static void bc_num_zero(BcNum *n)
1407{
1408 bc_num_setToZero(n, 0);
1409}
1410
1411static void bc_num_one(BcNum *n)
1412{
1413 bc_num_setToZero(n, 0);
1414 n->len = 1;
1415 n->num[0] = 1;
1416}
1417
1418static void bc_num_ten(BcNum *n)
1419{
1420 bc_num_setToZero(n, 0);
1421 n->len = 2;
1422 n->num[0] = 0;
1423 n->num[1] = 1;
1424}
1425
Denys Vlasenko3129f702018-12-09 12:04:44 +01001426// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001427static void bc_num_init(BcNum *n, size_t req)
1428{
1429 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001430 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001431 n->num = xmalloc(req);
1432 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001433 n->rdx = 0;
1434 n->len = 0;
1435 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001436}
1437
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001438static void bc_num_init_DEF_SIZE(BcNum *n)
1439{
1440 bc_num_init(n, BC_NUM_DEF_SIZE);
1441}
1442
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001443static void bc_num_expand(BcNum *n, size_t req)
1444{
1445 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1446 if (req > n->cap) {
1447 n->num = xrealloc(n->num, req);
1448 n->cap = req;
1449 }
1450}
1451
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001452static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001453{
1454 free(((BcNum *) num)->num);
1455}
1456
1457static void bc_num_copy(BcNum *d, BcNum *s)
1458{
1459 if (d != s) {
1460 bc_num_expand(d, s->cap);
1461 d->len = s->len;
1462 d->neg = s->neg;
1463 d->rdx = s->rdx;
1464 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1465 }
1466}
1467
Denys Vlasenko29301232018-12-11 15:29:32 +01001468static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001469{
1470 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001471 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001472
Denys Vlasenko29301232018-12-11 15:29:32 +01001473 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001474
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001475 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001476
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001477 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001478
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001479 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001480 pow *= 10;
1481
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001482 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001483 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001484 prev = result;
1485 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001486 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001487 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001488
Denys Vlasenko29301232018-12-11 15:29:32 +01001489 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001490}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001491#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001492# define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001493#endif
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001494
1495static void bc_num_ulong2num(BcNum *n, unsigned long val)
1496{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001497 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001498
1499 bc_num_zero(n);
1500
1501 if (val == 0) return;
1502
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001503 if (ULONG_MAX == 0xffffffffUL)
1504 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001505 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001506 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001507 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001508
1509 ptr = n->num;
1510 for (;;) {
1511 n->len++;
1512 *ptr++ = val % 10;
1513 val /= 10;
1514 if (val == 0) break;
1515 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001516}
1517
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001518static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001519 size_t len)
1520{
1521 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001522 for (i = 0; i < len; ++i) {
1523 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001524 a[i + j++] += 10;
1525 a[i + j] -= 1;
1526 }
1527 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001528}
1529
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01001530#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1531#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1532#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1533//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1534static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1535{
1536 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1537}
1538//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1539static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1540{
1541 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1542}
1543
Gavin Howard01055ba2018-11-03 11:00:21 -06001544static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1545{
1546 size_t i;
1547 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001548 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001549 return BC_NUM_NEG(i + 1, c < 0);
1550}
1551
1552static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1553{
1554 size_t i, min, a_int, b_int, diff;
1555 BcDig *max_num, *min_num;
1556 bool a_max, neg = false;
1557 ssize_t cmp;
1558
1559 if (a == b) return 0;
1560 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1561 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
1562 if (a->neg) {
1563 if (b->neg)
1564 neg = true;
1565 else
1566 return -1;
1567 }
1568 else if (b->neg)
1569 return 1;
1570
1571 a_int = BC_NUM_INT(a);
1572 b_int = BC_NUM_INT(b);
1573 a_int -= b_int;
1574 a_max = (a->rdx > b->rdx);
1575
1576 if (a_int != 0) return (ssize_t) a_int;
1577
1578 if (a_max) {
1579 min = b->rdx;
1580 diff = a->rdx - b->rdx;
1581 max_num = a->num + diff;
1582 min_num = b->num;
1583 }
1584 else {
1585 min = a->rdx;
1586 diff = b->rdx - a->rdx;
1587 max_num = b->num + diff;
1588 min_num = a->num;
1589 }
1590
1591 cmp = bc_num_compare(max_num, min_num, b_int + min);
1592 if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg);
1593
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001594 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001595 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
1596 }
1597
1598 return 0;
1599}
1600
1601static void bc_num_truncate(BcNum *n, size_t places)
1602{
1603 if (places == 0) return;
1604
1605 n->rdx -= places;
1606
1607 if (n->len != 0) {
1608 n->len -= places;
1609 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1610 }
1611}
1612
1613static void bc_num_extend(BcNum *n, size_t places)
1614{
1615 size_t len = n->len + places;
1616
1617 if (places != 0) {
1618
1619 if (n->cap < len) bc_num_expand(n, len);
1620
1621 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1622 memset(n->num, 0, sizeof(BcDig) * places);
1623
1624 n->len += places;
1625 n->rdx += places;
1626 }
1627}
1628
1629static void bc_num_clean(BcNum *n)
1630{
1631 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1632 if (n->len == 0)
1633 n->neg = false;
1634 else if (n->len < n->rdx)
1635 n->len = n->rdx;
1636}
1637
1638static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1639{
1640 if (n->rdx < scale)
1641 bc_num_extend(n, scale - n->rdx);
1642 else
1643 bc_num_truncate(n, n->rdx - scale);
1644
1645 bc_num_clean(n);
1646 if (n->len != 0) n->neg = !neg1 != !neg2;
1647}
1648
1649static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1650 BcNum *restrict b)
1651{
1652 if (idx < n->len) {
1653
1654 b->len = n->len - idx;
1655 a->len = idx;
1656 a->rdx = b->rdx = 0;
1657
1658 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1659 memcpy(a->num, n->num, idx * sizeof(BcDig));
1660 }
1661 else {
1662 bc_num_zero(b);
1663 bc_num_copy(a, n);
1664 }
1665
1666 bc_num_clean(a);
1667 bc_num_clean(b);
1668}
1669
Denys Vlasenko29301232018-12-11 15:29:32 +01001670static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001671{
Denys Vlasenko29301232018-12-11 15:29:32 +01001672 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001673
1674 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1675 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1676 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001677 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001678 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001679
1680 if (n->rdx >= places)
1681 n->rdx -= places;
1682 else {
1683 bc_num_extend(n, places - n->rdx);
1684 n->rdx = 0;
1685 }
1686
1687 bc_num_clean(n);
1688
Denys Vlasenko29301232018-12-11 15:29:32 +01001689 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001690}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001691#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001692# define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001693#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001694
1695static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale)
1696{
1697 BcNum one;
1698 BcDig num[2];
1699
1700 one.cap = 2;
1701 one.num = num;
1702 bc_num_one(&one);
1703
1704 return bc_num_div(&one, a, b, scale);
1705}
1706
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001707static FAST_FUNC BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001708{
1709 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1710 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1711 int carry, in;
1712
1713 // Because this function doesn't need to use scale (per the bc spec),
1714 // I am hijacking it to say whether it's doing an add or a subtract.
1715
1716 if (a->len == 0) {
1717 bc_num_copy(c, b);
1718 if (sub && c->len) c->neg = !c->neg;
1719 return BC_STATUS_SUCCESS;
1720 }
1721 else if (b->len == 0) {
1722 bc_num_copy(c, a);
1723 return BC_STATUS_SUCCESS;
1724 }
1725
1726 c->neg = a->neg;
1727 c->rdx = BC_MAX(a->rdx, b->rdx);
1728 min_rdx = BC_MIN(a->rdx, b->rdx);
1729 c->len = 0;
1730
1731 if (a->rdx > b->rdx) {
1732 diff = a->rdx - b->rdx;
1733 ptr = a->num;
1734 ptr_a = a->num + diff;
1735 ptr_b = b->num;
1736 }
1737 else {
1738 diff = b->rdx - a->rdx;
1739 ptr = b->num;
1740 ptr_a = a->num;
1741 ptr_b = b->num + diff;
1742 }
1743
1744 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1745
1746 ptr_c += diff;
1747 a_int = BC_NUM_INT(a);
1748 b_int = BC_NUM_INT(b);
1749
1750 if (a_int > b_int) {
1751 min_int = b_int;
1752 max = a_int;
1753 ptr = ptr_a;
1754 }
1755 else {
1756 min_int = a_int;
1757 max = b_int;
1758 ptr = ptr_b;
1759 }
1760
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001761 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001762 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1763 carry = in / 10;
1764 ptr_c[i] = (BcDig)(in % 10);
1765 }
1766
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001767 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001768 in = ((int) ptr[i]) + carry;
1769 carry = in / 10;
1770 ptr_c[i] = (BcDig)(in % 10);
1771 }
1772
1773 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1774
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001775 return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001776}
1777
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001778static FAST_FUNC BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001779{
Gavin Howard01055ba2018-11-03 11:00:21 -06001780 ssize_t cmp;
1781 BcNum *minuend, *subtrahend;
1782 size_t start;
1783 bool aneg, bneg, neg;
1784
1785 // Because this function doesn't need to use scale (per the bc spec),
1786 // I am hijacking it to say whether it's doing an add or a subtract.
1787
1788 if (a->len == 0) {
1789 bc_num_copy(c, b);
1790 if (sub && c->len) c->neg = !c->neg;
1791 return BC_STATUS_SUCCESS;
1792 }
1793 else if (b->len == 0) {
1794 bc_num_copy(c, a);
1795 return BC_STATUS_SUCCESS;
1796 }
1797
1798 aneg = a->neg;
1799 bneg = b->neg;
1800 a->neg = b->neg = false;
1801
1802 cmp = bc_num_cmp(a, b);
1803
1804 a->neg = aneg;
1805 b->neg = bneg;
1806
1807 if (cmp == 0) {
1808 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
1809 return BC_STATUS_SUCCESS;
1810 }
1811 else if (cmp > 0) {
1812 neg = a->neg;
1813 minuend = a;
1814 subtrahend = b;
1815 }
1816 else {
1817 neg = b->neg;
1818 if (sub) neg = !neg;
1819 minuend = b;
1820 subtrahend = a;
1821 }
1822
1823 bc_num_copy(c, minuend);
1824 c->neg = neg;
1825
1826 if (c->rdx < subtrahend->rdx) {
1827 bc_num_extend(c, subtrahend->rdx - c->rdx);
1828 start = 0;
1829 }
1830 else
1831 start = c->rdx - subtrahend->rdx;
1832
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001833 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001834
1835 bc_num_clean(c);
1836
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001837 return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001838}
1839
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001840static FAST_FUNC BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001841 BcNum *restrict c)
1842{
1843 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001844 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001845 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001846 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001847
Gavin Howard01055ba2018-11-03 11:00:21 -06001848 if (a->len == 0 || b->len == 0) {
1849 bc_num_zero(c);
1850 return BC_STATUS_SUCCESS;
1851 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001852 aone = BC_NUM_ONE(a);
1853 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001854 bc_num_copy(c, aone ? b : a);
1855 return BC_STATUS_SUCCESS;
1856 }
1857
1858 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1859 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1860 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001861 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001862 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001863
Gavin Howard01055ba2018-11-03 11:00:21 -06001864 bc_num_expand(c, a->len + b->len + 1);
1865
1866 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001867 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001868
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001869 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001870
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001871 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001872 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001873 unsigned in = c->num[i + j];
1874 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1875 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001876 carry = in / 10;
1877 c->num[i + j] = (BcDig)(in % 10);
1878 }
1879
1880 c->num[i + j] += (BcDig) carry;
1881 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001882
1883 // a=2^1000000
1884 // a*a <- without check below, this will not be interruptible
1885 if (G_interrupt) return BC_STATUS_FAILURE;
Gavin Howard01055ba2018-11-03 11:00:21 -06001886 }
1887
1888 c->len = len;
1889
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001890 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 }
1892
1893 bc_num_init(&l1, max);
1894 bc_num_init(&h1, max);
1895 bc_num_init(&l2, max);
1896 bc_num_init(&h2, max);
1897 bc_num_init(&m1, max);
1898 bc_num_init(&m2, max);
1899 bc_num_init(&z0, max);
1900 bc_num_init(&z1, max);
1901 bc_num_init(&z2, max);
1902 bc_num_init(&temp, max + max);
1903
1904 bc_num_split(a, max2, &l1, &h1);
1905 bc_num_split(b, max2, &l2, &h2);
1906
1907 s = bc_num_add(&h1, &l1, &m1, 0);
1908 if (s) goto err;
1909 s = bc_num_add(&h2, &l2, &m2, 0);
1910 if (s) goto err;
1911
1912 s = bc_num_k(&h1, &h2, &z0);
1913 if (s) goto err;
1914 s = bc_num_k(&m1, &m2, &z1);
1915 if (s) goto err;
1916 s = bc_num_k(&l1, &l2, &z2);
1917 if (s) goto err;
1918
1919 s = bc_num_sub(&z1, &z0, &temp, 0);
1920 if (s) goto err;
1921 s = bc_num_sub(&temp, &z2, &z1, 0);
1922 if (s) goto err;
1923
Denys Vlasenko29301232018-12-11 15:29:32 +01001924 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001925 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001926 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001927 if (s) goto err;
1928 s = bc_num_add(&z0, &z1, &temp, 0);
1929 if (s) goto err;
1930 s = bc_num_add(&temp, &z2, c, 0);
1931
1932err:
1933 bc_num_free(&temp);
1934 bc_num_free(&z2);
1935 bc_num_free(&z1);
1936 bc_num_free(&z0);
1937 bc_num_free(&m2);
1938 bc_num_free(&m1);
1939 bc_num_free(&h2);
1940 bc_num_free(&l2);
1941 bc_num_free(&h1);
1942 bc_num_free(&l1);
1943 return s;
1944}
1945
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001946static FAST_FUNC BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001947{
1948 BcStatus s;
1949 BcNum cpa, cpb;
1950 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1951
1952 scale = BC_MAX(scale, a->rdx);
1953 scale = BC_MAX(scale, b->rdx);
1954 scale = BC_MIN(a->rdx + b->rdx, scale);
1955 maxrdx = BC_MAX(maxrdx, scale);
1956
1957 bc_num_init(&cpa, a->len);
1958 bc_num_init(&cpb, b->len);
1959
1960 bc_num_copy(&cpa, a);
1961 bc_num_copy(&cpb, b);
1962 cpa.neg = cpb.neg = false;
1963
Denys Vlasenko29301232018-12-11 15:29:32 +01001964 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001965 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001966 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001967 if (s) goto err;
1968 s = bc_num_k(&cpa, &cpb, c);
1969 if (s) goto err;
1970
1971 maxrdx += scale;
1972 bc_num_expand(c, c->len + maxrdx);
1973
1974 if (c->len < maxrdx) {
1975 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1976 c->len += maxrdx;
1977 }
1978
1979 c->rdx = maxrdx;
1980 bc_num_retireMul(c, scale, a->neg, b->neg);
1981
1982err:
1983 bc_num_free(&cpb);
1984 bc_num_free(&cpa);
1985 return s;
1986}
1987
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001988static FAST_FUNC BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001989{
1990 BcStatus s = BC_STATUS_SUCCESS;
1991 BcDig *n, *p, q;
1992 size_t len, end, i;
1993 BcNum cp;
1994 bool zero = true;
1995
1996 if (b->len == 0)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01001997 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06001998 else if (a->len == 0) {
1999 bc_num_setToZero(c, scale);
2000 return BC_STATUS_SUCCESS;
2001 }
2002 else if (BC_NUM_ONE(b)) {
2003 bc_num_copy(c, a);
2004 bc_num_retireMul(c, scale, a->neg, b->neg);
2005 return BC_STATUS_SUCCESS;
2006 }
2007
2008 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2009 bc_num_copy(&cp, a);
2010 len = b->len;
2011
2012 if (len > cp.len) {
2013 bc_num_expand(&cp, len + 2);
2014 bc_num_extend(&cp, len - cp.len);
2015 }
2016
2017 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2018 cp.rdx -= b->rdx;
2019 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2020
2021 if (b->rdx == b->len) {
2022 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
2023 len -= i - 1;
2024 }
2025
2026 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2027
2028 // We want an extra zero in front to make things simpler.
2029 cp.num[cp.len++] = 0;
2030 end = cp.len - len;
2031
2032 bc_num_expand(c, cp.len);
2033
2034 bc_num_zero(c);
2035 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2036 c->rdx = cp.rdx;
2037 c->len = cp.len;
2038 p = b->num;
2039
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002040 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002041 n = cp.num + i;
2042 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002043 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002044 c->num[i] = q;
Denys Vlasenkof381a882018-12-05 19:21:34 +01002045 // a=2^100000
2046 // scale=40000
2047 // 1/a <- without check below, this will not be interruptible
2048 if (G_interrupt) {
2049 s = BC_STATUS_FAILURE;
2050 break;
2051 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002052 }
2053
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002054 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002055 bc_num_free(&cp);
2056
Denys Vlasenkof381a882018-12-05 19:21:34 +01002057 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06002058}
2059
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002060static FAST_FUNC BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002061 BcNum *restrict d, size_t scale, size_t ts)
2062{
2063 BcStatus s;
2064 BcNum temp;
2065 bool neg;
2066
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002067 if (b->len == 0)
2068 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06002069
2070 if (a->len == 0) {
2071 bc_num_setToZero(d, ts);
2072 return BC_STATUS_SUCCESS;
2073 }
2074
2075 bc_num_init(&temp, d->cap);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002076 s = bc_num_d(a, b, c, scale);
2077 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002078
2079 if (scale != 0) scale = ts;
2080
2081 s = bc_num_m(c, b, &temp, scale);
2082 if (s) goto err;
2083 s = bc_num_sub(a, &temp, d, scale);
2084 if (s) goto err;
2085
2086 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2087
2088 neg = d->neg;
2089 bc_num_retireMul(d, ts, a->neg, b->neg);
2090 d->neg = neg;
2091
2092err:
2093 bc_num_free(&temp);
2094 return s;
2095}
2096
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002097static FAST_FUNC BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002098{
2099 BcStatus s;
2100 BcNum c1;
2101 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2102
2103 bc_num_init(&c1, len);
2104 s = bc_num_r(a, b, &c1, c, scale, ts);
2105 bc_num_free(&c1);
2106
2107 return s;
2108}
2109
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002110static FAST_FUNC BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002111{
2112 BcStatus s = BC_STATUS_SUCCESS;
2113 BcNum copy;
2114 unsigned long pow;
2115 size_t i, powrdx, resrdx;
2116 bool neg, zero;
2117
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002118 if (b->rdx) return bc_error("non integer number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002119
2120 if (b->len == 0) {
2121 bc_num_one(c);
2122 return BC_STATUS_SUCCESS;
2123 }
2124 else if (a->len == 0) {
2125 bc_num_setToZero(c, scale);
2126 return BC_STATUS_SUCCESS;
2127 }
2128 else if (BC_NUM_ONE(b)) {
2129 if (!b->neg)
2130 bc_num_copy(c, a);
2131 else
2132 s = bc_num_inv(a, c, scale);
2133 return s;
2134 }
2135
2136 neg = b->neg;
2137 b->neg = false;
2138
Denys Vlasenko29301232018-12-11 15:29:32 +01002139 s = zbc_num_ulong(b, &pow);
Gavin Howard01055ba2018-11-03 11:00:21 -06002140 if (s) return s;
2141
2142 bc_num_init(&copy, a->len);
2143 bc_num_copy(&copy, a);
2144
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002145 if (!neg) {
2146 if (a->rdx > scale)
2147 scale = a->rdx;
2148 if (a->rdx * pow < scale)
2149 scale = a->rdx * pow;
2150 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002151
2152 b->neg = neg;
2153
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002154 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002155 powrdx <<= 1;
2156 s = bc_num_mul(&copy, &copy, &copy, powrdx);
2157 if (s) goto err;
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002158 // Not needed: bc_num_mul() has a check for ^C:
2159 //if (G_interrupt) {
2160 // s = BC_STATUS_FAILURE;
2161 // goto err;
2162 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002163 }
2164
Gavin Howard01055ba2018-11-03 11:00:21 -06002165 bc_num_copy(c, &copy);
2166
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002167 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002168
2169 powrdx <<= 1;
2170 s = bc_num_mul(&copy, &copy, &copy, powrdx);
2171 if (s) goto err;
2172
2173 if (pow & 1) {
2174 resrdx += powrdx;
2175 s = bc_num_mul(c, &copy, c, resrdx);
2176 if (s) goto err;
2177 }
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002178 // Not needed: bc_num_mul() has a check for ^C:
2179 //if (G_interrupt) {
2180 // s = BC_STATUS_FAILURE;
2181 // goto err;
2182 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002183 }
2184
2185 if (neg) {
2186 s = bc_num_inv(c, c, scale);
2187 if (s) goto err;
2188 }
2189
Gavin Howard01055ba2018-11-03 11:00:21 -06002190 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2191
2192 // We can't use bc_num_clean() here.
2193 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2194 if (zero) bc_num_setToZero(c, scale);
2195
2196err:
2197 bc_num_free(&copy);
2198 return s;
2199}
2200
2201static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
2202 BcNumBinaryOp op, size_t req)
2203{
2204 BcStatus s;
2205 BcNum num2, *ptr_a, *ptr_b;
2206 bool init = false;
2207
2208 if (c == a) {
2209 ptr_a = &num2;
2210 memcpy(ptr_a, c, sizeof(BcNum));
2211 init = true;
2212 }
2213 else
2214 ptr_a = a;
2215
2216 if (c == b) {
2217 ptr_b = &num2;
2218 if (c != a) {
2219 memcpy(ptr_b, c, sizeof(BcNum));
2220 init = true;
2221 }
2222 }
2223 else
2224 ptr_b = b;
2225
2226 if (init)
2227 bc_num_init(c, req);
2228 else
2229 bc_num_expand(c, req);
2230
2231 s = op(ptr_a, ptr_b, c, scale);
2232
2233 if (init) bc_num_free(&num2);
2234
2235 return s;
2236}
2237
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002238static void bc_num_printNewline(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002239{
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002240 if (G.prog.nchars == G.prog.len - 1) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002241 bb_putchar('\\');
2242 bb_putchar('\n');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002243 G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06002244 }
2245}
2246
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002247#if ENABLE_DC
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002248static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
Gavin Howard01055ba2018-11-03 11:00:21 -06002249{
Denys Vlasenko2a8ad482018-12-08 21:56:37 +01002250 (void) radix;
Denys Vlasenko00d77792018-11-30 23:13:42 +01002251 bb_putchar((char) num);
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002252 G.prog.nchars += width;
Gavin Howard01055ba2018-11-03 11:00:21 -06002253}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002254#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002255
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002256static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
Gavin Howard01055ba2018-11-03 11:00:21 -06002257{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002258 size_t exp, pow;
Gavin Howard01055ba2018-11-03 11:00:21 -06002259
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002260 bc_num_printNewline();
Denys Vlasenko00d77792018-11-30 23:13:42 +01002261 bb_putchar(radix ? '.' : ' ');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002262 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06002263
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002264 bc_num_printNewline();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002265 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
2266 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002267
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002268 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002269 size_t dig;
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002270 bc_num_printNewline();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002271 dig = num / pow;
2272 num -= dig * pow;
Denys Vlasenko00d77792018-11-30 23:13:42 +01002273 bb_putchar(((char) dig) + '0');
Gavin Howard01055ba2018-11-03 11:00:21 -06002274 }
2275}
2276
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002277static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
Gavin Howard01055ba2018-11-03 11:00:21 -06002278{
2279 if (radix) {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002280 bc_num_printNewline();
Denys Vlasenko00d77792018-11-30 23:13:42 +01002281 bb_putchar('.');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002282 G.prog.nchars += 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002283 }
2284
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002285 bc_num_printNewline();
Denys Vlasenko00d77792018-11-30 23:13:42 +01002286 bb_putchar(bb_hexdigits_upcase[num]);
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002287 G.prog.nchars += width;
Gavin Howard01055ba2018-11-03 11:00:21 -06002288}
2289
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002290static void bc_num_printDecimal(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06002291{
2292 size_t i, rdx = n->rdx - 1;
2293
Denys Vlasenko00d77792018-11-30 23:13:42 +01002294 if (n->neg) bb_putchar('-');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002295 G.prog.nchars += n->neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002296
2297 for (i = n->len - 1; i < n->len; --i)
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002298 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002299}
2300
Denys Vlasenko29301232018-12-11 15:29:32 +01002301static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
Gavin Howard01055ba2018-11-03 11:00:21 -06002302{
2303 BcStatus s;
2304 BcVec stack;
2305 BcNum intp, fracp, digit, frac_len;
2306 unsigned long dig, *ptr;
2307 size_t i;
2308 bool radix;
2309
2310 if (n->len == 0) {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002311 print(0, width, false);
Denys Vlasenko29301232018-12-11 15:29:32 +01002312 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002313 }
2314
2315 bc_vec_init(&stack, sizeof(long), NULL);
2316 bc_num_init(&intp, n->len);
2317 bc_num_init(&fracp, n->rdx);
2318 bc_num_init(&digit, width);
2319 bc_num_init(&frac_len, BC_NUM_INT(n));
2320 bc_num_copy(&intp, n);
2321 bc_num_one(&frac_len);
2322
2323 bc_num_truncate(&intp, intp.rdx);
2324 s = bc_num_sub(n, &intp, &fracp, 0);
2325 if (s) goto err;
2326
2327 while (intp.len != 0) {
2328 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
2329 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01002330 s = zbc_num_ulong(&digit, &dig);
Gavin Howard01055ba2018-11-03 11:00:21 -06002331 if (s) goto err;
2332 bc_vec_push(&stack, &dig);
2333 }
2334
2335 for (i = 0; i < stack.len; ++i) {
2336 ptr = bc_vec_item_rev(&stack, i);
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002337 print(*ptr, width, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06002338 }
2339
2340 if (!n->rdx) goto err;
2341
2342 for (radix = true; frac_len.len <= n->rdx; radix = false) {
2343 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
2344 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01002345 s = zbc_num_ulong(&fracp, &dig);
Gavin Howard01055ba2018-11-03 11:00:21 -06002346 if (s) goto err;
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01002347 bc_num_ulong2num(&intp, dig);
Gavin Howard01055ba2018-11-03 11:00:21 -06002348 s = bc_num_sub(&fracp, &intp, &fracp, 0);
2349 if (s) goto err;
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002350 print(dig, width, radix);
Gavin Howard01055ba2018-11-03 11:00:21 -06002351 s = bc_num_mul(&frac_len, base, &frac_len, 0);
2352 if (s) goto err;
2353 }
2354
2355err:
2356 bc_num_free(&frac_len);
2357 bc_num_free(&digit);
2358 bc_num_free(&fracp);
2359 bc_num_free(&intp);
2360 bc_vec_free(&stack);
Denys Vlasenko29301232018-12-11 15:29:32 +01002361 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002362}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002363#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002364# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002365#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002366
Denys Vlasenko29301232018-12-11 15:29:32 +01002367static BC_STATUS zbc_num_printBase(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06002368{
2369 BcStatus s;
2370 size_t width, i;
2371 BcNumDigitOp print;
2372 bool neg = n->neg;
2373
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002374 if (neg) {
2375 bb_putchar('-');
2376 G.prog.nchars++;
2377 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002378
2379 n->neg = false;
2380
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002381 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002382 width = 1;
2383 print = bc_num_printHex;
2384 }
2385 else {
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002386 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
2387 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002388 print = bc_num_printDigits;
2389 }
2390
Denys Vlasenko29301232018-12-11 15:29:32 +01002391 s = zbc_num_printNum(n, &G.prog.ob, width, print);
Gavin Howard01055ba2018-11-03 11:00:21 -06002392 n->neg = neg;
2393
Denys Vlasenko29301232018-12-11 15:29:32 +01002394 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002395}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002396#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002397# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002398#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002399
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002400#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01002401static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
Gavin Howard01055ba2018-11-03 11:00:21 -06002402{
Denys Vlasenko29301232018-12-11 15:29:32 +01002403 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
Gavin Howard01055ba2018-11-03 11:00:21 -06002404}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01002405#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002406# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01002407#endif
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002408#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002409
Denys Vlasenko9311e012018-12-10 11:54:18 +01002410static bool bc_num_strValid(const char *val, size_t base)
2411{
2412 BcDig b;
2413 bool radix;
2414
2415 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2416 radix = false;
2417 for (;;) {
2418 BcDig c = *val++;
2419 if (c == '\0')
2420 break;
2421 if (c == '.') {
2422 if (radix) return false;
2423 radix = true;
2424 continue;
2425 }
2426 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2427 return false;
2428 }
2429 return true;
2430}
2431
2432// Note: n is already "bc_num_zero()"ed,
2433// leading zeroes in "val" are removed
2434static void bc_num_parseDecimal(BcNum *n, const char *val)
2435{
2436 size_t len, i;
2437 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002438
2439 len = strlen(val);
2440 if (len == 0)
2441 return;
2442
Denys Vlasenko9311e012018-12-10 11:54:18 +01002443 bc_num_expand(n, len);
2444
2445 ptr = strchr(val, '.');
2446
2447 n->rdx = 0;
2448 if (ptr != NULL)
2449 n->rdx = (size_t)((val + len) - (ptr + 1));
2450
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002451 for (i = 0; val[i]; ++i) {
2452 if (val[i] != '0' && val[i] != '.') {
2453 // Not entirely zero value - convert it, and exit
2454 i = len - 1;
2455 for (;;) {
2456 n->num[n->len] = val[i] - '0';
2457 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002458 skip_dot:
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002459 if ((ssize_t)--i == (ssize_t)-1) break;
2460 if (val[i] == '.') goto skip_dot;
2461 }
2462 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002463 }
2464 }
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002465 // if this is reached, the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002466}
2467
2468// Note: n is already "bc_num_zero()"ed,
2469// leading zeroes in "val" are removed
2470static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2471{
2472 BcStatus s;
2473 BcNum temp, mult, result;
2474 BcDig c = '\0';
2475 unsigned long v;
2476 size_t i, digits;
2477
2478 for (i = 0; ; ++i) {
2479 if (val[i] == '\0')
2480 return;
2481 if (val[i] != '.' && val[i] != '0')
2482 break;
2483 }
2484
2485 bc_num_init_DEF_SIZE(&temp);
2486 bc_num_init_DEF_SIZE(&mult);
2487
2488 for (;;) {
2489 c = *val++;
2490 if (c == '\0') goto int_err;
2491 if (c == '.') break;
2492
2493 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2494
2495 s = bc_num_mul(n, base, &mult, 0);
2496 if (s) goto int_err;
2497 bc_num_ulong2num(&temp, v);
2498 s = bc_num_add(&mult, &temp, n, 0);
2499 if (s) goto int_err;
2500 }
2501
2502 bc_num_init(&result, base->len);
2503 //bc_num_zero(&result); - already is
2504 bc_num_one(&mult);
2505
2506 digits = 0;
2507 for (;;) {
2508 c = *val++;
2509 if (c == '\0') break;
2510 digits++;
2511
2512 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2513
2514 s = bc_num_mul(&result, base, &result, 0);
2515 if (s) goto err;
2516 bc_num_ulong2num(&temp, v);
2517 s = bc_num_add(&result, &temp, &result, 0);
2518 if (s) goto err;
2519 s = bc_num_mul(&mult, base, &mult, 0);
2520 if (s) goto err;
2521 }
2522
2523 s = bc_num_div(&result, &mult, &result, digits);
2524 if (s) goto err;
2525 s = bc_num_add(n, &result, n, digits);
2526 if (s) goto err;
2527
2528 if (n->len != 0) {
2529 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2530 } else
2531 bc_num_zero(n);
2532
2533err:
2534 bc_num_free(&result);
2535int_err:
2536 bc_num_free(&mult);
2537 bc_num_free(&temp);
2538}
2539
Denys Vlasenko29301232018-12-11 15:29:32 +01002540static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002541 size_t base_t)
2542{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002543 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002544 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002545
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002546 bc_num_zero(n);
2547 while (*val == '0') val++;
2548
Gavin Howard01055ba2018-11-03 11:00:21 -06002549 if (base_t == 10)
2550 bc_num_parseDecimal(n, val);
2551 else
2552 bc_num_parseBase(n, val, base);
2553
Denys Vlasenko29301232018-12-11 15:29:32 +01002554 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002555}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002556#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002557# define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002558#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002559
Denys Vlasenko29301232018-12-11 15:29:32 +01002560static BC_STATUS zbc_num_print(BcNum *n, bool newline)
Gavin Howard01055ba2018-11-03 11:00:21 -06002561{
2562 BcStatus s = BC_STATUS_SUCCESS;
2563
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002564 bc_num_printNewline();
Gavin Howard01055ba2018-11-03 11:00:21 -06002565
2566 if (n->len == 0) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002567 bb_putchar('0');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002568 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06002569 }
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002570 else if (G.prog.ob_t == 10)
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002571 bc_num_printDecimal(n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002572 else
Denys Vlasenko29301232018-12-11 15:29:32 +01002573 s = zbc_num_printBase(n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002574
2575 if (newline) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002576 bb_putchar('\n');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002577 G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06002578 }
2579
Denys Vlasenko29301232018-12-11 15:29:32 +01002580 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002581}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002582#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002583# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002584#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002585
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002586static FAST_FUNC BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002587{
2588 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
2589 (void) scale;
2590 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
2591}
2592
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002593static FAST_FUNC BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002594{
2595 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
2596 (void) scale;
2597 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
2598}
2599
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002600static FAST_FUNC BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002601{
2602 size_t req = BC_NUM_MREQ(a, b, scale);
2603 return bc_num_binary(a, b, c, scale, bc_num_m, req);
2604}
2605
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002606static FAST_FUNC BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002607{
2608 size_t req = BC_NUM_MREQ(a, b, scale);
2609 return bc_num_binary(a, b, c, scale, bc_num_d, req);
2610}
2611
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002612static FAST_FUNC BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002613{
2614 size_t req = BC_NUM_MREQ(a, b, scale);
2615 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
2616}
2617
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002618static FAST_FUNC BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002619{
2620 return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1);
2621}
2622
2623static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
2624{
2625 BcStatus s;
2626 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2627 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2628 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2629
2630 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2631 bc_num_expand(b, req);
2632
2633 if (a->len == 0) {
2634 bc_num_setToZero(b, scale);
2635 return BC_STATUS_SUCCESS;
2636 }
2637 else if (a->neg)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002638 return bc_error("negative number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002639 else if (BC_NUM_ONE(a)) {
2640 bc_num_one(b);
2641 bc_num_extend(b, scale);
2642 return BC_STATUS_SUCCESS;
2643 }
2644
2645 scale = BC_MAX(scale, a->rdx) + 1;
2646 len = a->len + scale;
2647
2648 bc_num_init(&num1, len);
2649 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002650 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002651
2652 bc_num_one(&half);
2653 half.num[0] = 5;
2654 half.rdx = 1;
2655
2656 bc_num_init(&f, len);
2657 bc_num_init(&fprime, len);
2658
2659 x0 = &num1;
2660 x1 = &num2;
2661
2662 bc_num_one(x0);
2663 pow = BC_NUM_INT(a);
2664
2665 if (pow) {
2666
2667 if (pow & 1)
2668 x0->num[0] = 2;
2669 else
2670 x0->num[0] = 6;
2671
2672 pow -= 2 - (pow & 1);
2673
2674 bc_num_extend(x0, pow);
2675
2676 // Make sure to move the radix back.
2677 x0->rdx -= pow;
2678 }
2679
2680 x0->rdx = digs = digs1 = 0;
2681 resrdx = scale + 2;
2682 len = BC_NUM_INT(x0) + resrdx - 1;
2683
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002684 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002685
2686 s = bc_num_div(a, x0, &f, resrdx);
2687 if (s) goto err;
2688 s = bc_num_add(x0, &f, &fprime, resrdx);
2689 if (s) goto err;
2690 s = bc_num_mul(&fprime, &half, x1, resrdx);
2691 if (s) goto err;
2692
2693 cmp = bc_num_cmp(x1, x0);
2694 digs = x1->len - (unsigned long long) llabs(cmp);
2695
2696 if (cmp == cmp2 && digs == digs1)
2697 times += 1;
2698 else
2699 times = 0;
2700
2701 resrdx += times > 4;
2702
2703 cmp2 = cmp1;
2704 cmp1 = cmp;
2705 digs1 = digs;
2706
2707 temp = x0;
2708 x0 = x1;
2709 x1 = temp;
2710 }
2711
Gavin Howard01055ba2018-11-03 11:00:21 -06002712 bc_num_copy(b, x0);
2713 scale -= 1;
2714 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2715
2716err:
2717 bc_num_free(&fprime);
2718 bc_num_free(&f);
2719 bc_num_free(&half);
2720 bc_num_free(&num2);
2721 bc_num_free(&num1);
2722 return s;
2723}
2724
2725static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
2726 size_t scale)
2727{
2728 BcStatus s;
2729 BcNum num2, *ptr_a;
2730 bool init = false;
2731 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2732
2733 if (c == a) {
2734 memcpy(&num2, c, sizeof(BcNum));
2735 ptr_a = &num2;
2736 bc_num_init(c, len);
2737 init = true;
2738 }
2739 else {
2740 ptr_a = a;
2741 bc_num_expand(c, len);
2742 }
2743
2744 s = bc_num_r(ptr_a, b, c, d, scale, ts);
2745
2746 if (init) bc_num_free(&num2);
2747
2748 return s;
2749}
2750
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002751#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002752static BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
2753{
2754 BcStatus s;
2755 BcNum base, exp, two, temp;
2756
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002757 if (c->len == 0)
2758 return bc_error("divide by zero");
2759 if (a->rdx || b->rdx || c->rdx)
2760 return bc_error("non integer number");
2761 if (b->neg)
2762 return bc_error("negative number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002763
2764 bc_num_expand(d, c->len);
2765 bc_num_init(&base, c->len);
2766 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002767 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002768 bc_num_init(&temp, b->len);
2769
2770 bc_num_one(&two);
2771 two.num[0] = 2;
2772 bc_num_one(d);
2773
2774 s = bc_num_rem(a, c, &base, 0);
2775 if (s) goto err;
2776 bc_num_copy(&exp, b);
2777
2778 while (exp.len != 0) {
2779
2780 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
2781 if (s) goto err;
2782
2783 if (BC_NUM_ONE(&temp)) {
2784 s = bc_num_mul(d, &base, &temp, 0);
2785 if (s) goto err;
2786 s = bc_num_rem(&temp, c, d, 0);
2787 if (s) goto err;
2788 }
2789
2790 s = bc_num_mul(&base, &base, &temp, 0);
2791 if (s) goto err;
2792 s = bc_num_rem(&temp, c, &base, 0);
2793 if (s) goto err;
2794 }
2795
2796err:
2797 bc_num_free(&temp);
2798 bc_num_free(&two);
2799 bc_num_free(&exp);
2800 bc_num_free(&base);
2801 return s;
2802}
2803#endif // ENABLE_DC
2804
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002805#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002806static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002807{
2808 BcId a;
2809 size_t i;
2810
2811 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002812 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002813 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002814 }
2815
2816 a.idx = var;
2817 a.name = name;
2818
2819 bc_vec_push(&f->autos, &a);
2820
Denys Vlasenko29301232018-12-11 15:29:32 +01002821 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002822}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002823#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002824# define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002825#endif
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002826#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002827
2828static void bc_func_init(BcFunc *f)
2829{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002830 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002831 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2832 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2833 f->nparams = 0;
2834}
2835
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002836static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002837{
2838 BcFunc *f = (BcFunc *) func;
2839 bc_vec_free(&f->code);
2840 bc_vec_free(&f->autos);
2841 bc_vec_free(&f->labels);
2842}
2843
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002844static void bc_array_expand(BcVec *a, size_t len);
2845
Gavin Howard01055ba2018-11-03 11:00:21 -06002846static void bc_array_init(BcVec *a, bool nums)
2847{
2848 if (nums)
2849 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2850 else
2851 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2852 bc_array_expand(a, 1);
2853}
2854
Gavin Howard01055ba2018-11-03 11:00:21 -06002855static void bc_array_expand(BcVec *a, size_t len)
2856{
2857 BcResultData data;
2858
2859 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2860 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002861 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002862 bc_vec_push(a, &data.n);
2863 }
2864 }
2865 else {
2866 while (len > a->len) {
2867 bc_array_init(&data.v, true);
2868 bc_vec_push(a, &data.v);
2869 }
2870 }
2871}
2872
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002873static void bc_array_copy(BcVec *d, const BcVec *s)
2874{
2875 size_t i;
2876
2877 bc_vec_pop_all(d);
2878 bc_vec_expand(d, s->cap);
2879 d->len = s->len;
2880
2881 for (i = 0; i < s->len; ++i) {
2882 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2883 bc_num_init(dnum, snum->len);
2884 bc_num_copy(dnum, snum);
2885 }
2886}
2887
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002888static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002889{
2890 free(*((char **) string));
2891}
2892
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002893#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002894static void bc_result_copy(BcResult *d, BcResult *src)
2895{
2896 d->t = src->t;
2897
2898 switch (d->t) {
2899
2900 case BC_RESULT_TEMP:
2901 case BC_RESULT_IBASE:
2902 case BC_RESULT_SCALE:
2903 case BC_RESULT_OBASE:
2904 {
2905 bc_num_init(&d->d.n, src->d.n.len);
2906 bc_num_copy(&d->d.n, &src->d.n);
2907 break;
2908 }
2909
2910 case BC_RESULT_VAR:
2911 case BC_RESULT_ARRAY:
2912 case BC_RESULT_ARRAY_ELEM:
2913 {
2914 d->d.id.name = xstrdup(src->d.id.name);
2915 break;
2916 }
2917
2918 case BC_RESULT_CONSTANT:
2919 case BC_RESULT_LAST:
2920 case BC_RESULT_ONE:
2921 case BC_RESULT_STR:
2922 {
2923 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2924 break;
2925 }
2926 }
2927}
2928#endif // ENABLE_DC
2929
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002930static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002931{
2932 BcResult *r = (BcResult *) result;
2933
2934 switch (r->t) {
2935
2936 case BC_RESULT_TEMP:
2937 case BC_RESULT_IBASE:
2938 case BC_RESULT_SCALE:
2939 case BC_RESULT_OBASE:
2940 {
2941 bc_num_free(&r->d.n);
2942 break;
2943 }
2944
2945 case BC_RESULT_VAR:
2946 case BC_RESULT_ARRAY:
2947 case BC_RESULT_ARRAY_ELEM:
2948 {
2949 free(r->d.id.name);
2950 break;
2951 }
2952
2953 default:
2954 {
2955 // Do nothing.
2956 break;
2957 }
2958 }
2959}
2960
2961static void bc_lex_lineComment(BcLex *l)
2962{
2963 l->t.t = BC_LEX_WHITESPACE;
2964 while (l->i < l->len && l->buf[l->i++] != '\n');
2965 --l->i;
2966}
2967
2968static void bc_lex_whitespace(BcLex *l)
2969{
2970 char c;
2971 l->t.t = BC_LEX_WHITESPACE;
2972 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
2973}
2974
Denys Vlasenko29301232018-12-11 15:29:32 +01002975static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002976{
2977 const char *buf = l->buf + l->i;
2978 size_t len, hits = 0, bslashes = 0, i = 0, j;
2979 char c = buf[i];
2980 bool last_pt, pt = start == '.';
2981
2982 last_pt = pt;
2983 l->t.t = BC_LEX_NUMBER;
2984
2985 while (c != 0 && (isdigit(c) || (c >= 'A' && c <= 'F') ||
2986 (c == '.' && !pt) || (c == '\\' && buf[i + 1] == '\n')))
2987 {
2988 if (c != '\\') {
2989 last_pt = c == '.';
2990 pt = pt || last_pt;
2991 }
2992 else {
2993 ++i;
2994 bslashes += 1;
2995 }
2996
2997 c = buf[++i];
2998 }
2999
Denys Vlasenkod5f77032018-12-04 21:37:56 +01003000 len = i + !last_pt - bslashes * 2;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003001 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
3002 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
3003 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01003004 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003005 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003006
Denys Vlasenko7d628012018-12-04 21:46:47 +01003007 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003008 bc_vec_expand(&l->t.v, len + 1);
3009 bc_vec_push(&l->t.v, &start);
3010
3011 for (buf -= 1, j = 1; j < len + hits * 2; ++j) {
3012
3013 c = buf[j];
3014
3015 // If we have hit a backslash, skip it. We don't have
3016 // to check for a newline because it's guaranteed.
3017 if (hits < bslashes && c == '\\') {
3018 ++hits;
3019 ++j;
3020 continue;
3021 }
3022
3023 bc_vec_push(&l->t.v, &c);
3024 }
3025
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003026 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003027 l->i += i;
3028
Denys Vlasenko29301232018-12-11 15:29:32 +01003029 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003030}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01003031#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003032# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01003033#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003034
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003035static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003036{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003037 size_t i;
3038 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003039
3040 l->t.t = BC_LEX_NAME;
3041
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003042 i = 0;
3043 buf = l->buf + l->i - 1;
3044 for (;;) {
3045 char c = buf[i];
3046 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
3047 i++;
3048 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003049
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003050#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01003051 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3052 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3053 if (i > BC_MAX_STRING)
3054 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
3055 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003056#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003057 bc_vec_string(&l->t.v, i, buf);
3058
3059 // Increment the index. We minus 1 because it has already been incremented.
3060 l->i += i - 1;
3061
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003062 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003063}
3064
3065static void bc_lex_init(BcLex *l, BcLexNext next)
3066{
3067 l->next = next;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003068 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003069}
3070
3071static void bc_lex_free(BcLex *l)
3072{
3073 bc_vec_free(&l->t.v);
3074}
3075
Denys Vlasenko0409ad32018-12-05 16:39:22 +01003076static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003077{
Denys Vlasenko5318f812018-12-05 17:48:01 +01003078 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003079 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06003080}
3081
3082static BcStatus bc_lex_next(BcLex *l)
3083{
3084 BcStatus s;
3085
3086 l->t.last = l->t.t;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003087 if (l->t.last == BC_LEX_EOF) return bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06003088
3089 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003090 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003091 l->t.t = BC_LEX_EOF;
3092
3093 l->newline = (l->i == l->len);
3094 if (l->newline) return BC_STATUS_SUCCESS;
3095
3096 // Loop until failure or we don't have whitespace. This
3097 // is so the parser doesn't get inundated with whitespace.
3098 do {
3099 s = l->next(l);
3100 } while (!s && l->t.t == BC_LEX_WHITESPACE);
3101
3102 return s;
3103}
3104
3105static BcStatus bc_lex_text(BcLex *l, const char *text)
3106{
3107 l->buf = text;
3108 l->i = 0;
3109 l->len = strlen(text);
3110 l->t.t = l->t.last = BC_LEX_INVALID;
3111 return bc_lex_next(l);
3112}
3113
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003114#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06003115static BcStatus bc_lex_identifier(BcLex *l)
3116{
3117 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003118 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003119 const char *buf = l->buf + l->i - 1;
3120
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003121 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3122 const char *keyword8 = bc_lex_kws[i].name8;
3123 unsigned j = 0;
3124 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3125 j++;
3126 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003127 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003128 if (keyword8[j] != '\0')
3129 continue;
3130 match:
3131 // buf starts with keyword bc_lex_kws[i]
3132 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003133 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003134 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003135 if (s) return s;
3136 }
3137
3138 // We minus 1 because the index has already been incremented.
3139 l->i += j - 1;
3140 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 }
3142
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003143 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003144
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003145 if (l->t.v.len > 2) {
3146 // Prevent this:
3147 // >>> qwe=1
3148 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3149 // '
3150 unsigned len = strchrnul(buf, '\n') - buf;
3151 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3152 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003153
3154 return s;
3155}
3156
3157static BcStatus bc_lex_string(BcLex *l)
3158{
3159 size_t len, nls = 0, i = l->i;
3160 char c;
3161
3162 l->t.t = BC_LEX_STR;
3163
3164 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n');
3165
3166 if (c == '\0') {
3167 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003168 return bc_error("string end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003169 }
3170
3171 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003172 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3173 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3174 if (len > BC_MAX_STRING)
3175 return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]");
3176 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003177 bc_vec_string(&l->t.v, len, l->buf + l->i);
3178
3179 l->i = i + 1;
3180 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003181 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003182
3183 return BC_STATUS_SUCCESS;
3184}
3185
3186static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3187{
3188 if (l->buf[l->i] == '=') {
3189 ++l->i;
3190 l->t.t = with;
3191 }
3192 else
3193 l->t.t = without;
3194}
3195
Denys Vlasenko29301232018-12-11 15:29:32 +01003196static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003197{
3198 size_t i, nls = 0;
3199 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003200
3201 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003202 i = ++l->i;
3203 for (;;) {
3204 char c = buf[i];
3205 check_star:
3206 if (c == '*') {
3207 c = buf[++i];
3208 if (c == '/')
3209 break;
3210 goto check_star;
3211 }
3212 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003214 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003215 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003216 nls += (c == '\n');
3217 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 }
3219
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003220 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003222 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003223
Denys Vlasenko29301232018-12-11 15:29:32 +01003224 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003225}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003226#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003227# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003228#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003229
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003230static FAST_FUNC BcStatus bc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003231{
3232 BcStatus s = BC_STATUS_SUCCESS;
3233 char c = l->buf[l->i++], c2;
3234
3235 // This is the workhorse of the lexer.
3236 switch (c) {
3237
3238 case '\0':
3239 case '\n':
3240 {
3241 l->newline = true;
3242 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3243 break;
3244 }
3245
3246 case '\t':
3247 case '\v':
3248 case '\f':
3249 case '\r':
3250 case ' ':
3251 {
3252 bc_lex_whitespace(l);
3253 break;
3254 }
3255
3256 case '!':
3257 {
3258 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3259
3260 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003261 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Gavin Howard01055ba2018-11-03 11:00:21 -06003262 if (s) return s;
3263 }
3264
3265 break;
3266 }
3267
3268 case '"':
3269 {
3270 s = bc_lex_string(l);
3271 break;
3272 }
3273
3274 case '#':
3275 {
Denys Vlasenko00646792018-12-05 18:12:27 +01003276 s = bc_POSIX_does_not_allow("'#' script comments");
Gavin Howard01055ba2018-11-03 11:00:21 -06003277 if (s) return s;
3278
3279 bc_lex_lineComment(l);
3280
3281 break;
3282 }
3283
3284 case '%':
3285 {
3286 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3287 break;
3288 }
3289
3290 case '&':
3291 {
3292 c2 = l->buf[l->i];
3293 if (c2 == '&') {
3294
Denys Vlasenko00646792018-12-05 18:12:27 +01003295 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Gavin Howard01055ba2018-11-03 11:00:21 -06003296 if (s) return s;
3297
3298 ++l->i;
3299 l->t.t = BC_LEX_OP_BOOL_AND;
3300 }
3301 else {
3302 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003303 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003304 }
3305
3306 break;
3307 }
3308
3309 case '(':
3310 case ')':
3311 {
3312 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3313 break;
3314 }
3315
3316 case '*':
3317 {
3318 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3319 break;
3320 }
3321
3322 case '+':
3323 {
3324 c2 = l->buf[l->i];
3325 if (c2 == '+') {
3326 ++l->i;
3327 l->t.t = BC_LEX_OP_INC;
3328 }
3329 else
3330 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3331 break;
3332 }
3333
3334 case ',':
3335 {
3336 l->t.t = BC_LEX_COMMA;
3337 break;
3338 }
3339
3340 case '-':
3341 {
3342 c2 = l->buf[l->i];
3343 if (c2 == '-') {
3344 ++l->i;
3345 l->t.t = BC_LEX_OP_DEC;
3346 }
3347 else
3348 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3349 break;
3350 }
3351
3352 case '.':
3353 {
3354 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003355 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003356 else {
3357 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003358 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003359 }
3360 break;
3361 }
3362
3363 case '/':
3364 {
3365 c2 = l->buf[l->i];
3366 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003367 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003368 else
3369 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3370 break;
3371 }
3372
3373 case '0':
3374 case '1':
3375 case '2':
3376 case '3':
3377 case '4':
3378 case '5':
3379 case '6':
3380 case '7':
3381 case '8':
3382 case '9':
3383 case 'A':
3384 case 'B':
3385 case 'C':
3386 case 'D':
3387 case 'E':
3388 case 'F':
3389 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003390 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003391 break;
3392 }
3393
3394 case ';':
3395 {
3396 l->t.t = BC_LEX_SCOLON;
3397 break;
3398 }
3399
3400 case '<':
3401 {
3402 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3403 break;
3404 }
3405
3406 case '=':
3407 {
3408 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3409 break;
3410 }
3411
3412 case '>':
3413 {
3414 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3415 break;
3416 }
3417
3418 case '[':
3419 case ']':
3420 {
3421 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3422 break;
3423 }
3424
3425 case '\\':
3426 {
3427 if (l->buf[l->i] == '\n') {
3428 l->t.t = BC_LEX_WHITESPACE;
3429 ++l->i;
3430 }
3431 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003432 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003433 break;
3434 }
3435
3436 case '^':
3437 {
3438 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3439 break;
3440 }
3441
3442 case 'a':
3443 case 'b':
3444 case 'c':
3445 case 'd':
3446 case 'e':
3447 case 'f':
3448 case 'g':
3449 case 'h':
3450 case 'i':
3451 case 'j':
3452 case 'k':
3453 case 'l':
3454 case 'm':
3455 case 'n':
3456 case 'o':
3457 case 'p':
3458 case 'q':
3459 case 'r':
3460 case 's':
3461 case 't':
3462 case 'u':
3463 case 'v':
3464 case 'w':
3465 case 'x':
3466 case 'y':
3467 case 'z':
3468 {
3469 s = bc_lex_identifier(l);
3470 break;
3471 }
3472
3473 case '{':
3474 case '}':
3475 {
3476 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3477 break;
3478 }
3479
3480 case '|':
3481 {
3482 c2 = l->buf[l->i];
3483
3484 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003485 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Gavin Howard01055ba2018-11-03 11:00:21 -06003486 if (s) return s;
3487
3488 ++l->i;
3489 l->t.t = BC_LEX_OP_BOOL_OR;
3490 }
3491 else {
3492 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003493 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003494 }
3495
3496 break;
3497 }
3498
3499 default:
3500 {
3501 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003502 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003503 break;
3504 }
3505 }
3506
3507 return s;
3508}
3509#endif // ENABLE_BC
3510
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003511#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003512static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003513{
Gavin Howard01055ba2018-11-03 11:00:21 -06003514 if (isspace(l->buf[l->i - 1])) {
3515 bc_lex_whitespace(l);
3516 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003517 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003518 RETURN_STATUS(bc_error("extended register"));
3519 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003520 }
3521 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003522 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003523 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003524 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003525 l->t.t = BC_LEX_NAME;
3526 }
3527
Denys Vlasenko29301232018-12-11 15:29:32 +01003528 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003529}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003530#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003531# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003532#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003533
Denys Vlasenko29301232018-12-11 15:29:32 +01003534static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003535{
3536 size_t depth = 1, nls = 0, i = l->i;
3537 char c;
3538
3539 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003540 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003541
3542 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3543
3544 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3545 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3546 nls += (c == '\n');
3547
3548 if (depth) bc_vec_push(&l->t.v, &c);
3549 }
3550
3551 if (c == '\0') {
3552 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003553 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003554 }
3555
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003556 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003557 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3558 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3559 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003560 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003561 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003562
3563 l->i = i;
3564 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003565 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003566
Denys Vlasenko29301232018-12-11 15:29:32 +01003567 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003568}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003569#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003570# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003571#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003572
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003573static FAST_FUNC BcStatus dc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003574{
3575 BcStatus s = BC_STATUS_SUCCESS;
3576 char c = l->buf[l->i++], c2;
3577 size_t i;
3578
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003579 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3580 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko29301232018-12-11 15:29:32 +01003581 return zdc_lex_register(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003582 }
3583
3584 if (c >= '%' && c <= '~' &&
3585 (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID)
3586 {
3587 return s;
3588 }
3589
3590 // This is the workhorse of the lexer.
3591 switch (c) {
3592
3593 case '\0':
3594 {
3595 l->t.t = BC_LEX_EOF;
3596 break;
3597 }
3598
3599 case '\n':
3600 case '\t':
3601 case '\v':
3602 case '\f':
3603 case '\r':
3604 case ' ':
3605 {
3606 l->newline = (c == '\n');
3607 bc_lex_whitespace(l);
3608 break;
3609 }
3610
3611 case '!':
3612 {
3613 c2 = l->buf[l->i];
3614
3615 if (c2 == '=')
3616 l->t.t = BC_LEX_OP_REL_NE;
3617 else if (c2 == '<')
3618 l->t.t = BC_LEX_OP_REL_LE;
3619 else if (c2 == '>')
3620 l->t.t = BC_LEX_OP_REL_GE;
3621 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003622 return bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003623
3624 ++l->i;
3625 break;
3626 }
3627
3628 case '#':
3629 {
3630 bc_lex_lineComment(l);
3631 break;
3632 }
3633
3634 case '.':
3635 {
3636 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003637 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003638 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003639 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003640 break;
3641 }
3642
3643 case '0':
3644 case '1':
3645 case '2':
3646 case '3':
3647 case '4':
3648 case '5':
3649 case '6':
3650 case '7':
3651 case '8':
3652 case '9':
3653 case 'A':
3654 case 'B':
3655 case 'C':
3656 case 'D':
3657 case 'E':
3658 case 'F':
3659 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003660 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003661 break;
3662 }
3663
3664 case '[':
3665 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003666 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003667 break;
3668 }
3669
3670 default:
3671 {
3672 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003673 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003674 break;
3675 }
3676 }
3677
3678 return s;
3679}
3680#endif // ENABLE_DC
3681
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003682static void bc_program_addFunc(char *name, size_t *idx);
3683
Gavin Howard01055ba2018-11-03 11:00:21 -06003684static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3685{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003686 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003687 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003688}
3689
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003690#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3691
Gavin Howard01055ba2018-11-03 11:00:21 -06003692static void bc_parse_pushName(BcParse *p, char *name)
3693{
3694 size_t i = 0, len = strlen(name);
3695
3696 for (; i < len; ++i) bc_parse_push(p, name[i]);
3697 bc_parse_push(p, BC_PARSE_STREND);
3698
3699 free(name);
3700}
3701
3702static void bc_parse_pushIndex(BcParse *p, size_t idx)
3703{
3704 unsigned char amt, i, nums[sizeof(size_t)];
3705
3706 for (amt = 0; idx; ++amt) {
3707 nums[amt] = (char) idx;
3708 idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT;
3709 }
3710
3711 bc_parse_push(p, amt);
3712 for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]);
3713}
3714
3715static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3716{
3717 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003718 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003719
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003720 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003721
3722 bc_parse_push(p, BC_INST_NUM);
3723 bc_parse_pushIndex(p, idx);
3724
3725 ++(*nexs);
3726 (*prev) = BC_INST_NUM;
3727}
3728
3729static BcStatus bc_parse_text(BcParse *p, const char *text)
3730{
3731 BcStatus s;
3732
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003733 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003734
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003735 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003736 p->l.t.t = BC_LEX_INVALID;
3737 s = p->parse(p);
3738 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003739 if (!BC_PARSE_CAN_EXEC(p))
3740 return bc_error("file is not executable");
Gavin Howard01055ba2018-11-03 11:00:21 -06003741 }
3742
3743 return bc_lex_text(&p->l, text);
3744}
3745
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003746// Called when parsing or execution detects a failure,
3747// resets execution structures.
3748static void bc_program_reset(void)
3749{
3750 BcFunc *f;
3751 BcInstPtr *ip;
3752
3753 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3754 bc_vec_pop_all(&G.prog.results);
3755
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003756 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003757 ip = bc_vec_top(&G.prog.stack);
3758 ip->idx = f->code.len;
3759}
3760
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003761#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003762 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003763
Denys Vlasenkod38af482018-12-04 19:11:02 +01003764// Called when bc/dc_parse_parse() detects a failure,
3765// resets parsing structures.
3766static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003767{
3768 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003769 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003770 bc_vec_pop_all(&p->func->code);
3771 bc_vec_pop_all(&p->func->autos);
3772 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003773
3774 bc_parse_updateFunc(p, BC_PROG_MAIN);
3775 }
3776
3777 p->l.i = p->l.len;
3778 p->l.t.t = BC_LEX_EOF;
3779 p->auto_part = (p->nbraces = 0);
3780
3781 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003782 bc_vec_pop_all(&p->exits);
3783 bc_vec_pop_all(&p->conds);
3784 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003785
Denys Vlasenkod38af482018-12-04 19:11:02 +01003786 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003787}
3788
3789static void bc_parse_free(BcParse *p)
3790{
3791 bc_vec_free(&p->flags);
3792 bc_vec_free(&p->exits);
3793 bc_vec_free(&p->conds);
3794 bc_vec_free(&p->ops);
3795 bc_lex_free(&p->l);
3796}
3797
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003798static void bc_parse_create(BcParse *p, size_t func,
Gavin Howard01055ba2018-11-03 11:00:21 -06003799 BcParseParse parse, BcLexNext next)
3800{
3801 memset(p, 0, sizeof(BcParse));
3802
3803 bc_lex_init(&p->l, next);
3804 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3805 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3806 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003807 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003808 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3809
3810 p->parse = parse;
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003811 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003812 bc_parse_updateFunc(p, func);
3813}
3814
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003815#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003816
3817#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3818#define BC_PARSE_LEAF(p, rparen) \
3819 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3820 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3821
3822// We can calculate the conversion between tokens and exprs by subtracting the
3823// position of the first operator in the lex enum and adding the position of the
3824// first in the expr enum. Note: This only works for binary operators.
3825#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3826
Gavin Howard01055ba2018-11-03 11:00:21 -06003827static BcStatus bc_parse_else(BcParse *p);
3828static BcStatus bc_parse_stmt(BcParse *p);
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003829static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003830static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Gavin Howard01055ba2018-11-03 11:00:21 -06003831
3832static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3833 size_t *nexprs, bool next)
3834{
3835 BcStatus s = BC_STATUS_SUCCESS;
3836 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003837 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3838 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003839
3840 while (p->ops.len > start) {
3841
3842 t = BC_PARSE_TOP_OP(p);
3843 if (t == BC_LEX_LPAREN) break;
3844
Denys Vlasenko65437582018-12-05 19:37:19 +01003845 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003846 if (l >= r && (l != r || !left)) break;
3847
3848 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3849 bc_vec_pop(&p->ops);
3850 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3851 }
3852
3853 bc_vec_push(&p->ops, &type);
3854 if (next) s = bc_lex_next(&p->l);
3855
3856 return s;
3857}
3858
3859static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
3860{
3861 BcLexType top;
3862
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003863 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003864 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003865 top = BC_PARSE_TOP_OP(p);
3866
3867 while (top != BC_LEX_LPAREN) {
3868
3869 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3870
3871 bc_vec_pop(&p->ops);
3872 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3873
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003874 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003875 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003876 top = BC_PARSE_TOP_OP(p);
3877 }
3878
3879 bc_vec_pop(&p->ops);
3880
3881 return bc_lex_next(&p->l);
3882}
3883
3884static BcStatus bc_parse_params(BcParse *p, uint8_t flags)
3885{
3886 BcStatus s;
3887 bool comma = false;
3888 size_t nparams;
3889
3890 s = bc_lex_next(&p->l);
3891 if (s) return s;
3892
3893 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
3894
3895 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3896 s = bc_parse_expr(p, flags, bc_parse_next_param);
3897 if (s) return s;
3898
3899 comma = p->l.t.t == BC_LEX_COMMA;
3900 if (comma) {
3901 s = bc_lex_next(&p->l);
3902 if (s) return s;
3903 }
3904 }
3905
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003906 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003907 bc_parse_push(p, BC_INST_CALL);
3908 bc_parse_pushIndex(p, nparams);
3909
3910 return BC_STATUS_SUCCESS;
3911}
3912
3913static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags)
3914{
3915 BcStatus s;
3916 BcId entry, *entry_ptr;
3917 size_t idx;
3918
3919 entry.name = name;
3920
3921 s = bc_parse_params(p, flags);
3922 if (s) goto err;
3923
3924 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003925 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003926 goto err;
3927 }
3928
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003929 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003930
3931 if (idx == BC_VEC_INVALID_IDX) {
3932 name = xstrdup(entry.name);
3933 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003934 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003935 free(entry.name);
3936 }
3937 else
3938 free(name);
3939
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003940 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003941 bc_parse_pushIndex(p, entry_ptr->idx);
3942
3943 return bc_lex_next(&p->l);
3944
3945err:
3946 free(name);
3947 return s;
3948}
3949
3950static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
3951{
3952 BcStatus s;
3953 char *name;
3954
3955 name = xstrdup(p->l.t.v.v);
3956 s = bc_lex_next(&p->l);
3957 if (s) goto err;
3958
3959 if (p->l.t.t == BC_LEX_LBRACKET) {
3960
3961 s = bc_lex_next(&p->l);
3962 if (s) goto err;
3963
3964 if (p->l.t.t == BC_LEX_RBRACKET) {
3965
3966 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003967 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003968 goto err;
3969 }
3970
3971 *type = BC_INST_ARRAY;
3972 }
3973 else {
3974
3975 *type = BC_INST_ARRAY_ELEM;
3976
3977 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3978 s = bc_parse_expr(p, flags, bc_parse_next_elem);
3979 if (s) goto err;
3980 }
3981
3982 s = bc_lex_next(&p->l);
3983 if (s) goto err;
3984 bc_parse_push(p, *type);
3985 bc_parse_pushName(p, name);
3986 }
3987 else if (p->l.t.t == BC_LEX_LPAREN) {
3988
3989 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003990 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003991 goto err;
3992 }
3993
3994 *type = BC_INST_CALL;
3995 s = bc_parse_call(p, name, flags);
3996 }
3997 else {
3998 *type = BC_INST_VAR;
3999 bc_parse_push(p, BC_INST_VAR);
4000 bc_parse_pushName(p, name);
4001 }
4002
4003 return s;
4004
4005err:
4006 free(name);
4007 return s;
4008}
4009
4010static BcStatus bc_parse_read(BcParse *p)
4011{
4012 BcStatus s;
4013
4014 s = bc_lex_next(&p->l);
4015 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004016 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004017
4018 s = bc_lex_next(&p->l);
4019 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004020 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004021
4022 bc_parse_push(p, BC_INST_READ);
4023
4024 return bc_lex_next(&p->l);
4025}
4026
4027static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
4028 BcInst *prev)
4029{
4030 BcStatus s;
4031
4032 s = bc_lex_next(&p->l);
4033 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004034 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004035
4036 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
4037
4038 s = bc_lex_next(&p->l);
4039 if (s) return s;
4040
4041 s = bc_parse_expr(p, flags, bc_parse_next_rel);
4042 if (s) return s;
4043
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004044 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004045
4046 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
4047 bc_parse_push(p, *prev);
4048
4049 return bc_lex_next(&p->l);
4050}
4051
4052static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
4053{
4054 BcStatus s;
4055
4056 s = bc_lex_next(&p->l);
4057 if (s) return s;
4058
4059 if (p->l.t.t != BC_LEX_LPAREN) {
4060 *type = BC_INST_SCALE;
4061 bc_parse_push(p, BC_INST_SCALE);
4062 return BC_STATUS_SUCCESS;
4063 }
4064
4065 *type = BC_INST_SCALE_FUNC;
4066 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
4067
4068 s = bc_lex_next(&p->l);
4069 if (s) return s;
4070
4071 s = bc_parse_expr(p, flags, bc_parse_next_rel);
4072 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004073 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004074 bc_parse_push(p, BC_INST_SCALE_FUNC);
4075
4076 return bc_lex_next(&p->l);
4077}
4078
4079static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
4080 size_t *nexprs, uint8_t flags)
4081{
4082 BcStatus s;
4083 BcLexType type;
4084 char inst;
4085 BcInst etype = *prev;
4086
4087 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
4088 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
4089 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
4090 {
4091 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
4092 bc_parse_push(p, inst);
4093 s = bc_lex_next(&p->l);
4094 }
4095 else {
4096
4097 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
4098 *paren_expr = true;
4099
4100 s = bc_lex_next(&p->l);
4101 if (s) return s;
4102 type = p->l.t.t;
4103
4104 // Because we parse the next part of the expression
4105 // right here, we need to increment this.
4106 *nexprs = *nexprs + 1;
4107
4108 switch (type) {
4109
4110 case BC_LEX_NAME:
4111 {
4112 s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
4113 break;
4114 }
4115
4116 case BC_LEX_KEY_IBASE:
4117 case BC_LEX_KEY_LAST:
4118 case BC_LEX_KEY_OBASE:
4119 {
4120 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4121 s = bc_lex_next(&p->l);
4122 break;
4123 }
4124
4125 case BC_LEX_KEY_SCALE:
4126 {
4127 s = bc_lex_next(&p->l);
4128 if (s) return s;
4129 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004130 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004131 else
4132 bc_parse_push(p, BC_INST_SCALE);
4133 break;
4134 }
4135
4136 default:
4137 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004138 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004139 break;
4140 }
4141 }
4142
4143 if (!s) bc_parse_push(p, inst);
4144 }
4145
4146 return s;
4147}
4148
4149static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
4150 bool rparen, size_t *nexprs)
4151{
4152 BcStatus s;
4153 BcLexType type;
4154 BcInst etype = *prev;
4155
4156 s = bc_lex_next(&p->l);
4157 if (s) return s;
4158
4159 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4160 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4161 BC_LEX_OP_MINUS :
4162 BC_LEX_NEG;
4163 *prev = BC_PARSE_TOKEN_INST(type);
4164
4165 // We can just push onto the op stack because this is the largest
4166 // precedence operator that gets pushed. Inc/dec does not.
4167 if (type != BC_LEX_OP_MINUS)
4168 bc_vec_push(&p->ops, &type);
4169 else
4170 s = bc_parse_operator(p, type, ops_bgn, nexprs, false);
4171
4172 return s;
4173}
4174
4175static BcStatus bc_parse_string(BcParse *p, char inst)
4176{
4177 char *str = xstrdup(p->l.t.v.v);
4178
4179 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004180 bc_parse_pushIndex(p, G.prog.strs.len);
4181 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004182 bc_parse_push(p, inst);
4183
4184 return bc_lex_next(&p->l);
4185}
4186
4187static BcStatus bc_parse_print(BcParse *p)
4188{
4189 BcStatus s;
4190 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004191 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06004192
4193 s = bc_lex_next(&p->l);
4194 if (s) return s;
4195
4196 type = p->l.t.t;
4197
4198 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004199 return bc_error("bad print statement");
Gavin Howard01055ba2018-11-03 11:00:21 -06004200
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004201 comma = false;
4202 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004203
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004204 if (type == BC_LEX_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004205 s = bc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004206 if (s) return s;
4207 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004208 s = bc_parse_expr(p, 0, bc_parse_next_print);
4209 if (s) return s;
4210 bc_parse_push(p, BC_INST_PRINT_POP);
4211 }
4212
Gavin Howard01055ba2018-11-03 11:00:21 -06004213 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004214 if (comma) {
4215 s = bc_lex_next(&p->l);
4216 if (s) return s;
4217 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004218 type = p->l.t.t;
4219 }
4220
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004221 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004222
4223 return bc_lex_next(&p->l);
4224}
4225
4226static BcStatus bc_parse_return(BcParse *p)
4227{
4228 BcStatus s;
4229 BcLexType t;
4230 bool paren;
4231
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004232 if (!BC_PARSE_FUNC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004233
4234 s = bc_lex_next(&p->l);
4235 if (s) return s;
4236
4237 t = p->l.t.t;
4238 paren = t == BC_LEX_LPAREN;
4239
4240 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4241 bc_parse_push(p, BC_INST_RET0);
4242 else {
4243
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004244 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004245 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004246 bc_parse_push(p, BC_INST_RET0);
4247 s = bc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004248 }
Denys Vlasenko452df922018-12-05 20:28:26 +01004249 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004250
4251 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004252 s = bc_POSIX_requires("parentheses around return expressions");
Gavin Howard01055ba2018-11-03 11:00:21 -06004253 if (s) return s;
4254 }
4255
4256 bc_parse_push(p, BC_INST_RET);
4257 }
4258
4259 return s;
4260}
4261
4262static BcStatus bc_parse_endBody(BcParse *p, bool brace)
4263{
4264 BcStatus s = BC_STATUS_SUCCESS;
4265
4266 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004267 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004268
4269 if (brace) {
4270
4271 if (p->l.t.t == BC_LEX_RBRACE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004272 if (!p->nbraces) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004273 --p->nbraces;
4274 s = bc_lex_next(&p->l);
4275 if (s) return s;
4276 }
4277 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004278 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004279 }
4280
4281 if (BC_PARSE_IF(p)) {
4282
4283 uint8_t *flag_ptr;
4284
4285 while (p->l.t.t == BC_LEX_NLINE) {
4286 s = bc_lex_next(&p->l);
4287 if (s) return s;
4288 }
4289
4290 bc_vec_pop(&p->flags);
4291
4292 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4293 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4294
4295 if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p);
4296 }
4297 else if (BC_PARSE_ELSE(p)) {
4298
4299 BcInstPtr *ip;
4300 size_t *label;
4301
4302 bc_vec_pop(&p->flags);
4303
4304 ip = bc_vec_top(&p->exits);
4305 label = bc_vec_item(&p->func->labels, ip->idx);
4306 *label = p->func->code.len;
4307
4308 bc_vec_pop(&p->exits);
4309 }
4310 else if (BC_PARSE_FUNC_INNER(p)) {
4311 bc_parse_push(p, BC_INST_RET0);
4312 bc_parse_updateFunc(p, BC_PROG_MAIN);
4313 bc_vec_pop(&p->flags);
4314 }
4315 else {
4316
4317 BcInstPtr *ip = bc_vec_top(&p->exits);
4318 size_t *label = bc_vec_top(&p->conds);
4319
4320 bc_parse_push(p, BC_INST_JUMP);
4321 bc_parse_pushIndex(p, *label);
4322
4323 label = bc_vec_item(&p->func->labels, ip->idx);
4324 *label = p->func->code.len;
4325
4326 bc_vec_pop(&p->flags);
4327 bc_vec_pop(&p->exits);
4328 bc_vec_pop(&p->conds);
4329 }
4330
4331 return s;
4332}
4333
4334static void bc_parse_startBody(BcParse *p, uint8_t flags)
4335{
4336 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4337 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4338 flags |= BC_PARSE_FLAG_BODY;
4339 bc_vec_push(&p->flags, &flags);
4340}
4341
4342static void bc_parse_noElse(BcParse *p)
4343{
4344 BcInstPtr *ip;
4345 size_t *label;
4346 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4347
4348 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4349
4350 ip = bc_vec_top(&p->exits);
4351 label = bc_vec_item(&p->func->labels, ip->idx);
4352 *label = p->func->code.len;
4353
4354 bc_vec_pop(&p->exits);
4355}
4356
4357static BcStatus bc_parse_if(BcParse *p)
4358{
4359 BcStatus s;
4360 BcInstPtr ip;
4361
4362 s = bc_lex_next(&p->l);
4363 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004364 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004365
4366 s = bc_lex_next(&p->l);
4367 if (s) return s;
4368 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4369 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004370 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004371
4372 s = bc_lex_next(&p->l);
4373 if (s) return s;
4374 bc_parse_push(p, BC_INST_JUMP_ZERO);
4375
4376 ip.idx = p->func->labels.len;
4377 ip.func = ip.len = 0;
4378
4379 bc_parse_pushIndex(p, ip.idx);
4380 bc_vec_push(&p->exits, &ip);
4381 bc_vec_push(&p->func->labels, &ip.idx);
4382 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4383
4384 return BC_STATUS_SUCCESS;
4385}
4386
4387static BcStatus bc_parse_else(BcParse *p)
4388{
4389 BcInstPtr ip;
4390
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004391 if (!BC_PARSE_IF_END(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004392
4393 ip.idx = p->func->labels.len;
4394 ip.func = ip.len = 0;
4395
4396 bc_parse_push(p, BC_INST_JUMP);
4397 bc_parse_pushIndex(p, ip.idx);
4398
4399 bc_parse_noElse(p);
4400
4401 bc_vec_push(&p->exits, &ip);
4402 bc_vec_push(&p->func->labels, &ip.idx);
4403 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4404
4405 return bc_lex_next(&p->l);
4406}
4407
4408static BcStatus bc_parse_while(BcParse *p)
4409{
4410 BcStatus s;
4411 BcInstPtr ip;
4412
4413 s = bc_lex_next(&p->l);
4414 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004415 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004416 s = bc_lex_next(&p->l);
4417 if (s) return s;
4418
4419 ip.idx = p->func->labels.len;
4420
4421 bc_vec_push(&p->func->labels, &p->func->code.len);
4422 bc_vec_push(&p->conds, &ip.idx);
4423
4424 ip.idx = p->func->labels.len;
4425 ip.func = 1;
4426 ip.len = 0;
4427
4428 bc_vec_push(&p->exits, &ip);
4429 bc_vec_push(&p->func->labels, &ip.idx);
4430
4431 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4432 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004433 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004434 s = bc_lex_next(&p->l);
4435 if (s) return s;
4436
4437 bc_parse_push(p, BC_INST_JUMP_ZERO);
4438 bc_parse_pushIndex(p, ip.idx);
4439 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4440
4441 return BC_STATUS_SUCCESS;
4442}
4443
4444static BcStatus bc_parse_for(BcParse *p)
4445{
4446 BcStatus s;
4447 BcInstPtr ip;
4448 size_t cond_idx, exit_idx, body_idx, update_idx;
4449
4450 s = bc_lex_next(&p->l);
4451 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004452 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004453 s = bc_lex_next(&p->l);
4454 if (s) return s;
4455
4456 if (p->l.t.t != BC_LEX_SCOLON)
4457 s = bc_parse_expr(p, 0, bc_parse_next_for);
4458 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004459 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004460
4461 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004462 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004463 s = bc_lex_next(&p->l);
4464 if (s) return s;
4465
4466 cond_idx = p->func->labels.len;
4467 update_idx = cond_idx + 1;
4468 body_idx = update_idx + 1;
4469 exit_idx = body_idx + 1;
4470
4471 bc_vec_push(&p->func->labels, &p->func->code.len);
4472
4473 if (p->l.t.t != BC_LEX_SCOLON)
4474 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
4475 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004476 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004477
4478 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004479 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004480
4481 s = bc_lex_next(&p->l);
4482 if (s) return s;
4483
4484 bc_parse_push(p, BC_INST_JUMP_ZERO);
4485 bc_parse_pushIndex(p, exit_idx);
4486 bc_parse_push(p, BC_INST_JUMP);
4487 bc_parse_pushIndex(p, body_idx);
4488
4489 ip.idx = p->func->labels.len;
4490
4491 bc_vec_push(&p->conds, &update_idx);
4492 bc_vec_push(&p->func->labels, &p->func->code.len);
4493
4494 if (p->l.t.t != BC_LEX_RPAREN)
4495 s = bc_parse_expr(p, 0, bc_parse_next_rel);
4496 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004497 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004498
4499 if (s) return s;
4500
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004501 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004502 bc_parse_push(p, BC_INST_JUMP);
4503 bc_parse_pushIndex(p, cond_idx);
4504 bc_vec_push(&p->func->labels, &p->func->code.len);
4505
4506 ip.idx = exit_idx;
4507 ip.func = 1;
4508 ip.len = 0;
4509
4510 bc_vec_push(&p->exits, &ip);
4511 bc_vec_push(&p->func->labels, &ip.idx);
4512 bc_lex_next(&p->l);
4513 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4514
4515 return BC_STATUS_SUCCESS;
4516}
4517
4518static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type)
4519{
4520 BcStatus s;
4521 size_t i;
4522 BcInstPtr *ip;
4523
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004524 if (!BC_PARSE_LOOP(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004525
4526 if (type == BC_LEX_KEY_BREAK) {
4527
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004528 if (p->exits.len == 0) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004529
4530 i = p->exits.len - 1;
4531 ip = bc_vec_item(&p->exits, i);
4532
4533 while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004534 if (i >= p->exits.len && !ip->func) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004535
4536 i = ip->idx;
4537 }
4538 else
4539 i = *((size_t *) bc_vec_top(&p->conds));
4540
4541 bc_parse_push(p, BC_INST_JUMP);
4542 bc_parse_pushIndex(p, i);
4543
4544 s = bc_lex_next(&p->l);
4545 if (s) return s;
4546
4547 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004548 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004549
4550 return bc_lex_next(&p->l);
4551}
4552
4553static BcStatus bc_parse_func(BcParse *p)
4554{
4555 BcStatus s;
4556 bool var, comma = false;
4557 uint8_t flags;
4558 char *name;
4559
4560 s = bc_lex_next(&p->l);
4561 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004562 if (p->l.t.t != BC_LEX_NAME)
4563 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004564
4565 name = xstrdup(p->l.t.v.v);
4566 bc_parse_addFunc(p, name, &p->fidx);
4567
4568 s = bc_lex_next(&p->l);
4569 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004570 if (p->l.t.t != BC_LEX_LPAREN)
4571 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004572 s = bc_lex_next(&p->l);
4573 if (s) return s;
4574
4575 while (p->l.t.t != BC_LEX_RPAREN) {
4576
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004577 if (p->l.t.t != BC_LEX_NAME)
4578 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004579
4580 ++p->func->nparams;
4581
4582 name = xstrdup(p->l.t.v.v);
4583 s = bc_lex_next(&p->l);
4584 if (s) goto err;
4585
4586 var = p->l.t.t != BC_LEX_LBRACKET;
4587
4588 if (!var) {
4589
4590 s = bc_lex_next(&p->l);
4591 if (s) goto err;
4592
4593 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004594 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004595 goto err;
4596 }
4597
4598 s = bc_lex_next(&p->l);
4599 if (s) goto err;
4600 }
4601
4602 comma = p->l.t.t == BC_LEX_COMMA;
4603 if (comma) {
4604 s = bc_lex_next(&p->l);
4605 if (s) goto err;
4606 }
4607
Denys Vlasenko29301232018-12-11 15:29:32 +01004608 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004609 if (s) goto err;
4610 }
4611
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004612 if (comma) return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004613
4614 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4615 bc_parse_startBody(p, flags);
4616
4617 s = bc_lex_next(&p->l);
4618 if (s) return s;
4619
4620 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004621 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004622
4623 return s;
4624
4625err:
4626 free(name);
4627 return s;
4628}
4629
4630static BcStatus bc_parse_auto(BcParse *p)
4631{
4632 BcStatus s;
4633 bool comma, var, one;
4634 char *name;
4635
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004636 if (!p->auto_part) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004637 s = bc_lex_next(&p->l);
4638 if (s) return s;
4639
4640 p->auto_part = comma = false;
4641 one = p->l.t.t == BC_LEX_NAME;
4642
4643 while (p->l.t.t == BC_LEX_NAME) {
4644
4645 name = xstrdup(p->l.t.v.v);
4646 s = bc_lex_next(&p->l);
4647 if (s) goto err;
4648
4649 var = p->l.t.t != BC_LEX_LBRACKET;
4650 if (!var) {
4651
4652 s = bc_lex_next(&p->l);
4653 if (s) goto err;
4654
4655 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004656 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004657 goto err;
4658 }
4659
4660 s = bc_lex_next(&p->l);
4661 if (s) goto err;
4662 }
4663
4664 comma = p->l.t.t == BC_LEX_COMMA;
4665 if (comma) {
4666 s = bc_lex_next(&p->l);
4667 if (s) goto err;
4668 }
4669
Denys Vlasenko29301232018-12-11 15:29:32 +01004670 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004671 if (s) goto err;
4672 }
4673
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004674 if (comma) return bc_error("bad function definition");
Denys Vlasenkoabbc4332018-12-03 21:46:41 +01004675 if (!one) return bc_error("no auto variable found");
Gavin Howard01055ba2018-11-03 11:00:21 -06004676
4677 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004678 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004679
4680 return bc_lex_next(&p->l);
4681
4682err:
4683 free(name);
4684 return s;
4685}
4686
4687static BcStatus bc_parse_body(BcParse *p, bool brace)
4688{
4689 BcStatus s = BC_STATUS_SUCCESS;
4690 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4691
4692 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4693
4694 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
4695
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004696 if (!brace) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004697 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4698
4699 if (!p->auto_part) {
4700 s = bc_parse_auto(p);
4701 if (s) return s;
4702 }
4703
4704 if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l);
4705 }
4706 else {
4707 s = bc_parse_stmt(p);
4708 if (!s && !brace) s = bc_parse_endBody(p, false);
4709 }
4710
4711 return s;
4712}
4713
4714static BcStatus bc_parse_stmt(BcParse *p)
4715{
4716 BcStatus s = BC_STATUS_SUCCESS;
4717
4718 switch (p->l.t.t) {
4719
4720 case BC_LEX_NLINE:
4721 {
4722 return bc_lex_next(&p->l);
4723 }
4724
4725 case BC_LEX_KEY_ELSE:
4726 {
4727 p->auto_part = false;
4728 break;
4729 }
4730
4731 case BC_LEX_LBRACE:
4732 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004733 if (!BC_PARSE_BODY(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004734
4735 ++p->nbraces;
4736 s = bc_lex_next(&p->l);
4737 if (s) return s;
4738
4739 return bc_parse_body(p, true);
4740 }
4741
4742 case BC_LEX_KEY_AUTO:
4743 {
4744 return bc_parse_auto(p);
4745 }
4746
4747 default:
4748 {
4749 p->auto_part = false;
4750
4751 if (BC_PARSE_IF_END(p)) {
4752 bc_parse_noElse(p);
4753 return BC_STATUS_SUCCESS;
4754 }
4755 else if (BC_PARSE_BODY(p))
4756 return bc_parse_body(p, false);
4757
4758 break;
4759 }
4760 }
4761
4762 switch (p->l.t.t) {
4763
4764 case BC_LEX_OP_INC:
4765 case BC_LEX_OP_DEC:
4766 case BC_LEX_OP_MINUS:
4767 case BC_LEX_OP_BOOL_NOT:
4768 case BC_LEX_LPAREN:
4769 case BC_LEX_NAME:
4770 case BC_LEX_NUMBER:
4771 case BC_LEX_KEY_IBASE:
4772 case BC_LEX_KEY_LAST:
4773 case BC_LEX_KEY_LENGTH:
4774 case BC_LEX_KEY_OBASE:
4775 case BC_LEX_KEY_READ:
4776 case BC_LEX_KEY_SCALE:
4777 case BC_LEX_KEY_SQRT:
4778 {
4779 s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
4780 break;
4781 }
4782
4783 case BC_LEX_KEY_ELSE:
4784 {
4785 s = bc_parse_else(p);
4786 break;
4787 }
4788
4789 case BC_LEX_SCOLON:
4790 {
4791 while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l);
4792 break;
4793 }
4794
4795 case BC_LEX_RBRACE:
4796 {
4797 s = bc_parse_endBody(p, true);
4798 break;
4799 }
4800
4801 case BC_LEX_STR:
4802 {
4803 s = bc_parse_string(p, BC_INST_PRINT_STR);
4804 break;
4805 }
4806
4807 case BC_LEX_KEY_BREAK:
4808 case BC_LEX_KEY_CONTINUE:
4809 {
4810 s = bc_parse_loopExit(p, p->l.t.t);
4811 break;
4812 }
4813
4814 case BC_LEX_KEY_FOR:
4815 {
4816 s = bc_parse_for(p);
4817 break;
4818 }
4819
4820 case BC_LEX_KEY_HALT:
4821 {
4822 bc_parse_push(p, BC_INST_HALT);
4823 s = bc_lex_next(&p->l);
4824 break;
4825 }
4826
4827 case BC_LEX_KEY_IF:
4828 {
4829 s = bc_parse_if(p);
4830 break;
4831 }
4832
4833 case BC_LEX_KEY_LIMITS:
4834 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004835 // "limits" is a compile-time command,
4836 // the output is produced at _parse time_.
Gavin Howard01055ba2018-11-03 11:00:21 -06004837 s = bc_lex_next(&p->l);
4838 if (s) return s;
Denys Vlasenko64074a12018-12-07 15:50:14 +01004839 printf(
4840 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4841 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4842 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4843 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4844 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4845 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4846 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4847 "Number of vars = "BC_MAX_VARS_STR "\n"
4848 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004849 break;
4850 }
4851
4852 case BC_LEX_KEY_PRINT:
4853 {
4854 s = bc_parse_print(p);
4855 break;
4856 }
4857
4858 case BC_LEX_KEY_QUIT:
4859 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004860 // "quit" is a compile-time command. For example,
4861 // "if (0 == 1) quit" terminates when parsing the statement,
4862 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004863 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004864 }
4865
4866 case BC_LEX_KEY_RETURN:
4867 {
4868 s = bc_parse_return(p);
4869 break;
4870 }
4871
4872 case BC_LEX_KEY_WHILE:
4873 {
4874 s = bc_parse_while(p);
4875 break;
4876 }
4877
4878 default:
4879 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004880 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004881 break;
4882 }
4883 }
4884
4885 return s;
4886}
4887
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01004888static FAST_FUNC BcStatus bc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004889{
4890 BcStatus s;
4891
4892 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004893 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 -06004894 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004895 if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004896 s = bc_parse_func(p);
4897 }
4898 else
4899 s = bc_parse_stmt(p);
4900
Denys Vlasenkod38af482018-12-04 19:11:02 +01004901 if (s || G_interrupt) {
4902 bc_parse_reset(p);
4903 s = BC_STATUS_FAILURE;
4904 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004905
4906 return s;
4907}
4908
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004909static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004910{
4911 BcStatus s = BC_STATUS_SUCCESS;
4912 BcInst prev = BC_INST_PRINT;
4913 BcLexType top, t = p->l.t.t;
4914 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004915 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004916 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4917
4918 paren_first = p->l.t.t == BC_LEX_LPAREN;
4919 nparens = nrelops = 0;
4920 paren_expr = rprn = done = get_token = assign = false;
4921 bin_last = true;
4922
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004923 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004924 switch (t) {
4925
4926 case BC_LEX_OP_INC:
4927 case BC_LEX_OP_DEC:
4928 {
4929 s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
4930 rprn = get_token = bin_last = false;
4931 break;
4932 }
4933
4934 case BC_LEX_OP_MINUS:
4935 {
4936 s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
4937 rprn = get_token = false;
4938 bin_last = prev == BC_INST_MINUS;
4939 break;
4940 }
4941
4942 case BC_LEX_OP_ASSIGN_POWER:
4943 case BC_LEX_OP_ASSIGN_MULTIPLY:
4944 case BC_LEX_OP_ASSIGN_DIVIDE:
4945 case BC_LEX_OP_ASSIGN_MODULUS:
4946 case BC_LEX_OP_ASSIGN_PLUS:
4947 case BC_LEX_OP_ASSIGN_MINUS:
4948 case BC_LEX_OP_ASSIGN:
4949 {
4950 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4951 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4952 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4953 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004954 s = bc_error("bad assignment:"
4955 " left side must be scale,"
4956 " ibase, obase, last, var,"
4957 " or array element"
4958 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004959 break;
4960 }
4961 }
4962 // Fallthrough.
4963 case BC_LEX_OP_POWER:
4964 case BC_LEX_OP_MULTIPLY:
4965 case BC_LEX_OP_DIVIDE:
4966 case BC_LEX_OP_MODULUS:
4967 case BC_LEX_OP_PLUS:
4968 case BC_LEX_OP_REL_EQ:
4969 case BC_LEX_OP_REL_LE:
4970 case BC_LEX_OP_REL_GE:
4971 case BC_LEX_OP_REL_NE:
4972 case BC_LEX_OP_REL_LT:
4973 case BC_LEX_OP_REL_GT:
4974 case BC_LEX_OP_BOOL_NOT:
4975 case BC_LEX_OP_BOOL_OR:
4976 case BC_LEX_OP_BOOL_AND:
4977 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004978 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4979 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4980 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004981 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004982 }
4983
4984 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4985 prev = BC_PARSE_TOKEN_INST(t);
4986 s = bc_parse_operator(p, t, ops_bgn, &nexprs, true);
4987 rprn = get_token = false;
4988 bin_last = t != BC_LEX_OP_BOOL_NOT;
4989
4990 break;
4991 }
4992
4993 case BC_LEX_LPAREN:
4994 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004995 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004996 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004997 ++nparens;
4998 paren_expr = rprn = bin_last = false;
4999 get_token = true;
5000 bc_vec_push(&p->ops, &t);
5001
5002 break;
5003 }
5004
5005 case BC_LEX_RPAREN:
5006 {
5007 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005008 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005009
5010 if (nparens == 0) {
5011 s = BC_STATUS_SUCCESS;
5012 done = true;
5013 get_token = false;
5014 break;
5015 }
5016 else if (!paren_expr)
5017 return BC_STATUS_PARSE_EMPTY_EXP;
5018
5019 --nparens;
5020 paren_expr = rprn = true;
5021 get_token = bin_last = false;
5022
5023 s = bc_parse_rightParen(p, ops_bgn, &nexprs);
5024
5025 break;
5026 }
5027
5028 case BC_LEX_NAME:
5029 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005030 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005031 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005032 paren_expr = true;
5033 rprn = get_token = bin_last = false;
5034 s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
5035 ++nexprs;
5036
5037 break;
5038 }
5039
5040 case BC_LEX_NUMBER:
5041 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005042 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005043 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005044 bc_parse_number(p, &prev, &nexprs);
5045 paren_expr = get_token = true;
5046 rprn = bin_last = false;
5047
5048 break;
5049 }
5050
5051 case BC_LEX_KEY_IBASE:
5052 case BC_LEX_KEY_LAST:
5053 case BC_LEX_KEY_OBASE:
5054 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005055 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005056 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005057 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
5058 bc_parse_push(p, (char) prev);
5059
5060 paren_expr = get_token = true;
5061 rprn = bin_last = false;
5062 ++nexprs;
5063
5064 break;
5065 }
5066
5067 case BC_LEX_KEY_LENGTH:
5068 case BC_LEX_KEY_SQRT:
5069 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005070 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005071 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005072 s = bc_parse_builtin(p, t, flags, &prev);
5073 paren_expr = true;
5074 rprn = get_token = bin_last = false;
5075 ++nexprs;
5076
5077 break;
5078 }
5079
5080 case BC_LEX_KEY_READ:
5081 {
5082 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005083 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005084 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005085 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005086 else
5087 s = bc_parse_read(p);
5088
5089 paren_expr = true;
5090 rprn = get_token = bin_last = false;
5091 ++nexprs;
5092 prev = BC_INST_READ;
5093
5094 break;
5095 }
5096
5097 case BC_LEX_KEY_SCALE:
5098 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005099 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005100 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005101 s = bc_parse_scale(p, &prev, flags);
5102 paren_expr = true;
5103 rprn = get_token = bin_last = false;
5104 ++nexprs;
5105 prev = BC_INST_SCALE;
5106
5107 break;
5108 }
5109
5110 default:
5111 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005112 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005113 break;
5114 }
5115 }
5116
5117 if (!s && get_token) s = bc_lex_next(&p->l);
5118 }
5119
5120 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01005121 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06005122
5123 while (p->ops.len > ops_bgn) {
5124
5125 top = BC_PARSE_TOP_OP(p);
5126 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
5127
5128 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005129 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005130
5131 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
5132
5133 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
5134 bc_vec_pop(&p->ops);
5135 }
5136
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005137 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005138 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005139
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005140 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
5141 for (;;) {
5142 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005143 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005144 if (next & 0x80) // last element?
5145 break;
5146 next >>= 8;
5147 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005148 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005149 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06005150
5151 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01005152 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Gavin Howard01055ba2018-11-03 11:00:21 -06005153 if (s) return s;
5154 }
5155 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01005156 s = bc_POSIX_requires("exactly one comparison operator per condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06005157 if (s) return s;
5158 }
5159
5160 if (flags & BC_PARSE_PRINT) {
5161 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
5162 bc_parse_push(p, BC_INST_POP);
5163 }
5164
5165 return s;
5166}
5167
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01005168static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
5169{
5170 BcStatus s;
5171
5172 s = bc_parse_expr_empty_ok(p, flags, next);
5173 if (s == BC_STATUS_PARSE_EMPTY_EXP)
5174 return bc_error("empty expression");
5175 return s;
5176}
5177
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005178static void bc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005179{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005180 bc_parse_create(p, func, bc_parse_parse, bc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005181}
5182
5183static BcStatus bc_parse_expression(BcParse *p, uint8_t flags)
5184{
5185 return bc_parse_expr(p, flags, bc_parse_next_read);
5186}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005187
Gavin Howard01055ba2018-11-03 11:00:21 -06005188#endif // ENABLE_BC
5189
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005190#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005191
5192#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
5193
Gavin Howard01055ba2018-11-03 11:00:21 -06005194static BcStatus dc_parse_register(BcParse *p)
5195{
5196 BcStatus s;
5197 char *name;
5198
5199 s = bc_lex_next(&p->l);
5200 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005201 if (p->l.t.t != BC_LEX_NAME) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005202
5203 name = xstrdup(p->l.t.v.v);
5204 bc_parse_pushName(p, name);
5205
5206 return s;
5207}
5208
5209static BcStatus dc_parse_string(BcParse *p)
5210{
5211 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005212 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005213
5214 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
5215 name = xstrdup(b);
5216
5217 str = xstrdup(p->l.t.v.v);
5218 bc_parse_push(p, BC_INST_STR);
5219 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005220 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005221 bc_parse_addFunc(p, name, &idx);
5222
5223 return bc_lex_next(&p->l);
5224}
5225
5226static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
5227{
5228 BcStatus s;
5229
5230 bc_parse_push(p, inst);
5231 if (name) {
5232 s = dc_parse_register(p);
5233 if (s) return s;
5234 }
5235
5236 if (store) {
5237 bc_parse_push(p, BC_INST_SWAP);
5238 bc_parse_push(p, BC_INST_ASSIGN);
5239 bc_parse_push(p, BC_INST_POP);
5240 }
5241
5242 return bc_lex_next(&p->l);
5243}
5244
5245static BcStatus dc_parse_cond(BcParse *p, uint8_t inst)
5246{
5247 BcStatus s;
5248
5249 bc_parse_push(p, inst);
5250 bc_parse_push(p, BC_INST_EXEC_COND);
5251
5252 s = dc_parse_register(p);
5253 if (s) return s;
5254
5255 s = bc_lex_next(&p->l);
5256 if (s) return s;
5257
5258 if (p->l.t.t == BC_LEX_ELSE) {
5259 s = dc_parse_register(p);
5260 if (s) return s;
5261 s = bc_lex_next(&p->l);
5262 }
5263 else
5264 bc_parse_push(p, BC_PARSE_STREND);
5265
5266 return s;
5267}
5268
5269static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
5270{
5271 BcStatus s = BC_STATUS_SUCCESS;
5272 BcInst prev;
5273 uint8_t inst;
5274 bool assign, get_token = false;
5275
5276 switch (t) {
5277
5278 case BC_LEX_OP_REL_EQ:
5279 case BC_LEX_OP_REL_LE:
5280 case BC_LEX_OP_REL_GE:
5281 case BC_LEX_OP_REL_NE:
5282 case BC_LEX_OP_REL_LT:
5283 case BC_LEX_OP_REL_GT:
5284 {
5285 s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
5286 break;
5287 }
5288
5289 case BC_LEX_SCOLON:
5290 case BC_LEX_COLON:
5291 {
5292 s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
5293 break;
5294 }
5295
5296 case BC_LEX_STR:
5297 {
5298 s = dc_parse_string(p);
5299 break;
5300 }
5301
5302 case BC_LEX_NEG:
5303 case BC_LEX_NUMBER:
5304 {
5305 if (t == BC_LEX_NEG) {
5306 s = bc_lex_next(&p->l);
5307 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005308 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005309 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005310 }
5311
5312 bc_parse_number(p, &prev, &p->nbraces);
5313
5314 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5315 get_token = true;
5316
5317 break;
5318 }
5319
5320 case BC_LEX_KEY_READ:
5321 {
5322 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005323 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005324 else
5325 bc_parse_push(p, BC_INST_READ);
5326 get_token = true;
5327 break;
5328 }
5329
5330 case BC_LEX_OP_ASSIGN:
5331 case BC_LEX_STORE_PUSH:
5332 {
5333 assign = t == BC_LEX_OP_ASSIGN;
5334 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
5335 s = dc_parse_mem(p, inst, true, assign);
5336 break;
5337 }
5338
5339 case BC_LEX_LOAD:
5340 case BC_LEX_LOAD_POP:
5341 {
5342 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
5343 s = dc_parse_mem(p, inst, true, false);
5344 break;
5345 }
5346
5347 case BC_LEX_STORE_IBASE:
5348 case BC_LEX_STORE_SCALE:
5349 case BC_LEX_STORE_OBASE:
5350 {
5351 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
5352 s = dc_parse_mem(p, inst, false, true);
5353 break;
5354 }
5355
5356 default:
5357 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005358 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005359 get_token = true;
5360 break;
5361 }
5362 }
5363
5364 if (!s && get_token) s = bc_lex_next(&p->l);
5365
5366 return s;
5367}
5368
5369static BcStatus dc_parse_expr(BcParse *p, uint8_t flags)
5370{
5371 BcStatus s = BC_STATUS_SUCCESS;
5372 BcInst inst;
5373 BcLexType t;
5374
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005375 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005376
5377 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
5378
5379 inst = dc_parse_insts[t];
5380
5381 if (inst != BC_INST_INVALID) {
5382 bc_parse_push(p, inst);
5383 s = bc_lex_next(&p->l);
5384 }
5385 else
5386 s = dc_parse_token(p, t, flags);
5387 }
5388
5389 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5390 bc_parse_push(p, BC_INST_POP_EXEC);
5391
5392 return s;
5393}
5394
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01005395static FAST_FUNC BcStatus dc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005396{
5397 BcStatus s;
5398
5399 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005400 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005401 else
5402 s = dc_parse_expr(p, 0);
5403
Denys Vlasenkod38af482018-12-04 19:11:02 +01005404 if (s || G_interrupt) {
5405 bc_parse_reset(p);
5406 s = BC_STATUS_FAILURE;
5407 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005408
5409 return s;
5410}
5411
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005412static void dc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005413{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005414 bc_parse_create(p, func, dc_parse_parse, dc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005415}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005416
Gavin Howard01055ba2018-11-03 11:00:21 -06005417#endif // ENABLE_DC
5418
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005419static void common_parse_init(BcParse *p, size_t func)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005420{
5421 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005422 IF_BC(bc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005423 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005424 IF_DC(dc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005425 }
5426}
5427
5428static BcStatus common_parse_expr(BcParse *p, uint8_t flags)
5429{
5430 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005431 IF_BC(return bc_parse_expression(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005432 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005433 IF_DC(return dc_parse_expr(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005434 }
5435}
5436
Denys Vlasenkodf515392018-12-02 19:27:48 +01005437static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005438{
Gavin Howard01055ba2018-11-03 11:00:21 -06005439 BcId e, *ptr;
5440 BcVec *v, *map;
5441 size_t i;
5442 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005443 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005444
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005445 v = var ? &G.prog.vars : &G.prog.arrs;
5446 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005447
5448 e.name = id;
5449 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005450 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005451
5452 if (new) {
5453 bc_array_init(&data.v, var);
5454 bc_vec_push(v, &data.v);
5455 }
5456
5457 ptr = bc_vec_item(map, i);
5458 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005459 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005460}
5461
Denys Vlasenko29301232018-12-11 15:29:32 +01005462static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005463{
Gavin Howard01055ba2018-11-03 11:00:21 -06005464 switch (r->t) {
5465
5466 case BC_RESULT_STR:
5467 case BC_RESULT_TEMP:
5468 case BC_RESULT_IBASE:
5469 case BC_RESULT_SCALE:
5470 case BC_RESULT_OBASE:
5471 {
5472 *num = &r->d.n;
5473 break;
5474 }
5475
5476 case BC_RESULT_CONSTANT:
5477 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005478 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005479 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005480 size_t base_t, len = strlen(*str);
5481 BcNum *base;
5482
5483 bc_num_init(&r->d.n, len);
5484
5485 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005486 base = hex ? &G.prog.hexb : &G.prog.ib;
5487 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005488 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005489
5490 if (s) {
5491 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005492 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005493 }
5494
5495 *num = &r->d.n;
5496 r->t = BC_RESULT_TEMP;
5497
5498 break;
5499 }
5500
5501 case BC_RESULT_VAR:
5502 case BC_RESULT_ARRAY:
5503 case BC_RESULT_ARRAY_ELEM:
5504 {
5505 BcVec *v;
5506
Denys Vlasenkodf515392018-12-02 19:27:48 +01005507 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005508
5509 if (r->t == BC_RESULT_ARRAY_ELEM) {
5510 v = bc_vec_top(v);
5511 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5512 *num = bc_vec_item(v, r->d.id.idx);
5513 }
5514 else
5515 *num = bc_vec_top(v);
5516
5517 break;
5518 }
5519
5520 case BC_RESULT_LAST:
5521 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005522 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005523 break;
5524 }
5525
5526 case BC_RESULT_ONE:
5527 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005528 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005529 break;
5530 }
5531 }
5532
Denys Vlasenko29301232018-12-11 15:29:32 +01005533 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005534}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005535#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005536# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005537#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005538
Denys Vlasenko29301232018-12-11 15:29:32 +01005539static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005540 BcResult **r, BcNum **rn, bool assign)
5541{
5542 BcStatus s;
5543 bool hex;
5544 BcResultType lt, rt;
5545
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005546 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005547 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005548
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005549 *r = bc_vec_item_rev(&G.prog.results, 0);
5550 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005551
5552 lt = (*l)->t;
5553 rt = (*r)->t;
5554 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5555
Denys Vlasenko29301232018-12-11 15:29:32 +01005556 s = zbc_program_num(*l, ln, false);
5557 if (s) RETURN_STATUS(s);
5558 s = zbc_program_num(*r, rn, hex);
5559 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005560
5561 // We run this again under these conditions in case any vector has been
5562 // reallocated out from under the BcNums or arrays we had.
5563 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005564 s = zbc_program_num(*l, ln, false);
5565 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005566 }
5567
5568 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005569 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005570 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005571 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005572
Denys Vlasenko29301232018-12-11 15:29:32 +01005573 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005574}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005575#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005576# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005577#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005578
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005579static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005580{
5581 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005582 bc_vec_pop(&G.prog.results);
5583 bc_vec_pop(&G.prog.results);
5584 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005585}
5586
Denys Vlasenko29301232018-12-11 15:29:32 +01005587static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005588{
5589 BcStatus s;
5590
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005591 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005592 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005593 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005594
Denys Vlasenko29301232018-12-11 15:29:32 +01005595 s = zbc_program_num(*r, n, false);
5596 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005597
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005598 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005599 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005600
Denys Vlasenko29301232018-12-11 15:29:32 +01005601 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005602}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005603#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005604# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005605#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005606
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005607static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005608{
5609 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005610 bc_vec_pop(&G.prog.results);
5611 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005612}
5613
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005614static BcStatus bc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005615{
5616 BcStatus s;
5617 BcResult *opd1, *opd2, res;
5618 BcNum *n1, *n2 = NULL;
5619
Denys Vlasenko29301232018-12-11 15:29:32 +01005620 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005621 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005622 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005623
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005624 s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005625 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005626 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005627
5628 return s;
5629
5630err:
5631 bc_num_free(&res.d.n);
5632 return s;
5633}
5634
Denys Vlasenko785e4b32018-12-02 17:18:52 +01005635static BcStatus bc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005636{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005637 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005638 BcStatus s;
5639 BcParse parse;
5640 BcVec buf;
5641 BcInstPtr ip;
5642 size_t i;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005643 BcFunc *f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005644
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005645 for (i = 0; i < G.prog.stack.len; ++i) {
5646 BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005647 if (ip_ptr->func == BC_PROG_READ)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005648 return bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005649 }
5650
Denys Vlasenko7d628012018-12-04 21:46:47 +01005651 bc_vec_pop_all(&f->code);
5652 bc_char_vec_init(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005653
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005654 sv_file = G.prog.file;
5655 G.prog.file = NULL;
5656
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01005657 s = bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005658 if (s) goto io_err;
5659
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005660 common_parse_init(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005661 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005662
5663 s = bc_parse_text(&parse, buf.v);
5664 if (s) goto exec_err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005665 s = common_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005666 if (s) goto exec_err;
5667
5668 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005669 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005670 goto exec_err;
5671 }
5672
5673 ip.func = BC_PROG_READ;
5674 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005675 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005676
5677 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005678 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005679
5680 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005681 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005682
5683exec_err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005684 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005685 bc_parse_free(&parse);
5686io_err:
5687 bc_vec_free(&buf);
5688 return s;
5689}
5690
5691static size_t bc_program_index(char *code, size_t *bgn)
5692{
5693 char amt = code[(*bgn)++], i = 0;
5694 size_t res = 0;
5695
5696 for (; i < amt; ++i, ++(*bgn))
5697 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5698
5699 return res;
5700}
5701
5702static char *bc_program_name(char *code, size_t *bgn)
5703{
5704 size_t i;
5705 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5706
5707 s = xmalloc(ptr - str + 1);
5708 c = code[(*bgn)++];
5709
5710 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5711 s[i] = c;
5712
5713 s[i] = '\0';
5714
5715 return s;
5716}
5717
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005718static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005719{
5720 size_t i, len = strlen(str);
5721
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005722#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005723 if (len == 0) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005724 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005725 return;
5726 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005727#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005728
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005729 for (i = 0; i < len; ++i, ++G.prog.nchars) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005730
5731 int c = str[i];
5732
5733 if (c != '\\' || i == len - 1)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005734 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005735 else {
5736
5737 c = str[++i];
5738
5739 switch (c) {
5740
5741 case 'a':
5742 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005743 bb_putchar('\a');
Gavin Howard01055ba2018-11-03 11:00:21 -06005744 break;
5745 }
5746
5747 case 'b':
5748 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005749 bb_putchar('\b');
Gavin Howard01055ba2018-11-03 11:00:21 -06005750 break;
5751 }
5752
5753 case '\\':
5754 case 'e':
5755 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005756 bb_putchar('\\');
Gavin Howard01055ba2018-11-03 11:00:21 -06005757 break;
5758 }
5759
5760 case 'f':
5761 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005762 bb_putchar('\f');
Gavin Howard01055ba2018-11-03 11:00:21 -06005763 break;
5764 }
5765
5766 case 'n':
5767 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005768 bb_putchar('\n');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005769 G.prog.nchars = SIZE_MAX;
Gavin Howard01055ba2018-11-03 11:00:21 -06005770 break;
5771 }
5772
5773 case 'r':
5774 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005775 bb_putchar('\r');
Gavin Howard01055ba2018-11-03 11:00:21 -06005776 break;
5777 }
5778
5779 case 'q':
5780 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005781 bb_putchar('"');
Gavin Howard01055ba2018-11-03 11:00:21 -06005782 break;
5783 }
5784
5785 case 't':
5786 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005787 bb_putchar('\t');
Gavin Howard01055ba2018-11-03 11:00:21 -06005788 break;
5789 }
5790
5791 default:
5792 {
5793 // Just print the backslash and following character.
Denys Vlasenko00d77792018-11-30 23:13:42 +01005794 bb_putchar('\\');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005795 ++G.prog.nchars;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005796 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005797 break;
5798 }
5799 }
5800 }
5801 }
5802}
5803
Denys Vlasenko29301232018-12-11 15:29:32 +01005804static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005805{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005806 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005807 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005808 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005809 bool pop = inst != BC_INST_PRINT;
5810
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005811 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005812 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005813
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005814 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005815 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005816 s = zbc_program_num(r, &num, false);
5817 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005818
5819 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005820 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005821 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005822 }
5823 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005824 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005825
5826 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005827 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005828
5829 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005830 for (;;) {
5831 char c = *str++;
5832 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005833 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005834 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005835 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005836 }
5837 }
5838 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005839 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005840 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005841 }
5842 }
5843
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005844 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005845
Denys Vlasenko29301232018-12-11 15:29:32 +01005846 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005847}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005848#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005849# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005850#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005851
Denys Vlasenko29301232018-12-11 15:29:32 +01005852static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005853{
5854 BcStatus s;
5855 BcResult res, *ptr;
5856 BcNum *num = NULL;
5857
Denys Vlasenko29301232018-12-11 15:29:32 +01005858 s = zbc_program_prep(&ptr, &num);
5859 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005860
5861 bc_num_init(&res.d.n, num->len);
5862 bc_num_copy(&res.d.n, num);
5863 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5864
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005865 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005866
Denys Vlasenko29301232018-12-11 15:29:32 +01005867 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005868}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005869#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005870# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005871#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005872
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005873static BcStatus bc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005874{
5875 BcStatus s;
5876 BcResult *opd1, *opd2, res;
5877 BcNum *n1, *n2;
5878 bool cond = 0;
5879 ssize_t cmp;
5880
Denys Vlasenko29301232018-12-11 15:29:32 +01005881 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005882 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005883 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005884
5885 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005886 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005887 else if (inst == BC_INST_BOOL_OR)
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 {
5890
5891 cmp = bc_num_cmp(n1, n2);
5892
5893 switch (inst) {
5894
5895 case BC_INST_REL_EQ:
5896 {
5897 cond = cmp == 0;
5898 break;
5899 }
5900
5901 case BC_INST_REL_LE:
5902 {
5903 cond = cmp <= 0;
5904 break;
5905 }
5906
5907 case BC_INST_REL_GE:
5908 {
5909 cond = cmp >= 0;
5910 break;
5911 }
5912
5913 case BC_INST_REL_NE:
5914 {
5915 cond = cmp != 0;
5916 break;
5917 }
5918
5919 case BC_INST_REL_LT:
5920 {
5921 cond = cmp < 0;
5922 break;
5923 }
5924
5925 case BC_INST_REL_GT:
5926 {
5927 cond = cmp > 0;
5928 break;
5929 }
5930 }
5931 }
5932
5933 (cond ? bc_num_one : bc_num_zero)(&res.d.n);
5934
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005935 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005936
5937 return s;
5938}
5939
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005940#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005941static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005942 bool push)
5943{
5944 BcNum n2;
5945 BcResult res;
5946
5947 memset(&n2, 0, sizeof(BcNum));
5948 n2.rdx = res.d.id.idx = r->d.id.idx;
5949 res.t = BC_RESULT_STR;
5950
5951 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005952 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005953 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005954 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005955 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005956 }
5957
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005958 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005959
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005960 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005961 bc_vec_push(v, &n2);
5962
Denys Vlasenko29301232018-12-11 15:29:32 +01005963 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005964}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005965#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005966# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005967#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005968#endif // ENABLE_DC
5969
Denys Vlasenko29301232018-12-11 15:29:32 +01005970static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005971{
5972 BcStatus s;
5973 BcResult *ptr, r;
5974 BcVec *v;
5975 BcNum *n;
5976
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005977 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005978 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005979
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005980 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005981 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005982 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005983 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005984
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005985#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005986 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005987 RETURN_STATUS(bc_error_variable_is_wrong_type());
5988 if (ptr->t == BC_RESULT_STR)
5989 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005990#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005991
Denys Vlasenko29301232018-12-11 15:29:32 +01005992 s = zbc_program_num(ptr, &n, false);
5993 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005994
5995 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005996 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005997
5998 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005999 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006000 bc_num_copy(&r.d.n, n);
6001 }
6002 else {
6003 bc_array_init(&r.d.v, true);
6004 bc_array_copy(&r.d.v, (BcVec *) n);
6005 }
6006
6007 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006008 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006009
Denys Vlasenko29301232018-12-11 15:29:32 +01006010 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006011}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006012#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006013# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006014#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006016static BcStatus bc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006017{
6018 BcStatus s;
6019 BcResult *left, *right, res;
6020 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006021 bool assign = inst == BC_INST_ASSIGN, ib, sc;
6022
Denys Vlasenko29301232018-12-11 15:29:32 +01006023 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06006024 if (s) return s;
6025
6026 ib = left->t == BC_RESULT_IBASE;
6027 sc = left->t == BC_RESULT_SCALE;
6028
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006029#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006030
6031 if (right->t == BC_RESULT_STR) {
6032
6033 BcVec *v;
6034
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006035 if (left->t != BC_RESULT_VAR)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006036 return bc_error_variable_is_wrong_type();
Denys Vlasenkodf515392018-12-02 19:27:48 +01006037 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006038
Denys Vlasenko29301232018-12-11 15:29:32 +01006039 return zbc_program_assignStr(right, v, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006040 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006041#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006042
6043 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006044 return bc_error("bad assignment:"
6045 " left side must be scale,"
6046 " ibase, obase, last, var,"
6047 " or array element"
6048 );
Gavin Howard01055ba2018-11-03 11:00:21 -06006049
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006050#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006051 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006052 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06006053
6054 if (assign)
6055 bc_num_copy(l, r);
6056 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006057 s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006058
6059 if (s) return s;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006060#else
Gavin Howard01055ba2018-11-03 11:00:21 -06006061 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006062#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006063
6064 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006065 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006066 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
6067 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
6068 NULL, //can't happen //BC_RESULT_LAST
6069 NULL, //can't happen //BC_RESULT_CONSTANT
6070 NULL, //can't happen //BC_RESULT_ONE
6071 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006072 };
Gavin Howard01055ba2018-11-03 11:00:21 -06006073 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01006074 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06006075
Denys Vlasenko29301232018-12-11 15:29:32 +01006076 s = zbc_num_ulong(l, &val);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006077 if (s)
6078 return s;
6079 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06006080 if (sc) {
6081 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006082 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006083 }
6084 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006085 if (val < BC_NUM_MIN_BASE)
6086 return bc_error(msg[s]);
Gavin Howard01055ba2018-11-03 11:00:21 -06006087 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006088 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006089 }
6090
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006091 if (val > max)
6092 return bc_error(msg[s]);
6093 if (!sc)
6094 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006095
6096 *ptr = (size_t) val;
6097 s = BC_STATUS_SUCCESS;
6098 }
6099
6100 bc_num_init(&res.d.n, l->len);
6101 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006102 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006103
6104 return s;
6105}
6106
Denys Vlasenko416ce762018-12-02 20:57:17 +01006107#if !ENABLE_DC
6108#define bc_program_pushVar(code, bgn, pop, copy) \
6109 bc_program_pushVar(code, bgn)
6110// for bc, 'pop' and 'copy' are always false
6111#endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006112static BcStatus bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006113 bool pop, bool copy)
6114{
6115 BcStatus s = BC_STATUS_SUCCESS;
6116 BcResult r;
6117 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006118
6119 r.t = BC_RESULT_VAR;
6120 r.d.id.name = name;
6121
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006122#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006123 {
6124 BcVec *v = bc_program_search(name, true);
6125 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006126
Denys Vlasenko416ce762018-12-02 20:57:17 +01006127 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006128
Denys Vlasenko416ce762018-12-02 20:57:17 +01006129 if (!BC_PROG_STACK(v, 2 - copy)) {
6130 free(name);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006131 return bc_error_stack_has_too_few_elements();
Denys Vlasenko416ce762018-12-02 20:57:17 +01006132 }
6133
Gavin Howard01055ba2018-11-03 11:00:21 -06006134 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006135 name = NULL;
6136
6137 if (!BC_PROG_STR(num)) {
6138
6139 r.t = BC_RESULT_TEMP;
6140
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006141 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006142 bc_num_copy(&r.d.n, num);
6143 }
6144 else {
6145 r.t = BC_RESULT_STR;
6146 r.d.id.idx = num->rdx;
6147 }
6148
6149 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006150 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006151 }
6152#endif // ENABLE_DC
6153
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006154 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006155
6156 return s;
6157}
6158
Denys Vlasenko29301232018-12-11 15:29:32 +01006159static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006160 char inst)
6161{
6162 BcStatus s = BC_STATUS_SUCCESS;
6163 BcResult r;
6164 BcNum *num;
6165
6166 r.d.id.name = bc_program_name(code, bgn);
6167
6168 if (inst == BC_INST_ARRAY) {
6169 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006170 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006171 }
6172 else {
6173
6174 BcResult *operand;
6175 unsigned long temp;
6176
Denys Vlasenko29301232018-12-11 15:29:32 +01006177 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006178 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006179 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006180 if (s) goto err;
6181
6182 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006183 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006184 goto err;
6185 }
6186
6187 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006188 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006189 }
6190
6191err:
6192 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006193 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006194}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006195#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006196# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006197#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006198
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006199#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006200static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006201{
6202 BcStatus s;
6203 BcResult *ptr, res, copy;
6204 BcNum *num = NULL;
6205 char inst2 = inst;
6206
Denys Vlasenko29301232018-12-11 15:29:32 +01006207 s = zbc_program_prep(&ptr, &num);
6208 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006209
6210 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6211 copy.t = BC_RESULT_TEMP;
6212 bc_num_init(&copy.d.n, num->len);
6213 bc_num_copy(&copy.d.n, num);
6214 }
6215
6216 res.t = BC_RESULT_ONE;
6217 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6218 BC_INST_ASSIGN_PLUS :
6219 BC_INST_ASSIGN_MINUS;
6220
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006221 bc_vec_push(&G.prog.results, &res);
6222 bc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006223
6224 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006225 bc_vec_pop(&G.prog.results);
6226 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006227 }
6228
Denys Vlasenko29301232018-12-11 15:29:32 +01006229 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006230}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006231#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006232# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006233#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006234
Denys Vlasenko29301232018-12-11 15:29:32 +01006235static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006236{
Gavin Howard01055ba2018-11-03 11:00:21 -06006237 BcInstPtr ip;
6238 size_t i, nparams = bc_program_index(code, idx);
6239 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006240 BcId *a;
6241 BcResultData param;
6242 BcResult *arg;
6243
6244 ip.idx = 0;
6245 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006246 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006247
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006248 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006249 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006250 }
6251 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006252 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006253 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006254 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006255
6256 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006257 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006258
6259 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006260 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006261
6262 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006263 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006264
Denys Vlasenko29301232018-12-11 15:29:32 +01006265 s = zbc_program_copyToVar(a->name, a->idx);
6266 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006267 }
6268
6269 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006270 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006271
6272 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006273 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006274
6275 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006276 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006277 bc_vec_push(v, &param.n);
6278 }
6279 else {
6280 bc_array_init(&param.v, true);
6281 bc_vec_push(v, &param.v);
6282 }
6283 }
6284
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006285 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006286
Denys Vlasenko29301232018-12-11 15:29:32 +01006287 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006288}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006289#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006290# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006291#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006292
Denys Vlasenko29301232018-12-11 15:29:32 +01006293static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006294{
Gavin Howard01055ba2018-11-03 11:00:21 -06006295 BcResult res;
6296 BcFunc *f;
6297 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006298 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006299
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006300 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006301 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006302
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006303 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006304 res.t = BC_RESULT_TEMP;
6305
6306 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006307 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006308 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006309 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006310
Denys Vlasenko29301232018-12-11 15:29:32 +01006311 s = zbc_program_num(operand, &num, false);
6312 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006313 bc_num_init(&res.d.n, num->len);
6314 bc_num_copy(&res.d.n, num);
6315 }
6316 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006317 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006318 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006319 }
6320
6321 // We need to pop arguments as well, so this takes that into account.
6322 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006323 BcVec *v;
6324 BcId *a = bc_vec_item(&f->autos, i);
6325
Denys Vlasenkodf515392018-12-02 19:27:48 +01006326 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006327 bc_vec_pop(v);
6328 }
6329
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006330 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6331 bc_vec_push(&G.prog.results, &res);
6332 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006333
Denys Vlasenko29301232018-12-11 15:29:32 +01006334 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006335}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006336#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006337# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006338#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006339#endif // ENABLE_BC
6340
6341static unsigned long bc_program_scale(BcNum *n)
6342{
6343 return (unsigned long) n->rdx;
6344}
6345
6346static unsigned long bc_program_len(BcNum *n)
6347{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006348 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006349
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006350 if (n->rdx != len) return len;
6351 for (;;) {
6352 if (len == 0) break;
6353 len--;
6354 if (n->num[len] != 0) break;
6355 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006356 return len;
6357}
6358
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006359static BcStatus bc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006360{
6361 BcStatus s;
6362 BcResult *opnd;
6363 BcNum *num = NULL;
6364 BcResult res;
6365 bool len = inst == BC_INST_LENGTH;
6366
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006367 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006368 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006369 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006370
Denys Vlasenko29301232018-12-11 15:29:32 +01006371 s = zbc_program_num(opnd, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006372 if (s) return s;
6373
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006374#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006375 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006376 return bc_error_variable_is_wrong_type();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006377#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006378
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006379 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006380
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006381 if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006382#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006383 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006384 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006385 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006386#endif
6387#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006388 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
6389
6390 char **str;
6391 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6392
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006393 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006394 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006395 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006396#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006397 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006398 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006399 }
6400
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006401 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006402
6403 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006404}
6405
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006406#if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006407static BcStatus bc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006408{
6409 BcStatus s;
6410 BcResult *opd1, *opd2, res, res2;
6411 BcNum *n1, *n2 = NULL;
6412
Denys Vlasenko29301232018-12-11 15:29:32 +01006413 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006414 if (s) return s;
6415
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006416 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006417 bc_num_init(&res2.d.n, n2->len);
6418
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006419 s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006420 if (s) goto err;
6421
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006422 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006423 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006424 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006425
6426 return s;
6427
6428err:
6429 bc_num_free(&res2.d.n);
6430 bc_num_free(&res.d.n);
6431 return s;
6432}
6433
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006434static BcStatus bc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006435{
6436 BcStatus s;
6437 BcResult *r1, *r2, *r3, res;
6438 BcNum *n1, *n2, *n3;
6439
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006440 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006441 return bc_error_stack_has_too_few_elements();
Denys Vlasenko29301232018-12-11 15:29:32 +01006442 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006443 if (s) return s;
6444
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006445 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006446 s = zbc_program_num(r1, &n1, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006447 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006448 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006449 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006450
6451 // Make sure that the values have their pointers updated, if necessary.
6452 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6453
6454 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006455 s = zbc_program_num(r2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006456 if (s) return s;
6457 }
6458
6459 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006460 s = zbc_program_num(r3, &n3, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006461 if (s) return s;
6462 }
6463 }
6464
6465 bc_num_init(&res.d.n, n3->len);
6466 s = bc_num_modexp(n1, n2, n3, &res.d.n);
6467 if (s) goto err;
6468
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006469 bc_vec_pop(&G.prog.results);
6470 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006471
6472 return s;
6473
6474err:
6475 bc_num_free(&res.d.n);
6476 return s;
6477}
6478
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006479static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006480{
Gavin Howard01055ba2018-11-03 11:00:21 -06006481 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006482 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006483
6484 res.t = BC_RESULT_TEMP;
6485
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006486 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006487 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006488 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006489}
6490
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006491static BcStatus bc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006492{
6493 BcStatus s;
6494 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006495 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006496 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006497 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006498 unsigned long val;
6499
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006500 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006501 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006502 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006503
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006504 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006505 s = zbc_program_num(r, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006506 if (s) return s;
6507
6508 if (BC_PROG_NUM(r, num)) {
6509
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006510 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006511 bc_num_copy(&n, num);
6512 bc_num_truncate(&n, n.rdx);
6513
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006514 s = bc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006515 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006516 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006517 if (s) goto num_err;
6518
6519 c = (char) val;
6520
6521 bc_num_free(&n);
6522 }
6523 else {
6524 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006525 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006526 c = str2[0];
6527 }
6528
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006529 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006530 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006531 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006532
6533 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006534 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006535
6536 if (idx != len + BC_PROG_REQ_FUNCS) {
6537
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006538 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006539 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006540 len = idx;
6541 break;
6542 }
6543 }
6544
6545 free(str);
6546 }
6547 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006548 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006549
6550 res.t = BC_RESULT_STR;
6551 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006552 bc_vec_pop(&G.prog.results);
6553 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006554
6555 return BC_STATUS_SUCCESS;
6556
6557num_err:
6558 bc_num_free(&n);
6559 return s;
6560}
6561
Denys Vlasenko29301232018-12-11 15:29:32 +01006562static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006563{
6564 BcStatus s;
6565 BcResult *r;
6566 BcNum *n = NULL;
6567 size_t idx;
6568 char *str;
6569
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006570 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006571 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006572 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006573
Denys Vlasenko29301232018-12-11 15:29:32 +01006574 s = zbc_program_num(r, &n, false);
6575 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006576
6577 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006578 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006579 else {
6580 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006581 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006582 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006583 }
6584
Denys Vlasenko29301232018-12-11 15:29:32 +01006585 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006586}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006587#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006588# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006589#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006590
Denys Vlasenko29301232018-12-11 15:29:32 +01006591static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006592{
6593 BcStatus s;
6594 BcResult *opnd;
6595 BcNum *num = NULL;
6596 unsigned long val;
6597
Denys Vlasenko29301232018-12-11 15:29:32 +01006598 s = zbc_program_prep(&opnd, &num);
6599 if (s) RETURN_STATUS(s);
6600 s = zbc_num_ulong(num, &val);
6601 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006602
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006603 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006604
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006605 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006606 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006607 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006608 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006609 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006610
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006611 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006612
Denys Vlasenko29301232018-12-11 15:29:32 +01006613 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006614}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006615#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006616# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006617#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006618
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006619static BcStatus bc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006620 bool cond)
6621{
6622 BcStatus s = BC_STATUS_SUCCESS;
6623 BcResult *r;
6624 char **str;
6625 BcFunc *f;
6626 BcParse prs;
6627 BcInstPtr ip;
6628 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006629
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006630 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006631 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006632
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006633 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006634
6635 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006636 BcNum *n = n; // for compiler
6637 bool exec;
6638 char *name;
6639 char *then_name = bc_program_name(code, bgn);
6640 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006641
6642 if (code[*bgn] == BC_PARSE_STREND)
6643 (*bgn) += 1;
6644 else
6645 else_name = bc_program_name(code, bgn);
6646
6647 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006648 name = then_name;
6649 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006650 exec = true;
6651 name = else_name;
6652 }
6653
6654 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006655 BcVec *v;
6656 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006657 n = bc_vec_top(v);
6658 }
6659
6660 free(then_name);
6661 free(else_name);
6662
6663 if (!exec) goto exit;
6664 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006665 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006666 goto exit;
6667 }
6668
6669 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006670 } else {
6671 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006672 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006673 } else if (r->t == BC_RESULT_VAR) {
6674 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006675 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006676 if (s || !BC_PROG_STR(n)) goto exit;
6677 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006678 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006679 goto exit;
6680 }
6681
6682 fidx = sidx + BC_PROG_REQ_FUNCS;
6683
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006684 str = bc_program_str(sidx);
6685 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006686
6687 if (f->code.len == 0) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006688 common_parse_init(&prs, fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006689 s = bc_parse_text(&prs, *str);
6690 if (s) goto err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01006691 s = common_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006692 if (s) goto err;
6693
6694 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006695 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006696 goto err;
6697 }
6698
6699 bc_parse_free(&prs);
6700 }
6701
6702 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006703 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006704 ip.func = fidx;
6705
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006706 bc_vec_pop(&G.prog.results);
6707 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006708
6709 return BC_STATUS_SUCCESS;
6710
6711err:
6712 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006713 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006714 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006715exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006716 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006717 return s;
6718}
6719#endif // ENABLE_DC
6720
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006721static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006722{
Gavin Howard01055ba2018-11-03 11:00:21 -06006723 BcResult res;
6724 unsigned long val;
6725
6726 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6727 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006728 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006729 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006730 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006731 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006732 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006733
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006734 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006735 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006736 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006737}
6738
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006739static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006740{
Gavin Howard01055ba2018-11-03 11:00:21 -06006741 BcId entry, *entry_ptr;
6742 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006743 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006744
6745 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006746 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006747
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006748 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6749 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006750
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006751 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006752 *idx = entry_ptr->idx;
6753
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006754 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006755
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006756 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006757
6758 // We need to reset these, so the function can be repopulated.
6759 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006760 bc_vec_pop_all(&func->autos);
6761 bc_vec_pop_all(&func->code);
6762 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006763 }
6764 else {
6765 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006766 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006767 }
6768}
6769
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006770static BcStatus bc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006771{
Gavin Howard01055ba2018-11-03 11:00:21 -06006772 BcResult r, *ptr;
6773 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006774 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006775 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006776 char *code = func->code.v;
6777 bool cond = false;
6778
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006779 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006780 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006781 char inst = code[(ip->idx)++];
6782
6783 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006784#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006785 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006786 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006787 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006788 cond = !bc_num_cmp(num, &G.prog.zero);
6789 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006790 // Fallthrough.
6791 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006792 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006793 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006794 addr = bc_vec_item(&func->labels, idx);
6795 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6796 break;
6797 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006798 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006799 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006800 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006801 case BC_INST_INC_PRE:
6802 case BC_INST_DEC_PRE:
6803 case BC_INST_INC_POST:
6804 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006805 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006806 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006807 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006808 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006809 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006810 case BC_INST_RET:
6811 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006812 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006813 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006814 case BC_INST_BOOL_OR:
6815 case BC_INST_BOOL_AND:
6816#endif // ENABLE_BC
6817 case BC_INST_REL_EQ:
6818 case BC_INST_REL_LE:
6819 case BC_INST_REL_GE:
6820 case BC_INST_REL_NE:
6821 case BC_INST_REL_LT:
6822 case BC_INST_REL_GT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006823 s = bc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006824 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006825 case BC_INST_READ:
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006826 s = bc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006827 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006828 case BC_INST_VAR:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006829 s = bc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006830 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006831 case BC_INST_ARRAY_ELEM:
6832 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006833 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006834 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006835 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006836 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006837 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006838 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006839 case BC_INST_IBASE:
6840 case BC_INST_SCALE:
6841 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006842 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006843 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006844 case BC_INST_SCALE_FUNC:
6845 case BC_INST_LENGTH:
6846 case BC_INST_SQRT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006847 s = bc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006848 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006849 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006850 r.t = BC_RESULT_CONSTANT;
6851 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006852 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006853 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006854 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006855 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006856 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006857 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006858 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006859 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006860 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006861 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006862 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006863 case BC_INST_PRINT:
6864 case BC_INST_PRINT_POP:
6865 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006866 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006867 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006868 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006869 r.t = BC_RESULT_STR;
6870 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006871 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006872 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006873 case BC_INST_POWER:
6874 case BC_INST_MULTIPLY:
6875 case BC_INST_DIVIDE:
6876 case BC_INST_MODULUS:
6877 case BC_INST_PLUS:
6878 case BC_INST_MINUS:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006879 s = bc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006880 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006881 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006882 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006883 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006884 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006885 if (!bc_num_cmp(num, &G.prog.zero))
6886 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006887 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006888 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006889 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006890 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006891 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006892 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006893#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006894 case BC_INST_ASSIGN_POWER:
6895 case BC_INST_ASSIGN_MULTIPLY:
6896 case BC_INST_ASSIGN_DIVIDE:
6897 case BC_INST_ASSIGN_MODULUS:
6898 case BC_INST_ASSIGN_PLUS:
6899 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006900#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006901 case BC_INST_ASSIGN:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006902 s = bc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006903 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006904#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006905 case BC_INST_MODEXP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006906 s = bc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006907 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006908 case BC_INST_DIVMOD:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006909 s = bc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006910 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006911 case BC_INST_EXECUTE:
6912 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006913 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006914 s = bc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006915 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006916 case BC_INST_PRINT_STACK: {
6917 size_t idx;
6918 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006919 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006920 if (s) break;
6921 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006922 break;
6923 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006924 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006925 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006926 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006927 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006928 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006929 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006930 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006931 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006932 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006933 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006934 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006935 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006936 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006937 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006938 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006939 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006940 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006941 ptr = bc_vec_item_rev(&G.prog.results, 0);
6942 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006943 memcpy(&r, ptr, sizeof(BcResult));
6944 memcpy(ptr, ptr2, sizeof(BcResult));
6945 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006946 break;
6947 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006948 case BC_INST_ASCIIFY:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006949 s = bc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006950 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006951 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006952 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006953 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006954 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006955 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006956 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006957 s = bc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006958 break;
6959 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006960 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006961 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006962 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006963 free(name);
6964 break;
6965 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006966 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006967 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006968 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006969 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006970 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006971 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006972 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006973 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006974#endif // ENABLE_DC
6975 }
6976
Denys Vlasenkod38af482018-12-04 19:11:02 +01006977 if (s || G_interrupt) {
6978 bc_program_reset();
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006979 return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01006980 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006981
6982 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006983 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006984 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006985 code = func->code.v;
6986 }
6987
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006988 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006989}
6990
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006991#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01006992static void bc_vm_info(void)
6993{
6994 printf("%s "BB_VER"\n"
6995 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01006996 , applet_name);
6997}
6998
6999static void bc_args(char **argv)
7000{
7001 unsigned opts;
7002 int i;
7003
7004 GETOPT_RESET();
7005#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007006 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01007007 "warn\0" No_argument "w"
7008 "version\0" No_argument "v"
7009 "standard\0" No_argument "s"
7010 "quiet\0" No_argument "q"
7011 "mathlib\0" No_argument "l"
7012 "interactive\0" No_argument "i"
7013 );
7014#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007015 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01007016#endif
7017 if (getenv("POSIXLY_CORRECT"))
7018 option_mask32 |= BC_FLAG_S;
7019
Denys Vlasenko54214c32018-12-06 09:07:06 +01007020 if (opts & BC_FLAG_V) {
7021 bc_vm_info();
7022 exit(0);
7023 }
7024
7025 for (i = optind; argv[i]; ++i)
7026 bc_vec_push(&G.files, argv + i);
7027}
7028
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007029static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007030{
Gavin Howard01055ba2018-11-03 11:00:21 -06007031 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007032 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01007033 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06007034
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007035 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06007036
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007037 G.env_args = xstrdup(env_args);
7038 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06007039
7040 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06007041
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007042 while (*(buf = skip_whitespace(buf)) != '\0') {
7043 bc_vec_push(&v, &buf);
7044 buf = skip_non_whitespace(buf);
7045 if (!*buf)
7046 break;
7047 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06007048 }
7049
Denys Vlasenko54214c32018-12-06 09:07:06 +01007050 // NULL terminate, and pass argv[] so that first arg is argv[1]
7051 if (sizeof(int) == sizeof(char*)) {
7052 bc_vec_push(&v, &const_int_0);
7053 } else {
7054 static char *const nullptr = NULL;
7055 bc_vec_push(&v, &nullptr);
7056 }
7057 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06007058
7059 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06007060}
7061#endif // ENABLE_BC
7062
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007063static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06007064{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007065 char *lenv;
7066 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06007067
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007068 lenv = getenv(var);
7069 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06007070 if (!lenv) return len;
7071
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007072 len = bb_strtou(lenv, NULL, 10) - 1;
7073 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06007074 len = BC_NUM_PRINT_WIDTH;
7075
7076 return len;
7077}
7078
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007079static BcStatus bc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06007080{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007081 BcStatus s = bc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06007082
Gavin Howard01055ba2018-11-03 11:00:21 -06007083 if (s) return s;
7084
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007085 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007086 s = G.prs.parse(&G.prs);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01007087 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007088 }
7089
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007090 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007091 s = bc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01007092 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007093 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01007094 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06007095 }
7096
7097 return s;
7098}
7099
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007100static BcStatus bc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007101{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007102 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007103 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007104 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007105 BcFunc *main_func;
7106 BcInstPtr *ip;
7107
Denys Vlasenkodf515392018-12-02 19:27:48 +01007108 data = bc_read_file(file);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007109 if (!data) return bc_error_fmt("file '%s' is not text", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007110
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007111 sv_file = G.prog.file;
7112 G.prog.file = file;
7113 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007114 s = bc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007115 if (s) goto err;
7116
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007117 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007118 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007119
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007120 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007121 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007122
7123err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007124 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007125 free(data);
7126 return s;
7127}
7128
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007129static BcStatus bc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007130{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007131 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007132 BcVec buf, buffer;
Gavin Howard01055ba2018-11-03 11:00:21 -06007133 size_t len, i, str = 0;
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007134 bool comment = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06007135
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007136 G.prog.file = NULL;
7137 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007138
Denys Vlasenko7d628012018-12-04 21:46:47 +01007139 bc_char_vec_init(&buffer);
7140 bc_char_vec_init(&buf);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01007141 bc_vec_pushZeroByte(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007142
7143 // This loop is complex because the vm tries not to send any lines that end
7144 // with a backslash to the parser. The reason for that is because the parser
7145 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7146 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007147 while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007148
7149 char *string = buf.v;
7150
7151 len = buf.len - 1;
7152
7153 if (len == 1) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007154 if (str && buf.v[0] == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007155 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007156 else if (buf.v[0] == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007157 str += 1;
7158 }
7159 else if (len > 1 || comment) {
7160
7161 for (i = 0; i < len; ++i) {
7162
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007163 bool notend = len > i + 1;
7164 char c = string[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06007165
7166 if (i - 1 > len || string[i - 1] != '\\') {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007167 if (G.sbgn == G.send)
7168 str ^= c == G.sbgn;
7169 else if (c == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007170 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007171 else if (c == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007172 str += 1;
7173 }
7174
7175 if (c == '/' && notend && !comment && string[i + 1] == '*') {
7176 comment = true;
7177 break;
7178 }
7179 else if (c == '*' && notend && comment && string[i + 1] == '/')
7180 comment = false;
7181 }
7182
7183 if (str || comment || string[len - 2] == '\\') {
7184 bc_vec_concat(&buffer, buf.v);
7185 continue;
7186 }
7187 }
7188
7189 bc_vec_concat(&buffer, buf.v);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007190 s = bc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007191 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007192 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007193 // Debug config, non-interactive mode:
7194 // return all the way back to main.
7195 // Non-debug builds do not come here, they exit.
7196 break;
7197 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007198 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007199
Denys Vlasenko7d628012018-12-04 21:46:47 +01007200 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007201 }
Denys Vlasenkof522dd92018-12-07 16:35:43 +01007202 if (s == BC_STATUS_EOF) // input EOF (^D) is not an error
7203 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007204
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007205 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007206 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007207 }
7208 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007209 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007210 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007211
Gavin Howard01055ba2018-11-03 11:00:21 -06007212 bc_vec_free(&buf);
7213 bc_vec_free(&buffer);
7214 return s;
7215}
7216
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007217#if ENABLE_BC
7218static const char bc_lib[] = {
7219 "scale=20"
7220"\n" "define e(x){"
7221"\n" "auto b,s,n,r,d,i,p,f,v"
7222"\n" "b=ibase"
7223"\n" "ibase=A"
7224"\n" "if(x<0){"
7225"\n" "n=1"
7226"\n" "x=-x"
7227"\n" "}"
7228"\n" "s=scale"
7229"\n" "r=6+s+0.44*x"
7230"\n" "scale=scale(x)+1"
7231"\n" "while(x>1){"
7232"\n" "d+=1"
7233"\n" "x/=2"
7234"\n" "scale+=1"
7235"\n" "}"
7236"\n" "scale=r"
7237"\n" "r=x+1"
7238"\n" "p=x"
7239"\n" "f=v=1"
7240"\n" "for(i=2;v!=0;++i){"
7241"\n" "p*=x"
7242"\n" "f*=i"
7243"\n" "v=p/f"
7244"\n" "r+=v"
7245"\n" "}"
7246"\n" "while((d--)!=0)r*=r"
7247"\n" "scale=s"
7248"\n" "ibase=b"
7249"\n" "if(n!=0)return(1/r)"
7250"\n" "return(r/1)"
7251"\n" "}"
7252"\n" "define l(x){"
7253"\n" "auto b,s,r,p,a,q,i,v"
7254"\n" "b=ibase"
7255"\n" "ibase=A"
7256"\n" "if(x<=0){"
7257"\n" "r=(1-10^scale)/1"
7258"\n" "ibase=b"
7259"\n" "return(r)"
7260"\n" "}"
7261"\n" "s=scale"
7262"\n" "scale+=6"
7263"\n" "p=2"
7264"\n" "while(x>=2){"
7265"\n" "p*=2"
7266"\n" "x=sqrt(x)"
7267"\n" "}"
7268"\n" "while(x<=0.5){"
7269"\n" "p*=2"
7270"\n" "x=sqrt(x)"
7271"\n" "}"
7272"\n" "r=a=(x-1)/(x+1)"
7273"\n" "q=a*a"
7274"\n" "v=1"
7275"\n" "for(i=3;v!=0;i+=2){"
7276"\n" "a*=q"
7277"\n" "v=a/i"
7278"\n" "r+=v"
7279"\n" "}"
7280"\n" "r*=p"
7281"\n" "scale=s"
7282"\n" "ibase=b"
7283"\n" "return(r/1)"
7284"\n" "}"
7285"\n" "define s(x){"
7286"\n" "auto b,s,r,n,a,q,i"
7287"\n" "b=ibase"
7288"\n" "ibase=A"
7289"\n" "s=scale"
7290"\n" "scale=1.1*s+2"
7291"\n" "a=a(1)"
7292"\n" "if(x<0){"
7293"\n" "n=1"
7294"\n" "x=-x"
7295"\n" "}"
7296"\n" "scale=0"
7297"\n" "q=(x/a+2)/4"
7298"\n" "x=x-4*q*a"
7299"\n" "if(q%2!=0)x=-x"
7300"\n" "scale=s+2"
7301"\n" "r=a=x"
7302"\n" "q=-x*x"
7303"\n" "for(i=3;a!=0;i+=2){"
7304"\n" "a*=q/(i*(i-1))"
7305"\n" "r+=a"
7306"\n" "}"
7307"\n" "scale=s"
7308"\n" "ibase=b"
7309"\n" "if(n!=0)return(-r/1)"
7310"\n" "return(r/1)"
7311"\n" "}"
7312"\n" "define c(x){"
7313"\n" "auto b,s"
7314"\n" "b=ibase"
7315"\n" "ibase=A"
7316"\n" "s=scale"
7317"\n" "scale*=1.2"
7318"\n" "x=s(2*a(1)+x)"
7319"\n" "scale=s"
7320"\n" "ibase=b"
7321"\n" "return(x/1)"
7322"\n" "}"
7323"\n" "define a(x){"
7324"\n" "auto b,s,r,n,a,m,t,f,i,u"
7325"\n" "b=ibase"
7326"\n" "ibase=A"
7327"\n" "n=1"
7328"\n" "if(x<0){"
7329"\n" "n=-1"
7330"\n" "x=-x"
7331"\n" "}"
7332"\n" "if(x==1){"
7333"\n" "if(scale<65){"
7334"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7335"\n" "}"
7336"\n" "}"
7337"\n" "if(x==.2){"
7338"\n" "if(scale<65){"
7339"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7340"\n" "}"
7341"\n" "}"
7342"\n" "s=scale"
7343"\n" "if(x>.2){"
7344"\n" "scale+=5"
7345"\n" "a=a(.2)"
7346"\n" "}"
7347"\n" "scale=s+3"
7348"\n" "while(x>.2){"
7349"\n" "m+=1"
7350"\n" "x=(x-.2)/(1+.2*x)"
7351"\n" "}"
7352"\n" "r=u=x"
7353"\n" "f=-x*x"
7354"\n" "t=1"
7355"\n" "for(i=3;t!=0;i+=2){"
7356"\n" "u*=f"
7357"\n" "t=u/i"
7358"\n" "r+=t"
7359"\n" "}"
7360"\n" "scale=s"
7361"\n" "ibase=b"
7362"\n" "return((m*a+r)/n)"
7363"\n" "}"
7364"\n" "define j(n,x){"
7365"\n" "auto b,s,o,a,i,v,f"
7366"\n" "b=ibase"
7367"\n" "ibase=A"
7368"\n" "s=scale"
7369"\n" "scale=0"
7370"\n" "n/=1"
7371"\n" "if(n<0){"
7372"\n" "n=-n"
7373"\n" "if(n%2==1)o=1"
7374"\n" "}"
7375"\n" "a=1"
7376"\n" "for(i=2;i<=n;++i)a*=i"
7377"\n" "scale=1.5*s"
7378"\n" "a=(x^n)/2^n/a"
7379"\n" "r=v=1"
7380"\n" "f=-x*x/4"
7381"\n" "scale=scale+length(a)-scale(a)"
7382"\n" "for(i=1;v!=0;++i){"
7383"\n" "v=v*f/i/(n+i)"
7384"\n" "r+=v"
7385"\n" "}"
7386"\n" "scale=s"
7387"\n" "ibase=b"
7388"\n" "if(o!=0)a=-a"
7389"\n" "return(a*r/1)"
7390"\n" "}"
7391};
7392#endif // ENABLE_BC
7393
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007394static BcStatus bc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007395{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007396 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007397 size_t i;
7398
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007399#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007400 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007401
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007402 // We know that internal library is not buggy,
7403 // thus error checking is normally disabled.
7404# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007405 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007406 s = bc_parse_text(&G.prs, bc_lib);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007407 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007408
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007409 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007410 s = G.prs.parse(&G.prs);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007411 if (DEBUG_LIB && s) return s;
7412 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007413 s = bc_program_exec();
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007414 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007415 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007416#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007417
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007418 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007419 for (i = 0; !s && i < G.files.len; ++i)
7420 s = bc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007421 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7422 // Debug config, non-interactive mode:
7423 // return all the way back to main.
7424 // Non-debug builds do not come here, they exit.
7425 return s;
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007426 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007427
Denys Vlasenko91cde952018-12-10 20:56:08 +01007428 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007429 s = bc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007430
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007431 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
7432 s = bc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007433
Denys Vlasenko00d77792018-11-30 23:13:42 +01007434 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007435}
7436
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007437#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007438static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007439{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007440 bc_num_free(&G.prog.ib);
7441 bc_num_free(&G.prog.ob);
7442 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007443# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007444 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007445# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007446 bc_vec_free(&G.prog.fns);
7447 bc_vec_free(&G.prog.fn_map);
7448 bc_vec_free(&G.prog.vars);
7449 bc_vec_free(&G.prog.var_map);
7450 bc_vec_free(&G.prog.arrs);
7451 bc_vec_free(&G.prog.arr_map);
7452 bc_vec_free(&G.prog.strs);
7453 bc_vec_free(&G.prog.consts);
7454 bc_vec_free(&G.prog.results);
7455 bc_vec_free(&G.prog.stack);
7456 bc_num_free(&G.prog.last);
7457 bc_num_free(&G.prog.zero);
7458 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007459}
7460
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007461static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007462{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007463 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007464 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007465 bc_parse_free(&G.prs);
7466 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007467}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007468#endif
7469
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007470static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007471{
7472 size_t idx;
7473 BcInstPtr ip;
7474
7475 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7476 memset(&ip, 0, sizeof(BcInstPtr));
7477
7478 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007479 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007480 bc_num_ten(&G.prog.ib);
7481 G.prog.ib_t = 10;
7482
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007483 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007484 bc_num_ten(&G.prog.ob);
7485 G.prog.ob_t = 10;
7486
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007487 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007488 bc_num_ten(&G.prog.hexb);
7489 G.prog.hexb.num[0] = 6;
7490
7491#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007492 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007493 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7494#endif
7495
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007496 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007497 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007498
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007499 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007500 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007501
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007502 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007503 bc_num_one(&G.prog.one);
7504
7505 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007506 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007507
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007508 bc_program_addFunc(xstrdup("(main)"), &idx);
7509 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007510
7511 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007512 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007513
7514 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007515 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007516
7517 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7518 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7519 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7520 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7521 bc_vec_push(&G.prog.stack, &ip);
7522}
Gavin Howard01055ba2018-11-03 11:00:21 -06007523
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007524static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007525{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007526#if ENABLE_FEATURE_EDITING
7527 G.line_input_state = new_line_input_t(DO_HISTORY);
7528#endif
7529 G.prog.len = bc_vm_envLen(env_len);
7530
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007531 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007532 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007533 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007534 bc_program_init();
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007535 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007536 IF_BC(bc_parse_init(&G.prs, BC_PROG_MAIN);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007537 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007538 IF_DC(dc_parse_init(&G.prs, BC_PROG_MAIN);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007539 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007540
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007541 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007542#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007543 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007544 // With SA_RESTART, most system calls will restart
7545 // (IOW: they won't fail with EINTR).
7546 // In particular, this means ^C won't cause
7547 // stdout to get into "error state" if SIGINT hits
7548 // within write() syscall.
7549 // The downside is that ^C while line input is taken
7550 // will only be handled after [Enter] since read()
7551 // from stdin is not interrupted by ^C either,
7552 // it restarts, thus fgetc() does not return on ^C.
7553 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7554
7555 // Without SA_RESTART, this exhibits a bug:
7556 // "while (1) print 1" and try ^C-ing it.
7557 // Intermittently, instead of returning to input line,
7558 // you'll get "output error: Interrupted system call"
7559 // and exit.
7560 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007561#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007562 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007563 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007564 return 0; // "not a tty"
7565}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007566
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007567static BcStatus bc_vm_run(void)
7568{
7569 BcStatus st = bc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007570#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007571 if (G_exiting) // it was actually "halt" or "quit"
7572 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007573 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007574# if ENABLE_FEATURE_EDITING
7575 free_line_input_t(G.line_input_state);
7576# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007577 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007578#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007579 return st;
7580}
7581
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007582#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007583int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007584int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007585{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007586 int is_tty;
7587
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007588 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007589 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007590
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007591 is_tty = bc_vm_init("BC_LINE_LENGTH");
7592
7593 bc_args(argv);
7594
7595 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7596 bc_vm_info();
7597
7598 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007599}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007600#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007601
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007602#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007603int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007604int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007605{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007606 int noscript;
7607
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007608 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007609 G.sbgn = '[';
7610 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007611 /*
7612 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7613 * 1 char wider than bc from the same package.
7614 * Both default width, and xC_LINE_LENGTH=N are wider:
7615 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7616 * 1234\
7617 * 56
7618 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7619 * 123\
7620 * 456
7621 * Do the same, or it's a bug?
7622 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007623 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007624
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007625 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7626 noscript = BC_FLAG_I;
7627 for (;;) {
7628 int n = getopt(argc, argv, "e:f:x");
7629 if (n <= 0)
7630 break;
7631 switch (n) {
7632 case 'e':
7633 noscript = 0;
7634 n = bc_vm_process(optarg);
7635 if (n) return n;
7636 break;
7637 case 'f':
7638 noscript = 0;
7639 bc_vm_file(optarg);
7640 break;
7641 case 'x':
7642 option_mask32 |= DC_FLAG_X;
7643 break;
7644 default:
7645 bb_show_usage();
7646 }
7647 }
7648 argv += optind;
7649
7650 while (*argv) {
7651 noscript = 0;
7652 bc_vec_push(&G.files, argv++);
7653 }
7654
7655 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7656
7657 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007658}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007659#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007660
7661#endif // not DC_SMALL