blob: 5f1baf43178889a7f44b83af57b86fc718504a98 [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 * Copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06005 */
6//config:config BC
7//config: bool "bc (45 kb; 49 kb when combined with dc)"
8//config: default y
9//config: help
10//config: bc is a command-line, arbitrary-precision calculator with a
11//config: Turing-complete language. See the GNU bc manual
12//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
13//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
14//config: for details.
15//config:
16//config: This bc has four differences to the GNU bc:
17//config:
18//config: 1) The period (.) can also be used as a shortcut for "last", as in
19//config: the BSD bc.
20//config: 2) Arrays are copied before being passed as arguments to
21//config: functions. This behavior is required by the bc spec.
22//config: 3) Arrays can be passed to the builtin "length" function to get
23//config: the number of elements currently in the array. The following
24//config: example prints "1":
25//config:
26//config: a[0] = 0
27//config: length(a[])
28//config:
29//config: 4) The precedence of the boolean "not" operator (!) is equal to
30//config: that of the unary minus (-), or negation, operator. This still
31//config: allows POSIX-compliant scripts to work while somewhat
32//config: preserving expected behavior (versus C) and making parsing
33//config: easier.
34//config:
35//config: Options:
36//config:
37//config: -i --interactive force interactive mode
38//config: -l --mathlib use predefined math routines:
39//config:
40//config: s(expr) = sine of expr in radians
41//config: c(expr) = cosine of expr in radians
42//config: a(expr) = arctangent of expr, returning
43//config: radians
44//config: l(expr) = natural log of expr
45//config: e(expr) = raises e to the power of expr
46//config: j(n, x) = Bessel function of integer order
47//config: n of x
48//config:
49//config: -q --quiet don't print version and copyright.
50//config: -s --standard error if any non-POSIX extensions are used.
51//config: -w --warn warn if any non-POSIX extensions are used.
52//config: -v --version print version and copyright and exit.
53//config:
54//config: Long options are only available if FEATURE_BC_LONG_OPTIONS is
55//config: enabled.
56//config:
57//config:config DC
58//config: bool "dc (38 kb; 49 kb when combined with bc)"
59//config: default y
60//config: help
61//config: dc is a reverse-polish notation command-line calculator which
62//config: supports unlimited precision arithmetic. See the FreeBSD man page
63//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
64//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html)
65//config: for details.
66//config:
67//config: This dc has a few differences from the two above:
68//config:
69//config: 1) When printing a byte stream (command "P"), this bc follows what
70//config: the FreeBSD dc does.
71//config: 2) This dc implements the GNU extensions for divmod ("~") and
72//config: modular exponentiation ("|").
73//config: 3) This dc implements all FreeBSD extensions, except for "J" and
74//config: "M".
75//config: 4) Like the FreeBSD dc, this dc supports extended registers.
76//config: However, they are implemented differently. When it encounters
77//config: whitespace where a register should be, it skips the whitespace.
78//config: If the character following is not a lowercase letter, an error
79//config: is issued. Otherwise, the register name is parsed by the
80//config: following regex:
81//config:
82//config: [a-z][a-z0-9_]*
83//config:
84//config: This generally means that register names will be surrounded by
85//config: whitespace.
86//config:
87//config: Examples:
88//config:
89//config: l idx s temp L index S temp2 < do_thing
90//config:
91//config: Also note that, like the FreeBSD dc, extended registers are not
92//config: allowed unless the "-x" option is given.
93//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010094//config:config FEATURE_DC_SMALL
95//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
96//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010097//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010098//config:
99//config:config FEATURE_DC_LIBM
100//config: bool "Enable power and exp functions (requires libm)"
101//config: default y
102//config: depends on FEATURE_DC_SMALL
103//config: help
104//config: Enable power and exp functions.
105//config: NOTE: This will require libm to be present for linking.
106//config:
Gavin Howard01055ba2018-11-03 11:00:21 -0600107//config:config FEATURE_BC_SIGNALS
108//config: bool "Enable bc/dc signal handling"
109//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100110//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600111//config: help
112//config: Enable signal handling for bc and dc.
113//config:
114//config:config FEATURE_BC_LONG_OPTIONS
115//config: bool "Enable bc/dc long options"
116//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100117//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600118//config: help
119//config: Enable long options for bc and dc.
120
121//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
122//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
123
124//kbuild:lib-$(CONFIG_BC) += bc.o
125//kbuild:lib-$(CONFIG_DC) += bc.o
126
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100127//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600128//usage:#define bc_trivial_usage
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100129//usage: "[-sqliw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600130//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100131//usage:#define bc_full_usage "\n"
132//usage: "\nArbitrary precision calculator"
133//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100134///////: "\n -i Interactive" - has no effect for now
135//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100136//usage: "\n -l Load standard math library"
137//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100138//usage: "\n -w Warn if extensions are used"
139///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100140//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100141//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600142//usage:
143//usage:#define bc_example_usage
144//usage: "3 + 4.129\n"
145//usage: "1903 - 2893\n"
146//usage: "-129 * 213.28935\n"
147//usage: "12 / -1932\n"
148//usage: "12 % 12\n"
149//usage: "34 ^ 189\n"
150//usage: "scale = 13\n"
151//usage: "ibase = 2\n"
152//usage: "obase = A\n"
153//usage:
154//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100155//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600156//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100157//usage:#define dc_full_usage "\n"
158//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100159//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "\np - print top of the stack (without popping)"
161//usage: "\nf - print entire stack"
162//usage: "\nk - pop the value and set the precision"
163//usage: "\ni - pop the value and set input radix"
164//usage: "\no - pop the value and set output radix"
165//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600166//usage:
167//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100168//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600169//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100170//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600171//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100172//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600173//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100174//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600175//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100176//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600177//usage: "64\n"
178
179#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100180#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600181
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100182#if ENABLE_FEATURE_DC_SMALL
183# include "dc.c"
184#else
185
Gavin Howard01055ba2018-11-03 11:00:21 -0600186typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100187 BC_STATUS_SUCCESS = 0,
188 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100189 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Denys Vlasenkof522dd92018-12-07 16:35:43 +0100190 BC_STATUS_EOF = 3, // bc_vm_stdin() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600191} BcStatus;
192
Gavin Howard01055ba2018-11-03 11:00:21 -0600193#define BC_VEC_INVALID_IDX ((size_t) -1)
194#define BC_VEC_START_CAP (1 << 5)
195
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100196typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600197
198typedef struct BcVec {
199 char *v;
200 size_t len;
201 size_t cap;
202 size_t size;
203 BcVecFree dtor;
204} BcVec;
205
Gavin Howard01055ba2018-11-03 11:00:21 -0600206typedef signed char BcDig;
207
208typedef struct BcNum {
209 BcDig *restrict num;
210 size_t rdx;
211 size_t len;
212 size_t cap;
213 bool neg;
214} BcNum;
215
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100216#define BC_NUM_MIN_BASE ((unsigned long) 2)
217#define BC_NUM_MAX_IBASE ((unsigned long) 16)
218// larger value might speed up BIGNUM calculations a bit:
219#define BC_NUM_DEF_SIZE (16)
220#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600221
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100222#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600223
Gavin Howard01055ba2018-11-03 11:00:21 -0600224typedef enum BcInst {
225
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100226#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600227 BC_INST_INC_PRE,
228 BC_INST_DEC_PRE,
229 BC_INST_INC_POST,
230 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100231#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600232
233 BC_INST_NEG,
234
235 BC_INST_POWER,
236 BC_INST_MULTIPLY,
237 BC_INST_DIVIDE,
238 BC_INST_MODULUS,
239 BC_INST_PLUS,
240 BC_INST_MINUS,
241
242 BC_INST_REL_EQ,
243 BC_INST_REL_LE,
244 BC_INST_REL_GE,
245 BC_INST_REL_NE,
246 BC_INST_REL_LT,
247 BC_INST_REL_GT,
248
249 BC_INST_BOOL_NOT,
250 BC_INST_BOOL_OR,
251 BC_INST_BOOL_AND,
252
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100253#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600254 BC_INST_ASSIGN_POWER,
255 BC_INST_ASSIGN_MULTIPLY,
256 BC_INST_ASSIGN_DIVIDE,
257 BC_INST_ASSIGN_MODULUS,
258 BC_INST_ASSIGN_PLUS,
259 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100260#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600261 BC_INST_ASSIGN,
262
263 BC_INST_NUM,
264 BC_INST_VAR,
265 BC_INST_ARRAY_ELEM,
266 BC_INST_ARRAY,
267
268 BC_INST_SCALE_FUNC,
269 BC_INST_IBASE,
270 BC_INST_SCALE,
271 BC_INST_LAST,
272 BC_INST_LENGTH,
273 BC_INST_READ,
274 BC_INST_OBASE,
275 BC_INST_SQRT,
276
277 BC_INST_PRINT,
278 BC_INST_PRINT_POP,
279 BC_INST_STR,
280 BC_INST_PRINT_STR,
281
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100282#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600283 BC_INST_JUMP,
284 BC_INST_JUMP_ZERO,
285
286 BC_INST_CALL,
287
288 BC_INST_RET,
289 BC_INST_RET0,
290
291 BC_INST_HALT,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100292#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600293
294 BC_INST_POP,
295 BC_INST_POP_EXEC,
296
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100297#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600298 BC_INST_MODEXP,
299 BC_INST_DIVMOD,
300
301 BC_INST_EXECUTE,
302 BC_INST_EXEC_COND,
303
304 BC_INST_ASCIIFY,
305 BC_INST_PRINT_STREAM,
306
307 BC_INST_PRINT_STACK,
308 BC_INST_CLEAR_STACK,
309 BC_INST_STACK_LEN,
310 BC_INST_DUPLICATE,
311 BC_INST_SWAP,
312
313 BC_INST_LOAD,
314 BC_INST_PUSH_VAR,
315 BC_INST_PUSH_TO_VAR,
316
317 BC_INST_QUIT,
318 BC_INST_NQUIT,
319
320 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100321#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600322
323} BcInst;
324
325typedef struct BcId {
326 char *name;
327 size_t idx;
328} BcId;
329
330typedef struct BcFunc {
331 BcVec code;
332 BcVec labels;
333 size_t nparams;
334 BcVec autos;
335} BcFunc;
336
337typedef enum BcResultType {
338
339 BC_RESULT_TEMP,
340
341 BC_RESULT_VAR,
342 BC_RESULT_ARRAY_ELEM,
343 BC_RESULT_ARRAY,
344
345 BC_RESULT_STR,
346
347 BC_RESULT_IBASE,
348 BC_RESULT_SCALE,
349 BC_RESULT_LAST,
350
351 // These are between to calculate ibase, obase, and last from instructions.
352 BC_RESULT_CONSTANT,
353 BC_RESULT_ONE,
354
355 BC_RESULT_OBASE,
356
357} BcResultType;
358
359typedef union BcResultData {
360 BcNum n;
361 BcVec v;
362 BcId id;
363} BcResultData;
364
365typedef struct BcResult {
366 BcResultType t;
367 BcResultData d;
368} BcResult;
369
370typedef struct BcInstPtr {
371 size_t func;
372 size_t idx;
373 size_t len;
374} BcInstPtr;
375
Gavin Howard01055ba2018-11-03 11:00:21 -0600376// BC_LEX_NEG is not used in lexing; it is only for parsing.
377typedef enum BcLexType {
378
379 BC_LEX_EOF,
380 BC_LEX_INVALID,
381
382 BC_LEX_OP_INC,
383 BC_LEX_OP_DEC,
384
385 BC_LEX_NEG,
386
387 BC_LEX_OP_POWER,
388 BC_LEX_OP_MULTIPLY,
389 BC_LEX_OP_DIVIDE,
390 BC_LEX_OP_MODULUS,
391 BC_LEX_OP_PLUS,
392 BC_LEX_OP_MINUS,
393
394 BC_LEX_OP_REL_EQ,
395 BC_LEX_OP_REL_LE,
396 BC_LEX_OP_REL_GE,
397 BC_LEX_OP_REL_NE,
398 BC_LEX_OP_REL_LT,
399 BC_LEX_OP_REL_GT,
400
401 BC_LEX_OP_BOOL_NOT,
402 BC_LEX_OP_BOOL_OR,
403 BC_LEX_OP_BOOL_AND,
404
405 BC_LEX_OP_ASSIGN_POWER,
406 BC_LEX_OP_ASSIGN_MULTIPLY,
407 BC_LEX_OP_ASSIGN_DIVIDE,
408 BC_LEX_OP_ASSIGN_MODULUS,
409 BC_LEX_OP_ASSIGN_PLUS,
410 BC_LEX_OP_ASSIGN_MINUS,
411 BC_LEX_OP_ASSIGN,
412
413 BC_LEX_NLINE,
414 BC_LEX_WHITESPACE,
415
416 BC_LEX_LPAREN,
417 BC_LEX_RPAREN,
418
419 BC_LEX_LBRACKET,
420 BC_LEX_COMMA,
421 BC_LEX_RBRACKET,
422
423 BC_LEX_LBRACE,
424 BC_LEX_SCOLON,
425 BC_LEX_RBRACE,
426
427 BC_LEX_STR,
428 BC_LEX_NAME,
429 BC_LEX_NUMBER,
430
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100431 BC_LEX_KEY_1st_keyword,
432 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600433 BC_LEX_KEY_BREAK,
434 BC_LEX_KEY_CONTINUE,
435 BC_LEX_KEY_DEFINE,
436 BC_LEX_KEY_ELSE,
437 BC_LEX_KEY_FOR,
438 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100439 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
440 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600441 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100442 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600443 BC_LEX_KEY_LENGTH,
444 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100445 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600446 BC_LEX_KEY_PRINT,
447 BC_LEX_KEY_QUIT,
448 BC_LEX_KEY_READ,
449 BC_LEX_KEY_RETURN,
450 BC_LEX_KEY_SCALE,
451 BC_LEX_KEY_SQRT,
452 BC_LEX_KEY_WHILE,
453
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100454#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600455 BC_LEX_EQ_NO_REG,
456 BC_LEX_OP_MODEXP,
457 BC_LEX_OP_DIVMOD,
458
459 BC_LEX_COLON,
460 BC_LEX_ELSE,
461 BC_LEX_EXECUTE,
462 BC_LEX_PRINT_STACK,
463 BC_LEX_CLEAR_STACK,
464 BC_LEX_STACK_LEVEL,
465 BC_LEX_DUPLICATE,
466 BC_LEX_SWAP,
467 BC_LEX_POP,
468
469 BC_LEX_ASCIIFY,
470 BC_LEX_PRINT_STREAM,
471
472 BC_LEX_STORE_IBASE,
473 BC_LEX_STORE_SCALE,
474 BC_LEX_LOAD,
475 BC_LEX_LOAD_POP,
476 BC_LEX_STORE_PUSH,
477 BC_LEX_STORE_OBASE,
478 BC_LEX_PRINT_POP,
479 BC_LEX_NQUIT,
480 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100481#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600482} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100483// must match order of BC_LEX_KEY_foo etc above
484#if ENABLE_BC
485struct BcLexKeyword {
486 char name8[8];
487};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100488#define BC_LEX_KW_ENTRY(a, b) \
489 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100490static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100491 BC_LEX_KW_ENTRY("auto" , 1), // 0
492 BC_LEX_KW_ENTRY("break" , 1), // 1
493 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
494 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100495
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100496 BC_LEX_KW_ENTRY("else" , 0), // 4
497 BC_LEX_KW_ENTRY("for" , 1), // 5
498 BC_LEX_KW_ENTRY("halt" , 0), // 6
499 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100500
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100501 BC_LEX_KW_ENTRY("if" , 1), // 8
502 BC_LEX_KW_ENTRY("last" , 0), // 9
503 BC_LEX_KW_ENTRY("length" , 1), // 10
504 BC_LEX_KW_ENTRY("limits" , 0), // 11
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100505
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100506 BC_LEX_KW_ENTRY("obase" , 1), // 12
507 BC_LEX_KW_ENTRY("print" , 0), // 13
508 BC_LEX_KW_ENTRY("quit" , 1), // 14
509 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100510
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100511 BC_LEX_KW_ENTRY("return" , 1), // 16
512 BC_LEX_KW_ENTRY("scale" , 1), // 17
513 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
514 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100515};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100516#undef BC_LEX_KW_ENTRY
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100517enum {
518 POSIX_KWORD_MASK = 0
519 | (1 << 0)
520 | (1 << 1)
521 | (0 << 2)
522 | (1 << 3)
523 \
524 | (0 << 4)
525 | (1 << 5)
526 | (0 << 6)
527 | (1 << 7)
528 \
529 | (1 << 8)
530 | (0 << 9)
531 | (1 << 10)
532 | (0 << 11)
533 \
534 | (1 << 12)
535 | (0 << 13)
536 | (1 << 14)
537 | (0 << 15)
538 \
539 | (1 << 16)
540 | (1 << 17)
541 | (1 << 18)
542 | (1 << 19)
543};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100544#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100545#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600546
547struct BcLex;
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100548typedef BcStatus (*BcLexNext)(struct BcLex *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600549
550typedef struct BcLex {
551
552 const char *buf;
553 size_t i;
554 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600555 size_t len;
556 bool newline;
557
558 struct {
559 BcLexType t;
560 BcLexType last;
561 BcVec v;
562 } t;
563
564 BcLexNext next;
565
566} BcLex;
567
568#define BC_PARSE_STREND ((char) UCHAR_MAX)
569
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100570#define BC_PARSE_REL (1 << 0)
571#define BC_PARSE_PRINT (1 << 1)
Gavin Howard01055ba2018-11-03 11:00:21 -0600572#define BC_PARSE_NOCALL (1 << 2)
573#define BC_PARSE_NOREAD (1 << 3)
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100574#define BC_PARSE_ARRAY (1 << 4)
Gavin Howard01055ba2018-11-03 11:00:21 -0600575
576#define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags))
577#define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse)))
578
579#define BC_PARSE_FLAG_FUNC_INNER (1 << 0)
580#define BC_PARSE_FUNC_INNER(parse) \
581 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER)
582
583#define BC_PARSE_FLAG_FUNC (1 << 1)
584#define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC)
585
586#define BC_PARSE_FLAG_BODY (1 << 2)
587#define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY)
588
589#define BC_PARSE_FLAG_LOOP (1 << 3)
590#define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP)
591
592#define BC_PARSE_FLAG_LOOP_INNER (1 << 4)
593#define BC_PARSE_LOOP_INNER(parse) \
594 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER)
595
596#define BC_PARSE_FLAG_IF (1 << 5)
597#define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF)
598
599#define BC_PARSE_FLAG_ELSE (1 << 6)
600#define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE)
601
602#define BC_PARSE_FLAG_IF_END (1 << 7)
603#define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END)
604
605#define BC_PARSE_CAN_EXEC(parse) \
606 (!(BC_PARSE_TOP_FLAG(parse) & \
607 (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \
608 BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \
609 BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END)))
610
Gavin Howard01055ba2018-11-03 11:00:21 -0600611struct BcParse;
612
613struct BcProgram;
614
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100615typedef BcStatus (*BcParseParse)(struct BcParse *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600616
617typedef struct BcParse {
618
619 BcParseParse parse;
620
621 BcLex l;
622
623 BcVec flags;
624
625 BcVec exits;
626 BcVec conds;
627
628 BcVec ops;
629
Gavin Howard01055ba2018-11-03 11:00:21 -0600630 BcFunc *func;
631 size_t fidx;
632
633 size_t nbraces;
634 bool auto_part;
635
636} BcParse;
637
Gavin Howard01055ba2018-11-03 11:00:21 -0600638typedef struct BcProgram {
639
640 size_t len;
641 size_t scale;
642
643 BcNum ib;
644 size_t ib_t;
645 BcNum ob;
646 size_t ob_t;
647
648 BcNum hexb;
649
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100650#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600651 BcNum strmb;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100652#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600653
654 BcVec results;
655 BcVec stack;
656
657 BcVec fns;
658 BcVec fn_map;
659
660 BcVec vars;
661 BcVec var_map;
662
663 BcVec arrs;
664 BcVec arr_map;
665
666 BcVec strs;
667 BcVec consts;
668
669 const char *file;
670
671 BcNum last;
672 BcNum zero;
673 BcNum one;
674
675 size_t nchars;
676
Gavin Howard01055ba2018-11-03 11:00:21 -0600677} BcProgram;
678
679#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
680
681#define BC_PROG_MAIN (0)
682#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100683#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600684#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100685#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600686
687#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
688#define BC_PROG_NUM(r, n) \
689 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
690
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100691#define BC_FLAG_W (1 << 0)
692#define BC_FLAG_V (1 << 1)
693#define BC_FLAG_S (1 << 2)
694#define BC_FLAG_Q (1 << 3)
695#define BC_FLAG_L (1 << 4)
696#define BC_FLAG_I (1 << 5)
697#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600698
699#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
700#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
701
Denys Vlasenko64074a12018-12-07 15:50:14 +0100702#define BC_MAX_OBASE ((unsigned) 999)
703#define BC_MAX_DIM ((unsigned) INT_MAX)
704#define BC_MAX_SCALE ((unsigned) UINT_MAX)
705#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
706#define BC_MAX_NUM BC_MAX_STRING
707// Unused apart from "limits" message. Just show a "biggish number" there.
708//#define BC_MAX_NAME BC_MAX_STRING
709//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
710//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
711#define BC_MAX_NAME_STR "999999999"
712#define BC_MAX_EXP_STR "999999999"
713#define BC_MAX_VARS_STR "999999999"
714
715#define BC_MAX_OBASE_STR "999"
716
717#if INT_MAX == 2147483647
718# define BC_MAX_DIM_STR "2147483647"
719#elif INT_MAX == 9223372036854775807
720# define BC_MAX_DIM_STR "9223372036854775807"
721#else
722# error Strange INT_MAX
723#endif
724
725#if UINT_MAX == 4294967295
726# define BC_MAX_SCALE_STR "4294967295"
727# define BC_MAX_STRING_STR "4294967294"
728#elif UINT_MAX == 18446744073709551615
729# define BC_MAX_SCALE_STR "18446744073709551615"
730# define BC_MAX_STRING_STR "18446744073709551614"
731#else
732# error Strange UINT_MAX
733#endif
734#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600735
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100736struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100737 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100738 IF_FEATURE_CLEAN_UP(smallint exiting;)
Gavin Howard01055ba2018-11-03 11:00:21 -0600739 char sbgn;
740 char send;
Gavin Howard01055ba2018-11-03 11:00:21 -0600741
742 BcParse prs;
743 BcProgram prog;
744
Denys Vlasenko5318f812018-12-05 17:48:01 +0100745 // For error messages. Can be set to current parsed line,
746 // or [TODO] to current executing line (can be before last parsed one)
747 unsigned err_line;
748
Gavin Howard01055ba2018-11-03 11:00:21 -0600749 BcVec files;
750
751 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100752
753#if ENABLE_FEATURE_EDITING
754 line_input_t *line_input_state;
755#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100756} FIX_ALIASING;
757#define G (*ptr_to_globals)
758#define INIT_G() do { \
759 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
760} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100761#define FREE_G() do { \
762 FREE_PTR_TO_GLOBALS(); \
763} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100764#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
765#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100766#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100767#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100768# define G_interrupt bb_got_signal
769# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100770#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100771# define G_interrupt 0
772# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100773#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100774#if ENABLE_FEATURE_CLEAN_UP
775# define G_exiting G.exiting
776#else
777# define G_exiting 0
778#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100779#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
780
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100781#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600782
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100783// This is a bit array that corresponds to token types. An entry is
Gavin Howard01055ba2018-11-03 11:00:21 -0600784// true if the token is valid in an expression, false otherwise.
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100785enum {
786 BC_PARSE_EXPRS_BITS = 0
787 + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
788 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
789 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
790 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
791 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
792 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
793 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
794 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8))
Gavin Howard01055ba2018-11-03 11:00:21 -0600795};
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100796static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
797{
798#if ULONG_MAX > 0xffffffff
799 // 64-bit version (will not work correctly for 32-bit longs!)
800 return BC_PARSE_EXPRS_BITS & (1UL << i);
801#else
802 // 32-bit version
803 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
804 if (i >= 32) {
805 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
806 i &= 31;
807 }
808 return m & (1UL << i);
809#endif
810}
Gavin Howard01055ba2018-11-03 11:00:21 -0600811
812// This is an array of data for operators that correspond to token types.
Denys Vlasenko65437582018-12-05 19:37:19 +0100813static const uint8_t bc_parse_ops[] = {
814#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100815 OP(0, false), OP( 0, false ), // inc dec
816 OP(1, false), // neg
Denys Vlasenko65437582018-12-05 19:37:19 +0100817 OP(2, false),
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100818 OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div
819 OP(4, true ), OP( 4, true ), // mod + -
820 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
821 OP(1, false), // not
822 OP(7, true ), OP( 7, true ), // or and
823 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
824 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenko65437582018-12-05 19:37:19 +0100825#undef OP
Gavin Howard01055ba2018-11-03 11:00:21 -0600826};
Denys Vlasenko65437582018-12-05 19:37:19 +0100827#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
828#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
Gavin Howard01055ba2018-11-03 11:00:21 -0600829
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100830// Byte array of up to 4 BC_LEX's, packed into 32-bit word
831typedef uint32_t BcParseNext;
832
Gavin Howard01055ba2018-11-03 11:00:21 -0600833// These identify what tokens can come after expressions in certain cases.
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100834enum {
835#define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) )
836#define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff)
837#define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff)
838 bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF),
839 bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA),
840 bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF),
841 bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN),
842 bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET),
843 bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON),
844 bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF),
845#undef BC_PARSE_NEXT4
846#undef BC_PARSE_NEXT2
847#undef BC_PARSE_NEXT1
848};
Gavin Howard01055ba2018-11-03 11:00:21 -0600849#endif // ENABLE_BC
850
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100851#if ENABLE_DC
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100852static const //BcLexType - should be this type, but narrower type saves size:
853uint8_t
854dc_lex_regs[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600855 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
856 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
857 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
858 BC_LEX_STORE_PUSH,
859};
860
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100861static const //BcLexType - should be this type
862uint8_t
863dc_lex_tokens[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600864 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
865 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
866 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
867 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
868 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
869 BC_LEX_INVALID, BC_LEX_INVALID,
870 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
871 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
872 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
873 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
874 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
875 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
876 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
877 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
878 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
879 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
880 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
881 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
882 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
883 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
884 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
885 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
886 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
887 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
888 BC_LEX_INVALID
889};
890
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100891static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
892int8_t
893dc_parse_insts[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600894 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
895 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
896 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
897 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
898 BC_INST_INVALID, BC_INST_INVALID,
899 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
900 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
901 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
902 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
903 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
904 BC_INST_INVALID, BC_INST_INVALID,
905 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
906 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
907 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
908 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
909 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
910 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
911 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
912 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
913 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
914 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
915 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
916 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
917};
918#endif // ENABLE_DC
919
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100920// In configurations where errors abort instead of propagating error
921// return code up the call chain, functions returning BC_STATUS
922// actually don't return anything, they always succeed and return "void".
923// A macro wrapper is provided, which makes this statement work:
924// s = zbc_func(...)
925// and makes it visible to the compiler that s is always zero,
926// allowing compiler to optimize dead code after the statement.
927//
928// To make code more readable, each such function has a "z"
929// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
930//
931#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100932# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100933# define ERRORFUNC /*nothing*/
934# define ERROR_RETURN(a) a
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100935# define BC_STATUS BcStatus
936# define RETURN_STATUS(v) return (v)
937#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100938# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100939# define ERRORFUNC NORETURN
940# define ERROR_RETURN(a) /*nothing*/
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100941# define BC_STATUS void
942# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
943#endif
944
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100945#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
946#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
947#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
948//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
949static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
950{
951 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
952}
953//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
954static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
955{
956 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
957}
958
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100959typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
960
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100961typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100962
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100963static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
964 BcNumBinaryOp op, size_t req);
965static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
966static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
967static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
968static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
969static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
970static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
971
972static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
973{
974 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
975 (void) scale;
976 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
977}
978
979static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
980{
981 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
982 (void) scale;
983 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
984}
985
986static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
987{
988 size_t req = BC_NUM_MREQ(a, b, scale);
989 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
990}
991
992static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
993{
994 size_t req = BC_NUM_MREQ(a, b, scale);
995 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
996}
997
998static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
999{
1000 size_t req = BC_NUM_MREQ(a, b, scale);
1001 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1002}
1003
1004static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1005{
1006 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1007}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +01001008
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001009static const BcNumBinaryOp zbc_program_ops[] = {
1010 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
Gavin Howard01055ba2018-11-03 11:00:21 -06001011};
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001012#if ERRORS_ARE_FATAL
1013# define zbc_num_add(...) (zbc_num_add(__VA_ARGS__), BC_STATUS_SUCCESS)
1014# define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__), BC_STATUS_SUCCESS)
1015# define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__), BC_STATUS_SUCCESS)
1016# define zbc_num_div(...) (zbc_num_div(__VA_ARGS__), BC_STATUS_SUCCESS)
1017# define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__), BC_STATUS_SUCCESS)
1018# define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__), BC_STATUS_SUCCESS)
1019#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001020
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001021static void fflush_and_check(void)
1022{
1023 fflush_all();
1024 if (ferror(stdout) || ferror(stderr))
1025 bb_perror_msg_and_die("output error");
1026}
1027
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001028#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001029#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001030do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001031 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001032 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001033 return BC_STATUS_FAILURE; \
1034} while (0)
1035#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001036#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001037#endif
1038
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001039static void quit(void) NORETURN;
1040static void quit(void)
1041{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001042 if (ferror(stdin))
1043 bb_perror_msg_and_die("input error");
1044 fflush_and_check();
1045 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001046}
1047
Denys Vlasenko5318f812018-12-05 17:48:01 +01001048static void bc_verror_msg(const char *fmt, va_list p)
1049{
1050 const char *sv = sv; /* for compiler */
1051 if (G.prog.file) {
1052 sv = applet_name;
1053 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
1054 }
1055 bb_verror_msg(fmt, p, NULL);
1056 if (G.prog.file) {
1057 free((char*)applet_name);
1058 applet_name = sv;
1059 }
1060}
1061
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001062static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001063{
1064 va_list p;
1065
1066 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001067 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001068 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01001069
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001070 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001071 exit(1);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001072 ERROR_RETURN(return BC_STATUS_FAILURE;)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001073}
1074
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001075#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001076static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001077{
1078 va_list p;
1079
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001080 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001081 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001082 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001083
1084 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001085 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001086 va_end(p);
1087
1088 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001089 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001090 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001091 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001092 exit(1);
1093 return BC_STATUS_FAILURE;
1094}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001095#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001096
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001097// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1098// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001099// function must not have caller-cleaned parameters on stack.
1100// Unfortunately, vararg function API does exactly that on most arches.
1101// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001102static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001103{
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001104 ERROR_RETURN(return) bc_error_fmt("%s", msg);
1105}
1106static ERRORFUNC int bc_error_bad_character(char c)
1107{
1108 ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c);
1109}
1110static ERRORFUNC int bc_error_bad_expression(void)
1111{
1112 ERROR_RETURN(return) bc_error("bad expression");
1113}
1114static ERRORFUNC int bc_error_bad_token(void)
1115{
1116 ERROR_RETURN(return) bc_error("bad token");
1117}
1118static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1119{
1120 ERROR_RETURN(return) bc_error("stack has too few elements");
1121}
1122static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1123{
1124 ERROR_RETURN(return) bc_error("variable is wrong type");
1125}
1126static ERRORFUNC int bc_error_nested_read_call(void)
1127{
1128 ERROR_RETURN(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001129}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001130#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001131static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001132{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001133 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001134}
Denys Vlasenko00646792018-12-05 18:12:27 +01001135static int bc_POSIX_does_not_allow(const char *msg)
1136{
1137 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1138}
1139static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1140{
1141 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1142}
1143static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1144{
1145 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1146}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001147#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001148
Gavin Howard01055ba2018-11-03 11:00:21 -06001149static void bc_vec_grow(BcVec *v, size_t n)
1150{
1151 size_t cap = v->cap * 2;
1152 while (cap < v->len + n) cap *= 2;
1153 v->v = xrealloc(v->v, v->size * cap);
1154 v->cap = cap;
1155}
1156
1157static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1158{
1159 v->size = esize;
1160 v->cap = BC_VEC_START_CAP;
1161 v->len = 0;
1162 v->dtor = dtor;
1163 v->v = xmalloc(esize * BC_VEC_START_CAP);
1164}
1165
Denys Vlasenko7d628012018-12-04 21:46:47 +01001166static void bc_char_vec_init(BcVec *v)
1167{
1168 bc_vec_init(v, sizeof(char), NULL);
1169}
1170
Gavin Howard01055ba2018-11-03 11:00:21 -06001171static void bc_vec_expand(BcVec *v, size_t req)
1172{
1173 if (v->cap < req) {
1174 v->v = xrealloc(v->v, v->size * req);
1175 v->cap = req;
1176 }
1177}
1178
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001179static void bc_vec_pop(BcVec *v)
1180{
1181 v->len--;
1182 if (v->dtor)
1183 v->dtor(v->v + (v->size * v->len));
1184}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001185
Gavin Howard01055ba2018-11-03 11:00:21 -06001186static void bc_vec_npop(BcVec *v, size_t n)
1187{
1188 if (!v->dtor)
1189 v->len -= n;
1190 else {
1191 size_t len = v->len - n;
1192 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1193 }
1194}
1195
Denys Vlasenko7d628012018-12-04 21:46:47 +01001196static void bc_vec_pop_all(BcVec *v)
1197{
1198 bc_vec_npop(v, v->len);
1199}
1200
Gavin Howard01055ba2018-11-03 11:00:21 -06001201static void bc_vec_push(BcVec *v, const void *data)
1202{
1203 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1204 memmove(v->v + (v->size * v->len), data, v->size);
1205 v->len += 1;
1206}
1207
1208static void bc_vec_pushByte(BcVec *v, char data)
1209{
1210 bc_vec_push(v, &data);
1211}
1212
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001213static void bc_vec_pushZeroByte(BcVec *v)
1214{
1215 //bc_vec_pushByte(v, '\0');
1216 // better:
1217 bc_vec_push(v, &const_int_0);
1218}
1219
Gavin Howard01055ba2018-11-03 11:00:21 -06001220static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1221{
1222 if (idx == v->len)
1223 bc_vec_push(v, data);
1224 else {
1225
1226 char *ptr;
1227
1228 if (v->len == v->cap) bc_vec_grow(v, 1);
1229
1230 ptr = v->v + v->size * idx;
1231
1232 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1233 memmove(ptr, data, v->size);
1234 }
1235}
1236
1237static void bc_vec_string(BcVec *v, size_t len, const char *str)
1238{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001239 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001240 bc_vec_expand(v, len + 1);
1241 memcpy(v->v, str, len);
1242 v->len = len;
1243
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001244 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001245}
1246
1247static void bc_vec_concat(BcVec *v, const char *str)
1248{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001249 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001250
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001251 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001252
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001253 slen = strlen(str);
1254 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001255
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001256 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001257 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001258
1259 v->len = len;
1260}
1261
1262static void *bc_vec_item(const BcVec *v, size_t idx)
1263{
1264 return v->v + v->size * idx;
1265}
1266
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01001267static char** bc_program_str(size_t idx)
1268{
1269 return bc_vec_item(&G.prog.strs, idx);
1270}
1271
1272static BcFunc* bc_program_func(size_t idx)
1273{
1274 return bc_vec_item(&G.prog.fns, idx);
1275}
1276
Gavin Howard01055ba2018-11-03 11:00:21 -06001277static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1278{
1279 return v->v + v->size * (v->len - idx - 1);
1280}
1281
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001282static void *bc_vec_top(const BcVec *v)
1283{
1284 return v->v + v->size * (v->len - 1);
1285}
1286
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001287static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001288{
1289 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001290 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001291 free(v->v);
1292}
1293
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001294static int bc_id_cmp(const void *e1, const void *e2)
1295{
1296 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1297}
1298
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001299static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001300{
1301 free(((BcId *) id)->name);
1302}
1303
Gavin Howard01055ba2018-11-03 11:00:21 -06001304static size_t bc_map_find(const BcVec *v, const void *ptr)
1305{
1306 size_t low = 0, high = v->len;
1307
1308 while (low < high) {
1309
1310 size_t mid = (low + high) / 2;
1311 BcId *id = bc_vec_item(v, mid);
1312 int result = bc_id_cmp(ptr, id);
1313
1314 if (result == 0)
1315 return mid;
1316 else if (result < 0)
1317 high = mid;
1318 else
1319 low = mid + 1;
1320 }
1321
1322 return low;
1323}
1324
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001325static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001326{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001327 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001328
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001329 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001330 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001331 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1332 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001333 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001334 bc_vec_pushAt(v, ptr, n);
1335 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001336}
1337
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001338#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06001339static size_t bc_map_index(const BcVec *v, const void *ptr)
1340{
1341 size_t i = bc_map_find(v, ptr);
1342 if (i >= v->len) return BC_VEC_INVALID_IDX;
1343 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1344}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001345#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001346
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001347static int push_input_byte(BcVec *vec, char c)
1348{
1349 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1350 || c > 0x7e
1351 ) {
1352 // Bad chars on this line, ignore entire line
1353 bc_error_fmt("illegal character 0x%02x", c);
1354 return 1;
1355 }
1356 bc_vec_pushByte(vec, (char)c);
1357 return 0;
1358}
1359
Denys Vlasenkob402ff82018-12-11 15:45:15 +01001360// This is not a "z" function: can also return BC_STATUS_EOF
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001361static BcStatus bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001362{
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001363 BcStatus s;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001364 bool bad_chars;
Gavin Howard01055ba2018-11-03 11:00:21 -06001365
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001366 s = BC_STATUS_SUCCESS;
Denys Vlasenko00d77792018-11-30 23:13:42 +01001367 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001368 int c;
Gavin Howard01055ba2018-11-03 11:00:21 -06001369
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001370 bad_chars = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001371 bc_vec_pop_all(vec);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001372
1373 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001374
Gavin Howard01055ba2018-11-03 11:00:21 -06001375#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001376 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001377 intr:
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001378 G_interrupt = 0;
Denys Vlasenkoac6ed112018-12-08 21:39:10 +01001379 // GNU bc says "interrupted execution."
1380 // GNU dc says "Interrupt!"
1381 fputs("\ninterrupted execution\n", stderr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001382 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001383# if ENABLE_FEATURE_EDITING
1384 if (G_ttyin) {
1385 int n, i;
1386# define line_buf bb_common_bufsiz1
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001387 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001388 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1389 if (n == 0) // ^C
1390 goto intr;
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001391 s = BC_STATUS_EOF;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001392 break;
1393 }
1394 i = 0;
1395 for (;;) {
1396 c = line_buf[i++];
1397 if (!c) break;
1398 bad_chars |= push_input_byte(vec, c);
1399 }
1400# undef line_buf
1401 } else
1402# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001403#endif
Denys Vlasenkoed849352018-12-06 10:26:13 +01001404 {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001405 IF_FEATURE_BC_SIGNALS(errno = 0;)
1406 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001407 c = fgetc(stdin);
1408#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
1409 // Both conditions appear simultaneously, check both just in case
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001410 if (errno == EINTR || G_interrupt) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001411 // ^C was pressed
1412 clearerr(stdin);
1413 goto intr;
1414 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001415#endif
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001416 if (c == EOF) {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001417 if (ferror(stdin))
1418 quit(); // this emits error message
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001419 s = BC_STATUS_EOF;
Denys Vlasenkoed849352018-12-06 10:26:13 +01001420 // Note: EOF does not append '\n', therefore:
1421 // printf 'print 123\n' | bc - works
1422 // printf 'print 123' | bc - fails (syntax error)
1423 break;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001424 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001425 bad_chars |= push_input_byte(vec, c);
1426 } while (c != '\n');
Denys Vlasenkoed849352018-12-06 10:26:13 +01001427 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001428 } while (bad_chars);
Gavin Howard01055ba2018-11-03 11:00:21 -06001429
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001430 bc_vec_pushZeroByte(vec);
Gavin Howard01055ba2018-11-03 11:00:21 -06001431
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001432 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001433}
1434
Denys Vlasenkodf515392018-12-02 19:27:48 +01001435static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001436{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001437 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001438 size_t size = ((size_t) -1);
1439 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001440
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001441 // Never returns NULL (dies on errors)
1442 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001443
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001444 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001445 char c = buf[i];
1446 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1447 || c > 0x7e
1448 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001449 free(buf);
1450 buf = NULL;
1451 break;
1452 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001453 }
1454
Denys Vlasenkodf515392018-12-02 19:27:48 +01001455 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001456}
1457
Gavin Howard01055ba2018-11-03 11:00:21 -06001458static void bc_num_setToZero(BcNum *n, size_t scale)
1459{
1460 n->len = 0;
1461 n->neg = false;
1462 n->rdx = scale;
1463}
1464
1465static void bc_num_zero(BcNum *n)
1466{
1467 bc_num_setToZero(n, 0);
1468}
1469
1470static void bc_num_one(BcNum *n)
1471{
1472 bc_num_setToZero(n, 0);
1473 n->len = 1;
1474 n->num[0] = 1;
1475}
1476
1477static void bc_num_ten(BcNum *n)
1478{
1479 bc_num_setToZero(n, 0);
1480 n->len = 2;
1481 n->num[0] = 0;
1482 n->num[1] = 1;
1483}
1484
Denys Vlasenko3129f702018-12-09 12:04:44 +01001485// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001486static void bc_num_init(BcNum *n, size_t req)
1487{
1488 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001489 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001490 n->num = xmalloc(req);
1491 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001492 n->rdx = 0;
1493 n->len = 0;
1494 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001495}
1496
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001497static void bc_num_init_DEF_SIZE(BcNum *n)
1498{
1499 bc_num_init(n, BC_NUM_DEF_SIZE);
1500}
1501
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001502static void bc_num_expand(BcNum *n, size_t req)
1503{
1504 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1505 if (req > n->cap) {
1506 n->num = xrealloc(n->num, req);
1507 n->cap = req;
1508 }
1509}
1510
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001511static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001512{
1513 free(((BcNum *) num)->num);
1514}
1515
1516static void bc_num_copy(BcNum *d, BcNum *s)
1517{
1518 if (d != s) {
1519 bc_num_expand(d, s->cap);
1520 d->len = s->len;
1521 d->neg = s->neg;
1522 d->rdx = s->rdx;
1523 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1524 }
1525}
1526
Denys Vlasenko29301232018-12-11 15:29:32 +01001527static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001528{
1529 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001530 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001531
Denys Vlasenko29301232018-12-11 15:29:32 +01001532 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001533
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001534 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001535
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001536 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001537
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001538 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001539 pow *= 10;
1540
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001541 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001542 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001543 prev = result;
1544 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001545 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001546 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001547
Denys Vlasenko29301232018-12-11 15:29:32 +01001548 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001549}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001550#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001551# define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001552#endif
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001553
1554static void bc_num_ulong2num(BcNum *n, unsigned long val)
1555{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001556 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001557
1558 bc_num_zero(n);
1559
1560 if (val == 0) return;
1561
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001562 if (ULONG_MAX == 0xffffffffUL)
1563 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001564 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001565 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001566 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001567
1568 ptr = n->num;
1569 for (;;) {
1570 n->len++;
1571 *ptr++ = val % 10;
1572 val /= 10;
1573 if (val == 0) break;
1574 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001575}
1576
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001577static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001578 size_t len)
1579{
1580 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001581 for (i = 0; i < len; ++i) {
1582 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001583 a[i + j++] += 10;
1584 a[i + j] -= 1;
1585 }
1586 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001587}
1588
1589static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1590{
1591 size_t i;
1592 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001593 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001594 return BC_NUM_NEG(i + 1, c < 0);
1595}
1596
1597static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1598{
1599 size_t i, min, a_int, b_int, diff;
1600 BcDig *max_num, *min_num;
1601 bool a_max, neg = false;
1602 ssize_t cmp;
1603
1604 if (a == b) return 0;
1605 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1606 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
1607 if (a->neg) {
1608 if (b->neg)
1609 neg = true;
1610 else
1611 return -1;
1612 }
1613 else if (b->neg)
1614 return 1;
1615
1616 a_int = BC_NUM_INT(a);
1617 b_int = BC_NUM_INT(b);
1618 a_int -= b_int;
1619 a_max = (a->rdx > b->rdx);
1620
1621 if (a_int != 0) return (ssize_t) a_int;
1622
1623 if (a_max) {
1624 min = b->rdx;
1625 diff = a->rdx - b->rdx;
1626 max_num = a->num + diff;
1627 min_num = b->num;
1628 }
1629 else {
1630 min = a->rdx;
1631 diff = b->rdx - a->rdx;
1632 max_num = b->num + diff;
1633 min_num = a->num;
1634 }
1635
1636 cmp = bc_num_compare(max_num, min_num, b_int + min);
1637 if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg);
1638
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001639 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001640 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
1641 }
1642
1643 return 0;
1644}
1645
1646static void bc_num_truncate(BcNum *n, size_t places)
1647{
1648 if (places == 0) return;
1649
1650 n->rdx -= places;
1651
1652 if (n->len != 0) {
1653 n->len -= places;
1654 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1655 }
1656}
1657
1658static void bc_num_extend(BcNum *n, size_t places)
1659{
1660 size_t len = n->len + places;
1661
1662 if (places != 0) {
1663
1664 if (n->cap < len) bc_num_expand(n, len);
1665
1666 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1667 memset(n->num, 0, sizeof(BcDig) * places);
1668
1669 n->len += places;
1670 n->rdx += places;
1671 }
1672}
1673
1674static void bc_num_clean(BcNum *n)
1675{
1676 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1677 if (n->len == 0)
1678 n->neg = false;
1679 else if (n->len < n->rdx)
1680 n->len = n->rdx;
1681}
1682
1683static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1684{
1685 if (n->rdx < scale)
1686 bc_num_extend(n, scale - n->rdx);
1687 else
1688 bc_num_truncate(n, n->rdx - scale);
1689
1690 bc_num_clean(n);
1691 if (n->len != 0) n->neg = !neg1 != !neg2;
1692}
1693
1694static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1695 BcNum *restrict b)
1696{
1697 if (idx < n->len) {
1698
1699 b->len = n->len - idx;
1700 a->len = idx;
1701 a->rdx = b->rdx = 0;
1702
1703 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1704 memcpy(a->num, n->num, idx * sizeof(BcDig));
1705 }
1706 else {
1707 bc_num_zero(b);
1708 bc_num_copy(a, n);
1709 }
1710
1711 bc_num_clean(a);
1712 bc_num_clean(b);
1713}
1714
Denys Vlasenko29301232018-12-11 15:29:32 +01001715static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001716{
Denys Vlasenko29301232018-12-11 15:29:32 +01001717 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001718
1719 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1720 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1721 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001722 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001723 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001724
1725 if (n->rdx >= places)
1726 n->rdx -= places;
1727 else {
1728 bc_num_extend(n, places - n->rdx);
1729 n->rdx = 0;
1730 }
1731
1732 bc_num_clean(n);
1733
Denys Vlasenko29301232018-12-11 15:29:32 +01001734 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001735}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001736#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001737# define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001738#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001739
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001740static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001741{
1742 BcNum one;
1743 BcDig num[2];
1744
1745 one.cap = 2;
1746 one.num = num;
1747 bc_num_one(&one);
1748
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001749 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001750}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001751#if ERRORS_ARE_FATAL
1752# define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS)
1753#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001754
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001755static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001756{
1757 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1758 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1759 int carry, in;
1760
1761 // Because this function doesn't need to use scale (per the bc spec),
1762 // I am hijacking it to say whether it's doing an add or a subtract.
1763
1764 if (a->len == 0) {
1765 bc_num_copy(c, b);
1766 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001767 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001768 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001769 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001770 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001771 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001772 }
1773
1774 c->neg = a->neg;
1775 c->rdx = BC_MAX(a->rdx, b->rdx);
1776 min_rdx = BC_MIN(a->rdx, b->rdx);
1777 c->len = 0;
1778
1779 if (a->rdx > b->rdx) {
1780 diff = a->rdx - b->rdx;
1781 ptr = a->num;
1782 ptr_a = a->num + diff;
1783 ptr_b = b->num;
1784 }
1785 else {
1786 diff = b->rdx - a->rdx;
1787 ptr = b->num;
1788 ptr_a = a->num;
1789 ptr_b = b->num + diff;
1790 }
1791
1792 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1793
1794 ptr_c += diff;
1795 a_int = BC_NUM_INT(a);
1796 b_int = BC_NUM_INT(b);
1797
1798 if (a_int > b_int) {
1799 min_int = b_int;
1800 max = a_int;
1801 ptr = ptr_a;
1802 }
1803 else {
1804 min_int = a_int;
1805 max = b_int;
1806 ptr = ptr_b;
1807 }
1808
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001809 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001810 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1811 carry = in / 10;
1812 ptr_c[i] = (BcDig)(in % 10);
1813 }
1814
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001815 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001816 in = ((int) ptr[i]) + carry;
1817 carry = in / 10;
1818 ptr_c[i] = (BcDig)(in % 10);
1819 }
1820
1821 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1822
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001823 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001824}
1825
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001826static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001827{
Gavin Howard01055ba2018-11-03 11:00:21 -06001828 ssize_t cmp;
1829 BcNum *minuend, *subtrahend;
1830 size_t start;
1831 bool aneg, bneg, neg;
1832
1833 // Because this function doesn't need to use scale (per the bc spec),
1834 // I am hijacking it to say whether it's doing an add or a subtract.
1835
1836 if (a->len == 0) {
1837 bc_num_copy(c, b);
1838 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001839 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001840 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001841 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001842 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001843 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001844 }
1845
1846 aneg = a->neg;
1847 bneg = b->neg;
1848 a->neg = b->neg = false;
1849
1850 cmp = bc_num_cmp(a, b);
1851
1852 a->neg = aneg;
1853 b->neg = bneg;
1854
1855 if (cmp == 0) {
1856 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001857 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001858 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001859 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001860 neg = a->neg;
1861 minuend = a;
1862 subtrahend = b;
1863 }
1864 else {
1865 neg = b->neg;
1866 if (sub) neg = !neg;
1867 minuend = b;
1868 subtrahend = a;
1869 }
1870
1871 bc_num_copy(c, minuend);
1872 c->neg = neg;
1873
1874 if (c->rdx < subtrahend->rdx) {
1875 bc_num_extend(c, subtrahend->rdx - c->rdx);
1876 start = 0;
1877 }
1878 else
1879 start = c->rdx - subtrahend->rdx;
1880
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001881 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001882
1883 bc_num_clean(c);
1884
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001885 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001886}
1887
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001888static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001889 BcNum *restrict c)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001890#if ERRORS_ARE_FATAL
1891# define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS)
1892#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001893{
1894 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001895 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001896 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001897 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001898
Gavin Howard01055ba2018-11-03 11:00:21 -06001899 if (a->len == 0 || b->len == 0) {
1900 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001901 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001902 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001903 aone = BC_NUM_ONE(a);
1904 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001905 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001906 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001907 }
1908
1909 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1910 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1911 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001912 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001913 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001914
Gavin Howard01055ba2018-11-03 11:00:21 -06001915 bc_num_expand(c, a->len + b->len + 1);
1916
1917 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001918 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001919
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001920 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001921
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001922 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001923 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001924 unsigned in = c->num[i + j];
1925 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1926 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001927 carry = in / 10;
1928 c->num[i + j] = (BcDig)(in % 10);
1929 }
1930
1931 c->num[i + j] += (BcDig) carry;
1932 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001933
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001934#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001935 // a=2^1000000
1936 // a*a <- without check below, this will not be interruptible
1937 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001938#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001939 }
1940
1941 c->len = len;
1942
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001943 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001944 }
1945
1946 bc_num_init(&l1, max);
1947 bc_num_init(&h1, max);
1948 bc_num_init(&l2, max);
1949 bc_num_init(&h2, max);
1950 bc_num_init(&m1, max);
1951 bc_num_init(&m2, max);
1952 bc_num_init(&z0, max);
1953 bc_num_init(&z1, max);
1954 bc_num_init(&z2, max);
1955 bc_num_init(&temp, max + max);
1956
1957 bc_num_split(a, max2, &l1, &h1);
1958 bc_num_split(b, max2, &l2, &h2);
1959
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001960 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001961 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001962 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001963 if (s) goto err;
1964
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001965 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001966 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001967 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001968 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001969 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001970 if (s) goto err;
1971
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001972 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001973 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001974 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001975 if (s) goto err;
1976
Denys Vlasenko29301232018-12-11 15:29:32 +01001977 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001978 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001979 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001980 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001981 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001982 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001983 s = zbc_num_add(&temp, &z2, c, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001984
1985err:
1986 bc_num_free(&temp);
1987 bc_num_free(&z2);
1988 bc_num_free(&z1);
1989 bc_num_free(&z0);
1990 bc_num_free(&m2);
1991 bc_num_free(&m1);
1992 bc_num_free(&h2);
1993 bc_num_free(&l2);
1994 bc_num_free(&h1);
1995 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001996 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001997}
1998
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001999static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002000{
2001 BcStatus s;
2002 BcNum cpa, cpb;
2003 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
2004
2005 scale = BC_MAX(scale, a->rdx);
2006 scale = BC_MAX(scale, b->rdx);
2007 scale = BC_MIN(a->rdx + b->rdx, scale);
2008 maxrdx = BC_MAX(maxrdx, scale);
2009
2010 bc_num_init(&cpa, a->len);
2011 bc_num_init(&cpb, b->len);
2012
2013 bc_num_copy(&cpa, a);
2014 bc_num_copy(&cpb, b);
2015 cpa.neg = cpb.neg = false;
2016
Denys Vlasenko29301232018-12-11 15:29:32 +01002017 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002018 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01002019 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002020 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002021 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06002022 if (s) goto err;
2023
2024 maxrdx += scale;
2025 bc_num_expand(c, c->len + maxrdx);
2026
2027 if (c->len < maxrdx) {
2028 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
2029 c->len += maxrdx;
2030 }
2031
2032 c->rdx = maxrdx;
2033 bc_num_retireMul(c, scale, a->neg, b->neg);
2034
2035err:
2036 bc_num_free(&cpb);
2037 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002038 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002039}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002040#if ERRORS_ARE_FATAL
2041# define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS)
2042#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002043
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002044static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002045{
2046 BcStatus s = BC_STATUS_SUCCESS;
2047 BcDig *n, *p, q;
2048 size_t len, end, i;
2049 BcNum cp;
2050 bool zero = true;
2051
2052 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002053 RETURN_STATUS(bc_error("divide by zero"));
2054 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002055 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002056 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002057 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002058 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002059 bc_num_copy(c, a);
2060 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002061 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002062 }
2063
2064 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2065 bc_num_copy(&cp, a);
2066 len = b->len;
2067
2068 if (len > cp.len) {
2069 bc_num_expand(&cp, len + 2);
2070 bc_num_extend(&cp, len - cp.len);
2071 }
2072
2073 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2074 cp.rdx -= b->rdx;
2075 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2076
2077 if (b->rdx == b->len) {
2078 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
2079 len -= i - 1;
2080 }
2081
2082 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2083
2084 // We want an extra zero in front to make things simpler.
2085 cp.num[cp.len++] = 0;
2086 end = cp.len - len;
2087
2088 bc_num_expand(c, cp.len);
2089
2090 bc_num_zero(c);
2091 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2092 c->rdx = cp.rdx;
2093 c->len = cp.len;
2094 p = b->num;
2095
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002096 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002097 n = cp.num + i;
2098 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002099 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002100 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002101#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002102 // a=2^100000
2103 // scale=40000
2104 // 1/a <- without check below, this will not be interruptible
2105 if (G_interrupt) {
2106 s = BC_STATUS_FAILURE;
2107 break;
2108 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002109#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002110 }
2111
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002112 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002113 bc_num_free(&cp);
2114
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002115 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002116}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002117#if ERRORS_ARE_FATAL
2118# define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS)
2119#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002120
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002121static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002122 BcNum *restrict d, size_t scale, size_t ts)
2123{
2124 BcStatus s;
2125 BcNum temp;
2126 bool neg;
2127
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002128 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002129 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002130
2131 if (a->len == 0) {
2132 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002133 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002134 }
2135
2136 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002137 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002138 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002139
2140 if (scale != 0) scale = ts;
2141
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002142 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002143 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002144 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002145 if (s) goto err;
2146
2147 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2148
2149 neg = d->neg;
2150 bc_num_retireMul(d, ts, a->neg, b->neg);
2151 d->neg = neg;
2152
2153err:
2154 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002155 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002156}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002157#if ERRORS_ARE_FATAL
2158# define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS)
2159#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002160
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002161static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002162{
2163 BcStatus s;
2164 BcNum c1;
2165 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2166
2167 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002168 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002169 bc_num_free(&c1);
2170
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002171 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002172}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002173#if ERRORS_ARE_FATAL
2174# define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS)
2175#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002176
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002177static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002178{
2179 BcStatus s = BC_STATUS_SUCCESS;
2180 BcNum copy;
2181 unsigned long pow;
2182 size_t i, powrdx, resrdx;
2183 bool neg, zero;
2184
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002185 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002186
2187 if (b->len == 0) {
2188 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002189 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002190 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002191 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002192 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002193 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002194 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002195 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002196 if (!b->neg)
2197 bc_num_copy(c, a);
2198 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002199 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002200 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002201 }
2202
2203 neg = b->neg;
2204 b->neg = false;
2205
Denys Vlasenko29301232018-12-11 15:29:32 +01002206 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002207 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002208
2209 bc_num_init(&copy, a->len);
2210 bc_num_copy(&copy, a);
2211
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002212 if (!neg) {
2213 if (a->rdx > scale)
2214 scale = a->rdx;
2215 if (a->rdx * pow < scale)
2216 scale = a->rdx * pow;
2217 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002218
2219 b->neg = neg;
2220
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002221 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002222 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002223 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002224 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002225 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002226 //if (G_interrupt) {
2227 // s = BC_STATUS_FAILURE;
2228 // goto err;
2229 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002230 }
2231
Gavin Howard01055ba2018-11-03 11:00:21 -06002232 bc_num_copy(c, &copy);
2233
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002234 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002235
2236 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002237 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002238 if (s) goto err;
2239
2240 if (pow & 1) {
2241 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002242 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002243 if (s) goto err;
2244 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002245 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002246 //if (G_interrupt) {
2247 // s = BC_STATUS_FAILURE;
2248 // goto err;
2249 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002250 }
2251
2252 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002253 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002254 if (s) goto err;
2255 }
2256
Gavin Howard01055ba2018-11-03 11:00:21 -06002257 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2258
2259 // We can't use bc_num_clean() here.
2260 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2261 if (zero) bc_num_setToZero(c, scale);
2262
2263err:
2264 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002265 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002266}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002267#if ERRORS_ARE_FATAL
2268# define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS)
2269#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002270
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002271static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002272 BcNumBinaryOp op, size_t req)
2273{
2274 BcStatus s;
2275 BcNum num2, *ptr_a, *ptr_b;
2276 bool init = false;
2277
2278 if (c == a) {
2279 ptr_a = &num2;
2280 memcpy(ptr_a, c, sizeof(BcNum));
2281 init = true;
2282 }
2283 else
2284 ptr_a = a;
2285
2286 if (c == b) {
2287 ptr_b = &num2;
2288 if (c != a) {
2289 memcpy(ptr_b, c, sizeof(BcNum));
2290 init = true;
2291 }
2292 }
2293 else
2294 ptr_b = b;
2295
2296 if (init)
2297 bc_num_init(c, req);
2298 else
2299 bc_num_expand(c, req);
2300
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002301#if !ERRORS_ARE_FATAL
Gavin Howard01055ba2018-11-03 11:00:21 -06002302 s = op(ptr_a, ptr_b, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002303#else
2304 op(ptr_a, ptr_b, c, scale);
2305 s = BC_STATUS_SUCCESS;
2306#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002307
2308 if (init) bc_num_free(&num2);
2309
Denys Vlasenko29301232018-12-11 15:29:32 +01002310 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002311}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002312#if ERRORS_ARE_FATAL
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002313# define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002314#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002315
Denys Vlasenko9311e012018-12-10 11:54:18 +01002316static bool bc_num_strValid(const char *val, size_t base)
2317{
2318 BcDig b;
2319 bool radix;
2320
2321 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2322 radix = false;
2323 for (;;) {
2324 BcDig c = *val++;
2325 if (c == '\0')
2326 break;
2327 if (c == '.') {
2328 if (radix) return false;
2329 radix = true;
2330 continue;
2331 }
2332 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2333 return false;
2334 }
2335 return true;
2336}
2337
2338// Note: n is already "bc_num_zero()"ed,
2339// leading zeroes in "val" are removed
2340static void bc_num_parseDecimal(BcNum *n, const char *val)
2341{
2342 size_t len, i;
2343 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002344
2345 len = strlen(val);
2346 if (len == 0)
2347 return;
2348
Denys Vlasenko9311e012018-12-10 11:54:18 +01002349 bc_num_expand(n, len);
2350
2351 ptr = strchr(val, '.');
2352
2353 n->rdx = 0;
2354 if (ptr != NULL)
2355 n->rdx = (size_t)((val + len) - (ptr + 1));
2356
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002357 for (i = 0; val[i]; ++i) {
2358 if (val[i] != '0' && val[i] != '.') {
2359 // Not entirely zero value - convert it, and exit
2360 i = len - 1;
2361 for (;;) {
2362 n->num[n->len] = val[i] - '0';
2363 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002364 skip_dot:
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002365 if ((ssize_t)--i == (ssize_t)-1) break;
2366 if (val[i] == '.') goto skip_dot;
2367 }
2368 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002369 }
2370 }
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002371 // if this is reached, the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002372}
2373
2374// Note: n is already "bc_num_zero()"ed,
2375// leading zeroes in "val" are removed
2376static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2377{
2378 BcStatus s;
2379 BcNum temp, mult, result;
2380 BcDig c = '\0';
2381 unsigned long v;
2382 size_t i, digits;
2383
2384 for (i = 0; ; ++i) {
2385 if (val[i] == '\0')
2386 return;
2387 if (val[i] != '.' && val[i] != '0')
2388 break;
2389 }
2390
2391 bc_num_init_DEF_SIZE(&temp);
2392 bc_num_init_DEF_SIZE(&mult);
2393
2394 for (;;) {
2395 c = *val++;
2396 if (c == '\0') goto int_err;
2397 if (c == '.') break;
2398
2399 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2400
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002401 s = zbc_num_mul(n, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002402 if (s) goto int_err;
2403 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002404 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002405 if (s) goto int_err;
2406 }
2407
2408 bc_num_init(&result, base->len);
2409 //bc_num_zero(&result); - already is
2410 bc_num_one(&mult);
2411
2412 digits = 0;
2413 for (;;) {
2414 c = *val++;
2415 if (c == '\0') break;
2416 digits++;
2417
2418 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2419
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002420 s = zbc_num_mul(&result, base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002421 if (s) goto err;
2422 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002423 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002424 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002425 s = zbc_num_mul(&mult, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002426 if (s) goto err;
2427 }
2428
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002429 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002430 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002431 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002432 if (s) goto err;
2433
2434 if (n->len != 0) {
2435 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2436 } else
2437 bc_num_zero(n);
2438
2439err:
2440 bc_num_free(&result);
2441int_err:
2442 bc_num_free(&mult);
2443 bc_num_free(&temp);
2444}
2445
Denys Vlasenko29301232018-12-11 15:29:32 +01002446static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002447 size_t base_t)
2448{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002449 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002450 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002451
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002452 bc_num_zero(n);
2453 while (*val == '0') val++;
2454
Gavin Howard01055ba2018-11-03 11:00:21 -06002455 if (base_t == 10)
2456 bc_num_parseDecimal(n, val);
2457 else
2458 bc_num_parseBase(n, val, base);
2459
Denys Vlasenko29301232018-12-11 15:29:32 +01002460 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002461}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002462#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002463# define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002464#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002465
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002466static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002467{
2468 BcStatus s;
2469 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2470 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2471 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2472
2473 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2474 bc_num_expand(b, req);
2475
2476 if (a->len == 0) {
2477 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002478 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002479 }
2480 else if (a->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002481 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002482 else if (BC_NUM_ONE(a)) {
2483 bc_num_one(b);
2484 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002485 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002486 }
2487
2488 scale = BC_MAX(scale, a->rdx) + 1;
2489 len = a->len + scale;
2490
2491 bc_num_init(&num1, len);
2492 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002493 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002494
2495 bc_num_one(&half);
2496 half.num[0] = 5;
2497 half.rdx = 1;
2498
2499 bc_num_init(&f, len);
2500 bc_num_init(&fprime, len);
2501
2502 x0 = &num1;
2503 x1 = &num2;
2504
2505 bc_num_one(x0);
2506 pow = BC_NUM_INT(a);
2507
2508 if (pow) {
2509
2510 if (pow & 1)
2511 x0->num[0] = 2;
2512 else
2513 x0->num[0] = 6;
2514
2515 pow -= 2 - (pow & 1);
2516
2517 bc_num_extend(x0, pow);
2518
2519 // Make sure to move the radix back.
2520 x0->rdx -= pow;
2521 }
2522
2523 x0->rdx = digs = digs1 = 0;
2524 resrdx = scale + 2;
2525 len = BC_NUM_INT(x0) + resrdx - 1;
2526
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002527 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002528
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002529 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002530 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002531 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002532 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002533 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002534 if (s) goto err;
2535
2536 cmp = bc_num_cmp(x1, x0);
2537 digs = x1->len - (unsigned long long) llabs(cmp);
2538
2539 if (cmp == cmp2 && digs == digs1)
2540 times += 1;
2541 else
2542 times = 0;
2543
2544 resrdx += times > 4;
2545
2546 cmp2 = cmp1;
2547 cmp1 = cmp;
2548 digs1 = digs;
2549
2550 temp = x0;
2551 x0 = x1;
2552 x1 = temp;
2553 }
2554
Gavin Howard01055ba2018-11-03 11:00:21 -06002555 bc_num_copy(b, x0);
2556 scale -= 1;
2557 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2558
2559err:
2560 bc_num_free(&fprime);
2561 bc_num_free(&f);
2562 bc_num_free(&half);
2563 bc_num_free(&num2);
2564 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002565 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002566}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002567#if ERRORS_ARE_FATAL
2568# define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS)
2569#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002570
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002571static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002572 size_t scale)
2573{
2574 BcStatus s;
2575 BcNum num2, *ptr_a;
2576 bool init = false;
2577 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2578
2579 if (c == a) {
2580 memcpy(&num2, c, sizeof(BcNum));
2581 ptr_a = &num2;
2582 bc_num_init(c, len);
2583 init = true;
2584 }
2585 else {
2586 ptr_a = a;
2587 bc_num_expand(c, len);
2588 }
2589
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002590 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002591
2592 if (init) bc_num_free(&num2);
2593
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002594 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002595}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002596#if ERRORS_ARE_FATAL
2597# define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
2598#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002599
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002600#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002601static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002602{
2603 BcStatus s;
2604 BcNum base, exp, two, temp;
2605
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002606 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002607 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002608 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002609 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002610 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002611 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002612
2613 bc_num_expand(d, c->len);
2614 bc_num_init(&base, c->len);
2615 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002616 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002617 bc_num_init(&temp, b->len);
2618
2619 bc_num_one(&two);
2620 two.num[0] = 2;
2621 bc_num_one(d);
2622
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002623 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002624 if (s) goto err;
2625 bc_num_copy(&exp, b);
2626
2627 while (exp.len != 0) {
2628
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002629 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002630 if (s) goto err;
2631
2632 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002633 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002634 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002635 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002636 if (s) goto err;
2637 }
2638
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002639 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002640 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002641 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002642 if (s) goto err;
2643 }
2644
2645err:
2646 bc_num_free(&temp);
2647 bc_num_free(&two);
2648 bc_num_free(&exp);
2649 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002650 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002651}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002652#if ERRORS_ARE_FATAL
2653# define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
2654#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002655#endif // ENABLE_DC
2656
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002657#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002658static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002659{
2660 BcId a;
2661 size_t i;
2662
2663 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002664 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002665 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002666 }
2667
2668 a.idx = var;
2669 a.name = name;
2670
2671 bc_vec_push(&f->autos, &a);
2672
Denys Vlasenko29301232018-12-11 15:29:32 +01002673 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002674}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002675#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002676# define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002677#endif
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002678#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002679
2680static void bc_func_init(BcFunc *f)
2681{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002682 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002683 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2684 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2685 f->nparams = 0;
2686}
2687
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002688static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002689{
2690 BcFunc *f = (BcFunc *) func;
2691 bc_vec_free(&f->code);
2692 bc_vec_free(&f->autos);
2693 bc_vec_free(&f->labels);
2694}
2695
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002696static void bc_array_expand(BcVec *a, size_t len);
2697
Gavin Howard01055ba2018-11-03 11:00:21 -06002698static void bc_array_init(BcVec *a, bool nums)
2699{
2700 if (nums)
2701 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2702 else
2703 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2704 bc_array_expand(a, 1);
2705}
2706
Gavin Howard01055ba2018-11-03 11:00:21 -06002707static void bc_array_expand(BcVec *a, size_t len)
2708{
2709 BcResultData data;
2710
2711 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2712 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002713 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002714 bc_vec_push(a, &data.n);
2715 }
2716 }
2717 else {
2718 while (len > a->len) {
2719 bc_array_init(&data.v, true);
2720 bc_vec_push(a, &data.v);
2721 }
2722 }
2723}
2724
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002725static void bc_array_copy(BcVec *d, const BcVec *s)
2726{
2727 size_t i;
2728
2729 bc_vec_pop_all(d);
2730 bc_vec_expand(d, s->cap);
2731 d->len = s->len;
2732
2733 for (i = 0; i < s->len; ++i) {
2734 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2735 bc_num_init(dnum, snum->len);
2736 bc_num_copy(dnum, snum);
2737 }
2738}
2739
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002740static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002741{
2742 free(*((char **) string));
2743}
2744
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002745#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002746static void bc_result_copy(BcResult *d, BcResult *src)
2747{
2748 d->t = src->t;
2749
2750 switch (d->t) {
2751
2752 case BC_RESULT_TEMP:
2753 case BC_RESULT_IBASE:
2754 case BC_RESULT_SCALE:
2755 case BC_RESULT_OBASE:
2756 {
2757 bc_num_init(&d->d.n, src->d.n.len);
2758 bc_num_copy(&d->d.n, &src->d.n);
2759 break;
2760 }
2761
2762 case BC_RESULT_VAR:
2763 case BC_RESULT_ARRAY:
2764 case BC_RESULT_ARRAY_ELEM:
2765 {
2766 d->d.id.name = xstrdup(src->d.id.name);
2767 break;
2768 }
2769
2770 case BC_RESULT_CONSTANT:
2771 case BC_RESULT_LAST:
2772 case BC_RESULT_ONE:
2773 case BC_RESULT_STR:
2774 {
2775 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2776 break;
2777 }
2778 }
2779}
2780#endif // ENABLE_DC
2781
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002782static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002783{
2784 BcResult *r = (BcResult *) result;
2785
2786 switch (r->t) {
2787
2788 case BC_RESULT_TEMP:
2789 case BC_RESULT_IBASE:
2790 case BC_RESULT_SCALE:
2791 case BC_RESULT_OBASE:
2792 {
2793 bc_num_free(&r->d.n);
2794 break;
2795 }
2796
2797 case BC_RESULT_VAR:
2798 case BC_RESULT_ARRAY:
2799 case BC_RESULT_ARRAY_ELEM:
2800 {
2801 free(r->d.id.name);
2802 break;
2803 }
2804
2805 default:
2806 {
2807 // Do nothing.
2808 break;
2809 }
2810 }
2811}
2812
2813static void bc_lex_lineComment(BcLex *l)
2814{
2815 l->t.t = BC_LEX_WHITESPACE;
2816 while (l->i < l->len && l->buf[l->i++] != '\n');
2817 --l->i;
2818}
2819
2820static void bc_lex_whitespace(BcLex *l)
2821{
2822 char c;
2823 l->t.t = BC_LEX_WHITESPACE;
2824 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
2825}
2826
Denys Vlasenko29301232018-12-11 15:29:32 +01002827static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002828{
2829 const char *buf = l->buf + l->i;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002830 size_t len, bslashes, i, ccnt;
2831 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002832
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002833 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002834 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002835 bslashes = 0;
2836 ccnt = i = 0;
2837 for (;;) {
2838 char c = buf[i];
2839 if (c == '\0')
2840 break;
2841 if (c == '\\' && buf[i + 1] == '\n') {
2842 i += 2;
2843 bslashes++;
2844 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002845 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002846 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2847 if (c != '.') break;
2848 // if '.' was already seen, stop on second one:
2849 if (pt) break;
2850 pt = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002851 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002852 // buf[i] is one of "0-9A-F."
2853 i++;
2854 if (c != '.')
2855 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002856 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002857 //i is buf[i] index of the first not-yet-parsed char
2858 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002859
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002860 //ccnt is the number of chars in the number string, excluding possible
2861 //trailing "." and possible following trailing "\<newline>"(s).
2862 len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination
2863
Denys Vlasenko64074a12018-12-07 15:50:14 +01002864 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2865 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2866 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002867 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002868 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002869
Denys Vlasenko7d628012018-12-04 21:46:47 +01002870 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002871 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002872 bc_vec_push(&l->t.v, &start);
2873
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002874 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002875 // If we have hit a backslash, skip it. We don't have
2876 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002877 if (*buf == '\\') {
2878 buf += 2;
2879 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002880 continue;
2881 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002882 bc_vec_push(&l->t.v, buf);
2883 buf++;
2884 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002885 }
2886
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002887 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002888
Denys Vlasenko29301232018-12-11 15:29:32 +01002889 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002890}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002891#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002892# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002893#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002894
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002895static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002896{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002897 size_t i;
2898 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002899
2900 l->t.t = BC_LEX_NAME;
2901
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002902 i = 0;
2903 buf = l->buf + l->i - 1;
2904 for (;;) {
2905 char c = buf[i];
2906 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2907 i++;
2908 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002909
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002910#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002911 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2912 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2913 if (i > BC_MAX_STRING)
2914 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2915 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002916#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002917 bc_vec_string(&l->t.v, i, buf);
2918
2919 // Increment the index. We minus 1 because it has already been incremented.
2920 l->i += i - 1;
2921
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002922 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002923}
2924
2925static void bc_lex_init(BcLex *l, BcLexNext next)
2926{
2927 l->next = next;
Denys Vlasenko7d628012018-12-04 21:46:47 +01002928 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002929}
2930
2931static void bc_lex_free(BcLex *l)
2932{
2933 bc_vec_free(&l->t.v);
2934}
2935
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002936static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002937{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002938 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002939 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002940}
2941
2942static BcStatus bc_lex_next(BcLex *l)
2943{
2944 BcStatus s;
2945
2946 l->t.last = l->t.t;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002947 if (l->t.last == BC_LEX_EOF) return bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06002948
2949 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002950 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06002951 l->t.t = BC_LEX_EOF;
2952
2953 l->newline = (l->i == l->len);
2954 if (l->newline) return BC_STATUS_SUCCESS;
2955
2956 // Loop until failure or we don't have whitespace. This
2957 // is so the parser doesn't get inundated with whitespace.
2958 do {
2959 s = l->next(l);
2960 } while (!s && l->t.t == BC_LEX_WHITESPACE);
2961
2962 return s;
2963}
2964
2965static BcStatus bc_lex_text(BcLex *l, const char *text)
2966{
2967 l->buf = text;
2968 l->i = 0;
2969 l->len = strlen(text);
2970 l->t.t = l->t.last = BC_LEX_INVALID;
2971 return bc_lex_next(l);
2972}
2973
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002974#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06002975static BcStatus bc_lex_identifier(BcLex *l)
2976{
2977 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002978 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002979 const char *buf = l->buf + l->i - 1;
2980
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002981 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2982 const char *keyword8 = bc_lex_kws[i].name8;
2983 unsigned j = 0;
2984 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2985 j++;
2986 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002987 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002988 if (keyword8[j] != '\0')
2989 continue;
2990 match:
2991 // buf starts with keyword bc_lex_kws[i]
2992 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01002993 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002994 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002995 if (s) return s;
2996 }
2997
2998 // We minus 1 because the index has already been incremented.
2999 l->i += j - 1;
3000 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003001 }
3002
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003003 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003004
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003005 if (l->t.v.len > 2) {
3006 // Prevent this:
3007 // >>> qwe=1
3008 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3009 // '
3010 unsigned len = strchrnul(buf, '\n') - buf;
3011 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3012 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003013
3014 return s;
3015}
3016
3017static BcStatus bc_lex_string(BcLex *l)
3018{
3019 size_t len, nls = 0, i = l->i;
3020 char c;
3021
3022 l->t.t = BC_LEX_STR;
3023
3024 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n');
3025
3026 if (c == '\0') {
3027 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003028 return bc_error("string end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003029 }
3030
3031 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003032 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3033 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3034 if (len > BC_MAX_STRING)
3035 return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]");
3036 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003037 bc_vec_string(&l->t.v, len, l->buf + l->i);
3038
3039 l->i = i + 1;
3040 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003041 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003042
3043 return BC_STATUS_SUCCESS;
3044}
3045
3046static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3047{
3048 if (l->buf[l->i] == '=') {
3049 ++l->i;
3050 l->t.t = with;
3051 }
3052 else
3053 l->t.t = without;
3054}
3055
Denys Vlasenko29301232018-12-11 15:29:32 +01003056static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003057{
3058 size_t i, nls = 0;
3059 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003060
3061 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003062 i = ++l->i;
3063 for (;;) {
3064 char c = buf[i];
3065 check_star:
3066 if (c == '*') {
3067 c = buf[++i];
3068 if (c == '/')
3069 break;
3070 goto check_star;
3071 }
3072 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003073 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003074 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003075 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003076 nls += (c == '\n');
3077 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003078 }
3079
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003080 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003081 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003082 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003083
Denys Vlasenko29301232018-12-11 15:29:32 +01003084 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003085}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003086#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003087# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003088#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003089
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003090static FAST_FUNC BcStatus bc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003091{
3092 BcStatus s = BC_STATUS_SUCCESS;
3093 char c = l->buf[l->i++], c2;
3094
3095 // This is the workhorse of the lexer.
3096 switch (c) {
3097
3098 case '\0':
3099 case '\n':
3100 {
3101 l->newline = true;
3102 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3103 break;
3104 }
3105
3106 case '\t':
3107 case '\v':
3108 case '\f':
3109 case '\r':
3110 case ' ':
3111 {
3112 bc_lex_whitespace(l);
3113 break;
3114 }
3115
3116 case '!':
3117 {
3118 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3119
3120 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003121 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Gavin Howard01055ba2018-11-03 11:00:21 -06003122 if (s) return s;
3123 }
3124
3125 break;
3126 }
3127
3128 case '"':
3129 {
3130 s = bc_lex_string(l);
3131 break;
3132 }
3133
3134 case '#':
3135 {
Denys Vlasenko00646792018-12-05 18:12:27 +01003136 s = bc_POSIX_does_not_allow("'#' script comments");
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 if (s) return s;
3138
3139 bc_lex_lineComment(l);
3140
3141 break;
3142 }
3143
3144 case '%':
3145 {
3146 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3147 break;
3148 }
3149
3150 case '&':
3151 {
3152 c2 = l->buf[l->i];
3153 if (c2 == '&') {
3154
Denys Vlasenko00646792018-12-05 18:12:27 +01003155 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 if (s) return s;
3157
3158 ++l->i;
3159 l->t.t = BC_LEX_OP_BOOL_AND;
3160 }
3161 else {
3162 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003163 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 }
3165
3166 break;
3167 }
3168
3169 case '(':
3170 case ')':
3171 {
3172 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3173 break;
3174 }
3175
3176 case '*':
3177 {
3178 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3179 break;
3180 }
3181
3182 case '+':
3183 {
3184 c2 = l->buf[l->i];
3185 if (c2 == '+') {
3186 ++l->i;
3187 l->t.t = BC_LEX_OP_INC;
3188 }
3189 else
3190 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3191 break;
3192 }
3193
3194 case ',':
3195 {
3196 l->t.t = BC_LEX_COMMA;
3197 break;
3198 }
3199
3200 case '-':
3201 {
3202 c2 = l->buf[l->i];
3203 if (c2 == '-') {
3204 ++l->i;
3205 l->t.t = BC_LEX_OP_DEC;
3206 }
3207 else
3208 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3209 break;
3210 }
3211
3212 case '.':
3213 {
3214 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003215 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 else {
3217 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003218 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 }
3220 break;
3221 }
3222
3223 case '/':
3224 {
3225 c2 = l->buf[l->i];
3226 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003227 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003228 else
3229 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3230 break;
3231 }
3232
3233 case '0':
3234 case '1':
3235 case '2':
3236 case '3':
3237 case '4':
3238 case '5':
3239 case '6':
3240 case '7':
3241 case '8':
3242 case '9':
3243 case 'A':
3244 case 'B':
3245 case 'C':
3246 case 'D':
3247 case 'E':
3248 case 'F':
3249 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003250 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003251 break;
3252 }
3253
3254 case ';':
3255 {
3256 l->t.t = BC_LEX_SCOLON;
3257 break;
3258 }
3259
3260 case '<':
3261 {
3262 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3263 break;
3264 }
3265
3266 case '=':
3267 {
3268 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3269 break;
3270 }
3271
3272 case '>':
3273 {
3274 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3275 break;
3276 }
3277
3278 case '[':
3279 case ']':
3280 {
3281 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3282 break;
3283 }
3284
3285 case '\\':
3286 {
3287 if (l->buf[l->i] == '\n') {
3288 l->t.t = BC_LEX_WHITESPACE;
3289 ++l->i;
3290 }
3291 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003292 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003293 break;
3294 }
3295
3296 case '^':
3297 {
3298 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3299 break;
3300 }
3301
3302 case 'a':
3303 case 'b':
3304 case 'c':
3305 case 'd':
3306 case 'e':
3307 case 'f':
3308 case 'g':
3309 case 'h':
3310 case 'i':
3311 case 'j':
3312 case 'k':
3313 case 'l':
3314 case 'm':
3315 case 'n':
3316 case 'o':
3317 case 'p':
3318 case 'q':
3319 case 'r':
3320 case 's':
3321 case 't':
3322 case 'u':
3323 case 'v':
3324 case 'w':
3325 case 'x':
3326 case 'y':
3327 case 'z':
3328 {
3329 s = bc_lex_identifier(l);
3330 break;
3331 }
3332
3333 case '{':
3334 case '}':
3335 {
3336 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3337 break;
3338 }
3339
3340 case '|':
3341 {
3342 c2 = l->buf[l->i];
3343
3344 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003345 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Gavin Howard01055ba2018-11-03 11:00:21 -06003346 if (s) return s;
3347
3348 ++l->i;
3349 l->t.t = BC_LEX_OP_BOOL_OR;
3350 }
3351 else {
3352 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003353 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003354 }
3355
3356 break;
3357 }
3358
3359 default:
3360 {
3361 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003362 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003363 break;
3364 }
3365 }
3366
3367 return s;
3368}
3369#endif // ENABLE_BC
3370
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003371#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003372static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003373{
Gavin Howard01055ba2018-11-03 11:00:21 -06003374 if (isspace(l->buf[l->i - 1])) {
3375 bc_lex_whitespace(l);
3376 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003377 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003378 RETURN_STATUS(bc_error("extended register"));
3379 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003380 }
3381 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003382 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003383 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003384 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003385 l->t.t = BC_LEX_NAME;
3386 }
3387
Denys Vlasenko29301232018-12-11 15:29:32 +01003388 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003389}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003390#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003391# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003392#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003393
Denys Vlasenko29301232018-12-11 15:29:32 +01003394static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003395{
3396 size_t depth = 1, nls = 0, i = l->i;
3397 char c;
3398
3399 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003400 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003401
3402 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3403
3404 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3405 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3406 nls += (c == '\n');
3407
3408 if (depth) bc_vec_push(&l->t.v, &c);
3409 }
3410
3411 if (c == '\0') {
3412 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003413 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003414 }
3415
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003416 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003417 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3418 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3419 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003420 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003421 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003422
3423 l->i = i;
3424 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003425 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003426
Denys Vlasenko29301232018-12-11 15:29:32 +01003427 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003428}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003429#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003430# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003431#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003432
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003433static FAST_FUNC BcStatus dc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003434{
3435 BcStatus s = BC_STATUS_SUCCESS;
3436 char c = l->buf[l->i++], c2;
3437 size_t i;
3438
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003439 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3440 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko29301232018-12-11 15:29:32 +01003441 return zdc_lex_register(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003442 }
3443
3444 if (c >= '%' && c <= '~' &&
3445 (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID)
3446 {
3447 return s;
3448 }
3449
3450 // This is the workhorse of the lexer.
3451 switch (c) {
3452
3453 case '\0':
3454 {
3455 l->t.t = BC_LEX_EOF;
3456 break;
3457 }
3458
3459 case '\n':
3460 case '\t':
3461 case '\v':
3462 case '\f':
3463 case '\r':
3464 case ' ':
3465 {
3466 l->newline = (c == '\n');
3467 bc_lex_whitespace(l);
3468 break;
3469 }
3470
3471 case '!':
3472 {
3473 c2 = l->buf[l->i];
3474
3475 if (c2 == '=')
3476 l->t.t = BC_LEX_OP_REL_NE;
3477 else if (c2 == '<')
3478 l->t.t = BC_LEX_OP_REL_LE;
3479 else if (c2 == '>')
3480 l->t.t = BC_LEX_OP_REL_GE;
3481 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003482 return bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003483
3484 ++l->i;
3485 break;
3486 }
3487
3488 case '#':
3489 {
3490 bc_lex_lineComment(l);
3491 break;
3492 }
3493
3494 case '.':
3495 {
3496 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003497 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003498 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003499 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003500 break;
3501 }
3502
3503 case '0':
3504 case '1':
3505 case '2':
3506 case '3':
3507 case '4':
3508 case '5':
3509 case '6':
3510 case '7':
3511 case '8':
3512 case '9':
3513 case 'A':
3514 case 'B':
3515 case 'C':
3516 case 'D':
3517 case 'E':
3518 case 'F':
3519 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003520 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003521 break;
3522 }
3523
3524 case '[':
3525 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003526 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003527 break;
3528 }
3529
3530 default:
3531 {
3532 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003533 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003534 break;
3535 }
3536 }
3537
3538 return s;
3539}
3540#endif // ENABLE_DC
3541
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003542static void bc_program_addFunc(char *name, size_t *idx);
3543
Gavin Howard01055ba2018-11-03 11:00:21 -06003544static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3545{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003546 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003547 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003548}
3549
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003550#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3551
Gavin Howard01055ba2018-11-03 11:00:21 -06003552static void bc_parse_pushName(BcParse *p, char *name)
3553{
3554 size_t i = 0, len = strlen(name);
3555
3556 for (; i < len; ++i) bc_parse_push(p, name[i]);
3557 bc_parse_push(p, BC_PARSE_STREND);
3558
3559 free(name);
3560}
3561
3562static void bc_parse_pushIndex(BcParse *p, size_t idx)
3563{
3564 unsigned char amt, i, nums[sizeof(size_t)];
3565
3566 for (amt = 0; idx; ++amt) {
3567 nums[amt] = (char) idx;
3568 idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT;
3569 }
3570
3571 bc_parse_push(p, amt);
3572 for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]);
3573}
3574
3575static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3576{
3577 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003578 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003579
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003580 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003581
3582 bc_parse_push(p, BC_INST_NUM);
3583 bc_parse_pushIndex(p, idx);
3584
3585 ++(*nexs);
3586 (*prev) = BC_INST_NUM;
3587}
3588
3589static BcStatus bc_parse_text(BcParse *p, const char *text)
3590{
3591 BcStatus s;
3592
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003593 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003594
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003595 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003596 p->l.t.t = BC_LEX_INVALID;
3597 s = p->parse(p);
3598 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003599 if (!BC_PARSE_CAN_EXEC(p))
3600 return bc_error("file is not executable");
Gavin Howard01055ba2018-11-03 11:00:21 -06003601 }
3602
3603 return bc_lex_text(&p->l, text);
3604}
3605
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003606// Called when parsing or execution detects a failure,
3607// resets execution structures.
3608static void bc_program_reset(void)
3609{
3610 BcFunc *f;
3611 BcInstPtr *ip;
3612
3613 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3614 bc_vec_pop_all(&G.prog.results);
3615
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003616 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003617 ip = bc_vec_top(&G.prog.stack);
3618 ip->idx = f->code.len;
3619}
3620
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003621#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003622 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003623
Denys Vlasenkod38af482018-12-04 19:11:02 +01003624// Called when bc/dc_parse_parse() detects a failure,
3625// resets parsing structures.
3626static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003627{
3628 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003629 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003630 bc_vec_pop_all(&p->func->code);
3631 bc_vec_pop_all(&p->func->autos);
3632 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003633
3634 bc_parse_updateFunc(p, BC_PROG_MAIN);
3635 }
3636
3637 p->l.i = p->l.len;
3638 p->l.t.t = BC_LEX_EOF;
3639 p->auto_part = (p->nbraces = 0);
3640
3641 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003642 bc_vec_pop_all(&p->exits);
3643 bc_vec_pop_all(&p->conds);
3644 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003645
Denys Vlasenkod38af482018-12-04 19:11:02 +01003646 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003647}
3648
3649static void bc_parse_free(BcParse *p)
3650{
3651 bc_vec_free(&p->flags);
3652 bc_vec_free(&p->exits);
3653 bc_vec_free(&p->conds);
3654 bc_vec_free(&p->ops);
3655 bc_lex_free(&p->l);
3656}
3657
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003658static void bc_parse_create(BcParse *p, size_t func,
Gavin Howard01055ba2018-11-03 11:00:21 -06003659 BcParseParse parse, BcLexNext next)
3660{
3661 memset(p, 0, sizeof(BcParse));
3662
3663 bc_lex_init(&p->l, next);
3664 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3665 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3666 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003667 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003668 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3669
3670 p->parse = parse;
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003671 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003672 bc_parse_updateFunc(p, func);
3673}
3674
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003675#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003676
3677#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3678#define BC_PARSE_LEAF(p, rparen) \
3679 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3680 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3681
3682// We can calculate the conversion between tokens and exprs by subtracting the
3683// position of the first operator in the lex enum and adding the position of the
3684// first in the expr enum. Note: This only works for binary operators.
3685#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3686
Gavin Howard01055ba2018-11-03 11:00:21 -06003687static BcStatus bc_parse_else(BcParse *p);
3688static BcStatus bc_parse_stmt(BcParse *p);
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003689static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003690static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Gavin Howard01055ba2018-11-03 11:00:21 -06003691
3692static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3693 size_t *nexprs, bool next)
3694{
3695 BcStatus s = BC_STATUS_SUCCESS;
3696 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003697 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3698 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003699
3700 while (p->ops.len > start) {
3701
3702 t = BC_PARSE_TOP_OP(p);
3703 if (t == BC_LEX_LPAREN) break;
3704
Denys Vlasenko65437582018-12-05 19:37:19 +01003705 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003706 if (l >= r && (l != r || !left)) break;
3707
3708 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3709 bc_vec_pop(&p->ops);
3710 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3711 }
3712
3713 bc_vec_push(&p->ops, &type);
3714 if (next) s = bc_lex_next(&p->l);
3715
3716 return s;
3717}
3718
3719static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
3720{
3721 BcLexType top;
3722
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003723 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003724 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003725 top = BC_PARSE_TOP_OP(p);
3726
3727 while (top != BC_LEX_LPAREN) {
3728
3729 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3730
3731 bc_vec_pop(&p->ops);
3732 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3733
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003734 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003735 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003736 top = BC_PARSE_TOP_OP(p);
3737 }
3738
3739 bc_vec_pop(&p->ops);
3740
3741 return bc_lex_next(&p->l);
3742}
3743
3744static BcStatus bc_parse_params(BcParse *p, uint8_t flags)
3745{
3746 BcStatus s;
3747 bool comma = false;
3748 size_t nparams;
3749
3750 s = bc_lex_next(&p->l);
3751 if (s) return s;
3752
3753 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
3754
3755 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3756 s = bc_parse_expr(p, flags, bc_parse_next_param);
3757 if (s) return s;
3758
3759 comma = p->l.t.t == BC_LEX_COMMA;
3760 if (comma) {
3761 s = bc_lex_next(&p->l);
3762 if (s) return s;
3763 }
3764 }
3765
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003766 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003767 bc_parse_push(p, BC_INST_CALL);
3768 bc_parse_pushIndex(p, nparams);
3769
3770 return BC_STATUS_SUCCESS;
3771}
3772
3773static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags)
3774{
3775 BcStatus s;
3776 BcId entry, *entry_ptr;
3777 size_t idx;
3778
3779 entry.name = name;
3780
3781 s = bc_parse_params(p, flags);
3782 if (s) goto err;
3783
3784 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003785 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003786 goto err;
3787 }
3788
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003789 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003790
3791 if (idx == BC_VEC_INVALID_IDX) {
3792 name = xstrdup(entry.name);
3793 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003794 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003795 free(entry.name);
3796 }
3797 else
3798 free(name);
3799
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003800 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003801 bc_parse_pushIndex(p, entry_ptr->idx);
3802
3803 return bc_lex_next(&p->l);
3804
3805err:
3806 free(name);
3807 return s;
3808}
3809
3810static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
3811{
3812 BcStatus s;
3813 char *name;
3814
3815 name = xstrdup(p->l.t.v.v);
3816 s = bc_lex_next(&p->l);
3817 if (s) goto err;
3818
3819 if (p->l.t.t == BC_LEX_LBRACKET) {
3820
3821 s = bc_lex_next(&p->l);
3822 if (s) goto err;
3823
3824 if (p->l.t.t == BC_LEX_RBRACKET) {
3825
3826 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003827 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003828 goto err;
3829 }
3830
3831 *type = BC_INST_ARRAY;
3832 }
3833 else {
3834
3835 *type = BC_INST_ARRAY_ELEM;
3836
3837 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3838 s = bc_parse_expr(p, flags, bc_parse_next_elem);
3839 if (s) goto err;
3840 }
3841
3842 s = bc_lex_next(&p->l);
3843 if (s) goto err;
3844 bc_parse_push(p, *type);
3845 bc_parse_pushName(p, name);
3846 }
3847 else if (p->l.t.t == BC_LEX_LPAREN) {
3848
3849 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003850 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003851 goto err;
3852 }
3853
3854 *type = BC_INST_CALL;
3855 s = bc_parse_call(p, name, flags);
3856 }
3857 else {
3858 *type = BC_INST_VAR;
3859 bc_parse_push(p, BC_INST_VAR);
3860 bc_parse_pushName(p, name);
3861 }
3862
3863 return s;
3864
3865err:
3866 free(name);
3867 return s;
3868}
3869
3870static BcStatus bc_parse_read(BcParse *p)
3871{
3872 BcStatus s;
3873
3874 s = bc_lex_next(&p->l);
3875 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003876 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003877
3878 s = bc_lex_next(&p->l);
3879 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003880 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003881
3882 bc_parse_push(p, BC_INST_READ);
3883
3884 return bc_lex_next(&p->l);
3885}
3886
3887static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
3888 BcInst *prev)
3889{
3890 BcStatus s;
3891
3892 s = bc_lex_next(&p->l);
3893 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003894 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003895
3896 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3897
3898 s = bc_lex_next(&p->l);
3899 if (s) return s;
3900
3901 s = bc_parse_expr(p, flags, bc_parse_next_rel);
3902 if (s) return s;
3903
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003904 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003905
3906 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3907 bc_parse_push(p, *prev);
3908
3909 return bc_lex_next(&p->l);
3910}
3911
3912static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
3913{
3914 BcStatus s;
3915
3916 s = bc_lex_next(&p->l);
3917 if (s) return s;
3918
3919 if (p->l.t.t != BC_LEX_LPAREN) {
3920 *type = BC_INST_SCALE;
3921 bc_parse_push(p, BC_INST_SCALE);
3922 return BC_STATUS_SUCCESS;
3923 }
3924
3925 *type = BC_INST_SCALE_FUNC;
3926 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3927
3928 s = bc_lex_next(&p->l);
3929 if (s) return s;
3930
3931 s = bc_parse_expr(p, flags, bc_parse_next_rel);
3932 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003933 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003934 bc_parse_push(p, BC_INST_SCALE_FUNC);
3935
3936 return bc_lex_next(&p->l);
3937}
3938
3939static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
3940 size_t *nexprs, uint8_t flags)
3941{
3942 BcStatus s;
3943 BcLexType type;
3944 char inst;
3945 BcInst etype = *prev;
3946
3947 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
3948 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
3949 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
3950 {
3951 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3952 bc_parse_push(p, inst);
3953 s = bc_lex_next(&p->l);
3954 }
3955 else {
3956
3957 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3958 *paren_expr = true;
3959
3960 s = bc_lex_next(&p->l);
3961 if (s) return s;
3962 type = p->l.t.t;
3963
3964 // Because we parse the next part of the expression
3965 // right here, we need to increment this.
3966 *nexprs = *nexprs + 1;
3967
3968 switch (type) {
3969
3970 case BC_LEX_NAME:
3971 {
3972 s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
3973 break;
3974 }
3975
3976 case BC_LEX_KEY_IBASE:
3977 case BC_LEX_KEY_LAST:
3978 case BC_LEX_KEY_OBASE:
3979 {
3980 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
3981 s = bc_lex_next(&p->l);
3982 break;
3983 }
3984
3985 case BC_LEX_KEY_SCALE:
3986 {
3987 s = bc_lex_next(&p->l);
3988 if (s) return s;
3989 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003990 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003991 else
3992 bc_parse_push(p, BC_INST_SCALE);
3993 break;
3994 }
3995
3996 default:
3997 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003998 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003999 break;
4000 }
4001 }
4002
4003 if (!s) bc_parse_push(p, inst);
4004 }
4005
4006 return s;
4007}
4008
4009static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
4010 bool rparen, size_t *nexprs)
4011{
4012 BcStatus s;
4013 BcLexType type;
4014 BcInst etype = *prev;
4015
4016 s = bc_lex_next(&p->l);
4017 if (s) return s;
4018
4019 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4020 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4021 BC_LEX_OP_MINUS :
4022 BC_LEX_NEG;
4023 *prev = BC_PARSE_TOKEN_INST(type);
4024
4025 // We can just push onto the op stack because this is the largest
4026 // precedence operator that gets pushed. Inc/dec does not.
4027 if (type != BC_LEX_OP_MINUS)
4028 bc_vec_push(&p->ops, &type);
4029 else
4030 s = bc_parse_operator(p, type, ops_bgn, nexprs, false);
4031
4032 return s;
4033}
4034
4035static BcStatus bc_parse_string(BcParse *p, char inst)
4036{
4037 char *str = xstrdup(p->l.t.v.v);
4038
4039 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004040 bc_parse_pushIndex(p, G.prog.strs.len);
4041 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004042 bc_parse_push(p, inst);
4043
4044 return bc_lex_next(&p->l);
4045}
4046
4047static BcStatus bc_parse_print(BcParse *p)
4048{
4049 BcStatus s;
4050 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004051 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06004052
4053 s = bc_lex_next(&p->l);
4054 if (s) return s;
4055
4056 type = p->l.t.t;
4057
4058 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004059 return bc_error("bad print statement");
Gavin Howard01055ba2018-11-03 11:00:21 -06004060
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004061 comma = false;
4062 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004063
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004064 if (type == BC_LEX_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004065 s = bc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004066 if (s) return s;
4067 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004068 s = bc_parse_expr(p, 0, bc_parse_next_print);
4069 if (s) return s;
4070 bc_parse_push(p, BC_INST_PRINT_POP);
4071 }
4072
Gavin Howard01055ba2018-11-03 11:00:21 -06004073 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004074 if (comma) {
4075 s = bc_lex_next(&p->l);
4076 if (s) return s;
4077 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004078 type = p->l.t.t;
4079 }
4080
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004081 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004082
4083 return bc_lex_next(&p->l);
4084}
4085
4086static BcStatus bc_parse_return(BcParse *p)
4087{
4088 BcStatus s;
4089 BcLexType t;
4090 bool paren;
4091
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004092 if (!BC_PARSE_FUNC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004093
4094 s = bc_lex_next(&p->l);
4095 if (s) return s;
4096
4097 t = p->l.t.t;
4098 paren = t == BC_LEX_LPAREN;
4099
4100 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4101 bc_parse_push(p, BC_INST_RET0);
4102 else {
4103
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004104 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004105 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004106 bc_parse_push(p, BC_INST_RET0);
4107 s = bc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004108 }
Denys Vlasenko452df922018-12-05 20:28:26 +01004109 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004110
4111 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004112 s = bc_POSIX_requires("parentheses around return expressions");
Gavin Howard01055ba2018-11-03 11:00:21 -06004113 if (s) return s;
4114 }
4115
4116 bc_parse_push(p, BC_INST_RET);
4117 }
4118
4119 return s;
4120}
4121
4122static BcStatus bc_parse_endBody(BcParse *p, bool brace)
4123{
4124 BcStatus s = BC_STATUS_SUCCESS;
4125
4126 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004127 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004128
4129 if (brace) {
4130
4131 if (p->l.t.t == BC_LEX_RBRACE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004132 if (!p->nbraces) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004133 --p->nbraces;
4134 s = bc_lex_next(&p->l);
4135 if (s) return s;
4136 }
4137 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004138 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004139 }
4140
4141 if (BC_PARSE_IF(p)) {
4142
4143 uint8_t *flag_ptr;
4144
4145 while (p->l.t.t == BC_LEX_NLINE) {
4146 s = bc_lex_next(&p->l);
4147 if (s) return s;
4148 }
4149
4150 bc_vec_pop(&p->flags);
4151
4152 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4153 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4154
4155 if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p);
4156 }
4157 else if (BC_PARSE_ELSE(p)) {
4158
4159 BcInstPtr *ip;
4160 size_t *label;
4161
4162 bc_vec_pop(&p->flags);
4163
4164 ip = bc_vec_top(&p->exits);
4165 label = bc_vec_item(&p->func->labels, ip->idx);
4166 *label = p->func->code.len;
4167
4168 bc_vec_pop(&p->exits);
4169 }
4170 else if (BC_PARSE_FUNC_INNER(p)) {
4171 bc_parse_push(p, BC_INST_RET0);
4172 bc_parse_updateFunc(p, BC_PROG_MAIN);
4173 bc_vec_pop(&p->flags);
4174 }
4175 else {
4176
4177 BcInstPtr *ip = bc_vec_top(&p->exits);
4178 size_t *label = bc_vec_top(&p->conds);
4179
4180 bc_parse_push(p, BC_INST_JUMP);
4181 bc_parse_pushIndex(p, *label);
4182
4183 label = bc_vec_item(&p->func->labels, ip->idx);
4184 *label = p->func->code.len;
4185
4186 bc_vec_pop(&p->flags);
4187 bc_vec_pop(&p->exits);
4188 bc_vec_pop(&p->conds);
4189 }
4190
4191 return s;
4192}
4193
4194static void bc_parse_startBody(BcParse *p, uint8_t flags)
4195{
4196 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4197 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4198 flags |= BC_PARSE_FLAG_BODY;
4199 bc_vec_push(&p->flags, &flags);
4200}
4201
4202static void bc_parse_noElse(BcParse *p)
4203{
4204 BcInstPtr *ip;
4205 size_t *label;
4206 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4207
4208 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4209
4210 ip = bc_vec_top(&p->exits);
4211 label = bc_vec_item(&p->func->labels, ip->idx);
4212 *label = p->func->code.len;
4213
4214 bc_vec_pop(&p->exits);
4215}
4216
4217static BcStatus bc_parse_if(BcParse *p)
4218{
4219 BcStatus s;
4220 BcInstPtr ip;
4221
4222 s = bc_lex_next(&p->l);
4223 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004224 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004225
4226 s = bc_lex_next(&p->l);
4227 if (s) return s;
4228 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4229 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004230 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004231
4232 s = bc_lex_next(&p->l);
4233 if (s) return s;
4234 bc_parse_push(p, BC_INST_JUMP_ZERO);
4235
4236 ip.idx = p->func->labels.len;
4237 ip.func = ip.len = 0;
4238
4239 bc_parse_pushIndex(p, ip.idx);
4240 bc_vec_push(&p->exits, &ip);
4241 bc_vec_push(&p->func->labels, &ip.idx);
4242 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4243
4244 return BC_STATUS_SUCCESS;
4245}
4246
4247static BcStatus bc_parse_else(BcParse *p)
4248{
4249 BcInstPtr ip;
4250
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004251 if (!BC_PARSE_IF_END(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004252
4253 ip.idx = p->func->labels.len;
4254 ip.func = ip.len = 0;
4255
4256 bc_parse_push(p, BC_INST_JUMP);
4257 bc_parse_pushIndex(p, ip.idx);
4258
4259 bc_parse_noElse(p);
4260
4261 bc_vec_push(&p->exits, &ip);
4262 bc_vec_push(&p->func->labels, &ip.idx);
4263 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4264
4265 return bc_lex_next(&p->l);
4266}
4267
4268static BcStatus bc_parse_while(BcParse *p)
4269{
4270 BcStatus s;
4271 BcInstPtr ip;
4272
4273 s = bc_lex_next(&p->l);
4274 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004275 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004276 s = bc_lex_next(&p->l);
4277 if (s) return s;
4278
4279 ip.idx = p->func->labels.len;
4280
4281 bc_vec_push(&p->func->labels, &p->func->code.len);
4282 bc_vec_push(&p->conds, &ip.idx);
4283
4284 ip.idx = p->func->labels.len;
4285 ip.func = 1;
4286 ip.len = 0;
4287
4288 bc_vec_push(&p->exits, &ip);
4289 bc_vec_push(&p->func->labels, &ip.idx);
4290
4291 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4292 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004293 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004294 s = bc_lex_next(&p->l);
4295 if (s) return s;
4296
4297 bc_parse_push(p, BC_INST_JUMP_ZERO);
4298 bc_parse_pushIndex(p, ip.idx);
4299 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4300
4301 return BC_STATUS_SUCCESS;
4302}
4303
4304static BcStatus bc_parse_for(BcParse *p)
4305{
4306 BcStatus s;
4307 BcInstPtr ip;
4308 size_t cond_idx, exit_idx, body_idx, update_idx;
4309
4310 s = bc_lex_next(&p->l);
4311 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004312 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004313 s = bc_lex_next(&p->l);
4314 if (s) return s;
4315
4316 if (p->l.t.t != BC_LEX_SCOLON)
4317 s = bc_parse_expr(p, 0, bc_parse_next_for);
4318 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004319 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004320
4321 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004322 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004323 s = bc_lex_next(&p->l);
4324 if (s) return s;
4325
4326 cond_idx = p->func->labels.len;
4327 update_idx = cond_idx + 1;
4328 body_idx = update_idx + 1;
4329 exit_idx = body_idx + 1;
4330
4331 bc_vec_push(&p->func->labels, &p->func->code.len);
4332
4333 if (p->l.t.t != BC_LEX_SCOLON)
4334 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
4335 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004336 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004337
4338 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004339 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004340
4341 s = bc_lex_next(&p->l);
4342 if (s) return s;
4343
4344 bc_parse_push(p, BC_INST_JUMP_ZERO);
4345 bc_parse_pushIndex(p, exit_idx);
4346 bc_parse_push(p, BC_INST_JUMP);
4347 bc_parse_pushIndex(p, body_idx);
4348
4349 ip.idx = p->func->labels.len;
4350
4351 bc_vec_push(&p->conds, &update_idx);
4352 bc_vec_push(&p->func->labels, &p->func->code.len);
4353
4354 if (p->l.t.t != BC_LEX_RPAREN)
4355 s = bc_parse_expr(p, 0, bc_parse_next_rel);
4356 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004357 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004358
4359 if (s) return s;
4360
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004361 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004362 bc_parse_push(p, BC_INST_JUMP);
4363 bc_parse_pushIndex(p, cond_idx);
4364 bc_vec_push(&p->func->labels, &p->func->code.len);
4365
4366 ip.idx = exit_idx;
4367 ip.func = 1;
4368 ip.len = 0;
4369
4370 bc_vec_push(&p->exits, &ip);
4371 bc_vec_push(&p->func->labels, &ip.idx);
4372 bc_lex_next(&p->l);
4373 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4374
4375 return BC_STATUS_SUCCESS;
4376}
4377
4378static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type)
4379{
4380 BcStatus s;
4381 size_t i;
4382 BcInstPtr *ip;
4383
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004384 if (!BC_PARSE_LOOP(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004385
4386 if (type == BC_LEX_KEY_BREAK) {
4387
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004388 if (p->exits.len == 0) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004389
4390 i = p->exits.len - 1;
4391 ip = bc_vec_item(&p->exits, i);
4392
4393 while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004394 if (i >= p->exits.len && !ip->func) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004395
4396 i = ip->idx;
4397 }
4398 else
4399 i = *((size_t *) bc_vec_top(&p->conds));
4400
4401 bc_parse_push(p, BC_INST_JUMP);
4402 bc_parse_pushIndex(p, i);
4403
4404 s = bc_lex_next(&p->l);
4405 if (s) return s;
4406
4407 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004408 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004409
4410 return bc_lex_next(&p->l);
4411}
4412
4413static BcStatus bc_parse_func(BcParse *p)
4414{
4415 BcStatus s;
4416 bool var, comma = false;
4417 uint8_t flags;
4418 char *name;
4419
4420 s = bc_lex_next(&p->l);
4421 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004422 if (p->l.t.t != BC_LEX_NAME)
4423 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004424
4425 name = xstrdup(p->l.t.v.v);
4426 bc_parse_addFunc(p, name, &p->fidx);
4427
4428 s = bc_lex_next(&p->l);
4429 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004430 if (p->l.t.t != BC_LEX_LPAREN)
4431 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004432 s = bc_lex_next(&p->l);
4433 if (s) return s;
4434
4435 while (p->l.t.t != BC_LEX_RPAREN) {
4436
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004437 if (p->l.t.t != BC_LEX_NAME)
4438 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004439
4440 ++p->func->nparams;
4441
4442 name = xstrdup(p->l.t.v.v);
4443 s = bc_lex_next(&p->l);
4444 if (s) goto err;
4445
4446 var = p->l.t.t != BC_LEX_LBRACKET;
4447
4448 if (!var) {
4449
4450 s = bc_lex_next(&p->l);
4451 if (s) goto err;
4452
4453 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004454 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004455 goto err;
4456 }
4457
4458 s = bc_lex_next(&p->l);
4459 if (s) goto err;
4460 }
4461
4462 comma = p->l.t.t == BC_LEX_COMMA;
4463 if (comma) {
4464 s = bc_lex_next(&p->l);
4465 if (s) goto err;
4466 }
4467
Denys Vlasenko29301232018-12-11 15:29:32 +01004468 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004469 if (s) goto err;
4470 }
4471
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004472 if (comma) return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004473
4474 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4475 bc_parse_startBody(p, flags);
4476
4477 s = bc_lex_next(&p->l);
4478 if (s) return s;
4479
4480 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004481 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004482
4483 return s;
4484
4485err:
4486 free(name);
4487 return s;
4488}
4489
4490static BcStatus bc_parse_auto(BcParse *p)
4491{
4492 BcStatus s;
4493 bool comma, var, one;
4494 char *name;
4495
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004496 if (!p->auto_part) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004497 s = bc_lex_next(&p->l);
4498 if (s) return s;
4499
4500 p->auto_part = comma = false;
4501 one = p->l.t.t == BC_LEX_NAME;
4502
4503 while (p->l.t.t == BC_LEX_NAME) {
4504
4505 name = xstrdup(p->l.t.v.v);
4506 s = bc_lex_next(&p->l);
4507 if (s) goto err;
4508
4509 var = p->l.t.t != BC_LEX_LBRACKET;
4510 if (!var) {
4511
4512 s = bc_lex_next(&p->l);
4513 if (s) goto err;
4514
4515 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004516 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004517 goto err;
4518 }
4519
4520 s = bc_lex_next(&p->l);
4521 if (s) goto err;
4522 }
4523
4524 comma = p->l.t.t == BC_LEX_COMMA;
4525 if (comma) {
4526 s = bc_lex_next(&p->l);
4527 if (s) goto err;
4528 }
4529
Denys Vlasenko29301232018-12-11 15:29:32 +01004530 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004531 if (s) goto err;
4532 }
4533
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004534 if (comma) return bc_error("bad function definition");
Denys Vlasenkoabbc4332018-12-03 21:46:41 +01004535 if (!one) return bc_error("no auto variable found");
Gavin Howard01055ba2018-11-03 11:00:21 -06004536
4537 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004538 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004539
4540 return bc_lex_next(&p->l);
4541
4542err:
4543 free(name);
4544 return s;
4545}
4546
4547static BcStatus bc_parse_body(BcParse *p, bool brace)
4548{
4549 BcStatus s = BC_STATUS_SUCCESS;
4550 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4551
4552 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4553
4554 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
4555
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004556 if (!brace) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004557 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4558
4559 if (!p->auto_part) {
4560 s = bc_parse_auto(p);
4561 if (s) return s;
4562 }
4563
4564 if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l);
4565 }
4566 else {
4567 s = bc_parse_stmt(p);
4568 if (!s && !brace) s = bc_parse_endBody(p, false);
4569 }
4570
4571 return s;
4572}
4573
4574static BcStatus bc_parse_stmt(BcParse *p)
4575{
4576 BcStatus s = BC_STATUS_SUCCESS;
4577
4578 switch (p->l.t.t) {
4579
4580 case BC_LEX_NLINE:
4581 {
4582 return bc_lex_next(&p->l);
4583 }
4584
4585 case BC_LEX_KEY_ELSE:
4586 {
4587 p->auto_part = false;
4588 break;
4589 }
4590
4591 case BC_LEX_LBRACE:
4592 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004593 if (!BC_PARSE_BODY(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004594
4595 ++p->nbraces;
4596 s = bc_lex_next(&p->l);
4597 if (s) return s;
4598
4599 return bc_parse_body(p, true);
4600 }
4601
4602 case BC_LEX_KEY_AUTO:
4603 {
4604 return bc_parse_auto(p);
4605 }
4606
4607 default:
4608 {
4609 p->auto_part = false;
4610
4611 if (BC_PARSE_IF_END(p)) {
4612 bc_parse_noElse(p);
4613 return BC_STATUS_SUCCESS;
4614 }
4615 else if (BC_PARSE_BODY(p))
4616 return bc_parse_body(p, false);
4617
4618 break;
4619 }
4620 }
4621
4622 switch (p->l.t.t) {
4623
4624 case BC_LEX_OP_INC:
4625 case BC_LEX_OP_DEC:
4626 case BC_LEX_OP_MINUS:
4627 case BC_LEX_OP_BOOL_NOT:
4628 case BC_LEX_LPAREN:
4629 case BC_LEX_NAME:
4630 case BC_LEX_NUMBER:
4631 case BC_LEX_KEY_IBASE:
4632 case BC_LEX_KEY_LAST:
4633 case BC_LEX_KEY_LENGTH:
4634 case BC_LEX_KEY_OBASE:
4635 case BC_LEX_KEY_READ:
4636 case BC_LEX_KEY_SCALE:
4637 case BC_LEX_KEY_SQRT:
4638 {
4639 s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
4640 break;
4641 }
4642
4643 case BC_LEX_KEY_ELSE:
4644 {
4645 s = bc_parse_else(p);
4646 break;
4647 }
4648
4649 case BC_LEX_SCOLON:
4650 {
4651 while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l);
4652 break;
4653 }
4654
4655 case BC_LEX_RBRACE:
4656 {
4657 s = bc_parse_endBody(p, true);
4658 break;
4659 }
4660
4661 case BC_LEX_STR:
4662 {
4663 s = bc_parse_string(p, BC_INST_PRINT_STR);
4664 break;
4665 }
4666
4667 case BC_LEX_KEY_BREAK:
4668 case BC_LEX_KEY_CONTINUE:
4669 {
4670 s = bc_parse_loopExit(p, p->l.t.t);
4671 break;
4672 }
4673
4674 case BC_LEX_KEY_FOR:
4675 {
4676 s = bc_parse_for(p);
4677 break;
4678 }
4679
4680 case BC_LEX_KEY_HALT:
4681 {
4682 bc_parse_push(p, BC_INST_HALT);
4683 s = bc_lex_next(&p->l);
4684 break;
4685 }
4686
4687 case BC_LEX_KEY_IF:
4688 {
4689 s = bc_parse_if(p);
4690 break;
4691 }
4692
4693 case BC_LEX_KEY_LIMITS:
4694 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004695 // "limits" is a compile-time command,
4696 // the output is produced at _parse time_.
Gavin Howard01055ba2018-11-03 11:00:21 -06004697 s = bc_lex_next(&p->l);
4698 if (s) return s;
Denys Vlasenko64074a12018-12-07 15:50:14 +01004699 printf(
4700 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4701 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4702 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4703 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4704 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4705 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4706 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4707 "Number of vars = "BC_MAX_VARS_STR "\n"
4708 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004709 break;
4710 }
4711
4712 case BC_LEX_KEY_PRINT:
4713 {
4714 s = bc_parse_print(p);
4715 break;
4716 }
4717
4718 case BC_LEX_KEY_QUIT:
4719 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004720 // "quit" is a compile-time command. For example,
4721 // "if (0 == 1) quit" terminates when parsing the statement,
4722 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004723 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004724 }
4725
4726 case BC_LEX_KEY_RETURN:
4727 {
4728 s = bc_parse_return(p);
4729 break;
4730 }
4731
4732 case BC_LEX_KEY_WHILE:
4733 {
4734 s = bc_parse_while(p);
4735 break;
4736 }
4737
4738 default:
4739 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004740 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004741 break;
4742 }
4743 }
4744
4745 return s;
4746}
4747
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01004748static FAST_FUNC BcStatus bc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004749{
4750 BcStatus s;
4751
4752 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004753 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 -06004754 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004755 if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004756 s = bc_parse_func(p);
4757 }
4758 else
4759 s = bc_parse_stmt(p);
4760
Denys Vlasenkod38af482018-12-04 19:11:02 +01004761 if (s || G_interrupt) {
4762 bc_parse_reset(p);
4763 s = BC_STATUS_FAILURE;
4764 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004765
4766 return s;
4767}
4768
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004769// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004770static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004771{
4772 BcStatus s = BC_STATUS_SUCCESS;
4773 BcInst prev = BC_INST_PRINT;
4774 BcLexType top, t = p->l.t.t;
4775 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004776 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004777 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4778
4779 paren_first = p->l.t.t == BC_LEX_LPAREN;
4780 nparens = nrelops = 0;
4781 paren_expr = rprn = done = get_token = assign = false;
4782 bin_last = true;
4783
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004784 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004785 switch (t) {
4786
4787 case BC_LEX_OP_INC:
4788 case BC_LEX_OP_DEC:
4789 {
4790 s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
4791 rprn = get_token = bin_last = false;
4792 break;
4793 }
4794
4795 case BC_LEX_OP_MINUS:
4796 {
4797 s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
4798 rprn = get_token = false;
4799 bin_last = prev == BC_INST_MINUS;
4800 break;
4801 }
4802
4803 case BC_LEX_OP_ASSIGN_POWER:
4804 case BC_LEX_OP_ASSIGN_MULTIPLY:
4805 case BC_LEX_OP_ASSIGN_DIVIDE:
4806 case BC_LEX_OP_ASSIGN_MODULUS:
4807 case BC_LEX_OP_ASSIGN_PLUS:
4808 case BC_LEX_OP_ASSIGN_MINUS:
4809 case BC_LEX_OP_ASSIGN:
4810 {
4811 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4812 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4813 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4814 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004815 s = bc_error("bad assignment:"
4816 " left side must be scale,"
4817 " ibase, obase, last, var,"
4818 " or array element"
4819 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004820 break;
4821 }
4822 }
4823 // Fallthrough.
4824 case BC_LEX_OP_POWER:
4825 case BC_LEX_OP_MULTIPLY:
4826 case BC_LEX_OP_DIVIDE:
4827 case BC_LEX_OP_MODULUS:
4828 case BC_LEX_OP_PLUS:
4829 case BC_LEX_OP_REL_EQ:
4830 case BC_LEX_OP_REL_LE:
4831 case BC_LEX_OP_REL_GE:
4832 case BC_LEX_OP_REL_NE:
4833 case BC_LEX_OP_REL_LT:
4834 case BC_LEX_OP_REL_GT:
4835 case BC_LEX_OP_BOOL_NOT:
4836 case BC_LEX_OP_BOOL_OR:
4837 case BC_LEX_OP_BOOL_AND:
4838 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004839 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4840 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4841 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004842 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004843 }
4844
4845 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4846 prev = BC_PARSE_TOKEN_INST(t);
4847 s = bc_parse_operator(p, t, ops_bgn, &nexprs, true);
4848 rprn = get_token = false;
4849 bin_last = t != BC_LEX_OP_BOOL_NOT;
4850
4851 break;
4852 }
4853
4854 case BC_LEX_LPAREN:
4855 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004856 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004857 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004858 ++nparens;
4859 paren_expr = rprn = bin_last = false;
4860 get_token = true;
4861 bc_vec_push(&p->ops, &t);
4862
4863 break;
4864 }
4865
4866 case BC_LEX_RPAREN:
4867 {
4868 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004869 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004870
4871 if (nparens == 0) {
4872 s = BC_STATUS_SUCCESS;
4873 done = true;
4874 get_token = false;
4875 break;
4876 }
4877 else if (!paren_expr)
4878 return BC_STATUS_PARSE_EMPTY_EXP;
4879
4880 --nparens;
4881 paren_expr = rprn = true;
4882 get_token = bin_last = false;
4883
4884 s = bc_parse_rightParen(p, ops_bgn, &nexprs);
4885
4886 break;
4887 }
4888
4889 case BC_LEX_NAME:
4890 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004891 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004892 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004893 paren_expr = true;
4894 rprn = get_token = bin_last = false;
4895 s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
4896 ++nexprs;
4897
4898 break;
4899 }
4900
4901 case BC_LEX_NUMBER:
4902 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004903 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004904 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004905 bc_parse_number(p, &prev, &nexprs);
4906 paren_expr = get_token = true;
4907 rprn = bin_last = false;
4908
4909 break;
4910 }
4911
4912 case BC_LEX_KEY_IBASE:
4913 case BC_LEX_KEY_LAST:
4914 case BC_LEX_KEY_OBASE:
4915 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004916 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004917 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004918 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4919 bc_parse_push(p, (char) prev);
4920
4921 paren_expr = get_token = true;
4922 rprn = bin_last = false;
4923 ++nexprs;
4924
4925 break;
4926 }
4927
4928 case BC_LEX_KEY_LENGTH:
4929 case BC_LEX_KEY_SQRT:
4930 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004931 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004932 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004933 s = bc_parse_builtin(p, t, flags, &prev);
4934 paren_expr = true;
4935 rprn = get_token = bin_last = false;
4936 ++nexprs;
4937
4938 break;
4939 }
4940
4941 case BC_LEX_KEY_READ:
4942 {
4943 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004944 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004945 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004946 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004947 else
4948 s = bc_parse_read(p);
4949
4950 paren_expr = true;
4951 rprn = get_token = bin_last = false;
4952 ++nexprs;
4953 prev = BC_INST_READ;
4954
4955 break;
4956 }
4957
4958 case BC_LEX_KEY_SCALE:
4959 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004960 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004961 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004962 s = bc_parse_scale(p, &prev, flags);
4963 paren_expr = true;
4964 rprn = get_token = bin_last = false;
4965 ++nexprs;
4966 prev = BC_INST_SCALE;
4967
4968 break;
4969 }
4970
4971 default:
4972 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004973 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004974 break;
4975 }
4976 }
4977
4978 if (!s && get_token) s = bc_lex_next(&p->l);
4979 }
4980
4981 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004982 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004983
4984 while (p->ops.len > ops_bgn) {
4985
4986 top = BC_PARSE_TOP_OP(p);
4987 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4988
4989 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004990 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004991
4992 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
4993
4994 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4995 bc_vec_pop(&p->ops);
4996 }
4997
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004998 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004999 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005000
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005001 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
5002 for (;;) {
5003 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005004 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005005 if (next & 0x80) // last element?
5006 break;
5007 next >>= 8;
5008 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005009 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005010 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06005011
5012 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01005013 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Gavin Howard01055ba2018-11-03 11:00:21 -06005014 if (s) return s;
5015 }
5016 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01005017 s = bc_POSIX_requires("exactly one comparison operator per condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06005018 if (s) return s;
5019 }
5020
5021 if (flags & BC_PARSE_PRINT) {
5022 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
5023 bc_parse_push(p, BC_INST_POP);
5024 }
5025
5026 return s;
5027}
5028
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01005029static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
5030{
5031 BcStatus s;
5032
5033 s = bc_parse_expr_empty_ok(p, flags, next);
5034 if (s == BC_STATUS_PARSE_EMPTY_EXP)
5035 return bc_error("empty expression");
5036 return s;
5037}
5038
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005039static void bc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005040{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005041 bc_parse_create(p, func, bc_parse_parse, bc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005042}
5043
5044static BcStatus bc_parse_expression(BcParse *p, uint8_t flags)
5045{
5046 return bc_parse_expr(p, flags, bc_parse_next_read);
5047}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005048
Gavin Howard01055ba2018-11-03 11:00:21 -06005049#endif // ENABLE_BC
5050
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005051#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005052
5053#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
5054
Gavin Howard01055ba2018-11-03 11:00:21 -06005055static BcStatus dc_parse_register(BcParse *p)
5056{
5057 BcStatus s;
5058 char *name;
5059
5060 s = bc_lex_next(&p->l);
5061 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005062 if (p->l.t.t != BC_LEX_NAME) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005063
5064 name = xstrdup(p->l.t.v.v);
5065 bc_parse_pushName(p, name);
5066
5067 return s;
5068}
5069
5070static BcStatus dc_parse_string(BcParse *p)
5071{
5072 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005073 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005074
5075 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
5076 name = xstrdup(b);
5077
5078 str = xstrdup(p->l.t.v.v);
5079 bc_parse_push(p, BC_INST_STR);
5080 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005081 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005082 bc_parse_addFunc(p, name, &idx);
5083
5084 return bc_lex_next(&p->l);
5085}
5086
5087static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
5088{
5089 BcStatus s;
5090
5091 bc_parse_push(p, inst);
5092 if (name) {
5093 s = dc_parse_register(p);
5094 if (s) return s;
5095 }
5096
5097 if (store) {
5098 bc_parse_push(p, BC_INST_SWAP);
5099 bc_parse_push(p, BC_INST_ASSIGN);
5100 bc_parse_push(p, BC_INST_POP);
5101 }
5102
5103 return bc_lex_next(&p->l);
5104}
5105
5106static BcStatus dc_parse_cond(BcParse *p, uint8_t inst)
5107{
5108 BcStatus s;
5109
5110 bc_parse_push(p, inst);
5111 bc_parse_push(p, BC_INST_EXEC_COND);
5112
5113 s = dc_parse_register(p);
5114 if (s) return s;
5115
5116 s = bc_lex_next(&p->l);
5117 if (s) return s;
5118
5119 if (p->l.t.t == BC_LEX_ELSE) {
5120 s = dc_parse_register(p);
5121 if (s) return s;
5122 s = bc_lex_next(&p->l);
5123 }
5124 else
5125 bc_parse_push(p, BC_PARSE_STREND);
5126
5127 return s;
5128}
5129
5130static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
5131{
5132 BcStatus s = BC_STATUS_SUCCESS;
5133 BcInst prev;
5134 uint8_t inst;
5135 bool assign, get_token = false;
5136
5137 switch (t) {
5138
5139 case BC_LEX_OP_REL_EQ:
5140 case BC_LEX_OP_REL_LE:
5141 case BC_LEX_OP_REL_GE:
5142 case BC_LEX_OP_REL_NE:
5143 case BC_LEX_OP_REL_LT:
5144 case BC_LEX_OP_REL_GT:
5145 {
5146 s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
5147 break;
5148 }
5149
5150 case BC_LEX_SCOLON:
5151 case BC_LEX_COLON:
5152 {
5153 s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
5154 break;
5155 }
5156
5157 case BC_LEX_STR:
5158 {
5159 s = dc_parse_string(p);
5160 break;
5161 }
5162
5163 case BC_LEX_NEG:
5164 case BC_LEX_NUMBER:
5165 {
5166 if (t == BC_LEX_NEG) {
5167 s = bc_lex_next(&p->l);
5168 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005169 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005170 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005171 }
5172
5173 bc_parse_number(p, &prev, &p->nbraces);
5174
5175 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5176 get_token = true;
5177
5178 break;
5179 }
5180
5181 case BC_LEX_KEY_READ:
5182 {
5183 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005184 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005185 else
5186 bc_parse_push(p, BC_INST_READ);
5187 get_token = true;
5188 break;
5189 }
5190
5191 case BC_LEX_OP_ASSIGN:
5192 case BC_LEX_STORE_PUSH:
5193 {
5194 assign = t == BC_LEX_OP_ASSIGN;
5195 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
5196 s = dc_parse_mem(p, inst, true, assign);
5197 break;
5198 }
5199
5200 case BC_LEX_LOAD:
5201 case BC_LEX_LOAD_POP:
5202 {
5203 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
5204 s = dc_parse_mem(p, inst, true, false);
5205 break;
5206 }
5207
5208 case BC_LEX_STORE_IBASE:
5209 case BC_LEX_STORE_SCALE:
5210 case BC_LEX_STORE_OBASE:
5211 {
5212 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
5213 s = dc_parse_mem(p, inst, false, true);
5214 break;
5215 }
5216
5217 default:
5218 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005219 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005220 get_token = true;
5221 break;
5222 }
5223 }
5224
5225 if (!s && get_token) s = bc_lex_next(&p->l);
5226
5227 return s;
5228}
5229
5230static BcStatus dc_parse_expr(BcParse *p, uint8_t flags)
5231{
5232 BcStatus s = BC_STATUS_SUCCESS;
5233 BcInst inst;
5234 BcLexType t;
5235
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005236 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005237
5238 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
5239
5240 inst = dc_parse_insts[t];
5241
5242 if (inst != BC_INST_INVALID) {
5243 bc_parse_push(p, inst);
5244 s = bc_lex_next(&p->l);
5245 }
5246 else
5247 s = dc_parse_token(p, t, flags);
5248 }
5249
5250 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5251 bc_parse_push(p, BC_INST_POP_EXEC);
5252
5253 return s;
5254}
5255
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01005256static FAST_FUNC BcStatus dc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005257{
5258 BcStatus s;
5259
5260 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005261 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005262 else
5263 s = dc_parse_expr(p, 0);
5264
Denys Vlasenkod38af482018-12-04 19:11:02 +01005265 if (s || G_interrupt) {
5266 bc_parse_reset(p);
5267 s = BC_STATUS_FAILURE;
5268 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005269
5270 return s;
5271}
5272
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005273static void dc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005274{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005275 bc_parse_create(p, func, dc_parse_parse, dc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005276}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005277
Gavin Howard01055ba2018-11-03 11:00:21 -06005278#endif // ENABLE_DC
5279
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005280static void common_parse_init(BcParse *p, size_t func)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005281{
5282 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005283 IF_BC(bc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005284 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005285 IF_DC(dc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005286 }
5287}
5288
5289static BcStatus common_parse_expr(BcParse *p, uint8_t flags)
5290{
5291 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005292 IF_BC(return bc_parse_expression(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005293 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005294 IF_DC(return dc_parse_expr(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005295 }
5296}
5297
Denys Vlasenkodf515392018-12-02 19:27:48 +01005298static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005299{
Gavin Howard01055ba2018-11-03 11:00:21 -06005300 BcId e, *ptr;
5301 BcVec *v, *map;
5302 size_t i;
5303 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005304 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005305
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005306 v = var ? &G.prog.vars : &G.prog.arrs;
5307 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005308
5309 e.name = id;
5310 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005311 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005312
5313 if (new) {
5314 bc_array_init(&data.v, var);
5315 bc_vec_push(v, &data.v);
5316 }
5317
5318 ptr = bc_vec_item(map, i);
5319 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005320 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005321}
5322
Denys Vlasenko29301232018-12-11 15:29:32 +01005323static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005324{
Gavin Howard01055ba2018-11-03 11:00:21 -06005325 switch (r->t) {
5326
5327 case BC_RESULT_STR:
5328 case BC_RESULT_TEMP:
5329 case BC_RESULT_IBASE:
5330 case BC_RESULT_SCALE:
5331 case BC_RESULT_OBASE:
5332 {
5333 *num = &r->d.n;
5334 break;
5335 }
5336
5337 case BC_RESULT_CONSTANT:
5338 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005339 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005340 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005341 size_t base_t, len = strlen(*str);
5342 BcNum *base;
5343
5344 bc_num_init(&r->d.n, len);
5345
5346 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005347 base = hex ? &G.prog.hexb : &G.prog.ib;
5348 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005349 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005350
5351 if (s) {
5352 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005353 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005354 }
5355
5356 *num = &r->d.n;
5357 r->t = BC_RESULT_TEMP;
5358
5359 break;
5360 }
5361
5362 case BC_RESULT_VAR:
5363 case BC_RESULT_ARRAY:
5364 case BC_RESULT_ARRAY_ELEM:
5365 {
5366 BcVec *v;
5367
Denys Vlasenkodf515392018-12-02 19:27:48 +01005368 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005369
5370 if (r->t == BC_RESULT_ARRAY_ELEM) {
5371 v = bc_vec_top(v);
5372 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5373 *num = bc_vec_item(v, r->d.id.idx);
5374 }
5375 else
5376 *num = bc_vec_top(v);
5377
5378 break;
5379 }
5380
5381 case BC_RESULT_LAST:
5382 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005383 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005384 break;
5385 }
5386
5387 case BC_RESULT_ONE:
5388 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005389 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005390 break;
5391 }
5392 }
5393
Denys Vlasenko29301232018-12-11 15:29:32 +01005394 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005395}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005396#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005397# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005398#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005399
Denys Vlasenko29301232018-12-11 15:29:32 +01005400static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005401 BcResult **r, BcNum **rn, bool assign)
5402{
5403 BcStatus s;
5404 bool hex;
5405 BcResultType lt, rt;
5406
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005407 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005408 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005409
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005410 *r = bc_vec_item_rev(&G.prog.results, 0);
5411 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005412
5413 lt = (*l)->t;
5414 rt = (*r)->t;
5415 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5416
Denys Vlasenko29301232018-12-11 15:29:32 +01005417 s = zbc_program_num(*l, ln, false);
5418 if (s) RETURN_STATUS(s);
5419 s = zbc_program_num(*r, rn, hex);
5420 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005421
5422 // We run this again under these conditions in case any vector has been
5423 // reallocated out from under the BcNums or arrays we had.
5424 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005425 s = zbc_program_num(*l, ln, false);
5426 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005427 }
5428
5429 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005430 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005431 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005432 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005433
Denys Vlasenko29301232018-12-11 15:29:32 +01005434 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005435}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005436#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005437# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005438#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005439
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005440static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005441{
5442 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005443 bc_vec_pop(&G.prog.results);
5444 bc_vec_pop(&G.prog.results);
5445 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005446}
5447
Denys Vlasenko29301232018-12-11 15:29:32 +01005448static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005449{
5450 BcStatus s;
5451
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005452 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005453 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005454 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005455
Denys Vlasenko29301232018-12-11 15:29:32 +01005456 s = zbc_program_num(*r, n, false);
5457 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005458
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005459 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005460 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005461
Denys Vlasenko29301232018-12-11 15:29:32 +01005462 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005463}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005464#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005465# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005466#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005467
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005468static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005469{
5470 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005471 bc_vec_pop(&G.prog.results);
5472 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005473}
5474
Denys Vlasenko259137d2018-12-11 19:42:05 +01005475static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005476{
5477 BcStatus s;
5478 BcResult *opd1, *opd2, res;
5479 BcNum *n1, *n2 = NULL;
5480
Denys Vlasenko29301232018-12-11 15:29:32 +01005481 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005482 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005483 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005484
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005485 s = BC_STATUS_SUCCESS;
5486#if !ERRORS_ARE_FATAL
5487 s =
5488#endif
5489 zbc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005490 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005491 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005492
Denys Vlasenko259137d2018-12-11 19:42:05 +01005493 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005494
5495err:
5496 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005497 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005498}
Denys Vlasenko259137d2018-12-11 19:42:05 +01005499#if ERRORS_ARE_FATAL
5500# define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS)
5501#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005502
Denys Vlasenko785e4b32018-12-02 17:18:52 +01005503static BcStatus bc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005504{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005505 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005506 BcStatus s;
5507 BcParse parse;
5508 BcVec buf;
5509 BcInstPtr ip;
5510 size_t i;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005511 BcFunc *f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005512
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005513 for (i = 0; i < G.prog.stack.len; ++i) {
5514 BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005515 if (ip_ptr->func == BC_PROG_READ)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005516 return bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005517 }
5518
Denys Vlasenko7d628012018-12-04 21:46:47 +01005519 bc_vec_pop_all(&f->code);
5520 bc_char_vec_init(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005521
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005522 sv_file = G.prog.file;
5523 G.prog.file = NULL;
5524
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01005525 s = bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005526 if (s) goto io_err;
5527
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005528 common_parse_init(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005529 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005530
5531 s = bc_parse_text(&parse, buf.v);
5532 if (s) goto exec_err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005533 s = common_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005534 if (s) goto exec_err;
5535
5536 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005537 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005538 goto exec_err;
5539 }
5540
5541 ip.func = BC_PROG_READ;
5542 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005543 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005544
5545 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005546 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005547
5548 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005549 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005550
5551exec_err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005552 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005553 bc_parse_free(&parse);
5554io_err:
5555 bc_vec_free(&buf);
5556 return s;
5557}
5558
5559static size_t bc_program_index(char *code, size_t *bgn)
5560{
5561 char amt = code[(*bgn)++], i = 0;
5562 size_t res = 0;
5563
5564 for (; i < amt; ++i, ++(*bgn))
5565 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5566
5567 return res;
5568}
5569
5570static char *bc_program_name(char *code, size_t *bgn)
5571{
5572 size_t i;
5573 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5574
5575 s = xmalloc(ptr - str + 1);
5576 c = code[(*bgn)++];
5577
5578 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5579 s[i] = c;
5580
5581 s[i] = '\0';
5582
5583 return s;
5584}
5585
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005586static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005587{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005588#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005589 // Huh? Example when this happens?
5590 if (!str[0]) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005591 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005592 return;
5593 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005594#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005595 while (*str) {
5596 int c = *str++;
5597 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005598 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005599 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005600 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005601 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005602 case 'a':
5603 bb_putchar('\a');
5604 break;
5605 case 'b':
5606 bb_putchar('\b');
5607 break;
5608 case '\\':
5609 case 'e':
5610 bb_putchar('\\');
5611 break;
5612 case 'f':
5613 bb_putchar('\f');
5614 break;
5615 case 'n':
5616 bb_putchar('\n');
5617 G.prog.nchars = SIZE_MAX;
5618 break;
5619 case 'r':
5620 bb_putchar('\r');
5621 break;
5622 case 'q':
5623 bb_putchar('"');
5624 break;
5625 case 't':
5626 bb_putchar('\t');
5627 break;
5628 default:
5629 // Just print the backslash and following character.
5630 bb_putchar('\\');
5631 ++G.prog.nchars;
5632 bb_putchar(c);
5633 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005634 }
5635 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005636 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005637 }
5638}
5639
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005640static void bc_num_printNewline(void)
5641{
5642 if (G.prog.nchars == G.prog.len - 1) {
5643 bb_putchar('\\');
5644 bb_putchar('\n');
5645 G.prog.nchars = 0;
5646 }
5647}
5648
5649#if ENABLE_DC
5650static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
5651{
5652 (void) radix;
5653 bb_putchar((char) num);
5654 G.prog.nchars += width;
5655}
5656#endif
5657
5658static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5659{
5660 size_t exp, pow;
5661
5662 bc_num_printNewline();
5663 bb_putchar(radix ? '.' : ' ');
5664 ++G.prog.nchars;
5665
5666 bc_num_printNewline();
5667 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5668 continue;
5669
5670 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5671 size_t dig;
5672 bc_num_printNewline();
5673 dig = num / pow;
5674 num -= dig * pow;
5675 bb_putchar(((char) dig) + '0');
5676 }
5677}
5678
5679static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5680{
5681 if (radix) {
5682 bc_num_printNewline();
5683 bb_putchar('.');
5684 G.prog.nchars += 1;
5685 }
5686
5687 bc_num_printNewline();
5688 bb_putchar(bb_hexdigits_upcase[num]);
5689 G.prog.nchars += width;
5690}
5691
5692static void bc_num_printDecimal(BcNum *n)
5693{
5694 size_t i, rdx = n->rdx - 1;
5695
5696 if (n->neg) bb_putchar('-');
5697 G.prog.nchars += n->neg;
5698
5699 for (i = n->len - 1; i < n->len; --i)
5700 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5701}
5702
5703static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
5704{
5705 BcStatus s;
5706 BcVec stack;
5707 BcNum intp, fracp, digit, frac_len;
5708 unsigned long dig, *ptr;
5709 size_t i;
5710 bool radix;
5711
5712 if (n->len == 0) {
5713 print(0, width, false);
5714 RETURN_STATUS(BC_STATUS_SUCCESS);
5715 }
5716
5717 bc_vec_init(&stack, sizeof(long), NULL);
5718 bc_num_init(&intp, n->len);
5719 bc_num_init(&fracp, n->rdx);
5720 bc_num_init(&digit, width);
5721 bc_num_init(&frac_len, BC_NUM_INT(n));
5722 bc_num_copy(&intp, n);
5723 bc_num_one(&frac_len);
5724
5725 bc_num_truncate(&intp, intp.rdx);
5726 s = zbc_num_sub(n, &intp, &fracp, 0);
5727 if (s) goto err;
5728
5729 while (intp.len != 0) {
5730 s = zbc_num_divmod(&intp, base, &intp, &digit, 0);
5731 if (s) goto err;
5732 s = zbc_num_ulong(&digit, &dig);
5733 if (s) goto err;
5734 bc_vec_push(&stack, &dig);
5735 }
5736
5737 for (i = 0; i < stack.len; ++i) {
5738 ptr = bc_vec_item_rev(&stack, i);
5739 print(*ptr, width, false);
5740 }
5741
5742 if (!n->rdx) goto err;
5743
5744 for (radix = true; frac_len.len <= n->rdx; radix = false) {
5745 s = zbc_num_mul(&fracp, base, &fracp, n->rdx);
5746 if (s) goto err;
5747 s = zbc_num_ulong(&fracp, &dig);
5748 if (s) goto err;
5749 bc_num_ulong2num(&intp, dig);
5750 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5751 if (s) goto err;
5752 print(dig, width, radix);
5753 s = zbc_num_mul(&frac_len, base, &frac_len, 0);
5754 if (s) goto err;
5755 }
5756
5757err:
5758 bc_num_free(&frac_len);
5759 bc_num_free(&digit);
5760 bc_num_free(&fracp);
5761 bc_num_free(&intp);
5762 bc_vec_free(&stack);
5763 RETURN_STATUS(s);
5764}
5765#if ERRORS_ARE_FATAL
5766# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
5767#endif
5768
5769static BC_STATUS zbc_num_printBase(BcNum *n)
5770{
5771 BcStatus s;
5772 size_t width, i;
5773 BcNumDigitOp print;
5774 bool neg = n->neg;
5775
5776 if (neg) {
5777 bb_putchar('-');
5778 G.prog.nchars++;
5779 }
5780
5781 n->neg = false;
5782
5783 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5784 width = 1;
5785 print = bc_num_printHex;
5786 }
5787 else {
5788 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
5789 continue;
5790 print = bc_num_printDigits;
5791 }
5792
5793 s = zbc_num_printNum(n, &G.prog.ob, width, print);
5794 n->neg = neg;
5795
5796 RETURN_STATUS(s);
5797}
5798#if ERRORS_ARE_FATAL
5799# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
5800#endif
5801
5802#if ENABLE_DC
5803static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
5804{
5805 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
5806}
5807#if ERRORS_ARE_FATAL
5808# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
5809#endif
5810#endif
5811
5812static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5813{
5814 BcStatus s = BC_STATUS_SUCCESS;
5815
5816 bc_num_printNewline();
5817
5818 if (n->len == 0) {
5819 bb_putchar('0');
5820 ++G.prog.nchars;
5821 }
5822 else if (G.prog.ob_t == 10)
5823 bc_num_printDecimal(n);
5824 else
5825 s = zbc_num_printBase(n);
5826
5827 if (newline) {
5828 bb_putchar('\n');
5829 G.prog.nchars = 0;
5830 }
5831
5832 RETURN_STATUS(s);
5833}
5834#if ERRORS_ARE_FATAL
5835# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
5836#endif
5837
Denys Vlasenko29301232018-12-11 15:29:32 +01005838static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005839{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005840 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005841 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005842 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005843 bool pop = inst != BC_INST_PRINT;
5844
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005845 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005846 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005847
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005848 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005849 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005850 s = zbc_program_num(r, &num, false);
5851 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005852
5853 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005854 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005855 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005856 }
5857 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005858 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005859
5860 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005861 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005862
5863 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005864 for (;;) {
5865 char c = *str++;
5866 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005867 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005868 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005869 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005870 }
5871 }
5872 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005873 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005874 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005875 }
5876 }
5877
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005878 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005879
Denys Vlasenko29301232018-12-11 15:29:32 +01005880 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005881}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005882#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005883# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005884#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005885
Denys Vlasenko29301232018-12-11 15:29:32 +01005886static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005887{
5888 BcStatus s;
5889 BcResult res, *ptr;
5890 BcNum *num = NULL;
5891
Denys Vlasenko29301232018-12-11 15:29:32 +01005892 s = zbc_program_prep(&ptr, &num);
5893 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005894
5895 bc_num_init(&res.d.n, num->len);
5896 bc_num_copy(&res.d.n, num);
5897 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5898
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005899 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005900
Denys Vlasenko29301232018-12-11 15:29:32 +01005901 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005902}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005903#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005904# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005905#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005906
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005907static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005908{
5909 BcStatus s;
5910 BcResult *opd1, *opd2, res;
5911 BcNum *n1, *n2;
5912 bool cond = 0;
5913 ssize_t cmp;
5914
Denys Vlasenko29301232018-12-11 15:29:32 +01005915 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005916 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005917
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005918 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005919
5920 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005921 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005922 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005923 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005924 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005925 cmp = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005926 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005927 case BC_INST_REL_EQ:
5928 cond = cmp == 0;
5929 break;
5930 case BC_INST_REL_LE:
5931 cond = cmp <= 0;
5932 break;
5933 case BC_INST_REL_GE:
5934 cond = cmp >= 0;
5935 break;
5936 case BC_INST_REL_NE:
5937 cond = cmp != 0;
5938 break;
5939 case BC_INST_REL_LT:
5940 cond = cmp < 0;
5941 break;
5942 case BC_INST_REL_GT:
5943 cond = cmp > 0;
5944 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005945 }
5946 }
5947
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005948 if (cond) bc_num_one(&res.d.n);
5949 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005950
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005951 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005952
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005953 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005954}
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005955#if ERRORS_ARE_FATAL
5956# define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS)
5957#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005958
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005959#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005960static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005961 bool push)
5962{
5963 BcNum n2;
5964 BcResult res;
5965
5966 memset(&n2, 0, sizeof(BcNum));
5967 n2.rdx = res.d.id.idx = r->d.id.idx;
5968 res.t = BC_RESULT_STR;
5969
5970 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005971 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005972 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005973 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005974 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005975 }
5976
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005977 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005978
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005979 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005980 bc_vec_push(v, &n2);
5981
Denys Vlasenko29301232018-12-11 15:29:32 +01005982 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005983}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005984#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005985# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005986#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005987#endif // ENABLE_DC
5988
Denys Vlasenko29301232018-12-11 15:29:32 +01005989static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005990{
5991 BcStatus s;
5992 BcResult *ptr, r;
5993 BcVec *v;
5994 BcNum *n;
5995
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005996 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005997 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005998
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005999 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006000 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01006001 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01006002 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06006003
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006004#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006005 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01006006 RETURN_STATUS(bc_error_variable_is_wrong_type());
6007 if (ptr->t == BC_RESULT_STR)
6008 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006009#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006010
Denys Vlasenko29301232018-12-11 15:29:32 +01006011 s = zbc_program_num(ptr, &n, false);
6012 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006013
6014 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01006015 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06006016
6017 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006018 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006019 bc_num_copy(&r.d.n, n);
6020 }
6021 else {
6022 bc_array_init(&r.d.v, true);
6023 bc_array_copy(&r.d.v, (BcVec *) n);
6024 }
6025
6026 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006027 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006028
Denys Vlasenko29301232018-12-11 15:29:32 +01006029 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006030}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006031#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006032# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006033#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006034
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006035static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006036{
6037 BcStatus s;
6038 BcResult *left, *right, res;
6039 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006040 bool assign = inst == BC_INST_ASSIGN, ib, sc;
6041
Denys Vlasenko29301232018-12-11 15:29:32 +01006042 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006043 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006044
6045 ib = left->t == BC_RESULT_IBASE;
6046 sc = left->t == BC_RESULT_SCALE;
6047
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006048#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006049
6050 if (right->t == BC_RESULT_STR) {
6051
6052 BcVec *v;
6053
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006054 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006055 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01006056 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006057
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006058 RETURN_STATUS(zbc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06006059 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006060#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006061
6062 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01006063 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006064 " left side must be scale,"
6065 " ibase, obase, last, var,"
6066 " or array element"
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006067 ));
Gavin Howard01055ba2018-11-03 11:00:21 -06006068
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006069#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006070 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006071 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06006072
6073 if (assign)
6074 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006075 else {
6076 s = BC_STATUS_SUCCESS;
6077#if !ERRORS_ARE_FATAL
6078 s =
6079#endif
6080 zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
6081 }
6082 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006083#else
Gavin Howard01055ba2018-11-03 11:00:21 -06006084 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006085#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006086
6087 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006088 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006089 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
6090 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
6091 NULL, //can't happen //BC_RESULT_LAST
6092 NULL, //can't happen //BC_RESULT_CONSTANT
6093 NULL, //can't happen //BC_RESULT_ONE
6094 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006095 };
Gavin Howard01055ba2018-11-03 11:00:21 -06006096 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01006097 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06006098
Denys Vlasenko29301232018-12-11 15:29:32 +01006099 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006100 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006101 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06006102 if (sc) {
6103 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006104 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006105 }
6106 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006107 if (val < BC_NUM_MIN_BASE)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006108 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06006109 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006110 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006111 }
6112
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006113 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006114 RETURN_STATUS(bc_error(msg[s]));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006115 if (!sc)
6116 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006117
6118 *ptr = (size_t) val;
6119 s = BC_STATUS_SUCCESS;
6120 }
6121
6122 bc_num_init(&res.d.n, l->len);
6123 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006124 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006125
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006126 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006127}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006128#if ERRORS_ARE_FATAL
6129# define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS)
6130#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006131
Denys Vlasenko416ce762018-12-02 20:57:17 +01006132#if !ENABLE_DC
6133#define bc_program_pushVar(code, bgn, pop, copy) \
6134 bc_program_pushVar(code, bgn)
6135// for bc, 'pop' and 'copy' are always false
6136#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006137static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006138 bool pop, bool copy)
6139{
Gavin Howard01055ba2018-11-03 11:00:21 -06006140 BcResult r;
6141 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006142
6143 r.t = BC_RESULT_VAR;
6144 r.d.id.name = name;
6145
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006146#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006147 {
6148 BcVec *v = bc_program_search(name, true);
6149 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006150
Denys Vlasenko416ce762018-12-02 20:57:17 +01006151 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006152
Denys Vlasenko416ce762018-12-02 20:57:17 +01006153 if (!BC_PROG_STACK(v, 2 - copy)) {
6154 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006155 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006156 }
6157
Gavin Howard01055ba2018-11-03 11:00:21 -06006158 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006159 name = NULL;
6160
6161 if (!BC_PROG_STR(num)) {
6162
6163 r.t = BC_RESULT_TEMP;
6164
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006165 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006166 bc_num_copy(&r.d.n, num);
6167 }
6168 else {
6169 r.t = BC_RESULT_STR;
6170 r.d.id.idx = num->rdx;
6171 }
6172
6173 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006174 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006175 }
6176#endif // ENABLE_DC
6177
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006178 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006179
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006180 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006181}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006182#if ERRORS_ARE_FATAL
6183# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6184#else
6185# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6186#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006187
Denys Vlasenko29301232018-12-11 15:29:32 +01006188static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006189 char inst)
6190{
6191 BcStatus s = BC_STATUS_SUCCESS;
6192 BcResult r;
6193 BcNum *num;
6194
6195 r.d.id.name = bc_program_name(code, bgn);
6196
6197 if (inst == BC_INST_ARRAY) {
6198 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006199 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006200 }
6201 else {
6202
6203 BcResult *operand;
6204 unsigned long temp;
6205
Denys Vlasenko29301232018-12-11 15:29:32 +01006206 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006207 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006208 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006209 if (s) goto err;
6210
6211 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006212 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006213 goto err;
6214 }
6215
6216 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006217 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006218 }
6219
6220err:
6221 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006222 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006223}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006224#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006225# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006226#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006227
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006228#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006229static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006230{
6231 BcStatus s;
6232 BcResult *ptr, res, copy;
6233 BcNum *num = NULL;
6234 char inst2 = inst;
6235
Denys Vlasenko29301232018-12-11 15:29:32 +01006236 s = zbc_program_prep(&ptr, &num);
6237 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006238
6239 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6240 copy.t = BC_RESULT_TEMP;
6241 bc_num_init(&copy.d.n, num->len);
6242 bc_num_copy(&copy.d.n, num);
6243 }
6244
6245 res.t = BC_RESULT_ONE;
6246 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6247 BC_INST_ASSIGN_PLUS :
6248 BC_INST_ASSIGN_MINUS;
6249
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006250 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006251 s = zbc_program_assign(inst);
6252 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006253
6254 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006255 bc_vec_pop(&G.prog.results);
6256 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006257 }
6258
Denys Vlasenko29301232018-12-11 15:29:32 +01006259 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006260}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006261#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006262# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006263#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006264
Denys Vlasenko29301232018-12-11 15:29:32 +01006265static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006266{
Gavin Howard01055ba2018-11-03 11:00:21 -06006267 BcInstPtr ip;
6268 size_t i, nparams = bc_program_index(code, idx);
6269 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006270 BcId *a;
6271 BcResultData param;
6272 BcResult *arg;
6273
6274 ip.idx = 0;
6275 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006276 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006277
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006278 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006279 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006280 }
6281 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006282 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006283 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006284 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006285
6286 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006287 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006288
6289 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006290 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006291
6292 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006293 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006294
Denys Vlasenko29301232018-12-11 15:29:32 +01006295 s = zbc_program_copyToVar(a->name, a->idx);
6296 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006297 }
6298
6299 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006300 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006301
6302 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006303 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006304
6305 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006306 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006307 bc_vec_push(v, &param.n);
6308 }
6309 else {
6310 bc_array_init(&param.v, true);
6311 bc_vec_push(v, &param.v);
6312 }
6313 }
6314
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006315 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006316
Denys Vlasenko29301232018-12-11 15:29:32 +01006317 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006318}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006319#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006320# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006321#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006322
Denys Vlasenko29301232018-12-11 15:29:32 +01006323static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006324{
Gavin Howard01055ba2018-11-03 11:00:21 -06006325 BcResult res;
6326 BcFunc *f;
6327 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006328 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006329
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006330 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006331 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006332
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006333 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006334 res.t = BC_RESULT_TEMP;
6335
6336 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006337 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006338 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006339 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006340
Denys Vlasenko29301232018-12-11 15:29:32 +01006341 s = zbc_program_num(operand, &num, false);
6342 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006343 bc_num_init(&res.d.n, num->len);
6344 bc_num_copy(&res.d.n, num);
6345 }
6346 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006347 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006348 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006349 }
6350
6351 // We need to pop arguments as well, so this takes that into account.
6352 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006353 BcVec *v;
6354 BcId *a = bc_vec_item(&f->autos, i);
6355
Denys Vlasenkodf515392018-12-02 19:27:48 +01006356 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006357 bc_vec_pop(v);
6358 }
6359
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006360 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6361 bc_vec_push(&G.prog.results, &res);
6362 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006363
Denys Vlasenko29301232018-12-11 15:29:32 +01006364 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006365}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006366#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006367# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006368#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006369#endif // ENABLE_BC
6370
6371static unsigned long bc_program_scale(BcNum *n)
6372{
6373 return (unsigned long) n->rdx;
6374}
6375
6376static unsigned long bc_program_len(BcNum *n)
6377{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006378 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006379
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006380 if (n->rdx != len) return len;
6381 for (;;) {
6382 if (len == 0) break;
6383 len--;
6384 if (n->num[len] != 0) break;
6385 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006386 return len;
6387}
6388
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006389static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006390{
6391 BcStatus s;
6392 BcResult *opnd;
6393 BcNum *num = NULL;
6394 BcResult res;
6395 bool len = inst == BC_INST_LENGTH;
6396
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006397 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006398 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006399 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006400
Denys Vlasenko29301232018-12-11 15:29:32 +01006401 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006402 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006403
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006404#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006405 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006406 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006407#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006408
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006409 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006410
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006411 if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006412#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006413 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006414 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006415 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006416#endif
6417#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006418 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006419 char **str;
6420 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6421
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006422 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006423 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006424 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006425#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006426 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006427 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006428 }
6429
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006430 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006431
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006432 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006433}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006434#if ERRORS_ARE_FATAL
6435# define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
6436#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006437
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006438#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006439static BC_STATUS zbc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006440{
6441 BcStatus s;
6442 BcResult *opd1, *opd2, res, res2;
6443 BcNum *n1, *n2 = NULL;
6444
Denys Vlasenko29301232018-12-11 15:29:32 +01006445 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006446 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006447
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006448 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006449 bc_num_init(&res2.d.n, n2->len);
6450
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006451 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006452 if (s) goto err;
6453
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006454 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006455 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006456 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006457
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006458 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006459
6460err:
6461 bc_num_free(&res2.d.n);
6462 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006463 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006464}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006465#if ERRORS_ARE_FATAL
6466# define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
6467#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006468
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006469static BC_STATUS zbc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006470{
6471 BcStatus s;
6472 BcResult *r1, *r2, *r3, res;
6473 BcNum *n1, *n2, *n3;
6474
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006475 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006476 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006477 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006478 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006479
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006480 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006481 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006482 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006483 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006484 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006485
6486 // Make sure that the values have their pointers updated, if necessary.
6487 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6488
6489 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006490 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006491 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006492 }
6493
6494 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006495 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006496 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006497 }
6498 }
6499
6500 bc_num_init(&res.d.n, n3->len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006501 s = zbc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006502 if (s) goto err;
6503
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006504 bc_vec_pop(&G.prog.results);
6505 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006506
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006507 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006508
6509err:
6510 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006511 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006512}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006513#if ERRORS_ARE_FATAL
6514# define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
6515#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006516
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006517static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006518{
Gavin Howard01055ba2018-11-03 11:00:21 -06006519 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006520 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006521
6522 res.t = BC_RESULT_TEMP;
6523
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006524 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006525 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006526 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006527}
6528
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006529static BcStatus bc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006530{
6531 BcStatus s;
6532 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006533 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006534 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006535 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006536 unsigned long val;
6537
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006538 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006539 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006540 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006541
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006542 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006543 s = zbc_program_num(r, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006544 if (s) return s;
6545
6546 if (BC_PROG_NUM(r, num)) {
6547
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006548 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006549 bc_num_copy(&n, num);
6550 bc_num_truncate(&n, n.rdx);
6551
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006552 s = zbc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006553 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006554 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006555 if (s) goto num_err;
6556
6557 c = (char) val;
6558
6559 bc_num_free(&n);
6560 }
6561 else {
6562 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006563 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006564 c = str2[0];
6565 }
6566
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006567 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006568 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006569 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006570
6571 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006572 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006573
6574 if (idx != len + BC_PROG_REQ_FUNCS) {
6575
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006576 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006577 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006578 len = idx;
6579 break;
6580 }
6581 }
6582
6583 free(str);
6584 }
6585 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006586 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006587
6588 res.t = BC_RESULT_STR;
6589 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006590 bc_vec_pop(&G.prog.results);
6591 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006592
6593 return BC_STATUS_SUCCESS;
6594
6595num_err:
6596 bc_num_free(&n);
6597 return s;
6598}
6599
Denys Vlasenko29301232018-12-11 15:29:32 +01006600static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006601{
6602 BcStatus s;
6603 BcResult *r;
6604 BcNum *n = NULL;
6605 size_t idx;
6606 char *str;
6607
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006608 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006609 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006610 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006611
Denys Vlasenko29301232018-12-11 15:29:32 +01006612 s = zbc_program_num(r, &n, false);
6613 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006614
6615 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006616 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006617 else {
6618 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006619 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006620 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006621 }
6622
Denys Vlasenko29301232018-12-11 15:29:32 +01006623 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006624}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006625#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006626# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006627#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006628
Denys Vlasenko29301232018-12-11 15:29:32 +01006629static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006630{
6631 BcStatus s;
6632 BcResult *opnd;
6633 BcNum *num = NULL;
6634 unsigned long val;
6635
Denys Vlasenko29301232018-12-11 15:29:32 +01006636 s = zbc_program_prep(&opnd, &num);
6637 if (s) RETURN_STATUS(s);
6638 s = zbc_num_ulong(num, &val);
6639 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006640
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006641 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006642
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006643 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006644 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006645 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006646 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006647 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006648
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006649 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006650
Denys Vlasenko29301232018-12-11 15:29:32 +01006651 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006652}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006653#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006654# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006655#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006656
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006657static BcStatus bc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006658 bool cond)
6659{
6660 BcStatus s = BC_STATUS_SUCCESS;
6661 BcResult *r;
6662 char **str;
6663 BcFunc *f;
6664 BcParse prs;
6665 BcInstPtr ip;
6666 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006667
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006668 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006669 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006670
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006671 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006672
6673 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006674 BcNum *n = n; // for compiler
6675 bool exec;
6676 char *name;
6677 char *then_name = bc_program_name(code, bgn);
6678 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006679
6680 if (code[*bgn] == BC_PARSE_STREND)
6681 (*bgn) += 1;
6682 else
6683 else_name = bc_program_name(code, bgn);
6684
6685 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006686 name = then_name;
6687 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006688 exec = true;
6689 name = else_name;
6690 }
6691
6692 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006693 BcVec *v;
6694 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006695 n = bc_vec_top(v);
6696 }
6697
6698 free(then_name);
6699 free(else_name);
6700
6701 if (!exec) goto exit;
6702 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006703 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006704 goto exit;
6705 }
6706
6707 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006708 } else {
6709 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006710 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006711 } else if (r->t == BC_RESULT_VAR) {
6712 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006713 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006714 if (s || !BC_PROG_STR(n)) goto exit;
6715 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006716 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006717 goto exit;
6718 }
6719
6720 fidx = sidx + BC_PROG_REQ_FUNCS;
6721
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006722 str = bc_program_str(sidx);
6723 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006724
6725 if (f->code.len == 0) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006726 common_parse_init(&prs, fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006727 s = bc_parse_text(&prs, *str);
6728 if (s) goto err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01006729 s = common_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006730 if (s) goto err;
6731
6732 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006733 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006734 goto err;
6735 }
6736
6737 bc_parse_free(&prs);
6738 }
6739
6740 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006741 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006742 ip.func = fidx;
6743
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006744 bc_vec_pop(&G.prog.results);
6745 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006746
6747 return BC_STATUS_SUCCESS;
6748
6749err:
6750 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006751 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006752 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006753exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006754 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006755 return s;
6756}
6757#endif // ENABLE_DC
6758
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006759static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006760{
Gavin Howard01055ba2018-11-03 11:00:21 -06006761 BcResult res;
6762 unsigned long val;
6763
6764 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6765 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006766 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006767 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006768 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006769 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006770 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006771
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006772 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006773 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006774 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006775}
6776
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006777static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006778{
Gavin Howard01055ba2018-11-03 11:00:21 -06006779 BcId entry, *entry_ptr;
6780 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006781 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006782
6783 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006784 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006785
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006786 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6787 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006788
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006789 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006790 *idx = entry_ptr->idx;
6791
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006792 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006793
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006794 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006795
6796 // We need to reset these, so the function can be repopulated.
6797 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006798 bc_vec_pop_all(&func->autos);
6799 bc_vec_pop_all(&func->code);
6800 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006801 }
6802 else {
6803 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006804 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006805 }
6806}
6807
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006808static BcStatus bc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006809{
Gavin Howard01055ba2018-11-03 11:00:21 -06006810 BcResult r, *ptr;
6811 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006812 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006813 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006814 char *code = func->code.v;
6815 bool cond = false;
6816
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006817 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006818 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006819 char inst = code[(ip->idx)++];
6820
6821 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006822#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006823 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006824 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006825 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006826 cond = !bc_num_cmp(num, &G.prog.zero);
6827 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006828 // Fallthrough.
6829 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006830 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006831 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006832 addr = bc_vec_item(&func->labels, idx);
6833 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6834 break;
6835 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006836 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006837 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006838 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006839 case BC_INST_INC_PRE:
6840 case BC_INST_DEC_PRE:
6841 case BC_INST_INC_POST:
6842 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006843 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006844 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006845 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006846 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006847 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006848 case BC_INST_RET:
6849 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006850 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006851 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006852 case BC_INST_BOOL_OR:
6853 case BC_INST_BOOL_AND:
6854#endif // ENABLE_BC
6855 case BC_INST_REL_EQ:
6856 case BC_INST_REL_LE:
6857 case BC_INST_REL_GE:
6858 case BC_INST_REL_NE:
6859 case BC_INST_REL_LT:
6860 case BC_INST_REL_GT:
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006861 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006862 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006863 case BC_INST_READ:
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006864 s = bc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006865 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006866 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006867 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006868 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006869 case BC_INST_ARRAY_ELEM:
6870 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006871 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006872 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006873 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006874 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006875 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006876 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006877 case BC_INST_IBASE:
6878 case BC_INST_SCALE:
6879 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006880 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006881 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006882 case BC_INST_SCALE_FUNC:
6883 case BC_INST_LENGTH:
6884 case BC_INST_SQRT:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006885 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006886 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006887 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006888 r.t = BC_RESULT_CONSTANT;
6889 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006890 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006891 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006892 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006893 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006894 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006895 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006896 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006897 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006898 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006899 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006900 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006901 case BC_INST_PRINT:
6902 case BC_INST_PRINT_POP:
6903 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006904 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006905 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006906 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006907 r.t = BC_RESULT_STR;
6908 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006909 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006910 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006911 case BC_INST_POWER:
6912 case BC_INST_MULTIPLY:
6913 case BC_INST_DIVIDE:
6914 case BC_INST_MODULUS:
6915 case BC_INST_PLUS:
6916 case BC_INST_MINUS:
Denys Vlasenko259137d2018-12-11 19:42:05 +01006917 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006918 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006919 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006920 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006921 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006922 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006923 if (!bc_num_cmp(num, &G.prog.zero))
6924 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006925 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006926 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006927 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006928 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006929 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006930 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006931#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006932 case BC_INST_ASSIGN_POWER:
6933 case BC_INST_ASSIGN_MULTIPLY:
6934 case BC_INST_ASSIGN_DIVIDE:
6935 case BC_INST_ASSIGN_MODULUS:
6936 case BC_INST_ASSIGN_PLUS:
6937 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006938#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006939 case BC_INST_ASSIGN:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006940 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006941 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006942#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006943 case BC_INST_MODEXP:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006944 s = zbc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006945 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006946 case BC_INST_DIVMOD:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006947 s = zbc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006948 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006949 case BC_INST_EXECUTE:
6950 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006951 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006952 s = bc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006953 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006954 case BC_INST_PRINT_STACK: {
6955 size_t idx;
6956 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006957 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006958 if (s) break;
6959 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006960 break;
6961 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006962 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006963 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006964 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006965 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006966 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006967 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006968 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006969 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006970 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006971 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006972 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006973 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006974 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006975 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006976 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006977 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006978 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006979 ptr = bc_vec_item_rev(&G.prog.results, 0);
6980 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006981 memcpy(&r, ptr, sizeof(BcResult));
6982 memcpy(ptr, ptr2, sizeof(BcResult));
6983 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006984 break;
6985 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006986 case BC_INST_ASCIIFY:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006987 s = bc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006988 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006989 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006990 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006991 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006992 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006993 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006994 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006995 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006996 break;
6997 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006998 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006999 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01007000 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06007001 free(name);
7002 break;
7003 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007004 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007005 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007006 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01007007 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06007008 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06007009 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01007010 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06007011 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06007012#endif // ENABLE_DC
7013 }
7014
Denys Vlasenkod38af482018-12-04 19:11:02 +01007015 if (s || G_interrupt) {
7016 bc_program_reset();
Denys Vlasenko927a7d62018-12-09 02:24:14 +01007017 return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01007018 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007019
7020 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007021 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007022 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06007023 code = func->code.v;
7024 }
7025
Denys Vlasenko927a7d62018-12-09 02:24:14 +01007026 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007027}
7028
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007029#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01007030static void bc_vm_info(void)
7031{
7032 printf("%s "BB_VER"\n"
7033 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01007034 , applet_name);
7035}
7036
7037static void bc_args(char **argv)
7038{
7039 unsigned opts;
7040 int i;
7041
7042 GETOPT_RESET();
7043#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007044 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01007045 "warn\0" No_argument "w"
7046 "version\0" No_argument "v"
7047 "standard\0" No_argument "s"
7048 "quiet\0" No_argument "q"
7049 "mathlib\0" No_argument "l"
7050 "interactive\0" No_argument "i"
7051 );
7052#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007053 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01007054#endif
7055 if (getenv("POSIXLY_CORRECT"))
7056 option_mask32 |= BC_FLAG_S;
7057
Denys Vlasenko54214c32018-12-06 09:07:06 +01007058 if (opts & BC_FLAG_V) {
7059 bc_vm_info();
7060 exit(0);
7061 }
7062
7063 for (i = optind; argv[i]; ++i)
7064 bc_vec_push(&G.files, argv + i);
7065}
7066
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007067static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007068{
Gavin Howard01055ba2018-11-03 11:00:21 -06007069 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007070 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01007071 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06007072
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007073 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06007074
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007075 G.env_args = xstrdup(env_args);
7076 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06007077
7078 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06007079
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007080 while (*(buf = skip_whitespace(buf)) != '\0') {
7081 bc_vec_push(&v, &buf);
7082 buf = skip_non_whitespace(buf);
7083 if (!*buf)
7084 break;
7085 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06007086 }
7087
Denys Vlasenko54214c32018-12-06 09:07:06 +01007088 // NULL terminate, and pass argv[] so that first arg is argv[1]
7089 if (sizeof(int) == sizeof(char*)) {
7090 bc_vec_push(&v, &const_int_0);
7091 } else {
7092 static char *const nullptr = NULL;
7093 bc_vec_push(&v, &nullptr);
7094 }
7095 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06007096
7097 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06007098}
7099#endif // ENABLE_BC
7100
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007101static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06007102{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007103 char *lenv;
7104 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06007105
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007106 lenv = getenv(var);
7107 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06007108 if (!lenv) return len;
7109
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007110 len = bb_strtou(lenv, NULL, 10) - 1;
7111 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06007112 len = BC_NUM_PRINT_WIDTH;
7113
7114 return len;
7115}
7116
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007117static BcStatus bc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06007118{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007119 BcStatus s = bc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06007120
Gavin Howard01055ba2018-11-03 11:00:21 -06007121 if (s) return s;
7122
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007123 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007124 s = G.prs.parse(&G.prs);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01007125 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007126 }
7127
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007128 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007129 s = bc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01007130 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007131 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01007132 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06007133 }
7134
7135 return s;
7136}
7137
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007138static BcStatus bc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007139{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007140 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007141 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007142 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007143 BcFunc *main_func;
7144 BcInstPtr *ip;
7145
Denys Vlasenkodf515392018-12-02 19:27:48 +01007146 data = bc_read_file(file);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007147 if (!data) return bc_error_fmt("file '%s' is not text", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007148
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007149 sv_file = G.prog.file;
7150 G.prog.file = file;
7151 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007152 s = bc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007153 if (s) goto err;
7154
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007155 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007156 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007157
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007158 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007159 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007160
7161err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007162 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007163 free(data);
7164 return s;
7165}
7166
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007167static BcStatus bc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007168{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007169 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007170 BcVec buf, buffer;
Gavin Howard01055ba2018-11-03 11:00:21 -06007171 size_t len, i, str = 0;
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007172 bool comment = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06007173
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007174 G.prog.file = NULL;
7175 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007176
Denys Vlasenko7d628012018-12-04 21:46:47 +01007177 bc_char_vec_init(&buffer);
7178 bc_char_vec_init(&buf);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01007179 bc_vec_pushZeroByte(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007180
7181 // This loop is complex because the vm tries not to send any lines that end
7182 // with a backslash to the parser. The reason for that is because the parser
7183 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7184 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007185 while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007186
7187 char *string = buf.v;
7188
7189 len = buf.len - 1;
7190
7191 if (len == 1) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007192 if (str && buf.v[0] == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007193 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007194 else if (buf.v[0] == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007195 str += 1;
7196 }
7197 else if (len > 1 || comment) {
7198
7199 for (i = 0; i < len; ++i) {
7200
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007201 bool notend = len > i + 1;
7202 char c = string[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06007203
7204 if (i - 1 > len || string[i - 1] != '\\') {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007205 if (G.sbgn == G.send)
7206 str ^= c == G.sbgn;
7207 else if (c == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007208 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007209 else if (c == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007210 str += 1;
7211 }
7212
7213 if (c == '/' && notend && !comment && string[i + 1] == '*') {
7214 comment = true;
7215 break;
7216 }
7217 else if (c == '*' && notend && comment && string[i + 1] == '/')
7218 comment = false;
7219 }
7220
7221 if (str || comment || string[len - 2] == '\\') {
7222 bc_vec_concat(&buffer, buf.v);
7223 continue;
7224 }
7225 }
7226
7227 bc_vec_concat(&buffer, buf.v);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007228 s = bc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007229 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007230 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007231 // Debug config, non-interactive mode:
7232 // return all the way back to main.
7233 // Non-debug builds do not come here, they exit.
7234 break;
7235 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007236 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007237
Denys Vlasenko7d628012018-12-04 21:46:47 +01007238 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007239 }
Denys Vlasenkof522dd92018-12-07 16:35:43 +01007240 if (s == BC_STATUS_EOF) // input EOF (^D) is not an error
7241 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007242
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007243 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007244 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007245 }
7246 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007247 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007248 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007249
Gavin Howard01055ba2018-11-03 11:00:21 -06007250 bc_vec_free(&buf);
7251 bc_vec_free(&buffer);
7252 return s;
7253}
7254
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007255#if ENABLE_BC
7256static const char bc_lib[] = {
7257 "scale=20"
7258"\n" "define e(x){"
7259"\n" "auto b,s,n,r,d,i,p,f,v"
7260"\n" "b=ibase"
7261"\n" "ibase=A"
7262"\n" "if(x<0){"
7263"\n" "n=1"
7264"\n" "x=-x"
7265"\n" "}"
7266"\n" "s=scale"
7267"\n" "r=6+s+0.44*x"
7268"\n" "scale=scale(x)+1"
7269"\n" "while(x>1){"
7270"\n" "d+=1"
7271"\n" "x/=2"
7272"\n" "scale+=1"
7273"\n" "}"
7274"\n" "scale=r"
7275"\n" "r=x+1"
7276"\n" "p=x"
7277"\n" "f=v=1"
7278"\n" "for(i=2;v!=0;++i){"
7279"\n" "p*=x"
7280"\n" "f*=i"
7281"\n" "v=p/f"
7282"\n" "r+=v"
7283"\n" "}"
7284"\n" "while((d--)!=0)r*=r"
7285"\n" "scale=s"
7286"\n" "ibase=b"
7287"\n" "if(n!=0)return(1/r)"
7288"\n" "return(r/1)"
7289"\n" "}"
7290"\n" "define l(x){"
7291"\n" "auto b,s,r,p,a,q,i,v"
7292"\n" "b=ibase"
7293"\n" "ibase=A"
7294"\n" "if(x<=0){"
7295"\n" "r=(1-10^scale)/1"
7296"\n" "ibase=b"
7297"\n" "return(r)"
7298"\n" "}"
7299"\n" "s=scale"
7300"\n" "scale+=6"
7301"\n" "p=2"
7302"\n" "while(x>=2){"
7303"\n" "p*=2"
7304"\n" "x=sqrt(x)"
7305"\n" "}"
7306"\n" "while(x<=0.5){"
7307"\n" "p*=2"
7308"\n" "x=sqrt(x)"
7309"\n" "}"
7310"\n" "r=a=(x-1)/(x+1)"
7311"\n" "q=a*a"
7312"\n" "v=1"
7313"\n" "for(i=3;v!=0;i+=2){"
7314"\n" "a*=q"
7315"\n" "v=a/i"
7316"\n" "r+=v"
7317"\n" "}"
7318"\n" "r*=p"
7319"\n" "scale=s"
7320"\n" "ibase=b"
7321"\n" "return(r/1)"
7322"\n" "}"
7323"\n" "define s(x){"
7324"\n" "auto b,s,r,n,a,q,i"
7325"\n" "b=ibase"
7326"\n" "ibase=A"
7327"\n" "s=scale"
7328"\n" "scale=1.1*s+2"
7329"\n" "a=a(1)"
7330"\n" "if(x<0){"
7331"\n" "n=1"
7332"\n" "x=-x"
7333"\n" "}"
7334"\n" "scale=0"
7335"\n" "q=(x/a+2)/4"
7336"\n" "x=x-4*q*a"
7337"\n" "if(q%2!=0)x=-x"
7338"\n" "scale=s+2"
7339"\n" "r=a=x"
7340"\n" "q=-x*x"
7341"\n" "for(i=3;a!=0;i+=2){"
7342"\n" "a*=q/(i*(i-1))"
7343"\n" "r+=a"
7344"\n" "}"
7345"\n" "scale=s"
7346"\n" "ibase=b"
7347"\n" "if(n!=0)return(-r/1)"
7348"\n" "return(r/1)"
7349"\n" "}"
7350"\n" "define c(x){"
7351"\n" "auto b,s"
7352"\n" "b=ibase"
7353"\n" "ibase=A"
7354"\n" "s=scale"
7355"\n" "scale*=1.2"
7356"\n" "x=s(2*a(1)+x)"
7357"\n" "scale=s"
7358"\n" "ibase=b"
7359"\n" "return(x/1)"
7360"\n" "}"
7361"\n" "define a(x){"
7362"\n" "auto b,s,r,n,a,m,t,f,i,u"
7363"\n" "b=ibase"
7364"\n" "ibase=A"
7365"\n" "n=1"
7366"\n" "if(x<0){"
7367"\n" "n=-1"
7368"\n" "x=-x"
7369"\n" "}"
7370"\n" "if(x==1){"
7371"\n" "if(scale<65){"
7372"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7373"\n" "}"
7374"\n" "}"
7375"\n" "if(x==.2){"
7376"\n" "if(scale<65){"
7377"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7378"\n" "}"
7379"\n" "}"
7380"\n" "s=scale"
7381"\n" "if(x>.2){"
7382"\n" "scale+=5"
7383"\n" "a=a(.2)"
7384"\n" "}"
7385"\n" "scale=s+3"
7386"\n" "while(x>.2){"
7387"\n" "m+=1"
7388"\n" "x=(x-.2)/(1+.2*x)"
7389"\n" "}"
7390"\n" "r=u=x"
7391"\n" "f=-x*x"
7392"\n" "t=1"
7393"\n" "for(i=3;t!=0;i+=2){"
7394"\n" "u*=f"
7395"\n" "t=u/i"
7396"\n" "r+=t"
7397"\n" "}"
7398"\n" "scale=s"
7399"\n" "ibase=b"
7400"\n" "return((m*a+r)/n)"
7401"\n" "}"
7402"\n" "define j(n,x){"
7403"\n" "auto b,s,o,a,i,v,f"
7404"\n" "b=ibase"
7405"\n" "ibase=A"
7406"\n" "s=scale"
7407"\n" "scale=0"
7408"\n" "n/=1"
7409"\n" "if(n<0){"
7410"\n" "n=-n"
7411"\n" "if(n%2==1)o=1"
7412"\n" "}"
7413"\n" "a=1"
7414"\n" "for(i=2;i<=n;++i)a*=i"
7415"\n" "scale=1.5*s"
7416"\n" "a=(x^n)/2^n/a"
7417"\n" "r=v=1"
7418"\n" "f=-x*x/4"
7419"\n" "scale=scale+length(a)-scale(a)"
7420"\n" "for(i=1;v!=0;++i){"
7421"\n" "v=v*f/i/(n+i)"
7422"\n" "r+=v"
7423"\n" "}"
7424"\n" "scale=s"
7425"\n" "ibase=b"
7426"\n" "if(o!=0)a=-a"
7427"\n" "return(a*r/1)"
7428"\n" "}"
7429};
7430#endif // ENABLE_BC
7431
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007432static BcStatus bc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007433{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007434 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007435 size_t i;
7436
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007437#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007438 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007439
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007440 // We know that internal library is not buggy,
7441 // thus error checking is normally disabled.
7442# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007443 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007444 s = bc_parse_text(&G.prs, bc_lib);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007445 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007446
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007447 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007448 s = G.prs.parse(&G.prs);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007449 if (DEBUG_LIB && s) return s;
7450 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007451 s = bc_program_exec();
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007452 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007453 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007454#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007455
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007456 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007457 for (i = 0; !s && i < G.files.len; ++i)
7458 s = bc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007459 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7460 // Debug config, non-interactive mode:
7461 // return all the way back to main.
7462 // Non-debug builds do not come here, they exit.
7463 return s;
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007464 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007465
Denys Vlasenko91cde952018-12-10 20:56:08 +01007466 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007467 s = bc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007468
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007469 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
7470 s = bc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007471
Denys Vlasenko00d77792018-11-30 23:13:42 +01007472 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007473}
7474
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007475#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007476static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007477{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007478 bc_num_free(&G.prog.ib);
7479 bc_num_free(&G.prog.ob);
7480 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007481# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007482 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007483# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007484 bc_vec_free(&G.prog.fns);
7485 bc_vec_free(&G.prog.fn_map);
7486 bc_vec_free(&G.prog.vars);
7487 bc_vec_free(&G.prog.var_map);
7488 bc_vec_free(&G.prog.arrs);
7489 bc_vec_free(&G.prog.arr_map);
7490 bc_vec_free(&G.prog.strs);
7491 bc_vec_free(&G.prog.consts);
7492 bc_vec_free(&G.prog.results);
7493 bc_vec_free(&G.prog.stack);
7494 bc_num_free(&G.prog.last);
7495 bc_num_free(&G.prog.zero);
7496 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007497}
7498
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007499static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007500{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007501 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007502 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007503 bc_parse_free(&G.prs);
7504 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007505}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007506#endif
7507
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007508static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007509{
7510 size_t idx;
7511 BcInstPtr ip;
7512
7513 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7514 memset(&ip, 0, sizeof(BcInstPtr));
7515
7516 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007517 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007518 bc_num_ten(&G.prog.ib);
7519 G.prog.ib_t = 10;
7520
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007521 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007522 bc_num_ten(&G.prog.ob);
7523 G.prog.ob_t = 10;
7524
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007525 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007526 bc_num_ten(&G.prog.hexb);
7527 G.prog.hexb.num[0] = 6;
7528
7529#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007530 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007531 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7532#endif
7533
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007534 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007535 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007536
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007537 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007538 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007539
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007540 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007541 bc_num_one(&G.prog.one);
7542
7543 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007544 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007545
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007546 bc_program_addFunc(xstrdup("(main)"), &idx);
7547 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007548
7549 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007550 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007551
7552 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007553 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007554
7555 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7556 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7557 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7558 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7559 bc_vec_push(&G.prog.stack, &ip);
7560}
Gavin Howard01055ba2018-11-03 11:00:21 -06007561
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007562static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007563{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007564#if ENABLE_FEATURE_EDITING
7565 G.line_input_state = new_line_input_t(DO_HISTORY);
7566#endif
7567 G.prog.len = bc_vm_envLen(env_len);
7568
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007569 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007570 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007571 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007572 bc_program_init();
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007573 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007574 IF_BC(bc_parse_init(&G.prs, BC_PROG_MAIN);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007575 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007576 IF_DC(dc_parse_init(&G.prs, BC_PROG_MAIN);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007577 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007578
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007579 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007580#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007581 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007582 // With SA_RESTART, most system calls will restart
7583 // (IOW: they won't fail with EINTR).
7584 // In particular, this means ^C won't cause
7585 // stdout to get into "error state" if SIGINT hits
7586 // within write() syscall.
7587 // The downside is that ^C while line input is taken
7588 // will only be handled after [Enter] since read()
7589 // from stdin is not interrupted by ^C either,
7590 // it restarts, thus fgetc() does not return on ^C.
7591 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7592
7593 // Without SA_RESTART, this exhibits a bug:
7594 // "while (1) print 1" and try ^C-ing it.
7595 // Intermittently, instead of returning to input line,
7596 // you'll get "output error: Interrupted system call"
7597 // and exit.
7598 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007599#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007600 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007601 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007602 return 0; // "not a tty"
7603}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007604
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007605static BcStatus bc_vm_run(void)
7606{
7607 BcStatus st = bc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007608#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007609 if (G_exiting) // it was actually "halt" or "quit"
7610 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007611 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007612# if ENABLE_FEATURE_EDITING
7613 free_line_input_t(G.line_input_state);
7614# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007615 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007616#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007617 return st;
7618}
7619
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007620#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007621int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007622int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007623{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007624 int is_tty;
7625
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007626 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007627 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007628
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007629 is_tty = bc_vm_init("BC_LINE_LENGTH");
7630
7631 bc_args(argv);
7632
7633 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7634 bc_vm_info();
7635
7636 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007637}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007638#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007639
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007640#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007641int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007642int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007643{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007644 int noscript;
7645
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007646 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007647 G.sbgn = '[';
7648 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007649 /*
7650 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7651 * 1 char wider than bc from the same package.
7652 * Both default width, and xC_LINE_LENGTH=N are wider:
7653 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7654 * 1234\
7655 * 56
7656 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7657 * 123\
7658 * 456
7659 * Do the same, or it's a bug?
7660 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007661 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007662
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007663 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7664 noscript = BC_FLAG_I;
7665 for (;;) {
7666 int n = getopt(argc, argv, "e:f:x");
7667 if (n <= 0)
7668 break;
7669 switch (n) {
7670 case 'e':
7671 noscript = 0;
7672 n = bc_vm_process(optarg);
7673 if (n) return n;
7674 break;
7675 case 'f':
7676 noscript = 0;
7677 bc_vm_file(optarg);
7678 break;
7679 case 'x':
7680 option_mask32 |= DC_FLAG_X;
7681 break;
7682 default:
7683 bb_show_usage();
7684 }
7685 }
7686 argv += optind;
7687
7688 while (*argv) {
7689 noscript = 0;
7690 bc_vec_push(&G.files, argv++);
7691 }
7692
7693 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7694
7695 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007696}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007697#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007698
7699#endif // not DC_SMALL