blob: 70db2ce3d240c21900f73cc8deb94d96f251d777 [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 Vlasenko12b9eaf2018-12-11 23:50:14 +01002997 ERROR_RETURN(if (s) return s;)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002998 }
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) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003099 case '\0':
3100 case '\n':
Gavin Howard01055ba2018-11-03 11:00:21 -06003101 l->newline = true;
3102 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3103 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003104 case '\t':
3105 case '\v':
3106 case '\f':
3107 case '\r':
3108 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003109 bc_lex_whitespace(l);
3110 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003111 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003112 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003113 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003114 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003115 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06003116 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003117 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003118 case '"':
Gavin Howard01055ba2018-11-03 11:00:21 -06003119 s = bc_lex_string(l);
3120 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003121 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003122 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003123 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06003124 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003126 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003127 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3128 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003129 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003130 c2 = l->buf[l->i];
3131 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003132 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003133 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06003134 ++l->i;
3135 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003136 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003138 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003139 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003140 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 case '(':
3142 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003143 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3144 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003146 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3147 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003148 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003149 c2 = l->buf[l->i];
3150 if (c2 == '+') {
3151 ++l->i;
3152 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003153 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003154 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3155 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003157 l->t.t = BC_LEX_COMMA;
3158 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003159 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003160 c2 = l->buf[l->i];
3161 if (c2 == '-') {
3162 ++l->i;
3163 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003164 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003165 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3166 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003167 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003168 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003169 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003170 else {
3171 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003172 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003173 }
3174 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003175 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003176 c2 = l->buf[l->i];
3177 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003178 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003179 else
3180 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3181 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003182 case '0':
3183 case '1':
3184 case '2':
3185 case '3':
3186 case '4':
3187 case '5':
3188 case '6':
3189 case '7':
3190 case '8':
3191 case '9':
3192 case 'A':
3193 case 'B':
3194 case 'C':
3195 case 'D':
3196 case 'E':
3197 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003198 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003199 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003201 l->t.t = BC_LEX_SCOLON;
3202 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003204 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3205 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003206 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003207 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3208 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003209 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003210 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3211 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003212 case '[':
3213 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003214 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3215 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003217 if (l->buf[l->i] == '\n') {
3218 l->t.t = BC_LEX_WHITESPACE;
3219 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003220 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003221 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003223 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003224 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3225 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003226 case 'a':
3227 case 'b':
3228 case 'c':
3229 case 'd':
3230 case 'e':
3231 case 'f':
3232 case 'g':
3233 case 'h':
3234 case 'i':
3235 case 'j':
3236 case 'k':
3237 case 'l':
3238 case 'm':
3239 case 'n':
3240 case 'o':
3241 case 'p':
3242 case 'q':
3243 case 'r':
3244 case 's':
3245 case 't':
3246 case 'u':
3247 case 'v':
3248 case 'w':
3249 case 'x':
3250 case 'y':
3251 case 'z':
Gavin Howard01055ba2018-11-03 11:00:21 -06003252 s = bc_lex_identifier(l);
3253 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003254 case '{':
3255 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003256 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3257 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003258 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003259 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003260 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003261 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003262 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06003263 ++l->i;
3264 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003265 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003266 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003267 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003268 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003269 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003270 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003271 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003272 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003273 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003274 }
3275
3276 return s;
3277}
3278#endif // ENABLE_BC
3279
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003280#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003281static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003282{
Gavin Howard01055ba2018-11-03 11:00:21 -06003283 if (isspace(l->buf[l->i - 1])) {
3284 bc_lex_whitespace(l);
3285 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003286 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003287 RETURN_STATUS(bc_error("extended register"));
3288 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003289 }
3290 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003291 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003292 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003293 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003294 l->t.t = BC_LEX_NAME;
3295 }
3296
Denys Vlasenko29301232018-12-11 15:29:32 +01003297 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003298}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003299#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003300# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003301#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003302
Denys Vlasenko29301232018-12-11 15:29:32 +01003303static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003304{
3305 size_t depth = 1, nls = 0, i = l->i;
3306 char c;
3307
3308 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003309 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003310
3311 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3312
3313 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3314 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3315 nls += (c == '\n');
3316
3317 if (depth) bc_vec_push(&l->t.v, &c);
3318 }
3319
3320 if (c == '\0') {
3321 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003322 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003323 }
3324
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003325 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003326 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3327 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3328 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003329 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003330 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003331
3332 l->i = i;
3333 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003334 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003335
Denys Vlasenko29301232018-12-11 15:29:32 +01003336 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003337}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003338#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003339# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003340#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003341
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01003342static FAST_FUNC BcStatus dc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003343{
3344 BcStatus s = BC_STATUS_SUCCESS;
3345 char c = l->buf[l->i++], c2;
3346 size_t i;
3347
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003348 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3349 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko29301232018-12-11 15:29:32 +01003350 return zdc_lex_register(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003351 }
3352
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003353 if (c >= '%' && c <= '~'
3354 && (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID
3355 ) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003356 return s;
3357 }
3358
3359 // This is the workhorse of the lexer.
3360 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003361 case '\0':
Gavin Howard01055ba2018-11-03 11:00:21 -06003362 l->t.t = BC_LEX_EOF;
3363 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003364 case '\n':
3365 case '\t':
3366 case '\v':
3367 case '\f':
3368 case '\r':
3369 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003370 l->newline = (c == '\n');
3371 bc_lex_whitespace(l);
3372 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003373 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003374 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003375 if (c2 == '=')
3376 l->t.t = BC_LEX_OP_REL_NE;
3377 else if (c2 == '<')
3378 l->t.t = BC_LEX_OP_REL_LE;
3379 else if (c2 == '>')
3380 l->t.t = BC_LEX_OP_REL_GE;
3381 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003382 return bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003383 ++l->i;
3384 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003385 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003386 bc_lex_lineComment(l);
3387 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003388 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003389 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003390 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003391 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003392 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003393 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003394 case '0':
3395 case '1':
3396 case '2':
3397 case '3':
3398 case '4':
3399 case '5':
3400 case '6':
3401 case '7':
3402 case '8':
3403 case '9':
3404 case 'A':
3405 case 'B':
3406 case 'C':
3407 case 'D':
3408 case 'E':
3409 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003410 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003411 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003412 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003413 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003414 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003415 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003416 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003417 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003418 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003419 }
3420
3421 return s;
3422}
3423#endif // ENABLE_DC
3424
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003425static void bc_program_addFunc(char *name, size_t *idx);
3426
Gavin Howard01055ba2018-11-03 11:00:21 -06003427static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3428{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003429 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003430 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003431}
3432
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003433#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3434
Gavin Howard01055ba2018-11-03 11:00:21 -06003435static void bc_parse_pushName(BcParse *p, char *name)
3436{
3437 size_t i = 0, len = strlen(name);
3438
3439 for (; i < len; ++i) bc_parse_push(p, name[i]);
3440 bc_parse_push(p, BC_PARSE_STREND);
3441
3442 free(name);
3443}
3444
3445static void bc_parse_pushIndex(BcParse *p, size_t idx)
3446{
3447 unsigned char amt, i, nums[sizeof(size_t)];
3448
3449 for (amt = 0; idx; ++amt) {
3450 nums[amt] = (char) idx;
3451 idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT;
3452 }
3453
3454 bc_parse_push(p, amt);
3455 for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]);
3456}
3457
3458static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3459{
3460 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003461 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003462
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003463 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003464
3465 bc_parse_push(p, BC_INST_NUM);
3466 bc_parse_pushIndex(p, idx);
3467
3468 ++(*nexs);
3469 (*prev) = BC_INST_NUM;
3470}
3471
3472static BcStatus bc_parse_text(BcParse *p, const char *text)
3473{
3474 BcStatus s;
3475
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003476 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003477
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003478 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003479 p->l.t.t = BC_LEX_INVALID;
3480 s = p->parse(p);
3481 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003482 if (!BC_PARSE_CAN_EXEC(p))
3483 return bc_error("file is not executable");
Gavin Howard01055ba2018-11-03 11:00:21 -06003484 }
3485
3486 return bc_lex_text(&p->l, text);
3487}
3488
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003489// Called when parsing or execution detects a failure,
3490// resets execution structures.
3491static void bc_program_reset(void)
3492{
3493 BcFunc *f;
3494 BcInstPtr *ip;
3495
3496 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3497 bc_vec_pop_all(&G.prog.results);
3498
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003499 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003500 ip = bc_vec_top(&G.prog.stack);
3501 ip->idx = f->code.len;
3502}
3503
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003504#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003505 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003506
Denys Vlasenkod38af482018-12-04 19:11:02 +01003507// Called when bc/dc_parse_parse() detects a failure,
3508// resets parsing structures.
3509static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003510{
3511 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003512 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003513 bc_vec_pop_all(&p->func->code);
3514 bc_vec_pop_all(&p->func->autos);
3515 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003516
3517 bc_parse_updateFunc(p, BC_PROG_MAIN);
3518 }
3519
3520 p->l.i = p->l.len;
3521 p->l.t.t = BC_LEX_EOF;
3522 p->auto_part = (p->nbraces = 0);
3523
3524 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003525 bc_vec_pop_all(&p->exits);
3526 bc_vec_pop_all(&p->conds);
3527 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003528
Denys Vlasenkod38af482018-12-04 19:11:02 +01003529 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003530}
3531
3532static void bc_parse_free(BcParse *p)
3533{
3534 bc_vec_free(&p->flags);
3535 bc_vec_free(&p->exits);
3536 bc_vec_free(&p->conds);
3537 bc_vec_free(&p->ops);
3538 bc_lex_free(&p->l);
3539}
3540
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003541static void bc_parse_create(BcParse *p, size_t func,
Gavin Howard01055ba2018-11-03 11:00:21 -06003542 BcParseParse parse, BcLexNext next)
3543{
3544 memset(p, 0, sizeof(BcParse));
3545
3546 bc_lex_init(&p->l, next);
3547 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3548 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3549 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003550 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003551 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3552
3553 p->parse = parse;
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003554 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003555 bc_parse_updateFunc(p, func);
3556}
3557
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003558#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003559
3560#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3561#define BC_PARSE_LEAF(p, rparen) \
3562 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3563 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3564
3565// We can calculate the conversion between tokens and exprs by subtracting the
3566// position of the first operator in the lex enum and adding the position of the
3567// first in the expr enum. Note: This only works for binary operators.
3568#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3569
Gavin Howard01055ba2018-11-03 11:00:21 -06003570static BcStatus bc_parse_else(BcParse *p);
3571static BcStatus bc_parse_stmt(BcParse *p);
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003572static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003573static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Gavin Howard01055ba2018-11-03 11:00:21 -06003574
3575static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3576 size_t *nexprs, bool next)
3577{
3578 BcStatus s = BC_STATUS_SUCCESS;
3579 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003580 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3581 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003582
3583 while (p->ops.len > start) {
3584
3585 t = BC_PARSE_TOP_OP(p);
3586 if (t == BC_LEX_LPAREN) break;
3587
Denys Vlasenko65437582018-12-05 19:37:19 +01003588 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003589 if (l >= r && (l != r || !left)) break;
3590
3591 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3592 bc_vec_pop(&p->ops);
3593 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3594 }
3595
3596 bc_vec_push(&p->ops, &type);
3597 if (next) s = bc_lex_next(&p->l);
3598
3599 return s;
3600}
3601
3602static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
3603{
3604 BcLexType top;
3605
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003606 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003607 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003608 top = BC_PARSE_TOP_OP(p);
3609
3610 while (top != BC_LEX_LPAREN) {
3611
3612 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3613
3614 bc_vec_pop(&p->ops);
3615 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3616
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003617 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003618 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003619 top = BC_PARSE_TOP_OP(p);
3620 }
3621
3622 bc_vec_pop(&p->ops);
3623
3624 return bc_lex_next(&p->l);
3625}
3626
3627static BcStatus bc_parse_params(BcParse *p, uint8_t flags)
3628{
3629 BcStatus s;
3630 bool comma = false;
3631 size_t nparams;
3632
3633 s = bc_lex_next(&p->l);
3634 if (s) return s;
3635
3636 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
3637
3638 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3639 s = bc_parse_expr(p, flags, bc_parse_next_param);
3640 if (s) return s;
3641
3642 comma = p->l.t.t == BC_LEX_COMMA;
3643 if (comma) {
3644 s = bc_lex_next(&p->l);
3645 if (s) return s;
3646 }
3647 }
3648
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003649 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003650 bc_parse_push(p, BC_INST_CALL);
3651 bc_parse_pushIndex(p, nparams);
3652
3653 return BC_STATUS_SUCCESS;
3654}
3655
3656static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags)
3657{
3658 BcStatus s;
3659 BcId entry, *entry_ptr;
3660 size_t idx;
3661
3662 entry.name = name;
3663
3664 s = bc_parse_params(p, flags);
3665 if (s) goto err;
3666
3667 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003668 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003669 goto err;
3670 }
3671
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003672 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003673
3674 if (idx == BC_VEC_INVALID_IDX) {
3675 name = xstrdup(entry.name);
3676 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003677 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003678 free(entry.name);
3679 }
3680 else
3681 free(name);
3682
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003683 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003684 bc_parse_pushIndex(p, entry_ptr->idx);
3685
3686 return bc_lex_next(&p->l);
3687
3688err:
3689 free(name);
3690 return s;
3691}
3692
3693static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
3694{
3695 BcStatus s;
3696 char *name;
3697
3698 name = xstrdup(p->l.t.v.v);
3699 s = bc_lex_next(&p->l);
3700 if (s) goto err;
3701
3702 if (p->l.t.t == BC_LEX_LBRACKET) {
3703
3704 s = bc_lex_next(&p->l);
3705 if (s) goto err;
3706
3707 if (p->l.t.t == BC_LEX_RBRACKET) {
3708
3709 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003710 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003711 goto err;
3712 }
3713
3714 *type = BC_INST_ARRAY;
3715 }
3716 else {
3717
3718 *type = BC_INST_ARRAY_ELEM;
3719
3720 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3721 s = bc_parse_expr(p, flags, bc_parse_next_elem);
3722 if (s) goto err;
3723 }
3724
3725 s = bc_lex_next(&p->l);
3726 if (s) goto err;
3727 bc_parse_push(p, *type);
3728 bc_parse_pushName(p, name);
3729 }
3730 else if (p->l.t.t == BC_LEX_LPAREN) {
3731
3732 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003733 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003734 goto err;
3735 }
3736
3737 *type = BC_INST_CALL;
3738 s = bc_parse_call(p, name, flags);
3739 }
3740 else {
3741 *type = BC_INST_VAR;
3742 bc_parse_push(p, BC_INST_VAR);
3743 bc_parse_pushName(p, name);
3744 }
3745
3746 return s;
3747
3748err:
3749 free(name);
3750 return s;
3751}
3752
3753static BcStatus bc_parse_read(BcParse *p)
3754{
3755 BcStatus s;
3756
3757 s = bc_lex_next(&p->l);
3758 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003759 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003760
3761 s = bc_lex_next(&p->l);
3762 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003763 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003764
3765 bc_parse_push(p, BC_INST_READ);
3766
3767 return bc_lex_next(&p->l);
3768}
3769
3770static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
3771 BcInst *prev)
3772{
3773 BcStatus s;
3774
3775 s = bc_lex_next(&p->l);
3776 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003777 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003778
3779 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3780
3781 s = bc_lex_next(&p->l);
3782 if (s) return s;
3783
3784 s = bc_parse_expr(p, flags, bc_parse_next_rel);
3785 if (s) return s;
3786
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003787 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003788
3789 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3790 bc_parse_push(p, *prev);
3791
3792 return bc_lex_next(&p->l);
3793}
3794
3795static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
3796{
3797 BcStatus s;
3798
3799 s = bc_lex_next(&p->l);
3800 if (s) return s;
3801
3802 if (p->l.t.t != BC_LEX_LPAREN) {
3803 *type = BC_INST_SCALE;
3804 bc_parse_push(p, BC_INST_SCALE);
3805 return BC_STATUS_SUCCESS;
3806 }
3807
3808 *type = BC_INST_SCALE_FUNC;
3809 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3810
3811 s = bc_lex_next(&p->l);
3812 if (s) return s;
3813
3814 s = bc_parse_expr(p, flags, bc_parse_next_rel);
3815 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003816 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003817 bc_parse_push(p, BC_INST_SCALE_FUNC);
3818
3819 return bc_lex_next(&p->l);
3820}
3821
3822static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
3823 size_t *nexprs, uint8_t flags)
3824{
3825 BcStatus s;
3826 BcLexType type;
3827 char inst;
3828 BcInst etype = *prev;
3829
3830 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
3831 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
3832 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
3833 {
3834 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3835 bc_parse_push(p, inst);
3836 s = bc_lex_next(&p->l);
3837 }
3838 else {
3839
3840 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3841 *paren_expr = true;
3842
3843 s = bc_lex_next(&p->l);
3844 if (s) return s;
3845 type = p->l.t.t;
3846
3847 // Because we parse the next part of the expression
3848 // right here, we need to increment this.
3849 *nexprs = *nexprs + 1;
3850
3851 switch (type) {
3852
3853 case BC_LEX_NAME:
3854 {
3855 s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
3856 break;
3857 }
3858
3859 case BC_LEX_KEY_IBASE:
3860 case BC_LEX_KEY_LAST:
3861 case BC_LEX_KEY_OBASE:
3862 {
3863 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
3864 s = bc_lex_next(&p->l);
3865 break;
3866 }
3867
3868 case BC_LEX_KEY_SCALE:
3869 {
3870 s = bc_lex_next(&p->l);
3871 if (s) return s;
3872 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003873 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003874 else
3875 bc_parse_push(p, BC_INST_SCALE);
3876 break;
3877 }
3878
3879 default:
3880 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003881 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003882 break;
3883 }
3884 }
3885
3886 if (!s) bc_parse_push(p, inst);
3887 }
3888
3889 return s;
3890}
3891
3892static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
3893 bool rparen, size_t *nexprs)
3894{
3895 BcStatus s;
3896 BcLexType type;
3897 BcInst etype = *prev;
3898
3899 s = bc_lex_next(&p->l);
3900 if (s) return s;
3901
3902 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
3903 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
3904 BC_LEX_OP_MINUS :
3905 BC_LEX_NEG;
3906 *prev = BC_PARSE_TOKEN_INST(type);
3907
3908 // We can just push onto the op stack because this is the largest
3909 // precedence operator that gets pushed. Inc/dec does not.
3910 if (type != BC_LEX_OP_MINUS)
3911 bc_vec_push(&p->ops, &type);
3912 else
3913 s = bc_parse_operator(p, type, ops_bgn, nexprs, false);
3914
3915 return s;
3916}
3917
3918static BcStatus bc_parse_string(BcParse *p, char inst)
3919{
3920 char *str = xstrdup(p->l.t.v.v);
3921
3922 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003923 bc_parse_pushIndex(p, G.prog.strs.len);
3924 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06003925 bc_parse_push(p, inst);
3926
3927 return bc_lex_next(&p->l);
3928}
3929
3930static BcStatus bc_parse_print(BcParse *p)
3931{
3932 BcStatus s;
3933 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003934 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06003935
3936 s = bc_lex_next(&p->l);
3937 if (s) return s;
3938
3939 type = p->l.t.t;
3940
3941 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003942 return bc_error("bad print statement");
Gavin Howard01055ba2018-11-03 11:00:21 -06003943
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003944 comma = false;
3945 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003946
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003947 if (type == BC_LEX_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003948 s = bc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003949 if (s) return s;
3950 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003951 s = bc_parse_expr(p, 0, bc_parse_next_print);
3952 if (s) return s;
3953 bc_parse_push(p, BC_INST_PRINT_POP);
3954 }
3955
Gavin Howard01055ba2018-11-03 11:00:21 -06003956 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003957 if (comma) {
3958 s = bc_lex_next(&p->l);
3959 if (s) return s;
3960 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003961 type = p->l.t.t;
3962 }
3963
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003964 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003965
3966 return bc_lex_next(&p->l);
3967}
3968
3969static BcStatus bc_parse_return(BcParse *p)
3970{
3971 BcStatus s;
3972 BcLexType t;
3973 bool paren;
3974
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003975 if (!BC_PARSE_FUNC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003976
3977 s = bc_lex_next(&p->l);
3978 if (s) return s;
3979
3980 t = p->l.t.t;
3981 paren = t == BC_LEX_LPAREN;
3982
3983 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
3984 bc_parse_push(p, BC_INST_RET0);
3985 else {
3986
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003987 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003988 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003989 bc_parse_push(p, BC_INST_RET0);
3990 s = bc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003991 }
Denys Vlasenko452df922018-12-05 20:28:26 +01003992 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06003993
3994 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01003995 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003996 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06003997 }
3998
3999 bc_parse_push(p, BC_INST_RET);
4000 }
4001
4002 return s;
4003}
4004
4005static BcStatus bc_parse_endBody(BcParse *p, bool brace)
4006{
4007 BcStatus s = BC_STATUS_SUCCESS;
4008
4009 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004010 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004011
4012 if (brace) {
4013
4014 if (p->l.t.t == BC_LEX_RBRACE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004015 if (!p->nbraces) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004016 --p->nbraces;
4017 s = bc_lex_next(&p->l);
4018 if (s) return s;
4019 }
4020 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004021 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004022 }
4023
4024 if (BC_PARSE_IF(p)) {
4025
4026 uint8_t *flag_ptr;
4027
4028 while (p->l.t.t == BC_LEX_NLINE) {
4029 s = bc_lex_next(&p->l);
4030 if (s) return s;
4031 }
4032
4033 bc_vec_pop(&p->flags);
4034
4035 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4036 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4037
4038 if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p);
4039 }
4040 else if (BC_PARSE_ELSE(p)) {
4041
4042 BcInstPtr *ip;
4043 size_t *label;
4044
4045 bc_vec_pop(&p->flags);
4046
4047 ip = bc_vec_top(&p->exits);
4048 label = bc_vec_item(&p->func->labels, ip->idx);
4049 *label = p->func->code.len;
4050
4051 bc_vec_pop(&p->exits);
4052 }
4053 else if (BC_PARSE_FUNC_INNER(p)) {
4054 bc_parse_push(p, BC_INST_RET0);
4055 bc_parse_updateFunc(p, BC_PROG_MAIN);
4056 bc_vec_pop(&p->flags);
4057 }
4058 else {
4059
4060 BcInstPtr *ip = bc_vec_top(&p->exits);
4061 size_t *label = bc_vec_top(&p->conds);
4062
4063 bc_parse_push(p, BC_INST_JUMP);
4064 bc_parse_pushIndex(p, *label);
4065
4066 label = bc_vec_item(&p->func->labels, ip->idx);
4067 *label = p->func->code.len;
4068
4069 bc_vec_pop(&p->flags);
4070 bc_vec_pop(&p->exits);
4071 bc_vec_pop(&p->conds);
4072 }
4073
4074 return s;
4075}
4076
4077static void bc_parse_startBody(BcParse *p, uint8_t flags)
4078{
4079 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4080 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4081 flags |= BC_PARSE_FLAG_BODY;
4082 bc_vec_push(&p->flags, &flags);
4083}
4084
4085static void bc_parse_noElse(BcParse *p)
4086{
4087 BcInstPtr *ip;
4088 size_t *label;
4089 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4090
4091 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4092
4093 ip = bc_vec_top(&p->exits);
4094 label = bc_vec_item(&p->func->labels, ip->idx);
4095 *label = p->func->code.len;
4096
4097 bc_vec_pop(&p->exits);
4098}
4099
4100static BcStatus bc_parse_if(BcParse *p)
4101{
4102 BcStatus s;
4103 BcInstPtr ip;
4104
4105 s = bc_lex_next(&p->l);
4106 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004107 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004108
4109 s = bc_lex_next(&p->l);
4110 if (s) return s;
4111 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4112 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004113 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004114
4115 s = bc_lex_next(&p->l);
4116 if (s) return s;
4117 bc_parse_push(p, BC_INST_JUMP_ZERO);
4118
4119 ip.idx = p->func->labels.len;
4120 ip.func = ip.len = 0;
4121
4122 bc_parse_pushIndex(p, ip.idx);
4123 bc_vec_push(&p->exits, &ip);
4124 bc_vec_push(&p->func->labels, &ip.idx);
4125 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4126
4127 return BC_STATUS_SUCCESS;
4128}
4129
4130static BcStatus bc_parse_else(BcParse *p)
4131{
4132 BcInstPtr ip;
4133
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004134 if (!BC_PARSE_IF_END(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004135
4136 ip.idx = p->func->labels.len;
4137 ip.func = ip.len = 0;
4138
4139 bc_parse_push(p, BC_INST_JUMP);
4140 bc_parse_pushIndex(p, ip.idx);
4141
4142 bc_parse_noElse(p);
4143
4144 bc_vec_push(&p->exits, &ip);
4145 bc_vec_push(&p->func->labels, &ip.idx);
4146 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4147
4148 return bc_lex_next(&p->l);
4149}
4150
4151static BcStatus bc_parse_while(BcParse *p)
4152{
4153 BcStatus s;
4154 BcInstPtr ip;
4155
4156 s = bc_lex_next(&p->l);
4157 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004158 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004159 s = bc_lex_next(&p->l);
4160 if (s) return s;
4161
4162 ip.idx = p->func->labels.len;
4163
4164 bc_vec_push(&p->func->labels, &p->func->code.len);
4165 bc_vec_push(&p->conds, &ip.idx);
4166
4167 ip.idx = p->func->labels.len;
4168 ip.func = 1;
4169 ip.len = 0;
4170
4171 bc_vec_push(&p->exits, &ip);
4172 bc_vec_push(&p->func->labels, &ip.idx);
4173
4174 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4175 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004176 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004177 s = bc_lex_next(&p->l);
4178 if (s) return s;
4179
4180 bc_parse_push(p, BC_INST_JUMP_ZERO);
4181 bc_parse_pushIndex(p, ip.idx);
4182 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4183
4184 return BC_STATUS_SUCCESS;
4185}
4186
4187static BcStatus bc_parse_for(BcParse *p)
4188{
4189 BcStatus s;
4190 BcInstPtr ip;
4191 size_t cond_idx, exit_idx, body_idx, update_idx;
4192
4193 s = bc_lex_next(&p->l);
4194 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004195 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004196 s = bc_lex_next(&p->l);
4197 if (s) return s;
4198
4199 if (p->l.t.t != BC_LEX_SCOLON)
4200 s = bc_parse_expr(p, 0, bc_parse_next_for);
4201 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004202 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004203
4204 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004205 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004206 s = bc_lex_next(&p->l);
4207 if (s) return s;
4208
4209 cond_idx = p->func->labels.len;
4210 update_idx = cond_idx + 1;
4211 body_idx = update_idx + 1;
4212 exit_idx = body_idx + 1;
4213
4214 bc_vec_push(&p->func->labels, &p->func->code.len);
4215
4216 if (p->l.t.t != BC_LEX_SCOLON)
4217 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
4218 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004219 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004220
4221 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004222 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004223
4224 s = bc_lex_next(&p->l);
4225 if (s) return s;
4226
4227 bc_parse_push(p, BC_INST_JUMP_ZERO);
4228 bc_parse_pushIndex(p, exit_idx);
4229 bc_parse_push(p, BC_INST_JUMP);
4230 bc_parse_pushIndex(p, body_idx);
4231
4232 ip.idx = p->func->labels.len;
4233
4234 bc_vec_push(&p->conds, &update_idx);
4235 bc_vec_push(&p->func->labels, &p->func->code.len);
4236
4237 if (p->l.t.t != BC_LEX_RPAREN)
4238 s = bc_parse_expr(p, 0, bc_parse_next_rel);
4239 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004240 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004241
4242 if (s) return s;
4243
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004244 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004245 bc_parse_push(p, BC_INST_JUMP);
4246 bc_parse_pushIndex(p, cond_idx);
4247 bc_vec_push(&p->func->labels, &p->func->code.len);
4248
4249 ip.idx = exit_idx;
4250 ip.func = 1;
4251 ip.len = 0;
4252
4253 bc_vec_push(&p->exits, &ip);
4254 bc_vec_push(&p->func->labels, &ip.idx);
4255 bc_lex_next(&p->l);
4256 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4257
4258 return BC_STATUS_SUCCESS;
4259}
4260
4261static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type)
4262{
4263 BcStatus s;
4264 size_t i;
4265 BcInstPtr *ip;
4266
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004267 if (!BC_PARSE_LOOP(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004268
4269 if (type == BC_LEX_KEY_BREAK) {
4270
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004271 if (p->exits.len == 0) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004272
4273 i = p->exits.len - 1;
4274 ip = bc_vec_item(&p->exits, i);
4275
4276 while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004277 if (i >= p->exits.len && !ip->func) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004278
4279 i = ip->idx;
4280 }
4281 else
4282 i = *((size_t *) bc_vec_top(&p->conds));
4283
4284 bc_parse_push(p, BC_INST_JUMP);
4285 bc_parse_pushIndex(p, i);
4286
4287 s = bc_lex_next(&p->l);
4288 if (s) return s;
4289
4290 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004291 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004292
4293 return bc_lex_next(&p->l);
4294}
4295
4296static BcStatus bc_parse_func(BcParse *p)
4297{
4298 BcStatus s;
4299 bool var, comma = false;
4300 uint8_t flags;
4301 char *name;
4302
4303 s = bc_lex_next(&p->l);
4304 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004305 if (p->l.t.t != BC_LEX_NAME)
4306 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004307
4308 name = xstrdup(p->l.t.v.v);
4309 bc_parse_addFunc(p, name, &p->fidx);
4310
4311 s = bc_lex_next(&p->l);
4312 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004313 if (p->l.t.t != BC_LEX_LPAREN)
4314 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004315 s = bc_lex_next(&p->l);
4316 if (s) return s;
4317
4318 while (p->l.t.t != BC_LEX_RPAREN) {
4319
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004320 if (p->l.t.t != BC_LEX_NAME)
4321 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004322
4323 ++p->func->nparams;
4324
4325 name = xstrdup(p->l.t.v.v);
4326 s = bc_lex_next(&p->l);
4327 if (s) goto err;
4328
4329 var = p->l.t.t != BC_LEX_LBRACKET;
4330
4331 if (!var) {
4332
4333 s = bc_lex_next(&p->l);
4334 if (s) goto err;
4335
4336 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004337 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004338 goto err;
4339 }
4340
4341 s = bc_lex_next(&p->l);
4342 if (s) goto err;
4343 }
4344
4345 comma = p->l.t.t == BC_LEX_COMMA;
4346 if (comma) {
4347 s = bc_lex_next(&p->l);
4348 if (s) goto err;
4349 }
4350
Denys Vlasenko29301232018-12-11 15:29:32 +01004351 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004352 if (s) goto err;
4353 }
4354
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004355 if (comma) return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004356
4357 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4358 bc_parse_startBody(p, flags);
4359
4360 s = bc_lex_next(&p->l);
4361 if (s) return s;
4362
4363 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004364 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004365
4366 return s;
4367
4368err:
4369 free(name);
4370 return s;
4371}
4372
4373static BcStatus bc_parse_auto(BcParse *p)
4374{
4375 BcStatus s;
4376 bool comma, var, one;
4377 char *name;
4378
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004379 if (!p->auto_part) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004380 s = bc_lex_next(&p->l);
4381 if (s) return s;
4382
4383 p->auto_part = comma = false;
4384 one = p->l.t.t == BC_LEX_NAME;
4385
4386 while (p->l.t.t == BC_LEX_NAME) {
4387
4388 name = xstrdup(p->l.t.v.v);
4389 s = bc_lex_next(&p->l);
4390 if (s) goto err;
4391
4392 var = p->l.t.t != BC_LEX_LBRACKET;
4393 if (!var) {
4394
4395 s = bc_lex_next(&p->l);
4396 if (s) goto err;
4397
4398 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004399 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004400 goto err;
4401 }
4402
4403 s = bc_lex_next(&p->l);
4404 if (s) goto err;
4405 }
4406
4407 comma = p->l.t.t == BC_LEX_COMMA;
4408 if (comma) {
4409 s = bc_lex_next(&p->l);
4410 if (s) goto err;
4411 }
4412
Denys Vlasenko29301232018-12-11 15:29:32 +01004413 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004414 if (s) goto err;
4415 }
4416
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004417 if (comma) return bc_error("bad function definition");
Denys Vlasenkoabbc4332018-12-03 21:46:41 +01004418 if (!one) return bc_error("no auto variable found");
Gavin Howard01055ba2018-11-03 11:00:21 -06004419
4420 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004421 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004422
4423 return bc_lex_next(&p->l);
4424
4425err:
4426 free(name);
4427 return s;
4428}
4429
4430static BcStatus bc_parse_body(BcParse *p, bool brace)
4431{
4432 BcStatus s = BC_STATUS_SUCCESS;
4433 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4434
4435 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4436
4437 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
4438
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004439 if (!brace) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004440 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4441
4442 if (!p->auto_part) {
4443 s = bc_parse_auto(p);
4444 if (s) return s;
4445 }
4446
4447 if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l);
4448 }
4449 else {
4450 s = bc_parse_stmt(p);
4451 if (!s && !brace) s = bc_parse_endBody(p, false);
4452 }
4453
4454 return s;
4455}
4456
4457static BcStatus bc_parse_stmt(BcParse *p)
4458{
4459 BcStatus s = BC_STATUS_SUCCESS;
4460
4461 switch (p->l.t.t) {
4462
4463 case BC_LEX_NLINE:
4464 {
4465 return bc_lex_next(&p->l);
4466 }
4467
4468 case BC_LEX_KEY_ELSE:
4469 {
4470 p->auto_part = false;
4471 break;
4472 }
4473
4474 case BC_LEX_LBRACE:
4475 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004476 if (!BC_PARSE_BODY(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004477
4478 ++p->nbraces;
4479 s = bc_lex_next(&p->l);
4480 if (s) return s;
4481
4482 return bc_parse_body(p, true);
4483 }
4484
4485 case BC_LEX_KEY_AUTO:
4486 {
4487 return bc_parse_auto(p);
4488 }
4489
4490 default:
4491 {
4492 p->auto_part = false;
4493
4494 if (BC_PARSE_IF_END(p)) {
4495 bc_parse_noElse(p);
4496 return BC_STATUS_SUCCESS;
4497 }
4498 else if (BC_PARSE_BODY(p))
4499 return bc_parse_body(p, false);
4500
4501 break;
4502 }
4503 }
4504
4505 switch (p->l.t.t) {
4506
4507 case BC_LEX_OP_INC:
4508 case BC_LEX_OP_DEC:
4509 case BC_LEX_OP_MINUS:
4510 case BC_LEX_OP_BOOL_NOT:
4511 case BC_LEX_LPAREN:
4512 case BC_LEX_NAME:
4513 case BC_LEX_NUMBER:
4514 case BC_LEX_KEY_IBASE:
4515 case BC_LEX_KEY_LAST:
4516 case BC_LEX_KEY_LENGTH:
4517 case BC_LEX_KEY_OBASE:
4518 case BC_LEX_KEY_READ:
4519 case BC_LEX_KEY_SCALE:
4520 case BC_LEX_KEY_SQRT:
4521 {
4522 s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
4523 break;
4524 }
4525
4526 case BC_LEX_KEY_ELSE:
4527 {
4528 s = bc_parse_else(p);
4529 break;
4530 }
4531
4532 case BC_LEX_SCOLON:
4533 {
4534 while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l);
4535 break;
4536 }
4537
4538 case BC_LEX_RBRACE:
4539 {
4540 s = bc_parse_endBody(p, true);
4541 break;
4542 }
4543
4544 case BC_LEX_STR:
4545 {
4546 s = bc_parse_string(p, BC_INST_PRINT_STR);
4547 break;
4548 }
4549
4550 case BC_LEX_KEY_BREAK:
4551 case BC_LEX_KEY_CONTINUE:
4552 {
4553 s = bc_parse_loopExit(p, p->l.t.t);
4554 break;
4555 }
4556
4557 case BC_LEX_KEY_FOR:
4558 {
4559 s = bc_parse_for(p);
4560 break;
4561 }
4562
4563 case BC_LEX_KEY_HALT:
4564 {
4565 bc_parse_push(p, BC_INST_HALT);
4566 s = bc_lex_next(&p->l);
4567 break;
4568 }
4569
4570 case BC_LEX_KEY_IF:
4571 {
4572 s = bc_parse_if(p);
4573 break;
4574 }
4575
4576 case BC_LEX_KEY_LIMITS:
4577 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004578 // "limits" is a compile-time command,
4579 // the output is produced at _parse time_.
Gavin Howard01055ba2018-11-03 11:00:21 -06004580 s = bc_lex_next(&p->l);
4581 if (s) return s;
Denys Vlasenko64074a12018-12-07 15:50:14 +01004582 printf(
4583 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4584 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4585 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4586 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4587 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4588 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4589 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4590 "Number of vars = "BC_MAX_VARS_STR "\n"
4591 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004592 break;
4593 }
4594
4595 case BC_LEX_KEY_PRINT:
4596 {
4597 s = bc_parse_print(p);
4598 break;
4599 }
4600
4601 case BC_LEX_KEY_QUIT:
4602 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004603 // "quit" is a compile-time command. For example,
4604 // "if (0 == 1) quit" terminates when parsing the statement,
4605 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004606 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004607 }
4608
4609 case BC_LEX_KEY_RETURN:
4610 {
4611 s = bc_parse_return(p);
4612 break;
4613 }
4614
4615 case BC_LEX_KEY_WHILE:
4616 {
4617 s = bc_parse_while(p);
4618 break;
4619 }
4620
4621 default:
4622 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004623 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004624 break;
4625 }
4626 }
4627
4628 return s;
4629}
4630
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01004631static FAST_FUNC BcStatus bc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004632{
4633 BcStatus s;
4634
4635 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004636 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 -06004637 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004638 if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004639 s = bc_parse_func(p);
4640 }
4641 else
4642 s = bc_parse_stmt(p);
4643
Denys Vlasenkod38af482018-12-04 19:11:02 +01004644 if (s || G_interrupt) {
4645 bc_parse_reset(p);
4646 s = BC_STATUS_FAILURE;
4647 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004648
4649 return s;
4650}
4651
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004652// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004653static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004654{
4655 BcStatus s = BC_STATUS_SUCCESS;
4656 BcInst prev = BC_INST_PRINT;
4657 BcLexType top, t = p->l.t.t;
4658 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004659 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004660 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4661
4662 paren_first = p->l.t.t == BC_LEX_LPAREN;
4663 nparens = nrelops = 0;
4664 paren_expr = rprn = done = get_token = assign = false;
4665 bin_last = true;
4666
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004667 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004668 switch (t) {
4669
4670 case BC_LEX_OP_INC:
4671 case BC_LEX_OP_DEC:
4672 {
4673 s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
4674 rprn = get_token = bin_last = false;
4675 break;
4676 }
4677
4678 case BC_LEX_OP_MINUS:
4679 {
4680 s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
4681 rprn = get_token = false;
4682 bin_last = prev == BC_INST_MINUS;
4683 break;
4684 }
4685
4686 case BC_LEX_OP_ASSIGN_POWER:
4687 case BC_LEX_OP_ASSIGN_MULTIPLY:
4688 case BC_LEX_OP_ASSIGN_DIVIDE:
4689 case BC_LEX_OP_ASSIGN_MODULUS:
4690 case BC_LEX_OP_ASSIGN_PLUS:
4691 case BC_LEX_OP_ASSIGN_MINUS:
4692 case BC_LEX_OP_ASSIGN:
4693 {
4694 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4695 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4696 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4697 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004698 s = bc_error("bad assignment:"
4699 " left side must be scale,"
4700 " ibase, obase, last, var,"
4701 " or array element"
4702 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004703 break;
4704 }
4705 }
4706 // Fallthrough.
4707 case BC_LEX_OP_POWER:
4708 case BC_LEX_OP_MULTIPLY:
4709 case BC_LEX_OP_DIVIDE:
4710 case BC_LEX_OP_MODULUS:
4711 case BC_LEX_OP_PLUS:
4712 case BC_LEX_OP_REL_EQ:
4713 case BC_LEX_OP_REL_LE:
4714 case BC_LEX_OP_REL_GE:
4715 case BC_LEX_OP_REL_NE:
4716 case BC_LEX_OP_REL_LT:
4717 case BC_LEX_OP_REL_GT:
4718 case BC_LEX_OP_BOOL_NOT:
4719 case BC_LEX_OP_BOOL_OR:
4720 case BC_LEX_OP_BOOL_AND:
4721 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004722 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4723 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4724 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004725 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004726 }
4727
4728 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4729 prev = BC_PARSE_TOKEN_INST(t);
4730 s = bc_parse_operator(p, t, ops_bgn, &nexprs, true);
4731 rprn = get_token = false;
4732 bin_last = t != BC_LEX_OP_BOOL_NOT;
4733
4734 break;
4735 }
4736
4737 case BC_LEX_LPAREN:
4738 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004739 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004740 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004741 ++nparens;
4742 paren_expr = rprn = bin_last = false;
4743 get_token = true;
4744 bc_vec_push(&p->ops, &t);
4745
4746 break;
4747 }
4748
4749 case BC_LEX_RPAREN:
4750 {
4751 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004752 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004753
4754 if (nparens == 0) {
4755 s = BC_STATUS_SUCCESS;
4756 done = true;
4757 get_token = false;
4758 break;
4759 }
4760 else if (!paren_expr)
4761 return BC_STATUS_PARSE_EMPTY_EXP;
4762
4763 --nparens;
4764 paren_expr = rprn = true;
4765 get_token = bin_last = false;
4766
4767 s = bc_parse_rightParen(p, ops_bgn, &nexprs);
4768
4769 break;
4770 }
4771
4772 case BC_LEX_NAME:
4773 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004774 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004775 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004776 paren_expr = true;
4777 rprn = get_token = bin_last = false;
4778 s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
4779 ++nexprs;
4780
4781 break;
4782 }
4783
4784 case BC_LEX_NUMBER:
4785 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004786 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004787 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004788 bc_parse_number(p, &prev, &nexprs);
4789 paren_expr = get_token = true;
4790 rprn = bin_last = false;
4791
4792 break;
4793 }
4794
4795 case BC_LEX_KEY_IBASE:
4796 case BC_LEX_KEY_LAST:
4797 case BC_LEX_KEY_OBASE:
4798 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004799 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004800 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004801 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4802 bc_parse_push(p, (char) prev);
4803
4804 paren_expr = get_token = true;
4805 rprn = bin_last = false;
4806 ++nexprs;
4807
4808 break;
4809 }
4810
4811 case BC_LEX_KEY_LENGTH:
4812 case BC_LEX_KEY_SQRT:
4813 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004814 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004815 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004816 s = bc_parse_builtin(p, t, flags, &prev);
4817 paren_expr = true;
4818 rprn = get_token = bin_last = false;
4819 ++nexprs;
4820
4821 break;
4822 }
4823
4824 case BC_LEX_KEY_READ:
4825 {
4826 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004827 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004828 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004829 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004830 else
4831 s = bc_parse_read(p);
4832
4833 paren_expr = true;
4834 rprn = get_token = bin_last = false;
4835 ++nexprs;
4836 prev = BC_INST_READ;
4837
4838 break;
4839 }
4840
4841 case BC_LEX_KEY_SCALE:
4842 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004843 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004844 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004845 s = bc_parse_scale(p, &prev, flags);
4846 paren_expr = true;
4847 rprn = get_token = bin_last = false;
4848 ++nexprs;
4849 prev = BC_INST_SCALE;
4850
4851 break;
4852 }
4853
4854 default:
4855 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004856 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004857 break;
4858 }
4859 }
4860
4861 if (!s && get_token) s = bc_lex_next(&p->l);
4862 }
4863
4864 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004865 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004866
4867 while (p->ops.len > ops_bgn) {
4868
4869 top = BC_PARSE_TOP_OP(p);
4870 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4871
4872 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004873 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004874
4875 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
4876
4877 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4878 bc_vec_pop(&p->ops);
4879 }
4880
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004881 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004882 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004883
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004884 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
4885 for (;;) {
4886 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004887 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004888 if (next & 0x80) // last element?
4889 break;
4890 next >>= 8;
4891 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004892 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004893 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06004894
4895 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004896 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004897 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004898 }
4899 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004900 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004901 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004902 }
4903
4904 if (flags & BC_PARSE_PRINT) {
4905 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4906 bc_parse_push(p, BC_INST_POP);
4907 }
4908
4909 return s;
4910}
4911
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004912static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
4913{
4914 BcStatus s;
4915
4916 s = bc_parse_expr_empty_ok(p, flags, next);
4917 if (s == BC_STATUS_PARSE_EMPTY_EXP)
4918 return bc_error("empty expression");
4919 return s;
4920}
4921
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004922static void bc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06004923{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004924 bc_parse_create(p, func, bc_parse_parse, bc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06004925}
4926
4927static BcStatus bc_parse_expression(BcParse *p, uint8_t flags)
4928{
4929 return bc_parse_expr(p, flags, bc_parse_next_read);
4930}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004931
Gavin Howard01055ba2018-11-03 11:00:21 -06004932#endif // ENABLE_BC
4933
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004934#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004935
4936#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
4937
Gavin Howard01055ba2018-11-03 11:00:21 -06004938static BcStatus dc_parse_register(BcParse *p)
4939{
4940 BcStatus s;
4941 char *name;
4942
4943 s = bc_lex_next(&p->l);
4944 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004945 if (p->l.t.t != BC_LEX_NAME) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004946
4947 name = xstrdup(p->l.t.v.v);
4948 bc_parse_pushName(p, name);
4949
4950 return s;
4951}
4952
4953static BcStatus dc_parse_string(BcParse *p)
4954{
4955 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004956 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004957
4958 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
4959 name = xstrdup(b);
4960
4961 str = xstrdup(p->l.t.v.v);
4962 bc_parse_push(p, BC_INST_STR);
4963 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004964 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004965 bc_parse_addFunc(p, name, &idx);
4966
4967 return bc_lex_next(&p->l);
4968}
4969
4970static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
4971{
4972 BcStatus s;
4973
4974 bc_parse_push(p, inst);
4975 if (name) {
4976 s = dc_parse_register(p);
4977 if (s) return s;
4978 }
4979
4980 if (store) {
4981 bc_parse_push(p, BC_INST_SWAP);
4982 bc_parse_push(p, BC_INST_ASSIGN);
4983 bc_parse_push(p, BC_INST_POP);
4984 }
4985
4986 return bc_lex_next(&p->l);
4987}
4988
4989static BcStatus dc_parse_cond(BcParse *p, uint8_t inst)
4990{
4991 BcStatus s;
4992
4993 bc_parse_push(p, inst);
4994 bc_parse_push(p, BC_INST_EXEC_COND);
4995
4996 s = dc_parse_register(p);
4997 if (s) return s;
4998
4999 s = bc_lex_next(&p->l);
5000 if (s) return s;
5001
5002 if (p->l.t.t == BC_LEX_ELSE) {
5003 s = dc_parse_register(p);
5004 if (s) return s;
5005 s = bc_lex_next(&p->l);
5006 }
5007 else
5008 bc_parse_push(p, BC_PARSE_STREND);
5009
5010 return s;
5011}
5012
5013static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
5014{
5015 BcStatus s = BC_STATUS_SUCCESS;
5016 BcInst prev;
5017 uint8_t inst;
5018 bool assign, get_token = false;
5019
5020 switch (t) {
5021
5022 case BC_LEX_OP_REL_EQ:
5023 case BC_LEX_OP_REL_LE:
5024 case BC_LEX_OP_REL_GE:
5025 case BC_LEX_OP_REL_NE:
5026 case BC_LEX_OP_REL_LT:
5027 case BC_LEX_OP_REL_GT:
5028 {
5029 s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
5030 break;
5031 }
5032
5033 case BC_LEX_SCOLON:
5034 case BC_LEX_COLON:
5035 {
5036 s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
5037 break;
5038 }
5039
5040 case BC_LEX_STR:
5041 {
5042 s = dc_parse_string(p);
5043 break;
5044 }
5045
5046 case BC_LEX_NEG:
5047 case BC_LEX_NUMBER:
5048 {
5049 if (t == BC_LEX_NEG) {
5050 s = bc_lex_next(&p->l);
5051 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005052 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005053 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005054 }
5055
5056 bc_parse_number(p, &prev, &p->nbraces);
5057
5058 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5059 get_token = true;
5060
5061 break;
5062 }
5063
5064 case BC_LEX_KEY_READ:
5065 {
5066 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005067 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005068 else
5069 bc_parse_push(p, BC_INST_READ);
5070 get_token = true;
5071 break;
5072 }
5073
5074 case BC_LEX_OP_ASSIGN:
5075 case BC_LEX_STORE_PUSH:
5076 {
5077 assign = t == BC_LEX_OP_ASSIGN;
5078 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
5079 s = dc_parse_mem(p, inst, true, assign);
5080 break;
5081 }
5082
5083 case BC_LEX_LOAD:
5084 case BC_LEX_LOAD_POP:
5085 {
5086 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
5087 s = dc_parse_mem(p, inst, true, false);
5088 break;
5089 }
5090
5091 case BC_LEX_STORE_IBASE:
5092 case BC_LEX_STORE_SCALE:
5093 case BC_LEX_STORE_OBASE:
5094 {
5095 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
5096 s = dc_parse_mem(p, inst, false, true);
5097 break;
5098 }
5099
5100 default:
5101 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005102 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005103 get_token = true;
5104 break;
5105 }
5106 }
5107
5108 if (!s && get_token) s = bc_lex_next(&p->l);
5109
5110 return s;
5111}
5112
5113static BcStatus dc_parse_expr(BcParse *p, uint8_t flags)
5114{
5115 BcStatus s = BC_STATUS_SUCCESS;
5116 BcInst inst;
5117 BcLexType t;
5118
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005119 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005120
5121 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
5122
5123 inst = dc_parse_insts[t];
5124
5125 if (inst != BC_INST_INVALID) {
5126 bc_parse_push(p, inst);
5127 s = bc_lex_next(&p->l);
5128 }
5129 else
5130 s = dc_parse_token(p, t, flags);
5131 }
5132
5133 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5134 bc_parse_push(p, BC_INST_POP_EXEC);
5135
5136 return s;
5137}
5138
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01005139static FAST_FUNC BcStatus dc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005140{
5141 BcStatus s;
5142
5143 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005144 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005145 else
5146 s = dc_parse_expr(p, 0);
5147
Denys Vlasenkod38af482018-12-04 19:11:02 +01005148 if (s || G_interrupt) {
5149 bc_parse_reset(p);
5150 s = BC_STATUS_FAILURE;
5151 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005152
5153 return s;
5154}
5155
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005156static void dc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005157{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005158 bc_parse_create(p, func, dc_parse_parse, dc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005159}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005160
Gavin Howard01055ba2018-11-03 11:00:21 -06005161#endif // ENABLE_DC
5162
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005163static void common_parse_init(BcParse *p, size_t func)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005164{
5165 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005166 IF_BC(bc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005167 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005168 IF_DC(dc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005169 }
5170}
5171
5172static BcStatus common_parse_expr(BcParse *p, uint8_t flags)
5173{
5174 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005175 IF_BC(return bc_parse_expression(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005176 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005177 IF_DC(return dc_parse_expr(p, flags);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005178 }
5179}
5180
Denys Vlasenkodf515392018-12-02 19:27:48 +01005181static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005182{
Gavin Howard01055ba2018-11-03 11:00:21 -06005183 BcId e, *ptr;
5184 BcVec *v, *map;
5185 size_t i;
5186 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005187 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005188
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005189 v = var ? &G.prog.vars : &G.prog.arrs;
5190 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005191
5192 e.name = id;
5193 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005194 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005195
5196 if (new) {
5197 bc_array_init(&data.v, var);
5198 bc_vec_push(v, &data.v);
5199 }
5200
5201 ptr = bc_vec_item(map, i);
5202 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005203 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005204}
5205
Denys Vlasenko29301232018-12-11 15:29:32 +01005206static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005207{
Gavin Howard01055ba2018-11-03 11:00:21 -06005208 switch (r->t) {
5209
5210 case BC_RESULT_STR:
5211 case BC_RESULT_TEMP:
5212 case BC_RESULT_IBASE:
5213 case BC_RESULT_SCALE:
5214 case BC_RESULT_OBASE:
5215 {
5216 *num = &r->d.n;
5217 break;
5218 }
5219
5220 case BC_RESULT_CONSTANT:
5221 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005222 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005223 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005224 size_t base_t, len = strlen(*str);
5225 BcNum *base;
5226
5227 bc_num_init(&r->d.n, len);
5228
5229 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005230 base = hex ? &G.prog.hexb : &G.prog.ib;
5231 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005232 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005233
5234 if (s) {
5235 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005236 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005237 }
5238
5239 *num = &r->d.n;
5240 r->t = BC_RESULT_TEMP;
5241
5242 break;
5243 }
5244
5245 case BC_RESULT_VAR:
5246 case BC_RESULT_ARRAY:
5247 case BC_RESULT_ARRAY_ELEM:
5248 {
5249 BcVec *v;
5250
Denys Vlasenkodf515392018-12-02 19:27:48 +01005251 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005252
5253 if (r->t == BC_RESULT_ARRAY_ELEM) {
5254 v = bc_vec_top(v);
5255 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5256 *num = bc_vec_item(v, r->d.id.idx);
5257 }
5258 else
5259 *num = bc_vec_top(v);
5260
5261 break;
5262 }
5263
5264 case BC_RESULT_LAST:
5265 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005266 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005267 break;
5268 }
5269
5270 case BC_RESULT_ONE:
5271 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005272 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005273 break;
5274 }
5275 }
5276
Denys Vlasenko29301232018-12-11 15:29:32 +01005277 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005278}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005279#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005280# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005281#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005282
Denys Vlasenko29301232018-12-11 15:29:32 +01005283static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005284 BcResult **r, BcNum **rn, bool assign)
5285{
5286 BcStatus s;
5287 bool hex;
5288 BcResultType lt, rt;
5289
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005290 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005291 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005292
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005293 *r = bc_vec_item_rev(&G.prog.results, 0);
5294 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005295
5296 lt = (*l)->t;
5297 rt = (*r)->t;
5298 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5299
Denys Vlasenko29301232018-12-11 15:29:32 +01005300 s = zbc_program_num(*l, ln, false);
5301 if (s) RETURN_STATUS(s);
5302 s = zbc_program_num(*r, rn, hex);
5303 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005304
5305 // We run this again under these conditions in case any vector has been
5306 // reallocated out from under the BcNums or arrays we had.
5307 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005308 s = zbc_program_num(*l, ln, false);
5309 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005310 }
5311
5312 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005313 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005314 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005315 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005316
Denys Vlasenko29301232018-12-11 15:29:32 +01005317 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005318}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005319#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005320# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005321#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005322
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005323static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005324{
5325 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005326 bc_vec_pop(&G.prog.results);
5327 bc_vec_pop(&G.prog.results);
5328 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005329}
5330
Denys Vlasenko29301232018-12-11 15:29:32 +01005331static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005332{
5333 BcStatus s;
5334
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005335 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005336 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005337 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005338
Denys Vlasenko29301232018-12-11 15:29:32 +01005339 s = zbc_program_num(*r, n, false);
5340 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005341
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005342 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005343 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005344
Denys Vlasenko29301232018-12-11 15:29:32 +01005345 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005346}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005347#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005348# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005349#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005350
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005351static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005352{
5353 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005354 bc_vec_pop(&G.prog.results);
5355 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005356}
5357
Denys Vlasenko259137d2018-12-11 19:42:05 +01005358static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005359{
5360 BcStatus s;
5361 BcResult *opd1, *opd2, res;
5362 BcNum *n1, *n2 = NULL;
5363
Denys Vlasenko29301232018-12-11 15:29:32 +01005364 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005365 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005366 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005367
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005368 s = BC_STATUS_SUCCESS;
5369#if !ERRORS_ARE_FATAL
5370 s =
5371#endif
5372 zbc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005373 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005374 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005375
Denys Vlasenko259137d2018-12-11 19:42:05 +01005376 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005377
5378err:
5379 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005380 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005381}
Denys Vlasenko259137d2018-12-11 19:42:05 +01005382#if ERRORS_ARE_FATAL
5383# define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS)
5384#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005385
Denys Vlasenko785e4b32018-12-02 17:18:52 +01005386static BcStatus bc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005387{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005388 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005389 BcStatus s;
5390 BcParse parse;
5391 BcVec buf;
5392 BcInstPtr ip;
5393 size_t i;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005394 BcFunc *f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005395
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005396 for (i = 0; i < G.prog.stack.len; ++i) {
5397 BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005398 if (ip_ptr->func == BC_PROG_READ)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005399 return bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005400 }
5401
Denys Vlasenko7d628012018-12-04 21:46:47 +01005402 bc_vec_pop_all(&f->code);
5403 bc_char_vec_init(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005404
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005405 sv_file = G.prog.file;
5406 G.prog.file = NULL;
5407
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01005408 s = bc_read_line(&buf);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005409 //if (s) goto io_err; - wrong, nonzero return means EOF, not error
Gavin Howard01055ba2018-11-03 11:00:21 -06005410
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005411 common_parse_init(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005412 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005413
5414 s = bc_parse_text(&parse, buf.v);
5415 if (s) goto exec_err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005416 s = common_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005417 if (s) goto exec_err;
5418
5419 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005420 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005421 goto exec_err;
5422 }
5423
5424 ip.func = BC_PROG_READ;
5425 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005426 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005427
5428 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005429 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005430
5431 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005432 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005433
5434exec_err:
5435 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005436//io_err:
5437 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005438 bc_vec_free(&buf);
5439 return s;
5440}
5441
5442static size_t bc_program_index(char *code, size_t *bgn)
5443{
5444 char amt = code[(*bgn)++], i = 0;
5445 size_t res = 0;
5446
5447 for (; i < amt; ++i, ++(*bgn))
5448 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5449
5450 return res;
5451}
5452
5453static char *bc_program_name(char *code, size_t *bgn)
5454{
5455 size_t i;
5456 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5457
5458 s = xmalloc(ptr - str + 1);
5459 c = code[(*bgn)++];
5460
5461 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5462 s[i] = c;
5463
5464 s[i] = '\0';
5465
5466 return s;
5467}
5468
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005469static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005470{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005471#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005472 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005473 // Example: echo '[]ap' | dc
5474 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005475 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005476 return;
5477 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005478#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005479 while (*str) {
5480 int c = *str++;
5481 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005482 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005483 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005484 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005485 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005486 case 'a':
5487 bb_putchar('\a');
5488 break;
5489 case 'b':
5490 bb_putchar('\b');
5491 break;
5492 case '\\':
5493 case 'e':
5494 bb_putchar('\\');
5495 break;
5496 case 'f':
5497 bb_putchar('\f');
5498 break;
5499 case 'n':
5500 bb_putchar('\n');
5501 G.prog.nchars = SIZE_MAX;
5502 break;
5503 case 'r':
5504 bb_putchar('\r');
5505 break;
5506 case 'q':
5507 bb_putchar('"');
5508 break;
5509 case 't':
5510 bb_putchar('\t');
5511 break;
5512 default:
5513 // Just print the backslash and following character.
5514 bb_putchar('\\');
5515 ++G.prog.nchars;
5516 bb_putchar(c);
5517 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005518 }
5519 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005520 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005521 }
5522}
5523
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005524static void bc_num_printNewline(void)
5525{
5526 if (G.prog.nchars == G.prog.len - 1) {
5527 bb_putchar('\\');
5528 bb_putchar('\n');
5529 G.prog.nchars = 0;
5530 }
5531}
5532
5533#if ENABLE_DC
5534static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
5535{
5536 (void) radix;
5537 bb_putchar((char) num);
5538 G.prog.nchars += width;
5539}
5540#endif
5541
5542static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5543{
5544 size_t exp, pow;
5545
5546 bc_num_printNewline();
5547 bb_putchar(radix ? '.' : ' ');
5548 ++G.prog.nchars;
5549
5550 bc_num_printNewline();
5551 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5552 continue;
5553
5554 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5555 size_t dig;
5556 bc_num_printNewline();
5557 dig = num / pow;
5558 num -= dig * pow;
5559 bb_putchar(((char) dig) + '0');
5560 }
5561}
5562
5563static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5564{
5565 if (radix) {
5566 bc_num_printNewline();
5567 bb_putchar('.');
5568 G.prog.nchars += 1;
5569 }
5570
5571 bc_num_printNewline();
5572 bb_putchar(bb_hexdigits_upcase[num]);
5573 G.prog.nchars += width;
5574}
5575
5576static void bc_num_printDecimal(BcNum *n)
5577{
5578 size_t i, rdx = n->rdx - 1;
5579
5580 if (n->neg) bb_putchar('-');
5581 G.prog.nchars += n->neg;
5582
5583 for (i = n->len - 1; i < n->len; --i)
5584 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5585}
5586
5587static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
5588{
5589 BcStatus s;
5590 BcVec stack;
5591 BcNum intp, fracp, digit, frac_len;
5592 unsigned long dig, *ptr;
5593 size_t i;
5594 bool radix;
5595
5596 if (n->len == 0) {
5597 print(0, width, false);
5598 RETURN_STATUS(BC_STATUS_SUCCESS);
5599 }
5600
5601 bc_vec_init(&stack, sizeof(long), NULL);
5602 bc_num_init(&intp, n->len);
5603 bc_num_init(&fracp, n->rdx);
5604 bc_num_init(&digit, width);
5605 bc_num_init(&frac_len, BC_NUM_INT(n));
5606 bc_num_copy(&intp, n);
5607 bc_num_one(&frac_len);
5608
5609 bc_num_truncate(&intp, intp.rdx);
5610 s = zbc_num_sub(n, &intp, &fracp, 0);
5611 if (s) goto err;
5612
5613 while (intp.len != 0) {
5614 s = zbc_num_divmod(&intp, base, &intp, &digit, 0);
5615 if (s) goto err;
5616 s = zbc_num_ulong(&digit, &dig);
5617 if (s) goto err;
5618 bc_vec_push(&stack, &dig);
5619 }
5620
5621 for (i = 0; i < stack.len; ++i) {
5622 ptr = bc_vec_item_rev(&stack, i);
5623 print(*ptr, width, false);
5624 }
5625
5626 if (!n->rdx) goto err;
5627
5628 for (radix = true; frac_len.len <= n->rdx; radix = false) {
5629 s = zbc_num_mul(&fracp, base, &fracp, n->rdx);
5630 if (s) goto err;
5631 s = zbc_num_ulong(&fracp, &dig);
5632 if (s) goto err;
5633 bc_num_ulong2num(&intp, dig);
5634 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5635 if (s) goto err;
5636 print(dig, width, radix);
5637 s = zbc_num_mul(&frac_len, base, &frac_len, 0);
5638 if (s) goto err;
5639 }
5640
5641err:
5642 bc_num_free(&frac_len);
5643 bc_num_free(&digit);
5644 bc_num_free(&fracp);
5645 bc_num_free(&intp);
5646 bc_vec_free(&stack);
5647 RETURN_STATUS(s);
5648}
5649#if ERRORS_ARE_FATAL
5650# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
5651#endif
5652
5653static BC_STATUS zbc_num_printBase(BcNum *n)
5654{
5655 BcStatus s;
5656 size_t width, i;
5657 BcNumDigitOp print;
5658 bool neg = n->neg;
5659
5660 if (neg) {
5661 bb_putchar('-');
5662 G.prog.nchars++;
5663 }
5664
5665 n->neg = false;
5666
5667 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5668 width = 1;
5669 print = bc_num_printHex;
5670 }
5671 else {
5672 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
5673 continue;
5674 print = bc_num_printDigits;
5675 }
5676
5677 s = zbc_num_printNum(n, &G.prog.ob, width, print);
5678 n->neg = neg;
5679
5680 RETURN_STATUS(s);
5681}
5682#if ERRORS_ARE_FATAL
5683# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
5684#endif
5685
5686#if ENABLE_DC
5687static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
5688{
5689 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
5690}
5691#if ERRORS_ARE_FATAL
5692# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
5693#endif
5694#endif
5695
5696static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5697{
5698 BcStatus s = BC_STATUS_SUCCESS;
5699
5700 bc_num_printNewline();
5701
5702 if (n->len == 0) {
5703 bb_putchar('0');
5704 ++G.prog.nchars;
5705 }
5706 else if (G.prog.ob_t == 10)
5707 bc_num_printDecimal(n);
5708 else
5709 s = zbc_num_printBase(n);
5710
5711 if (newline) {
5712 bb_putchar('\n');
5713 G.prog.nchars = 0;
5714 }
5715
5716 RETURN_STATUS(s);
5717}
5718#if ERRORS_ARE_FATAL
5719# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
5720#endif
5721
Denys Vlasenko29301232018-12-11 15:29:32 +01005722static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005723{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005724 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005725 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005726 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005727 bool pop = inst != BC_INST_PRINT;
5728
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005729 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005730 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005731
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005732 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005733 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005734 s = zbc_program_num(r, &num, false);
5735 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005736
5737 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005738 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005739 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005740 }
5741 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005742 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005743
5744 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005745 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005746
5747 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005748 for (;;) {
5749 char c = *str++;
5750 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005751 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005752 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005753 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005754 }
5755 }
5756 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005757 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005758 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005759 }
5760 }
5761
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005762 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005763
Denys Vlasenko29301232018-12-11 15:29:32 +01005764 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005765}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005766#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005767# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005768#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005769
Denys Vlasenko29301232018-12-11 15:29:32 +01005770static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005771{
5772 BcStatus s;
5773 BcResult res, *ptr;
5774 BcNum *num = NULL;
5775
Denys Vlasenko29301232018-12-11 15:29:32 +01005776 s = zbc_program_prep(&ptr, &num);
5777 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005778
5779 bc_num_init(&res.d.n, num->len);
5780 bc_num_copy(&res.d.n, num);
5781 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5782
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005783 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005784
Denys Vlasenko29301232018-12-11 15:29:32 +01005785 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005786}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005787#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005788# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005789#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005790
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005791static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005792{
5793 BcStatus s;
5794 BcResult *opd1, *opd2, res;
5795 BcNum *n1, *n2;
5796 bool cond = 0;
5797 ssize_t cmp;
5798
Denys Vlasenko29301232018-12-11 15:29:32 +01005799 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005800 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005801
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005802 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005803
5804 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005805 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005806 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005807 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005808 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06005809 cmp = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005810 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005811 case BC_INST_REL_EQ:
5812 cond = cmp == 0;
5813 break;
5814 case BC_INST_REL_LE:
5815 cond = cmp <= 0;
5816 break;
5817 case BC_INST_REL_GE:
5818 cond = cmp >= 0;
5819 break;
5820 case BC_INST_REL_NE:
5821 cond = cmp != 0;
5822 break;
5823 case BC_INST_REL_LT:
5824 cond = cmp < 0;
5825 break;
5826 case BC_INST_REL_GT:
5827 cond = cmp > 0;
5828 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005829 }
5830 }
5831
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005832 if (cond) bc_num_one(&res.d.n);
5833 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005834
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005835 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005836
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005837 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005838}
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005839#if ERRORS_ARE_FATAL
5840# define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS)
5841#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005842
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005843#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005844static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005845 bool push)
5846{
5847 BcNum n2;
5848 BcResult res;
5849
5850 memset(&n2, 0, sizeof(BcNum));
5851 n2.rdx = res.d.id.idx = r->d.id.idx;
5852 res.t = BC_RESULT_STR;
5853
5854 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005855 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005856 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005857 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005858 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005859 }
5860
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005861 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005862
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005863 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005864 bc_vec_push(v, &n2);
5865
Denys Vlasenko29301232018-12-11 15:29:32 +01005866 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005867}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005868#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005869# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005870#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005871#endif // ENABLE_DC
5872
Denys Vlasenko29301232018-12-11 15:29:32 +01005873static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005874{
5875 BcStatus s;
5876 BcResult *ptr, r;
5877 BcVec *v;
5878 BcNum *n;
5879
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005880 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005881 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005882
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005883 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005884 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005885 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005886 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005887
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005888#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005889 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005890 RETURN_STATUS(bc_error_variable_is_wrong_type());
5891 if (ptr->t == BC_RESULT_STR)
5892 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005893#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005894
Denys Vlasenko29301232018-12-11 15:29:32 +01005895 s = zbc_program_num(ptr, &n, false);
5896 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005897
5898 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005899 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005900
5901 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005902 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005903 bc_num_copy(&r.d.n, n);
5904 }
5905 else {
5906 bc_array_init(&r.d.v, true);
5907 bc_array_copy(&r.d.v, (BcVec *) n);
5908 }
5909
5910 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005911 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005912
Denys Vlasenko29301232018-12-11 15:29:32 +01005913 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005914}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005915#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005916# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005917#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005918
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005919static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005920{
5921 BcStatus s;
5922 BcResult *left, *right, res;
5923 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005924 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5925
Denys Vlasenko29301232018-12-11 15:29:32 +01005926 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005927 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005928
5929 ib = left->t == BC_RESULT_IBASE;
5930 sc = left->t == BC_RESULT_SCALE;
5931
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005932#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005933
5934 if (right->t == BC_RESULT_STR) {
5935
5936 BcVec *v;
5937
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005938 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005939 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005940 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005941
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005942 RETURN_STATUS(zbc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005944#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005945
5946 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005947 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005948 " left side must be scale,"
5949 " ibase, obase, last, var,"
5950 " or array element"
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005951 ));
Gavin Howard01055ba2018-11-03 11:00:21 -06005952
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005953#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005954 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005955 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005956
5957 if (assign)
5958 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005959 else {
5960 s = BC_STATUS_SUCCESS;
5961#if !ERRORS_ARE_FATAL
5962 s =
5963#endif
5964 zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
5965 }
5966 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005967#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005968 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005969#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005970
5971 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005972 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005973 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5974 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5975 NULL, //can't happen //BC_RESULT_LAST
5976 NULL, //can't happen //BC_RESULT_CONSTANT
5977 NULL, //can't happen //BC_RESULT_ONE
5978 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005979 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005980 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01005981 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06005982
Denys Vlasenko29301232018-12-11 15:29:32 +01005983 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005984 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005985 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005986 if (sc) {
5987 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005988 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06005989 }
5990 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005991 if (val < BC_NUM_MIN_BASE)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005992 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005993 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005994 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005995 }
5996
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005997 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005998 RETURN_STATUS(bc_error(msg[s]));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005999 if (!sc)
6000 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006001
6002 *ptr = (size_t) val;
6003 s = BC_STATUS_SUCCESS;
6004 }
6005
6006 bc_num_init(&res.d.n, l->len);
6007 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006008 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006009
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006010 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006011}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006012#if ERRORS_ARE_FATAL
6013# define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS)
6014#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
Denys Vlasenko416ce762018-12-02 20:57:17 +01006016#if !ENABLE_DC
6017#define bc_program_pushVar(code, bgn, pop, copy) \
6018 bc_program_pushVar(code, bgn)
6019// for bc, 'pop' and 'copy' are always false
6020#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006021static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006022 bool pop, bool copy)
6023{
Gavin Howard01055ba2018-11-03 11:00:21 -06006024 BcResult r;
6025 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006026
6027 r.t = BC_RESULT_VAR;
6028 r.d.id.name = name;
6029
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006030#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006031 {
6032 BcVec *v = bc_program_search(name, true);
6033 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006034
Denys Vlasenko416ce762018-12-02 20:57:17 +01006035 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006036
Denys Vlasenko416ce762018-12-02 20:57:17 +01006037 if (!BC_PROG_STACK(v, 2 - copy)) {
6038 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006039 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006040 }
6041
Gavin Howard01055ba2018-11-03 11:00:21 -06006042 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006043 name = NULL;
6044
6045 if (!BC_PROG_STR(num)) {
6046
6047 r.t = BC_RESULT_TEMP;
6048
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006049 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006050 bc_num_copy(&r.d.n, num);
6051 }
6052 else {
6053 r.t = BC_RESULT_STR;
6054 r.d.id.idx = num->rdx;
6055 }
6056
6057 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006058 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006059 }
6060#endif // ENABLE_DC
6061
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006062 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006063
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006064 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006065}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006066#if ERRORS_ARE_FATAL
6067# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6068#else
6069# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6070#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006071
Denys Vlasenko29301232018-12-11 15:29:32 +01006072static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006073 char inst)
6074{
6075 BcStatus s = BC_STATUS_SUCCESS;
6076 BcResult r;
6077 BcNum *num;
6078
6079 r.d.id.name = bc_program_name(code, bgn);
6080
6081 if (inst == BC_INST_ARRAY) {
6082 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006083 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006084 }
6085 else {
6086
6087 BcResult *operand;
6088 unsigned long temp;
6089
Denys Vlasenko29301232018-12-11 15:29:32 +01006090 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006091 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006092 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006093 if (s) goto err;
6094
6095 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006096 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006097 goto err;
6098 }
6099
6100 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006101 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006102 }
6103
6104err:
6105 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006106 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006107}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006108#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006109# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006110#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006111
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006112#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006113static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006114{
6115 BcStatus s;
6116 BcResult *ptr, res, copy;
6117 BcNum *num = NULL;
6118 char inst2 = inst;
6119
Denys Vlasenko29301232018-12-11 15:29:32 +01006120 s = zbc_program_prep(&ptr, &num);
6121 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006122
6123 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6124 copy.t = BC_RESULT_TEMP;
6125 bc_num_init(&copy.d.n, num->len);
6126 bc_num_copy(&copy.d.n, num);
6127 }
6128
6129 res.t = BC_RESULT_ONE;
6130 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6131 BC_INST_ASSIGN_PLUS :
6132 BC_INST_ASSIGN_MINUS;
6133
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006134 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006135 s = zbc_program_assign(inst);
6136 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006137
6138 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006139 bc_vec_pop(&G.prog.results);
6140 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006141 }
6142
Denys Vlasenko29301232018-12-11 15:29:32 +01006143 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006144}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006145#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006146# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006147#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006148
Denys Vlasenko29301232018-12-11 15:29:32 +01006149static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006150{
Gavin Howard01055ba2018-11-03 11:00:21 -06006151 BcInstPtr ip;
6152 size_t i, nparams = bc_program_index(code, idx);
6153 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006154 BcId *a;
6155 BcResultData param;
6156 BcResult *arg;
6157
6158 ip.idx = 0;
6159 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006160 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006161
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006162 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006163 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006164 }
6165 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006166 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006167 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006168 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006169
6170 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006171 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006172
6173 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006174 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006175
6176 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006177 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006178
Denys Vlasenko29301232018-12-11 15:29:32 +01006179 s = zbc_program_copyToVar(a->name, a->idx);
6180 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006181 }
6182
6183 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006184 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006185
6186 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006187 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006188
6189 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006190 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006191 bc_vec_push(v, &param.n);
6192 }
6193 else {
6194 bc_array_init(&param.v, true);
6195 bc_vec_push(v, &param.v);
6196 }
6197 }
6198
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006199 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006200
Denys Vlasenko29301232018-12-11 15:29:32 +01006201 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006202}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006203#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006204# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006205#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006206
Denys Vlasenko29301232018-12-11 15:29:32 +01006207static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006208{
Gavin Howard01055ba2018-11-03 11:00:21 -06006209 BcResult res;
6210 BcFunc *f;
6211 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006212 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006213
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006214 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006215 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006216
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006217 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006218 res.t = BC_RESULT_TEMP;
6219
6220 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006221 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006222 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006223 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006224
Denys Vlasenko29301232018-12-11 15:29:32 +01006225 s = zbc_program_num(operand, &num, false);
6226 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006227 bc_num_init(&res.d.n, num->len);
6228 bc_num_copy(&res.d.n, num);
6229 }
6230 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006231 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006232 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006233 }
6234
6235 // We need to pop arguments as well, so this takes that into account.
6236 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006237 BcVec *v;
6238 BcId *a = bc_vec_item(&f->autos, i);
6239
Denys Vlasenkodf515392018-12-02 19:27:48 +01006240 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006241 bc_vec_pop(v);
6242 }
6243
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006244 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6245 bc_vec_push(&G.prog.results, &res);
6246 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006247
Denys Vlasenko29301232018-12-11 15:29:32 +01006248 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006249}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006250#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006251# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006252#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006253#endif // ENABLE_BC
6254
6255static unsigned long bc_program_scale(BcNum *n)
6256{
6257 return (unsigned long) n->rdx;
6258}
6259
6260static unsigned long bc_program_len(BcNum *n)
6261{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006262 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006263
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006264 if (n->rdx != len) return len;
6265 for (;;) {
6266 if (len == 0) break;
6267 len--;
6268 if (n->num[len] != 0) break;
6269 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006270 return len;
6271}
6272
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006273static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006274{
6275 BcStatus s;
6276 BcResult *opnd;
6277 BcNum *num = NULL;
6278 BcResult res;
6279 bool len = inst == BC_INST_LENGTH;
6280
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006281 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006282 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006283 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006284
Denys Vlasenko29301232018-12-11 15:29:32 +01006285 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006286 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006287
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006288#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006289 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006290 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006291#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006292
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006293 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006294
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006295 if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006296#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006297 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006298 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006299 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006300#endif
6301#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006302 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006303 char **str;
6304 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6305
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006306 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006307 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006308 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006309#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006310 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006311 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006312 }
6313
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006314 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006315
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006316 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006317}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006318#if ERRORS_ARE_FATAL
6319# define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
6320#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006321
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006322#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006323static BC_STATUS zbc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006324{
6325 BcStatus s;
6326 BcResult *opd1, *opd2, res, res2;
6327 BcNum *n1, *n2 = NULL;
6328
Denys Vlasenko29301232018-12-11 15:29:32 +01006329 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006330 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006331
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006332 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006333 bc_num_init(&res2.d.n, n2->len);
6334
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006335 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006336 if (s) goto err;
6337
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006338 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006339 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006340 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006341
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006342 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006343
6344err:
6345 bc_num_free(&res2.d.n);
6346 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006347 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006348}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006349#if ERRORS_ARE_FATAL
6350# define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
6351#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006352
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006353static BC_STATUS zbc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006354{
6355 BcStatus s;
6356 BcResult *r1, *r2, *r3, res;
6357 BcNum *n1, *n2, *n3;
6358
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006359 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006360 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006361 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006362 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006363
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006364 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006365 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006366 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006367 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006368 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006369
6370 // Make sure that the values have their pointers updated, if necessary.
6371 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6372
6373 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006374 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006375 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006376 }
6377
6378 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006379 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006380 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006381 }
6382 }
6383
6384 bc_num_init(&res.d.n, n3->len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006385 s = zbc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006386 if (s) goto err;
6387
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006388 bc_vec_pop(&G.prog.results);
6389 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006390
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006391 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006392
6393err:
6394 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006395 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006396}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006397#if ERRORS_ARE_FATAL
6398# define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
6399#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006400
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006401static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006402{
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006404 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006405
6406 res.t = BC_RESULT_TEMP;
6407
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006408 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006409 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006410 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006411}
6412
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006413static BC_STATUS zbc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006414{
6415 BcStatus s;
6416 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006417 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006418 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006419 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006420 unsigned long val;
6421
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006422 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006423 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006424 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006425
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006426 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006427 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006428 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006429
6430 if (BC_PROG_NUM(r, num)) {
6431
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006432 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006433 bc_num_copy(&n, num);
6434 bc_num_truncate(&n, n.rdx);
6435
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006436 s = zbc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006437 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006438 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006439 if (s) goto num_err;
6440
6441 c = (char) val;
6442
6443 bc_num_free(&n);
6444 }
6445 else {
6446 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006447 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006448 c = str2[0];
6449 }
6450
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006451 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006452 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006453 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006454
6455 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006456 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006457
6458 if (idx != len + BC_PROG_REQ_FUNCS) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006459 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006460 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006461 len = idx;
6462 break;
6463 }
6464 }
6465
6466 free(str);
6467 }
6468 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006469 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006470
6471 res.t = BC_RESULT_STR;
6472 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006473 bc_vec_pop(&G.prog.results);
6474 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006475
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006476 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006477
6478num_err:
6479 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006480 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006481}
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006482#if ERRORS_ARE_FATAL
6483# define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__), BC_STATUS_SUCCESS)
6484#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006485
Denys Vlasenko29301232018-12-11 15:29:32 +01006486static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006487{
6488 BcStatus s;
6489 BcResult *r;
6490 BcNum *n = NULL;
6491 size_t idx;
6492 char *str;
6493
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006494 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006495 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006496 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006497
Denys Vlasenko29301232018-12-11 15:29:32 +01006498 s = zbc_program_num(r, &n, false);
6499 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006500
6501 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006502 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006503 else {
6504 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006505 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006506 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006507 }
6508
Denys Vlasenko29301232018-12-11 15:29:32 +01006509 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006510}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006511#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006512# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006513#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006514
Denys Vlasenko29301232018-12-11 15:29:32 +01006515static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006516{
6517 BcStatus s;
6518 BcResult *opnd;
6519 BcNum *num = NULL;
6520 unsigned long val;
6521
Denys Vlasenko29301232018-12-11 15:29:32 +01006522 s = zbc_program_prep(&opnd, &num);
6523 if (s) RETURN_STATUS(s);
6524 s = zbc_num_ulong(num, &val);
6525 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006526
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006527 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006528
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006529 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006530 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006531 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006532 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006533 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006534
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006535 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006536
Denys Vlasenko29301232018-12-11 15:29:32 +01006537 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006538}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006539#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006540# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006541#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006542
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006543static BcStatus bc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006544 bool cond)
6545{
6546 BcStatus s = BC_STATUS_SUCCESS;
6547 BcResult *r;
6548 char **str;
6549 BcFunc *f;
6550 BcParse prs;
6551 BcInstPtr ip;
6552 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006553
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006554 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006555 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006556
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006557 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006558
6559 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006560 BcNum *n = n; // for compiler
6561 bool exec;
6562 char *name;
6563 char *then_name = bc_program_name(code, bgn);
6564 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006565
6566 if (code[*bgn] == BC_PARSE_STREND)
6567 (*bgn) += 1;
6568 else
6569 else_name = bc_program_name(code, bgn);
6570
6571 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006572 name = then_name;
6573 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006574 exec = true;
6575 name = else_name;
6576 }
6577
6578 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006579 BcVec *v;
6580 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006581 n = bc_vec_top(v);
6582 }
6583
6584 free(then_name);
6585 free(else_name);
6586
6587 if (!exec) goto exit;
6588 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006589 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006590 goto exit;
6591 }
6592
6593 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006594 } else {
6595 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006596 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006597 } else if (r->t == BC_RESULT_VAR) {
6598 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006599 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006600 if (s || !BC_PROG_STR(n)) goto exit;
6601 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006602 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006603 goto exit;
6604 }
6605
6606 fidx = sidx + BC_PROG_REQ_FUNCS;
6607
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006608 str = bc_program_str(sidx);
6609 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006610
6611 if (f->code.len == 0) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006612 common_parse_init(&prs, fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006613 s = bc_parse_text(&prs, *str);
6614 if (s) goto err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01006615 s = common_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006616 if (s) goto err;
6617
6618 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006619 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006620 goto err;
6621 }
6622
6623 bc_parse_free(&prs);
6624 }
6625
6626 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006627 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006628 ip.func = fidx;
6629
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006630 bc_vec_pop(&G.prog.results);
6631 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006632
6633 return BC_STATUS_SUCCESS;
6634
6635err:
6636 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006637 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006638 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006639exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006640 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006641 return s;
6642}
6643#endif // ENABLE_DC
6644
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006645static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006646{
Gavin Howard01055ba2018-11-03 11:00:21 -06006647 BcResult res;
6648 unsigned long val;
6649
6650 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6651 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006652 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006653 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006654 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006655 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006656 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006657
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006658 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006659 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006660 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006661}
6662
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006663static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006664{
Gavin Howard01055ba2018-11-03 11:00:21 -06006665 BcId entry, *entry_ptr;
6666 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006667 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006668
6669 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006670 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006671
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006672 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6673 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006674
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006675 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006676 *idx = entry_ptr->idx;
6677
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006678 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006679
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006680 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006681
6682 // We need to reset these, so the function can be repopulated.
6683 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006684 bc_vec_pop_all(&func->autos);
6685 bc_vec_pop_all(&func->code);
6686 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006687 }
6688 else {
6689 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006690 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006691 }
6692}
6693
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006694static BcStatus bc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006695{
Gavin Howard01055ba2018-11-03 11:00:21 -06006696 BcResult r, *ptr;
6697 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006698 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006699 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006700 char *code = func->code.v;
6701 bool cond = false;
6702
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006703 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006704 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006705 char inst = code[(ip->idx)++];
6706
6707 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006708#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006709 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006710 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006711 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006712 cond = !bc_num_cmp(num, &G.prog.zero);
6713 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006714 // Fallthrough.
6715 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006716 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006717 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006718 addr = bc_vec_item(&func->labels, idx);
6719 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6720 break;
6721 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006722 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006723 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006724 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006725 case BC_INST_INC_PRE:
6726 case BC_INST_DEC_PRE:
6727 case BC_INST_INC_POST:
6728 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006729 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006730 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006731 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006732 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006733 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006734 case BC_INST_RET:
6735 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006736 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006737 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006738 case BC_INST_BOOL_OR:
6739 case BC_INST_BOOL_AND:
6740#endif // ENABLE_BC
6741 case BC_INST_REL_EQ:
6742 case BC_INST_REL_LE:
6743 case BC_INST_REL_GE:
6744 case BC_INST_REL_NE:
6745 case BC_INST_REL_LT:
6746 case BC_INST_REL_GT:
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006747 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006748 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006749 case BC_INST_READ:
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006750 s = bc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006751 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006752 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006753 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006754 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006755 case BC_INST_ARRAY_ELEM:
6756 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006757 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006758 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006759 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006760 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006761 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006762 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006763 case BC_INST_IBASE:
6764 case BC_INST_SCALE:
6765 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006766 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006767 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006768 case BC_INST_SCALE_FUNC:
6769 case BC_INST_LENGTH:
6770 case BC_INST_SQRT:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006771 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006772 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006773 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006774 r.t = BC_RESULT_CONSTANT;
6775 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006776 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006777 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006778 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006779 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006780 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006781 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006782 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006783 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006784 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006785 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006786 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006787 case BC_INST_PRINT:
6788 case BC_INST_PRINT_POP:
6789 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006790 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006791 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006792 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006793 r.t = BC_RESULT_STR;
6794 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006795 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006796 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006797 case BC_INST_POWER:
6798 case BC_INST_MULTIPLY:
6799 case BC_INST_DIVIDE:
6800 case BC_INST_MODULUS:
6801 case BC_INST_PLUS:
6802 case BC_INST_MINUS:
Denys Vlasenko259137d2018-12-11 19:42:05 +01006803 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006804 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006805 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006806 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006807 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006808 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006809 if (!bc_num_cmp(num, &G.prog.zero))
6810 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006811 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006812 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006813 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006814 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006815 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006816 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006817#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006818 case BC_INST_ASSIGN_POWER:
6819 case BC_INST_ASSIGN_MULTIPLY:
6820 case BC_INST_ASSIGN_DIVIDE:
6821 case BC_INST_ASSIGN_MODULUS:
6822 case BC_INST_ASSIGN_PLUS:
6823 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006824#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006825 case BC_INST_ASSIGN:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006826 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006827 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006828#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006829 case BC_INST_MODEXP:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006830 s = zbc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006831 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006832 case BC_INST_DIVMOD:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006833 s = zbc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006834 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006835 case BC_INST_EXECUTE:
6836 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006837 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006838 s = bc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006839 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006840 case BC_INST_PRINT_STACK: {
6841 size_t idx;
6842 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006843 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006844 if (s) break;
6845 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006846 break;
6847 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006848 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006849 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006850 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006851 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006852 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006853 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006854 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006855 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006856 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006857 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006858 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006859 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006860 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006861 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006862 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006863 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006864 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006865 ptr = bc_vec_item_rev(&G.prog.results, 0);
6866 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006867 memcpy(&r, ptr, sizeof(BcResult));
6868 memcpy(ptr, ptr2, sizeof(BcResult));
6869 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006870 break;
6871 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006872 case BC_INST_ASCIIFY:
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006873 s = zbc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006874 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006875 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006876 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006877 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006878 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006879 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006880 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006881 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006882 break;
6883 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006884 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006885 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006886 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006887 free(name);
6888 break;
6889 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006890 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006891 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006892 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006893 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006894 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006895 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006896 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006897 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006898#endif // ENABLE_DC
6899 }
6900
Denys Vlasenkod38af482018-12-04 19:11:02 +01006901 if (s || G_interrupt) {
6902 bc_program_reset();
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006903 return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01006904 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006905
6906 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006907 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006908 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006909 code = func->code.v;
6910 }
6911
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006912 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006913}
6914
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006915#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01006916static void bc_vm_info(void)
6917{
6918 printf("%s "BB_VER"\n"
6919 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01006920 , applet_name);
6921}
6922
6923static void bc_args(char **argv)
6924{
6925 unsigned opts;
6926 int i;
6927
6928 GETOPT_RESET();
6929#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006930 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01006931 "warn\0" No_argument "w"
6932 "version\0" No_argument "v"
6933 "standard\0" No_argument "s"
6934 "quiet\0" No_argument "q"
6935 "mathlib\0" No_argument "l"
6936 "interactive\0" No_argument "i"
6937 );
6938#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006939 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01006940#endif
6941 if (getenv("POSIXLY_CORRECT"))
6942 option_mask32 |= BC_FLAG_S;
6943
Denys Vlasenko54214c32018-12-06 09:07:06 +01006944 if (opts & BC_FLAG_V) {
6945 bc_vm_info();
6946 exit(0);
6947 }
6948
6949 for (i = optind; argv[i]; ++i)
6950 bc_vec_push(&G.files, argv + i);
6951}
6952
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006953static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006954{
Gavin Howard01055ba2018-11-03 11:00:21 -06006955 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006956 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01006957 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06006958
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01006959 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06006960
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006961 G.env_args = xstrdup(env_args);
6962 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06006963
6964 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006965
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006966 while (*(buf = skip_whitespace(buf)) != '\0') {
6967 bc_vec_push(&v, &buf);
6968 buf = skip_non_whitespace(buf);
6969 if (!*buf)
6970 break;
6971 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06006972 }
6973
Denys Vlasenko54214c32018-12-06 09:07:06 +01006974 // NULL terminate, and pass argv[] so that first arg is argv[1]
6975 if (sizeof(int) == sizeof(char*)) {
6976 bc_vec_push(&v, &const_int_0);
6977 } else {
6978 static char *const nullptr = NULL;
6979 bc_vec_push(&v, &nullptr);
6980 }
6981 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006982
6983 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006984}
6985#endif // ENABLE_BC
6986
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006987static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006988{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006989 char *lenv;
6990 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006991
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006992 lenv = getenv(var);
6993 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006994 if (!lenv) return len;
6995
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006996 len = bb_strtou(lenv, NULL, 10) - 1;
6997 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006998 len = BC_NUM_PRINT_WIDTH;
6999
7000 return len;
7001}
7002
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007003static BcStatus bc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06007004{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007005 BcStatus s = bc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06007006
Gavin Howard01055ba2018-11-03 11:00:21 -06007007 if (s) return s;
7008
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007009 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007010 s = G.prs.parse(&G.prs);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01007011 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007012 }
7013
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007014 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007015 s = bc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01007016 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007017 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01007018 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06007019 }
7020
7021 return s;
7022}
7023
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007024static BcStatus bc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007025{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007026 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007027 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007028 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007029 BcFunc *main_func;
7030 BcInstPtr *ip;
7031
Denys Vlasenkodf515392018-12-02 19:27:48 +01007032 data = bc_read_file(file);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007033 if (!data) return bc_error_fmt("file '%s' is not text", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007034
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007035 sv_file = G.prog.file;
7036 G.prog.file = file;
7037 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007038 s = bc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007039 if (s) goto err;
7040
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007041 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007042 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007043
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007044 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007045 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007046
7047err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007048 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007049 free(data);
7050 return s;
7051}
7052
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007053static BcStatus bc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007054{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007055 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007056 BcVec buf, buffer;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007057 size_t str;
7058 bool comment;
Gavin Howard01055ba2018-11-03 11:00:21 -06007059
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007060 G.prog.file = NULL;
7061 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007062
Denys Vlasenko7d628012018-12-04 21:46:47 +01007063 bc_char_vec_init(&buffer);
7064 bc_char_vec_init(&buf);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01007065 bc_vec_pushZeroByte(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007066
7067 // This loop is complex because the vm tries not to send any lines that end
7068 // with a backslash to the parser. The reason for that is because the parser
7069 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7070 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007071 comment = false;
7072 str = 0;
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007073 while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) {
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007074 size_t len;
Gavin Howard01055ba2018-11-03 11:00:21 -06007075 char *string = buf.v;
7076
7077 len = buf.len - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06007078 if (len == 1) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007079 if (str && buf.v[0] == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007080 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007081 else if (buf.v[0] == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007082 str += 1;
7083 }
7084 else if (len > 1 || comment) {
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007085 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06007086 for (i = 0; i < len; ++i) {
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007087 bool notend = len > i + 1;
7088 char c = string[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06007089
7090 if (i - 1 > len || string[i - 1] != '\\') {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007091 if (G.sbgn == G.send)
7092 str ^= c == G.sbgn;
7093 else if (c == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007094 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007095 else if (c == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007096 str += 1;
7097 }
7098
7099 if (c == '/' && notend && !comment && string[i + 1] == '*') {
7100 comment = true;
7101 break;
7102 }
7103 else if (c == '*' && notend && comment && string[i + 1] == '/')
7104 comment = false;
7105 }
7106
7107 if (str || comment || string[len - 2] == '\\') {
7108 bc_vec_concat(&buffer, buf.v);
7109 continue;
7110 }
7111 }
7112
7113 bc_vec_concat(&buffer, buf.v);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007114 s = bc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007115 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007116 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007117 // Debug config, non-interactive mode:
7118 // return all the way back to main.
7119 // Non-debug builds do not come here, they exit.
7120 break;
7121 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007122 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007123
Denys Vlasenko7d628012018-12-04 21:46:47 +01007124 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007125 }
Denys Vlasenkof522dd92018-12-07 16:35:43 +01007126 if (s == BC_STATUS_EOF) // input EOF (^D) is not an error
7127 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007128
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007129 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007130 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007131 }
7132 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007133 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007134 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007135
Gavin Howard01055ba2018-11-03 11:00:21 -06007136 bc_vec_free(&buf);
7137 bc_vec_free(&buffer);
7138 return s;
7139}
7140
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007141#if ENABLE_BC
7142static const char bc_lib[] = {
7143 "scale=20"
7144"\n" "define e(x){"
7145"\n" "auto b,s,n,r,d,i,p,f,v"
7146"\n" "b=ibase"
7147"\n" "ibase=A"
7148"\n" "if(x<0){"
7149"\n" "n=1"
7150"\n" "x=-x"
7151"\n" "}"
7152"\n" "s=scale"
7153"\n" "r=6+s+0.44*x"
7154"\n" "scale=scale(x)+1"
7155"\n" "while(x>1){"
7156"\n" "d+=1"
7157"\n" "x/=2"
7158"\n" "scale+=1"
7159"\n" "}"
7160"\n" "scale=r"
7161"\n" "r=x+1"
7162"\n" "p=x"
7163"\n" "f=v=1"
7164"\n" "for(i=2;v!=0;++i){"
7165"\n" "p*=x"
7166"\n" "f*=i"
7167"\n" "v=p/f"
7168"\n" "r+=v"
7169"\n" "}"
7170"\n" "while((d--)!=0)r*=r"
7171"\n" "scale=s"
7172"\n" "ibase=b"
7173"\n" "if(n!=0)return(1/r)"
7174"\n" "return(r/1)"
7175"\n" "}"
7176"\n" "define l(x){"
7177"\n" "auto b,s,r,p,a,q,i,v"
7178"\n" "b=ibase"
7179"\n" "ibase=A"
7180"\n" "if(x<=0){"
7181"\n" "r=(1-10^scale)/1"
7182"\n" "ibase=b"
7183"\n" "return(r)"
7184"\n" "}"
7185"\n" "s=scale"
7186"\n" "scale+=6"
7187"\n" "p=2"
7188"\n" "while(x>=2){"
7189"\n" "p*=2"
7190"\n" "x=sqrt(x)"
7191"\n" "}"
7192"\n" "while(x<=0.5){"
7193"\n" "p*=2"
7194"\n" "x=sqrt(x)"
7195"\n" "}"
7196"\n" "r=a=(x-1)/(x+1)"
7197"\n" "q=a*a"
7198"\n" "v=1"
7199"\n" "for(i=3;v!=0;i+=2){"
7200"\n" "a*=q"
7201"\n" "v=a/i"
7202"\n" "r+=v"
7203"\n" "}"
7204"\n" "r*=p"
7205"\n" "scale=s"
7206"\n" "ibase=b"
7207"\n" "return(r/1)"
7208"\n" "}"
7209"\n" "define s(x){"
7210"\n" "auto b,s,r,n,a,q,i"
7211"\n" "b=ibase"
7212"\n" "ibase=A"
7213"\n" "s=scale"
7214"\n" "scale=1.1*s+2"
7215"\n" "a=a(1)"
7216"\n" "if(x<0){"
7217"\n" "n=1"
7218"\n" "x=-x"
7219"\n" "}"
7220"\n" "scale=0"
7221"\n" "q=(x/a+2)/4"
7222"\n" "x=x-4*q*a"
7223"\n" "if(q%2!=0)x=-x"
7224"\n" "scale=s+2"
7225"\n" "r=a=x"
7226"\n" "q=-x*x"
7227"\n" "for(i=3;a!=0;i+=2){"
7228"\n" "a*=q/(i*(i-1))"
7229"\n" "r+=a"
7230"\n" "}"
7231"\n" "scale=s"
7232"\n" "ibase=b"
7233"\n" "if(n!=0)return(-r/1)"
7234"\n" "return(r/1)"
7235"\n" "}"
7236"\n" "define c(x){"
7237"\n" "auto b,s"
7238"\n" "b=ibase"
7239"\n" "ibase=A"
7240"\n" "s=scale"
7241"\n" "scale*=1.2"
7242"\n" "x=s(2*a(1)+x)"
7243"\n" "scale=s"
7244"\n" "ibase=b"
7245"\n" "return(x/1)"
7246"\n" "}"
7247"\n" "define a(x){"
7248"\n" "auto b,s,r,n,a,m,t,f,i,u"
7249"\n" "b=ibase"
7250"\n" "ibase=A"
7251"\n" "n=1"
7252"\n" "if(x<0){"
7253"\n" "n=-1"
7254"\n" "x=-x"
7255"\n" "}"
7256"\n" "if(x==1){"
7257"\n" "if(scale<65){"
7258"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7259"\n" "}"
7260"\n" "}"
7261"\n" "if(x==.2){"
7262"\n" "if(scale<65){"
7263"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7264"\n" "}"
7265"\n" "}"
7266"\n" "s=scale"
7267"\n" "if(x>.2){"
7268"\n" "scale+=5"
7269"\n" "a=a(.2)"
7270"\n" "}"
7271"\n" "scale=s+3"
7272"\n" "while(x>.2){"
7273"\n" "m+=1"
7274"\n" "x=(x-.2)/(1+.2*x)"
7275"\n" "}"
7276"\n" "r=u=x"
7277"\n" "f=-x*x"
7278"\n" "t=1"
7279"\n" "for(i=3;t!=0;i+=2){"
7280"\n" "u*=f"
7281"\n" "t=u/i"
7282"\n" "r+=t"
7283"\n" "}"
7284"\n" "scale=s"
7285"\n" "ibase=b"
7286"\n" "return((m*a+r)/n)"
7287"\n" "}"
7288"\n" "define j(n,x){"
7289"\n" "auto b,s,o,a,i,v,f"
7290"\n" "b=ibase"
7291"\n" "ibase=A"
7292"\n" "s=scale"
7293"\n" "scale=0"
7294"\n" "n/=1"
7295"\n" "if(n<0){"
7296"\n" "n=-n"
7297"\n" "if(n%2==1)o=1"
7298"\n" "}"
7299"\n" "a=1"
7300"\n" "for(i=2;i<=n;++i)a*=i"
7301"\n" "scale=1.5*s"
7302"\n" "a=(x^n)/2^n/a"
7303"\n" "r=v=1"
7304"\n" "f=-x*x/4"
7305"\n" "scale=scale+length(a)-scale(a)"
7306"\n" "for(i=1;v!=0;++i){"
7307"\n" "v=v*f/i/(n+i)"
7308"\n" "r+=v"
7309"\n" "}"
7310"\n" "scale=s"
7311"\n" "ibase=b"
7312"\n" "if(o!=0)a=-a"
7313"\n" "return(a*r/1)"
7314"\n" "}"
7315};
7316#endif // ENABLE_BC
7317
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007318static BcStatus bc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007319{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007320 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007321 size_t i;
7322
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007323#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007324 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007325
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007326 // We know that internal library is not buggy,
7327 // thus error checking is normally disabled.
7328# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007329 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007330 s = bc_parse_text(&G.prs, bc_lib);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007331 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007332
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007333 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007334 s = G.prs.parse(&G.prs);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007335 if (DEBUG_LIB && s) return s;
7336 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007337 s = bc_program_exec();
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007338 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007339 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007340#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007341
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007342 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007343 for (i = 0; !s && i < G.files.len; ++i)
7344 s = bc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007345 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7346 // Debug config, non-interactive mode:
7347 // return all the way back to main.
7348 // Non-debug builds do not come here, they exit.
7349 return s;
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007350 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007351
Denys Vlasenko91cde952018-12-10 20:56:08 +01007352 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007353 s = bc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007354
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007355 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
7356 s = bc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007357
Denys Vlasenko00d77792018-11-30 23:13:42 +01007358 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007359}
7360
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007361#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007362static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007363{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007364 bc_num_free(&G.prog.ib);
7365 bc_num_free(&G.prog.ob);
7366 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007367# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007368 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007369# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007370 bc_vec_free(&G.prog.fns);
7371 bc_vec_free(&G.prog.fn_map);
7372 bc_vec_free(&G.prog.vars);
7373 bc_vec_free(&G.prog.var_map);
7374 bc_vec_free(&G.prog.arrs);
7375 bc_vec_free(&G.prog.arr_map);
7376 bc_vec_free(&G.prog.strs);
7377 bc_vec_free(&G.prog.consts);
7378 bc_vec_free(&G.prog.results);
7379 bc_vec_free(&G.prog.stack);
7380 bc_num_free(&G.prog.last);
7381 bc_num_free(&G.prog.zero);
7382 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007383}
7384
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007385static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007386{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007387 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007388 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007389 bc_parse_free(&G.prs);
7390 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007391}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007392#endif
7393
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007394static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007395{
7396 size_t idx;
7397 BcInstPtr ip;
7398
7399 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7400 memset(&ip, 0, sizeof(BcInstPtr));
7401
7402 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007403 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007404 bc_num_ten(&G.prog.ib);
7405 G.prog.ib_t = 10;
7406
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007407 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007408 bc_num_ten(&G.prog.ob);
7409 G.prog.ob_t = 10;
7410
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007411 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007412 bc_num_ten(&G.prog.hexb);
7413 G.prog.hexb.num[0] = 6;
7414
7415#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007416 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007417 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7418#endif
7419
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007420 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007421 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007422
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007423 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007424 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007425
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007426 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007427 bc_num_one(&G.prog.one);
7428
7429 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007430 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007431
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007432 bc_program_addFunc(xstrdup("(main)"), &idx);
7433 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007434
7435 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007436 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007437
7438 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007439 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007440
7441 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7442 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7443 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7444 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7445 bc_vec_push(&G.prog.stack, &ip);
7446}
Gavin Howard01055ba2018-11-03 11:00:21 -06007447
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007448static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007449{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007450#if ENABLE_FEATURE_EDITING
7451 G.line_input_state = new_line_input_t(DO_HISTORY);
7452#endif
7453 G.prog.len = bc_vm_envLen(env_len);
7454
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007455 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007456 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007457 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007458 bc_program_init();
Denys Vlasenkof0f069b2018-12-11 23:22:52 +01007459 common_parse_init(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007460
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007461 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007462#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007463 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007464 // With SA_RESTART, most system calls will restart
7465 // (IOW: they won't fail with EINTR).
7466 // In particular, this means ^C won't cause
7467 // stdout to get into "error state" if SIGINT hits
7468 // within write() syscall.
7469 // The downside is that ^C while line input is taken
7470 // will only be handled after [Enter] since read()
7471 // from stdin is not interrupted by ^C either,
7472 // it restarts, thus fgetc() does not return on ^C.
7473 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7474
7475 // Without SA_RESTART, this exhibits a bug:
7476 // "while (1) print 1" and try ^C-ing it.
7477 // Intermittently, instead of returning to input line,
7478 // you'll get "output error: Interrupted system call"
7479 // and exit.
7480 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007481#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007482 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007483 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007484 return 0; // "not a tty"
7485}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007486
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007487static BcStatus bc_vm_run(void)
7488{
7489 BcStatus st = bc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007490#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007491 if (G_exiting) // it was actually "halt" or "quit"
7492 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007493 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007494# if ENABLE_FEATURE_EDITING
7495 free_line_input_t(G.line_input_state);
7496# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007497 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007498#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007499 return st;
7500}
7501
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007502#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007503int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007504int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007505{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007506 int is_tty;
7507
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007508 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007509 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007510
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007511 is_tty = bc_vm_init("BC_LINE_LENGTH");
7512
7513 bc_args(argv);
7514
7515 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7516 bc_vm_info();
7517
7518 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007519}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007520#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007521
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007522#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007523int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007524int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007525{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007526 int noscript;
7527
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007528 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007529 G.sbgn = '[';
7530 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007531 /*
7532 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7533 * 1 char wider than bc from the same package.
7534 * Both default width, and xC_LINE_LENGTH=N are wider:
7535 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7536 * 1234\
7537 * 56
7538 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7539 * 123\
7540 * 456
7541 * Do the same, or it's a bug?
7542 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007543 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007544
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007545 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7546 noscript = BC_FLAG_I;
7547 for (;;) {
7548 int n = getopt(argc, argv, "e:f:x");
7549 if (n <= 0)
7550 break;
7551 switch (n) {
7552 case 'e':
7553 noscript = 0;
7554 n = bc_vm_process(optarg);
7555 if (n) return n;
7556 break;
7557 case 'f':
7558 noscript = 0;
7559 bc_vm_file(optarg);
7560 break;
7561 case 'x':
7562 option_mask32 |= DC_FLAG_X;
7563 break;
7564 default:
7565 bb_show_usage();
7566 }
7567 }
7568 argv += optind;
7569
7570 while (*argv) {
7571 noscript = 0;
7572 bc_vec_push(&G.files, argv++);
7573 }
7574
7575 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7576
7577 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007578}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007579#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007580
7581#endif // not DC_SMALL