blob: 7ed6dd91d2901be50c8315e2c07443371618ddcd [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 Vlasenko24fb2cd2018-12-05 16:03:46 +0100979static NOINLINE int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100980{
981 va_list p;
982
983 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100984 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100985 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100986
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100987 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100988 exit(1);
989 return BC_STATUS_FAILURE;
990}
991
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +0100992#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100993static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100994{
995 va_list p;
996
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100997 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100998 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100999 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001000
1001 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001002 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001003 va_end(p);
1004
1005 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001006 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001007 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001008 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001009 exit(1);
1010 return BC_STATUS_FAILURE;
1011}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001012#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001013
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001014// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1015// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001016// function must not have caller-cleaned parameters on stack.
1017// Unfortunately, vararg function API does exactly that on most arches.
1018// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001019static int bc_error(const char *msg)
1020{
1021 return bc_error_fmt("%s", msg);
1022}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001023#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001024static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001025{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001026 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001027}
Denys Vlasenko00646792018-12-05 18:12:27 +01001028static int bc_POSIX_does_not_allow(const char *msg)
1029{
1030 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1031}
1032static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1033{
1034 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1035}
1036static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1037{
1038 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1039}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001040#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001041static int bc_error_bad_character(char c)
1042{
1043 return bc_error_fmt("bad character '%c'", c);
1044}
1045static int bc_error_bad_expression(void)
1046{
1047 return bc_error("bad expression");
1048}
1049static int bc_error_bad_token(void)
1050{
1051 return bc_error("bad token");
1052}
1053static int bc_error_stack_has_too_few_elements(void)
1054{
1055 return bc_error("stack has too few elements");
1056}
1057static int bc_error_variable_is_wrong_type(void)
1058{
1059 return bc_error("variable is wrong type");
1060}
1061static int bc_error_nested_read_call(void)
1062{
1063 return bc_error("read() call inside of a read() call");
1064}
1065
Gavin Howard01055ba2018-11-03 11:00:21 -06001066static void bc_vec_grow(BcVec *v, size_t n)
1067{
1068 size_t cap = v->cap * 2;
1069 while (cap < v->len + n) cap *= 2;
1070 v->v = xrealloc(v->v, v->size * cap);
1071 v->cap = cap;
1072}
1073
1074static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1075{
1076 v->size = esize;
1077 v->cap = BC_VEC_START_CAP;
1078 v->len = 0;
1079 v->dtor = dtor;
1080 v->v = xmalloc(esize * BC_VEC_START_CAP);
1081}
1082
Denys Vlasenko7d628012018-12-04 21:46:47 +01001083static void bc_char_vec_init(BcVec *v)
1084{
1085 bc_vec_init(v, sizeof(char), NULL);
1086}
1087
Gavin Howard01055ba2018-11-03 11:00:21 -06001088static void bc_vec_expand(BcVec *v, size_t req)
1089{
1090 if (v->cap < req) {
1091 v->v = xrealloc(v->v, v->size * req);
1092 v->cap = req;
1093 }
1094}
1095
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001096static void bc_vec_pop(BcVec *v)
1097{
1098 v->len--;
1099 if (v->dtor)
1100 v->dtor(v->v + (v->size * v->len));
1101}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001102
Gavin Howard01055ba2018-11-03 11:00:21 -06001103static void bc_vec_npop(BcVec *v, size_t n)
1104{
1105 if (!v->dtor)
1106 v->len -= n;
1107 else {
1108 size_t len = v->len - n;
1109 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1110 }
1111}
1112
Denys Vlasenko7d628012018-12-04 21:46:47 +01001113static void bc_vec_pop_all(BcVec *v)
1114{
1115 bc_vec_npop(v, v->len);
1116}
1117
Gavin Howard01055ba2018-11-03 11:00:21 -06001118static void bc_vec_push(BcVec *v, const void *data)
1119{
1120 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1121 memmove(v->v + (v->size * v->len), data, v->size);
1122 v->len += 1;
1123}
1124
1125static void bc_vec_pushByte(BcVec *v, char data)
1126{
1127 bc_vec_push(v, &data);
1128}
1129
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001130static void bc_vec_pushZeroByte(BcVec *v)
1131{
1132 //bc_vec_pushByte(v, '\0');
1133 // better:
1134 bc_vec_push(v, &const_int_0);
1135}
1136
Gavin Howard01055ba2018-11-03 11:00:21 -06001137static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1138{
1139 if (idx == v->len)
1140 bc_vec_push(v, data);
1141 else {
1142
1143 char *ptr;
1144
1145 if (v->len == v->cap) bc_vec_grow(v, 1);
1146
1147 ptr = v->v + v->size * idx;
1148
1149 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1150 memmove(ptr, data, v->size);
1151 }
1152}
1153
1154static void bc_vec_string(BcVec *v, size_t len, const char *str)
1155{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001156 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001157 bc_vec_expand(v, len + 1);
1158 memcpy(v->v, str, len);
1159 v->len = len;
1160
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001161 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001162}
1163
1164static void bc_vec_concat(BcVec *v, const char *str)
1165{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001166 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001167
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001168 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001169
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001170 slen = strlen(str);
1171 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001172
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001173 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001174 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001175
1176 v->len = len;
1177}
1178
1179static void *bc_vec_item(const BcVec *v, size_t idx)
1180{
1181 return v->v + v->size * idx;
1182}
1183
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01001184static char** bc_program_str(size_t idx)
1185{
1186 return bc_vec_item(&G.prog.strs, idx);
1187}
1188
1189static BcFunc* bc_program_func(size_t idx)
1190{
1191 return bc_vec_item(&G.prog.fns, idx);
1192}
1193
Gavin Howard01055ba2018-11-03 11:00:21 -06001194static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1195{
1196 return v->v + v->size * (v->len - idx - 1);
1197}
1198
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001199static void *bc_vec_top(const BcVec *v)
1200{
1201 return v->v + v->size * (v->len - 1);
1202}
1203
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001204static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001205{
1206 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001207 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001208 free(v->v);
1209}
1210
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001211static int bc_id_cmp(const void *e1, const void *e2)
1212{
1213 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1214}
1215
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001216static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001217{
1218 free(((BcId *) id)->name);
1219}
1220
Gavin Howard01055ba2018-11-03 11:00:21 -06001221static size_t bc_map_find(const BcVec *v, const void *ptr)
1222{
1223 size_t low = 0, high = v->len;
1224
1225 while (low < high) {
1226
1227 size_t mid = (low + high) / 2;
1228 BcId *id = bc_vec_item(v, mid);
1229 int result = bc_id_cmp(ptr, id);
1230
1231 if (result == 0)
1232 return mid;
1233 else if (result < 0)
1234 high = mid;
1235 else
1236 low = mid + 1;
1237 }
1238
1239 return low;
1240}
1241
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001242static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001243{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001244 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001245
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001246 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001247 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001248 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1249 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001250 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001251 bc_vec_pushAt(v, ptr, n);
1252 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001253}
1254
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001255#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06001256static size_t bc_map_index(const BcVec *v, const void *ptr)
1257{
1258 size_t i = bc_map_find(v, ptr);
1259 if (i >= v->len) return BC_VEC_INVALID_IDX;
1260 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1261}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001262#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001263
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001264static int push_input_byte(BcVec *vec, char c)
1265{
1266 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1267 || c > 0x7e
1268 ) {
1269 // Bad chars on this line, ignore entire line
1270 bc_error_fmt("illegal character 0x%02x", c);
1271 return 1;
1272 }
1273 bc_vec_pushByte(vec, (char)c);
1274 return 0;
1275}
1276
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001277static BcStatus bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001278{
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001279 BcStatus s;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001280 bool bad_chars;
Gavin Howard01055ba2018-11-03 11:00:21 -06001281
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001282 s = BC_STATUS_SUCCESS;
Denys Vlasenko00d77792018-11-30 23:13:42 +01001283 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001284 int c;
Gavin Howard01055ba2018-11-03 11:00:21 -06001285
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001286 bad_chars = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001287 bc_vec_pop_all(vec);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001288
1289 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001290
Gavin Howard01055ba2018-11-03 11:00:21 -06001291#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001292 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001293 intr:
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001294 G_interrupt = 0;
Denys Vlasenkoac6ed112018-12-08 21:39:10 +01001295 // GNU bc says "interrupted execution."
1296 // GNU dc says "Interrupt!"
1297 fputs("\ninterrupted execution\n", stderr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001298 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001299# if ENABLE_FEATURE_EDITING
1300 if (G_ttyin) {
1301 int n, i;
1302# define line_buf bb_common_bufsiz1
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001303 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001304 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1305 if (n == 0) // ^C
1306 goto intr;
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001307 s = BC_STATUS_EOF;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001308 break;
1309 }
1310 i = 0;
1311 for (;;) {
1312 c = line_buf[i++];
1313 if (!c) break;
1314 bad_chars |= push_input_byte(vec, c);
1315 }
1316# undef line_buf
1317 } else
1318# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001319#endif
Denys Vlasenkoed849352018-12-06 10:26:13 +01001320 {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001321 IF_FEATURE_BC_SIGNALS(errno = 0;)
1322 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001323 c = fgetc(stdin);
1324#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
1325 // Both conditions appear simultaneously, check both just in case
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001326 if (errno == EINTR || G_interrupt) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001327 // ^C was pressed
1328 clearerr(stdin);
1329 goto intr;
1330 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001331#endif
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001332 if (c == EOF) {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001333 if (ferror(stdin))
1334 quit(); // this emits error message
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001335 s = BC_STATUS_EOF;
Denys Vlasenkoed849352018-12-06 10:26:13 +01001336 // Note: EOF does not append '\n', therefore:
1337 // printf 'print 123\n' | bc - works
1338 // printf 'print 123' | bc - fails (syntax error)
1339 break;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001340 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001341 bad_chars |= push_input_byte(vec, c);
1342 } while (c != '\n');
Denys Vlasenkoed849352018-12-06 10:26:13 +01001343 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001344 } while (bad_chars);
Gavin Howard01055ba2018-11-03 11:00:21 -06001345
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001346 bc_vec_pushZeroByte(vec);
Gavin Howard01055ba2018-11-03 11:00:21 -06001347
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001348 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001349}
1350
Denys Vlasenkodf515392018-12-02 19:27:48 +01001351static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001352{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001353 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001354 size_t size = ((size_t) -1);
1355 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001356
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001357 // Never returns NULL (dies on errors)
1358 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001359
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001360 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001361 char c = buf[i];
1362 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1363 || c > 0x7e
1364 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001365 free(buf);
1366 buf = NULL;
1367 break;
1368 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001369 }
1370
Denys Vlasenkodf515392018-12-02 19:27:48 +01001371 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001372}
1373
Gavin Howard01055ba2018-11-03 11:00:21 -06001374static void bc_num_setToZero(BcNum *n, size_t scale)
1375{
1376 n->len = 0;
1377 n->neg = false;
1378 n->rdx = scale;
1379}
1380
1381static void bc_num_zero(BcNum *n)
1382{
1383 bc_num_setToZero(n, 0);
1384}
1385
1386static void bc_num_one(BcNum *n)
1387{
1388 bc_num_setToZero(n, 0);
1389 n->len = 1;
1390 n->num[0] = 1;
1391}
1392
1393static void bc_num_ten(BcNum *n)
1394{
1395 bc_num_setToZero(n, 0);
1396 n->len = 2;
1397 n->num[0] = 0;
1398 n->num[1] = 1;
1399}
1400
Denys Vlasenko3129f702018-12-09 12:04:44 +01001401// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001402static void bc_num_init(BcNum *n, size_t req)
1403{
1404 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001405 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001406 n->num = xmalloc(req);
1407 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001408 n->rdx = 0;
1409 n->len = 0;
1410 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001411}
1412
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001413static void bc_num_init_DEF_SIZE(BcNum *n)
1414{
1415 bc_num_init(n, BC_NUM_DEF_SIZE);
1416}
1417
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001418static void bc_num_expand(BcNum *n, size_t req)
1419{
1420 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1421 if (req > n->cap) {
1422 n->num = xrealloc(n->num, req);
1423 n->cap = req;
1424 }
1425}
1426
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001427static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001428{
1429 free(((BcNum *) num)->num);
1430}
1431
1432static void bc_num_copy(BcNum *d, BcNum *s)
1433{
1434 if (d != s) {
1435 bc_num_expand(d, s->cap);
1436 d->len = s->len;
1437 d->neg = s->neg;
1438 d->rdx = s->rdx;
1439 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1440 }
1441}
1442
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001443static BcStatus bc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001444{
1445 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001446 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001447
1448 if (n->neg) return bc_error("negative number");
1449
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001450 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001451
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001452 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001453
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001454 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001455 pow *= 10;
1456
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001457 if (result < prev || pow < powprev)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001458 return bc_error("overflow");
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001459 prev = result;
1460 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001461 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001462 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001463
1464 return BC_STATUS_SUCCESS;
1465}
1466
1467static void bc_num_ulong2num(BcNum *n, unsigned long val)
1468{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001469 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001470
1471 bc_num_zero(n);
1472
1473 if (val == 0) return;
1474
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001475 if (ULONG_MAX == 0xffffffffUL)
1476 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001477 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001478 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001479 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001480
1481 ptr = n->num;
1482 for (;;) {
1483 n->len++;
1484 *ptr++ = val % 10;
1485 val /= 10;
1486 if (val == 0) break;
1487 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001488}
1489
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001490static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001491 size_t len)
1492{
1493 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001494 for (i = 0; i < len; ++i) {
1495 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001496 a[i + j++] += 10;
1497 a[i + j] -= 1;
1498 }
1499 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001500}
1501
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01001502#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
1503#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
1504#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
1505//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
1506static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
1507{
1508 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
1509}
1510//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
1511static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
1512{
1513 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
1514}
1515
Gavin Howard01055ba2018-11-03 11:00:21 -06001516static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1517{
1518 size_t i;
1519 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001520 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001521 return BC_NUM_NEG(i + 1, c < 0);
1522}
1523
1524static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1525{
1526 size_t i, min, a_int, b_int, diff;
1527 BcDig *max_num, *min_num;
1528 bool a_max, neg = false;
1529 ssize_t cmp;
1530
1531 if (a == b) return 0;
1532 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1533 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
1534 if (a->neg) {
1535 if (b->neg)
1536 neg = true;
1537 else
1538 return -1;
1539 }
1540 else if (b->neg)
1541 return 1;
1542
1543 a_int = BC_NUM_INT(a);
1544 b_int = BC_NUM_INT(b);
1545 a_int -= b_int;
1546 a_max = (a->rdx > b->rdx);
1547
1548 if (a_int != 0) return (ssize_t) a_int;
1549
1550 if (a_max) {
1551 min = b->rdx;
1552 diff = a->rdx - b->rdx;
1553 max_num = a->num + diff;
1554 min_num = b->num;
1555 }
1556 else {
1557 min = a->rdx;
1558 diff = b->rdx - a->rdx;
1559 max_num = b->num + diff;
1560 min_num = a->num;
1561 }
1562
1563 cmp = bc_num_compare(max_num, min_num, b_int + min);
1564 if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg);
1565
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001566 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001567 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
1568 }
1569
1570 return 0;
1571}
1572
1573static void bc_num_truncate(BcNum *n, size_t places)
1574{
1575 if (places == 0) return;
1576
1577 n->rdx -= places;
1578
1579 if (n->len != 0) {
1580 n->len -= places;
1581 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1582 }
1583}
1584
1585static void bc_num_extend(BcNum *n, size_t places)
1586{
1587 size_t len = n->len + places;
1588
1589 if (places != 0) {
1590
1591 if (n->cap < len) bc_num_expand(n, len);
1592
1593 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1594 memset(n->num, 0, sizeof(BcDig) * places);
1595
1596 n->len += places;
1597 n->rdx += places;
1598 }
1599}
1600
1601static void bc_num_clean(BcNum *n)
1602{
1603 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1604 if (n->len == 0)
1605 n->neg = false;
1606 else if (n->len < n->rdx)
1607 n->len = n->rdx;
1608}
1609
1610static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1611{
1612 if (n->rdx < scale)
1613 bc_num_extend(n, scale - n->rdx);
1614 else
1615 bc_num_truncate(n, n->rdx - scale);
1616
1617 bc_num_clean(n);
1618 if (n->len != 0) n->neg = !neg1 != !neg2;
1619}
1620
1621static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1622 BcNum *restrict b)
1623{
1624 if (idx < n->len) {
1625
1626 b->len = n->len - idx;
1627 a->len = idx;
1628 a->rdx = b->rdx = 0;
1629
1630 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1631 memcpy(a->num, n->num, idx * sizeof(BcDig));
1632 }
1633 else {
1634 bc_num_zero(b);
1635 bc_num_copy(a, n);
1636 }
1637
1638 bc_num_clean(a);
1639 bc_num_clean(b);
1640}
1641
1642static BcStatus bc_num_shift(BcNum *n, size_t places)
1643{
1644 if (places == 0 || n->len == 0) return BC_STATUS_SUCCESS;
Denys Vlasenko64074a12018-12-07 15:50:14 +01001645
1646 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1647 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1648 if (places + n->len > BC_MAX_NUM)
1649 return bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]");
1650 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001651
1652 if (n->rdx >= places)
1653 n->rdx -= places;
1654 else {
1655 bc_num_extend(n, places - n->rdx);
1656 n->rdx = 0;
1657 }
1658
1659 bc_num_clean(n);
1660
1661 return BC_STATUS_SUCCESS;
1662}
1663
1664static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale)
1665{
1666 BcNum one;
1667 BcDig num[2];
1668
1669 one.cap = 2;
1670 one.num = num;
1671 bc_num_one(&one);
1672
1673 return bc_num_div(&one, a, b, scale);
1674}
1675
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001676static FAST_FUNC BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001677{
1678 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1679 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1680 int carry, in;
1681
1682 // Because this function doesn't need to use scale (per the bc spec),
1683 // I am hijacking it to say whether it's doing an add or a subtract.
1684
1685 if (a->len == 0) {
1686 bc_num_copy(c, b);
1687 if (sub && c->len) c->neg = !c->neg;
1688 return BC_STATUS_SUCCESS;
1689 }
1690 else if (b->len == 0) {
1691 bc_num_copy(c, a);
1692 return BC_STATUS_SUCCESS;
1693 }
1694
1695 c->neg = a->neg;
1696 c->rdx = BC_MAX(a->rdx, b->rdx);
1697 min_rdx = BC_MIN(a->rdx, b->rdx);
1698 c->len = 0;
1699
1700 if (a->rdx > b->rdx) {
1701 diff = a->rdx - b->rdx;
1702 ptr = a->num;
1703 ptr_a = a->num + diff;
1704 ptr_b = b->num;
1705 }
1706 else {
1707 diff = b->rdx - a->rdx;
1708 ptr = b->num;
1709 ptr_a = a->num;
1710 ptr_b = b->num + diff;
1711 }
1712
1713 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1714
1715 ptr_c += diff;
1716 a_int = BC_NUM_INT(a);
1717 b_int = BC_NUM_INT(b);
1718
1719 if (a_int > b_int) {
1720 min_int = b_int;
1721 max = a_int;
1722 ptr = ptr_a;
1723 }
1724 else {
1725 min_int = a_int;
1726 max = b_int;
1727 ptr = ptr_b;
1728 }
1729
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001730 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001731 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1732 carry = in / 10;
1733 ptr_c[i] = (BcDig)(in % 10);
1734 }
1735
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001736 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001737 in = ((int) ptr[i]) + carry;
1738 carry = in / 10;
1739 ptr_c[i] = (BcDig)(in % 10);
1740 }
1741
1742 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1743
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001744 return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001745}
1746
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001747static FAST_FUNC BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001748{
Gavin Howard01055ba2018-11-03 11:00:21 -06001749 ssize_t cmp;
1750 BcNum *minuend, *subtrahend;
1751 size_t start;
1752 bool aneg, bneg, neg;
1753
1754 // Because this function doesn't need to use scale (per the bc spec),
1755 // I am hijacking it to say whether it's doing an add or a subtract.
1756
1757 if (a->len == 0) {
1758 bc_num_copy(c, b);
1759 if (sub && c->len) c->neg = !c->neg;
1760 return BC_STATUS_SUCCESS;
1761 }
1762 else if (b->len == 0) {
1763 bc_num_copy(c, a);
1764 return BC_STATUS_SUCCESS;
1765 }
1766
1767 aneg = a->neg;
1768 bneg = b->neg;
1769 a->neg = b->neg = false;
1770
1771 cmp = bc_num_cmp(a, b);
1772
1773 a->neg = aneg;
1774 b->neg = bneg;
1775
1776 if (cmp == 0) {
1777 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
1778 return BC_STATUS_SUCCESS;
1779 }
1780 else if (cmp > 0) {
1781 neg = a->neg;
1782 minuend = a;
1783 subtrahend = b;
1784 }
1785 else {
1786 neg = b->neg;
1787 if (sub) neg = !neg;
1788 minuend = b;
1789 subtrahend = a;
1790 }
1791
1792 bc_num_copy(c, minuend);
1793 c->neg = neg;
1794
1795 if (c->rdx < subtrahend->rdx) {
1796 bc_num_extend(c, subtrahend->rdx - c->rdx);
1797 start = 0;
1798 }
1799 else
1800 start = c->rdx - subtrahend->rdx;
1801
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001802 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001803
1804 bc_num_clean(c);
1805
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001806 return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001807}
1808
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001809static FAST_FUNC BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001810 BcNum *restrict c)
1811{
1812 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001813 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001814 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001815 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001816
Gavin Howard01055ba2018-11-03 11:00:21 -06001817 if (a->len == 0 || b->len == 0) {
1818 bc_num_zero(c);
1819 return BC_STATUS_SUCCESS;
1820 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001821 aone = BC_NUM_ONE(a);
1822 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001823 bc_num_copy(c, aone ? b : a);
1824 return BC_STATUS_SUCCESS;
1825 }
1826
1827 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1828 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1829 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001830 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001831 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001832
Gavin Howard01055ba2018-11-03 11:00:21 -06001833 bc_num_expand(c, a->len + b->len + 1);
1834
1835 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001836 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001837
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001838 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001839
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001840 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001841 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001842 unsigned in = c->num[i + j];
1843 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1844 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001845 carry = in / 10;
1846 c->num[i + j] = (BcDig)(in % 10);
1847 }
1848
1849 c->num[i + j] += (BcDig) carry;
1850 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001851
1852 // a=2^1000000
1853 // a*a <- without check below, this will not be interruptible
1854 if (G_interrupt) return BC_STATUS_FAILURE;
Gavin Howard01055ba2018-11-03 11:00:21 -06001855 }
1856
1857 c->len = len;
1858
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001859 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06001860 }
1861
1862 bc_num_init(&l1, max);
1863 bc_num_init(&h1, max);
1864 bc_num_init(&l2, max);
1865 bc_num_init(&h2, max);
1866 bc_num_init(&m1, max);
1867 bc_num_init(&m2, max);
1868 bc_num_init(&z0, max);
1869 bc_num_init(&z1, max);
1870 bc_num_init(&z2, max);
1871 bc_num_init(&temp, max + max);
1872
1873 bc_num_split(a, max2, &l1, &h1);
1874 bc_num_split(b, max2, &l2, &h2);
1875
1876 s = bc_num_add(&h1, &l1, &m1, 0);
1877 if (s) goto err;
1878 s = bc_num_add(&h2, &l2, &m2, 0);
1879 if (s) goto err;
1880
1881 s = bc_num_k(&h1, &h2, &z0);
1882 if (s) goto err;
1883 s = bc_num_k(&m1, &m2, &z1);
1884 if (s) goto err;
1885 s = bc_num_k(&l1, &l2, &z2);
1886 if (s) goto err;
1887
1888 s = bc_num_sub(&z1, &z0, &temp, 0);
1889 if (s) goto err;
1890 s = bc_num_sub(&temp, &z2, &z1, 0);
1891 if (s) goto err;
1892
1893 s = bc_num_shift(&z0, max2 * 2);
1894 if (s) goto err;
1895 s = bc_num_shift(&z1, max2);
1896 if (s) goto err;
1897 s = bc_num_add(&z0, &z1, &temp, 0);
1898 if (s) goto err;
1899 s = bc_num_add(&temp, &z2, c, 0);
1900
1901err:
1902 bc_num_free(&temp);
1903 bc_num_free(&z2);
1904 bc_num_free(&z1);
1905 bc_num_free(&z0);
1906 bc_num_free(&m2);
1907 bc_num_free(&m1);
1908 bc_num_free(&h2);
1909 bc_num_free(&l2);
1910 bc_num_free(&h1);
1911 bc_num_free(&l1);
1912 return s;
1913}
1914
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001915static FAST_FUNC BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001916{
1917 BcStatus s;
1918 BcNum cpa, cpb;
1919 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1920
1921 scale = BC_MAX(scale, a->rdx);
1922 scale = BC_MAX(scale, b->rdx);
1923 scale = BC_MIN(a->rdx + b->rdx, scale);
1924 maxrdx = BC_MAX(maxrdx, scale);
1925
1926 bc_num_init(&cpa, a->len);
1927 bc_num_init(&cpb, b->len);
1928
1929 bc_num_copy(&cpa, a);
1930 bc_num_copy(&cpb, b);
1931 cpa.neg = cpb.neg = false;
1932
1933 s = bc_num_shift(&cpa, maxrdx);
1934 if (s) goto err;
1935 s = bc_num_shift(&cpb, maxrdx);
1936 if (s) goto err;
1937 s = bc_num_k(&cpa, &cpb, c);
1938 if (s) goto err;
1939
1940 maxrdx += scale;
1941 bc_num_expand(c, c->len + maxrdx);
1942
1943 if (c->len < maxrdx) {
1944 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1945 c->len += maxrdx;
1946 }
1947
1948 c->rdx = maxrdx;
1949 bc_num_retireMul(c, scale, a->neg, b->neg);
1950
1951err:
1952 bc_num_free(&cpb);
1953 bc_num_free(&cpa);
1954 return s;
1955}
1956
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001957static FAST_FUNC BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001958{
1959 BcStatus s = BC_STATUS_SUCCESS;
1960 BcDig *n, *p, q;
1961 size_t len, end, i;
1962 BcNum cp;
1963 bool zero = true;
1964
1965 if (b->len == 0)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01001966 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06001967 else if (a->len == 0) {
1968 bc_num_setToZero(c, scale);
1969 return BC_STATUS_SUCCESS;
1970 }
1971 else if (BC_NUM_ONE(b)) {
1972 bc_num_copy(c, a);
1973 bc_num_retireMul(c, scale, a->neg, b->neg);
1974 return BC_STATUS_SUCCESS;
1975 }
1976
1977 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1978 bc_num_copy(&cp, a);
1979 len = b->len;
1980
1981 if (len > cp.len) {
1982 bc_num_expand(&cp, len + 2);
1983 bc_num_extend(&cp, len - cp.len);
1984 }
1985
1986 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1987 cp.rdx -= b->rdx;
1988 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1989
1990 if (b->rdx == b->len) {
1991 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
1992 len -= i - 1;
1993 }
1994
1995 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1996
1997 // We want an extra zero in front to make things simpler.
1998 cp.num[cp.len++] = 0;
1999 end = cp.len - len;
2000
2001 bc_num_expand(c, cp.len);
2002
2003 bc_num_zero(c);
2004 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2005 c->rdx = cp.rdx;
2006 c->len = cp.len;
2007 p = b->num;
2008
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002009 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002010 n = cp.num + i;
2011 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002012 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002013 c->num[i] = q;
Denys Vlasenkof381a882018-12-05 19:21:34 +01002014 // a=2^100000
2015 // scale=40000
2016 // 1/a <- without check below, this will not be interruptible
2017 if (G_interrupt) {
2018 s = BC_STATUS_FAILURE;
2019 break;
2020 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002021 }
2022
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002023 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002024 bc_num_free(&cp);
2025
Denys Vlasenkof381a882018-12-05 19:21:34 +01002026 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06002027}
2028
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002029static FAST_FUNC BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002030 BcNum *restrict d, size_t scale, size_t ts)
2031{
2032 BcStatus s;
2033 BcNum temp;
2034 bool neg;
2035
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002036 if (b->len == 0)
2037 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06002038
2039 if (a->len == 0) {
2040 bc_num_setToZero(d, ts);
2041 return BC_STATUS_SUCCESS;
2042 }
2043
2044 bc_num_init(&temp, d->cap);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002045 s = bc_num_d(a, b, c, scale);
2046 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002047
2048 if (scale != 0) scale = ts;
2049
2050 s = bc_num_m(c, b, &temp, scale);
2051 if (s) goto err;
2052 s = bc_num_sub(a, &temp, d, scale);
2053 if (s) goto err;
2054
2055 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2056
2057 neg = d->neg;
2058 bc_num_retireMul(d, ts, a->neg, b->neg);
2059 d->neg = neg;
2060
2061err:
2062 bc_num_free(&temp);
2063 return s;
2064}
2065
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002066static FAST_FUNC BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002067{
2068 BcStatus s;
2069 BcNum c1;
2070 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2071
2072 bc_num_init(&c1, len);
2073 s = bc_num_r(a, b, &c1, c, scale, ts);
2074 bc_num_free(&c1);
2075
2076 return s;
2077}
2078
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002079static FAST_FUNC BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002080{
2081 BcStatus s = BC_STATUS_SUCCESS;
2082 BcNum copy;
2083 unsigned long pow;
2084 size_t i, powrdx, resrdx;
2085 bool neg, zero;
2086
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002087 if (b->rdx) return bc_error("non integer number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002088
2089 if (b->len == 0) {
2090 bc_num_one(c);
2091 return BC_STATUS_SUCCESS;
2092 }
2093 else if (a->len == 0) {
2094 bc_num_setToZero(c, scale);
2095 return BC_STATUS_SUCCESS;
2096 }
2097 else if (BC_NUM_ONE(b)) {
2098 if (!b->neg)
2099 bc_num_copy(c, a);
2100 else
2101 s = bc_num_inv(a, c, scale);
2102 return s;
2103 }
2104
2105 neg = b->neg;
2106 b->neg = false;
2107
2108 s = bc_num_ulong(b, &pow);
2109 if (s) return s;
2110
2111 bc_num_init(&copy, a->len);
2112 bc_num_copy(&copy, a);
2113
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002114 if (!neg) {
2115 if (a->rdx > scale)
2116 scale = a->rdx;
2117 if (a->rdx * pow < scale)
2118 scale = a->rdx * pow;
2119 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002120
2121 b->neg = neg;
2122
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002123 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002124 powrdx <<= 1;
2125 s = bc_num_mul(&copy, &copy, &copy, powrdx);
2126 if (s) goto err;
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002127 // Not needed: bc_num_mul() has a check for ^C:
2128 //if (G_interrupt) {
2129 // s = BC_STATUS_FAILURE;
2130 // goto err;
2131 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002132 }
2133
Gavin Howard01055ba2018-11-03 11:00:21 -06002134 bc_num_copy(c, &copy);
2135
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002136 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002137
2138 powrdx <<= 1;
2139 s = bc_num_mul(&copy, &copy, &copy, powrdx);
2140 if (s) goto err;
2141
2142 if (pow & 1) {
2143 resrdx += powrdx;
2144 s = bc_num_mul(c, &copy, c, resrdx);
2145 if (s) goto err;
2146 }
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002147 // Not needed: bc_num_mul() has a check for ^C:
2148 //if (G_interrupt) {
2149 // s = BC_STATUS_FAILURE;
2150 // goto err;
2151 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002152 }
2153
2154 if (neg) {
2155 s = bc_num_inv(c, c, scale);
2156 if (s) goto err;
2157 }
2158
Gavin Howard01055ba2018-11-03 11:00:21 -06002159 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2160
2161 // We can't use bc_num_clean() here.
2162 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2163 if (zero) bc_num_setToZero(c, scale);
2164
2165err:
2166 bc_num_free(&copy);
2167 return s;
2168}
2169
2170static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
2171 BcNumBinaryOp op, size_t req)
2172{
2173 BcStatus s;
2174 BcNum num2, *ptr_a, *ptr_b;
2175 bool init = false;
2176
2177 if (c == a) {
2178 ptr_a = &num2;
2179 memcpy(ptr_a, c, sizeof(BcNum));
2180 init = true;
2181 }
2182 else
2183 ptr_a = a;
2184
2185 if (c == b) {
2186 ptr_b = &num2;
2187 if (c != a) {
2188 memcpy(ptr_b, c, sizeof(BcNum));
2189 init = true;
2190 }
2191 }
2192 else
2193 ptr_b = b;
2194
2195 if (init)
2196 bc_num_init(c, req);
2197 else
2198 bc_num_expand(c, req);
2199
2200 s = op(ptr_a, ptr_b, c, scale);
2201
2202 if (init) bc_num_free(&num2);
2203
2204 return s;
2205}
2206
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002207static void bc_num_printNewline(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06002208{
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002209 if (G.prog.nchars == G.prog.len - 1) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002210 bb_putchar('\\');
2211 bb_putchar('\n');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002212 G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06002213 }
2214}
2215
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002216#if ENABLE_DC
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002217static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
Gavin Howard01055ba2018-11-03 11:00:21 -06002218{
Denys Vlasenko2a8ad482018-12-08 21:56:37 +01002219 (void) radix;
Denys Vlasenko00d77792018-11-30 23:13:42 +01002220 bb_putchar((char) num);
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002221 G.prog.nchars += width;
Gavin Howard01055ba2018-11-03 11:00:21 -06002222}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002223#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002224
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002225static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
Gavin Howard01055ba2018-11-03 11:00:21 -06002226{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002227 size_t exp, pow;
Gavin Howard01055ba2018-11-03 11:00:21 -06002228
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002229 bc_num_printNewline();
Denys Vlasenko00d77792018-11-30 23:13:42 +01002230 bb_putchar(radix ? '.' : ' ');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002231 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06002232
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002233 bc_num_printNewline();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002234 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
2235 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002236
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002237 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002238 size_t dig;
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002239 bc_num_printNewline();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002240 dig = num / pow;
2241 num -= dig * pow;
Denys Vlasenko00d77792018-11-30 23:13:42 +01002242 bb_putchar(((char) dig) + '0');
Gavin Howard01055ba2018-11-03 11:00:21 -06002243 }
2244}
2245
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002246static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
Gavin Howard01055ba2018-11-03 11:00:21 -06002247{
2248 if (radix) {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002249 bc_num_printNewline();
Denys Vlasenko00d77792018-11-30 23:13:42 +01002250 bb_putchar('.');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002251 G.prog.nchars += 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002252 }
2253
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002254 bc_num_printNewline();
Denys Vlasenko00d77792018-11-30 23:13:42 +01002255 bb_putchar(bb_hexdigits_upcase[num]);
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002256 G.prog.nchars += width;
Gavin Howard01055ba2018-11-03 11:00:21 -06002257}
2258
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002259static void bc_num_printDecimal(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06002260{
2261 size_t i, rdx = n->rdx - 1;
2262
Denys Vlasenko00d77792018-11-30 23:13:42 +01002263 if (n->neg) bb_putchar('-');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002264 G.prog.nchars += n->neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06002265
2266 for (i = n->len - 1; i < n->len; --i)
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002267 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002268}
2269
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002270static BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
Gavin Howard01055ba2018-11-03 11:00:21 -06002271{
2272 BcStatus s;
2273 BcVec stack;
2274 BcNum intp, fracp, digit, frac_len;
2275 unsigned long dig, *ptr;
2276 size_t i;
2277 bool radix;
2278
2279 if (n->len == 0) {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002280 print(0, width, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06002281 return BC_STATUS_SUCCESS;
2282 }
2283
2284 bc_vec_init(&stack, sizeof(long), NULL);
2285 bc_num_init(&intp, n->len);
2286 bc_num_init(&fracp, n->rdx);
2287 bc_num_init(&digit, width);
2288 bc_num_init(&frac_len, BC_NUM_INT(n));
2289 bc_num_copy(&intp, n);
2290 bc_num_one(&frac_len);
2291
2292 bc_num_truncate(&intp, intp.rdx);
2293 s = bc_num_sub(n, &intp, &fracp, 0);
2294 if (s) goto err;
2295
2296 while (intp.len != 0) {
2297 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
2298 if (s) goto err;
2299 s = bc_num_ulong(&digit, &dig);
2300 if (s) goto err;
2301 bc_vec_push(&stack, &dig);
2302 }
2303
2304 for (i = 0; i < stack.len; ++i) {
2305 ptr = bc_vec_item_rev(&stack, i);
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002306 print(*ptr, width, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06002307 }
2308
2309 if (!n->rdx) goto err;
2310
2311 for (radix = true; frac_len.len <= n->rdx; radix = false) {
2312 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
2313 if (s) goto err;
2314 s = bc_num_ulong(&fracp, &dig);
2315 if (s) goto err;
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01002316 bc_num_ulong2num(&intp, dig);
Gavin Howard01055ba2018-11-03 11:00:21 -06002317 s = bc_num_sub(&fracp, &intp, &fracp, 0);
2318 if (s) goto err;
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002319 print(dig, width, radix);
Gavin Howard01055ba2018-11-03 11:00:21 -06002320 s = bc_num_mul(&frac_len, base, &frac_len, 0);
2321 if (s) goto err;
2322 }
2323
2324err:
2325 bc_num_free(&frac_len);
2326 bc_num_free(&digit);
2327 bc_num_free(&fracp);
2328 bc_num_free(&intp);
2329 bc_vec_free(&stack);
2330 return s;
2331}
2332
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002333static BcStatus bc_num_printBase(BcNum *n)
Gavin Howard01055ba2018-11-03 11:00:21 -06002334{
2335 BcStatus s;
2336 size_t width, i;
2337 BcNumDigitOp print;
2338 bool neg = n->neg;
2339
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002340 if (neg) {
2341 bb_putchar('-');
2342 G.prog.nchars++;
2343 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002344
2345 n->neg = false;
2346
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002347 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002348 width = 1;
2349 print = bc_num_printHex;
2350 }
2351 else {
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002352 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
2353 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002354 print = bc_num_printDigits;
2355 }
2356
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002357 s = bc_num_printNum(n, &G.prog.ob, width, print);
Gavin Howard01055ba2018-11-03 11:00:21 -06002358 n->neg = neg;
2359
2360 return s;
2361}
2362
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002363#if ENABLE_DC
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002364static BcStatus bc_num_stream(BcNum *n, BcNum *base)
Gavin Howard01055ba2018-11-03 11:00:21 -06002365{
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002366 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard01055ba2018-11-03 11:00:21 -06002367}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002368#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002369
Denys Vlasenko9311e012018-12-10 11:54:18 +01002370static bool bc_num_strValid(const char *val, size_t base)
2371{
2372 BcDig b;
2373 bool radix;
2374
2375 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2376 radix = false;
2377 for (;;) {
2378 BcDig c = *val++;
2379 if (c == '\0')
2380 break;
2381 if (c == '.') {
2382 if (radix) return false;
2383 radix = true;
2384 continue;
2385 }
2386 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2387 return false;
2388 }
2389 return true;
2390}
2391
2392// Note: n is already "bc_num_zero()"ed,
2393// leading zeroes in "val" are removed
2394static void bc_num_parseDecimal(BcNum *n, const char *val)
2395{
2396 size_t len, i;
2397 const char *ptr;
2398 bool zero;
2399
2400 len = strlen(val);
2401 if (len == 0)
2402 return;
2403
2404 zero = true;
2405 for (i = 0; val[i]; ++i) {
2406 if (val[i] != '0' && val[i] != '.') {
2407 zero = false;
2408 break;
2409 }
2410 }
2411 bc_num_expand(n, len);
2412
2413 ptr = strchr(val, '.');
2414
2415 n->rdx = 0;
2416 if (ptr != NULL)
2417 n->rdx = (size_t)((val + len) - (ptr + 1));
2418
2419 if (!zero) {
2420 i = len - 1;
2421 for (;;) {
2422 n->num[n->len] = val[i] - '0';
2423 ++n->len;
2424 skip_dot:
2425 if ((ssize_t)--i == (ssize_t)-1) break;
2426 if (val[i] == '.') goto skip_dot;
2427 }
2428 }
2429}
2430
2431// Note: n is already "bc_num_zero()"ed,
2432// leading zeroes in "val" are removed
2433static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2434{
2435 BcStatus s;
2436 BcNum temp, mult, result;
2437 BcDig c = '\0';
2438 unsigned long v;
2439 size_t i, digits;
2440
2441 for (i = 0; ; ++i) {
2442 if (val[i] == '\0')
2443 return;
2444 if (val[i] != '.' && val[i] != '0')
2445 break;
2446 }
2447
2448 bc_num_init_DEF_SIZE(&temp);
2449 bc_num_init_DEF_SIZE(&mult);
2450
2451 for (;;) {
2452 c = *val++;
2453 if (c == '\0') goto int_err;
2454 if (c == '.') break;
2455
2456 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2457
2458 s = bc_num_mul(n, base, &mult, 0);
2459 if (s) goto int_err;
2460 bc_num_ulong2num(&temp, v);
2461 s = bc_num_add(&mult, &temp, n, 0);
2462 if (s) goto int_err;
2463 }
2464
2465 bc_num_init(&result, base->len);
2466 //bc_num_zero(&result); - already is
2467 bc_num_one(&mult);
2468
2469 digits = 0;
2470 for (;;) {
2471 c = *val++;
2472 if (c == '\0') break;
2473 digits++;
2474
2475 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2476
2477 s = bc_num_mul(&result, base, &result, 0);
2478 if (s) goto err;
2479 bc_num_ulong2num(&temp, v);
2480 s = bc_num_add(&result, &temp, &result, 0);
2481 if (s) goto err;
2482 s = bc_num_mul(&mult, base, &mult, 0);
2483 if (s) goto err;
2484 }
2485
2486 s = bc_num_div(&result, &mult, &result, digits);
2487 if (s) goto err;
2488 s = bc_num_add(n, &result, n, digits);
2489 if (s) goto err;
2490
2491 if (n->len != 0) {
2492 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2493 } else
2494 bc_num_zero(n);
2495
2496err:
2497 bc_num_free(&result);
2498int_err:
2499 bc_num_free(&mult);
2500 bc_num_free(&temp);
2501}
2502
Gavin Howard01055ba2018-11-03 11:00:21 -06002503static BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base,
2504 size_t base_t)
2505{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002506 if (!bc_num_strValid(val, base_t))
2507 return bc_error("bad number string");
Gavin Howard01055ba2018-11-03 11:00:21 -06002508
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002509 bc_num_zero(n);
2510 while (*val == '0') val++;
2511
Gavin Howard01055ba2018-11-03 11:00:21 -06002512 if (base_t == 10)
2513 bc_num_parseDecimal(n, val);
2514 else
2515 bc_num_parseBase(n, val, base);
2516
2517 return BC_STATUS_SUCCESS;
2518}
2519
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002520static BcStatus bc_num_print(BcNum *n, bool newline)
Gavin Howard01055ba2018-11-03 11:00:21 -06002521{
2522 BcStatus s = BC_STATUS_SUCCESS;
2523
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002524 bc_num_printNewline();
Gavin Howard01055ba2018-11-03 11:00:21 -06002525
2526 if (n->len == 0) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002527 bb_putchar('0');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002528 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06002529 }
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002530 else if (G.prog.ob_t == 10)
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002531 bc_num_printDecimal(n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002532 else
Denys Vlasenko0f37b322018-12-08 23:48:53 +01002533 s = bc_num_printBase(n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002534
2535 if (newline) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002536 bb_putchar('\n');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01002537 G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06002538 }
2539
2540 return s;
2541}
2542
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002543static FAST_FUNC BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002544{
2545 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
2546 (void) scale;
2547 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
2548}
2549
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002550static FAST_FUNC BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002551{
2552 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
2553 (void) scale;
2554 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
2555}
2556
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002557static FAST_FUNC BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002558{
2559 size_t req = BC_NUM_MREQ(a, b, scale);
2560 return bc_num_binary(a, b, c, scale, bc_num_m, req);
2561}
2562
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002563static FAST_FUNC BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002564{
2565 size_t req = BC_NUM_MREQ(a, b, scale);
2566 return bc_num_binary(a, b, c, scale, bc_num_d, req);
2567}
2568
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002569static FAST_FUNC BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002570{
2571 size_t req = BC_NUM_MREQ(a, b, scale);
2572 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
2573}
2574
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002575static FAST_FUNC BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002576{
2577 return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1);
2578}
2579
2580static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
2581{
2582 BcStatus s;
2583 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2584 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2585 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2586
2587 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2588 bc_num_expand(b, req);
2589
2590 if (a->len == 0) {
2591 bc_num_setToZero(b, scale);
2592 return BC_STATUS_SUCCESS;
2593 }
2594 else if (a->neg)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002595 return bc_error("negative number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002596 else if (BC_NUM_ONE(a)) {
2597 bc_num_one(b);
2598 bc_num_extend(b, scale);
2599 return BC_STATUS_SUCCESS;
2600 }
2601
2602 scale = BC_MAX(scale, a->rdx) + 1;
2603 len = a->len + scale;
2604
2605 bc_num_init(&num1, len);
2606 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002607 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002608
2609 bc_num_one(&half);
2610 half.num[0] = 5;
2611 half.rdx = 1;
2612
2613 bc_num_init(&f, len);
2614 bc_num_init(&fprime, len);
2615
2616 x0 = &num1;
2617 x1 = &num2;
2618
2619 bc_num_one(x0);
2620 pow = BC_NUM_INT(a);
2621
2622 if (pow) {
2623
2624 if (pow & 1)
2625 x0->num[0] = 2;
2626 else
2627 x0->num[0] = 6;
2628
2629 pow -= 2 - (pow & 1);
2630
2631 bc_num_extend(x0, pow);
2632
2633 // Make sure to move the radix back.
2634 x0->rdx -= pow;
2635 }
2636
2637 x0->rdx = digs = digs1 = 0;
2638 resrdx = scale + 2;
2639 len = BC_NUM_INT(x0) + resrdx - 1;
2640
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002641 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002642
2643 s = bc_num_div(a, x0, &f, resrdx);
2644 if (s) goto err;
2645 s = bc_num_add(x0, &f, &fprime, resrdx);
2646 if (s) goto err;
2647 s = bc_num_mul(&fprime, &half, x1, resrdx);
2648 if (s) goto err;
2649
2650 cmp = bc_num_cmp(x1, x0);
2651 digs = x1->len - (unsigned long long) llabs(cmp);
2652
2653 if (cmp == cmp2 && digs == digs1)
2654 times += 1;
2655 else
2656 times = 0;
2657
2658 resrdx += times > 4;
2659
2660 cmp2 = cmp1;
2661 cmp1 = cmp;
2662 digs1 = digs;
2663
2664 temp = x0;
2665 x0 = x1;
2666 x1 = temp;
2667 }
2668
Gavin Howard01055ba2018-11-03 11:00:21 -06002669 bc_num_copy(b, x0);
2670 scale -= 1;
2671 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2672
2673err:
2674 bc_num_free(&fprime);
2675 bc_num_free(&f);
2676 bc_num_free(&half);
2677 bc_num_free(&num2);
2678 bc_num_free(&num1);
2679 return s;
2680}
2681
2682static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
2683 size_t scale)
2684{
2685 BcStatus s;
2686 BcNum num2, *ptr_a;
2687 bool init = false;
2688 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2689
2690 if (c == a) {
2691 memcpy(&num2, c, sizeof(BcNum));
2692 ptr_a = &num2;
2693 bc_num_init(c, len);
2694 init = true;
2695 }
2696 else {
2697 ptr_a = a;
2698 bc_num_expand(c, len);
2699 }
2700
2701 s = bc_num_r(ptr_a, b, c, d, scale, ts);
2702
2703 if (init) bc_num_free(&num2);
2704
2705 return s;
2706}
2707
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002708#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002709static BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
2710{
2711 BcStatus s;
2712 BcNum base, exp, two, temp;
2713
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002714 if (c->len == 0)
2715 return bc_error("divide by zero");
2716 if (a->rdx || b->rdx || c->rdx)
2717 return bc_error("non integer number");
2718 if (b->neg)
2719 return bc_error("negative number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002720
2721 bc_num_expand(d, c->len);
2722 bc_num_init(&base, c->len);
2723 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002724 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002725 bc_num_init(&temp, b->len);
2726
2727 bc_num_one(&two);
2728 two.num[0] = 2;
2729 bc_num_one(d);
2730
2731 s = bc_num_rem(a, c, &base, 0);
2732 if (s) goto err;
2733 bc_num_copy(&exp, b);
2734
2735 while (exp.len != 0) {
2736
2737 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
2738 if (s) goto err;
2739
2740 if (BC_NUM_ONE(&temp)) {
2741 s = bc_num_mul(d, &base, &temp, 0);
2742 if (s) goto err;
2743 s = bc_num_rem(&temp, c, d, 0);
2744 if (s) goto err;
2745 }
2746
2747 s = bc_num_mul(&base, &base, &temp, 0);
2748 if (s) goto err;
2749 s = bc_num_rem(&temp, c, &base, 0);
2750 if (s) goto err;
2751 }
2752
2753err:
2754 bc_num_free(&temp);
2755 bc_num_free(&two);
2756 bc_num_free(&exp);
2757 bc_num_free(&base);
2758 return s;
2759}
2760#endif // ENABLE_DC
2761
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002762#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06002763static BcStatus bc_func_insert(BcFunc *f, char *name, bool var)
2764{
2765 BcId a;
2766 size_t i;
2767
2768 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002769 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
2770 return bc_error("function parameter or auto var has the same name as another");
Gavin Howard01055ba2018-11-03 11:00:21 -06002771 }
2772
2773 a.idx = var;
2774 a.name = name;
2775
2776 bc_vec_push(&f->autos, &a);
2777
2778 return BC_STATUS_SUCCESS;
2779}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002780#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002781
2782static void bc_func_init(BcFunc *f)
2783{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002784 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002785 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2786 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2787 f->nparams = 0;
2788}
2789
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002790static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002791{
2792 BcFunc *f = (BcFunc *) func;
2793 bc_vec_free(&f->code);
2794 bc_vec_free(&f->autos);
2795 bc_vec_free(&f->labels);
2796}
2797
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002798static void bc_array_expand(BcVec *a, size_t len);
2799
Gavin Howard01055ba2018-11-03 11:00:21 -06002800static void bc_array_init(BcVec *a, bool nums)
2801{
2802 if (nums)
2803 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2804 else
2805 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2806 bc_array_expand(a, 1);
2807}
2808
Gavin Howard01055ba2018-11-03 11:00:21 -06002809static void bc_array_expand(BcVec *a, size_t len)
2810{
2811 BcResultData data;
2812
2813 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2814 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002815 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002816 bc_vec_push(a, &data.n);
2817 }
2818 }
2819 else {
2820 while (len > a->len) {
2821 bc_array_init(&data.v, true);
2822 bc_vec_push(a, &data.v);
2823 }
2824 }
2825}
2826
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002827static void bc_array_copy(BcVec *d, const BcVec *s)
2828{
2829 size_t i;
2830
2831 bc_vec_pop_all(d);
2832 bc_vec_expand(d, s->cap);
2833 d->len = s->len;
2834
2835 for (i = 0; i < s->len; ++i) {
2836 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2837 bc_num_init(dnum, snum->len);
2838 bc_num_copy(dnum, snum);
2839 }
2840}
2841
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002842static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002843{
2844 free(*((char **) string));
2845}
2846
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002847#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002848static void bc_result_copy(BcResult *d, BcResult *src)
2849{
2850 d->t = src->t;
2851
2852 switch (d->t) {
2853
2854 case BC_RESULT_TEMP:
2855 case BC_RESULT_IBASE:
2856 case BC_RESULT_SCALE:
2857 case BC_RESULT_OBASE:
2858 {
2859 bc_num_init(&d->d.n, src->d.n.len);
2860 bc_num_copy(&d->d.n, &src->d.n);
2861 break;
2862 }
2863
2864 case BC_RESULT_VAR:
2865 case BC_RESULT_ARRAY:
2866 case BC_RESULT_ARRAY_ELEM:
2867 {
2868 d->d.id.name = xstrdup(src->d.id.name);
2869 break;
2870 }
2871
2872 case BC_RESULT_CONSTANT:
2873 case BC_RESULT_LAST:
2874 case BC_RESULT_ONE:
2875 case BC_RESULT_STR:
2876 {
2877 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2878 break;
2879 }
2880 }
2881}
2882#endif // ENABLE_DC
2883
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002884static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002885{
2886 BcResult *r = (BcResult *) result;
2887
2888 switch (r->t) {
2889
2890 case BC_RESULT_TEMP:
2891 case BC_RESULT_IBASE:
2892 case BC_RESULT_SCALE:
2893 case BC_RESULT_OBASE:
2894 {
2895 bc_num_free(&r->d.n);
2896 break;
2897 }
2898
2899 case BC_RESULT_VAR:
2900 case BC_RESULT_ARRAY:
2901 case BC_RESULT_ARRAY_ELEM:
2902 {
2903 free(r->d.id.name);
2904 break;
2905 }
2906
2907 default:
2908 {
2909 // Do nothing.
2910 break;
2911 }
2912 }
2913}
2914
2915static void bc_lex_lineComment(BcLex *l)
2916{
2917 l->t.t = BC_LEX_WHITESPACE;
2918 while (l->i < l->len && l->buf[l->i++] != '\n');
2919 --l->i;
2920}
2921
2922static void bc_lex_whitespace(BcLex *l)
2923{
2924 char c;
2925 l->t.t = BC_LEX_WHITESPACE;
2926 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
2927}
2928
2929static BcStatus bc_lex_number(BcLex *l, char start)
2930{
2931 const char *buf = l->buf + l->i;
2932 size_t len, hits = 0, bslashes = 0, i = 0, j;
2933 char c = buf[i];
2934 bool last_pt, pt = start == '.';
2935
2936 last_pt = pt;
2937 l->t.t = BC_LEX_NUMBER;
2938
2939 while (c != 0 && (isdigit(c) || (c >= 'A' && c <= 'F') ||
2940 (c == '.' && !pt) || (c == '\\' && buf[i + 1] == '\n')))
2941 {
2942 if (c != '\\') {
2943 last_pt = c == '.';
2944 pt = pt || last_pt;
2945 }
2946 else {
2947 ++i;
2948 bslashes += 1;
2949 }
2950
2951 c = buf[++i];
2952 }
2953
Denys Vlasenkod5f77032018-12-04 21:37:56 +01002954 len = i + !last_pt - bslashes * 2;
Denys Vlasenko64074a12018-12-07 15:50:14 +01002955 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2956 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2957 if (len > BC_MAX_NUM)
2958 return bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]");
2959 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002960
Denys Vlasenko7d628012018-12-04 21:46:47 +01002961 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002962 bc_vec_expand(&l->t.v, len + 1);
2963 bc_vec_push(&l->t.v, &start);
2964
2965 for (buf -= 1, j = 1; j < len + hits * 2; ++j) {
2966
2967 c = buf[j];
2968
2969 // If we have hit a backslash, skip it. We don't have
2970 // to check for a newline because it's guaranteed.
2971 if (hits < bslashes && c == '\\') {
2972 ++hits;
2973 ++j;
2974 continue;
2975 }
2976
2977 bc_vec_push(&l->t.v, &c);
2978 }
2979
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002980 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002981 l->i += i;
2982
2983 return BC_STATUS_SUCCESS;
2984}
2985
2986static BcStatus bc_lex_name(BcLex *l)
2987{
2988 size_t i = 0;
2989 const char *buf = l->buf + l->i - 1;
2990 char c = buf[i];
2991
2992 l->t.t = BC_LEX_NAME;
2993
2994 while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i];
2995
Denys Vlasenko64074a12018-12-07 15:50:14 +01002996 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2997 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2998 if (i > BC_MAX_STRING)
2999 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
3000 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003001 bc_vec_string(&l->t.v, i, buf);
3002
3003 // Increment the index. We minus 1 because it has already been incremented.
3004 l->i += i - 1;
3005
3006 return BC_STATUS_SUCCESS;
3007}
3008
3009static void bc_lex_init(BcLex *l, BcLexNext next)
3010{
3011 l->next = next;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003012 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003013}
3014
3015static void bc_lex_free(BcLex *l)
3016{
3017 bc_vec_free(&l->t.v);
3018}
3019
Denys Vlasenko0409ad32018-12-05 16:39:22 +01003020static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003021{
Denys Vlasenko5318f812018-12-05 17:48:01 +01003022 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003023 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06003024}
3025
3026static BcStatus bc_lex_next(BcLex *l)
3027{
3028 BcStatus s;
3029
3030 l->t.last = l->t.t;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003031 if (l->t.last == BC_LEX_EOF) return bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06003032
3033 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003034 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003035 l->t.t = BC_LEX_EOF;
3036
3037 l->newline = (l->i == l->len);
3038 if (l->newline) return BC_STATUS_SUCCESS;
3039
3040 // Loop until failure or we don't have whitespace. This
3041 // is so the parser doesn't get inundated with whitespace.
3042 do {
3043 s = l->next(l);
3044 } while (!s && l->t.t == BC_LEX_WHITESPACE);
3045
3046 return s;
3047}
3048
3049static BcStatus bc_lex_text(BcLex *l, const char *text)
3050{
3051 l->buf = text;
3052 l->i = 0;
3053 l->len = strlen(text);
3054 l->t.t = l->t.last = BC_LEX_INVALID;
3055 return bc_lex_next(l);
3056}
3057
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003058#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06003059static BcStatus bc_lex_identifier(BcLex *l)
3060{
3061 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003062 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06003063 const char *buf = l->buf + l->i - 1;
3064
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003065 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
3066 const char *keyword8 = bc_lex_kws[i].name8;
3067 unsigned j = 0;
3068 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3069 j++;
3070 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003071 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003072 if (keyword8[j] != '\0')
3073 continue;
3074 match:
3075 // buf starts with keyword bc_lex_kws[i]
3076 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003077 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003078 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003079 if (s) return s;
3080 }
3081
3082 // We minus 1 because the index has already been incremented.
3083 l->i += j - 1;
3084 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003085 }
3086
3087 s = bc_lex_name(l);
3088 if (s) return s;
3089
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003090 if (l->t.v.len > 2) {
3091 // Prevent this:
3092 // >>> qwe=1
3093 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3094 // '
3095 unsigned len = strchrnul(buf, '\n') - buf;
3096 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3097 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003098
3099 return s;
3100}
3101
3102static BcStatus bc_lex_string(BcLex *l)
3103{
3104 size_t len, nls = 0, i = l->i;
3105 char c;
3106
3107 l->t.t = BC_LEX_STR;
3108
3109 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n');
3110
3111 if (c == '\0') {
3112 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003113 return bc_error("string end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003114 }
3115
3116 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003117 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3118 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3119 if (len > BC_MAX_STRING)
3120 return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]");
3121 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003122 bc_vec_string(&l->t.v, len, l->buf + l->i);
3123
3124 l->i = i + 1;
3125 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003126 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003127
3128 return BC_STATUS_SUCCESS;
3129}
3130
3131static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3132{
3133 if (l->buf[l->i] == '=') {
3134 ++l->i;
3135 l->t.t = with;
3136 }
3137 else
3138 l->t.t = without;
3139}
3140
3141static BcStatus bc_lex_comment(BcLex *l)
3142{
3143 size_t i, nls = 0;
3144 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003145
3146 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003147 i = ++l->i;
3148 for (;;) {
3149 char c = buf[i];
3150 check_star:
3151 if (c == '*') {
3152 c = buf[++i];
3153 if (c == '/')
3154 break;
3155 goto check_star;
3156 }
3157 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003158 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003159 return bc_error("comment end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003160 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003161 nls += (c == '\n');
3162 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003163 }
3164
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003165 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003167 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003168
3169 return BC_STATUS_SUCCESS;
3170}
3171
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003172static FAST_FUNC BcStatus bc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003173{
3174 BcStatus s = BC_STATUS_SUCCESS;
3175 char c = l->buf[l->i++], c2;
3176
3177 // This is the workhorse of the lexer.
3178 switch (c) {
3179
3180 case '\0':
3181 case '\n':
3182 {
3183 l->newline = true;
3184 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3185 break;
3186 }
3187
3188 case '\t':
3189 case '\v':
3190 case '\f':
3191 case '\r':
3192 case ' ':
3193 {
3194 bc_lex_whitespace(l);
3195 break;
3196 }
3197
3198 case '!':
3199 {
3200 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3201
3202 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003203 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Gavin Howard01055ba2018-11-03 11:00:21 -06003204 if (s) return s;
3205 }
3206
3207 break;
3208 }
3209
3210 case '"':
3211 {
3212 s = bc_lex_string(l);
3213 break;
3214 }
3215
3216 case '#':
3217 {
Denys Vlasenko00646792018-12-05 18:12:27 +01003218 s = bc_POSIX_does_not_allow("'#' script comments");
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 if (s) return s;
3220
3221 bc_lex_lineComment(l);
3222
3223 break;
3224 }
3225
3226 case '%':
3227 {
3228 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3229 break;
3230 }
3231
3232 case '&':
3233 {
3234 c2 = l->buf[l->i];
3235 if (c2 == '&') {
3236
Denys Vlasenko00646792018-12-05 18:12:27 +01003237 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Gavin Howard01055ba2018-11-03 11:00:21 -06003238 if (s) return s;
3239
3240 ++l->i;
3241 l->t.t = BC_LEX_OP_BOOL_AND;
3242 }
3243 else {
3244 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003245 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003246 }
3247
3248 break;
3249 }
3250
3251 case '(':
3252 case ')':
3253 {
3254 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3255 break;
3256 }
3257
3258 case '*':
3259 {
3260 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3261 break;
3262 }
3263
3264 case '+':
3265 {
3266 c2 = l->buf[l->i];
3267 if (c2 == '+') {
3268 ++l->i;
3269 l->t.t = BC_LEX_OP_INC;
3270 }
3271 else
3272 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3273 break;
3274 }
3275
3276 case ',':
3277 {
3278 l->t.t = BC_LEX_COMMA;
3279 break;
3280 }
3281
3282 case '-':
3283 {
3284 c2 = l->buf[l->i];
3285 if (c2 == '-') {
3286 ++l->i;
3287 l->t.t = BC_LEX_OP_DEC;
3288 }
3289 else
3290 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3291 break;
3292 }
3293
3294 case '.':
3295 {
3296 if (isdigit(l->buf[l->i]))
3297 s = bc_lex_number(l, c);
3298 else {
3299 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003300 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003301 }
3302 break;
3303 }
3304
3305 case '/':
3306 {
3307 c2 = l->buf[l->i];
3308 if (c2 == '*')
3309 s = bc_lex_comment(l);
3310 else
3311 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3312 break;
3313 }
3314
3315 case '0':
3316 case '1':
3317 case '2':
3318 case '3':
3319 case '4':
3320 case '5':
3321 case '6':
3322 case '7':
3323 case '8':
3324 case '9':
3325 case 'A':
3326 case 'B':
3327 case 'C':
3328 case 'D':
3329 case 'E':
3330 case 'F':
3331 {
3332 s = bc_lex_number(l, c);
3333 break;
3334 }
3335
3336 case ';':
3337 {
3338 l->t.t = BC_LEX_SCOLON;
3339 break;
3340 }
3341
3342 case '<':
3343 {
3344 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3345 break;
3346 }
3347
3348 case '=':
3349 {
3350 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3351 break;
3352 }
3353
3354 case '>':
3355 {
3356 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3357 break;
3358 }
3359
3360 case '[':
3361 case ']':
3362 {
3363 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3364 break;
3365 }
3366
3367 case '\\':
3368 {
3369 if (l->buf[l->i] == '\n') {
3370 l->t.t = BC_LEX_WHITESPACE;
3371 ++l->i;
3372 }
3373 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003374 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003375 break;
3376 }
3377
3378 case '^':
3379 {
3380 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3381 break;
3382 }
3383
3384 case 'a':
3385 case 'b':
3386 case 'c':
3387 case 'd':
3388 case 'e':
3389 case 'f':
3390 case 'g':
3391 case 'h':
3392 case 'i':
3393 case 'j':
3394 case 'k':
3395 case 'l':
3396 case 'm':
3397 case 'n':
3398 case 'o':
3399 case 'p':
3400 case 'q':
3401 case 'r':
3402 case 's':
3403 case 't':
3404 case 'u':
3405 case 'v':
3406 case 'w':
3407 case 'x':
3408 case 'y':
3409 case 'z':
3410 {
3411 s = bc_lex_identifier(l);
3412 break;
3413 }
3414
3415 case '{':
3416 case '}':
3417 {
3418 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3419 break;
3420 }
3421
3422 case '|':
3423 {
3424 c2 = l->buf[l->i];
3425
3426 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003427 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Gavin Howard01055ba2018-11-03 11:00:21 -06003428 if (s) return s;
3429
3430 ++l->i;
3431 l->t.t = BC_LEX_OP_BOOL_OR;
3432 }
3433 else {
3434 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003435 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003436 }
3437
3438 break;
3439 }
3440
3441 default:
3442 {
3443 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003444 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003445 break;
3446 }
3447 }
3448
3449 return s;
3450}
3451#endif // ENABLE_BC
3452
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003453#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06003454static BcStatus dc_lex_register(BcLex *l)
3455{
3456 BcStatus s = BC_STATUS_SUCCESS;
3457
3458 if (isspace(l->buf[l->i - 1])) {
3459 bc_lex_whitespace(l);
3460 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003461 if (!G_exreg)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003462 s = bc_error("extended register");
Gavin Howard01055ba2018-11-03 11:00:21 -06003463 else
3464 s = bc_lex_name(l);
3465 }
3466 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003467 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003468 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003469 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003470 l->t.t = BC_LEX_NAME;
3471 }
3472
3473 return s;
3474}
3475
3476static BcStatus dc_lex_string(BcLex *l)
3477{
3478 size_t depth = 1, nls = 0, i = l->i;
3479 char c;
3480
3481 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003482 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003483
3484 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3485
3486 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3487 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3488 nls += (c == '\n');
3489
3490 if (depth) bc_vec_push(&l->t.v, &c);
3491 }
3492
3493 if (c == '\0') {
3494 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003495 return bc_error("string end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003496 }
3497
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003498 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003499 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3500 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3501 if (i - l->i > BC_MAX_STRING)
3502 return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]");
3503 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003504
3505 l->i = i;
3506 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003507 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003508
3509 return BC_STATUS_SUCCESS;
3510}
3511
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003512static FAST_FUNC BcStatus dc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003513{
3514 BcStatus s = BC_STATUS_SUCCESS;
3515 char c = l->buf[l->i++], c2;
3516 size_t i;
3517
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003518 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3519 if (l->t.last == dc_lex_regs[i])
3520 return dc_lex_register(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003521 }
3522
3523 if (c >= '%' && c <= '~' &&
3524 (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID)
3525 {
3526 return s;
3527 }
3528
3529 // This is the workhorse of the lexer.
3530 switch (c) {
3531
3532 case '\0':
3533 {
3534 l->t.t = BC_LEX_EOF;
3535 break;
3536 }
3537
3538 case '\n':
3539 case '\t':
3540 case '\v':
3541 case '\f':
3542 case '\r':
3543 case ' ':
3544 {
3545 l->newline = (c == '\n');
3546 bc_lex_whitespace(l);
3547 break;
3548 }
3549
3550 case '!':
3551 {
3552 c2 = l->buf[l->i];
3553
3554 if (c2 == '=')
3555 l->t.t = BC_LEX_OP_REL_NE;
3556 else if (c2 == '<')
3557 l->t.t = BC_LEX_OP_REL_LE;
3558 else if (c2 == '>')
3559 l->t.t = BC_LEX_OP_REL_GE;
3560 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003561 return bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003562
3563 ++l->i;
3564 break;
3565 }
3566
3567 case '#':
3568 {
3569 bc_lex_lineComment(l);
3570 break;
3571 }
3572
3573 case '.':
3574 {
3575 if (isdigit(l->buf[l->i]))
3576 s = bc_lex_number(l, c);
3577 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003578 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003579 break;
3580 }
3581
3582 case '0':
3583 case '1':
3584 case '2':
3585 case '3':
3586 case '4':
3587 case '5':
3588 case '6':
3589 case '7':
3590 case '8':
3591 case '9':
3592 case 'A':
3593 case 'B':
3594 case 'C':
3595 case 'D':
3596 case 'E':
3597 case 'F':
3598 {
3599 s = bc_lex_number(l, c);
3600 break;
3601 }
3602
3603 case '[':
3604 {
3605 s = dc_lex_string(l);
3606 break;
3607 }
3608
3609 default:
3610 {
3611 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003612 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003613 break;
3614 }
3615 }
3616
3617 return s;
3618}
3619#endif // ENABLE_DC
3620
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003621static void bc_program_addFunc(char *name, size_t *idx);
3622
Gavin Howard01055ba2018-11-03 11:00:21 -06003623static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3624{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003625 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003626 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003627}
3628
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003629#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3630
Gavin Howard01055ba2018-11-03 11:00:21 -06003631static void bc_parse_pushName(BcParse *p, char *name)
3632{
3633 size_t i = 0, len = strlen(name);
3634
3635 for (; i < len; ++i) bc_parse_push(p, name[i]);
3636 bc_parse_push(p, BC_PARSE_STREND);
3637
3638 free(name);
3639}
3640
3641static void bc_parse_pushIndex(BcParse *p, size_t idx)
3642{
3643 unsigned char amt, i, nums[sizeof(size_t)];
3644
3645 for (amt = 0; idx; ++amt) {
3646 nums[amt] = (char) idx;
3647 idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT;
3648 }
3649
3650 bc_parse_push(p, amt);
3651 for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]);
3652}
3653
3654static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3655{
3656 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003657 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003658
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003659 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003660
3661 bc_parse_push(p, BC_INST_NUM);
3662 bc_parse_pushIndex(p, idx);
3663
3664 ++(*nexs);
3665 (*prev) = BC_INST_NUM;
3666}
3667
3668static BcStatus bc_parse_text(BcParse *p, const char *text)
3669{
3670 BcStatus s;
3671
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003672 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003673
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003674 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003675 p->l.t.t = BC_LEX_INVALID;
3676 s = p->parse(p);
3677 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003678 if (!BC_PARSE_CAN_EXEC(p))
3679 return bc_error("file is not executable");
Gavin Howard01055ba2018-11-03 11:00:21 -06003680 }
3681
3682 return bc_lex_text(&p->l, text);
3683}
3684
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003685// Called when parsing or execution detects a failure,
3686// resets execution structures.
3687static void bc_program_reset(void)
3688{
3689 BcFunc *f;
3690 BcInstPtr *ip;
3691
3692 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3693 bc_vec_pop_all(&G.prog.results);
3694
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003695 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003696 ip = bc_vec_top(&G.prog.stack);
3697 ip->idx = f->code.len;
3698}
3699
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003700#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003701 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003702
Denys Vlasenkod38af482018-12-04 19:11:02 +01003703// Called when bc/dc_parse_parse() detects a failure,
3704// resets parsing structures.
3705static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003706{
3707 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003708 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003709 bc_vec_pop_all(&p->func->code);
3710 bc_vec_pop_all(&p->func->autos);
3711 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003712
3713 bc_parse_updateFunc(p, BC_PROG_MAIN);
3714 }
3715
3716 p->l.i = p->l.len;
3717 p->l.t.t = BC_LEX_EOF;
3718 p->auto_part = (p->nbraces = 0);
3719
3720 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003721 bc_vec_pop_all(&p->exits);
3722 bc_vec_pop_all(&p->conds);
3723 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003724
Denys Vlasenkod38af482018-12-04 19:11:02 +01003725 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003726}
3727
3728static void bc_parse_free(BcParse *p)
3729{
3730 bc_vec_free(&p->flags);
3731 bc_vec_free(&p->exits);
3732 bc_vec_free(&p->conds);
3733 bc_vec_free(&p->ops);
3734 bc_lex_free(&p->l);
3735}
3736
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003737static void bc_parse_create(BcParse *p, size_t func,
Gavin Howard01055ba2018-11-03 11:00:21 -06003738 BcParseParse parse, BcLexNext next)
3739{
3740 memset(p, 0, sizeof(BcParse));
3741
3742 bc_lex_init(&p->l, next);
3743 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3744 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3745 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003746 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003747 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3748
3749 p->parse = parse;
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003750 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003751 bc_parse_updateFunc(p, func);
3752}
3753
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003754#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003755
3756#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3757#define BC_PARSE_LEAF(p, rparen) \
3758 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3759 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3760
3761// We can calculate the conversion between tokens and exprs by subtracting the
3762// position of the first operator in the lex enum and adding the position of the
3763// first in the expr enum. Note: This only works for binary operators.
3764#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3765
Gavin Howard01055ba2018-11-03 11:00:21 -06003766static BcStatus bc_parse_else(BcParse *p);
3767static BcStatus bc_parse_stmt(BcParse *p);
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003768static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003769static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Gavin Howard01055ba2018-11-03 11:00:21 -06003770
3771static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3772 size_t *nexprs, bool next)
3773{
3774 BcStatus s = BC_STATUS_SUCCESS;
3775 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003776 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3777 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003778
3779 while (p->ops.len > start) {
3780
3781 t = BC_PARSE_TOP_OP(p);
3782 if (t == BC_LEX_LPAREN) break;
3783
Denys Vlasenko65437582018-12-05 19:37:19 +01003784 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003785 if (l >= r && (l != r || !left)) break;
3786
3787 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3788 bc_vec_pop(&p->ops);
3789 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3790 }
3791
3792 bc_vec_push(&p->ops, &type);
3793 if (next) s = bc_lex_next(&p->l);
3794
3795 return s;
3796}
3797
3798static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
3799{
3800 BcLexType top;
3801
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003802 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003803 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003804 top = BC_PARSE_TOP_OP(p);
3805
3806 while (top != BC_LEX_LPAREN) {
3807
3808 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3809
3810 bc_vec_pop(&p->ops);
3811 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3812
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003813 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003814 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003815 top = BC_PARSE_TOP_OP(p);
3816 }
3817
3818 bc_vec_pop(&p->ops);
3819
3820 return bc_lex_next(&p->l);
3821}
3822
3823static BcStatus bc_parse_params(BcParse *p, uint8_t flags)
3824{
3825 BcStatus s;
3826 bool comma = false;
3827 size_t nparams;
3828
3829 s = bc_lex_next(&p->l);
3830 if (s) return s;
3831
3832 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
3833
3834 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3835 s = bc_parse_expr(p, flags, bc_parse_next_param);
3836 if (s) return s;
3837
3838 comma = p->l.t.t == BC_LEX_COMMA;
3839 if (comma) {
3840 s = bc_lex_next(&p->l);
3841 if (s) return s;
3842 }
3843 }
3844
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003845 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003846 bc_parse_push(p, BC_INST_CALL);
3847 bc_parse_pushIndex(p, nparams);
3848
3849 return BC_STATUS_SUCCESS;
3850}
3851
3852static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags)
3853{
3854 BcStatus s;
3855 BcId entry, *entry_ptr;
3856 size_t idx;
3857
3858 entry.name = name;
3859
3860 s = bc_parse_params(p, flags);
3861 if (s) goto err;
3862
3863 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003864 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003865 goto err;
3866 }
3867
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003868 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003869
3870 if (idx == BC_VEC_INVALID_IDX) {
3871 name = xstrdup(entry.name);
3872 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003873 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003874 free(entry.name);
3875 }
3876 else
3877 free(name);
3878
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003879 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003880 bc_parse_pushIndex(p, entry_ptr->idx);
3881
3882 return bc_lex_next(&p->l);
3883
3884err:
3885 free(name);
3886 return s;
3887}
3888
3889static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
3890{
3891 BcStatus s;
3892 char *name;
3893
3894 name = xstrdup(p->l.t.v.v);
3895 s = bc_lex_next(&p->l);
3896 if (s) goto err;
3897
3898 if (p->l.t.t == BC_LEX_LBRACKET) {
3899
3900 s = bc_lex_next(&p->l);
3901 if (s) goto err;
3902
3903 if (p->l.t.t == BC_LEX_RBRACKET) {
3904
3905 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003906 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003907 goto err;
3908 }
3909
3910 *type = BC_INST_ARRAY;
3911 }
3912 else {
3913
3914 *type = BC_INST_ARRAY_ELEM;
3915
3916 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3917 s = bc_parse_expr(p, flags, bc_parse_next_elem);
3918 if (s) goto err;
3919 }
3920
3921 s = bc_lex_next(&p->l);
3922 if (s) goto err;
3923 bc_parse_push(p, *type);
3924 bc_parse_pushName(p, name);
3925 }
3926 else if (p->l.t.t == BC_LEX_LPAREN) {
3927
3928 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003929 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003930 goto err;
3931 }
3932
3933 *type = BC_INST_CALL;
3934 s = bc_parse_call(p, name, flags);
3935 }
3936 else {
3937 *type = BC_INST_VAR;
3938 bc_parse_push(p, BC_INST_VAR);
3939 bc_parse_pushName(p, name);
3940 }
3941
3942 return s;
3943
3944err:
3945 free(name);
3946 return s;
3947}
3948
3949static BcStatus bc_parse_read(BcParse *p)
3950{
3951 BcStatus s;
3952
3953 s = bc_lex_next(&p->l);
3954 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003955 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003956
3957 s = bc_lex_next(&p->l);
3958 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003959 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003960
3961 bc_parse_push(p, BC_INST_READ);
3962
3963 return bc_lex_next(&p->l);
3964}
3965
3966static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
3967 BcInst *prev)
3968{
3969 BcStatus s;
3970
3971 s = bc_lex_next(&p->l);
3972 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003973 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003974
3975 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3976
3977 s = bc_lex_next(&p->l);
3978 if (s) return s;
3979
3980 s = bc_parse_expr(p, flags, bc_parse_next_rel);
3981 if (s) return s;
3982
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003983 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003984
3985 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3986 bc_parse_push(p, *prev);
3987
3988 return bc_lex_next(&p->l);
3989}
3990
3991static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
3992{
3993 BcStatus s;
3994
3995 s = bc_lex_next(&p->l);
3996 if (s) return s;
3997
3998 if (p->l.t.t != BC_LEX_LPAREN) {
3999 *type = BC_INST_SCALE;
4000 bc_parse_push(p, BC_INST_SCALE);
4001 return BC_STATUS_SUCCESS;
4002 }
4003
4004 *type = BC_INST_SCALE_FUNC;
4005 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
4006
4007 s = bc_lex_next(&p->l);
4008 if (s) return s;
4009
4010 s = bc_parse_expr(p, flags, bc_parse_next_rel);
4011 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004012 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004013 bc_parse_push(p, BC_INST_SCALE_FUNC);
4014
4015 return bc_lex_next(&p->l);
4016}
4017
4018static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
4019 size_t *nexprs, uint8_t flags)
4020{
4021 BcStatus s;
4022 BcLexType type;
4023 char inst;
4024 BcInst etype = *prev;
4025
4026 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
4027 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
4028 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
4029 {
4030 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
4031 bc_parse_push(p, inst);
4032 s = bc_lex_next(&p->l);
4033 }
4034 else {
4035
4036 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
4037 *paren_expr = true;
4038
4039 s = bc_lex_next(&p->l);
4040 if (s) return s;
4041 type = p->l.t.t;
4042
4043 // Because we parse the next part of the expression
4044 // right here, we need to increment this.
4045 *nexprs = *nexprs + 1;
4046
4047 switch (type) {
4048
4049 case BC_LEX_NAME:
4050 {
4051 s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
4052 break;
4053 }
4054
4055 case BC_LEX_KEY_IBASE:
4056 case BC_LEX_KEY_LAST:
4057 case BC_LEX_KEY_OBASE:
4058 {
4059 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4060 s = bc_lex_next(&p->l);
4061 break;
4062 }
4063
4064 case BC_LEX_KEY_SCALE:
4065 {
4066 s = bc_lex_next(&p->l);
4067 if (s) return s;
4068 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004069 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004070 else
4071 bc_parse_push(p, BC_INST_SCALE);
4072 break;
4073 }
4074
4075 default:
4076 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004077 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004078 break;
4079 }
4080 }
4081
4082 if (!s) bc_parse_push(p, inst);
4083 }
4084
4085 return s;
4086}
4087
4088static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
4089 bool rparen, size_t *nexprs)
4090{
4091 BcStatus s;
4092 BcLexType type;
4093 BcInst etype = *prev;
4094
4095 s = bc_lex_next(&p->l);
4096 if (s) return s;
4097
4098 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4099 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4100 BC_LEX_OP_MINUS :
4101 BC_LEX_NEG;
4102 *prev = BC_PARSE_TOKEN_INST(type);
4103
4104 // We can just push onto the op stack because this is the largest
4105 // precedence operator that gets pushed. Inc/dec does not.
4106 if (type != BC_LEX_OP_MINUS)
4107 bc_vec_push(&p->ops, &type);
4108 else
4109 s = bc_parse_operator(p, type, ops_bgn, nexprs, false);
4110
4111 return s;
4112}
4113
4114static BcStatus bc_parse_string(BcParse *p, char inst)
4115{
4116 char *str = xstrdup(p->l.t.v.v);
4117
4118 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004119 bc_parse_pushIndex(p, G.prog.strs.len);
4120 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004121 bc_parse_push(p, inst);
4122
4123 return bc_lex_next(&p->l);
4124}
4125
4126static BcStatus bc_parse_print(BcParse *p)
4127{
4128 BcStatus s;
4129 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004130 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06004131
4132 s = bc_lex_next(&p->l);
4133 if (s) return s;
4134
4135 type = p->l.t.t;
4136
4137 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004138 return bc_error("bad print statement");
Gavin Howard01055ba2018-11-03 11:00:21 -06004139
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004140 comma = false;
4141 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004142
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004143 if (type == BC_LEX_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004144 s = bc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004145 if (s) return s;
4146 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004147 s = bc_parse_expr(p, 0, bc_parse_next_print);
4148 if (s) return s;
4149 bc_parse_push(p, BC_INST_PRINT_POP);
4150 }
4151
Gavin Howard01055ba2018-11-03 11:00:21 -06004152 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004153 if (comma) {
4154 s = bc_lex_next(&p->l);
4155 if (s) return s;
4156 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004157 type = p->l.t.t;
4158 }
4159
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004160 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004161
4162 return bc_lex_next(&p->l);
4163}
4164
4165static BcStatus bc_parse_return(BcParse *p)
4166{
4167 BcStatus s;
4168 BcLexType t;
4169 bool paren;
4170
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004171 if (!BC_PARSE_FUNC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004172
4173 s = bc_lex_next(&p->l);
4174 if (s) return s;
4175
4176 t = p->l.t.t;
4177 paren = t == BC_LEX_LPAREN;
4178
4179 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4180 bc_parse_push(p, BC_INST_RET0);
4181 else {
4182
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004183 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004184 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004185 bc_parse_push(p, BC_INST_RET0);
4186 s = bc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004187 }
Denys Vlasenko452df922018-12-05 20:28:26 +01004188 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004189
4190 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004191 s = bc_POSIX_requires("parentheses around return expressions");
Gavin Howard01055ba2018-11-03 11:00:21 -06004192 if (s) return s;
4193 }
4194
4195 bc_parse_push(p, BC_INST_RET);
4196 }
4197
4198 return s;
4199}
4200
4201static BcStatus bc_parse_endBody(BcParse *p, bool brace)
4202{
4203 BcStatus s = BC_STATUS_SUCCESS;
4204
4205 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004206 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004207
4208 if (brace) {
4209
4210 if (p->l.t.t == BC_LEX_RBRACE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004211 if (!p->nbraces) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004212 --p->nbraces;
4213 s = bc_lex_next(&p->l);
4214 if (s) return s;
4215 }
4216 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004217 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004218 }
4219
4220 if (BC_PARSE_IF(p)) {
4221
4222 uint8_t *flag_ptr;
4223
4224 while (p->l.t.t == BC_LEX_NLINE) {
4225 s = bc_lex_next(&p->l);
4226 if (s) return s;
4227 }
4228
4229 bc_vec_pop(&p->flags);
4230
4231 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4232 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4233
4234 if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p);
4235 }
4236 else if (BC_PARSE_ELSE(p)) {
4237
4238 BcInstPtr *ip;
4239 size_t *label;
4240
4241 bc_vec_pop(&p->flags);
4242
4243 ip = bc_vec_top(&p->exits);
4244 label = bc_vec_item(&p->func->labels, ip->idx);
4245 *label = p->func->code.len;
4246
4247 bc_vec_pop(&p->exits);
4248 }
4249 else if (BC_PARSE_FUNC_INNER(p)) {
4250 bc_parse_push(p, BC_INST_RET0);
4251 bc_parse_updateFunc(p, BC_PROG_MAIN);
4252 bc_vec_pop(&p->flags);
4253 }
4254 else {
4255
4256 BcInstPtr *ip = bc_vec_top(&p->exits);
4257 size_t *label = bc_vec_top(&p->conds);
4258
4259 bc_parse_push(p, BC_INST_JUMP);
4260 bc_parse_pushIndex(p, *label);
4261
4262 label = bc_vec_item(&p->func->labels, ip->idx);
4263 *label = p->func->code.len;
4264
4265 bc_vec_pop(&p->flags);
4266 bc_vec_pop(&p->exits);
4267 bc_vec_pop(&p->conds);
4268 }
4269
4270 return s;
4271}
4272
4273static void bc_parse_startBody(BcParse *p, uint8_t flags)
4274{
4275 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4276 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4277 flags |= BC_PARSE_FLAG_BODY;
4278 bc_vec_push(&p->flags, &flags);
4279}
4280
4281static void bc_parse_noElse(BcParse *p)
4282{
4283 BcInstPtr *ip;
4284 size_t *label;
4285 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4286
4287 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4288
4289 ip = bc_vec_top(&p->exits);
4290 label = bc_vec_item(&p->func->labels, ip->idx);
4291 *label = p->func->code.len;
4292
4293 bc_vec_pop(&p->exits);
4294}
4295
4296static BcStatus bc_parse_if(BcParse *p)
4297{
4298 BcStatus s;
4299 BcInstPtr ip;
4300
4301 s = bc_lex_next(&p->l);
4302 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004303 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004304
4305 s = bc_lex_next(&p->l);
4306 if (s) return s;
4307 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4308 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004309 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004310
4311 s = bc_lex_next(&p->l);
4312 if (s) return s;
4313 bc_parse_push(p, BC_INST_JUMP_ZERO);
4314
4315 ip.idx = p->func->labels.len;
4316 ip.func = ip.len = 0;
4317
4318 bc_parse_pushIndex(p, ip.idx);
4319 bc_vec_push(&p->exits, &ip);
4320 bc_vec_push(&p->func->labels, &ip.idx);
4321 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4322
4323 return BC_STATUS_SUCCESS;
4324}
4325
4326static BcStatus bc_parse_else(BcParse *p)
4327{
4328 BcInstPtr ip;
4329
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004330 if (!BC_PARSE_IF_END(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004331
4332 ip.idx = p->func->labels.len;
4333 ip.func = ip.len = 0;
4334
4335 bc_parse_push(p, BC_INST_JUMP);
4336 bc_parse_pushIndex(p, ip.idx);
4337
4338 bc_parse_noElse(p);
4339
4340 bc_vec_push(&p->exits, &ip);
4341 bc_vec_push(&p->func->labels, &ip.idx);
4342 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4343
4344 return bc_lex_next(&p->l);
4345}
4346
4347static BcStatus bc_parse_while(BcParse *p)
4348{
4349 BcStatus s;
4350 BcInstPtr ip;
4351
4352 s = bc_lex_next(&p->l);
4353 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004354 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004355 s = bc_lex_next(&p->l);
4356 if (s) return s;
4357
4358 ip.idx = p->func->labels.len;
4359
4360 bc_vec_push(&p->func->labels, &p->func->code.len);
4361 bc_vec_push(&p->conds, &ip.idx);
4362
4363 ip.idx = p->func->labels.len;
4364 ip.func = 1;
4365 ip.len = 0;
4366
4367 bc_vec_push(&p->exits, &ip);
4368 bc_vec_push(&p->func->labels, &ip.idx);
4369
4370 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4371 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004372 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004373 s = bc_lex_next(&p->l);
4374 if (s) return s;
4375
4376 bc_parse_push(p, BC_INST_JUMP_ZERO);
4377 bc_parse_pushIndex(p, ip.idx);
4378 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4379
4380 return BC_STATUS_SUCCESS;
4381}
4382
4383static BcStatus bc_parse_for(BcParse *p)
4384{
4385 BcStatus s;
4386 BcInstPtr ip;
4387 size_t cond_idx, exit_idx, body_idx, update_idx;
4388
4389 s = bc_lex_next(&p->l);
4390 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004391 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004392 s = bc_lex_next(&p->l);
4393 if (s) return s;
4394
4395 if (p->l.t.t != BC_LEX_SCOLON)
4396 s = bc_parse_expr(p, 0, bc_parse_next_for);
4397 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004398 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004399
4400 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004401 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004402 s = bc_lex_next(&p->l);
4403 if (s) return s;
4404
4405 cond_idx = p->func->labels.len;
4406 update_idx = cond_idx + 1;
4407 body_idx = update_idx + 1;
4408 exit_idx = body_idx + 1;
4409
4410 bc_vec_push(&p->func->labels, &p->func->code.len);
4411
4412 if (p->l.t.t != BC_LEX_SCOLON)
4413 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
4414 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004415 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004416
4417 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004418 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004419
4420 s = bc_lex_next(&p->l);
4421 if (s) return s;
4422
4423 bc_parse_push(p, BC_INST_JUMP_ZERO);
4424 bc_parse_pushIndex(p, exit_idx);
4425 bc_parse_push(p, BC_INST_JUMP);
4426 bc_parse_pushIndex(p, body_idx);
4427
4428 ip.idx = p->func->labels.len;
4429
4430 bc_vec_push(&p->conds, &update_idx);
4431 bc_vec_push(&p->func->labels, &p->func->code.len);
4432
4433 if (p->l.t.t != BC_LEX_RPAREN)
4434 s = bc_parse_expr(p, 0, bc_parse_next_rel);
4435 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004436 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004437
4438 if (s) return s;
4439
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004440 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004441 bc_parse_push(p, BC_INST_JUMP);
4442 bc_parse_pushIndex(p, cond_idx);
4443 bc_vec_push(&p->func->labels, &p->func->code.len);
4444
4445 ip.idx = exit_idx;
4446 ip.func = 1;
4447 ip.len = 0;
4448
4449 bc_vec_push(&p->exits, &ip);
4450 bc_vec_push(&p->func->labels, &ip.idx);
4451 bc_lex_next(&p->l);
4452 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4453
4454 return BC_STATUS_SUCCESS;
4455}
4456
4457static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type)
4458{
4459 BcStatus s;
4460 size_t i;
4461 BcInstPtr *ip;
4462
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004463 if (!BC_PARSE_LOOP(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004464
4465 if (type == BC_LEX_KEY_BREAK) {
4466
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004467 if (p->exits.len == 0) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004468
4469 i = p->exits.len - 1;
4470 ip = bc_vec_item(&p->exits, i);
4471
4472 while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004473 if (i >= p->exits.len && !ip->func) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004474
4475 i = ip->idx;
4476 }
4477 else
4478 i = *((size_t *) bc_vec_top(&p->conds));
4479
4480 bc_parse_push(p, BC_INST_JUMP);
4481 bc_parse_pushIndex(p, i);
4482
4483 s = bc_lex_next(&p->l);
4484 if (s) return s;
4485
4486 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004487 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004488
4489 return bc_lex_next(&p->l);
4490}
4491
4492static BcStatus bc_parse_func(BcParse *p)
4493{
4494 BcStatus s;
4495 bool var, comma = false;
4496 uint8_t flags;
4497 char *name;
4498
4499 s = bc_lex_next(&p->l);
4500 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004501 if (p->l.t.t != BC_LEX_NAME)
4502 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004503
4504 name = xstrdup(p->l.t.v.v);
4505 bc_parse_addFunc(p, name, &p->fidx);
4506
4507 s = bc_lex_next(&p->l);
4508 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004509 if (p->l.t.t != BC_LEX_LPAREN)
4510 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004511 s = bc_lex_next(&p->l);
4512 if (s) return s;
4513
4514 while (p->l.t.t != BC_LEX_RPAREN) {
4515
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004516 if (p->l.t.t != BC_LEX_NAME)
4517 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004518
4519 ++p->func->nparams;
4520
4521 name = xstrdup(p->l.t.v.v);
4522 s = bc_lex_next(&p->l);
4523 if (s) goto err;
4524
4525 var = p->l.t.t != BC_LEX_LBRACKET;
4526
4527 if (!var) {
4528
4529 s = bc_lex_next(&p->l);
4530 if (s) goto err;
4531
4532 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004533 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004534 goto err;
4535 }
4536
4537 s = bc_lex_next(&p->l);
4538 if (s) goto err;
4539 }
4540
4541 comma = p->l.t.t == BC_LEX_COMMA;
4542 if (comma) {
4543 s = bc_lex_next(&p->l);
4544 if (s) goto err;
4545 }
4546
4547 s = bc_func_insert(p->func, name, var);
4548 if (s) goto err;
4549 }
4550
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004551 if (comma) return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004552
4553 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4554 bc_parse_startBody(p, flags);
4555
4556 s = bc_lex_next(&p->l);
4557 if (s) return s;
4558
4559 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004560 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004561
4562 return s;
4563
4564err:
4565 free(name);
4566 return s;
4567}
4568
4569static BcStatus bc_parse_auto(BcParse *p)
4570{
4571 BcStatus s;
4572 bool comma, var, one;
4573 char *name;
4574
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004575 if (!p->auto_part) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 s = bc_lex_next(&p->l);
4577 if (s) return s;
4578
4579 p->auto_part = comma = false;
4580 one = p->l.t.t == BC_LEX_NAME;
4581
4582 while (p->l.t.t == BC_LEX_NAME) {
4583
4584 name = xstrdup(p->l.t.v.v);
4585 s = bc_lex_next(&p->l);
4586 if (s) goto err;
4587
4588 var = p->l.t.t != BC_LEX_LBRACKET;
4589 if (!var) {
4590
4591 s = bc_lex_next(&p->l);
4592 if (s) goto err;
4593
4594 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004595 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004596 goto err;
4597 }
4598
4599 s = bc_lex_next(&p->l);
4600 if (s) goto err;
4601 }
4602
4603 comma = p->l.t.t == BC_LEX_COMMA;
4604 if (comma) {
4605 s = bc_lex_next(&p->l);
4606 if (s) goto err;
4607 }
4608
4609 s = bc_func_insert(p->func, name, var);
4610 if (s) goto err;
4611 }
4612
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004613 if (comma) return bc_error("bad function definition");
Denys Vlasenkoabbc4332018-12-03 21:46:41 +01004614 if (!one) return bc_error("no auto variable found");
Gavin Howard01055ba2018-11-03 11:00:21 -06004615
4616 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004617 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004618
4619 return bc_lex_next(&p->l);
4620
4621err:
4622 free(name);
4623 return s;
4624}
4625
4626static BcStatus bc_parse_body(BcParse *p, bool brace)
4627{
4628 BcStatus s = BC_STATUS_SUCCESS;
4629 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4630
4631 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4632
4633 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
4634
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004635 if (!brace) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004636 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4637
4638 if (!p->auto_part) {
4639 s = bc_parse_auto(p);
4640 if (s) return s;
4641 }
4642
4643 if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l);
4644 }
4645 else {
4646 s = bc_parse_stmt(p);
4647 if (!s && !brace) s = bc_parse_endBody(p, false);
4648 }
4649
4650 return s;
4651}
4652
4653static BcStatus bc_parse_stmt(BcParse *p)
4654{
4655 BcStatus s = BC_STATUS_SUCCESS;
4656
4657 switch (p->l.t.t) {
4658
4659 case BC_LEX_NLINE:
4660 {
4661 return bc_lex_next(&p->l);
4662 }
4663
4664 case BC_LEX_KEY_ELSE:
4665 {
4666 p->auto_part = false;
4667 break;
4668 }
4669
4670 case BC_LEX_LBRACE:
4671 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004672 if (!BC_PARSE_BODY(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004673
4674 ++p->nbraces;
4675 s = bc_lex_next(&p->l);
4676 if (s) return s;
4677
4678 return bc_parse_body(p, true);
4679 }
4680
4681 case BC_LEX_KEY_AUTO:
4682 {
4683 return bc_parse_auto(p);
4684 }
4685
4686 default:
4687 {
4688 p->auto_part = false;
4689
4690 if (BC_PARSE_IF_END(p)) {
4691 bc_parse_noElse(p);
4692 return BC_STATUS_SUCCESS;
4693 }
4694 else if (BC_PARSE_BODY(p))
4695 return bc_parse_body(p, false);
4696
4697 break;
4698 }
4699 }
4700
4701 switch (p->l.t.t) {
4702
4703 case BC_LEX_OP_INC:
4704 case BC_LEX_OP_DEC:
4705 case BC_LEX_OP_MINUS:
4706 case BC_LEX_OP_BOOL_NOT:
4707 case BC_LEX_LPAREN:
4708 case BC_LEX_NAME:
4709 case BC_LEX_NUMBER:
4710 case BC_LEX_KEY_IBASE:
4711 case BC_LEX_KEY_LAST:
4712 case BC_LEX_KEY_LENGTH:
4713 case BC_LEX_KEY_OBASE:
4714 case BC_LEX_KEY_READ:
4715 case BC_LEX_KEY_SCALE:
4716 case BC_LEX_KEY_SQRT:
4717 {
4718 s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
4719 break;
4720 }
4721
4722 case BC_LEX_KEY_ELSE:
4723 {
4724 s = bc_parse_else(p);
4725 break;
4726 }
4727
4728 case BC_LEX_SCOLON:
4729 {
4730 while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l);
4731 break;
4732 }
4733
4734 case BC_LEX_RBRACE:
4735 {
4736 s = bc_parse_endBody(p, true);
4737 break;
4738 }
4739
4740 case BC_LEX_STR:
4741 {
4742 s = bc_parse_string(p, BC_INST_PRINT_STR);
4743 break;
4744 }
4745
4746 case BC_LEX_KEY_BREAK:
4747 case BC_LEX_KEY_CONTINUE:
4748 {
4749 s = bc_parse_loopExit(p, p->l.t.t);
4750 break;
4751 }
4752
4753 case BC_LEX_KEY_FOR:
4754 {
4755 s = bc_parse_for(p);
4756 break;
4757 }
4758
4759 case BC_LEX_KEY_HALT:
4760 {
4761 bc_parse_push(p, BC_INST_HALT);
4762 s = bc_lex_next(&p->l);
4763 break;
4764 }
4765
4766 case BC_LEX_KEY_IF:
4767 {
4768 s = bc_parse_if(p);
4769 break;
4770 }
4771
4772 case BC_LEX_KEY_LIMITS:
4773 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004774 // "limits" is a compile-time command,
4775 // the output is produced at _parse time_.
Gavin Howard01055ba2018-11-03 11:00:21 -06004776 s = bc_lex_next(&p->l);
4777 if (s) return s;
Denys Vlasenko64074a12018-12-07 15:50:14 +01004778 printf(
4779 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4780 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4781 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4782 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4783 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4784 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4785 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4786 "Number of vars = "BC_MAX_VARS_STR "\n"
4787 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004788 break;
4789 }
4790
4791 case BC_LEX_KEY_PRINT:
4792 {
4793 s = bc_parse_print(p);
4794 break;
4795 }
4796
4797 case BC_LEX_KEY_QUIT:
4798 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004799 // "quit" is a compile-time command. For example,
4800 // "if (0 == 1) quit" terminates when parsing the statement,
4801 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004802 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004803 }
4804
4805 case BC_LEX_KEY_RETURN:
4806 {
4807 s = bc_parse_return(p);
4808 break;
4809 }
4810
4811 case BC_LEX_KEY_WHILE:
4812 {
4813 s = bc_parse_while(p);
4814 break;
4815 }
4816
4817 default:
4818 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004819 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004820 break;
4821 }
4822 }
4823
4824 return s;
4825}
4826
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01004827static FAST_FUNC BcStatus bc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004828{
4829 BcStatus s;
4830
4831 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004832 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 -06004833 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004834 if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004835 s = bc_parse_func(p);
4836 }
4837 else
4838 s = bc_parse_stmt(p);
4839
Denys Vlasenkod38af482018-12-04 19:11:02 +01004840 if (s || G_interrupt) {
4841 bc_parse_reset(p);
4842 s = BC_STATUS_FAILURE;
4843 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004844
4845 return s;
4846}
4847
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004848static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004849{
4850 BcStatus s = BC_STATUS_SUCCESS;
4851 BcInst prev = BC_INST_PRINT;
4852 BcLexType top, t = p->l.t.t;
4853 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004854 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004855 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4856
4857 paren_first = p->l.t.t == BC_LEX_LPAREN;
4858 nparens = nrelops = 0;
4859 paren_expr = rprn = done = get_token = assign = false;
4860 bin_last = true;
4861
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004862 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004863 switch (t) {
4864
4865 case BC_LEX_OP_INC:
4866 case BC_LEX_OP_DEC:
4867 {
4868 s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
4869 rprn = get_token = bin_last = false;
4870 break;
4871 }
4872
4873 case BC_LEX_OP_MINUS:
4874 {
4875 s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
4876 rprn = get_token = false;
4877 bin_last = prev == BC_INST_MINUS;
4878 break;
4879 }
4880
4881 case BC_LEX_OP_ASSIGN_POWER:
4882 case BC_LEX_OP_ASSIGN_MULTIPLY:
4883 case BC_LEX_OP_ASSIGN_DIVIDE:
4884 case BC_LEX_OP_ASSIGN_MODULUS:
4885 case BC_LEX_OP_ASSIGN_PLUS:
4886 case BC_LEX_OP_ASSIGN_MINUS:
4887 case BC_LEX_OP_ASSIGN:
4888 {
4889 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4890 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4891 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4892 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004893 s = bc_error("bad assignment:"
4894 " left side must be scale,"
4895 " ibase, obase, last, var,"
4896 " or array element"
4897 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004898 break;
4899 }
4900 }
4901 // Fallthrough.
4902 case BC_LEX_OP_POWER:
4903 case BC_LEX_OP_MULTIPLY:
4904 case BC_LEX_OP_DIVIDE:
4905 case BC_LEX_OP_MODULUS:
4906 case BC_LEX_OP_PLUS:
4907 case BC_LEX_OP_REL_EQ:
4908 case BC_LEX_OP_REL_LE:
4909 case BC_LEX_OP_REL_GE:
4910 case BC_LEX_OP_REL_NE:
4911 case BC_LEX_OP_REL_LT:
4912 case BC_LEX_OP_REL_GT:
4913 case BC_LEX_OP_BOOL_NOT:
4914 case BC_LEX_OP_BOOL_OR:
4915 case BC_LEX_OP_BOOL_AND:
4916 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004917 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4918 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4919 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004920 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004921 }
4922
4923 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4924 prev = BC_PARSE_TOKEN_INST(t);
4925 s = bc_parse_operator(p, t, ops_bgn, &nexprs, true);
4926 rprn = get_token = false;
4927 bin_last = t != BC_LEX_OP_BOOL_NOT;
4928
4929 break;
4930 }
4931
4932 case BC_LEX_LPAREN:
4933 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004934 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004935 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004936 ++nparens;
4937 paren_expr = rprn = bin_last = false;
4938 get_token = true;
4939 bc_vec_push(&p->ops, &t);
4940
4941 break;
4942 }
4943
4944 case BC_LEX_RPAREN:
4945 {
4946 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004947 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004948
4949 if (nparens == 0) {
4950 s = BC_STATUS_SUCCESS;
4951 done = true;
4952 get_token = false;
4953 break;
4954 }
4955 else if (!paren_expr)
4956 return BC_STATUS_PARSE_EMPTY_EXP;
4957
4958 --nparens;
4959 paren_expr = rprn = true;
4960 get_token = bin_last = false;
4961
4962 s = bc_parse_rightParen(p, ops_bgn, &nexprs);
4963
4964 break;
4965 }
4966
4967 case BC_LEX_NAME:
4968 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004969 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004970 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004971 paren_expr = true;
4972 rprn = get_token = bin_last = false;
4973 s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
4974 ++nexprs;
4975
4976 break;
4977 }
4978
4979 case BC_LEX_NUMBER:
4980 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004981 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004982 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004983 bc_parse_number(p, &prev, &nexprs);
4984 paren_expr = get_token = true;
4985 rprn = bin_last = false;
4986
4987 break;
4988 }
4989
4990 case BC_LEX_KEY_IBASE:
4991 case BC_LEX_KEY_LAST:
4992 case BC_LEX_KEY_OBASE:
4993 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004994 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004995 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004996 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4997 bc_parse_push(p, (char) prev);
4998
4999 paren_expr = get_token = true;
5000 rprn = bin_last = false;
5001 ++nexprs;
5002
5003 break;
5004 }
5005
5006 case BC_LEX_KEY_LENGTH:
5007 case BC_LEX_KEY_SQRT:
5008 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005009 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005010 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005011 s = bc_parse_builtin(p, t, flags, &prev);
5012 paren_expr = true;
5013 rprn = get_token = bin_last = false;
5014 ++nexprs;
5015
5016 break;
5017 }
5018
5019 case BC_LEX_KEY_READ:
5020 {
5021 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005022 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005023 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005024 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005025 else
5026 s = bc_parse_read(p);
5027
5028 paren_expr = true;
5029 rprn = get_token = bin_last = false;
5030 ++nexprs;
5031 prev = BC_INST_READ;
5032
5033 break;
5034 }
5035
5036 case BC_LEX_KEY_SCALE:
5037 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005038 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005039 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005040 s = bc_parse_scale(p, &prev, flags);
5041 paren_expr = true;
5042 rprn = get_token = bin_last = false;
5043 ++nexprs;
5044 prev = BC_INST_SCALE;
5045
5046 break;
5047 }
5048
5049 default:
5050 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005051 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005052 break;
5053 }
5054 }
5055
5056 if (!s && get_token) s = bc_lex_next(&p->l);
5057 }
5058
5059 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01005060 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06005061
5062 while (p->ops.len > ops_bgn) {
5063
5064 top = BC_PARSE_TOP_OP(p);
5065 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
5066
5067 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005068 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005069
5070 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
5071
5072 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
5073 bc_vec_pop(&p->ops);
5074 }
5075
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005076 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005077 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005078
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005079 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
5080 for (;;) {
5081 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005082 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005083 if (next & 0x80) // last element?
5084 break;
5085 next >>= 8;
5086 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005087 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005088 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06005089
5090 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01005091 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Gavin Howard01055ba2018-11-03 11:00:21 -06005092 if (s) return s;
5093 }
5094 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01005095 s = bc_POSIX_requires("exactly one comparison operator per condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06005096 if (s) return s;
5097 }
5098
5099 if (flags & BC_PARSE_PRINT) {
5100 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
5101 bc_parse_push(p, BC_INST_POP);
5102 }
5103
5104 return s;
5105}
5106
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01005107static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
5108{
5109 BcStatus s;
5110
5111 s = bc_parse_expr_empty_ok(p, flags, next);
5112 if (s == BC_STATUS_PARSE_EMPTY_EXP)
5113 return bc_error("empty expression");
5114 return s;
5115}
5116
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005117static void bc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005118{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005119 bc_parse_create(p, func, bc_parse_parse, bc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005120}
5121
5122static BcStatus bc_parse_expression(BcParse *p, uint8_t flags)
5123{
5124 return bc_parse_expr(p, flags, bc_parse_next_read);
5125}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005126
Gavin Howard01055ba2018-11-03 11:00:21 -06005127#endif // ENABLE_BC
5128
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005129#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005130
5131#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
5132
Gavin Howard01055ba2018-11-03 11:00:21 -06005133static BcStatus dc_parse_register(BcParse *p)
5134{
5135 BcStatus s;
5136 char *name;
5137
5138 s = bc_lex_next(&p->l);
5139 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005140 if (p->l.t.t != BC_LEX_NAME) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005141
5142 name = xstrdup(p->l.t.v.v);
5143 bc_parse_pushName(p, name);
5144
5145 return s;
5146}
5147
5148static BcStatus dc_parse_string(BcParse *p)
5149{
5150 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005151 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005152
5153 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
5154 name = xstrdup(b);
5155
5156 str = xstrdup(p->l.t.v.v);
5157 bc_parse_push(p, BC_INST_STR);
5158 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005159 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005160 bc_parse_addFunc(p, name, &idx);
5161
5162 return bc_lex_next(&p->l);
5163}
5164
5165static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
5166{
5167 BcStatus s;
5168
5169 bc_parse_push(p, inst);
5170 if (name) {
5171 s = dc_parse_register(p);
5172 if (s) return s;
5173 }
5174
5175 if (store) {
5176 bc_parse_push(p, BC_INST_SWAP);
5177 bc_parse_push(p, BC_INST_ASSIGN);
5178 bc_parse_push(p, BC_INST_POP);
5179 }
5180
5181 return bc_lex_next(&p->l);
5182}
5183
5184static BcStatus dc_parse_cond(BcParse *p, uint8_t inst)
5185{
5186 BcStatus s;
5187
5188 bc_parse_push(p, inst);
5189 bc_parse_push(p, BC_INST_EXEC_COND);
5190
5191 s = dc_parse_register(p);
5192 if (s) return s;
5193
5194 s = bc_lex_next(&p->l);
5195 if (s) return s;
5196
5197 if (p->l.t.t == BC_LEX_ELSE) {
5198 s = dc_parse_register(p);
5199 if (s) return s;
5200 s = bc_lex_next(&p->l);
5201 }
5202 else
5203 bc_parse_push(p, BC_PARSE_STREND);
5204
5205 return s;
5206}
5207
5208static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
5209{
5210 BcStatus s = BC_STATUS_SUCCESS;
5211 BcInst prev;
5212 uint8_t inst;
5213 bool assign, get_token = false;
5214
5215 switch (t) {
5216
5217 case BC_LEX_OP_REL_EQ:
5218 case BC_LEX_OP_REL_LE:
5219 case BC_LEX_OP_REL_GE:
5220 case BC_LEX_OP_REL_NE:
5221 case BC_LEX_OP_REL_LT:
5222 case BC_LEX_OP_REL_GT:
5223 {
5224 s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
5225 break;
5226 }
5227
5228 case BC_LEX_SCOLON:
5229 case BC_LEX_COLON:
5230 {
5231 s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
5232 break;
5233 }
5234
5235 case BC_LEX_STR:
5236 {
5237 s = dc_parse_string(p);
5238 break;
5239 }
5240
5241 case BC_LEX_NEG:
5242 case BC_LEX_NUMBER:
5243 {
5244 if (t == BC_LEX_NEG) {
5245 s = bc_lex_next(&p->l);
5246 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005247 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005248 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005249 }
5250
5251 bc_parse_number(p, &prev, &p->nbraces);
5252
5253 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5254 get_token = true;
5255
5256 break;
5257 }
5258
5259 case BC_LEX_KEY_READ:
5260 {
5261 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005262 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005263 else
5264 bc_parse_push(p, BC_INST_READ);
5265 get_token = true;
5266 break;
5267 }
5268
5269 case BC_LEX_OP_ASSIGN:
5270 case BC_LEX_STORE_PUSH:
5271 {
5272 assign = t == BC_LEX_OP_ASSIGN;
5273 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
5274 s = dc_parse_mem(p, inst, true, assign);
5275 break;
5276 }
5277
5278 case BC_LEX_LOAD:
5279 case BC_LEX_LOAD_POP:
5280 {
5281 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
5282 s = dc_parse_mem(p, inst, true, false);
5283 break;
5284 }
5285
5286 case BC_LEX_STORE_IBASE:
5287 case BC_LEX_STORE_SCALE:
5288 case BC_LEX_STORE_OBASE:
5289 {
5290 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
5291 s = dc_parse_mem(p, inst, false, true);
5292 break;
5293 }
5294
5295 default:
5296 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005297 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005298 get_token = true;
5299 break;
5300 }
5301 }
5302
5303 if (!s && get_token) s = bc_lex_next(&p->l);
5304
5305 return s;
5306}
5307
5308static BcStatus dc_parse_expr(BcParse *p, uint8_t flags)
5309{
5310 BcStatus s = BC_STATUS_SUCCESS;
5311 BcInst inst;
5312 BcLexType t;
5313
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005314 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005315
5316 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
5317
5318 inst = dc_parse_insts[t];
5319
5320 if (inst != BC_INST_INVALID) {
5321 bc_parse_push(p, inst);
5322 s = bc_lex_next(&p->l);
5323 }
5324 else
5325 s = dc_parse_token(p, t, flags);
5326 }
5327
5328 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5329 bc_parse_push(p, BC_INST_POP_EXEC);
5330
5331 return s;
5332}
5333
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01005334static FAST_FUNC BcStatus dc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005335{
5336 BcStatus s;
5337
5338 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005339 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005340 else
5341 s = dc_parse_expr(p, 0);
5342
Denys Vlasenkod38af482018-12-04 19:11:02 +01005343 if (s || G_interrupt) {
5344 bc_parse_reset(p);
5345 s = BC_STATUS_FAILURE;
5346 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005347
5348 return s;
5349}
5350
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005351static void dc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005352{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005353 bc_parse_create(p, func, dc_parse_parse, dc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005354}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005355
Gavin Howard01055ba2018-11-03 11:00:21 -06005356#endif // ENABLE_DC
5357
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005358static void common_parse_init(BcParse *p, size_t func)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005359{
5360 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005361 IF_BC(bc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005362 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005363 IF_DC(dc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005364 }
5365}
5366
5367static BcStatus common_parse_expr(BcParse *p, uint8_t flags)
5368{
5369 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005370 IF_BC(return bc_parse_expression(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005371 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005372 IF_DC(return dc_parse_expr(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005373 }
5374}
5375
Denys Vlasenkodf515392018-12-02 19:27:48 +01005376static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005377{
Gavin Howard01055ba2018-11-03 11:00:21 -06005378 BcId e, *ptr;
5379 BcVec *v, *map;
5380 size_t i;
5381 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005382 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005383
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005384 v = var ? &G.prog.vars : &G.prog.arrs;
5385 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005386
5387 e.name = id;
5388 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005389 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005390
5391 if (new) {
5392 bc_array_init(&data.v, var);
5393 bc_vec_push(v, &data.v);
5394 }
5395
5396 ptr = bc_vec_item(map, i);
5397 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005398 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005399}
5400
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005401static BcStatus bc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005402{
5403 BcStatus s = BC_STATUS_SUCCESS;
5404
5405 switch (r->t) {
5406
5407 case BC_RESULT_STR:
5408 case BC_RESULT_TEMP:
5409 case BC_RESULT_IBASE:
5410 case BC_RESULT_SCALE:
5411 case BC_RESULT_OBASE:
5412 {
5413 *num = &r->d.n;
5414 break;
5415 }
5416
5417 case BC_RESULT_CONSTANT:
5418 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005419 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005420 size_t base_t, len = strlen(*str);
5421 BcNum *base;
5422
5423 bc_num_init(&r->d.n, len);
5424
5425 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005426 base = hex ? &G.prog.hexb : &G.prog.ib;
5427 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005428 s = bc_num_parse(&r->d.n, *str, base, base_t);
5429
5430 if (s) {
5431 bc_num_free(&r->d.n);
5432 return s;
5433 }
5434
5435 *num = &r->d.n;
5436 r->t = BC_RESULT_TEMP;
5437
5438 break;
5439 }
5440
5441 case BC_RESULT_VAR:
5442 case BC_RESULT_ARRAY:
5443 case BC_RESULT_ARRAY_ELEM:
5444 {
5445 BcVec *v;
5446
Denys Vlasenkodf515392018-12-02 19:27:48 +01005447 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005448
5449 if (r->t == BC_RESULT_ARRAY_ELEM) {
5450 v = bc_vec_top(v);
5451 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5452 *num = bc_vec_item(v, r->d.id.idx);
5453 }
5454 else
5455 *num = bc_vec_top(v);
5456
5457 break;
5458 }
5459
5460 case BC_RESULT_LAST:
5461 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005462 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005463 break;
5464 }
5465
5466 case BC_RESULT_ONE:
5467 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005468 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005469 break;
5470 }
5471 }
5472
5473 return s;
5474}
5475
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005476static BcStatus bc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005477 BcResult **r, BcNum **rn, bool assign)
5478{
5479 BcStatus s;
5480 bool hex;
5481 BcResultType lt, rt;
5482
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005483 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005484 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06005485
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005486 *r = bc_vec_item_rev(&G.prog.results, 0);
5487 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005488
5489 lt = (*l)->t;
5490 rt = (*r)->t;
5491 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5492
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005493 s = bc_program_num(*l, ln, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005494 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005495 s = bc_program_num(*r, rn, hex);
Gavin Howard01055ba2018-11-03 11:00:21 -06005496 if (s) return s;
5497
5498 // We run this again under these conditions in case any vector has been
5499 // reallocated out from under the BcNums or arrays we had.
5500 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005501 s = bc_program_num(*l, ln, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005502 if (s) return s;
5503 }
5504
5505 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005506 return bc_error_variable_is_wrong_type();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005507 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005508 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06005509
Gavin Howard01055ba2018-11-03 11:00:21 -06005510 return s;
5511}
5512
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005513static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005514{
5515 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005516 bc_vec_pop(&G.prog.results);
5517 bc_vec_pop(&G.prog.results);
5518 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005519}
5520
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005521static BcStatus bc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005522{
5523 BcStatus s;
5524
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005525 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005526 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005527 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005528
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005529 s = bc_program_num(*r, n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005530 if (s) return s;
5531
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005532 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005533 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06005534
5535 return s;
5536}
5537
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005538static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005539{
5540 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005541 bc_vec_pop(&G.prog.results);
5542 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005543}
5544
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005545static BcStatus bc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005546{
5547 BcStatus s;
5548 BcResult *opd1, *opd2, res;
5549 BcNum *n1, *n2 = NULL;
5550
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005551 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005552 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005553 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005554
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005555 s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005556 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005557 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005558
5559 return s;
5560
5561err:
5562 bc_num_free(&res.d.n);
5563 return s;
5564}
5565
Denys Vlasenko785e4b32018-12-02 17:18:52 +01005566static BcStatus bc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005567{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005568 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005569 BcStatus s;
5570 BcParse parse;
5571 BcVec buf;
5572 BcInstPtr ip;
5573 size_t i;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005574 BcFunc *f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005575
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005576 for (i = 0; i < G.prog.stack.len; ++i) {
5577 BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005578 if (ip_ptr->func == BC_PROG_READ)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005579 return bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005580 }
5581
Denys Vlasenko7d628012018-12-04 21:46:47 +01005582 bc_vec_pop_all(&f->code);
5583 bc_char_vec_init(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005584
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005585 sv_file = G.prog.file;
5586 G.prog.file = NULL;
5587
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01005588 s = bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005589 if (s) goto io_err;
5590
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005591 common_parse_init(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005592 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005593
5594 s = bc_parse_text(&parse, buf.v);
5595 if (s) goto exec_err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005596 s = common_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005597 if (s) goto exec_err;
5598
5599 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005600 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005601 goto exec_err;
5602 }
5603
5604 ip.func = BC_PROG_READ;
5605 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005606 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005607
5608 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005609 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005610
5611 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005612 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005613
5614exec_err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005615 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005616 bc_parse_free(&parse);
5617io_err:
5618 bc_vec_free(&buf);
5619 return s;
5620}
5621
5622static size_t bc_program_index(char *code, size_t *bgn)
5623{
5624 char amt = code[(*bgn)++], i = 0;
5625 size_t res = 0;
5626
5627 for (; i < amt; ++i, ++(*bgn))
5628 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5629
5630 return res;
5631}
5632
5633static char *bc_program_name(char *code, size_t *bgn)
5634{
5635 size_t i;
5636 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5637
5638 s = xmalloc(ptr - str + 1);
5639 c = code[(*bgn)++];
5640
5641 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5642 s[i] = c;
5643
5644 s[i] = '\0';
5645
5646 return s;
5647}
5648
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005649static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005650{
5651 size_t i, len = strlen(str);
5652
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005653#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005654 if (len == 0) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005655 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005656 return;
5657 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005658#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005659
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005660 for (i = 0; i < len; ++i, ++G.prog.nchars) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005661
5662 int c = str[i];
5663
5664 if (c != '\\' || i == len - 1)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005665 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005666 else {
5667
5668 c = str[++i];
5669
5670 switch (c) {
5671
5672 case 'a':
5673 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005674 bb_putchar('\a');
Gavin Howard01055ba2018-11-03 11:00:21 -06005675 break;
5676 }
5677
5678 case 'b':
5679 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005680 bb_putchar('\b');
Gavin Howard01055ba2018-11-03 11:00:21 -06005681 break;
5682 }
5683
5684 case '\\':
5685 case 'e':
5686 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005687 bb_putchar('\\');
Gavin Howard01055ba2018-11-03 11:00:21 -06005688 break;
5689 }
5690
5691 case 'f':
5692 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005693 bb_putchar('\f');
Gavin Howard01055ba2018-11-03 11:00:21 -06005694 break;
5695 }
5696
5697 case 'n':
5698 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005699 bb_putchar('\n');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005700 G.prog.nchars = SIZE_MAX;
Gavin Howard01055ba2018-11-03 11:00:21 -06005701 break;
5702 }
5703
5704 case 'r':
5705 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005706 bb_putchar('\r');
Gavin Howard01055ba2018-11-03 11:00:21 -06005707 break;
5708 }
5709
5710 case 'q':
5711 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005712 bb_putchar('"');
Gavin Howard01055ba2018-11-03 11:00:21 -06005713 break;
5714 }
5715
5716 case 't':
5717 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005718 bb_putchar('\t');
Gavin Howard01055ba2018-11-03 11:00:21 -06005719 break;
5720 }
5721
5722 default:
5723 {
5724 // Just print the backslash and following character.
Denys Vlasenko00d77792018-11-30 23:13:42 +01005725 bb_putchar('\\');
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005726 ++G.prog.nchars;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005727 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005728 break;
5729 }
5730 }
5731 }
5732 }
5733}
5734
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005735static BcStatus bc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005736{
5737 BcStatus s = BC_STATUS_SUCCESS;
5738 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005739 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005740 bool pop = inst != BC_INST_PRINT;
5741
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005742 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005743 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06005744
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005745 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005746 num = NULL; // is this NULL necessary?
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005747 s = bc_program_num(r, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005748 if (s) return s;
5749
5750 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005751 s = bc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005752 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005753 }
5754 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005755 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005756
5757 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005758 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005759
5760 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005761 for (;;) {
5762 char c = *str++;
5763 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005764 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005765 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005766 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005767 }
5768 }
5769 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005770 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005771 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005772 }
5773 }
5774
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005775 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005776
5777 return s;
5778}
5779
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005780static BcStatus bc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005781{
5782 BcStatus s;
5783 BcResult res, *ptr;
5784 BcNum *num = NULL;
5785
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005786 s = bc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005787 if (s) return s;
5788
5789 bc_num_init(&res.d.n, num->len);
5790 bc_num_copy(&res.d.n, num);
5791 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5792
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005793 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005794
5795 return s;
5796}
5797
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005798static BcStatus bc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005799{
5800 BcStatus s;
5801 BcResult *opd1, *opd2, res;
5802 BcNum *n1, *n2;
5803 bool cond = 0;
5804 ssize_t cmp;
5805
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005806 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005807 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005808 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005809
5810 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005811 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005812 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005813 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005814 else {
5815
5816 cmp = bc_num_cmp(n1, n2);
5817
5818 switch (inst) {
5819
5820 case BC_INST_REL_EQ:
5821 {
5822 cond = cmp == 0;
5823 break;
5824 }
5825
5826 case BC_INST_REL_LE:
5827 {
5828 cond = cmp <= 0;
5829 break;
5830 }
5831
5832 case BC_INST_REL_GE:
5833 {
5834 cond = cmp >= 0;
5835 break;
5836 }
5837
5838 case BC_INST_REL_NE:
5839 {
5840 cond = cmp != 0;
5841 break;
5842 }
5843
5844 case BC_INST_REL_LT:
5845 {
5846 cond = cmp < 0;
5847 break;
5848 }
5849
5850 case BC_INST_REL_GT:
5851 {
5852 cond = cmp > 0;
5853 break;
5854 }
5855 }
5856 }
5857
5858 (cond ? bc_num_one : bc_num_zero)(&res.d.n);
5859
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005860 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005861
5862 return s;
5863}
5864
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005865#if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005866static BcStatus bc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005867 bool push)
5868{
5869 BcNum n2;
5870 BcResult res;
5871
5872 memset(&n2, 0, sizeof(BcNum));
5873 n2.rdx = res.d.id.idx = r->d.id.idx;
5874 res.t = BC_RESULT_STR;
5875
5876 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005877 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005878 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06005879 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005880 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005881 }
5882
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005883 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005884
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005885 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005886 bc_vec_push(v, &n2);
5887
5888 return BC_STATUS_SUCCESS;
5889}
5890#endif // ENABLE_DC
5891
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005892static BcStatus bc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005893{
5894 BcStatus s;
5895 BcResult *ptr, r;
5896 BcVec *v;
5897 BcNum *n;
5898
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005899 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005900 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06005901
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005902 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005903 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005904 return bc_error_variable_is_wrong_type();
Denys Vlasenkodf515392018-12-02 19:27:48 +01005905 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005906
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005907#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005908 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005909 return bc_error_variable_is_wrong_type();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005910 if (ptr->t == BC_RESULT_STR) return bc_program_assignStr(ptr, v, true);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005911#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005912
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005913 s = bc_program_num(ptr, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005914 if (s) return s;
5915
5916 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005917 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005918
5919 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005920 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005921 bc_num_copy(&r.d.n, n);
5922 }
5923 else {
5924 bc_array_init(&r.d.v, true);
5925 bc_array_copy(&r.d.v, (BcVec *) n);
5926 }
5927
5928 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005929 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005930
5931 return s;
5932}
5933
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005934static BcStatus bc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005935{
5936 BcStatus s;
5937 BcResult *left, *right, res;
5938 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005939 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5940
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005941 s = bc_program_binOpPrep(&left, &l, &right, &r, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005942 if (s) return s;
5943
5944 ib = left->t == BC_RESULT_IBASE;
5945 sc = left->t == BC_RESULT_SCALE;
5946
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005947#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005948
5949 if (right->t == BC_RESULT_STR) {
5950
5951 BcVec *v;
5952
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005953 if (left->t != BC_RESULT_VAR)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005954 return bc_error_variable_is_wrong_type();
Denys Vlasenkodf515392018-12-02 19:27:48 +01005955 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005956
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005957 return bc_program_assignStr(right, v, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005958 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005959#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005960
5961 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005962 return bc_error("bad assignment:"
5963 " left side must be scale,"
5964 " ibase, obase, last, var,"
5965 " or array element"
5966 );
Gavin Howard01055ba2018-11-03 11:00:21 -06005967
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005968#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005969 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005970 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06005971
5972 if (assign)
5973 bc_num_copy(l, r);
5974 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005975 s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005976
5977 if (s) return s;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005978#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005979 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005980#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005981
5982 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005983 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005984 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5985 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5986 NULL, //can't happen //BC_RESULT_LAST
5987 NULL, //can't happen //BC_RESULT_CONSTANT
5988 NULL, //can't happen //BC_RESULT_ONE
5989 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005990 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005991 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01005992 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06005993
5994 s = bc_num_ulong(l, &val);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005995 if (s)
5996 return s;
5997 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005998 if (sc) {
5999 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006000 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006001 }
6002 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006003 if (val < BC_NUM_MIN_BASE)
6004 return bc_error(msg[s]);
Gavin Howard01055ba2018-11-03 11:00:21 -06006005 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006006 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006007 }
6008
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006009 if (val > max)
6010 return bc_error(msg[s]);
6011 if (!sc)
6012 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006013
6014 *ptr = (size_t) val;
6015 s = BC_STATUS_SUCCESS;
6016 }
6017
6018 bc_num_init(&res.d.n, l->len);
6019 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006020 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006021
6022 return s;
6023}
6024
Denys Vlasenko416ce762018-12-02 20:57:17 +01006025#if !ENABLE_DC
6026#define bc_program_pushVar(code, bgn, pop, copy) \
6027 bc_program_pushVar(code, bgn)
6028// for bc, 'pop' and 'copy' are always false
6029#endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006030static BcStatus bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006031 bool pop, bool copy)
6032{
6033 BcStatus s = BC_STATUS_SUCCESS;
6034 BcResult r;
6035 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006036
6037 r.t = BC_RESULT_VAR;
6038 r.d.id.name = name;
6039
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006040#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006041 {
6042 BcVec *v = bc_program_search(name, true);
6043 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006044
Denys Vlasenko416ce762018-12-02 20:57:17 +01006045 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006046
Denys Vlasenko416ce762018-12-02 20:57:17 +01006047 if (!BC_PROG_STACK(v, 2 - copy)) {
6048 free(name);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006049 return bc_error_stack_has_too_few_elements();
Denys Vlasenko416ce762018-12-02 20:57:17 +01006050 }
6051
Gavin Howard01055ba2018-11-03 11:00:21 -06006052 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006053 name = NULL;
6054
6055 if (!BC_PROG_STR(num)) {
6056
6057 r.t = BC_RESULT_TEMP;
6058
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006059 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006060 bc_num_copy(&r.d.n, num);
6061 }
6062 else {
6063 r.t = BC_RESULT_STR;
6064 r.d.id.idx = num->rdx;
6065 }
6066
6067 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006068 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006069 }
6070#endif // ENABLE_DC
6071
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006072 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006073
6074 return s;
6075}
6076
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006077static BcStatus bc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006078 char inst)
6079{
6080 BcStatus s = BC_STATUS_SUCCESS;
6081 BcResult r;
6082 BcNum *num;
6083
6084 r.d.id.name = bc_program_name(code, bgn);
6085
6086 if (inst == BC_INST_ARRAY) {
6087 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006088 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006089 }
6090 else {
6091
6092 BcResult *operand;
6093 unsigned long temp;
6094
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006095 s = bc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 if (s) goto err;
6097 s = bc_num_ulong(num, &temp);
6098 if (s) goto err;
6099
6100 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006101 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006102 goto err;
6103 }
6104
6105 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006106 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006107 }
6108
6109err:
6110 if (s) free(r.d.id.name);
6111 return s;
6112}
6113
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006114#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006115static BcStatus bc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006116{
6117 BcStatus s;
6118 BcResult *ptr, res, copy;
6119 BcNum *num = NULL;
6120 char inst2 = inst;
6121
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006122 s = bc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006123 if (s) return s;
6124
6125 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6126 copy.t = BC_RESULT_TEMP;
6127 bc_num_init(&copy.d.n, num->len);
6128 bc_num_copy(&copy.d.n, num);
6129 }
6130
6131 res.t = BC_RESULT_ONE;
6132 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6133 BC_INST_ASSIGN_PLUS :
6134 BC_INST_ASSIGN_MINUS;
6135
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006136 bc_vec_push(&G.prog.results, &res);
6137 bc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006138
6139 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006140 bc_vec_pop(&G.prog.results);
6141 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006142 }
6143
6144 return s;
6145}
6146
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006147static BcStatus bc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006148{
6149 BcStatus s = BC_STATUS_SUCCESS;
6150 BcInstPtr ip;
6151 size_t i, nparams = bc_program_index(code, idx);
6152 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006153 BcId *a;
6154 BcResultData param;
6155 BcResult *arg;
6156
6157 ip.idx = 0;
6158 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006159 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006160
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006161 if (func->code.len == 0) {
6162 return bc_error("undefined function");
6163 }
6164 if (nparams != func->nparams) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006165 return bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams);
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006166 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006167 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006168
6169 for (i = 0; i < nparams; ++i) {
6170
6171 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006172 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006173
6174 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006175 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006176
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006177 s = bc_program_copyToVar(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006178 if (s) return s;
6179 }
6180
6181 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006182 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006183
6184 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006185 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006186
6187 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006188 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006189 bc_vec_push(v, &param.n);
6190 }
6191 else {
6192 bc_array_init(&param.v, true);
6193 bc_vec_push(v, &param.v);
6194 }
6195 }
6196
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006197 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006198
6199 return BC_STATUS_SUCCESS;
6200}
6201
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006202static BcStatus bc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006203{
6204 BcStatus s;
6205 BcResult res;
6206 BcFunc *f;
6207 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006208 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006209
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006210 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006211 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006212
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006213 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006214 res.t = BC_RESULT_TEMP;
6215
6216 if (inst == BC_INST_RET) {
6217
6218 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006219 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006220
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006221 s = bc_program_num(operand, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006222 if (s) return s;
6223 bc_num_init(&res.d.n, num->len);
6224 bc_num_copy(&res.d.n, num);
6225 }
6226 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006227 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006228 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006229 }
6230
6231 // We need to pop arguments as well, so this takes that into account.
6232 for (i = 0; i < f->autos.len; ++i) {
6233
6234 BcVec *v;
6235 BcId *a = bc_vec_item(&f->autos, i);
6236
Denys Vlasenkodf515392018-12-02 19:27:48 +01006237 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006238 bc_vec_pop(v);
6239 }
6240
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006241 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6242 bc_vec_push(&G.prog.results, &res);
6243 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006244
6245 return BC_STATUS_SUCCESS;
6246}
6247#endif // ENABLE_BC
6248
6249static unsigned long bc_program_scale(BcNum *n)
6250{
6251 return (unsigned long) n->rdx;
6252}
6253
6254static unsigned long bc_program_len(BcNum *n)
6255{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006256 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006257
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006258 if (n->rdx != len) return len;
6259 for (;;) {
6260 if (len == 0) break;
6261 len--;
6262 if (n->num[len] != 0) break;
6263 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006264 return len;
6265}
6266
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006267static BcStatus bc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006268{
6269 BcStatus s;
6270 BcResult *opnd;
6271 BcNum *num = NULL;
6272 BcResult res;
6273 bool len = inst == BC_INST_LENGTH;
6274
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006275 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006276 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006277 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006278
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006279 s = bc_program_num(opnd, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006280 if (s) return s;
6281
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006282#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006283 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006284 return bc_error_variable_is_wrong_type();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006285#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006286
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006287 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006288
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006289 if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006290#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006291 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006292 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006293 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006294#endif
6295#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006296 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
6297
6298 char **str;
6299 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6300
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006301 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006302 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006303 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006304#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006305 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006306 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006307 }
6308
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006309 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006310
6311 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006312}
6313
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006314#if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006315static BcStatus bc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006316{
6317 BcStatus s;
6318 BcResult *opd1, *opd2, res, res2;
6319 BcNum *n1, *n2 = NULL;
6320
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006321 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006322 if (s) return s;
6323
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006324 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006325 bc_num_init(&res2.d.n, n2->len);
6326
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006327 s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006328 if (s) goto err;
6329
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006330 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006331 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006332 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006333
6334 return s;
6335
6336err:
6337 bc_num_free(&res2.d.n);
6338 bc_num_free(&res.d.n);
6339 return s;
6340}
6341
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006342static BcStatus bc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006343{
6344 BcStatus s;
6345 BcResult *r1, *r2, *r3, res;
6346 BcNum *n1, *n2, *n3;
6347
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006348 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006349 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006350 s = bc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006351 if (s) return s;
6352
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006353 r1 = bc_vec_item_rev(&G.prog.results, 2);
6354 s = bc_program_num(r1, &n1, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006355 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006356 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006357 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006358
6359 // Make sure that the values have their pointers updated, if necessary.
6360 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6361
6362 if (r1->t == r2->t) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006363 s = bc_program_num(r2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006364 if (s) return s;
6365 }
6366
6367 if (r1->t == r3->t) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006368 s = bc_program_num(r3, &n3, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006369 if (s) return s;
6370 }
6371 }
6372
6373 bc_num_init(&res.d.n, n3->len);
6374 s = bc_num_modexp(n1, n2, n3, &res.d.n);
6375 if (s) goto err;
6376
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006377 bc_vec_pop(&G.prog.results);
6378 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006379
6380 return s;
6381
6382err:
6383 bc_num_free(&res.d.n);
6384 return s;
6385}
6386
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006387static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006388{
Gavin Howard01055ba2018-11-03 11:00:21 -06006389 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006390 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006391
6392 res.t = BC_RESULT_TEMP;
6393
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006394 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006395 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006396 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006397}
6398
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006399static BcStatus bc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006400{
6401 BcStatus s;
6402 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006403 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006404 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006405 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006406 unsigned long val;
6407
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006408 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006409 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006410 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006411
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006412 num = NULL; // TODO: is this NULL needed?
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006413 s = bc_program_num(r, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006414 if (s) return s;
6415
6416 if (BC_PROG_NUM(r, num)) {
6417
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006418 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006419 bc_num_copy(&n, num);
6420 bc_num_truncate(&n, n.rdx);
6421
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006422 s = bc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006423 if (s) goto num_err;
6424 s = bc_num_ulong(&n, &val);
6425 if (s) goto num_err;
6426
6427 c = (char) val;
6428
6429 bc_num_free(&n);
6430 }
6431 else {
6432 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006433 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006434 c = str2[0];
6435 }
6436
6437 str = xmalloc(2);
6438 str[0] = c;
6439 str[1] = '\0';
6440
6441 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006442 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006443
6444 if (idx != len + BC_PROG_REQ_FUNCS) {
6445
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006446 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006447 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006448 len = idx;
6449 break;
6450 }
6451 }
6452
6453 free(str);
6454 }
6455 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006456 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006457
6458 res.t = BC_RESULT_STR;
6459 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006460 bc_vec_pop(&G.prog.results);
6461 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006462
6463 return BC_STATUS_SUCCESS;
6464
6465num_err:
6466 bc_num_free(&n);
6467 return s;
6468}
6469
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006470static BcStatus bc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006471{
6472 BcStatus s;
6473 BcResult *r;
6474 BcNum *n = NULL;
6475 size_t idx;
6476 char *str;
6477
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006478 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006479 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006480 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006481
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006482 s = bc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006483 if (s) return s;
6484
6485 if (BC_PROG_NUM(r, n))
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01006486 s = bc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006487 else {
6488 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006489 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006490 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006491 }
6492
6493 return s;
6494}
6495
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006496static BcStatus bc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006497{
6498 BcStatus s;
6499 BcResult *opnd;
6500 BcNum *num = NULL;
6501 unsigned long val;
6502
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006503 s = bc_program_prep(&opnd, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006504 if (s) return s;
6505 s = bc_num_ulong(num, &val);
6506 if (s) return s;
6507
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006508 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006509
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006510 if (G.prog.stack.len < val)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006511 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006512 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006513 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006514 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006515
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006516 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006517
6518 return s;
6519}
6520
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006521static BcStatus bc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006522 bool cond)
6523{
6524 BcStatus s = BC_STATUS_SUCCESS;
6525 BcResult *r;
6526 char **str;
6527 BcFunc *f;
6528 BcParse prs;
6529 BcInstPtr ip;
6530 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006531
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006532 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006533 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006534
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006535 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006536
6537 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006538 BcNum *n = n; // for compiler
6539 bool exec;
6540 char *name;
6541 char *then_name = bc_program_name(code, bgn);
6542 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006543
6544 if (code[*bgn] == BC_PARSE_STREND)
6545 (*bgn) += 1;
6546 else
6547 else_name = bc_program_name(code, bgn);
6548
6549 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006550 name = then_name;
6551 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006552 exec = true;
6553 name = else_name;
6554 }
6555
6556 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006557 BcVec *v;
6558 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006559 n = bc_vec_top(v);
6560 }
6561
6562 free(then_name);
6563 free(else_name);
6564
6565 if (!exec) goto exit;
6566 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006567 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006568 goto exit;
6569 }
6570
6571 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006572 } else {
6573 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006574 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006575 } else if (r->t == BC_RESULT_VAR) {
6576 BcNum *n;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006577 s = bc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006578 if (s || !BC_PROG_STR(n)) goto exit;
6579 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006580 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006581 goto exit;
6582 }
6583
6584 fidx = sidx + BC_PROG_REQ_FUNCS;
6585
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006586 str = bc_program_str(sidx);
6587 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006588
6589 if (f->code.len == 0) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006590 common_parse_init(&prs, fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006591 s = bc_parse_text(&prs, *str);
6592 if (s) goto err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01006593 s = common_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006594 if (s) goto err;
6595
6596 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006597 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006598 goto err;
6599 }
6600
6601 bc_parse_free(&prs);
6602 }
6603
6604 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006605 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006606 ip.func = fidx;
6607
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006608 bc_vec_pop(&G.prog.results);
6609 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006610
6611 return BC_STATUS_SUCCESS;
6612
6613err:
6614 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006615 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006616 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006617exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006618 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006619 return s;
6620}
6621#endif // ENABLE_DC
6622
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006623static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006624{
Gavin Howard01055ba2018-11-03 11:00:21 -06006625 BcResult res;
6626 unsigned long val;
6627
6628 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6629 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006630 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006631 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006632 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006633 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006634 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006635
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006636 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006637 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006638 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006639}
6640
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006641static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006642{
Gavin Howard01055ba2018-11-03 11:00:21 -06006643 BcId entry, *entry_ptr;
6644 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006645 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006646
6647 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006648 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006649
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006650 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6651 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006652
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006653 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006654 *idx = entry_ptr->idx;
6655
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006656 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006657
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006658 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006659
6660 // We need to reset these, so the function can be repopulated.
6661 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006662 bc_vec_pop_all(&func->autos);
6663 bc_vec_pop_all(&func->code);
6664 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006665 }
6666 else {
6667 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006668 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006669 }
6670}
6671
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006672static BcStatus bc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006673{
Gavin Howard01055ba2018-11-03 11:00:21 -06006674 BcResult r, *ptr;
6675 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006676 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006677 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006678 char *code = func->code.v;
6679 bool cond = false;
6680
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006681 while (ip->idx < func->code.len) {
6682 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006683 char inst = code[(ip->idx)++];
6684
6685 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006686#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006687 case BC_INST_JUMP_ZERO:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006688 s = bc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006689 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006690 cond = !bc_num_cmp(num, &G.prog.zero);
6691 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006692 // Fallthrough.
6693 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006694 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006695 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006696 addr = bc_vec_item(&func->labels, idx);
6697 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6698 break;
6699 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006700 case BC_INST_CALL:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006701 s = bc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006702 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006703 case BC_INST_INC_PRE:
6704 case BC_INST_DEC_PRE:
6705 case BC_INST_INC_POST:
6706 case BC_INST_DEC_POST:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006707 s = bc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006708 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006709 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006710 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006711 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006712 case BC_INST_RET:
6713 case BC_INST_RET0:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006714 s = bc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006715 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006716 case BC_INST_BOOL_OR:
6717 case BC_INST_BOOL_AND:
6718#endif // ENABLE_BC
6719 case BC_INST_REL_EQ:
6720 case BC_INST_REL_LE:
6721 case BC_INST_REL_GE:
6722 case BC_INST_REL_NE:
6723 case BC_INST_REL_LT:
6724 case BC_INST_REL_GT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006725 s = bc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006726 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006727 case BC_INST_READ:
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006728 s = bc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006729 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006730 case BC_INST_VAR:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006731 s = bc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006732 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006733 case BC_INST_ARRAY_ELEM:
6734 case BC_INST_ARRAY:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006735 s = bc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006736 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006737 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006738 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006739 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006740 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006741 case BC_INST_IBASE:
6742 case BC_INST_SCALE:
6743 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006744 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006745 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006746 case BC_INST_SCALE_FUNC:
6747 case BC_INST_LENGTH:
6748 case BC_INST_SQRT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006749 s = bc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006750 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006751 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006752 r.t = BC_RESULT_CONSTANT;
6753 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006754 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006755 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006756 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006757 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006758 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006759 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006760 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006761 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006762 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006763 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006764 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006765 case BC_INST_PRINT:
6766 case BC_INST_PRINT_POP:
6767 case BC_INST_PRINT_STR:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006768 s = bc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006769 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006770 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006771 r.t = BC_RESULT_STR;
6772 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006773 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006774 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006775 case BC_INST_POWER:
6776 case BC_INST_MULTIPLY:
6777 case BC_INST_DIVIDE:
6778 case BC_INST_MODULUS:
6779 case BC_INST_PLUS:
6780 case BC_INST_MINUS:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006781 s = bc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006782 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006783 case BC_INST_BOOL_NOT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006784 s = bc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006785 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006786 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006787 if (!bc_num_cmp(num, &G.prog.zero))
6788 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006789 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006790 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006791 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006792 case BC_INST_NEG:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006793 s = bc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006794 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006795#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006796 case BC_INST_ASSIGN_POWER:
6797 case BC_INST_ASSIGN_MULTIPLY:
6798 case BC_INST_ASSIGN_DIVIDE:
6799 case BC_INST_ASSIGN_MODULUS:
6800 case BC_INST_ASSIGN_PLUS:
6801 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006802#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006803 case BC_INST_ASSIGN:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006804 s = bc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006805 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006806#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006807 case BC_INST_MODEXP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006808 s = bc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006809 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006810 case BC_INST_DIVMOD:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006811 s = bc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006812 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006813 case BC_INST_EXECUTE:
6814 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006815 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006816 s = bc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006817 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006818 case BC_INST_PRINT_STACK: {
6819 size_t idx;
6820 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006821 s = bc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006822 if (s) break;
6823 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006824 break;
6825 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006826 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006827 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006828 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006829 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006830 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006831 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006832 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006833 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006834 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006835 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006836 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006837 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006838 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006839 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006840 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006841 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006842 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006843 ptr = bc_vec_item_rev(&G.prog.results, 0);
6844 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006845 memcpy(&r, ptr, sizeof(BcResult));
6846 memcpy(ptr, ptr2, sizeof(BcResult));
6847 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006848 break;
6849 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006850 case BC_INST_ASCIIFY:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006851 s = bc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006852 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006853 case BC_INST_PRINT_STREAM:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006854 s = bc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006855 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006856 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006857 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006858 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006859 s = bc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006860 break;
6861 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006862 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006863 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006864 s = bc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006865 free(name);
6866 break;
6867 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006868 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006869 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006870 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006871 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006872 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006873 case BC_INST_NQUIT:
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006874 s = bc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006875 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006876#endif // ENABLE_DC
6877 }
6878
Denys Vlasenkod38af482018-12-04 19:11:02 +01006879 if (s || G_interrupt) {
6880 bc_program_reset();
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006881 return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01006882 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006883
6884 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006885 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006886 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006887 code = func->code.v;
6888 }
6889
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006890 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006891}
6892
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006893#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01006894static void bc_vm_info(void)
6895{
6896 printf("%s "BB_VER"\n"
6897 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01006898 , applet_name);
6899}
6900
6901static void bc_args(char **argv)
6902{
6903 unsigned opts;
6904 int i;
6905
6906 GETOPT_RESET();
6907#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006908 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01006909 "warn\0" No_argument "w"
6910 "version\0" No_argument "v"
6911 "standard\0" No_argument "s"
6912 "quiet\0" No_argument "q"
6913 "mathlib\0" No_argument "l"
6914 "interactive\0" No_argument "i"
6915 );
6916#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006917 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01006918#endif
6919 if (getenv("POSIXLY_CORRECT"))
6920 option_mask32 |= BC_FLAG_S;
6921
Denys Vlasenko54214c32018-12-06 09:07:06 +01006922 if (opts & BC_FLAG_V) {
6923 bc_vm_info();
6924 exit(0);
6925 }
6926
6927 for (i = optind; argv[i]; ++i)
6928 bc_vec_push(&G.files, argv + i);
6929}
6930
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006931static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006932{
Gavin Howard01055ba2018-11-03 11:00:21 -06006933 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006934 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01006935 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06006936
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01006937 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06006938
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006939 G.env_args = xstrdup(env_args);
6940 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06006941
6942 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006943
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006944 while (*(buf = skip_whitespace(buf)) != '\0') {
6945 bc_vec_push(&v, &buf);
6946 buf = skip_non_whitespace(buf);
6947 if (!*buf)
6948 break;
6949 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06006950 }
6951
Denys Vlasenko54214c32018-12-06 09:07:06 +01006952 // NULL terminate, and pass argv[] so that first arg is argv[1]
6953 if (sizeof(int) == sizeof(char*)) {
6954 bc_vec_push(&v, &const_int_0);
6955 } else {
6956 static char *const nullptr = NULL;
6957 bc_vec_push(&v, &nullptr);
6958 }
6959 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006960
6961 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006962}
6963#endif // ENABLE_BC
6964
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006965static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006966{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006967 char *lenv;
6968 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006969
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006970 lenv = getenv(var);
6971 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006972 if (!lenv) return len;
6973
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006974 len = bb_strtou(lenv, NULL, 10) - 1;
6975 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006976 len = BC_NUM_PRINT_WIDTH;
6977
6978 return len;
6979}
6980
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006981static BcStatus bc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006982{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006983 BcStatus s = bc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06006984
Gavin Howard01055ba2018-11-03 11:00:21 -06006985 if (s) return s;
6986
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006987 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006988 s = G.prs.parse(&G.prs);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006989 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006990 }
6991
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006992 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006993 s = bc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01006994 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01006995 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01006996 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06006997 }
6998
6999 return s;
7000}
7001
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007002static BcStatus bc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007003{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007004 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007005 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007006 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007007 BcFunc *main_func;
7008 BcInstPtr *ip;
7009
Denys Vlasenkodf515392018-12-02 19:27:48 +01007010 data = bc_read_file(file);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007011 if (!data) return bc_error_fmt("file '%s' is not text", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007012
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007013 sv_file = G.prog.file;
7014 G.prog.file = file;
7015 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007016 s = bc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007017 if (s) goto err;
7018
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007019 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007020 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007021
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007022 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007023 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007024
7025err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007026 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007027 free(data);
7028 return s;
7029}
7030
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007031static BcStatus bc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007032{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007033 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007034 BcVec buf, buffer;
Gavin Howard01055ba2018-11-03 11:00:21 -06007035 size_t len, i, str = 0;
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007036 bool comment = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06007037
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007038 G.prog.file = NULL;
7039 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007040
Denys Vlasenko7d628012018-12-04 21:46:47 +01007041 bc_char_vec_init(&buffer);
7042 bc_char_vec_init(&buf);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01007043 bc_vec_pushZeroByte(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007044
7045 // This loop is complex because the vm tries not to send any lines that end
7046 // with a backslash to the parser. The reason for that is because the parser
7047 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7048 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007049 while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007050
7051 char *string = buf.v;
7052
7053 len = buf.len - 1;
7054
7055 if (len == 1) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007056 if (str && buf.v[0] == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007057 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007058 else if (buf.v[0] == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007059 str += 1;
7060 }
7061 else if (len > 1 || comment) {
7062
7063 for (i = 0; i < len; ++i) {
7064
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007065 bool notend = len > i + 1;
7066 char c = string[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06007067
7068 if (i - 1 > len || string[i - 1] != '\\') {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007069 if (G.sbgn == G.send)
7070 str ^= c == G.sbgn;
7071 else if (c == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007072 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007073 else if (c == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007074 str += 1;
7075 }
7076
7077 if (c == '/' && notend && !comment && string[i + 1] == '*') {
7078 comment = true;
7079 break;
7080 }
7081 else if (c == '*' && notend && comment && string[i + 1] == '/')
7082 comment = false;
7083 }
7084
7085 if (str || comment || string[len - 2] == '\\') {
7086 bc_vec_concat(&buffer, buf.v);
7087 continue;
7088 }
7089 }
7090
7091 bc_vec_concat(&buffer, buf.v);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007092 s = bc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007093 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007094 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007095 // Debug config, non-interactive mode:
7096 // return all the way back to main.
7097 // Non-debug builds do not come here, they exit.
7098 break;
7099 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007100 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007101
Denys Vlasenko7d628012018-12-04 21:46:47 +01007102 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007103 }
Denys Vlasenkof522dd92018-12-07 16:35:43 +01007104 if (s == BC_STATUS_EOF) // input EOF (^D) is not an error
7105 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007106
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007107 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007108 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007109 }
7110 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007111 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007112 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007113
Gavin Howard01055ba2018-11-03 11:00:21 -06007114 bc_vec_free(&buf);
7115 bc_vec_free(&buffer);
7116 return s;
7117}
7118
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007119#if ENABLE_BC
7120static const char bc_lib[] = {
7121 "scale=20"
7122"\n" "define e(x){"
7123"\n" "auto b,s,n,r,d,i,p,f,v"
7124"\n" "b=ibase"
7125"\n" "ibase=A"
7126"\n" "if(x<0){"
7127"\n" "n=1"
7128"\n" "x=-x"
7129"\n" "}"
7130"\n" "s=scale"
7131"\n" "r=6+s+0.44*x"
7132"\n" "scale=scale(x)+1"
7133"\n" "while(x>1){"
7134"\n" "d+=1"
7135"\n" "x/=2"
7136"\n" "scale+=1"
7137"\n" "}"
7138"\n" "scale=r"
7139"\n" "r=x+1"
7140"\n" "p=x"
7141"\n" "f=v=1"
7142"\n" "for(i=2;v!=0;++i){"
7143"\n" "p*=x"
7144"\n" "f*=i"
7145"\n" "v=p/f"
7146"\n" "r+=v"
7147"\n" "}"
7148"\n" "while((d--)!=0)r*=r"
7149"\n" "scale=s"
7150"\n" "ibase=b"
7151"\n" "if(n!=0)return(1/r)"
7152"\n" "return(r/1)"
7153"\n" "}"
7154"\n" "define l(x){"
7155"\n" "auto b,s,r,p,a,q,i,v"
7156"\n" "b=ibase"
7157"\n" "ibase=A"
7158"\n" "if(x<=0){"
7159"\n" "r=(1-10^scale)/1"
7160"\n" "ibase=b"
7161"\n" "return(r)"
7162"\n" "}"
7163"\n" "s=scale"
7164"\n" "scale+=6"
7165"\n" "p=2"
7166"\n" "while(x>=2){"
7167"\n" "p*=2"
7168"\n" "x=sqrt(x)"
7169"\n" "}"
7170"\n" "while(x<=0.5){"
7171"\n" "p*=2"
7172"\n" "x=sqrt(x)"
7173"\n" "}"
7174"\n" "r=a=(x-1)/(x+1)"
7175"\n" "q=a*a"
7176"\n" "v=1"
7177"\n" "for(i=3;v!=0;i+=2){"
7178"\n" "a*=q"
7179"\n" "v=a/i"
7180"\n" "r+=v"
7181"\n" "}"
7182"\n" "r*=p"
7183"\n" "scale=s"
7184"\n" "ibase=b"
7185"\n" "return(r/1)"
7186"\n" "}"
7187"\n" "define s(x){"
7188"\n" "auto b,s,r,n,a,q,i"
7189"\n" "b=ibase"
7190"\n" "ibase=A"
7191"\n" "s=scale"
7192"\n" "scale=1.1*s+2"
7193"\n" "a=a(1)"
7194"\n" "if(x<0){"
7195"\n" "n=1"
7196"\n" "x=-x"
7197"\n" "}"
7198"\n" "scale=0"
7199"\n" "q=(x/a+2)/4"
7200"\n" "x=x-4*q*a"
7201"\n" "if(q%2!=0)x=-x"
7202"\n" "scale=s+2"
7203"\n" "r=a=x"
7204"\n" "q=-x*x"
7205"\n" "for(i=3;a!=0;i+=2){"
7206"\n" "a*=q/(i*(i-1))"
7207"\n" "r+=a"
7208"\n" "}"
7209"\n" "scale=s"
7210"\n" "ibase=b"
7211"\n" "if(n!=0)return(-r/1)"
7212"\n" "return(r/1)"
7213"\n" "}"
7214"\n" "define c(x){"
7215"\n" "auto b,s"
7216"\n" "b=ibase"
7217"\n" "ibase=A"
7218"\n" "s=scale"
7219"\n" "scale*=1.2"
7220"\n" "x=s(2*a(1)+x)"
7221"\n" "scale=s"
7222"\n" "ibase=b"
7223"\n" "return(x/1)"
7224"\n" "}"
7225"\n" "define a(x){"
7226"\n" "auto b,s,r,n,a,m,t,f,i,u"
7227"\n" "b=ibase"
7228"\n" "ibase=A"
7229"\n" "n=1"
7230"\n" "if(x<0){"
7231"\n" "n=-1"
7232"\n" "x=-x"
7233"\n" "}"
7234"\n" "if(x==1){"
7235"\n" "if(scale<65){"
7236"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7237"\n" "}"
7238"\n" "}"
7239"\n" "if(x==.2){"
7240"\n" "if(scale<65){"
7241"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7242"\n" "}"
7243"\n" "}"
7244"\n" "s=scale"
7245"\n" "if(x>.2){"
7246"\n" "scale+=5"
7247"\n" "a=a(.2)"
7248"\n" "}"
7249"\n" "scale=s+3"
7250"\n" "while(x>.2){"
7251"\n" "m+=1"
7252"\n" "x=(x-.2)/(1+.2*x)"
7253"\n" "}"
7254"\n" "r=u=x"
7255"\n" "f=-x*x"
7256"\n" "t=1"
7257"\n" "for(i=3;t!=0;i+=2){"
7258"\n" "u*=f"
7259"\n" "t=u/i"
7260"\n" "r+=t"
7261"\n" "}"
7262"\n" "scale=s"
7263"\n" "ibase=b"
7264"\n" "return((m*a+r)/n)"
7265"\n" "}"
7266"\n" "define j(n,x){"
7267"\n" "auto b,s,o,a,i,v,f"
7268"\n" "b=ibase"
7269"\n" "ibase=A"
7270"\n" "s=scale"
7271"\n" "scale=0"
7272"\n" "n/=1"
7273"\n" "if(n<0){"
7274"\n" "n=-n"
7275"\n" "if(n%2==1)o=1"
7276"\n" "}"
7277"\n" "a=1"
7278"\n" "for(i=2;i<=n;++i)a*=i"
7279"\n" "scale=1.5*s"
7280"\n" "a=(x^n)/2^n/a"
7281"\n" "r=v=1"
7282"\n" "f=-x*x/4"
7283"\n" "scale=scale+length(a)-scale(a)"
7284"\n" "for(i=1;v!=0;++i){"
7285"\n" "v=v*f/i/(n+i)"
7286"\n" "r+=v"
7287"\n" "}"
7288"\n" "scale=s"
7289"\n" "ibase=b"
7290"\n" "if(o!=0)a=-a"
7291"\n" "return(a*r/1)"
7292"\n" "}"
7293};
7294#endif // ENABLE_BC
7295
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007296static BcStatus bc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007297{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007298 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007299 size_t i;
7300
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007301#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007302 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007303
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007304 // We know that internal library is not buggy,
7305 // thus error checking is normally disabled.
7306# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007307 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007308 s = bc_parse_text(&G.prs, bc_lib);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007309 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007310
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007311 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007312 s = G.prs.parse(&G.prs);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007313 if (DEBUG_LIB && s) return s;
7314 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007315 s = bc_program_exec();
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007316 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007317 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007318#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007319
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007320 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007321 for (i = 0; !s && i < G.files.len; ++i)
7322 s = bc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007323 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7324 // Debug config, non-interactive mode:
7325 // return all the way back to main.
7326 // Non-debug builds do not come here, they exit.
7327 return s;
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007328 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007329
Denys Vlasenkoac6ed112018-12-08 21:39:10 +01007330 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007331 s = bc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007332
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007333 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
7334 s = bc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007335
Denys Vlasenko00d77792018-11-30 23:13:42 +01007336 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007337}
7338
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007339#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007340static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007341{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007342 bc_num_free(&G.prog.ib);
7343 bc_num_free(&G.prog.ob);
7344 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007345# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007346 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007347# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007348 bc_vec_free(&G.prog.fns);
7349 bc_vec_free(&G.prog.fn_map);
7350 bc_vec_free(&G.prog.vars);
7351 bc_vec_free(&G.prog.var_map);
7352 bc_vec_free(&G.prog.arrs);
7353 bc_vec_free(&G.prog.arr_map);
7354 bc_vec_free(&G.prog.strs);
7355 bc_vec_free(&G.prog.consts);
7356 bc_vec_free(&G.prog.results);
7357 bc_vec_free(&G.prog.stack);
7358 bc_num_free(&G.prog.last);
7359 bc_num_free(&G.prog.zero);
7360 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007361}
7362
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007363static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007364{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007365 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007366 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007367 bc_parse_free(&G.prs);
7368 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007369}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007370#endif
7371
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007372static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007373{
7374 size_t idx;
7375 BcInstPtr ip;
7376
7377 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7378 memset(&ip, 0, sizeof(BcInstPtr));
7379
7380 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007381 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007382 bc_num_ten(&G.prog.ib);
7383 G.prog.ib_t = 10;
7384
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007385 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007386 bc_num_ten(&G.prog.ob);
7387 G.prog.ob_t = 10;
7388
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007389 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007390 bc_num_ten(&G.prog.hexb);
7391 G.prog.hexb.num[0] = 6;
7392
7393#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007394 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007395 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7396#endif
7397
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007398 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007399 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007400
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007401 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007402 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007403
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007404 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007405 bc_num_one(&G.prog.one);
7406
7407 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007408 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007409
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007410 bc_program_addFunc(xstrdup("(main)"), &idx);
7411 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007412
7413 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007414 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007415
7416 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007417 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007418
7419 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7420 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7421 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7422 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7423 bc_vec_push(&G.prog.stack, &ip);
7424}
Gavin Howard01055ba2018-11-03 11:00:21 -06007425
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007426static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007427{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007428#if ENABLE_FEATURE_EDITING
7429 G.line_input_state = new_line_input_t(DO_HISTORY);
7430#endif
7431 G.prog.len = bc_vm_envLen(env_len);
7432
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007433 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007434 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007435 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007436 bc_program_init();
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007437 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007438 IF_BC(bc_parse_init(&G.prs, BC_PROG_MAIN);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007439 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007440 IF_DC(dc_parse_init(&G.prs, BC_PROG_MAIN);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007441 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007442
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007443 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007444#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007445 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007446 // With SA_RESTART, most system calls will restart
7447 // (IOW: they won't fail with EINTR).
7448 // In particular, this means ^C won't cause
7449 // stdout to get into "error state" if SIGINT hits
7450 // within write() syscall.
7451 // The downside is that ^C while line input is taken
7452 // will only be handled after [Enter] since read()
7453 // from stdin is not interrupted by ^C either,
7454 // it restarts, thus fgetc() does not return on ^C.
7455 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7456
7457 // Without SA_RESTART, this exhibits a bug:
7458 // "while (1) print 1" and try ^C-ing it.
7459 // Intermittently, instead of returning to input line,
7460 // you'll get "output error: Interrupted system call"
7461 // and exit.
7462 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007463#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007464 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007465 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007466 return 0; // "not a tty"
7467}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007468
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007469static BcStatus bc_vm_run(void)
7470{
7471 BcStatus st = bc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007472#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007473 if (G_exiting) // it was actually "halt" or "quit"
7474 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007475 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007476# if ENABLE_FEATURE_EDITING
7477 free_line_input_t(G.line_input_state);
7478# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007479 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007480#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007481 return st;
7482}
7483
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007484#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007485int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007486int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007487{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007488 int is_tty;
7489
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007490 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007491 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007492
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007493 is_tty = bc_vm_init("BC_LINE_LENGTH");
7494
7495 bc_args(argv);
7496
7497 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7498 bc_vm_info();
7499
7500 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007501}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007502#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007503
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007504#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007505int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007506int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007507{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007508 int noscript;
7509
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007510 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007511 G.sbgn = '[';
7512 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007513 /*
7514 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7515 * 1 char wider than bc from the same package.
7516 * Both default width, and xC_LINE_LENGTH=N are wider:
7517 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7518 * 1234\
7519 * 56
7520 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7521 * 123\
7522 * 456
7523 * Do the same, or it's a bug?
7524 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007525 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007526
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007527 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7528 noscript = BC_FLAG_I;
7529 for (;;) {
7530 int n = getopt(argc, argv, "e:f:x");
7531 if (n <= 0)
7532 break;
7533 switch (n) {
7534 case 'e':
7535 noscript = 0;
7536 n = bc_vm_process(optarg);
7537 if (n) return n;
7538 break;
7539 case 'f':
7540 noscript = 0;
7541 bc_vm_file(optarg);
7542 break;
7543 case 'x':
7544 option_mask32 |= DC_FLAG_X;
7545 break;
7546 default:
7547 bb_show_usage();
7548 }
7549 }
7550 argv += optind;
7551
7552 while (*argv) {
7553 noscript = 0;
7554 bc_vec_push(&G.files, argv++);
7555 }
7556
7557 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7558
7559 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007560}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007561#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007562
7563#endif // not DC_SMALL