blob: e3e8198b7b31d06105502325087ec6750bb94674 [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 Vlasenko4dd36522018-12-11 22:26:38 +01001361// Can return success (0) or BC_STATUS_EOF.
1362// Exits with error message if read error is detected.
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001363static BcStatus bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001364{
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001365 BcStatus s;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001366 bool bad_chars;
Gavin Howard01055ba2018-11-03 11:00:21 -06001367
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001368 s = BC_STATUS_SUCCESS;
Denys Vlasenko00d77792018-11-30 23:13:42 +01001369 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001370 int c;
Gavin Howard01055ba2018-11-03 11:00:21 -06001371
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001372 bad_chars = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001373 bc_vec_pop_all(vec);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001374
1375 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001376
Gavin Howard01055ba2018-11-03 11:00:21 -06001377#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001378 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001379 intr:
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001380 G_interrupt = 0;
Denys Vlasenkoac6ed112018-12-08 21:39:10 +01001381 // GNU bc says "interrupted execution."
1382 // GNU dc says "Interrupt!"
1383 fputs("\ninterrupted execution\n", stderr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001384 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001385# if ENABLE_FEATURE_EDITING
1386 if (G_ttyin) {
1387 int n, i;
1388# define line_buf bb_common_bufsiz1
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001389 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001390 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1391 if (n == 0) // ^C
1392 goto intr;
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001393 s = BC_STATUS_EOF;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001394 break;
1395 }
1396 i = 0;
1397 for (;;) {
1398 c = line_buf[i++];
1399 if (!c) break;
1400 bad_chars |= push_input_byte(vec, c);
1401 }
1402# undef line_buf
1403 } else
1404# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001405#endif
Denys Vlasenkoed849352018-12-06 10:26:13 +01001406 {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001407 IF_FEATURE_BC_SIGNALS(errno = 0;)
1408 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001409 c = fgetc(stdin);
1410#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
1411 // Both conditions appear simultaneously, check both just in case
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001412 if (errno == EINTR || G_interrupt) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001413 // ^C was pressed
1414 clearerr(stdin);
1415 goto intr;
1416 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001417#endif
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001418 if (c == EOF) {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001419 if (ferror(stdin))
1420 quit(); // this emits error message
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001421 s = BC_STATUS_EOF;
Denys Vlasenkoed849352018-12-06 10:26:13 +01001422 // Note: EOF does not append '\n', therefore:
1423 // printf 'print 123\n' | bc - works
1424 // printf 'print 123' | bc - fails (syntax error)
1425 break;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001426 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001427 bad_chars |= push_input_byte(vec, c);
1428 } while (c != '\n');
Denys Vlasenkoed849352018-12-06 10:26:13 +01001429 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001430 } while (bad_chars);
Gavin Howard01055ba2018-11-03 11:00:21 -06001431
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001432 bc_vec_pushZeroByte(vec);
Gavin Howard01055ba2018-11-03 11:00:21 -06001433
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001434 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001435}
1436
Denys Vlasenkodf515392018-12-02 19:27:48 +01001437static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001438{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001439 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001440 size_t size = ((size_t) -1);
1441 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001442
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001443 // Never returns NULL (dies on errors)
1444 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001445
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001446 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001447 char c = buf[i];
1448 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1449 || c > 0x7e
1450 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001451 free(buf);
1452 buf = NULL;
1453 break;
1454 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001455 }
1456
Denys Vlasenkodf515392018-12-02 19:27:48 +01001457 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001458}
1459
Gavin Howard01055ba2018-11-03 11:00:21 -06001460static void bc_num_setToZero(BcNum *n, size_t scale)
1461{
1462 n->len = 0;
1463 n->neg = false;
1464 n->rdx = scale;
1465}
1466
1467static void bc_num_zero(BcNum *n)
1468{
1469 bc_num_setToZero(n, 0);
1470}
1471
1472static void bc_num_one(BcNum *n)
1473{
1474 bc_num_setToZero(n, 0);
1475 n->len = 1;
1476 n->num[0] = 1;
1477}
1478
1479static void bc_num_ten(BcNum *n)
1480{
1481 bc_num_setToZero(n, 0);
1482 n->len = 2;
1483 n->num[0] = 0;
1484 n->num[1] = 1;
1485}
1486
Denys Vlasenko3129f702018-12-09 12:04:44 +01001487// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001488static void bc_num_init(BcNum *n, size_t req)
1489{
1490 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001491 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001492 n->num = xmalloc(req);
1493 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001494 n->rdx = 0;
1495 n->len = 0;
1496 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001497}
1498
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001499static void bc_num_init_DEF_SIZE(BcNum *n)
1500{
1501 bc_num_init(n, BC_NUM_DEF_SIZE);
1502}
1503
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001504static void bc_num_expand(BcNum *n, size_t req)
1505{
1506 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1507 if (req > n->cap) {
1508 n->num = xrealloc(n->num, req);
1509 n->cap = req;
1510 }
1511}
1512
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001513static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001514{
1515 free(((BcNum *) num)->num);
1516}
1517
1518static void bc_num_copy(BcNum *d, BcNum *s)
1519{
1520 if (d != s) {
1521 bc_num_expand(d, s->cap);
1522 d->len = s->len;
1523 d->neg = s->neg;
1524 d->rdx = s->rdx;
1525 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1526 }
1527}
1528
Denys Vlasenko29301232018-12-11 15:29:32 +01001529static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001530{
1531 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001532 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001533
Denys Vlasenko29301232018-12-11 15:29:32 +01001534 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001535
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001536 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001537
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001538 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001539
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001540 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001541 pow *= 10;
1542
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001543 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001544 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001545 prev = result;
1546 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001547 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001548 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001549
Denys Vlasenko29301232018-12-11 15:29:32 +01001550 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001551}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001552#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001553# define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001554#endif
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001555
1556static void bc_num_ulong2num(BcNum *n, unsigned long val)
1557{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001558 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001559
1560 bc_num_zero(n);
1561
1562 if (val == 0) return;
1563
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001564 if (ULONG_MAX == 0xffffffffUL)
1565 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001566 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001567 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001568 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001569
1570 ptr = n->num;
1571 for (;;) {
1572 n->len++;
1573 *ptr++ = val % 10;
1574 val /= 10;
1575 if (val == 0) break;
1576 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001577}
1578
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001579static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001580 size_t len)
1581{
1582 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001583 for (i = 0; i < len; ++i) {
1584 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001585 a[i + j++] += 10;
1586 a[i + j] -= 1;
1587 }
1588 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001589}
1590
1591static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1592{
1593 size_t i;
1594 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001595 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001596 return BC_NUM_NEG(i + 1, c < 0);
1597}
1598
1599static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1600{
1601 size_t i, min, a_int, b_int, diff;
1602 BcDig *max_num, *min_num;
1603 bool a_max, neg = false;
1604 ssize_t cmp;
1605
1606 if (a == b) return 0;
1607 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1608 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
1609 if (a->neg) {
1610 if (b->neg)
1611 neg = true;
1612 else
1613 return -1;
1614 }
1615 else if (b->neg)
1616 return 1;
1617
1618 a_int = BC_NUM_INT(a);
1619 b_int = BC_NUM_INT(b);
1620 a_int -= b_int;
1621 a_max = (a->rdx > b->rdx);
1622
1623 if (a_int != 0) return (ssize_t) a_int;
1624
1625 if (a_max) {
1626 min = b->rdx;
1627 diff = a->rdx - b->rdx;
1628 max_num = a->num + diff;
1629 min_num = b->num;
1630 }
1631 else {
1632 min = a->rdx;
1633 diff = b->rdx - a->rdx;
1634 max_num = b->num + diff;
1635 min_num = a->num;
1636 }
1637
1638 cmp = bc_num_compare(max_num, min_num, b_int + min);
1639 if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg);
1640
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001641 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001642 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
1643 }
1644
1645 return 0;
1646}
1647
1648static void bc_num_truncate(BcNum *n, size_t places)
1649{
1650 if (places == 0) return;
1651
1652 n->rdx -= places;
1653
1654 if (n->len != 0) {
1655 n->len -= places;
1656 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1657 }
1658}
1659
1660static void bc_num_extend(BcNum *n, size_t places)
1661{
1662 size_t len = n->len + places;
1663
1664 if (places != 0) {
1665
1666 if (n->cap < len) bc_num_expand(n, len);
1667
1668 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1669 memset(n->num, 0, sizeof(BcDig) * places);
1670
1671 n->len += places;
1672 n->rdx += places;
1673 }
1674}
1675
1676static void bc_num_clean(BcNum *n)
1677{
1678 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1679 if (n->len == 0)
1680 n->neg = false;
1681 else if (n->len < n->rdx)
1682 n->len = n->rdx;
1683}
1684
1685static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1686{
1687 if (n->rdx < scale)
1688 bc_num_extend(n, scale - n->rdx);
1689 else
1690 bc_num_truncate(n, n->rdx - scale);
1691
1692 bc_num_clean(n);
1693 if (n->len != 0) n->neg = !neg1 != !neg2;
1694}
1695
1696static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1697 BcNum *restrict b)
1698{
1699 if (idx < n->len) {
1700
1701 b->len = n->len - idx;
1702 a->len = idx;
1703 a->rdx = b->rdx = 0;
1704
1705 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1706 memcpy(a->num, n->num, idx * sizeof(BcDig));
1707 }
1708 else {
1709 bc_num_zero(b);
1710 bc_num_copy(a, n);
1711 }
1712
1713 bc_num_clean(a);
1714 bc_num_clean(b);
1715}
1716
Denys Vlasenko29301232018-12-11 15:29:32 +01001717static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001718{
Denys Vlasenko29301232018-12-11 15:29:32 +01001719 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001720
1721 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1722 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1723 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001724 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001725 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001726
1727 if (n->rdx >= places)
1728 n->rdx -= places;
1729 else {
1730 bc_num_extend(n, places - n->rdx);
1731 n->rdx = 0;
1732 }
1733
1734 bc_num_clean(n);
1735
Denys Vlasenko29301232018-12-11 15:29:32 +01001736 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001737}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001738#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001739# define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001740#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001741
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001742static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001743{
1744 BcNum one;
1745 BcDig num[2];
1746
1747 one.cap = 2;
1748 one.num = num;
1749 bc_num_one(&one);
1750
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001751 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001752}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001753#if ERRORS_ARE_FATAL
1754# define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS)
1755#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001756
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001757static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001758{
1759 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1760 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1761 int carry, in;
1762
1763 // Because this function doesn't need to use scale (per the bc spec),
1764 // I am hijacking it to say whether it's doing an add or a subtract.
1765
1766 if (a->len == 0) {
1767 bc_num_copy(c, b);
1768 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001769 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001770 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001771 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001772 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001773 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001774 }
1775
1776 c->neg = a->neg;
1777 c->rdx = BC_MAX(a->rdx, b->rdx);
1778 min_rdx = BC_MIN(a->rdx, b->rdx);
1779 c->len = 0;
1780
1781 if (a->rdx > b->rdx) {
1782 diff = a->rdx - b->rdx;
1783 ptr = a->num;
1784 ptr_a = a->num + diff;
1785 ptr_b = b->num;
1786 }
1787 else {
1788 diff = b->rdx - a->rdx;
1789 ptr = b->num;
1790 ptr_a = a->num;
1791 ptr_b = b->num + diff;
1792 }
1793
1794 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1795
1796 ptr_c += diff;
1797 a_int = BC_NUM_INT(a);
1798 b_int = BC_NUM_INT(b);
1799
1800 if (a_int > b_int) {
1801 min_int = b_int;
1802 max = a_int;
1803 ptr = ptr_a;
1804 }
1805 else {
1806 min_int = a_int;
1807 max = b_int;
1808 ptr = ptr_b;
1809 }
1810
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001811 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001812 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1813 carry = in / 10;
1814 ptr_c[i] = (BcDig)(in % 10);
1815 }
1816
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001817 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001818 in = ((int) ptr[i]) + carry;
1819 carry = in / 10;
1820 ptr_c[i] = (BcDig)(in % 10);
1821 }
1822
1823 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1824
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001825 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001826}
1827
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001828static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001829{
Gavin Howard01055ba2018-11-03 11:00:21 -06001830 ssize_t cmp;
1831 BcNum *minuend, *subtrahend;
1832 size_t start;
1833 bool aneg, bneg, neg;
1834
1835 // Because this function doesn't need to use scale (per the bc spec),
1836 // I am hijacking it to say whether it's doing an add or a subtract.
1837
1838 if (a->len == 0) {
1839 bc_num_copy(c, b);
1840 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001841 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001842 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001843 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001844 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001845 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001846 }
1847
1848 aneg = a->neg;
1849 bneg = b->neg;
1850 a->neg = b->neg = false;
1851
1852 cmp = bc_num_cmp(a, b);
1853
1854 a->neg = aneg;
1855 b->neg = bneg;
1856
1857 if (cmp == 0) {
1858 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001859 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001860 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001861 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001862 neg = a->neg;
1863 minuend = a;
1864 subtrahend = b;
1865 }
1866 else {
1867 neg = b->neg;
1868 if (sub) neg = !neg;
1869 minuend = b;
1870 subtrahend = a;
1871 }
1872
1873 bc_num_copy(c, minuend);
1874 c->neg = neg;
1875
1876 if (c->rdx < subtrahend->rdx) {
1877 bc_num_extend(c, subtrahend->rdx - c->rdx);
1878 start = 0;
1879 }
1880 else
1881 start = c->rdx - subtrahend->rdx;
1882
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001883 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001884
1885 bc_num_clean(c);
1886
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001887 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001888}
1889
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001890static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001891 BcNum *restrict c)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001892#if ERRORS_ARE_FATAL
1893# define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS)
1894#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001895{
1896 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001897 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001898 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001899 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001900
Gavin Howard01055ba2018-11-03 11:00:21 -06001901 if (a->len == 0 || b->len == 0) {
1902 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001903 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001904 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001905 aone = BC_NUM_ONE(a);
1906 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001907 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001908 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001909 }
1910
1911 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1912 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1913 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001914 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001915 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001916
Gavin Howard01055ba2018-11-03 11:00:21 -06001917 bc_num_expand(c, a->len + b->len + 1);
1918
1919 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001920 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001921
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001922 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001923
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001924 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001925 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001926 unsigned in = c->num[i + j];
1927 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1928 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001929 carry = in / 10;
1930 c->num[i + j] = (BcDig)(in % 10);
1931 }
1932
1933 c->num[i + j] += (BcDig) carry;
1934 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001935
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001936#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001937 // a=2^1000000
1938 // a*a <- without check below, this will not be interruptible
1939 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001940#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001941 }
1942
1943 c->len = len;
1944
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001945 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001946 }
1947
1948 bc_num_init(&l1, max);
1949 bc_num_init(&h1, max);
1950 bc_num_init(&l2, max);
1951 bc_num_init(&h2, max);
1952 bc_num_init(&m1, max);
1953 bc_num_init(&m2, max);
1954 bc_num_init(&z0, max);
1955 bc_num_init(&z1, max);
1956 bc_num_init(&z2, max);
1957 bc_num_init(&temp, max + max);
1958
1959 bc_num_split(a, max2, &l1, &h1);
1960 bc_num_split(b, max2, &l2, &h2);
1961
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001962 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001963 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001964 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001965 if (s) goto err;
1966
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001967 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001968 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001969 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001970 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001971 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001972 if (s) goto err;
1973
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001974 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001975 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001976 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001977 if (s) goto err;
1978
Denys Vlasenko29301232018-12-11 15:29:32 +01001979 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001980 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001981 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001982 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001983 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001984 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001985 s = zbc_num_add(&temp, &z2, c, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001986
1987err:
1988 bc_num_free(&temp);
1989 bc_num_free(&z2);
1990 bc_num_free(&z1);
1991 bc_num_free(&z0);
1992 bc_num_free(&m2);
1993 bc_num_free(&m1);
1994 bc_num_free(&h2);
1995 bc_num_free(&l2);
1996 bc_num_free(&h1);
1997 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001998 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001999}
2000
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002001static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002002{
2003 BcStatus s;
2004 BcNum cpa, cpb;
2005 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
2006
2007 scale = BC_MAX(scale, a->rdx);
2008 scale = BC_MAX(scale, b->rdx);
2009 scale = BC_MIN(a->rdx + b->rdx, scale);
2010 maxrdx = BC_MAX(maxrdx, scale);
2011
2012 bc_num_init(&cpa, a->len);
2013 bc_num_init(&cpb, b->len);
2014
2015 bc_num_copy(&cpa, a);
2016 bc_num_copy(&cpb, b);
2017 cpa.neg = cpb.neg = false;
2018
Denys Vlasenko29301232018-12-11 15:29:32 +01002019 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002020 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01002021 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002022 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002023 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06002024 if (s) goto err;
2025
2026 maxrdx += scale;
2027 bc_num_expand(c, c->len + maxrdx);
2028
2029 if (c->len < maxrdx) {
2030 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
2031 c->len += maxrdx;
2032 }
2033
2034 c->rdx = maxrdx;
2035 bc_num_retireMul(c, scale, a->neg, b->neg);
2036
2037err:
2038 bc_num_free(&cpb);
2039 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002040 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002041}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002042#if ERRORS_ARE_FATAL
2043# define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS)
2044#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002045
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002046static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002047{
2048 BcStatus s = BC_STATUS_SUCCESS;
2049 BcDig *n, *p, q;
2050 size_t len, end, i;
2051 BcNum cp;
2052 bool zero = true;
2053
2054 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002055 RETURN_STATUS(bc_error("divide by zero"));
2056 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002057 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002058 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002059 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002060 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002061 bc_num_copy(c, a);
2062 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002063 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002064 }
2065
2066 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2067 bc_num_copy(&cp, a);
2068 len = b->len;
2069
2070 if (len > cp.len) {
2071 bc_num_expand(&cp, len + 2);
2072 bc_num_extend(&cp, len - cp.len);
2073 }
2074
2075 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2076 cp.rdx -= b->rdx;
2077 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2078
2079 if (b->rdx == b->len) {
2080 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
2081 len -= i - 1;
2082 }
2083
2084 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2085
2086 // We want an extra zero in front to make things simpler.
2087 cp.num[cp.len++] = 0;
2088 end = cp.len - len;
2089
2090 bc_num_expand(c, cp.len);
2091
2092 bc_num_zero(c);
2093 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2094 c->rdx = cp.rdx;
2095 c->len = cp.len;
2096 p = b->num;
2097
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002098 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002099 n = cp.num + i;
2100 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002101 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002102 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002103#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002104 // a=2^100000
2105 // scale=40000
2106 // 1/a <- without check below, this will not be interruptible
2107 if (G_interrupt) {
2108 s = BC_STATUS_FAILURE;
2109 break;
2110 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002111#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002112 }
2113
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002114 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002115 bc_num_free(&cp);
2116
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002117 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002118}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002119#if ERRORS_ARE_FATAL
2120# define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS)
2121#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002122
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002123static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002124 BcNum *restrict d, size_t scale, size_t ts)
2125{
2126 BcStatus s;
2127 BcNum temp;
2128 bool neg;
2129
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002130 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002131 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002132
2133 if (a->len == 0) {
2134 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002135 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002136 }
2137
2138 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002139 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002140 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002141
2142 if (scale != 0) scale = ts;
2143
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002144 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002145 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002146 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002147 if (s) goto err;
2148
2149 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2150
2151 neg = d->neg;
2152 bc_num_retireMul(d, ts, a->neg, b->neg);
2153 d->neg = neg;
2154
2155err:
2156 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002157 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002158}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002159#if ERRORS_ARE_FATAL
2160# define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS)
2161#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002162
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002163static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002164{
2165 BcStatus s;
2166 BcNum c1;
2167 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2168
2169 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002170 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002171 bc_num_free(&c1);
2172
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002173 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002174}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002175#if ERRORS_ARE_FATAL
2176# define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS)
2177#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002178
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002179static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002180{
2181 BcStatus s = BC_STATUS_SUCCESS;
2182 BcNum copy;
2183 unsigned long pow;
2184 size_t i, powrdx, resrdx;
2185 bool neg, zero;
2186
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002187 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002188
2189 if (b->len == 0) {
2190 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002191 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002192 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002193 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002194 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002195 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002196 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002197 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002198 if (!b->neg)
2199 bc_num_copy(c, a);
2200 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002201 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002202 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002203 }
2204
2205 neg = b->neg;
2206 b->neg = false;
2207
Denys Vlasenko29301232018-12-11 15:29:32 +01002208 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002209 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002210
2211 bc_num_init(&copy, a->len);
2212 bc_num_copy(&copy, a);
2213
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002214 if (!neg) {
2215 if (a->rdx > scale)
2216 scale = a->rdx;
2217 if (a->rdx * pow < scale)
2218 scale = a->rdx * pow;
2219 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002220
2221 b->neg = neg;
2222
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002223 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002224 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002225 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002226 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002227 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002228 //if (G_interrupt) {
2229 // s = BC_STATUS_FAILURE;
2230 // goto err;
2231 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002232 }
2233
Gavin Howard01055ba2018-11-03 11:00:21 -06002234 bc_num_copy(c, &copy);
2235
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002236 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002237
2238 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002239 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002240 if (s) goto err;
2241
2242 if (pow & 1) {
2243 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002244 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002245 if (s) goto err;
2246 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002247 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002248 //if (G_interrupt) {
2249 // s = BC_STATUS_FAILURE;
2250 // goto err;
2251 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002252 }
2253
2254 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002255 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002256 if (s) goto err;
2257 }
2258
Gavin Howard01055ba2018-11-03 11:00:21 -06002259 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2260
2261 // We can't use bc_num_clean() here.
2262 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2263 if (zero) bc_num_setToZero(c, scale);
2264
2265err:
2266 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002267 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002268}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002269#if ERRORS_ARE_FATAL
2270# define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS)
2271#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002272
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002273static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002274 BcNumBinaryOp op, size_t req)
2275{
2276 BcStatus s;
2277 BcNum num2, *ptr_a, *ptr_b;
2278 bool init = false;
2279
2280 if (c == a) {
2281 ptr_a = &num2;
2282 memcpy(ptr_a, c, sizeof(BcNum));
2283 init = true;
2284 }
2285 else
2286 ptr_a = a;
2287
2288 if (c == b) {
2289 ptr_b = &num2;
2290 if (c != a) {
2291 memcpy(ptr_b, c, sizeof(BcNum));
2292 init = true;
2293 }
2294 }
2295 else
2296 ptr_b = b;
2297
2298 if (init)
2299 bc_num_init(c, req);
2300 else
2301 bc_num_expand(c, req);
2302
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002303#if !ERRORS_ARE_FATAL
Gavin Howard01055ba2018-11-03 11:00:21 -06002304 s = op(ptr_a, ptr_b, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002305#else
2306 op(ptr_a, ptr_b, c, scale);
2307 s = BC_STATUS_SUCCESS;
2308#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002309
2310 if (init) bc_num_free(&num2);
2311
Denys Vlasenko29301232018-12-11 15:29:32 +01002312 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002313}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002314#if ERRORS_ARE_FATAL
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002315# define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002316#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002317
Denys Vlasenko9311e012018-12-10 11:54:18 +01002318static bool bc_num_strValid(const char *val, size_t base)
2319{
2320 BcDig b;
2321 bool radix;
2322
2323 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2324 radix = false;
2325 for (;;) {
2326 BcDig c = *val++;
2327 if (c == '\0')
2328 break;
2329 if (c == '.') {
2330 if (radix) return false;
2331 radix = true;
2332 continue;
2333 }
2334 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2335 return false;
2336 }
2337 return true;
2338}
2339
2340// Note: n is already "bc_num_zero()"ed,
2341// leading zeroes in "val" are removed
2342static void bc_num_parseDecimal(BcNum *n, const char *val)
2343{
2344 size_t len, i;
2345 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002346
2347 len = strlen(val);
2348 if (len == 0)
2349 return;
2350
Denys Vlasenko9311e012018-12-10 11:54:18 +01002351 bc_num_expand(n, len);
2352
2353 ptr = strchr(val, '.');
2354
2355 n->rdx = 0;
2356 if (ptr != NULL)
2357 n->rdx = (size_t)((val + len) - (ptr + 1));
2358
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002359 for (i = 0; val[i]; ++i) {
2360 if (val[i] != '0' && val[i] != '.') {
2361 // Not entirely zero value - convert it, and exit
2362 i = len - 1;
2363 for (;;) {
2364 n->num[n->len] = val[i] - '0';
2365 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002366 skip_dot:
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002367 if ((ssize_t)--i == (ssize_t)-1) break;
2368 if (val[i] == '.') goto skip_dot;
2369 }
2370 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002371 }
2372 }
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002373 // if this is reached, the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002374}
2375
2376// Note: n is already "bc_num_zero()"ed,
2377// leading zeroes in "val" are removed
2378static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2379{
2380 BcStatus s;
2381 BcNum temp, mult, result;
2382 BcDig c = '\0';
2383 unsigned long v;
2384 size_t i, digits;
2385
2386 for (i = 0; ; ++i) {
2387 if (val[i] == '\0')
2388 return;
2389 if (val[i] != '.' && val[i] != '0')
2390 break;
2391 }
2392
2393 bc_num_init_DEF_SIZE(&temp);
2394 bc_num_init_DEF_SIZE(&mult);
2395
2396 for (;;) {
2397 c = *val++;
2398 if (c == '\0') goto int_err;
2399 if (c == '.') break;
2400
2401 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2402
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002403 s = zbc_num_mul(n, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002404 if (s) goto int_err;
2405 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002406 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002407 if (s) goto int_err;
2408 }
2409
2410 bc_num_init(&result, base->len);
2411 //bc_num_zero(&result); - already is
2412 bc_num_one(&mult);
2413
2414 digits = 0;
2415 for (;;) {
2416 c = *val++;
2417 if (c == '\0') break;
2418 digits++;
2419
2420 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2421
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002422 s = zbc_num_mul(&result, base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002423 if (s) goto err;
2424 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002425 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002426 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002427 s = zbc_num_mul(&mult, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002428 if (s) goto err;
2429 }
2430
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002431 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002432 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002433 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002434 if (s) goto err;
2435
2436 if (n->len != 0) {
2437 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2438 } else
2439 bc_num_zero(n);
2440
2441err:
2442 bc_num_free(&result);
2443int_err:
2444 bc_num_free(&mult);
2445 bc_num_free(&temp);
2446}
2447
Denys Vlasenko29301232018-12-11 15:29:32 +01002448static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002449 size_t base_t)
2450{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002451 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002452 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002453
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002454 bc_num_zero(n);
2455 while (*val == '0') val++;
2456
Gavin Howard01055ba2018-11-03 11:00:21 -06002457 if (base_t == 10)
2458 bc_num_parseDecimal(n, val);
2459 else
2460 bc_num_parseBase(n, val, base);
2461
Denys Vlasenko29301232018-12-11 15:29:32 +01002462 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002463}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002464#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002465# define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002466#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002467
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002468static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002469{
2470 BcStatus s;
2471 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2472 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2473 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2474
2475 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2476 bc_num_expand(b, req);
2477
2478 if (a->len == 0) {
2479 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002480 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002481 }
2482 else if (a->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002483 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002484 else if (BC_NUM_ONE(a)) {
2485 bc_num_one(b);
2486 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002487 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002488 }
2489
2490 scale = BC_MAX(scale, a->rdx) + 1;
2491 len = a->len + scale;
2492
2493 bc_num_init(&num1, len);
2494 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002495 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002496
2497 bc_num_one(&half);
2498 half.num[0] = 5;
2499 half.rdx = 1;
2500
2501 bc_num_init(&f, len);
2502 bc_num_init(&fprime, len);
2503
2504 x0 = &num1;
2505 x1 = &num2;
2506
2507 bc_num_one(x0);
2508 pow = BC_NUM_INT(a);
2509
2510 if (pow) {
2511
2512 if (pow & 1)
2513 x0->num[0] = 2;
2514 else
2515 x0->num[0] = 6;
2516
2517 pow -= 2 - (pow & 1);
2518
2519 bc_num_extend(x0, pow);
2520
2521 // Make sure to move the radix back.
2522 x0->rdx -= pow;
2523 }
2524
2525 x0->rdx = digs = digs1 = 0;
2526 resrdx = scale + 2;
2527 len = BC_NUM_INT(x0) + resrdx - 1;
2528
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002529 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002530
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002531 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002532 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002533 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002534 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002535 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002536 if (s) goto err;
2537
2538 cmp = bc_num_cmp(x1, x0);
2539 digs = x1->len - (unsigned long long) llabs(cmp);
2540
2541 if (cmp == cmp2 && digs == digs1)
2542 times += 1;
2543 else
2544 times = 0;
2545
2546 resrdx += times > 4;
2547
2548 cmp2 = cmp1;
2549 cmp1 = cmp;
2550 digs1 = digs;
2551
2552 temp = x0;
2553 x0 = x1;
2554 x1 = temp;
2555 }
2556
Gavin Howard01055ba2018-11-03 11:00:21 -06002557 bc_num_copy(b, x0);
2558 scale -= 1;
2559 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2560
2561err:
2562 bc_num_free(&fprime);
2563 bc_num_free(&f);
2564 bc_num_free(&half);
2565 bc_num_free(&num2);
2566 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002567 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002568}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002569#if ERRORS_ARE_FATAL
2570# define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS)
2571#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002572
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002573static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002574 size_t scale)
2575{
2576 BcStatus s;
2577 BcNum num2, *ptr_a;
2578 bool init = false;
2579 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2580
2581 if (c == a) {
2582 memcpy(&num2, c, sizeof(BcNum));
2583 ptr_a = &num2;
2584 bc_num_init(c, len);
2585 init = true;
2586 }
2587 else {
2588 ptr_a = a;
2589 bc_num_expand(c, len);
2590 }
2591
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002592 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002593
2594 if (init) bc_num_free(&num2);
2595
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002596 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002597}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002598#if ERRORS_ARE_FATAL
2599# define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
2600#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002601
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002602#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002603static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002604{
2605 BcStatus s;
2606 BcNum base, exp, two, temp;
2607
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002608 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002609 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002610 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002611 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002612 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002613 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002614
2615 bc_num_expand(d, c->len);
2616 bc_num_init(&base, c->len);
2617 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002618 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002619 bc_num_init(&temp, b->len);
2620
2621 bc_num_one(&two);
2622 two.num[0] = 2;
2623 bc_num_one(d);
2624
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002625 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002626 if (s) goto err;
2627 bc_num_copy(&exp, b);
2628
2629 while (exp.len != 0) {
2630
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002631 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002632 if (s) goto err;
2633
2634 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002635 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002636 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002637 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002638 if (s) goto err;
2639 }
2640
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002641 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002642 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002643 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002644 if (s) goto err;
2645 }
2646
2647err:
2648 bc_num_free(&temp);
2649 bc_num_free(&two);
2650 bc_num_free(&exp);
2651 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002652 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002653}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002654#if ERRORS_ARE_FATAL
2655# define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
2656#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002657#endif // ENABLE_DC
2658
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002659#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002660static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002661{
2662 BcId a;
2663 size_t i;
2664
2665 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002666 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002667 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002668 }
2669
2670 a.idx = var;
2671 a.name = name;
2672
2673 bc_vec_push(&f->autos, &a);
2674
Denys Vlasenko29301232018-12-11 15:29:32 +01002675 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002676}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002677#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002678# define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002679#endif
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002680#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002681
2682static void bc_func_init(BcFunc *f)
2683{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002684 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002685 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2686 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2687 f->nparams = 0;
2688}
2689
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002690static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002691{
2692 BcFunc *f = (BcFunc *) func;
2693 bc_vec_free(&f->code);
2694 bc_vec_free(&f->autos);
2695 bc_vec_free(&f->labels);
2696}
2697
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002698static void bc_array_expand(BcVec *a, size_t len);
2699
Gavin Howard01055ba2018-11-03 11:00:21 -06002700static void bc_array_init(BcVec *a, bool nums)
2701{
2702 if (nums)
2703 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2704 else
2705 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2706 bc_array_expand(a, 1);
2707}
2708
Gavin Howard01055ba2018-11-03 11:00:21 -06002709static void bc_array_expand(BcVec *a, size_t len)
2710{
2711 BcResultData data;
2712
2713 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2714 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002715 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002716 bc_vec_push(a, &data.n);
2717 }
2718 }
2719 else {
2720 while (len > a->len) {
2721 bc_array_init(&data.v, true);
2722 bc_vec_push(a, &data.v);
2723 }
2724 }
2725}
2726
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002727static void bc_array_copy(BcVec *d, const BcVec *s)
2728{
2729 size_t i;
2730
2731 bc_vec_pop_all(d);
2732 bc_vec_expand(d, s->cap);
2733 d->len = s->len;
2734
2735 for (i = 0; i < s->len; ++i) {
2736 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2737 bc_num_init(dnum, snum->len);
2738 bc_num_copy(dnum, snum);
2739 }
2740}
2741
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002742static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002743{
2744 free(*((char **) string));
2745}
2746
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002747#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002748static void bc_result_copy(BcResult *d, BcResult *src)
2749{
2750 d->t = src->t;
2751
2752 switch (d->t) {
2753
2754 case BC_RESULT_TEMP:
2755 case BC_RESULT_IBASE:
2756 case BC_RESULT_SCALE:
2757 case BC_RESULT_OBASE:
2758 {
2759 bc_num_init(&d->d.n, src->d.n.len);
2760 bc_num_copy(&d->d.n, &src->d.n);
2761 break;
2762 }
2763
2764 case BC_RESULT_VAR:
2765 case BC_RESULT_ARRAY:
2766 case BC_RESULT_ARRAY_ELEM:
2767 {
2768 d->d.id.name = xstrdup(src->d.id.name);
2769 break;
2770 }
2771
2772 case BC_RESULT_CONSTANT:
2773 case BC_RESULT_LAST:
2774 case BC_RESULT_ONE:
2775 case BC_RESULT_STR:
2776 {
2777 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2778 break;
2779 }
2780 }
2781}
2782#endif // ENABLE_DC
2783
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002784static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002785{
2786 BcResult *r = (BcResult *) result;
2787
2788 switch (r->t) {
2789
2790 case BC_RESULT_TEMP:
2791 case BC_RESULT_IBASE:
2792 case BC_RESULT_SCALE:
2793 case BC_RESULT_OBASE:
2794 {
2795 bc_num_free(&r->d.n);
2796 break;
2797 }
2798
2799 case BC_RESULT_VAR:
2800 case BC_RESULT_ARRAY:
2801 case BC_RESULT_ARRAY_ELEM:
2802 {
2803 free(r->d.id.name);
2804 break;
2805 }
2806
2807 default:
2808 {
2809 // Do nothing.
2810 break;
2811 }
2812 }
2813}
2814
2815static void bc_lex_lineComment(BcLex *l)
2816{
2817 l->t.t = BC_LEX_WHITESPACE;
2818 while (l->i < l->len && l->buf[l->i++] != '\n');
2819 --l->i;
2820}
2821
2822static void bc_lex_whitespace(BcLex *l)
2823{
2824 char c;
2825 l->t.t = BC_LEX_WHITESPACE;
2826 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
2827}
2828
Denys Vlasenko29301232018-12-11 15:29:32 +01002829static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002830{
2831 const char *buf = l->buf + l->i;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002832 size_t len, bslashes, i, ccnt;
2833 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002834
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002835 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002836 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002837 bslashes = 0;
2838 ccnt = i = 0;
2839 for (;;) {
2840 char c = buf[i];
2841 if (c == '\0')
2842 break;
2843 if (c == '\\' && buf[i + 1] == '\n') {
2844 i += 2;
2845 bslashes++;
2846 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002847 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002848 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2849 if (c != '.') break;
2850 // if '.' was already seen, stop on second one:
2851 if (pt) break;
2852 pt = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002853 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002854 // buf[i] is one of "0-9A-F."
2855 i++;
2856 if (c != '.')
2857 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002858 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002859 //i is buf[i] index of the first not-yet-parsed char
2860 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002861
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002862 //ccnt is the number of chars in the number string, excluding possible
2863 //trailing "." and possible following trailing "\<newline>"(s).
2864 len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination
2865
Denys Vlasenko64074a12018-12-07 15:50:14 +01002866 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2867 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2868 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002869 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002870 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002871
Denys Vlasenko7d628012018-12-04 21:46:47 +01002872 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002873 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002874 bc_vec_push(&l->t.v, &start);
2875
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002876 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002877 // If we have hit a backslash, skip it. We don't have
2878 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002879 if (*buf == '\\') {
2880 buf += 2;
2881 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002882 continue;
2883 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002884 bc_vec_push(&l->t.v, buf);
2885 buf++;
2886 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002887 }
2888
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002889 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002890
Denys Vlasenko29301232018-12-11 15:29:32 +01002891 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002892}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002893#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002894# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002895#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002896
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002897static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002898{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002899 size_t i;
2900 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002901
2902 l->t.t = BC_LEX_NAME;
2903
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002904 i = 0;
2905 buf = l->buf + l->i - 1;
2906 for (;;) {
2907 char c = buf[i];
2908 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2909 i++;
2910 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002911
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002912#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002913 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2914 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2915 if (i > BC_MAX_STRING)
2916 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2917 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002918#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002919 bc_vec_string(&l->t.v, i, buf);
2920
2921 // Increment the index. We minus 1 because it has already been incremented.
2922 l->i += i - 1;
2923
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002924 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002925}
2926
2927static void bc_lex_init(BcLex *l, BcLexNext next)
2928{
2929 l->next = next;
Denys Vlasenko7d628012018-12-04 21:46:47 +01002930 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002931}
2932
2933static void bc_lex_free(BcLex *l)
2934{
2935 bc_vec_free(&l->t.v);
2936}
2937
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002938static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002939{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002940 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002941 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002942}
2943
2944static BcStatus bc_lex_next(BcLex *l)
2945{
2946 BcStatus s;
2947
2948 l->t.last = l->t.t;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002949 if (l->t.last == BC_LEX_EOF) return bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06002950
2951 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002952 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06002953 l->t.t = BC_LEX_EOF;
2954
2955 l->newline = (l->i == l->len);
2956 if (l->newline) return BC_STATUS_SUCCESS;
2957
2958 // Loop until failure or we don't have whitespace. This
2959 // is so the parser doesn't get inundated with whitespace.
2960 do {
2961 s = l->next(l);
2962 } while (!s && l->t.t == BC_LEX_WHITESPACE);
2963
2964 return s;
2965}
2966
2967static BcStatus bc_lex_text(BcLex *l, const char *text)
2968{
2969 l->buf = text;
2970 l->i = 0;
2971 l->len = strlen(text);
2972 l->t.t = l->t.last = BC_LEX_INVALID;
2973 return bc_lex_next(l);
2974}
2975
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002976#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06002977static BcStatus bc_lex_identifier(BcLex *l)
2978{
2979 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002980 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002981 const char *buf = l->buf + l->i - 1;
2982
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002983 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2984 const char *keyword8 = bc_lex_kws[i].name8;
2985 unsigned j = 0;
2986 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2987 j++;
2988 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002989 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002990 if (keyword8[j] != '\0')
2991 continue;
2992 match:
2993 // buf starts with keyword bc_lex_kws[i]
2994 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01002995 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002996 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002997 if (s) return s;
2998 }
2999
3000 // We minus 1 because the index has already been incremented.
3001 l->i += j - 1;
3002 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06003003 }
3004
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003005 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003006
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003007 if (l->t.v.len > 2) {
3008 // Prevent this:
3009 // >>> qwe=1
3010 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3011 // '
3012 unsigned len = strchrnul(buf, '\n') - buf;
3013 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3014 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003015
3016 return s;
3017}
3018
3019static BcStatus bc_lex_string(BcLex *l)
3020{
3021 size_t len, nls = 0, i = l->i;
3022 char c;
3023
3024 l->t.t = BC_LEX_STR;
3025
3026 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n');
3027
3028 if (c == '\0') {
3029 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003030 return bc_error("string end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003031 }
3032
3033 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003034 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3035 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3036 if (len > BC_MAX_STRING)
3037 return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]");
3038 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003039 bc_vec_string(&l->t.v, len, l->buf + l->i);
3040
3041 l->i = i + 1;
3042 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003043 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003044
3045 return BC_STATUS_SUCCESS;
3046}
3047
3048static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3049{
3050 if (l->buf[l->i] == '=') {
3051 ++l->i;
3052 l->t.t = with;
3053 }
3054 else
3055 l->t.t = without;
3056}
3057
Denys Vlasenko29301232018-12-11 15:29:32 +01003058static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003059{
3060 size_t i, nls = 0;
3061 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003062
3063 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003064 i = ++l->i;
3065 for (;;) {
3066 char c = buf[i];
3067 check_star:
3068 if (c == '*') {
3069 c = buf[++i];
3070 if (c == '/')
3071 break;
3072 goto check_star;
3073 }
3074 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003075 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003076 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003077 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003078 nls += (c == '\n');
3079 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003080 }
3081
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003082 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003083 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003084 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003085
Denys Vlasenko29301232018-12-11 15:29:32 +01003086 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003087}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003088#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003089# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003090#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003091
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003092static FAST_FUNC BcStatus bc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003093{
3094 BcStatus s = BC_STATUS_SUCCESS;
3095 char c = l->buf[l->i++], c2;
3096
3097 // This is the workhorse of the lexer.
3098 switch (c) {
3099
3100 case '\0':
3101 case '\n':
3102 {
3103 l->newline = true;
3104 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3105 break;
3106 }
3107
3108 case '\t':
3109 case '\v':
3110 case '\f':
3111 case '\r':
3112 case ' ':
3113 {
3114 bc_lex_whitespace(l);
3115 break;
3116 }
3117
3118 case '!':
3119 {
3120 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3121
3122 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003123 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Gavin Howard01055ba2018-11-03 11:00:21 -06003124 if (s) return s;
3125 }
3126
3127 break;
3128 }
3129
3130 case '"':
3131 {
3132 s = bc_lex_string(l);
3133 break;
3134 }
3135
3136 case '#':
3137 {
Denys Vlasenko00646792018-12-05 18:12:27 +01003138 s = bc_POSIX_does_not_allow("'#' script comments");
Gavin Howard01055ba2018-11-03 11:00:21 -06003139 if (s) return s;
3140
3141 bc_lex_lineComment(l);
3142
3143 break;
3144 }
3145
3146 case '%':
3147 {
3148 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3149 break;
3150 }
3151
3152 case '&':
3153 {
3154 c2 = l->buf[l->i];
3155 if (c2 == '&') {
3156
Denys Vlasenko00646792018-12-05 18:12:27 +01003157 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Gavin Howard01055ba2018-11-03 11:00:21 -06003158 if (s) return s;
3159
3160 ++l->i;
3161 l->t.t = BC_LEX_OP_BOOL_AND;
3162 }
3163 else {
3164 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003165 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 }
3167
3168 break;
3169 }
3170
3171 case '(':
3172 case ')':
3173 {
3174 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3175 break;
3176 }
3177
3178 case '*':
3179 {
3180 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3181 break;
3182 }
3183
3184 case '+':
3185 {
3186 c2 = l->buf[l->i];
3187 if (c2 == '+') {
3188 ++l->i;
3189 l->t.t = BC_LEX_OP_INC;
3190 }
3191 else
3192 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3193 break;
3194 }
3195
3196 case ',':
3197 {
3198 l->t.t = BC_LEX_COMMA;
3199 break;
3200 }
3201
3202 case '-':
3203 {
3204 c2 = l->buf[l->i];
3205 if (c2 == '-') {
3206 ++l->i;
3207 l->t.t = BC_LEX_OP_DEC;
3208 }
3209 else
3210 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3211 break;
3212 }
3213
3214 case '.':
3215 {
3216 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003217 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003218 else {
3219 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003220 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003221 }
3222 break;
3223 }
3224
3225 case '/':
3226 {
3227 c2 = l->buf[l->i];
3228 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003229 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003230 else
3231 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3232 break;
3233 }
3234
3235 case '0':
3236 case '1':
3237 case '2':
3238 case '3':
3239 case '4':
3240 case '5':
3241 case '6':
3242 case '7':
3243 case '8':
3244 case '9':
3245 case 'A':
3246 case 'B':
3247 case 'C':
3248 case 'D':
3249 case 'E':
3250 case 'F':
3251 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003252 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003253 break;
3254 }
3255
3256 case ';':
3257 {
3258 l->t.t = BC_LEX_SCOLON;
3259 break;
3260 }
3261
3262 case '<':
3263 {
3264 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3265 break;
3266 }
3267
3268 case '=':
3269 {
3270 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3271 break;
3272 }
3273
3274 case '>':
3275 {
3276 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3277 break;
3278 }
3279
3280 case '[':
3281 case ']':
3282 {
3283 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3284 break;
3285 }
3286
3287 case '\\':
3288 {
3289 if (l->buf[l->i] == '\n') {
3290 l->t.t = BC_LEX_WHITESPACE;
3291 ++l->i;
3292 }
3293 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003294 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003295 break;
3296 }
3297
3298 case '^':
3299 {
3300 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3301 break;
3302 }
3303
3304 case 'a':
3305 case 'b':
3306 case 'c':
3307 case 'd':
3308 case 'e':
3309 case 'f':
3310 case 'g':
3311 case 'h':
3312 case 'i':
3313 case 'j':
3314 case 'k':
3315 case 'l':
3316 case 'm':
3317 case 'n':
3318 case 'o':
3319 case 'p':
3320 case 'q':
3321 case 'r':
3322 case 's':
3323 case 't':
3324 case 'u':
3325 case 'v':
3326 case 'w':
3327 case 'x':
3328 case 'y':
3329 case 'z':
3330 {
3331 s = bc_lex_identifier(l);
3332 break;
3333 }
3334
3335 case '{':
3336 case '}':
3337 {
3338 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3339 break;
3340 }
3341
3342 case '|':
3343 {
3344 c2 = l->buf[l->i];
3345
3346 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003347 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Gavin Howard01055ba2018-11-03 11:00:21 -06003348 if (s) return s;
3349
3350 ++l->i;
3351 l->t.t = BC_LEX_OP_BOOL_OR;
3352 }
3353 else {
3354 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003355 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003356 }
3357
3358 break;
3359 }
3360
3361 default:
3362 {
3363 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003364 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003365 break;
3366 }
3367 }
3368
3369 return s;
3370}
3371#endif // ENABLE_BC
3372
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003373#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003374static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003375{
Gavin Howard01055ba2018-11-03 11:00:21 -06003376 if (isspace(l->buf[l->i - 1])) {
3377 bc_lex_whitespace(l);
3378 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003379 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003380 RETURN_STATUS(bc_error("extended register"));
3381 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003382 }
3383 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003384 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003385 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003386 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003387 l->t.t = BC_LEX_NAME;
3388 }
3389
Denys Vlasenko29301232018-12-11 15:29:32 +01003390 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003391}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003392#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003393# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003394#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003395
Denys Vlasenko29301232018-12-11 15:29:32 +01003396static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003397{
3398 size_t depth = 1, nls = 0, i = l->i;
3399 char c;
3400
3401 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003402 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003403
3404 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3405
3406 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3407 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3408 nls += (c == '\n');
3409
3410 if (depth) bc_vec_push(&l->t.v, &c);
3411 }
3412
3413 if (c == '\0') {
3414 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003415 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003416 }
3417
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003418 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003419 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3420 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3421 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003422 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003423 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003424
3425 l->i = i;
3426 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003427 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003428
Denys Vlasenko29301232018-12-11 15:29:32 +01003429 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003430}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003431#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003432# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003433#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003434
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003435static FAST_FUNC BcStatus dc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003436{
3437 BcStatus s = BC_STATUS_SUCCESS;
3438 char c = l->buf[l->i++], c2;
3439 size_t i;
3440
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003441 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3442 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko29301232018-12-11 15:29:32 +01003443 return zdc_lex_register(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003444 }
3445
3446 if (c >= '%' && c <= '~' &&
3447 (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID)
3448 {
3449 return s;
3450 }
3451
3452 // This is the workhorse of the lexer.
3453 switch (c) {
3454
3455 case '\0':
3456 {
3457 l->t.t = BC_LEX_EOF;
3458 break;
3459 }
3460
3461 case '\n':
3462 case '\t':
3463 case '\v':
3464 case '\f':
3465 case '\r':
3466 case ' ':
3467 {
3468 l->newline = (c == '\n');
3469 bc_lex_whitespace(l);
3470 break;
3471 }
3472
3473 case '!':
3474 {
3475 c2 = l->buf[l->i];
3476
3477 if (c2 == '=')
3478 l->t.t = BC_LEX_OP_REL_NE;
3479 else if (c2 == '<')
3480 l->t.t = BC_LEX_OP_REL_LE;
3481 else if (c2 == '>')
3482 l->t.t = BC_LEX_OP_REL_GE;
3483 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003484 return bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003485
3486 ++l->i;
3487 break;
3488 }
3489
3490 case '#':
3491 {
3492 bc_lex_lineComment(l);
3493 break;
3494 }
3495
3496 case '.':
3497 {
3498 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003499 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003500 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003501 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003502 break;
3503 }
3504
3505 case '0':
3506 case '1':
3507 case '2':
3508 case '3':
3509 case '4':
3510 case '5':
3511 case '6':
3512 case '7':
3513 case '8':
3514 case '9':
3515 case 'A':
3516 case 'B':
3517 case 'C':
3518 case 'D':
3519 case 'E':
3520 case 'F':
3521 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003522 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003523 break;
3524 }
3525
3526 case '[':
3527 {
Denys Vlasenko29301232018-12-11 15:29:32 +01003528 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003529 break;
3530 }
3531
3532 default:
3533 {
3534 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003535 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003536 break;
3537 }
3538 }
3539
3540 return s;
3541}
3542#endif // ENABLE_DC
3543
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003544static void bc_program_addFunc(char *name, size_t *idx);
3545
Gavin Howard01055ba2018-11-03 11:00:21 -06003546static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3547{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003548 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003549 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003550}
3551
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003552#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3553
Gavin Howard01055ba2018-11-03 11:00:21 -06003554static void bc_parse_pushName(BcParse *p, char *name)
3555{
3556 size_t i = 0, len = strlen(name);
3557
3558 for (; i < len; ++i) bc_parse_push(p, name[i]);
3559 bc_parse_push(p, BC_PARSE_STREND);
3560
3561 free(name);
3562}
3563
3564static void bc_parse_pushIndex(BcParse *p, size_t idx)
3565{
3566 unsigned char amt, i, nums[sizeof(size_t)];
3567
3568 for (amt = 0; idx; ++amt) {
3569 nums[amt] = (char) idx;
3570 idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT;
3571 }
3572
3573 bc_parse_push(p, amt);
3574 for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]);
3575}
3576
3577static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3578{
3579 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003580 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003581
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003582 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003583
3584 bc_parse_push(p, BC_INST_NUM);
3585 bc_parse_pushIndex(p, idx);
3586
3587 ++(*nexs);
3588 (*prev) = BC_INST_NUM;
3589}
3590
3591static BcStatus bc_parse_text(BcParse *p, const char *text)
3592{
3593 BcStatus s;
3594
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003595 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003596
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003597 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003598 p->l.t.t = BC_LEX_INVALID;
3599 s = p->parse(p);
3600 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003601 if (!BC_PARSE_CAN_EXEC(p))
3602 return bc_error("file is not executable");
Gavin Howard01055ba2018-11-03 11:00:21 -06003603 }
3604
3605 return bc_lex_text(&p->l, text);
3606}
3607
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003608// Called when parsing or execution detects a failure,
3609// resets execution structures.
3610static void bc_program_reset(void)
3611{
3612 BcFunc *f;
3613 BcInstPtr *ip;
3614
3615 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3616 bc_vec_pop_all(&G.prog.results);
3617
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003618 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003619 ip = bc_vec_top(&G.prog.stack);
3620 ip->idx = f->code.len;
3621}
3622
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003623#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003624 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003625
Denys Vlasenkod38af482018-12-04 19:11:02 +01003626// Called when bc/dc_parse_parse() detects a failure,
3627// resets parsing structures.
3628static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003629{
3630 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003631 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003632 bc_vec_pop_all(&p->func->code);
3633 bc_vec_pop_all(&p->func->autos);
3634 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003635
3636 bc_parse_updateFunc(p, BC_PROG_MAIN);
3637 }
3638
3639 p->l.i = p->l.len;
3640 p->l.t.t = BC_LEX_EOF;
3641 p->auto_part = (p->nbraces = 0);
3642
3643 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003644 bc_vec_pop_all(&p->exits);
3645 bc_vec_pop_all(&p->conds);
3646 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003647
Denys Vlasenkod38af482018-12-04 19:11:02 +01003648 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003649}
3650
3651static void bc_parse_free(BcParse *p)
3652{
3653 bc_vec_free(&p->flags);
3654 bc_vec_free(&p->exits);
3655 bc_vec_free(&p->conds);
3656 bc_vec_free(&p->ops);
3657 bc_lex_free(&p->l);
3658}
3659
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003660static void bc_parse_create(BcParse *p, size_t func,
Gavin Howard01055ba2018-11-03 11:00:21 -06003661 BcParseParse parse, BcLexNext next)
3662{
3663 memset(p, 0, sizeof(BcParse));
3664
3665 bc_lex_init(&p->l, next);
3666 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3667 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3668 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003669 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003670 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3671
3672 p->parse = parse;
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003673 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003674 bc_parse_updateFunc(p, func);
3675}
3676
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003677#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003678
3679#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3680#define BC_PARSE_LEAF(p, rparen) \
3681 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3682 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3683
3684// We can calculate the conversion between tokens and exprs by subtracting the
3685// position of the first operator in the lex enum and adding the position of the
3686// first in the expr enum. Note: This only works for binary operators.
3687#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3688
Gavin Howard01055ba2018-11-03 11:00:21 -06003689static BcStatus bc_parse_else(BcParse *p);
3690static BcStatus bc_parse_stmt(BcParse *p);
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003691static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003692static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Gavin Howard01055ba2018-11-03 11:00:21 -06003693
3694static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3695 size_t *nexprs, bool next)
3696{
3697 BcStatus s = BC_STATUS_SUCCESS;
3698 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003699 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3700 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003701
3702 while (p->ops.len > start) {
3703
3704 t = BC_PARSE_TOP_OP(p);
3705 if (t == BC_LEX_LPAREN) break;
3706
Denys Vlasenko65437582018-12-05 19:37:19 +01003707 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003708 if (l >= r && (l != r || !left)) break;
3709
3710 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3711 bc_vec_pop(&p->ops);
3712 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3713 }
3714
3715 bc_vec_push(&p->ops, &type);
3716 if (next) s = bc_lex_next(&p->l);
3717
3718 return s;
3719}
3720
3721static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
3722{
3723 BcLexType top;
3724
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003725 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003726 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003727 top = BC_PARSE_TOP_OP(p);
3728
3729 while (top != BC_LEX_LPAREN) {
3730
3731 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3732
3733 bc_vec_pop(&p->ops);
3734 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3735
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003736 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003737 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003738 top = BC_PARSE_TOP_OP(p);
3739 }
3740
3741 bc_vec_pop(&p->ops);
3742
3743 return bc_lex_next(&p->l);
3744}
3745
3746static BcStatus bc_parse_params(BcParse *p, uint8_t flags)
3747{
3748 BcStatus s;
3749 bool comma = false;
3750 size_t nparams;
3751
3752 s = bc_lex_next(&p->l);
3753 if (s) return s;
3754
3755 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
3756
3757 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3758 s = bc_parse_expr(p, flags, bc_parse_next_param);
3759 if (s) return s;
3760
3761 comma = p->l.t.t == BC_LEX_COMMA;
3762 if (comma) {
3763 s = bc_lex_next(&p->l);
3764 if (s) return s;
3765 }
3766 }
3767
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003768 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003769 bc_parse_push(p, BC_INST_CALL);
3770 bc_parse_pushIndex(p, nparams);
3771
3772 return BC_STATUS_SUCCESS;
3773}
3774
3775static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags)
3776{
3777 BcStatus s;
3778 BcId entry, *entry_ptr;
3779 size_t idx;
3780
3781 entry.name = name;
3782
3783 s = bc_parse_params(p, flags);
3784 if (s) goto err;
3785
3786 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003787 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003788 goto err;
3789 }
3790
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003791 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003792
3793 if (idx == BC_VEC_INVALID_IDX) {
3794 name = xstrdup(entry.name);
3795 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003796 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003797 free(entry.name);
3798 }
3799 else
3800 free(name);
3801
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003802 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003803 bc_parse_pushIndex(p, entry_ptr->idx);
3804
3805 return bc_lex_next(&p->l);
3806
3807err:
3808 free(name);
3809 return s;
3810}
3811
3812static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
3813{
3814 BcStatus s;
3815 char *name;
3816
3817 name = xstrdup(p->l.t.v.v);
3818 s = bc_lex_next(&p->l);
3819 if (s) goto err;
3820
3821 if (p->l.t.t == BC_LEX_LBRACKET) {
3822
3823 s = bc_lex_next(&p->l);
3824 if (s) goto err;
3825
3826 if (p->l.t.t == BC_LEX_RBRACKET) {
3827
3828 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003829 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003830 goto err;
3831 }
3832
3833 *type = BC_INST_ARRAY;
3834 }
3835 else {
3836
3837 *type = BC_INST_ARRAY_ELEM;
3838
3839 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3840 s = bc_parse_expr(p, flags, bc_parse_next_elem);
3841 if (s) goto err;
3842 }
3843
3844 s = bc_lex_next(&p->l);
3845 if (s) goto err;
3846 bc_parse_push(p, *type);
3847 bc_parse_pushName(p, name);
3848 }
3849 else if (p->l.t.t == BC_LEX_LPAREN) {
3850
3851 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003852 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003853 goto err;
3854 }
3855
3856 *type = BC_INST_CALL;
3857 s = bc_parse_call(p, name, flags);
3858 }
3859 else {
3860 *type = BC_INST_VAR;
3861 bc_parse_push(p, BC_INST_VAR);
3862 bc_parse_pushName(p, name);
3863 }
3864
3865 return s;
3866
3867err:
3868 free(name);
3869 return s;
3870}
3871
3872static BcStatus bc_parse_read(BcParse *p)
3873{
3874 BcStatus s;
3875
3876 s = bc_lex_next(&p->l);
3877 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003878 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003879
3880 s = bc_lex_next(&p->l);
3881 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003882 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003883
3884 bc_parse_push(p, BC_INST_READ);
3885
3886 return bc_lex_next(&p->l);
3887}
3888
3889static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
3890 BcInst *prev)
3891{
3892 BcStatus s;
3893
3894 s = bc_lex_next(&p->l);
3895 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003896 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003897
3898 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3899
3900 s = bc_lex_next(&p->l);
3901 if (s) return s;
3902
3903 s = bc_parse_expr(p, flags, bc_parse_next_rel);
3904 if (s) return s;
3905
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003906 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003907
3908 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3909 bc_parse_push(p, *prev);
3910
3911 return bc_lex_next(&p->l);
3912}
3913
3914static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
3915{
3916 BcStatus s;
3917
3918 s = bc_lex_next(&p->l);
3919 if (s) return s;
3920
3921 if (p->l.t.t != BC_LEX_LPAREN) {
3922 *type = BC_INST_SCALE;
3923 bc_parse_push(p, BC_INST_SCALE);
3924 return BC_STATUS_SUCCESS;
3925 }
3926
3927 *type = BC_INST_SCALE_FUNC;
3928 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3929
3930 s = bc_lex_next(&p->l);
3931 if (s) return s;
3932
3933 s = bc_parse_expr(p, flags, bc_parse_next_rel);
3934 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003935 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003936 bc_parse_push(p, BC_INST_SCALE_FUNC);
3937
3938 return bc_lex_next(&p->l);
3939}
3940
3941static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
3942 size_t *nexprs, uint8_t flags)
3943{
3944 BcStatus s;
3945 BcLexType type;
3946 char inst;
3947 BcInst etype = *prev;
3948
3949 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
3950 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
3951 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
3952 {
3953 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3954 bc_parse_push(p, inst);
3955 s = bc_lex_next(&p->l);
3956 }
3957 else {
3958
3959 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3960 *paren_expr = true;
3961
3962 s = bc_lex_next(&p->l);
3963 if (s) return s;
3964 type = p->l.t.t;
3965
3966 // Because we parse the next part of the expression
3967 // right here, we need to increment this.
3968 *nexprs = *nexprs + 1;
3969
3970 switch (type) {
3971
3972 case BC_LEX_NAME:
3973 {
3974 s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
3975 break;
3976 }
3977
3978 case BC_LEX_KEY_IBASE:
3979 case BC_LEX_KEY_LAST:
3980 case BC_LEX_KEY_OBASE:
3981 {
3982 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
3983 s = bc_lex_next(&p->l);
3984 break;
3985 }
3986
3987 case BC_LEX_KEY_SCALE:
3988 {
3989 s = bc_lex_next(&p->l);
3990 if (s) return s;
3991 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003992 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003993 else
3994 bc_parse_push(p, BC_INST_SCALE);
3995 break;
3996 }
3997
3998 default:
3999 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004000 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004001 break;
4002 }
4003 }
4004
4005 if (!s) bc_parse_push(p, inst);
4006 }
4007
4008 return s;
4009}
4010
4011static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
4012 bool rparen, size_t *nexprs)
4013{
4014 BcStatus s;
4015 BcLexType type;
4016 BcInst etype = *prev;
4017
4018 s = bc_lex_next(&p->l);
4019 if (s) return s;
4020
4021 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
4022 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
4023 BC_LEX_OP_MINUS :
4024 BC_LEX_NEG;
4025 *prev = BC_PARSE_TOKEN_INST(type);
4026
4027 // We can just push onto the op stack because this is the largest
4028 // precedence operator that gets pushed. Inc/dec does not.
4029 if (type != BC_LEX_OP_MINUS)
4030 bc_vec_push(&p->ops, &type);
4031 else
4032 s = bc_parse_operator(p, type, ops_bgn, nexprs, false);
4033
4034 return s;
4035}
4036
4037static BcStatus bc_parse_string(BcParse *p, char inst)
4038{
4039 char *str = xstrdup(p->l.t.v.v);
4040
4041 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004042 bc_parse_pushIndex(p, G.prog.strs.len);
4043 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004044 bc_parse_push(p, inst);
4045
4046 return bc_lex_next(&p->l);
4047}
4048
4049static BcStatus bc_parse_print(BcParse *p)
4050{
4051 BcStatus s;
4052 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004053 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06004054
4055 s = bc_lex_next(&p->l);
4056 if (s) return s;
4057
4058 type = p->l.t.t;
4059
4060 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004061 return bc_error("bad print statement");
Gavin Howard01055ba2018-11-03 11:00:21 -06004062
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004063 comma = false;
4064 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004065
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004066 if (type == BC_LEX_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004067 s = bc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004068 if (s) return s;
4069 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004070 s = bc_parse_expr(p, 0, bc_parse_next_print);
4071 if (s) return s;
4072 bc_parse_push(p, BC_INST_PRINT_POP);
4073 }
4074
Gavin Howard01055ba2018-11-03 11:00:21 -06004075 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01004076 if (comma) {
4077 s = bc_lex_next(&p->l);
4078 if (s) return s;
4079 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004080 type = p->l.t.t;
4081 }
4082
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004083 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004084
4085 return bc_lex_next(&p->l);
4086}
4087
4088static BcStatus bc_parse_return(BcParse *p)
4089{
4090 BcStatus s;
4091 BcLexType t;
4092 bool paren;
4093
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004094 if (!BC_PARSE_FUNC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004095
4096 s = bc_lex_next(&p->l);
4097 if (s) return s;
4098
4099 t = p->l.t.t;
4100 paren = t == BC_LEX_LPAREN;
4101
4102 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4103 bc_parse_push(p, BC_INST_RET0);
4104 else {
4105
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004106 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004107 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004108 bc_parse_push(p, BC_INST_RET0);
4109 s = bc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004110 }
Denys Vlasenko452df922018-12-05 20:28:26 +01004111 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004112
4113 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004114 s = bc_POSIX_requires("parentheses around return expressions");
Gavin Howard01055ba2018-11-03 11:00:21 -06004115 if (s) return s;
4116 }
4117
4118 bc_parse_push(p, BC_INST_RET);
4119 }
4120
4121 return s;
4122}
4123
4124static BcStatus bc_parse_endBody(BcParse *p, bool brace)
4125{
4126 BcStatus s = BC_STATUS_SUCCESS;
4127
4128 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004129 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004130
4131 if (brace) {
4132
4133 if (p->l.t.t == BC_LEX_RBRACE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004134 if (!p->nbraces) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004135 --p->nbraces;
4136 s = bc_lex_next(&p->l);
4137 if (s) return s;
4138 }
4139 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004140 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004141 }
4142
4143 if (BC_PARSE_IF(p)) {
4144
4145 uint8_t *flag_ptr;
4146
4147 while (p->l.t.t == BC_LEX_NLINE) {
4148 s = bc_lex_next(&p->l);
4149 if (s) return s;
4150 }
4151
4152 bc_vec_pop(&p->flags);
4153
4154 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4155 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4156
4157 if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p);
4158 }
4159 else if (BC_PARSE_ELSE(p)) {
4160
4161 BcInstPtr *ip;
4162 size_t *label;
4163
4164 bc_vec_pop(&p->flags);
4165
4166 ip = bc_vec_top(&p->exits);
4167 label = bc_vec_item(&p->func->labels, ip->idx);
4168 *label = p->func->code.len;
4169
4170 bc_vec_pop(&p->exits);
4171 }
4172 else if (BC_PARSE_FUNC_INNER(p)) {
4173 bc_parse_push(p, BC_INST_RET0);
4174 bc_parse_updateFunc(p, BC_PROG_MAIN);
4175 bc_vec_pop(&p->flags);
4176 }
4177 else {
4178
4179 BcInstPtr *ip = bc_vec_top(&p->exits);
4180 size_t *label = bc_vec_top(&p->conds);
4181
4182 bc_parse_push(p, BC_INST_JUMP);
4183 bc_parse_pushIndex(p, *label);
4184
4185 label = bc_vec_item(&p->func->labels, ip->idx);
4186 *label = p->func->code.len;
4187
4188 bc_vec_pop(&p->flags);
4189 bc_vec_pop(&p->exits);
4190 bc_vec_pop(&p->conds);
4191 }
4192
4193 return s;
4194}
4195
4196static void bc_parse_startBody(BcParse *p, uint8_t flags)
4197{
4198 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4199 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4200 flags |= BC_PARSE_FLAG_BODY;
4201 bc_vec_push(&p->flags, &flags);
4202}
4203
4204static void bc_parse_noElse(BcParse *p)
4205{
4206 BcInstPtr *ip;
4207 size_t *label;
4208 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4209
4210 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4211
4212 ip = bc_vec_top(&p->exits);
4213 label = bc_vec_item(&p->func->labels, ip->idx);
4214 *label = p->func->code.len;
4215
4216 bc_vec_pop(&p->exits);
4217}
4218
4219static BcStatus bc_parse_if(BcParse *p)
4220{
4221 BcStatus s;
4222 BcInstPtr ip;
4223
4224 s = bc_lex_next(&p->l);
4225 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004226 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004227
4228 s = bc_lex_next(&p->l);
4229 if (s) return s;
4230 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4231 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004232 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004233
4234 s = bc_lex_next(&p->l);
4235 if (s) return s;
4236 bc_parse_push(p, BC_INST_JUMP_ZERO);
4237
4238 ip.idx = p->func->labels.len;
4239 ip.func = ip.len = 0;
4240
4241 bc_parse_pushIndex(p, ip.idx);
4242 bc_vec_push(&p->exits, &ip);
4243 bc_vec_push(&p->func->labels, &ip.idx);
4244 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4245
4246 return BC_STATUS_SUCCESS;
4247}
4248
4249static BcStatus bc_parse_else(BcParse *p)
4250{
4251 BcInstPtr ip;
4252
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004253 if (!BC_PARSE_IF_END(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004254
4255 ip.idx = p->func->labels.len;
4256 ip.func = ip.len = 0;
4257
4258 bc_parse_push(p, BC_INST_JUMP);
4259 bc_parse_pushIndex(p, ip.idx);
4260
4261 bc_parse_noElse(p);
4262
4263 bc_vec_push(&p->exits, &ip);
4264 bc_vec_push(&p->func->labels, &ip.idx);
4265 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4266
4267 return bc_lex_next(&p->l);
4268}
4269
4270static BcStatus bc_parse_while(BcParse *p)
4271{
4272 BcStatus s;
4273 BcInstPtr ip;
4274
4275 s = bc_lex_next(&p->l);
4276 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004277 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004278 s = bc_lex_next(&p->l);
4279 if (s) return s;
4280
4281 ip.idx = p->func->labels.len;
4282
4283 bc_vec_push(&p->func->labels, &p->func->code.len);
4284 bc_vec_push(&p->conds, &ip.idx);
4285
4286 ip.idx = p->func->labels.len;
4287 ip.func = 1;
4288 ip.len = 0;
4289
4290 bc_vec_push(&p->exits, &ip);
4291 bc_vec_push(&p->func->labels, &ip.idx);
4292
4293 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4294 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004295 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004296 s = bc_lex_next(&p->l);
4297 if (s) return s;
4298
4299 bc_parse_push(p, BC_INST_JUMP_ZERO);
4300 bc_parse_pushIndex(p, ip.idx);
4301 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4302
4303 return BC_STATUS_SUCCESS;
4304}
4305
4306static BcStatus bc_parse_for(BcParse *p)
4307{
4308 BcStatus s;
4309 BcInstPtr ip;
4310 size_t cond_idx, exit_idx, body_idx, update_idx;
4311
4312 s = bc_lex_next(&p->l);
4313 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004314 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004315 s = bc_lex_next(&p->l);
4316 if (s) return s;
4317
4318 if (p->l.t.t != BC_LEX_SCOLON)
4319 s = bc_parse_expr(p, 0, bc_parse_next_for);
4320 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004321 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004322
4323 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004324 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004325 s = bc_lex_next(&p->l);
4326 if (s) return s;
4327
4328 cond_idx = p->func->labels.len;
4329 update_idx = cond_idx + 1;
4330 body_idx = update_idx + 1;
4331 exit_idx = body_idx + 1;
4332
4333 bc_vec_push(&p->func->labels, &p->func->code.len);
4334
4335 if (p->l.t.t != BC_LEX_SCOLON)
4336 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
4337 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004338 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004339
4340 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004341 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004342
4343 s = bc_lex_next(&p->l);
4344 if (s) return s;
4345
4346 bc_parse_push(p, BC_INST_JUMP_ZERO);
4347 bc_parse_pushIndex(p, exit_idx);
4348 bc_parse_push(p, BC_INST_JUMP);
4349 bc_parse_pushIndex(p, body_idx);
4350
4351 ip.idx = p->func->labels.len;
4352
4353 bc_vec_push(&p->conds, &update_idx);
4354 bc_vec_push(&p->func->labels, &p->func->code.len);
4355
4356 if (p->l.t.t != BC_LEX_RPAREN)
4357 s = bc_parse_expr(p, 0, bc_parse_next_rel);
4358 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004359 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004360
4361 if (s) return s;
4362
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004363 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004364 bc_parse_push(p, BC_INST_JUMP);
4365 bc_parse_pushIndex(p, cond_idx);
4366 bc_vec_push(&p->func->labels, &p->func->code.len);
4367
4368 ip.idx = exit_idx;
4369 ip.func = 1;
4370 ip.len = 0;
4371
4372 bc_vec_push(&p->exits, &ip);
4373 bc_vec_push(&p->func->labels, &ip.idx);
4374 bc_lex_next(&p->l);
4375 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4376
4377 return BC_STATUS_SUCCESS;
4378}
4379
4380static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type)
4381{
4382 BcStatus s;
4383 size_t i;
4384 BcInstPtr *ip;
4385
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004386 if (!BC_PARSE_LOOP(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004387
4388 if (type == BC_LEX_KEY_BREAK) {
4389
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004390 if (p->exits.len == 0) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004391
4392 i = p->exits.len - 1;
4393 ip = bc_vec_item(&p->exits, i);
4394
4395 while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004396 if (i >= p->exits.len && !ip->func) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004397
4398 i = ip->idx;
4399 }
4400 else
4401 i = *((size_t *) bc_vec_top(&p->conds));
4402
4403 bc_parse_push(p, BC_INST_JUMP);
4404 bc_parse_pushIndex(p, i);
4405
4406 s = bc_lex_next(&p->l);
4407 if (s) return s;
4408
4409 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004410 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004411
4412 return bc_lex_next(&p->l);
4413}
4414
4415static BcStatus bc_parse_func(BcParse *p)
4416{
4417 BcStatus s;
4418 bool var, comma = false;
4419 uint8_t flags;
4420 char *name;
4421
4422 s = bc_lex_next(&p->l);
4423 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004424 if (p->l.t.t != BC_LEX_NAME)
4425 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004426
4427 name = xstrdup(p->l.t.v.v);
4428 bc_parse_addFunc(p, name, &p->fidx);
4429
4430 s = bc_lex_next(&p->l);
4431 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004432 if (p->l.t.t != BC_LEX_LPAREN)
4433 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004434 s = bc_lex_next(&p->l);
4435 if (s) return s;
4436
4437 while (p->l.t.t != BC_LEX_RPAREN) {
4438
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004439 if (p->l.t.t != BC_LEX_NAME)
4440 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004441
4442 ++p->func->nparams;
4443
4444 name = xstrdup(p->l.t.v.v);
4445 s = bc_lex_next(&p->l);
4446 if (s) goto err;
4447
4448 var = p->l.t.t != BC_LEX_LBRACKET;
4449
4450 if (!var) {
4451
4452 s = bc_lex_next(&p->l);
4453 if (s) goto err;
4454
4455 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004456 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004457 goto err;
4458 }
4459
4460 s = bc_lex_next(&p->l);
4461 if (s) goto err;
4462 }
4463
4464 comma = p->l.t.t == BC_LEX_COMMA;
4465 if (comma) {
4466 s = bc_lex_next(&p->l);
4467 if (s) goto err;
4468 }
4469
Denys Vlasenko29301232018-12-11 15:29:32 +01004470 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004471 if (s) goto err;
4472 }
4473
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004474 if (comma) return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004475
4476 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4477 bc_parse_startBody(p, flags);
4478
4479 s = bc_lex_next(&p->l);
4480 if (s) return s;
4481
4482 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004483 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004484
4485 return s;
4486
4487err:
4488 free(name);
4489 return s;
4490}
4491
4492static BcStatus bc_parse_auto(BcParse *p)
4493{
4494 BcStatus s;
4495 bool comma, var, one;
4496 char *name;
4497
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004498 if (!p->auto_part) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004499 s = bc_lex_next(&p->l);
4500 if (s) return s;
4501
4502 p->auto_part = comma = false;
4503 one = p->l.t.t == BC_LEX_NAME;
4504
4505 while (p->l.t.t == BC_LEX_NAME) {
4506
4507 name = xstrdup(p->l.t.v.v);
4508 s = bc_lex_next(&p->l);
4509 if (s) goto err;
4510
4511 var = p->l.t.t != BC_LEX_LBRACKET;
4512 if (!var) {
4513
4514 s = bc_lex_next(&p->l);
4515 if (s) goto err;
4516
4517 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004518 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004519 goto err;
4520 }
4521
4522 s = bc_lex_next(&p->l);
4523 if (s) goto err;
4524 }
4525
4526 comma = p->l.t.t == BC_LEX_COMMA;
4527 if (comma) {
4528 s = bc_lex_next(&p->l);
4529 if (s) goto err;
4530 }
4531
Denys Vlasenko29301232018-12-11 15:29:32 +01004532 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004533 if (s) goto err;
4534 }
4535
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004536 if (comma) return bc_error("bad function definition");
Denys Vlasenkoabbc4332018-12-03 21:46:41 +01004537 if (!one) return bc_error("no auto variable found");
Gavin Howard01055ba2018-11-03 11:00:21 -06004538
4539 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004540 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004541
4542 return bc_lex_next(&p->l);
4543
4544err:
4545 free(name);
4546 return s;
4547}
4548
4549static BcStatus bc_parse_body(BcParse *p, bool brace)
4550{
4551 BcStatus s = BC_STATUS_SUCCESS;
4552 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4553
4554 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4555
4556 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
4557
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004558 if (!brace) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004559 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4560
4561 if (!p->auto_part) {
4562 s = bc_parse_auto(p);
4563 if (s) return s;
4564 }
4565
4566 if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l);
4567 }
4568 else {
4569 s = bc_parse_stmt(p);
4570 if (!s && !brace) s = bc_parse_endBody(p, false);
4571 }
4572
4573 return s;
4574}
4575
4576static BcStatus bc_parse_stmt(BcParse *p)
4577{
4578 BcStatus s = BC_STATUS_SUCCESS;
4579
4580 switch (p->l.t.t) {
4581
4582 case BC_LEX_NLINE:
4583 {
4584 return bc_lex_next(&p->l);
4585 }
4586
4587 case BC_LEX_KEY_ELSE:
4588 {
4589 p->auto_part = false;
4590 break;
4591 }
4592
4593 case BC_LEX_LBRACE:
4594 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004595 if (!BC_PARSE_BODY(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004596
4597 ++p->nbraces;
4598 s = bc_lex_next(&p->l);
4599 if (s) return s;
4600
4601 return bc_parse_body(p, true);
4602 }
4603
4604 case BC_LEX_KEY_AUTO:
4605 {
4606 return bc_parse_auto(p);
4607 }
4608
4609 default:
4610 {
4611 p->auto_part = false;
4612
4613 if (BC_PARSE_IF_END(p)) {
4614 bc_parse_noElse(p);
4615 return BC_STATUS_SUCCESS;
4616 }
4617 else if (BC_PARSE_BODY(p))
4618 return bc_parse_body(p, false);
4619
4620 break;
4621 }
4622 }
4623
4624 switch (p->l.t.t) {
4625
4626 case BC_LEX_OP_INC:
4627 case BC_LEX_OP_DEC:
4628 case BC_LEX_OP_MINUS:
4629 case BC_LEX_OP_BOOL_NOT:
4630 case BC_LEX_LPAREN:
4631 case BC_LEX_NAME:
4632 case BC_LEX_NUMBER:
4633 case BC_LEX_KEY_IBASE:
4634 case BC_LEX_KEY_LAST:
4635 case BC_LEX_KEY_LENGTH:
4636 case BC_LEX_KEY_OBASE:
4637 case BC_LEX_KEY_READ:
4638 case BC_LEX_KEY_SCALE:
4639 case BC_LEX_KEY_SQRT:
4640 {
4641 s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
4642 break;
4643 }
4644
4645 case BC_LEX_KEY_ELSE:
4646 {
4647 s = bc_parse_else(p);
4648 break;
4649 }
4650
4651 case BC_LEX_SCOLON:
4652 {
4653 while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l);
4654 break;
4655 }
4656
4657 case BC_LEX_RBRACE:
4658 {
4659 s = bc_parse_endBody(p, true);
4660 break;
4661 }
4662
4663 case BC_LEX_STR:
4664 {
4665 s = bc_parse_string(p, BC_INST_PRINT_STR);
4666 break;
4667 }
4668
4669 case BC_LEX_KEY_BREAK:
4670 case BC_LEX_KEY_CONTINUE:
4671 {
4672 s = bc_parse_loopExit(p, p->l.t.t);
4673 break;
4674 }
4675
4676 case BC_LEX_KEY_FOR:
4677 {
4678 s = bc_parse_for(p);
4679 break;
4680 }
4681
4682 case BC_LEX_KEY_HALT:
4683 {
4684 bc_parse_push(p, BC_INST_HALT);
4685 s = bc_lex_next(&p->l);
4686 break;
4687 }
4688
4689 case BC_LEX_KEY_IF:
4690 {
4691 s = bc_parse_if(p);
4692 break;
4693 }
4694
4695 case BC_LEX_KEY_LIMITS:
4696 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004697 // "limits" is a compile-time command,
4698 // the output is produced at _parse time_.
Gavin Howard01055ba2018-11-03 11:00:21 -06004699 s = bc_lex_next(&p->l);
4700 if (s) return s;
Denys Vlasenko64074a12018-12-07 15:50:14 +01004701 printf(
4702 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4703 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4704 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4705 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4706 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4707 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4708 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4709 "Number of vars = "BC_MAX_VARS_STR "\n"
4710 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004711 break;
4712 }
4713
4714 case BC_LEX_KEY_PRINT:
4715 {
4716 s = bc_parse_print(p);
4717 break;
4718 }
4719
4720 case BC_LEX_KEY_QUIT:
4721 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004722 // "quit" is a compile-time command. For example,
4723 // "if (0 == 1) quit" terminates when parsing the statement,
4724 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004725 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004726 }
4727
4728 case BC_LEX_KEY_RETURN:
4729 {
4730 s = bc_parse_return(p);
4731 break;
4732 }
4733
4734 case BC_LEX_KEY_WHILE:
4735 {
4736 s = bc_parse_while(p);
4737 break;
4738 }
4739
4740 default:
4741 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004742 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004743 break;
4744 }
4745 }
4746
4747 return s;
4748}
4749
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01004750static FAST_FUNC BcStatus bc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004751{
4752 BcStatus s;
4753
4754 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004755 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 -06004756 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004757 if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004758 s = bc_parse_func(p);
4759 }
4760 else
4761 s = bc_parse_stmt(p);
4762
Denys Vlasenkod38af482018-12-04 19:11:02 +01004763 if (s || G_interrupt) {
4764 bc_parse_reset(p);
4765 s = BC_STATUS_FAILURE;
4766 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004767
4768 return s;
4769}
4770
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004771// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004772static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004773{
4774 BcStatus s = BC_STATUS_SUCCESS;
4775 BcInst prev = BC_INST_PRINT;
4776 BcLexType top, t = p->l.t.t;
4777 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004778 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004779 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4780
4781 paren_first = p->l.t.t == BC_LEX_LPAREN;
4782 nparens = nrelops = 0;
4783 paren_expr = rprn = done = get_token = assign = false;
4784 bin_last = true;
4785
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004786 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004787 switch (t) {
4788
4789 case BC_LEX_OP_INC:
4790 case BC_LEX_OP_DEC:
4791 {
4792 s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
4793 rprn = get_token = bin_last = false;
4794 break;
4795 }
4796
4797 case BC_LEX_OP_MINUS:
4798 {
4799 s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
4800 rprn = get_token = false;
4801 bin_last = prev == BC_INST_MINUS;
4802 break;
4803 }
4804
4805 case BC_LEX_OP_ASSIGN_POWER:
4806 case BC_LEX_OP_ASSIGN_MULTIPLY:
4807 case BC_LEX_OP_ASSIGN_DIVIDE:
4808 case BC_LEX_OP_ASSIGN_MODULUS:
4809 case BC_LEX_OP_ASSIGN_PLUS:
4810 case BC_LEX_OP_ASSIGN_MINUS:
4811 case BC_LEX_OP_ASSIGN:
4812 {
4813 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4814 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4815 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4816 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004817 s = bc_error("bad assignment:"
4818 " left side must be scale,"
4819 " ibase, obase, last, var,"
4820 " or array element"
4821 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004822 break;
4823 }
4824 }
4825 // Fallthrough.
4826 case BC_LEX_OP_POWER:
4827 case BC_LEX_OP_MULTIPLY:
4828 case BC_LEX_OP_DIVIDE:
4829 case BC_LEX_OP_MODULUS:
4830 case BC_LEX_OP_PLUS:
4831 case BC_LEX_OP_REL_EQ:
4832 case BC_LEX_OP_REL_LE:
4833 case BC_LEX_OP_REL_GE:
4834 case BC_LEX_OP_REL_NE:
4835 case BC_LEX_OP_REL_LT:
4836 case BC_LEX_OP_REL_GT:
4837 case BC_LEX_OP_BOOL_NOT:
4838 case BC_LEX_OP_BOOL_OR:
4839 case BC_LEX_OP_BOOL_AND:
4840 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004841 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4842 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4843 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004844 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004845 }
4846
4847 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4848 prev = BC_PARSE_TOKEN_INST(t);
4849 s = bc_parse_operator(p, t, ops_bgn, &nexprs, true);
4850 rprn = get_token = false;
4851 bin_last = t != BC_LEX_OP_BOOL_NOT;
4852
4853 break;
4854 }
4855
4856 case BC_LEX_LPAREN:
4857 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004858 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004859 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004860 ++nparens;
4861 paren_expr = rprn = bin_last = false;
4862 get_token = true;
4863 bc_vec_push(&p->ops, &t);
4864
4865 break;
4866 }
4867
4868 case BC_LEX_RPAREN:
4869 {
4870 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004871 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004872
4873 if (nparens == 0) {
4874 s = BC_STATUS_SUCCESS;
4875 done = true;
4876 get_token = false;
4877 break;
4878 }
4879 else if (!paren_expr)
4880 return BC_STATUS_PARSE_EMPTY_EXP;
4881
4882 --nparens;
4883 paren_expr = rprn = true;
4884 get_token = bin_last = false;
4885
4886 s = bc_parse_rightParen(p, ops_bgn, &nexprs);
4887
4888 break;
4889 }
4890
4891 case BC_LEX_NAME:
4892 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004893 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004894 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004895 paren_expr = true;
4896 rprn = get_token = bin_last = false;
4897 s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
4898 ++nexprs;
4899
4900 break;
4901 }
4902
4903 case BC_LEX_NUMBER:
4904 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004905 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004906 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004907 bc_parse_number(p, &prev, &nexprs);
4908 paren_expr = get_token = true;
4909 rprn = bin_last = false;
4910
4911 break;
4912 }
4913
4914 case BC_LEX_KEY_IBASE:
4915 case BC_LEX_KEY_LAST:
4916 case BC_LEX_KEY_OBASE:
4917 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004918 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004919 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004920 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4921 bc_parse_push(p, (char) prev);
4922
4923 paren_expr = get_token = true;
4924 rprn = bin_last = false;
4925 ++nexprs;
4926
4927 break;
4928 }
4929
4930 case BC_LEX_KEY_LENGTH:
4931 case BC_LEX_KEY_SQRT:
4932 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004933 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004934 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004935 s = bc_parse_builtin(p, t, flags, &prev);
4936 paren_expr = true;
4937 rprn = get_token = bin_last = false;
4938 ++nexprs;
4939
4940 break;
4941 }
4942
4943 case BC_LEX_KEY_READ:
4944 {
4945 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004946 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004947 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004948 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004949 else
4950 s = bc_parse_read(p);
4951
4952 paren_expr = true;
4953 rprn = get_token = bin_last = false;
4954 ++nexprs;
4955 prev = BC_INST_READ;
4956
4957 break;
4958 }
4959
4960 case BC_LEX_KEY_SCALE:
4961 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004962 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004963 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004964 s = bc_parse_scale(p, &prev, flags);
4965 paren_expr = true;
4966 rprn = get_token = bin_last = false;
4967 ++nexprs;
4968 prev = BC_INST_SCALE;
4969
4970 break;
4971 }
4972
4973 default:
4974 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004975 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004976 break;
4977 }
4978 }
4979
4980 if (!s && get_token) s = bc_lex_next(&p->l);
4981 }
4982
4983 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004984 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004985
4986 while (p->ops.len > ops_bgn) {
4987
4988 top = BC_PARSE_TOP_OP(p);
4989 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4990
4991 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004992 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004993
4994 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
4995
4996 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4997 bc_vec_pop(&p->ops);
4998 }
4999
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005000 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005001 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06005002
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005003 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
5004 for (;;) {
5005 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005006 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01005007 if (next & 0x80) // last element?
5008 break;
5009 next >>= 8;
5010 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005011 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005012 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06005013
5014 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01005015 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Gavin Howard01055ba2018-11-03 11:00:21 -06005016 if (s) return s;
5017 }
5018 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01005019 s = bc_POSIX_requires("exactly one comparison operator per condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06005020 if (s) return s;
5021 }
5022
5023 if (flags & BC_PARSE_PRINT) {
5024 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
5025 bc_parse_push(p, BC_INST_POP);
5026 }
5027
5028 return s;
5029}
5030
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01005031static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
5032{
5033 BcStatus s;
5034
5035 s = bc_parse_expr_empty_ok(p, flags, next);
5036 if (s == BC_STATUS_PARSE_EMPTY_EXP)
5037 return bc_error("empty expression");
5038 return s;
5039}
5040
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005041static void bc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005042{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005043 bc_parse_create(p, func, bc_parse_parse, bc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005044}
5045
5046static BcStatus bc_parse_expression(BcParse *p, uint8_t flags)
5047{
5048 return bc_parse_expr(p, flags, bc_parse_next_read);
5049}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005050
Gavin Howard01055ba2018-11-03 11:00:21 -06005051#endif // ENABLE_BC
5052
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005053#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005054
5055#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
5056
Gavin Howard01055ba2018-11-03 11:00:21 -06005057static BcStatus dc_parse_register(BcParse *p)
5058{
5059 BcStatus s;
5060 char *name;
5061
5062 s = bc_lex_next(&p->l);
5063 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005064 if (p->l.t.t != BC_LEX_NAME) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005065
5066 name = xstrdup(p->l.t.v.v);
5067 bc_parse_pushName(p, name);
5068
5069 return s;
5070}
5071
5072static BcStatus dc_parse_string(BcParse *p)
5073{
5074 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005075 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005076
5077 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
5078 name = xstrdup(b);
5079
5080 str = xstrdup(p->l.t.v.v);
5081 bc_parse_push(p, BC_INST_STR);
5082 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005083 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005084 bc_parse_addFunc(p, name, &idx);
5085
5086 return bc_lex_next(&p->l);
5087}
5088
5089static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
5090{
5091 BcStatus s;
5092
5093 bc_parse_push(p, inst);
5094 if (name) {
5095 s = dc_parse_register(p);
5096 if (s) return s;
5097 }
5098
5099 if (store) {
5100 bc_parse_push(p, BC_INST_SWAP);
5101 bc_parse_push(p, BC_INST_ASSIGN);
5102 bc_parse_push(p, BC_INST_POP);
5103 }
5104
5105 return bc_lex_next(&p->l);
5106}
5107
5108static BcStatus dc_parse_cond(BcParse *p, uint8_t inst)
5109{
5110 BcStatus s;
5111
5112 bc_parse_push(p, inst);
5113 bc_parse_push(p, BC_INST_EXEC_COND);
5114
5115 s = dc_parse_register(p);
5116 if (s) return s;
5117
5118 s = bc_lex_next(&p->l);
5119 if (s) return s;
5120
5121 if (p->l.t.t == BC_LEX_ELSE) {
5122 s = dc_parse_register(p);
5123 if (s) return s;
5124 s = bc_lex_next(&p->l);
5125 }
5126 else
5127 bc_parse_push(p, BC_PARSE_STREND);
5128
5129 return s;
5130}
5131
5132static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
5133{
5134 BcStatus s = BC_STATUS_SUCCESS;
5135 BcInst prev;
5136 uint8_t inst;
5137 bool assign, get_token = false;
5138
5139 switch (t) {
5140
5141 case BC_LEX_OP_REL_EQ:
5142 case BC_LEX_OP_REL_LE:
5143 case BC_LEX_OP_REL_GE:
5144 case BC_LEX_OP_REL_NE:
5145 case BC_LEX_OP_REL_LT:
5146 case BC_LEX_OP_REL_GT:
5147 {
5148 s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
5149 break;
5150 }
5151
5152 case BC_LEX_SCOLON:
5153 case BC_LEX_COLON:
5154 {
5155 s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
5156 break;
5157 }
5158
5159 case BC_LEX_STR:
5160 {
5161 s = dc_parse_string(p);
5162 break;
5163 }
5164
5165 case BC_LEX_NEG:
5166 case BC_LEX_NUMBER:
5167 {
5168 if (t == BC_LEX_NEG) {
5169 s = bc_lex_next(&p->l);
5170 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005171 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005172 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005173 }
5174
5175 bc_parse_number(p, &prev, &p->nbraces);
5176
5177 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5178 get_token = true;
5179
5180 break;
5181 }
5182
5183 case BC_LEX_KEY_READ:
5184 {
5185 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005186 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005187 else
5188 bc_parse_push(p, BC_INST_READ);
5189 get_token = true;
5190 break;
5191 }
5192
5193 case BC_LEX_OP_ASSIGN:
5194 case BC_LEX_STORE_PUSH:
5195 {
5196 assign = t == BC_LEX_OP_ASSIGN;
5197 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
5198 s = dc_parse_mem(p, inst, true, assign);
5199 break;
5200 }
5201
5202 case BC_LEX_LOAD:
5203 case BC_LEX_LOAD_POP:
5204 {
5205 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
5206 s = dc_parse_mem(p, inst, true, false);
5207 break;
5208 }
5209
5210 case BC_LEX_STORE_IBASE:
5211 case BC_LEX_STORE_SCALE:
5212 case BC_LEX_STORE_OBASE:
5213 {
5214 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
5215 s = dc_parse_mem(p, inst, false, true);
5216 break;
5217 }
5218
5219 default:
5220 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005221 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005222 get_token = true;
5223 break;
5224 }
5225 }
5226
5227 if (!s && get_token) s = bc_lex_next(&p->l);
5228
5229 return s;
5230}
5231
5232static BcStatus dc_parse_expr(BcParse *p, uint8_t flags)
5233{
5234 BcStatus s = BC_STATUS_SUCCESS;
5235 BcInst inst;
5236 BcLexType t;
5237
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005238 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005239
5240 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
5241
5242 inst = dc_parse_insts[t];
5243
5244 if (inst != BC_INST_INVALID) {
5245 bc_parse_push(p, inst);
5246 s = bc_lex_next(&p->l);
5247 }
5248 else
5249 s = dc_parse_token(p, t, flags);
5250 }
5251
5252 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5253 bc_parse_push(p, BC_INST_POP_EXEC);
5254
5255 return s;
5256}
5257
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01005258static FAST_FUNC BcStatus dc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005259{
5260 BcStatus s;
5261
5262 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005263 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005264 else
5265 s = dc_parse_expr(p, 0);
5266
Denys Vlasenkod38af482018-12-04 19:11:02 +01005267 if (s || G_interrupt) {
5268 bc_parse_reset(p);
5269 s = BC_STATUS_FAILURE;
5270 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005271
5272 return s;
5273}
5274
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005275static void dc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005276{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005277 bc_parse_create(p, func, dc_parse_parse, dc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005278}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005279
Gavin Howard01055ba2018-11-03 11:00:21 -06005280#endif // ENABLE_DC
5281
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005282static void common_parse_init(BcParse *p, size_t func)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005283{
5284 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005285 IF_BC(bc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005286 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005287 IF_DC(dc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005288 }
5289}
5290
5291static BcStatus common_parse_expr(BcParse *p, uint8_t flags)
5292{
5293 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005294 IF_BC(return bc_parse_expression(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005295 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005296 IF_DC(return dc_parse_expr(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005297 }
5298}
5299
Denys Vlasenkodf515392018-12-02 19:27:48 +01005300static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005301{
Gavin Howard01055ba2018-11-03 11:00:21 -06005302 BcId e, *ptr;
5303 BcVec *v, *map;
5304 size_t i;
5305 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005306 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005307
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005308 v = var ? &G.prog.vars : &G.prog.arrs;
5309 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005310
5311 e.name = id;
5312 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005313 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005314
5315 if (new) {
5316 bc_array_init(&data.v, var);
5317 bc_vec_push(v, &data.v);
5318 }
5319
5320 ptr = bc_vec_item(map, i);
5321 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005322 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005323}
5324
Denys Vlasenko29301232018-12-11 15:29:32 +01005325static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005326{
Gavin Howard01055ba2018-11-03 11:00:21 -06005327 switch (r->t) {
5328
5329 case BC_RESULT_STR:
5330 case BC_RESULT_TEMP:
5331 case BC_RESULT_IBASE:
5332 case BC_RESULT_SCALE:
5333 case BC_RESULT_OBASE:
5334 {
5335 *num = &r->d.n;
5336 break;
5337 }
5338
5339 case BC_RESULT_CONSTANT:
5340 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005341 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005342 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005343 size_t base_t, len = strlen(*str);
5344 BcNum *base;
5345
5346 bc_num_init(&r->d.n, len);
5347
5348 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005349 base = hex ? &G.prog.hexb : &G.prog.ib;
5350 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005351 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005352
5353 if (s) {
5354 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005355 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005356 }
5357
5358 *num = &r->d.n;
5359 r->t = BC_RESULT_TEMP;
5360
5361 break;
5362 }
5363
5364 case BC_RESULT_VAR:
5365 case BC_RESULT_ARRAY:
5366 case BC_RESULT_ARRAY_ELEM:
5367 {
5368 BcVec *v;
5369
Denys Vlasenkodf515392018-12-02 19:27:48 +01005370 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005371
5372 if (r->t == BC_RESULT_ARRAY_ELEM) {
5373 v = bc_vec_top(v);
5374 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5375 *num = bc_vec_item(v, r->d.id.idx);
5376 }
5377 else
5378 *num = bc_vec_top(v);
5379
5380 break;
5381 }
5382
5383 case BC_RESULT_LAST:
5384 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005385 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005386 break;
5387 }
5388
5389 case BC_RESULT_ONE:
5390 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005391 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005392 break;
5393 }
5394 }
5395
Denys Vlasenko29301232018-12-11 15:29:32 +01005396 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005397}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005398#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005399# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005400#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005401
Denys Vlasenko29301232018-12-11 15:29:32 +01005402static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005403 BcResult **r, BcNum **rn, bool assign)
5404{
5405 BcStatus s;
5406 bool hex;
5407 BcResultType lt, rt;
5408
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005409 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005410 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005411
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005412 *r = bc_vec_item_rev(&G.prog.results, 0);
5413 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005414
5415 lt = (*l)->t;
5416 rt = (*r)->t;
5417 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5418
Denys Vlasenko29301232018-12-11 15:29:32 +01005419 s = zbc_program_num(*l, ln, false);
5420 if (s) RETURN_STATUS(s);
5421 s = zbc_program_num(*r, rn, hex);
5422 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005423
5424 // We run this again under these conditions in case any vector has been
5425 // reallocated out from under the BcNums or arrays we had.
5426 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005427 s = zbc_program_num(*l, ln, false);
5428 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005429 }
5430
5431 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005432 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005433 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005434 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005435
Denys Vlasenko29301232018-12-11 15:29:32 +01005436 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005437}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005438#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005439# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005440#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005441
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005442static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005443{
5444 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005445 bc_vec_pop(&G.prog.results);
5446 bc_vec_pop(&G.prog.results);
5447 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005448}
5449
Denys Vlasenko29301232018-12-11 15:29:32 +01005450static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005451{
5452 BcStatus s;
5453
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005454 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005455 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005456 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005457
Denys Vlasenko29301232018-12-11 15:29:32 +01005458 s = zbc_program_num(*r, n, false);
5459 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005460
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005461 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005462 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005463
Denys Vlasenko29301232018-12-11 15:29:32 +01005464 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005465}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005466#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005467# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005468#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005469
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005470static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005471{
5472 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005473 bc_vec_pop(&G.prog.results);
5474 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005475}
5476
Denys Vlasenko259137d2018-12-11 19:42:05 +01005477static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005478{
5479 BcStatus s;
5480 BcResult *opd1, *opd2, res;
5481 BcNum *n1, *n2 = NULL;
5482
Denys Vlasenko29301232018-12-11 15:29:32 +01005483 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005484 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005485 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005486
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005487 s = BC_STATUS_SUCCESS;
5488#if !ERRORS_ARE_FATAL
5489 s =
5490#endif
5491 zbc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005492 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005493 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005494
Denys Vlasenko259137d2018-12-11 19:42:05 +01005495 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005496
5497err:
5498 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005499 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005500}
Denys Vlasenko259137d2018-12-11 19:42:05 +01005501#if ERRORS_ARE_FATAL
5502# define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS)
5503#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005504
Denys Vlasenko785e4b32018-12-02 17:18:52 +01005505static BcStatus bc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005506{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005507 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005508 BcStatus s;
5509 BcParse parse;
5510 BcVec buf;
5511 BcInstPtr ip;
5512 size_t i;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005513 BcFunc *f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005514
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005515 for (i = 0; i < G.prog.stack.len; ++i) {
5516 BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005517 if (ip_ptr->func == BC_PROG_READ)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005518 return bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005519 }
5520
Denys Vlasenko7d628012018-12-04 21:46:47 +01005521 bc_vec_pop_all(&f->code);
5522 bc_char_vec_init(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005523
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005524 sv_file = G.prog.file;
5525 G.prog.file = NULL;
5526
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01005527 s = bc_read_line(&buf);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005528 //if (s) goto io_err; - wrong, nonzero return means EOF, not error
Gavin Howard01055ba2018-11-03 11:00:21 -06005529
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005530 common_parse_init(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005531 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005532
5533 s = bc_parse_text(&parse, buf.v);
5534 if (s) goto exec_err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005535 s = common_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005536 if (s) goto exec_err;
5537
5538 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005539 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005540 goto exec_err;
5541 }
5542
5543 ip.func = BC_PROG_READ;
5544 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005545 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005546
5547 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005548 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005549
5550 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005551 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005552
5553exec_err:
5554 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005555//io_err:
5556 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005557 bc_vec_free(&buf);
5558 return s;
5559}
5560
5561static size_t bc_program_index(char *code, size_t *bgn)
5562{
5563 char amt = code[(*bgn)++], i = 0;
5564 size_t res = 0;
5565
5566 for (; i < amt; ++i, ++(*bgn))
5567 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5568
5569 return res;
5570}
5571
5572static char *bc_program_name(char *code, size_t *bgn)
5573{
5574 size_t i;
5575 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5576
5577 s = xmalloc(ptr - str + 1);
5578 c = code[(*bgn)++];
5579
5580 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5581 s[i] = c;
5582
5583 s[i] = '\0';
5584
5585 return s;
5586}
5587
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005588static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005589{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005590#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005591 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005592 // Example: echo '[]ap' | dc
5593 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005594 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005595 return;
5596 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005597#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005598 while (*str) {
5599 int c = *str++;
5600 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005601 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005602 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005603 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005604 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005605 case 'a':
5606 bb_putchar('\a');
5607 break;
5608 case 'b':
5609 bb_putchar('\b');
5610 break;
5611 case '\\':
5612 case 'e':
5613 bb_putchar('\\');
5614 break;
5615 case 'f':
5616 bb_putchar('\f');
5617 break;
5618 case 'n':
5619 bb_putchar('\n');
5620 G.prog.nchars = SIZE_MAX;
5621 break;
5622 case 'r':
5623 bb_putchar('\r');
5624 break;
5625 case 'q':
5626 bb_putchar('"');
5627 break;
5628 case 't':
5629 bb_putchar('\t');
5630 break;
5631 default:
5632 // Just print the backslash and following character.
5633 bb_putchar('\\');
5634 ++G.prog.nchars;
5635 bb_putchar(c);
5636 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005637 }
5638 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005639 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005640 }
5641}
5642
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005643static void bc_num_printNewline(void)
5644{
5645 if (G.prog.nchars == G.prog.len - 1) {
5646 bb_putchar('\\');
5647 bb_putchar('\n');
5648 G.prog.nchars = 0;
5649 }
5650}
5651
5652#if ENABLE_DC
5653static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
5654{
5655 (void) radix;
5656 bb_putchar((char) num);
5657 G.prog.nchars += width;
5658}
5659#endif
5660
5661static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5662{
5663 size_t exp, pow;
5664
5665 bc_num_printNewline();
5666 bb_putchar(radix ? '.' : ' ');
5667 ++G.prog.nchars;
5668
5669 bc_num_printNewline();
5670 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5671 continue;
5672
5673 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5674 size_t dig;
5675 bc_num_printNewline();
5676 dig = num / pow;
5677 num -= dig * pow;
5678 bb_putchar(((char) dig) + '0');
5679 }
5680}
5681
5682static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5683{
5684 if (radix) {
5685 bc_num_printNewline();
5686 bb_putchar('.');
5687 G.prog.nchars += 1;
5688 }
5689
5690 bc_num_printNewline();
5691 bb_putchar(bb_hexdigits_upcase[num]);
5692 G.prog.nchars += width;
5693}
5694
5695static void bc_num_printDecimal(BcNum *n)
5696{
5697 size_t i, rdx = n->rdx - 1;
5698
5699 if (n->neg) bb_putchar('-');
5700 G.prog.nchars += n->neg;
5701
5702 for (i = n->len - 1; i < n->len; --i)
5703 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5704}
5705
5706static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
5707{
5708 BcStatus s;
5709 BcVec stack;
5710 BcNum intp, fracp, digit, frac_len;
5711 unsigned long dig, *ptr;
5712 size_t i;
5713 bool radix;
5714
5715 if (n->len == 0) {
5716 print(0, width, false);
5717 RETURN_STATUS(BC_STATUS_SUCCESS);
5718 }
5719
5720 bc_vec_init(&stack, sizeof(long), NULL);
5721 bc_num_init(&intp, n->len);
5722 bc_num_init(&fracp, n->rdx);
5723 bc_num_init(&digit, width);
5724 bc_num_init(&frac_len, BC_NUM_INT(n));
5725 bc_num_copy(&intp, n);
5726 bc_num_one(&frac_len);
5727
5728 bc_num_truncate(&intp, intp.rdx);
5729 s = zbc_num_sub(n, &intp, &fracp, 0);
5730 if (s) goto err;
5731
5732 while (intp.len != 0) {
5733 s = zbc_num_divmod(&intp, base, &intp, &digit, 0);
5734 if (s) goto err;
5735 s = zbc_num_ulong(&digit, &dig);
5736 if (s) goto err;
5737 bc_vec_push(&stack, &dig);
5738 }
5739
5740 for (i = 0; i < stack.len; ++i) {
5741 ptr = bc_vec_item_rev(&stack, i);
5742 print(*ptr, width, false);
5743 }
5744
5745 if (!n->rdx) goto err;
5746
5747 for (radix = true; frac_len.len <= n->rdx; radix = false) {
5748 s = zbc_num_mul(&fracp, base, &fracp, n->rdx);
5749 if (s) goto err;
5750 s = zbc_num_ulong(&fracp, &dig);
5751 if (s) goto err;
5752 bc_num_ulong2num(&intp, dig);
5753 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5754 if (s) goto err;
5755 print(dig, width, radix);
5756 s = zbc_num_mul(&frac_len, base, &frac_len, 0);
5757 if (s) goto err;
5758 }
5759
5760err:
5761 bc_num_free(&frac_len);
5762 bc_num_free(&digit);
5763 bc_num_free(&fracp);
5764 bc_num_free(&intp);
5765 bc_vec_free(&stack);
5766 RETURN_STATUS(s);
5767}
5768#if ERRORS_ARE_FATAL
5769# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
5770#endif
5771
5772static BC_STATUS zbc_num_printBase(BcNum *n)
5773{
5774 BcStatus s;
5775 size_t width, i;
5776 BcNumDigitOp print;
5777 bool neg = n->neg;
5778
5779 if (neg) {
5780 bb_putchar('-');
5781 G.prog.nchars++;
5782 }
5783
5784 n->neg = false;
5785
5786 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5787 width = 1;
5788 print = bc_num_printHex;
5789 }
5790 else {
5791 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
5792 continue;
5793 print = bc_num_printDigits;
5794 }
5795
5796 s = zbc_num_printNum(n, &G.prog.ob, width, print);
5797 n->neg = neg;
5798
5799 RETURN_STATUS(s);
5800}
5801#if ERRORS_ARE_FATAL
5802# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
5803#endif
5804
5805#if ENABLE_DC
5806static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
5807{
5808 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
5809}
5810#if ERRORS_ARE_FATAL
5811# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
5812#endif
5813#endif
5814
5815static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5816{
5817 BcStatus s = BC_STATUS_SUCCESS;
5818
5819 bc_num_printNewline();
5820
5821 if (n->len == 0) {
5822 bb_putchar('0');
5823 ++G.prog.nchars;
5824 }
5825 else if (G.prog.ob_t == 10)
5826 bc_num_printDecimal(n);
5827 else
5828 s = zbc_num_printBase(n);
5829
5830 if (newline) {
5831 bb_putchar('\n');
5832 G.prog.nchars = 0;
5833 }
5834
5835 RETURN_STATUS(s);
5836}
5837#if ERRORS_ARE_FATAL
5838# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
5839#endif
5840
Denys Vlasenko29301232018-12-11 15:29:32 +01005841static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005842{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005843 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005844 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005845 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005846 bool pop = inst != BC_INST_PRINT;
5847
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005848 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005849 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005850
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005851 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005852 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005853 s = zbc_program_num(r, &num, false);
5854 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005855
5856 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005857 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005858 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005859 }
5860 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005861 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005862
5863 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005864 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005865
5866 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005867 for (;;) {
5868 char c = *str++;
5869 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005870 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005871 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005872 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005873 }
5874 }
5875 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005876 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005877 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005878 }
5879 }
5880
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005881 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005882
Denys Vlasenko29301232018-12-11 15:29:32 +01005883 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005884}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005885#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005886# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005887#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005888
Denys Vlasenko29301232018-12-11 15:29:32 +01005889static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005890{
5891 BcStatus s;
5892 BcResult res, *ptr;
5893 BcNum *num = NULL;
5894
Denys Vlasenko29301232018-12-11 15:29:32 +01005895 s = zbc_program_prep(&ptr, &num);
5896 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005897
5898 bc_num_init(&res.d.n, num->len);
5899 bc_num_copy(&res.d.n, num);
5900 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5901
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005902 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005903
Denys Vlasenko29301232018-12-11 15:29:32 +01005904 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005905}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005906#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005907# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005908#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005909
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005910static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005911{
5912 BcStatus s;
5913 BcResult *opd1, *opd2, res;
5914 BcNum *n1, *n2;
5915 bool cond = 0;
5916 ssize_t cmp;
5917
Denys Vlasenko29301232018-12-11 15:29:32 +01005918 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005919 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005920
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005921 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005922
5923 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005924 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005925 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005926 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005927 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005928 cmp = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005929 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005930 case BC_INST_REL_EQ:
5931 cond = cmp == 0;
5932 break;
5933 case BC_INST_REL_LE:
5934 cond = cmp <= 0;
5935 break;
5936 case BC_INST_REL_GE:
5937 cond = cmp >= 0;
5938 break;
5939 case BC_INST_REL_NE:
5940 cond = cmp != 0;
5941 break;
5942 case BC_INST_REL_LT:
5943 cond = cmp < 0;
5944 break;
5945 case BC_INST_REL_GT:
5946 cond = cmp > 0;
5947 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005948 }
5949 }
5950
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005951 if (cond) bc_num_one(&res.d.n);
5952 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005953
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005954 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005955
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005956 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005957}
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005958#if ERRORS_ARE_FATAL
5959# define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS)
5960#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005961
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005962#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005963static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005964 bool push)
5965{
5966 BcNum n2;
5967 BcResult res;
5968
5969 memset(&n2, 0, sizeof(BcNum));
5970 n2.rdx = res.d.id.idx = r->d.id.idx;
5971 res.t = BC_RESULT_STR;
5972
5973 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005974 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005975 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005976 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005977 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005978 }
5979
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005980 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005981
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005982 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005983 bc_vec_push(v, &n2);
5984
Denys Vlasenko29301232018-12-11 15:29:32 +01005985 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005986}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005987#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005988# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005989#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005990#endif // ENABLE_DC
5991
Denys Vlasenko29301232018-12-11 15:29:32 +01005992static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005993{
5994 BcStatus s;
5995 BcResult *ptr, r;
5996 BcVec *v;
5997 BcNum *n;
5998
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005999 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006000 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006001
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006002 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006003 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01006004 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01006005 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06006006
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006007#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006008 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01006009 RETURN_STATUS(bc_error_variable_is_wrong_type());
6010 if (ptr->t == BC_RESULT_STR)
6011 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006012#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006013
Denys Vlasenko29301232018-12-11 15:29:32 +01006014 s = zbc_program_num(ptr, &n, false);
6015 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006016
6017 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01006018 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06006019
6020 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006021 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006022 bc_num_copy(&r.d.n, n);
6023 }
6024 else {
6025 bc_array_init(&r.d.v, true);
6026 bc_array_copy(&r.d.v, (BcVec *) n);
6027 }
6028
6029 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006030 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006031
Denys Vlasenko29301232018-12-11 15:29:32 +01006032 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006033}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006034#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006035# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006036#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006037
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006038static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006039{
6040 BcStatus s;
6041 BcResult *left, *right, res;
6042 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006043 bool assign = inst == BC_INST_ASSIGN, ib, sc;
6044
Denys Vlasenko29301232018-12-11 15:29:32 +01006045 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006046 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006047
6048 ib = left->t == BC_RESULT_IBASE;
6049 sc = left->t == BC_RESULT_SCALE;
6050
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006051#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006052
6053 if (right->t == BC_RESULT_STR) {
6054
6055 BcVec *v;
6056
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006057 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006058 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01006059 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006060
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006061 RETURN_STATUS(zbc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06006062 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006063#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006064
6065 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01006066 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006067 " left side must be scale,"
6068 " ibase, obase, last, var,"
6069 " or array element"
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006070 ));
Gavin Howard01055ba2018-11-03 11:00:21 -06006071
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006072#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006073 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006074 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06006075
6076 if (assign)
6077 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006078 else {
6079 s = BC_STATUS_SUCCESS;
6080#if !ERRORS_ARE_FATAL
6081 s =
6082#endif
6083 zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
6084 }
6085 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006086#else
Gavin Howard01055ba2018-11-03 11:00:21 -06006087 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006088#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006089
6090 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006091 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006092 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
6093 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
6094 NULL, //can't happen //BC_RESULT_LAST
6095 NULL, //can't happen //BC_RESULT_CONSTANT
6096 NULL, //can't happen //BC_RESULT_ONE
6097 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006098 };
Gavin Howard01055ba2018-11-03 11:00:21 -06006099 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01006100 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06006101
Denys Vlasenko29301232018-12-11 15:29:32 +01006102 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006103 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006104 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06006105 if (sc) {
6106 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006107 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006108 }
6109 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006110 if (val < BC_NUM_MIN_BASE)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006111 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06006112 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006113 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006114 }
6115
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006116 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006117 RETURN_STATUS(bc_error(msg[s]));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006118 if (!sc)
6119 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006120
6121 *ptr = (size_t) val;
6122 s = BC_STATUS_SUCCESS;
6123 }
6124
6125 bc_num_init(&res.d.n, l->len);
6126 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006127 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006128
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006129 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006130}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006131#if ERRORS_ARE_FATAL
6132# define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS)
6133#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006134
Denys Vlasenko416ce762018-12-02 20:57:17 +01006135#if !ENABLE_DC
6136#define bc_program_pushVar(code, bgn, pop, copy) \
6137 bc_program_pushVar(code, bgn)
6138// for bc, 'pop' and 'copy' are always false
6139#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006140static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006141 bool pop, bool copy)
6142{
Gavin Howard01055ba2018-11-03 11:00:21 -06006143 BcResult r;
6144 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006145
6146 r.t = BC_RESULT_VAR;
6147 r.d.id.name = name;
6148
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006149#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006150 {
6151 BcVec *v = bc_program_search(name, true);
6152 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006153
Denys Vlasenko416ce762018-12-02 20:57:17 +01006154 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006155
Denys Vlasenko416ce762018-12-02 20:57:17 +01006156 if (!BC_PROG_STACK(v, 2 - copy)) {
6157 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006158 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006159 }
6160
Gavin Howard01055ba2018-11-03 11:00:21 -06006161 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006162 name = NULL;
6163
6164 if (!BC_PROG_STR(num)) {
6165
6166 r.t = BC_RESULT_TEMP;
6167
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006168 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006169 bc_num_copy(&r.d.n, num);
6170 }
6171 else {
6172 r.t = BC_RESULT_STR;
6173 r.d.id.idx = num->rdx;
6174 }
6175
6176 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006177 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006178 }
6179#endif // ENABLE_DC
6180
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006181 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006182
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006183 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006184}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006185#if ERRORS_ARE_FATAL
6186# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6187#else
6188# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6189#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006190
Denys Vlasenko29301232018-12-11 15:29:32 +01006191static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006192 char inst)
6193{
6194 BcStatus s = BC_STATUS_SUCCESS;
6195 BcResult r;
6196 BcNum *num;
6197
6198 r.d.id.name = bc_program_name(code, bgn);
6199
6200 if (inst == BC_INST_ARRAY) {
6201 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006202 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006203 }
6204 else {
6205
6206 BcResult *operand;
6207 unsigned long temp;
6208
Denys Vlasenko29301232018-12-11 15:29:32 +01006209 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006210 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006211 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006212 if (s) goto err;
6213
6214 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006215 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006216 goto err;
6217 }
6218
6219 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006220 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006221 }
6222
6223err:
6224 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006225 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006226}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006227#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006228# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006229#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006230
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006231#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006232static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006233{
6234 BcStatus s;
6235 BcResult *ptr, res, copy;
6236 BcNum *num = NULL;
6237 char inst2 = inst;
6238
Denys Vlasenko29301232018-12-11 15:29:32 +01006239 s = zbc_program_prep(&ptr, &num);
6240 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006241
6242 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6243 copy.t = BC_RESULT_TEMP;
6244 bc_num_init(&copy.d.n, num->len);
6245 bc_num_copy(&copy.d.n, num);
6246 }
6247
6248 res.t = BC_RESULT_ONE;
6249 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6250 BC_INST_ASSIGN_PLUS :
6251 BC_INST_ASSIGN_MINUS;
6252
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006253 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006254 s = zbc_program_assign(inst);
6255 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006256
6257 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006258 bc_vec_pop(&G.prog.results);
6259 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006260 }
6261
Denys Vlasenko29301232018-12-11 15:29:32 +01006262 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006263}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006264#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006265# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006266#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006267
Denys Vlasenko29301232018-12-11 15:29:32 +01006268static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006269{
Gavin Howard01055ba2018-11-03 11:00:21 -06006270 BcInstPtr ip;
6271 size_t i, nparams = bc_program_index(code, idx);
6272 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006273 BcId *a;
6274 BcResultData param;
6275 BcResult *arg;
6276
6277 ip.idx = 0;
6278 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006279 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006280
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006281 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006282 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006283 }
6284 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006285 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006286 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006287 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006288
6289 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006290 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006291
6292 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006293 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006294
6295 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006296 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006297
Denys Vlasenko29301232018-12-11 15:29:32 +01006298 s = zbc_program_copyToVar(a->name, a->idx);
6299 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006300 }
6301
6302 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006303 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006304
6305 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006306 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006307
6308 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006309 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006310 bc_vec_push(v, &param.n);
6311 }
6312 else {
6313 bc_array_init(&param.v, true);
6314 bc_vec_push(v, &param.v);
6315 }
6316 }
6317
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006318 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006319
Denys Vlasenko29301232018-12-11 15:29:32 +01006320 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006321}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006322#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006323# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006324#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006325
Denys Vlasenko29301232018-12-11 15:29:32 +01006326static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006327{
Gavin Howard01055ba2018-11-03 11:00:21 -06006328 BcResult res;
6329 BcFunc *f;
6330 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006331 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006332
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006333 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006334 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006335
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006336 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006337 res.t = BC_RESULT_TEMP;
6338
6339 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006340 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006341 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006342 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006343
Denys Vlasenko29301232018-12-11 15:29:32 +01006344 s = zbc_program_num(operand, &num, false);
6345 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006346 bc_num_init(&res.d.n, num->len);
6347 bc_num_copy(&res.d.n, num);
6348 }
6349 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006350 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006351 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006352 }
6353
6354 // We need to pop arguments as well, so this takes that into account.
6355 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006356 BcVec *v;
6357 BcId *a = bc_vec_item(&f->autos, i);
6358
Denys Vlasenkodf515392018-12-02 19:27:48 +01006359 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006360 bc_vec_pop(v);
6361 }
6362
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006363 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6364 bc_vec_push(&G.prog.results, &res);
6365 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006366
Denys Vlasenko29301232018-12-11 15:29:32 +01006367 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006368}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006369#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006370# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006371#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006372#endif // ENABLE_BC
6373
6374static unsigned long bc_program_scale(BcNum *n)
6375{
6376 return (unsigned long) n->rdx;
6377}
6378
6379static unsigned long bc_program_len(BcNum *n)
6380{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006381 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006382
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006383 if (n->rdx != len) return len;
6384 for (;;) {
6385 if (len == 0) break;
6386 len--;
6387 if (n->num[len] != 0) break;
6388 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006389 return len;
6390}
6391
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006392static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006393{
6394 BcStatus s;
6395 BcResult *opnd;
6396 BcNum *num = NULL;
6397 BcResult res;
6398 bool len = inst == BC_INST_LENGTH;
6399
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006400 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006401 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006402 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006403
Denys Vlasenko29301232018-12-11 15:29:32 +01006404 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006405 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006406
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006407#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006408 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006409 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006410#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006411
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006412 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006413
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006414 if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006415#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006416 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006417 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006418 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006419#endif
6420#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006421 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006422 char **str;
6423 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6424
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006425 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006426 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006427 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006428#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006429 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006430 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006431 }
6432
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006433 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006434
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006435 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006436}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006437#if ERRORS_ARE_FATAL
6438# define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
6439#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006440
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006441#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006442static BC_STATUS zbc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006443{
6444 BcStatus s;
6445 BcResult *opd1, *opd2, res, res2;
6446 BcNum *n1, *n2 = NULL;
6447
Denys Vlasenko29301232018-12-11 15:29:32 +01006448 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006449 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006450
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006451 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006452 bc_num_init(&res2.d.n, n2->len);
6453
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006454 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006455 if (s) goto err;
6456
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006457 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006458 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006459 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006460
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006461 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006462
6463err:
6464 bc_num_free(&res2.d.n);
6465 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006466 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006467}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006468#if ERRORS_ARE_FATAL
6469# define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
6470#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006471
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006472static BC_STATUS zbc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006473{
6474 BcStatus s;
6475 BcResult *r1, *r2, *r3, res;
6476 BcNum *n1, *n2, *n3;
6477
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006478 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006479 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006480 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006481 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006482
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006483 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006484 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006485 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006486 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006487 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006488
6489 // Make sure that the values have their pointers updated, if necessary.
6490 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6491
6492 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006493 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006494 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006495 }
6496
6497 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006498 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006499 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006500 }
6501 }
6502
6503 bc_num_init(&res.d.n, n3->len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006504 s = zbc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006505 if (s) goto err;
6506
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006507 bc_vec_pop(&G.prog.results);
6508 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006509
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006510 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006511
6512err:
6513 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006514 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006515}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006516#if ERRORS_ARE_FATAL
6517# define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
6518#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006519
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006520static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006521{
Gavin Howard01055ba2018-11-03 11:00:21 -06006522 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006523 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006524
6525 res.t = BC_RESULT_TEMP;
6526
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006527 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006528 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006529 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006530}
6531
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006532static BC_STATUS zbc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006533{
6534 BcStatus s;
6535 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006536 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006537 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006538 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006539 unsigned long val;
6540
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006541 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006542 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006543 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006544
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006545 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006546 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006547 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006548
6549 if (BC_PROG_NUM(r, num)) {
6550
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006551 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006552 bc_num_copy(&n, num);
6553 bc_num_truncate(&n, n.rdx);
6554
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006555 s = zbc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006556 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006557 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006558 if (s) goto num_err;
6559
6560 c = (char) val;
6561
6562 bc_num_free(&n);
6563 }
6564 else {
6565 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006566 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006567 c = str2[0];
6568 }
6569
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006570 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006571 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006572 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006573
6574 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006575 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006576
6577 if (idx != len + BC_PROG_REQ_FUNCS) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006578 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006579 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006580 len = idx;
6581 break;
6582 }
6583 }
6584
6585 free(str);
6586 }
6587 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006588 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006589
6590 res.t = BC_RESULT_STR;
6591 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006592 bc_vec_pop(&G.prog.results);
6593 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006594
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006595 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006596
6597num_err:
6598 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006599 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006600}
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006601#if ERRORS_ARE_FATAL
6602# define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__), BC_STATUS_SUCCESS)
6603#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006604
Denys Vlasenko29301232018-12-11 15:29:32 +01006605static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006606{
6607 BcStatus s;
6608 BcResult *r;
6609 BcNum *n = NULL;
6610 size_t idx;
6611 char *str;
6612
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006613 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006614 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006615 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006616
Denys Vlasenko29301232018-12-11 15:29:32 +01006617 s = zbc_program_num(r, &n, false);
6618 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006619
6620 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006621 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006622 else {
6623 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006624 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006625 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006626 }
6627
Denys Vlasenko29301232018-12-11 15:29:32 +01006628 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006629}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006630#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006631# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006632#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006633
Denys Vlasenko29301232018-12-11 15:29:32 +01006634static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006635{
6636 BcStatus s;
6637 BcResult *opnd;
6638 BcNum *num = NULL;
6639 unsigned long val;
6640
Denys Vlasenko29301232018-12-11 15:29:32 +01006641 s = zbc_program_prep(&opnd, &num);
6642 if (s) RETURN_STATUS(s);
6643 s = zbc_num_ulong(num, &val);
6644 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006645
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006646 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006647
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006648 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006649 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006650 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006651 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006652 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006653
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006654 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006655
Denys Vlasenko29301232018-12-11 15:29:32 +01006656 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006657}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006658#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006659# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006660#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006661
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006662static BcStatus bc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006663 bool cond)
6664{
6665 BcStatus s = BC_STATUS_SUCCESS;
6666 BcResult *r;
6667 char **str;
6668 BcFunc *f;
6669 BcParse prs;
6670 BcInstPtr ip;
6671 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006672
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006673 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006674 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006675
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006676 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006677
6678 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006679 BcNum *n = n; // for compiler
6680 bool exec;
6681 char *name;
6682 char *then_name = bc_program_name(code, bgn);
6683 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006684
6685 if (code[*bgn] == BC_PARSE_STREND)
6686 (*bgn) += 1;
6687 else
6688 else_name = bc_program_name(code, bgn);
6689
6690 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006691 name = then_name;
6692 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006693 exec = true;
6694 name = else_name;
6695 }
6696
6697 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006698 BcVec *v;
6699 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006700 n = bc_vec_top(v);
6701 }
6702
6703 free(then_name);
6704 free(else_name);
6705
6706 if (!exec) goto exit;
6707 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006708 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006709 goto exit;
6710 }
6711
6712 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006713 } else {
6714 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006715 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006716 } else if (r->t == BC_RESULT_VAR) {
6717 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006718 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006719 if (s || !BC_PROG_STR(n)) goto exit;
6720 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006721 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006722 goto exit;
6723 }
6724
6725 fidx = sidx + BC_PROG_REQ_FUNCS;
6726
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006727 str = bc_program_str(sidx);
6728 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006729
6730 if (f->code.len == 0) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006731 common_parse_init(&prs, fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006732 s = bc_parse_text(&prs, *str);
6733 if (s) goto err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01006734 s = common_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006735 if (s) goto err;
6736
6737 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006738 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006739 goto err;
6740 }
6741
6742 bc_parse_free(&prs);
6743 }
6744
6745 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006746 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006747 ip.func = fidx;
6748
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006749 bc_vec_pop(&G.prog.results);
6750 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006751
6752 return BC_STATUS_SUCCESS;
6753
6754err:
6755 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006756 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006757 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006758exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006759 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006760 return s;
6761}
6762#endif // ENABLE_DC
6763
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006764static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006765{
Gavin Howard01055ba2018-11-03 11:00:21 -06006766 BcResult res;
6767 unsigned long val;
6768
6769 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6770 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006771 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006772 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006773 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006774 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006775 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006776
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006777 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006778 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006779 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006780}
6781
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006782static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006783{
Gavin Howard01055ba2018-11-03 11:00:21 -06006784 BcId entry, *entry_ptr;
6785 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006786 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006787
6788 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006789 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006790
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006791 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6792 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006793
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006794 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006795 *idx = entry_ptr->idx;
6796
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006797 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006798
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006799 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006800
6801 // We need to reset these, so the function can be repopulated.
6802 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006803 bc_vec_pop_all(&func->autos);
6804 bc_vec_pop_all(&func->code);
6805 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006806 }
6807 else {
6808 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006809 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006810 }
6811}
6812
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006813static BcStatus bc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006814{
Gavin Howard01055ba2018-11-03 11:00:21 -06006815 BcResult r, *ptr;
6816 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006817 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006818 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006819 char *code = func->code.v;
6820 bool cond = false;
6821
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006822 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006823 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006824 char inst = code[(ip->idx)++];
6825
6826 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006827#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006828 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006829 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006830 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006831 cond = !bc_num_cmp(num, &G.prog.zero);
6832 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006833 // Fallthrough.
6834 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006835 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006836 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006837 addr = bc_vec_item(&func->labels, idx);
6838 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6839 break;
6840 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006841 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006842 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006843 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006844 case BC_INST_INC_PRE:
6845 case BC_INST_DEC_PRE:
6846 case BC_INST_INC_POST:
6847 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006848 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006849 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006850 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006851 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006852 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006853 case BC_INST_RET:
6854 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006855 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006856 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006857 case BC_INST_BOOL_OR:
6858 case BC_INST_BOOL_AND:
6859#endif // ENABLE_BC
6860 case BC_INST_REL_EQ:
6861 case BC_INST_REL_LE:
6862 case BC_INST_REL_GE:
6863 case BC_INST_REL_NE:
6864 case BC_INST_REL_LT:
6865 case BC_INST_REL_GT:
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006866 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006867 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006868 case BC_INST_READ:
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006869 s = bc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006870 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006871 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006872 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006873 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006874 case BC_INST_ARRAY_ELEM:
6875 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006876 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006877 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006878 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006879 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006880 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006881 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006882 case BC_INST_IBASE:
6883 case BC_INST_SCALE:
6884 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006885 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006886 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006887 case BC_INST_SCALE_FUNC:
6888 case BC_INST_LENGTH:
6889 case BC_INST_SQRT:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006890 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006891 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006892 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006893 r.t = BC_RESULT_CONSTANT;
6894 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006895 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006896 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006897 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006898 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006899 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006900 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006901 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006902 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006903 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006904 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006905 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006906 case BC_INST_PRINT:
6907 case BC_INST_PRINT_POP:
6908 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006909 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006910 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006911 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006912 r.t = BC_RESULT_STR;
6913 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006914 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006915 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006916 case BC_INST_POWER:
6917 case BC_INST_MULTIPLY:
6918 case BC_INST_DIVIDE:
6919 case BC_INST_MODULUS:
6920 case BC_INST_PLUS:
6921 case BC_INST_MINUS:
Denys Vlasenko259137d2018-12-11 19:42:05 +01006922 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006923 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006924 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006925 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006926 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006927 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006928 if (!bc_num_cmp(num, &G.prog.zero))
6929 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006930 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006931 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006932 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006933 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006934 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006935 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006936#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006937 case BC_INST_ASSIGN_POWER:
6938 case BC_INST_ASSIGN_MULTIPLY:
6939 case BC_INST_ASSIGN_DIVIDE:
6940 case BC_INST_ASSIGN_MODULUS:
6941 case BC_INST_ASSIGN_PLUS:
6942 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006943#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006944 case BC_INST_ASSIGN:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006945 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006946 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006947#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006948 case BC_INST_MODEXP:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006949 s = zbc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006950 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006951 case BC_INST_DIVMOD:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006952 s = zbc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006953 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006954 case BC_INST_EXECUTE:
6955 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006956 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006957 s = bc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006958 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006959 case BC_INST_PRINT_STACK: {
6960 size_t idx;
6961 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006962 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006963 if (s) break;
6964 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006965 break;
6966 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006967 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006968 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006969 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006970 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006971 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006972 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006973 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006974 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006975 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006976 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006977 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006978 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006979 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006980 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006981 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006982 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006983 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006984 ptr = bc_vec_item_rev(&G.prog.results, 0);
6985 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006986 memcpy(&r, ptr, sizeof(BcResult));
6987 memcpy(ptr, ptr2, sizeof(BcResult));
6988 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006989 break;
6990 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006991 case BC_INST_ASCIIFY:
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006992 s = zbc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006993 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006994 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006995 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006996 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006997 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006998 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006999 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01007000 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06007001 break;
7002 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01007003 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06007004 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01007005 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06007006 free(name);
7007 break;
7008 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007009 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007010 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007011 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01007012 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06007013 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06007014 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01007015 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06007016 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06007017#endif // ENABLE_DC
7018 }
7019
Denys Vlasenkod38af482018-12-04 19:11:02 +01007020 if (s || G_interrupt) {
7021 bc_program_reset();
Denys Vlasenko927a7d62018-12-09 02:24:14 +01007022 return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01007023 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007024
7025 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007026 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007027 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06007028 code = func->code.v;
7029 }
7030
Denys Vlasenko927a7d62018-12-09 02:24:14 +01007031 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007032}
7033
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007034#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01007035static void bc_vm_info(void)
7036{
7037 printf("%s "BB_VER"\n"
7038 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01007039 , applet_name);
7040}
7041
7042static void bc_args(char **argv)
7043{
7044 unsigned opts;
7045 int i;
7046
7047 GETOPT_RESET();
7048#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007049 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01007050 "warn\0" No_argument "w"
7051 "version\0" No_argument "v"
7052 "standard\0" No_argument "s"
7053 "quiet\0" No_argument "q"
7054 "mathlib\0" No_argument "l"
7055 "interactive\0" No_argument "i"
7056 );
7057#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007058 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01007059#endif
7060 if (getenv("POSIXLY_CORRECT"))
7061 option_mask32 |= BC_FLAG_S;
7062
Denys Vlasenko54214c32018-12-06 09:07:06 +01007063 if (opts & BC_FLAG_V) {
7064 bc_vm_info();
7065 exit(0);
7066 }
7067
7068 for (i = optind; argv[i]; ++i)
7069 bc_vec_push(&G.files, argv + i);
7070}
7071
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007072static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007073{
Gavin Howard01055ba2018-11-03 11:00:21 -06007074 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007075 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01007076 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06007077
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007078 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06007079
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007080 G.env_args = xstrdup(env_args);
7081 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06007082
7083 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06007084
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007085 while (*(buf = skip_whitespace(buf)) != '\0') {
7086 bc_vec_push(&v, &buf);
7087 buf = skip_non_whitespace(buf);
7088 if (!*buf)
7089 break;
7090 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06007091 }
7092
Denys Vlasenko54214c32018-12-06 09:07:06 +01007093 // NULL terminate, and pass argv[] so that first arg is argv[1]
7094 if (sizeof(int) == sizeof(char*)) {
7095 bc_vec_push(&v, &const_int_0);
7096 } else {
7097 static char *const nullptr = NULL;
7098 bc_vec_push(&v, &nullptr);
7099 }
7100 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06007101
7102 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06007103}
7104#endif // ENABLE_BC
7105
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007106static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06007107{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007108 char *lenv;
7109 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06007110
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007111 lenv = getenv(var);
7112 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06007113 if (!lenv) return len;
7114
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007115 len = bb_strtou(lenv, NULL, 10) - 1;
7116 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06007117 len = BC_NUM_PRINT_WIDTH;
7118
7119 return len;
7120}
7121
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007122static BcStatus bc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06007123{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007124 BcStatus s = bc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06007125
Gavin Howard01055ba2018-11-03 11:00:21 -06007126 if (s) return s;
7127
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007128 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007129 s = G.prs.parse(&G.prs);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01007130 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007131 }
7132
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007133 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007134 s = bc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01007135 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007136 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01007137 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06007138 }
7139
7140 return s;
7141}
7142
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007143static BcStatus bc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007144{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007145 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007146 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007147 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007148 BcFunc *main_func;
7149 BcInstPtr *ip;
7150
Denys Vlasenkodf515392018-12-02 19:27:48 +01007151 data = bc_read_file(file);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007152 if (!data) return bc_error_fmt("file '%s' is not text", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007153
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007154 sv_file = G.prog.file;
7155 G.prog.file = file;
7156 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007157 s = bc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007158 if (s) goto err;
7159
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007160 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007161 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007162
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007163 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007164 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007165
7166err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007167 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007168 free(data);
7169 return s;
7170}
7171
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007172static BcStatus bc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007173{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007174 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007175 BcVec buf, buffer;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007176 size_t str;
7177 bool comment;
Gavin Howard01055ba2018-11-03 11:00:21 -06007178
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007179 G.prog.file = NULL;
7180 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007181
Denys Vlasenko7d628012018-12-04 21:46:47 +01007182 bc_char_vec_init(&buffer);
7183 bc_char_vec_init(&buf);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01007184 bc_vec_pushZeroByte(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007185
7186 // This loop is complex because the vm tries not to send any lines that end
7187 // with a backslash to the parser. The reason for that is because the parser
7188 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7189 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007190 comment = false;
7191 str = 0;
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007192 while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) {
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007193 size_t len;
Gavin Howard01055ba2018-11-03 11:00:21 -06007194 char *string = buf.v;
7195
7196 len = buf.len - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06007197 if (len == 1) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007198 if (str && buf.v[0] == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007199 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007200 else if (buf.v[0] == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007201 str += 1;
7202 }
7203 else if (len > 1 || comment) {
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007204 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06007205 for (i = 0; i < len; ++i) {
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007206 bool notend = len > i + 1;
7207 char c = string[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06007208
7209 if (i - 1 > len || string[i - 1] != '\\') {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007210 if (G.sbgn == G.send)
7211 str ^= c == G.sbgn;
7212 else if (c == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007213 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007214 else if (c == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007215 str += 1;
7216 }
7217
7218 if (c == '/' && notend && !comment && string[i + 1] == '*') {
7219 comment = true;
7220 break;
7221 }
7222 else if (c == '*' && notend && comment && string[i + 1] == '/')
7223 comment = false;
7224 }
7225
7226 if (str || comment || string[len - 2] == '\\') {
7227 bc_vec_concat(&buffer, buf.v);
7228 continue;
7229 }
7230 }
7231
7232 bc_vec_concat(&buffer, buf.v);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007233 s = bc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007234 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007235 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007236 // Debug config, non-interactive mode:
7237 // return all the way back to main.
7238 // Non-debug builds do not come here, they exit.
7239 break;
7240 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007241 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007242
Denys Vlasenko7d628012018-12-04 21:46:47 +01007243 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007244 }
Denys Vlasenkof522dd92018-12-07 16:35:43 +01007245 if (s == BC_STATUS_EOF) // input EOF (^D) is not an error
7246 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007247
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007248 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007249 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007250 }
7251 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007252 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007253 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007254
Gavin Howard01055ba2018-11-03 11:00:21 -06007255 bc_vec_free(&buf);
7256 bc_vec_free(&buffer);
7257 return s;
7258}
7259
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007260#if ENABLE_BC
7261static const char bc_lib[] = {
7262 "scale=20"
7263"\n" "define e(x){"
7264"\n" "auto b,s,n,r,d,i,p,f,v"
7265"\n" "b=ibase"
7266"\n" "ibase=A"
7267"\n" "if(x<0){"
7268"\n" "n=1"
7269"\n" "x=-x"
7270"\n" "}"
7271"\n" "s=scale"
7272"\n" "r=6+s+0.44*x"
7273"\n" "scale=scale(x)+1"
7274"\n" "while(x>1){"
7275"\n" "d+=1"
7276"\n" "x/=2"
7277"\n" "scale+=1"
7278"\n" "}"
7279"\n" "scale=r"
7280"\n" "r=x+1"
7281"\n" "p=x"
7282"\n" "f=v=1"
7283"\n" "for(i=2;v!=0;++i){"
7284"\n" "p*=x"
7285"\n" "f*=i"
7286"\n" "v=p/f"
7287"\n" "r+=v"
7288"\n" "}"
7289"\n" "while((d--)!=0)r*=r"
7290"\n" "scale=s"
7291"\n" "ibase=b"
7292"\n" "if(n!=0)return(1/r)"
7293"\n" "return(r/1)"
7294"\n" "}"
7295"\n" "define l(x){"
7296"\n" "auto b,s,r,p,a,q,i,v"
7297"\n" "b=ibase"
7298"\n" "ibase=A"
7299"\n" "if(x<=0){"
7300"\n" "r=(1-10^scale)/1"
7301"\n" "ibase=b"
7302"\n" "return(r)"
7303"\n" "}"
7304"\n" "s=scale"
7305"\n" "scale+=6"
7306"\n" "p=2"
7307"\n" "while(x>=2){"
7308"\n" "p*=2"
7309"\n" "x=sqrt(x)"
7310"\n" "}"
7311"\n" "while(x<=0.5){"
7312"\n" "p*=2"
7313"\n" "x=sqrt(x)"
7314"\n" "}"
7315"\n" "r=a=(x-1)/(x+1)"
7316"\n" "q=a*a"
7317"\n" "v=1"
7318"\n" "for(i=3;v!=0;i+=2){"
7319"\n" "a*=q"
7320"\n" "v=a/i"
7321"\n" "r+=v"
7322"\n" "}"
7323"\n" "r*=p"
7324"\n" "scale=s"
7325"\n" "ibase=b"
7326"\n" "return(r/1)"
7327"\n" "}"
7328"\n" "define s(x){"
7329"\n" "auto b,s,r,n,a,q,i"
7330"\n" "b=ibase"
7331"\n" "ibase=A"
7332"\n" "s=scale"
7333"\n" "scale=1.1*s+2"
7334"\n" "a=a(1)"
7335"\n" "if(x<0){"
7336"\n" "n=1"
7337"\n" "x=-x"
7338"\n" "}"
7339"\n" "scale=0"
7340"\n" "q=(x/a+2)/4"
7341"\n" "x=x-4*q*a"
7342"\n" "if(q%2!=0)x=-x"
7343"\n" "scale=s+2"
7344"\n" "r=a=x"
7345"\n" "q=-x*x"
7346"\n" "for(i=3;a!=0;i+=2){"
7347"\n" "a*=q/(i*(i-1))"
7348"\n" "r+=a"
7349"\n" "}"
7350"\n" "scale=s"
7351"\n" "ibase=b"
7352"\n" "if(n!=0)return(-r/1)"
7353"\n" "return(r/1)"
7354"\n" "}"
7355"\n" "define c(x){"
7356"\n" "auto b,s"
7357"\n" "b=ibase"
7358"\n" "ibase=A"
7359"\n" "s=scale"
7360"\n" "scale*=1.2"
7361"\n" "x=s(2*a(1)+x)"
7362"\n" "scale=s"
7363"\n" "ibase=b"
7364"\n" "return(x/1)"
7365"\n" "}"
7366"\n" "define a(x){"
7367"\n" "auto b,s,r,n,a,m,t,f,i,u"
7368"\n" "b=ibase"
7369"\n" "ibase=A"
7370"\n" "n=1"
7371"\n" "if(x<0){"
7372"\n" "n=-1"
7373"\n" "x=-x"
7374"\n" "}"
7375"\n" "if(x==1){"
7376"\n" "if(scale<65){"
7377"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7378"\n" "}"
7379"\n" "}"
7380"\n" "if(x==.2){"
7381"\n" "if(scale<65){"
7382"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7383"\n" "}"
7384"\n" "}"
7385"\n" "s=scale"
7386"\n" "if(x>.2){"
7387"\n" "scale+=5"
7388"\n" "a=a(.2)"
7389"\n" "}"
7390"\n" "scale=s+3"
7391"\n" "while(x>.2){"
7392"\n" "m+=1"
7393"\n" "x=(x-.2)/(1+.2*x)"
7394"\n" "}"
7395"\n" "r=u=x"
7396"\n" "f=-x*x"
7397"\n" "t=1"
7398"\n" "for(i=3;t!=0;i+=2){"
7399"\n" "u*=f"
7400"\n" "t=u/i"
7401"\n" "r+=t"
7402"\n" "}"
7403"\n" "scale=s"
7404"\n" "ibase=b"
7405"\n" "return((m*a+r)/n)"
7406"\n" "}"
7407"\n" "define j(n,x){"
7408"\n" "auto b,s,o,a,i,v,f"
7409"\n" "b=ibase"
7410"\n" "ibase=A"
7411"\n" "s=scale"
7412"\n" "scale=0"
7413"\n" "n/=1"
7414"\n" "if(n<0){"
7415"\n" "n=-n"
7416"\n" "if(n%2==1)o=1"
7417"\n" "}"
7418"\n" "a=1"
7419"\n" "for(i=2;i<=n;++i)a*=i"
7420"\n" "scale=1.5*s"
7421"\n" "a=(x^n)/2^n/a"
7422"\n" "r=v=1"
7423"\n" "f=-x*x/4"
7424"\n" "scale=scale+length(a)-scale(a)"
7425"\n" "for(i=1;v!=0;++i){"
7426"\n" "v=v*f/i/(n+i)"
7427"\n" "r+=v"
7428"\n" "}"
7429"\n" "scale=s"
7430"\n" "ibase=b"
7431"\n" "if(o!=0)a=-a"
7432"\n" "return(a*r/1)"
7433"\n" "}"
7434};
7435#endif // ENABLE_BC
7436
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007437static BcStatus bc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007438{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007439 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007440 size_t i;
7441
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007442#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007443 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007444
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007445 // We know that internal library is not buggy,
7446 // thus error checking is normally disabled.
7447# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007448 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007449 s = bc_parse_text(&G.prs, bc_lib);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007450 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007451
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007452 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007453 s = G.prs.parse(&G.prs);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007454 if (DEBUG_LIB && s) return s;
7455 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007456 s = bc_program_exec();
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007457 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007458 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007459#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007460
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007461 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007462 for (i = 0; !s && i < G.files.len; ++i)
7463 s = bc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007464 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7465 // Debug config, non-interactive mode:
7466 // return all the way back to main.
7467 // Non-debug builds do not come here, they exit.
7468 return s;
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007469 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007470
Denys Vlasenko91cde952018-12-10 20:56:08 +01007471 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007472 s = bc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007473
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007474 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
7475 s = bc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007476
Denys Vlasenko00d77792018-11-30 23:13:42 +01007477 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007478}
7479
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007480#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007481static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007482{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007483 bc_num_free(&G.prog.ib);
7484 bc_num_free(&G.prog.ob);
7485 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007486# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007487 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007488# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007489 bc_vec_free(&G.prog.fns);
7490 bc_vec_free(&G.prog.fn_map);
7491 bc_vec_free(&G.prog.vars);
7492 bc_vec_free(&G.prog.var_map);
7493 bc_vec_free(&G.prog.arrs);
7494 bc_vec_free(&G.prog.arr_map);
7495 bc_vec_free(&G.prog.strs);
7496 bc_vec_free(&G.prog.consts);
7497 bc_vec_free(&G.prog.results);
7498 bc_vec_free(&G.prog.stack);
7499 bc_num_free(&G.prog.last);
7500 bc_num_free(&G.prog.zero);
7501 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007502}
7503
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007504static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007505{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007506 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007507 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007508 bc_parse_free(&G.prs);
7509 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007510}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007511#endif
7512
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007513static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007514{
7515 size_t idx;
7516 BcInstPtr ip;
7517
7518 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7519 memset(&ip, 0, sizeof(BcInstPtr));
7520
7521 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007522 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007523 bc_num_ten(&G.prog.ib);
7524 G.prog.ib_t = 10;
7525
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007526 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007527 bc_num_ten(&G.prog.ob);
7528 G.prog.ob_t = 10;
7529
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007530 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007531 bc_num_ten(&G.prog.hexb);
7532 G.prog.hexb.num[0] = 6;
7533
7534#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007535 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007536 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7537#endif
7538
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007539 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007540 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007541
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007542 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007543 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007544
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007545 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007546 bc_num_one(&G.prog.one);
7547
7548 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007549 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007550
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007551 bc_program_addFunc(xstrdup("(main)"), &idx);
7552 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007553
7554 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007555 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007556
7557 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007558 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007559
7560 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7561 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7562 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7563 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7564 bc_vec_push(&G.prog.stack, &ip);
7565}
Gavin Howard01055ba2018-11-03 11:00:21 -06007566
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007567static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007568{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007569#if ENABLE_FEATURE_EDITING
7570 G.line_input_state = new_line_input_t(DO_HISTORY);
7571#endif
7572 G.prog.len = bc_vm_envLen(env_len);
7573
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007574 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007575 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007576 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007577 bc_program_init();
Denys Vlasenkof0f069b2018-12-11 23:22:52 +01007578 common_parse_init(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007579
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007580 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007581#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007582 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007583 // With SA_RESTART, most system calls will restart
7584 // (IOW: they won't fail with EINTR).
7585 // In particular, this means ^C won't cause
7586 // stdout to get into "error state" if SIGINT hits
7587 // within write() syscall.
7588 // The downside is that ^C while line input is taken
7589 // will only be handled after [Enter] since read()
7590 // from stdin is not interrupted by ^C either,
7591 // it restarts, thus fgetc() does not return on ^C.
7592 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7593
7594 // Without SA_RESTART, this exhibits a bug:
7595 // "while (1) print 1" and try ^C-ing it.
7596 // Intermittently, instead of returning to input line,
7597 // you'll get "output error: Interrupted system call"
7598 // and exit.
7599 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007600#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007601 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007602 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007603 return 0; // "not a tty"
7604}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007605
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007606static BcStatus bc_vm_run(void)
7607{
7608 BcStatus st = bc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007609#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007610 if (G_exiting) // it was actually "halt" or "quit"
7611 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007612 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007613# if ENABLE_FEATURE_EDITING
7614 free_line_input_t(G.line_input_state);
7615# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007616 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007617#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007618 return st;
7619}
7620
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007621#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007622int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007623int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007624{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007625 int is_tty;
7626
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007627 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007628 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007629
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007630 is_tty = bc_vm_init("BC_LINE_LENGTH");
7631
7632 bc_args(argv);
7633
7634 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7635 bc_vm_info();
7636
7637 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007638}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007639#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007640
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007641#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007642int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007643int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007644{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007645 int noscript;
7646
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007647 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007648 G.sbgn = '[';
7649 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007650 /*
7651 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7652 * 1 char wider than bc from the same package.
7653 * Both default width, and xC_LINE_LENGTH=N are wider:
7654 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7655 * 1234\
7656 * 56
7657 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7658 * 123\
7659 * 456
7660 * Do the same, or it's a bug?
7661 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007662 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007663
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007664 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7665 noscript = BC_FLAG_I;
7666 for (;;) {
7667 int n = getopt(argc, argv, "e:f:x");
7668 if (n <= 0)
7669 break;
7670 switch (n) {
7671 case 'e':
7672 noscript = 0;
7673 n = bc_vm_process(optarg);
7674 if (n) return n;
7675 break;
7676 case 'f':
7677 noscript = 0;
7678 bc_vm_file(optarg);
7679 break;
7680 case 'x':
7681 option_mask32 |= DC_FLAG_X;
7682 break;
7683 default:
7684 bb_show_usage();
7685 }
7686 }
7687 argv += optind;
7688
7689 while (*argv) {
7690 noscript = 0;
7691 bc_vec_push(&G.files, argv++);
7692 }
7693
7694 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7695
7696 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007697}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007698#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007699
7700#endif // not DC_SMALL