blob: 0908d7cb90ddc5f37c1a7f6333c20d302e99cb3c [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.
5 *
6 * ** Automatically generated from https://github.com/gavinhoward/bc **
7 * ** Do not edit unless you know what you are doing. **
8 */
9//config:config BC
10//config: bool "bc (45 kb; 49 kb when combined with dc)"
11//config: default y
12//config: help
13//config: bc is a command-line, arbitrary-precision calculator with a
14//config: Turing-complete language. See the GNU bc manual
15//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
16//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
17//config: for details.
18//config:
19//config: This bc has four differences to the GNU bc:
20//config:
21//config: 1) The period (.) can also be used as a shortcut for "last", as in
22//config: the BSD bc.
23//config: 2) Arrays are copied before being passed as arguments to
24//config: functions. This behavior is required by the bc spec.
25//config: 3) Arrays can be passed to the builtin "length" function to get
26//config: the number of elements currently in the array. The following
27//config: example prints "1":
28//config:
29//config: a[0] = 0
30//config: length(a[])
31//config:
32//config: 4) The precedence of the boolean "not" operator (!) is equal to
33//config: that of the unary minus (-), or negation, operator. This still
34//config: allows POSIX-compliant scripts to work while somewhat
35//config: preserving expected behavior (versus C) and making parsing
36//config: easier.
37//config:
38//config: Options:
39//config:
40//config: -i --interactive force interactive mode
41//config: -l --mathlib use predefined math routines:
42//config:
43//config: s(expr) = sine of expr in radians
44//config: c(expr) = cosine of expr in radians
45//config: a(expr) = arctangent of expr, returning
46//config: radians
47//config: l(expr) = natural log of expr
48//config: e(expr) = raises e to the power of expr
49//config: j(n, x) = Bessel function of integer order
50//config: n of x
51//config:
52//config: -q --quiet don't print version and copyright.
53//config: -s --standard error if any non-POSIX extensions are used.
54//config: -w --warn warn if any non-POSIX extensions are used.
55//config: -v --version print version and copyright and exit.
56//config:
57//config: Long options are only available if FEATURE_BC_LONG_OPTIONS is
58//config: enabled.
59//config:
60//config:config DC
61//config: bool "dc (38 kb; 49 kb when combined with bc)"
62//config: default y
63//config: help
64//config: dc is a reverse-polish notation command-line calculator which
65//config: supports unlimited precision arithmetic. See the FreeBSD man page
66//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
67//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html)
68//config: for details.
69//config:
70//config: This dc has a few differences from the two above:
71//config:
72//config: 1) When printing a byte stream (command "P"), this bc follows what
73//config: the FreeBSD dc does.
74//config: 2) This dc implements the GNU extensions for divmod ("~") and
75//config: modular exponentiation ("|").
76//config: 3) This dc implements all FreeBSD extensions, except for "J" and
77//config: "M".
78//config: 4) Like the FreeBSD dc, this dc supports extended registers.
79//config: However, they are implemented differently. When it encounters
80//config: whitespace where a register should be, it skips the whitespace.
81//config: If the character following is not a lowercase letter, an error
82//config: is issued. Otherwise, the register name is parsed by the
83//config: following regex:
84//config:
85//config: [a-z][a-z0-9_]*
86//config:
87//config: This generally means that register names will be surrounded by
88//config: whitespace.
89//config:
90//config: Examples:
91//config:
92//config: l idx s temp L index S temp2 < do_thing
93//config:
94//config: Also note that, like the FreeBSD dc, extended registers are not
95//config: allowed unless the "-x" option is given.
96//config:
97//config:config FEATURE_BC_SIGNALS
98//config: bool "Enable bc/dc signal handling"
99//config: default y
100//config: depends on BC || DC
101//config: help
102//config: Enable signal handling for bc and dc.
103//config:
104//config:config FEATURE_BC_LONG_OPTIONS
105//config: bool "Enable bc/dc long options"
106//config: default y
107//config: depends on BC || DC
108//config: help
109//config: Enable long options for bc and dc.
110
111//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
112//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
113
114//kbuild:lib-$(CONFIG_BC) += bc.o
115//kbuild:lib-$(CONFIG_DC) += bc.o
116
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100117//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600118//usage:#define bc_trivial_usage
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100119//usage: "[-sqli] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600120//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100121//usage:#define bc_full_usage "\n"
122//usage: "\nArbitrary precision calculator"
123//usage: "\n"
124//usage: "\n -i Interactive"
125//usage: "\n -l Load standard math library"
126//usage: "\n -s Be POSIX compatible"
127//usage: "\n -q Quiet"
128//usage: "\n -w Warn if extensions are used"
129///////: "\n -v Version"
Gavin Howard01055ba2018-11-03 11:00:21 -0600130//usage:
131//usage:#define bc_example_usage
132//usage: "3 + 4.129\n"
133//usage: "1903 - 2893\n"
134//usage: "-129 * 213.28935\n"
135//usage: "12 / -1932\n"
136//usage: "12 % 12\n"
137//usage: "34 ^ 189\n"
138//usage: "scale = 13\n"
139//usage: "ibase = 2\n"
140//usage: "obase = A\n"
141//usage:
142//usage:#define dc_trivial_usage
143//usage: "EXPRESSION..."
144//usage:
145//usage:#define dc_full_usage "\n\n"
146//usage: "Tiny RPN calculator. Operations:\n"
147//usage: "+, add, -, sub, *, mul, /, div, %, mod, ^, exp, ~, divmod, |, "
148//usage: "modular exponentiation,\n"
149//usage: "p - print top of the stack (without popping),\n"
150//usage: "f - print entire stack,\n"
151//usage: "k - pop the value and set the precision.\n"
152//usage: "i - pop the value and set input radix.\n"
153//usage: "o - pop the value and set output radix.\n"
154//usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
155//usage:
156//usage:#define dc_example_usage
157//usage: "$ dc 2 2 + p\n"
158//usage: "4\n"
159//usage: "$ dc 8 8 \\* 2 2 + / p\n"
160//usage: "16\n"
161//usage: "$ dc 0 1 and p\n"
162//usage: "0\n"
163//usage: "$ dc 0 1 or p\n"
164//usage: "1\n"
165//usage: "$ echo 72 9 div 8 mul p | dc\n"
166//usage: "64\n"
167
168#include "libbb.h"
169
170typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100171 BC_STATUS_SUCCESS = 0,
172 BC_STATUS_FAILURE = 1,
173 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600174} BcStatus;
175
Gavin Howard01055ba2018-11-03 11:00:21 -0600176#define BC_VEC_INVALID_IDX ((size_t) -1)
177#define BC_VEC_START_CAP (1 << 5)
178
179typedef void (*BcVecFree)(void *);
Gavin Howard01055ba2018-11-03 11:00:21 -0600180
181typedef struct BcVec {
182 char *v;
183 size_t len;
184 size_t cap;
185 size_t size;
186 BcVecFree dtor;
187} BcVec;
188
189#define bc_vec_pop(v) (bc_vec_npop((v), 1))
190#define bc_vec_top(v) (bc_vec_item_rev((v), 0))
191
Gavin Howard01055ba2018-11-03 11:00:21 -0600192typedef signed char BcDig;
193
194typedef struct BcNum {
195 BcDig *restrict num;
196 size_t rdx;
197 size_t len;
198 size_t cap;
199 bool neg;
200} BcNum;
201
202#define BC_NUM_MIN_BASE ((unsigned long) 2)
203#define BC_NUM_MAX_IBASE ((unsigned long) 16)
204#define BC_NUM_DEF_SIZE (16)
205#define BC_NUM_PRINT_WIDTH (69)
206
207#define BC_NUM_KARATSUBA_LEN (32)
208
209#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
210#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
211#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
212#define BC_NUM_AREQ(a, b) \
213 (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
214#define BC_NUM_MREQ(a, b, scale) \
215 (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
216
217typedef BcStatus (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t);
218typedef void (*BcNumDigitOp)(size_t, size_t, bool, size_t *, size_t);
219
220static void bc_num_init(BcNum *n, size_t req);
221static void bc_num_expand(BcNum *n, size_t req);
222static void bc_num_copy(BcNum *d, BcNum *s);
223static void bc_num_free(void *num);
224
225static BcStatus bc_num_ulong(BcNum *n, unsigned long *result);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +0100226static void bc_num_ulong2num(BcNum *n, unsigned long val);
Gavin Howard01055ba2018-11-03 11:00:21 -0600227
228static BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale);
229static BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale);
230static BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale);
231static BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale);
232static BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale);
233static BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale);
234static BcStatus bc_num_sqrt(BcNum *a, BcNum *b, size_t scale);
235static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
236 size_t scale);
237
238typedef enum BcInst {
239
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100240#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600241 BC_INST_INC_PRE,
242 BC_INST_DEC_PRE,
243 BC_INST_INC_POST,
244 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100245#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600246
247 BC_INST_NEG,
248
249 BC_INST_POWER,
250 BC_INST_MULTIPLY,
251 BC_INST_DIVIDE,
252 BC_INST_MODULUS,
253 BC_INST_PLUS,
254 BC_INST_MINUS,
255
256 BC_INST_REL_EQ,
257 BC_INST_REL_LE,
258 BC_INST_REL_GE,
259 BC_INST_REL_NE,
260 BC_INST_REL_LT,
261 BC_INST_REL_GT,
262
263 BC_INST_BOOL_NOT,
264 BC_INST_BOOL_OR,
265 BC_INST_BOOL_AND,
266
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100267#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600268 BC_INST_ASSIGN_POWER,
269 BC_INST_ASSIGN_MULTIPLY,
270 BC_INST_ASSIGN_DIVIDE,
271 BC_INST_ASSIGN_MODULUS,
272 BC_INST_ASSIGN_PLUS,
273 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100274#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600275 BC_INST_ASSIGN,
276
277 BC_INST_NUM,
278 BC_INST_VAR,
279 BC_INST_ARRAY_ELEM,
280 BC_INST_ARRAY,
281
282 BC_INST_SCALE_FUNC,
283 BC_INST_IBASE,
284 BC_INST_SCALE,
285 BC_INST_LAST,
286 BC_INST_LENGTH,
287 BC_INST_READ,
288 BC_INST_OBASE,
289 BC_INST_SQRT,
290
291 BC_INST_PRINT,
292 BC_INST_PRINT_POP,
293 BC_INST_STR,
294 BC_INST_PRINT_STR,
295
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100296#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600297 BC_INST_JUMP,
298 BC_INST_JUMP_ZERO,
299
300 BC_INST_CALL,
301
302 BC_INST_RET,
303 BC_INST_RET0,
304
305 BC_INST_HALT,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100306#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600307
308 BC_INST_POP,
309 BC_INST_POP_EXEC,
310
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100311#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600312 BC_INST_MODEXP,
313 BC_INST_DIVMOD,
314
315 BC_INST_EXECUTE,
316 BC_INST_EXEC_COND,
317
318 BC_INST_ASCIIFY,
319 BC_INST_PRINT_STREAM,
320
321 BC_INST_PRINT_STACK,
322 BC_INST_CLEAR_STACK,
323 BC_INST_STACK_LEN,
324 BC_INST_DUPLICATE,
325 BC_INST_SWAP,
326
327 BC_INST_LOAD,
328 BC_INST_PUSH_VAR,
329 BC_INST_PUSH_TO_VAR,
330
331 BC_INST_QUIT,
332 BC_INST_NQUIT,
333
334 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100335#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600336
337} BcInst;
338
339typedef struct BcId {
340 char *name;
341 size_t idx;
342} BcId;
343
344typedef struct BcFunc {
345 BcVec code;
346 BcVec labels;
347 size_t nparams;
348 BcVec autos;
349} BcFunc;
350
351typedef enum BcResultType {
352
353 BC_RESULT_TEMP,
354
355 BC_RESULT_VAR,
356 BC_RESULT_ARRAY_ELEM,
357 BC_RESULT_ARRAY,
358
359 BC_RESULT_STR,
360
361 BC_RESULT_IBASE,
362 BC_RESULT_SCALE,
363 BC_RESULT_LAST,
364
365 // These are between to calculate ibase, obase, and last from instructions.
366 BC_RESULT_CONSTANT,
367 BC_RESULT_ONE,
368
369 BC_RESULT_OBASE,
370
371} BcResultType;
372
373typedef union BcResultData {
374 BcNum n;
375 BcVec v;
376 BcId id;
377} BcResultData;
378
379typedef struct BcResult {
380 BcResultType t;
381 BcResultData d;
382} BcResult;
383
384typedef struct BcInstPtr {
385 size_t func;
386 size_t idx;
387 size_t len;
388} BcInstPtr;
389
390static void bc_array_expand(BcVec *a, size_t len);
391static int bc_id_cmp(const void *e1, const void *e2);
392
393// BC_LEX_NEG is not used in lexing; it is only for parsing.
394typedef enum BcLexType {
395
396 BC_LEX_EOF,
397 BC_LEX_INVALID,
398
399 BC_LEX_OP_INC,
400 BC_LEX_OP_DEC,
401
402 BC_LEX_NEG,
403
404 BC_LEX_OP_POWER,
405 BC_LEX_OP_MULTIPLY,
406 BC_LEX_OP_DIVIDE,
407 BC_LEX_OP_MODULUS,
408 BC_LEX_OP_PLUS,
409 BC_LEX_OP_MINUS,
410
411 BC_LEX_OP_REL_EQ,
412 BC_LEX_OP_REL_LE,
413 BC_LEX_OP_REL_GE,
414 BC_LEX_OP_REL_NE,
415 BC_LEX_OP_REL_LT,
416 BC_LEX_OP_REL_GT,
417
418 BC_LEX_OP_BOOL_NOT,
419 BC_LEX_OP_BOOL_OR,
420 BC_LEX_OP_BOOL_AND,
421
422 BC_LEX_OP_ASSIGN_POWER,
423 BC_LEX_OP_ASSIGN_MULTIPLY,
424 BC_LEX_OP_ASSIGN_DIVIDE,
425 BC_LEX_OP_ASSIGN_MODULUS,
426 BC_LEX_OP_ASSIGN_PLUS,
427 BC_LEX_OP_ASSIGN_MINUS,
428 BC_LEX_OP_ASSIGN,
429
430 BC_LEX_NLINE,
431 BC_LEX_WHITESPACE,
432
433 BC_LEX_LPAREN,
434 BC_LEX_RPAREN,
435
436 BC_LEX_LBRACKET,
437 BC_LEX_COMMA,
438 BC_LEX_RBRACKET,
439
440 BC_LEX_LBRACE,
441 BC_LEX_SCOLON,
442 BC_LEX_RBRACE,
443
444 BC_LEX_STR,
445 BC_LEX_NAME,
446 BC_LEX_NUMBER,
447
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100448 BC_LEX_KEY_1st_keyword,
449 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600450 BC_LEX_KEY_BREAK,
451 BC_LEX_KEY_CONTINUE,
452 BC_LEX_KEY_DEFINE,
453 BC_LEX_KEY_ELSE,
454 BC_LEX_KEY_FOR,
455 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100456 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
457 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600458 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100459 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600460 BC_LEX_KEY_LENGTH,
461 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100462 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600463 BC_LEX_KEY_PRINT,
464 BC_LEX_KEY_QUIT,
465 BC_LEX_KEY_READ,
466 BC_LEX_KEY_RETURN,
467 BC_LEX_KEY_SCALE,
468 BC_LEX_KEY_SQRT,
469 BC_LEX_KEY_WHILE,
470
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100471#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600472 BC_LEX_EQ_NO_REG,
473 BC_LEX_OP_MODEXP,
474 BC_LEX_OP_DIVMOD,
475
476 BC_LEX_COLON,
477 BC_LEX_ELSE,
478 BC_LEX_EXECUTE,
479 BC_LEX_PRINT_STACK,
480 BC_LEX_CLEAR_STACK,
481 BC_LEX_STACK_LEVEL,
482 BC_LEX_DUPLICATE,
483 BC_LEX_SWAP,
484 BC_LEX_POP,
485
486 BC_LEX_ASCIIFY,
487 BC_LEX_PRINT_STREAM,
488
489 BC_LEX_STORE_IBASE,
490 BC_LEX_STORE_SCALE,
491 BC_LEX_LOAD,
492 BC_LEX_LOAD_POP,
493 BC_LEX_STORE_PUSH,
494 BC_LEX_STORE_OBASE,
495 BC_LEX_PRINT_POP,
496 BC_LEX_NQUIT,
497 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100498#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600499} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100500// must match order of BC_LEX_KEY_foo etc above
501#if ENABLE_BC
502struct BcLexKeyword {
503 char name8[8];
504};
505#define BC_LEX_KW_ENTRY(a, b, c) \
506 { .name8 = a /*, .len = b, .posix = c*/ }
507static const struct BcLexKeyword bc_lex_kws[20] = {
508 BC_LEX_KW_ENTRY("auto" , 4, 1), // 0
509 BC_LEX_KW_ENTRY("break" , 5, 1), // 1
510 BC_LEX_KW_ENTRY("continue", 8, 0), // 2 note: this one has no terminating NUL
511 BC_LEX_KW_ENTRY("define" , 6, 1), // 3
512
513 BC_LEX_KW_ENTRY("else" , 4, 0), // 4
514 BC_LEX_KW_ENTRY("for" , 3, 1), // 5
515 BC_LEX_KW_ENTRY("halt" , 4, 0), // 6
516 BC_LEX_KW_ENTRY("ibase" , 5, 1), // 7
517
518 BC_LEX_KW_ENTRY("if" , 2, 1), // 8
519 BC_LEX_KW_ENTRY("last" , 4, 0), // 9
520 BC_LEX_KW_ENTRY("length" , 6, 1), // 10
521 BC_LEX_KW_ENTRY("limits" , 6, 0), // 11
522
523 BC_LEX_KW_ENTRY("obase" , 5, 1), // 12
524 BC_LEX_KW_ENTRY("print" , 5, 0), // 13
525 BC_LEX_KW_ENTRY("quit" , 4, 1), // 14
526 BC_LEX_KW_ENTRY("read" , 4, 0), // 15
527
528 BC_LEX_KW_ENTRY("return" , 6, 1), // 16
529 BC_LEX_KW_ENTRY("scale" , 5, 1), // 17
530 BC_LEX_KW_ENTRY("sqrt" , 4, 1), // 18
531 BC_LEX_KW_ENTRY("while" , 5, 1), // 19
532};
533enum {
534 POSIX_KWORD_MASK = 0
535 | (1 << 0)
536 | (1 << 1)
537 | (0 << 2)
538 | (1 << 3)
539 \
540 | (0 << 4)
541 | (1 << 5)
542 | (0 << 6)
543 | (1 << 7)
544 \
545 | (1 << 8)
546 | (0 << 9)
547 | (1 << 10)
548 | (0 << 11)
549 \
550 | (1 << 12)
551 | (0 << 13)
552 | (1 << 14)
553 | (0 << 15)
554 \
555 | (1 << 16)
556 | (1 << 17)
557 | (1 << 18)
558 | (1 << 19)
559};
560#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600561
562struct BcLex;
563typedef BcStatus (*BcLexNext)(struct BcLex *);
564
565typedef struct BcLex {
566
567 const char *buf;
568 size_t i;
569 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600570 size_t len;
571 bool newline;
572
573 struct {
574 BcLexType t;
575 BcLexType last;
576 BcVec v;
577 } t;
578
579 BcLexNext next;
580
581} BcLex;
582
583#define BC_PARSE_STREND ((char) UCHAR_MAX)
584
585#define bc_parse_push(p, i) (bc_vec_pushByte(&(p)->func->code, (char) (i)))
586#define bc_parse_updateFunc(p, f) \
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +0100587 ((p)->func = bc_vec_item(&G.prog.fns, ((p)->fidx = (f))))
Gavin Howard01055ba2018-11-03 11:00:21 -0600588
589#define BC_PARSE_REL (1 << 0)
590#define BC_PARSE_PRINT (1 << 1)
591#define BC_PARSE_NOCALL (1 << 2)
592#define BC_PARSE_NOREAD (1 << 3)
593#define BC_PARSE_ARRAY (1 << 4)
594
595#define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags))
596#define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse)))
597
598#define BC_PARSE_FLAG_FUNC_INNER (1 << 0)
599#define BC_PARSE_FUNC_INNER(parse) \
600 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER)
601
602#define BC_PARSE_FLAG_FUNC (1 << 1)
603#define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC)
604
605#define BC_PARSE_FLAG_BODY (1 << 2)
606#define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY)
607
608#define BC_PARSE_FLAG_LOOP (1 << 3)
609#define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP)
610
611#define BC_PARSE_FLAG_LOOP_INNER (1 << 4)
612#define BC_PARSE_LOOP_INNER(parse) \
613 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER)
614
615#define BC_PARSE_FLAG_IF (1 << 5)
616#define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF)
617
618#define BC_PARSE_FLAG_ELSE (1 << 6)
619#define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE)
620
621#define BC_PARSE_FLAG_IF_END (1 << 7)
622#define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END)
623
624#define BC_PARSE_CAN_EXEC(parse) \
625 (!(BC_PARSE_TOP_FLAG(parse) & \
626 (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \
627 BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \
628 BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END)))
629
Gavin Howard01055ba2018-11-03 11:00:21 -0600630typedef struct BcParseNext {
631 uint32_t len;
632 BcLexType tokens[4];
633} BcParseNext;
634
635#define BC_PARSE_NEXT_TOKENS(...) .tokens = { __VA_ARGS__ }
636#define BC_PARSE_NEXT(a, ...) \
637 { \
638 .len = (a), BC_PARSE_NEXT_TOKENS(__VA_ARGS__) \
639 }
640
641struct BcParse;
642
643struct BcProgram;
644
Gavin Howard01055ba2018-11-03 11:00:21 -0600645typedef BcStatus (*BcParseParse)(struct BcParse *);
Gavin Howard01055ba2018-11-03 11:00:21 -0600646
647typedef struct BcParse {
648
649 BcParseParse parse;
650
651 BcLex l;
652
653 BcVec flags;
654
655 BcVec exits;
656 BcVec conds;
657
658 BcVec ops;
659
Gavin Howard01055ba2018-11-03 11:00:21 -0600660 BcFunc *func;
661 size_t fidx;
662
663 size_t nbraces;
664 bool auto_part;
665
666} BcParse;
667
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100668#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600669
Gavin Howard01055ba2018-11-03 11:00:21 -0600670static BcStatus bc_lex_token(BcLex *l);
671
672#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
673#define BC_PARSE_LEAF(p, rparen) \
674 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
675 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
676
677// We can calculate the conversion between tokens and exprs by subtracting the
678// position of the first operator in the lex enum and adding the position of the
679// first in the expr enum. Note: This only works for binary operators.
680#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
681
Gavin Howard01055ba2018-11-03 11:00:21 -0600682static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
683
Denys Vlasenko00d77792018-11-30 23:13:42 +0100684#endif // ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600685
Denys Vlasenko00d77792018-11-30 23:13:42 +0100686#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600687
688#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
689
Gavin Howard01055ba2018-11-03 11:00:21 -0600690static BcStatus dc_lex_token(BcLex *l);
691
Gavin Howard01055ba2018-11-03 11:00:21 -0600692static BcStatus dc_parse_expr(BcParse *p, uint8_t flags);
693
694#endif // ENABLE_DC
695
696typedef struct BcProgram {
697
698 size_t len;
699 size_t scale;
700
701 BcNum ib;
702 size_t ib_t;
703 BcNum ob;
704 size_t ob_t;
705
706 BcNum hexb;
707
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100708#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600709 BcNum strmb;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100710#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600711
712 BcVec results;
713 BcVec stack;
714
715 BcVec fns;
716 BcVec fn_map;
717
718 BcVec vars;
719 BcVec var_map;
720
721 BcVec arrs;
722 BcVec arr_map;
723
724 BcVec strs;
725 BcVec consts;
726
727 const char *file;
728
729 BcNum last;
730 BcNum zero;
731 BcNum one;
732
733 size_t nchars;
734
Gavin Howard01055ba2018-11-03 11:00:21 -0600735} BcProgram;
736
737#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
738
739#define BC_PROG_MAIN (0)
740#define BC_PROG_READ (1)
741
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100742#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600743#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100744#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600745
746#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
747#define BC_PROG_NUM(r, n) \
748 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
749
750typedef unsigned long (*BcProgramBuiltIn)(BcNum *);
751
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +0100752static void bc_program_addFunc(char *name, size_t *idx);
Denys Vlasenkod38af482018-12-04 19:11:02 +0100753static void bc_program_reset(void);
Gavin Howard01055ba2018-11-03 11:00:21 -0600754
755#define BC_FLAG_X (1 << 0)
756#define BC_FLAG_W (1 << 1)
757#define BC_FLAG_V (1 << 2)
758#define BC_FLAG_S (1 << 3)
759#define BC_FLAG_Q (1 << 4)
760#define BC_FLAG_L (1 << 5)
761#define BC_FLAG_I (1 << 6)
762
763#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
764#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
765
Denys Vlasenkod9d66552018-12-02 21:02:54 +0100766#define BC_MAX_OBASE ((unsigned) 999)
767#define BC_MAX_DIM ((unsigned) INT_MAX)
768#define BC_MAX_SCALE ((unsigned) UINT_MAX)
769#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
770#define BC_MAX_NAME BC_MAX_STRING
771#define BC_MAX_NUM BC_MAX_STRING
772#define BC_MAX_EXP ((unsigned long) LONG_MAX)
773#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
Gavin Howard01055ba2018-11-03 11:00:21 -0600774
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100775struct globals {
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100776 smallint ttyin;
777 smallint eof;
Gavin Howard01055ba2018-11-03 11:00:21 -0600778 char sbgn;
779 char send;
Gavin Howard01055ba2018-11-03 11:00:21 -0600780
781 BcParse prs;
782 BcProgram prog;
783
Denys Vlasenko5318f812018-12-05 17:48:01 +0100784 // For error messages. Can be set to current parsed line,
785 // or [TODO] to current executing line (can be before last parsed one)
786 unsigned err_line;
787
Gavin Howard01055ba2018-11-03 11:00:21 -0600788 BcVec files;
789
790 char *env_args;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100791} FIX_ALIASING;
792#define G (*ptr_to_globals)
793#define INIT_G() do { \
794 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
795} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100796#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
797#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
798#define G_exreg (ENABLE_DC && (option_mask32 & BC_FLAG_X))
Denys Vlasenkoab3c5682018-12-02 16:32:36 +0100799#define G_interrupt (ENABLE_FEATURE_BC_SIGNALS ? bb_got_signal : 0)
Gavin Howard01055ba2018-11-03 11:00:21 -0600800
Gavin Howard01055ba2018-11-03 11:00:21 -0600801
Denys Vlasenko00d77792018-11-30 23:13:42 +0100802#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
803
Denys Vlasenko00d77792018-11-30 23:13:42 +0100804static void bc_vm_info(void);
Gavin Howard01055ba2018-11-03 11:00:21 -0600805
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100806#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600807
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100808// This is a bit array that corresponds to token types. An entry is
Gavin Howard01055ba2018-11-03 11:00:21 -0600809// true if the token is valid in an expression, false otherwise.
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100810enum {
811 BC_PARSE_EXPRS_BITS = 0
812 + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
813 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
814 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
815 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
816 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
817 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
818 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
819 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8))
Gavin Howard01055ba2018-11-03 11:00:21 -0600820};
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100821static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
822{
823#if ULONG_MAX > 0xffffffff
824 // 64-bit version (will not work correctly for 32-bit longs!)
825 return BC_PARSE_EXPRS_BITS & (1UL << i);
826#else
827 // 32-bit version
828 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
829 if (i >= 32) {
830 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
831 i &= 31;
832 }
833 return m & (1UL << i);
834#endif
835}
Gavin Howard01055ba2018-11-03 11:00:21 -0600836
837// This is an array of data for operators that correspond to token types.
Denys Vlasenko65437582018-12-05 19:37:19 +0100838static const uint8_t bc_parse_ops[] = {
839#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100840 OP(0, false), OP( 0, false ), // inc dec
841 OP(1, false), // neg
Denys Vlasenko65437582018-12-05 19:37:19 +0100842 OP(2, false),
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100843 OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div
844 OP(4, true ), OP( 4, true ), // mod + -
845 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
846 OP(1, false), // not
847 OP(7, true ), OP( 7, true ), // or and
848 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
849 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenko65437582018-12-05 19:37:19 +0100850#undef OP
Gavin Howard01055ba2018-11-03 11:00:21 -0600851};
Denys Vlasenko65437582018-12-05 19:37:19 +0100852#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
853#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
Gavin Howard01055ba2018-11-03 11:00:21 -0600854
855// These identify what tokens can come after expressions in certain cases.
856static const BcParseNext bc_parse_next_expr =
857 BC_PARSE_NEXT(4, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF);
858static const BcParseNext bc_parse_next_param =
859 BC_PARSE_NEXT(2, BC_LEX_RPAREN, BC_LEX_COMMA);
860static const BcParseNext bc_parse_next_print =
861 BC_PARSE_NEXT(4, BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF);
862static const BcParseNext bc_parse_next_rel = BC_PARSE_NEXT(1, BC_LEX_RPAREN);
863static const BcParseNext bc_parse_next_elem = BC_PARSE_NEXT(1, BC_LEX_RBRACKET);
864static const BcParseNext bc_parse_next_for = BC_PARSE_NEXT(1, BC_LEX_SCOLON);
865static const BcParseNext bc_parse_next_read =
866 BC_PARSE_NEXT(2, BC_LEX_NLINE, BC_LEX_EOF);
867#endif // ENABLE_BC
868
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100869#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600870static const BcLexType dc_lex_regs[] = {
871 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
872 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
873 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
874 BC_LEX_STORE_PUSH,
875};
876
Gavin Howard01055ba2018-11-03 11:00:21 -0600877static const BcLexType dc_lex_tokens[] = {
878 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
879 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
880 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
881 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
882 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
883 BC_LEX_INVALID, BC_LEX_INVALID,
884 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
885 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
886 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
887 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
888 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
889 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
890 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
891 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
892 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
893 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
894 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
895 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
896 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
897 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
898 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
899 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
900 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
901 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
902 BC_LEX_INVALID
903};
904
905static const BcInst dc_parse_insts[] = {
906 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
907 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
908 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
909 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
910 BC_INST_INVALID, BC_INST_INVALID,
911 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
912 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
913 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
914 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
915 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
916 BC_INST_INVALID, BC_INST_INVALID,
917 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
918 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
919 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
920 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
921 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
922 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
923 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
924 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
925 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
926 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
927 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
928 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
929};
930#endif // ENABLE_DC
931
Gavin Howard01055ba2018-11-03 11:00:21 -0600932static const BcNumBinaryOp bc_program_ops[] = {
933 bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub,
934};
935
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100936static void fflush_and_check(void)
937{
938 fflush_all();
939 if (ferror(stdout) || ferror(stderr))
940 bb_perror_msg_and_die("output error");
941}
942
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100943static void quit(void) NORETURN;
944static void quit(void)
945{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +0100946 if (ferror(stdin))
947 bb_perror_msg_and_die("input error");
948 fflush_and_check();
949 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +0100950}
951
Denys Vlasenko5318f812018-12-05 17:48:01 +0100952static void bc_verror_msg(const char *fmt, va_list p)
953{
954 const char *sv = sv; /* for compiler */
955 if (G.prog.file) {
956 sv = applet_name;
957 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
958 }
959 bb_verror_msg(fmt, p, NULL);
960 if (G.prog.file) {
961 free((char*)applet_name);
962 applet_name = sv;
963 }
964}
965
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100966static NOINLINE int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100967{
968 va_list p;
969
970 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100971 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100972 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +0100973
Denys Vlasenkoc1c24702018-12-03 16:06:02 +0100974 if (!G.ttyin)
975 exit(1);
976 return BC_STATUS_FAILURE;
977}
978
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100979static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100980{
981 va_list p;
982
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100983 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100984 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100985 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100986
987 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +0100988 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100989 va_end(p);
990
991 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100992 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +0100993 return BC_STATUS_SUCCESS; // no, it's a warning
994 if (!G.ttyin)
995 exit(1);
996 return BC_STATUS_FAILURE;
997}
998
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +0100999// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1000// This idiom begs for tail-call optimization, but for it to work,
1001// function must not have calller-cleaned parameters on stack.
1002// Unfortunately, vararg functions do exactly that on most arches.
1003// Thus, these shims for the cases when we have no PARAMS:
1004static int bc_error(const char *msg)
1005{
1006 return bc_error_fmt("%s", msg);
1007}
1008static int bc_posix_error(const char *msg)
1009{
1010 return bc_posix_error_fmt("%s", msg);
1011}
Denys Vlasenko00646792018-12-05 18:12:27 +01001012static int bc_POSIX_does_not_allow(const char *msg)
1013{
1014 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1015}
1016static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1017{
1018 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1019}
1020static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1021{
1022 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1023}
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001024static int bc_error_bad_character(char c)
1025{
1026 return bc_error_fmt("bad character '%c'", c);
1027}
1028static int bc_error_bad_expression(void)
1029{
1030 return bc_error("bad expression");
1031}
1032static int bc_error_bad_token(void)
1033{
1034 return bc_error("bad token");
1035}
1036static int bc_error_stack_has_too_few_elements(void)
1037{
1038 return bc_error("stack has too few elements");
1039}
1040static int bc_error_variable_is_wrong_type(void)
1041{
1042 return bc_error("variable is wrong type");
1043}
1044static int bc_error_nested_read_call(void)
1045{
1046 return bc_error("read() call inside of a read() call");
1047}
1048
Gavin Howard01055ba2018-11-03 11:00:21 -06001049static void bc_vec_grow(BcVec *v, size_t n)
1050{
1051 size_t cap = v->cap * 2;
1052 while (cap < v->len + n) cap *= 2;
1053 v->v = xrealloc(v->v, v->size * cap);
1054 v->cap = cap;
1055}
1056
1057static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1058{
1059 v->size = esize;
1060 v->cap = BC_VEC_START_CAP;
1061 v->len = 0;
1062 v->dtor = dtor;
1063 v->v = xmalloc(esize * BC_VEC_START_CAP);
1064}
1065
Denys Vlasenko7d628012018-12-04 21:46:47 +01001066static void bc_char_vec_init(BcVec *v)
1067{
1068 bc_vec_init(v, sizeof(char), NULL);
1069}
1070
Gavin Howard01055ba2018-11-03 11:00:21 -06001071static void bc_vec_expand(BcVec *v, size_t req)
1072{
1073 if (v->cap < req) {
1074 v->v = xrealloc(v->v, v->size * req);
1075 v->cap = req;
1076 }
1077}
1078
1079static void bc_vec_npop(BcVec *v, size_t n)
1080{
1081 if (!v->dtor)
1082 v->len -= n;
1083 else {
1084 size_t len = v->len - n;
1085 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1086 }
1087}
1088
Denys Vlasenko7d628012018-12-04 21:46:47 +01001089static void bc_vec_pop_all(BcVec *v)
1090{
1091 bc_vec_npop(v, v->len);
1092}
1093
Gavin Howard01055ba2018-11-03 11:00:21 -06001094static void bc_vec_push(BcVec *v, const void *data)
1095{
1096 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1097 memmove(v->v + (v->size * v->len), data, v->size);
1098 v->len += 1;
1099}
1100
1101static void bc_vec_pushByte(BcVec *v, char data)
1102{
1103 bc_vec_push(v, &data);
1104}
1105
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001106static void bc_vec_pushZeroByte(BcVec *v)
1107{
1108 //bc_vec_pushByte(v, '\0');
1109 // better:
1110 bc_vec_push(v, &const_int_0);
1111}
1112
Gavin Howard01055ba2018-11-03 11:00:21 -06001113static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1114{
1115 if (idx == v->len)
1116 bc_vec_push(v, data);
1117 else {
1118
1119 char *ptr;
1120
1121 if (v->len == v->cap) bc_vec_grow(v, 1);
1122
1123 ptr = v->v + v->size * idx;
1124
1125 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1126 memmove(ptr, data, v->size);
1127 }
1128}
1129
1130static void bc_vec_string(BcVec *v, size_t len, const char *str)
1131{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001132 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001133 bc_vec_expand(v, len + 1);
1134 memcpy(v->v, str, len);
1135 v->len = len;
1136
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001137 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001138}
1139
1140static void bc_vec_concat(BcVec *v, const char *str)
1141{
1142 size_t len;
1143
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001144 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001145
1146 len = v->len + strlen(str);
1147
1148 if (v->cap < len) bc_vec_grow(v, len - v->len);
1149 strcat(v->v, str);
1150
1151 v->len = len;
1152}
1153
1154static void *bc_vec_item(const BcVec *v, size_t idx)
1155{
1156 return v->v + v->size * idx;
1157}
1158
1159static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1160{
1161 return v->v + v->size * (v->len - idx - 1);
1162}
1163
1164static void bc_vec_free(void *vec)
1165{
1166 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001167 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001168 free(v->v);
1169}
1170
1171static size_t bc_map_find(const BcVec *v, const void *ptr)
1172{
1173 size_t low = 0, high = v->len;
1174
1175 while (low < high) {
1176
1177 size_t mid = (low + high) / 2;
1178 BcId *id = bc_vec_item(v, mid);
1179 int result = bc_id_cmp(ptr, id);
1180
1181 if (result == 0)
1182 return mid;
1183 else if (result < 0)
1184 high = mid;
1185 else
1186 low = mid + 1;
1187 }
1188
1189 return low;
1190}
1191
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001192static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001193{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001194 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001195
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001196 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001197 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001198 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1199 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001200 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001201 bc_vec_pushAt(v, ptr, n);
1202 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001203}
1204
1205static size_t bc_map_index(const BcVec *v, const void *ptr)
1206{
1207 size_t i = bc_map_find(v, ptr);
1208 if (i >= v->len) return BC_VEC_INVALID_IDX;
1209 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1210}
1211
1212static BcStatus bc_read_line(BcVec *vec, const char *prompt)
1213{
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001214 bool bad_chars;
Gavin Howard01055ba2018-11-03 11:00:21 -06001215
Denys Vlasenko00d77792018-11-30 23:13:42 +01001216 do {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001217 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001218
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001219 bad_chars = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001220 bc_vec_pop_all(vec);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001221
1222 fflush_and_check();
Gavin Howard01055ba2018-11-03 11:00:21 -06001223#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001224 if (bb_got_signal) { // ^C was pressed
1225 intr:
1226 bb_got_signal = 0; // resets G_interrupt to zero
1227 fputs(IS_BC
1228 ? "\ninterrupt (type \"quit\" to exit)\n"
1229 : "\ninterrupt (type \"q\" to exit)\n"
1230 , stderr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001231 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001232#endif
1233 if (G.ttyin && !G_posix)
1234 fputs(prompt, stderr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001235
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001236#if ENABLE_FEATURE_BC_SIGNALS
1237 errno = 0;
1238#endif
1239 do {
1240 i = fgetc(stdin);
1241 if (i == EOF) {
1242#if ENABLE_FEATURE_BC_SIGNALS
1243 // Both conditions appear simultaneously, check both just in case
1244 if (errno == EINTR || bb_got_signal) {
1245 // ^C was pressed
1246 clearerr(stdin);
1247 goto intr;
1248 }
1249#endif
1250 if (ferror(stdin))
1251 quit(); // this emits error message
1252 G.eof = 1;
1253 // Note: EOF does not append '\n', therefore:
1254 // printf 'print 123\n' | bc - works
1255 // printf 'print 123' | bc - fails (syntax error)
1256 break;
1257 }
1258
1259 if ((i < ' ' && i != '\t' && i != '\r' && i != '\n') // also allow '\v' '\f'?
1260 || i > 0x7e
1261 ) {
1262 // Bad chars on this line, ignore entire line
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001263 bc_error_fmt("illegal character 0x%02x", i);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001264 bad_chars = 1;
1265 }
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001266 bc_vec_pushByte(vec, (char)i);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001267 } while (i != '\n');
1268 } while (bad_chars);
Gavin Howard01055ba2018-11-03 11:00:21 -06001269
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001270 bc_vec_pushZeroByte(vec);
Gavin Howard01055ba2018-11-03 11:00:21 -06001271
1272 return BC_STATUS_SUCCESS;
1273}
1274
Denys Vlasenkodf515392018-12-02 19:27:48 +01001275static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001276{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001277 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001278 size_t size = ((size_t) -1);
1279 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001280
Denys Vlasenkodf515392018-12-02 19:27:48 +01001281 buf = xmalloc_open_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001282
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001283 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001284 char c = buf[i];
1285 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1286 || c > 0x7e
1287 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001288 free(buf);
1289 buf = NULL;
1290 break;
1291 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001292 }
1293
Denys Vlasenkodf515392018-12-02 19:27:48 +01001294 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001295}
1296
Denys Vlasenko785e4b32018-12-02 17:18:52 +01001297static void bc_args(int argc, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06001298{
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001299 unsigned opts;
Gavin Howard01055ba2018-11-03 11:00:21 -06001300 int i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001301
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01001302 GETOPT_RESET();
Gavin Howard01055ba2018-11-03 11:00:21 -06001303#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001304 opts = getopt32long(argv, "xwvsqli",
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01001305 "extended-register\0" No_argument "x"
1306 "warn\0" No_argument "w"
1307 "version\0" No_argument "v"
1308 "standard\0" No_argument "s"
1309 "quiet\0" No_argument "q"
1310 "mathlib\0" No_argument "l"
1311 "interactive\0" No_argument "i"
1312 );
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001313#else
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001314 opts = getopt32(argv, "xwvsqli");
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001315#endif
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001316 if (getenv("POSIXLY_CORRECT"))
1317 option_mask32 |= BC_FLAG_S;
Gavin Howard01055ba2018-11-03 11:00:21 -06001318
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001319 if (opts & BC_FLAG_V) bc_vm_info();
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01001320 // should not be necessary, getopt32() handles this??
1321 //if (argv[optind] && !strcmp(argv[optind], "--")) ++optind;
Gavin Howard01055ba2018-11-03 11:00:21 -06001322
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001323 for (i = optind; i < argc; ++i)
1324 bc_vec_push(&G.files, argv + i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001325}
1326
1327static void bc_num_setToZero(BcNum *n, size_t scale)
1328{
1329 n->len = 0;
1330 n->neg = false;
1331 n->rdx = scale;
1332}
1333
1334static void bc_num_zero(BcNum *n)
1335{
1336 bc_num_setToZero(n, 0);
1337}
1338
1339static void bc_num_one(BcNum *n)
1340{
1341 bc_num_setToZero(n, 0);
1342 n->len = 1;
1343 n->num[0] = 1;
1344}
1345
1346static void bc_num_ten(BcNum *n)
1347{
1348 bc_num_setToZero(n, 0);
1349 n->len = 2;
1350 n->num[0] = 0;
1351 n->num[1] = 1;
1352}
1353
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001354static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001355 size_t len)
1356{
1357 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001358 for (i = 0; i < len; ++i) {
1359 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001360 a[i + j++] += 10;
1361 a[i + j] -= 1;
1362 }
1363 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001364}
1365
1366static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1367{
1368 size_t i;
1369 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001370 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001371 return BC_NUM_NEG(i + 1, c < 0);
1372}
1373
1374static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1375{
1376 size_t i, min, a_int, b_int, diff;
1377 BcDig *max_num, *min_num;
1378 bool a_max, neg = false;
1379 ssize_t cmp;
1380
1381 if (a == b) return 0;
1382 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1383 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
1384 if (a->neg) {
1385 if (b->neg)
1386 neg = true;
1387 else
1388 return -1;
1389 }
1390 else if (b->neg)
1391 return 1;
1392
1393 a_int = BC_NUM_INT(a);
1394 b_int = BC_NUM_INT(b);
1395 a_int -= b_int;
1396 a_max = (a->rdx > b->rdx);
1397
1398 if (a_int != 0) return (ssize_t) a_int;
1399
1400 if (a_max) {
1401 min = b->rdx;
1402 diff = a->rdx - b->rdx;
1403 max_num = a->num + diff;
1404 min_num = b->num;
1405 }
1406 else {
1407 min = a->rdx;
1408 diff = b->rdx - a->rdx;
1409 max_num = b->num + diff;
1410 min_num = a->num;
1411 }
1412
1413 cmp = bc_num_compare(max_num, min_num, b_int + min);
1414 if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg);
1415
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001416 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001417 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
1418 }
1419
1420 return 0;
1421}
1422
1423static void bc_num_truncate(BcNum *n, size_t places)
1424{
1425 if (places == 0) return;
1426
1427 n->rdx -= places;
1428
1429 if (n->len != 0) {
1430 n->len -= places;
1431 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1432 }
1433}
1434
1435static void bc_num_extend(BcNum *n, size_t places)
1436{
1437 size_t len = n->len + places;
1438
1439 if (places != 0) {
1440
1441 if (n->cap < len) bc_num_expand(n, len);
1442
1443 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1444 memset(n->num, 0, sizeof(BcDig) * places);
1445
1446 n->len += places;
1447 n->rdx += places;
1448 }
1449}
1450
1451static void bc_num_clean(BcNum *n)
1452{
1453 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1454 if (n->len == 0)
1455 n->neg = false;
1456 else if (n->len < n->rdx)
1457 n->len = n->rdx;
1458}
1459
1460static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1461{
1462 if (n->rdx < scale)
1463 bc_num_extend(n, scale - n->rdx);
1464 else
1465 bc_num_truncate(n, n->rdx - scale);
1466
1467 bc_num_clean(n);
1468 if (n->len != 0) n->neg = !neg1 != !neg2;
1469}
1470
1471static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1472 BcNum *restrict b)
1473{
1474 if (idx < n->len) {
1475
1476 b->len = n->len - idx;
1477 a->len = idx;
1478 a->rdx = b->rdx = 0;
1479
1480 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1481 memcpy(a->num, n->num, idx * sizeof(BcDig));
1482 }
1483 else {
1484 bc_num_zero(b);
1485 bc_num_copy(a, n);
1486 }
1487
1488 bc_num_clean(a);
1489 bc_num_clean(b);
1490}
1491
1492static BcStatus bc_num_shift(BcNum *n, size_t places)
1493{
1494 if (places == 0 || n->len == 0) return BC_STATUS_SUCCESS;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01001495 if (places + n->len > BC_MAX_NUM)
1496 return bc_error("number too long: must be [1, BC_NUM_MAX]");
Gavin Howard01055ba2018-11-03 11:00:21 -06001497
1498 if (n->rdx >= places)
1499 n->rdx -= places;
1500 else {
1501 bc_num_extend(n, places - n->rdx);
1502 n->rdx = 0;
1503 }
1504
1505 bc_num_clean(n);
1506
1507 return BC_STATUS_SUCCESS;
1508}
1509
1510static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale)
1511{
1512 BcNum one;
1513 BcDig num[2];
1514
1515 one.cap = 2;
1516 one.num = num;
1517 bc_num_one(&one);
1518
1519 return bc_num_div(&one, a, b, scale);
1520}
1521
1522static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
1523{
1524 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1525 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1526 int carry, in;
1527
1528 // Because this function doesn't need to use scale (per the bc spec),
1529 // I am hijacking it to say whether it's doing an add or a subtract.
1530
1531 if (a->len == 0) {
1532 bc_num_copy(c, b);
1533 if (sub && c->len) c->neg = !c->neg;
1534 return BC_STATUS_SUCCESS;
1535 }
1536 else if (b->len == 0) {
1537 bc_num_copy(c, a);
1538 return BC_STATUS_SUCCESS;
1539 }
1540
1541 c->neg = a->neg;
1542 c->rdx = BC_MAX(a->rdx, b->rdx);
1543 min_rdx = BC_MIN(a->rdx, b->rdx);
1544 c->len = 0;
1545
1546 if (a->rdx > b->rdx) {
1547 diff = a->rdx - b->rdx;
1548 ptr = a->num;
1549 ptr_a = a->num + diff;
1550 ptr_b = b->num;
1551 }
1552 else {
1553 diff = b->rdx - a->rdx;
1554 ptr = b->num;
1555 ptr_a = a->num;
1556 ptr_b = b->num + diff;
1557 }
1558
1559 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1560
1561 ptr_c += diff;
1562 a_int = BC_NUM_INT(a);
1563 b_int = BC_NUM_INT(b);
1564
1565 if (a_int > b_int) {
1566 min_int = b_int;
1567 max = a_int;
1568 ptr = ptr_a;
1569 }
1570 else {
1571 min_int = a_int;
1572 max = b_int;
1573 ptr = ptr_b;
1574 }
1575
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001576 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001577 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1578 carry = in / 10;
1579 ptr_c[i] = (BcDig)(in % 10);
1580 }
1581
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001582 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001583 in = ((int) ptr[i]) + carry;
1584 carry = in / 10;
1585 ptr_c[i] = (BcDig)(in % 10);
1586 }
1587
1588 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1589
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001590 return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001591}
1592
1593static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
1594{
Gavin Howard01055ba2018-11-03 11:00:21 -06001595 ssize_t cmp;
1596 BcNum *minuend, *subtrahend;
1597 size_t start;
1598 bool aneg, bneg, neg;
1599
1600 // Because this function doesn't need to use scale (per the bc spec),
1601 // I am hijacking it to say whether it's doing an add or a subtract.
1602
1603 if (a->len == 0) {
1604 bc_num_copy(c, b);
1605 if (sub && c->len) c->neg = !c->neg;
1606 return BC_STATUS_SUCCESS;
1607 }
1608 else if (b->len == 0) {
1609 bc_num_copy(c, a);
1610 return BC_STATUS_SUCCESS;
1611 }
1612
1613 aneg = a->neg;
1614 bneg = b->neg;
1615 a->neg = b->neg = false;
1616
1617 cmp = bc_num_cmp(a, b);
1618
1619 a->neg = aneg;
1620 b->neg = bneg;
1621
1622 if (cmp == 0) {
1623 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
1624 return BC_STATUS_SUCCESS;
1625 }
1626 else if (cmp > 0) {
1627 neg = a->neg;
1628 minuend = a;
1629 subtrahend = b;
1630 }
1631 else {
1632 neg = b->neg;
1633 if (sub) neg = !neg;
1634 minuend = b;
1635 subtrahend = a;
1636 }
1637
1638 bc_num_copy(c, minuend);
1639 c->neg = neg;
1640
1641 if (c->rdx < subtrahend->rdx) {
1642 bc_num_extend(c, subtrahend->rdx - c->rdx);
1643 start = 0;
1644 }
1645 else
1646 start = c->rdx - subtrahend->rdx;
1647
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001648 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001649
1650 bc_num_clean(c);
1651
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001652 return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001653}
1654
1655static BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b,
1656 BcNum *restrict c)
1657{
1658 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001659 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001660 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001661 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001662
Gavin Howard01055ba2018-11-03 11:00:21 -06001663 if (a->len == 0 || b->len == 0) {
1664 bc_num_zero(c);
1665 return BC_STATUS_SUCCESS;
1666 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001667 aone = BC_NUM_ONE(a);
1668 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001669 bc_num_copy(c, aone ? b : a);
1670 return BC_STATUS_SUCCESS;
1671 }
1672
1673 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1674 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1675 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001676 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001677 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001678
Gavin Howard01055ba2018-11-03 11:00:21 -06001679 bc_num_expand(c, a->len + b->len + 1);
1680
1681 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001682 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001683
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001684 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001685
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001686 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001687 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001688 unsigned in = c->num[i + j];
1689 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1690 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001691 carry = in / 10;
1692 c->num[i + j] = (BcDig)(in % 10);
1693 }
1694
1695 c->num[i + j] += (BcDig) carry;
1696 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001697
1698 // a=2^1000000
1699 // a*a <- without check below, this will not be interruptible
1700 if (G_interrupt) return BC_STATUS_FAILURE;
Gavin Howard01055ba2018-11-03 11:00:21 -06001701 }
1702
1703 c->len = len;
1704
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001705 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06001706 }
1707
1708 bc_num_init(&l1, max);
1709 bc_num_init(&h1, max);
1710 bc_num_init(&l2, max);
1711 bc_num_init(&h2, max);
1712 bc_num_init(&m1, max);
1713 bc_num_init(&m2, max);
1714 bc_num_init(&z0, max);
1715 bc_num_init(&z1, max);
1716 bc_num_init(&z2, max);
1717 bc_num_init(&temp, max + max);
1718
1719 bc_num_split(a, max2, &l1, &h1);
1720 bc_num_split(b, max2, &l2, &h2);
1721
1722 s = bc_num_add(&h1, &l1, &m1, 0);
1723 if (s) goto err;
1724 s = bc_num_add(&h2, &l2, &m2, 0);
1725 if (s) goto err;
1726
1727 s = bc_num_k(&h1, &h2, &z0);
1728 if (s) goto err;
1729 s = bc_num_k(&m1, &m2, &z1);
1730 if (s) goto err;
1731 s = bc_num_k(&l1, &l2, &z2);
1732 if (s) goto err;
1733
1734 s = bc_num_sub(&z1, &z0, &temp, 0);
1735 if (s) goto err;
1736 s = bc_num_sub(&temp, &z2, &z1, 0);
1737 if (s) goto err;
1738
1739 s = bc_num_shift(&z0, max2 * 2);
1740 if (s) goto err;
1741 s = bc_num_shift(&z1, max2);
1742 if (s) goto err;
1743 s = bc_num_add(&z0, &z1, &temp, 0);
1744 if (s) goto err;
1745 s = bc_num_add(&temp, &z2, c, 0);
1746
1747err:
1748 bc_num_free(&temp);
1749 bc_num_free(&z2);
1750 bc_num_free(&z1);
1751 bc_num_free(&z0);
1752 bc_num_free(&m2);
1753 bc_num_free(&m1);
1754 bc_num_free(&h2);
1755 bc_num_free(&l2);
1756 bc_num_free(&h1);
1757 bc_num_free(&l1);
1758 return s;
1759}
1760
1761static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1762{
1763 BcStatus s;
1764 BcNum cpa, cpb;
1765 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1766
1767 scale = BC_MAX(scale, a->rdx);
1768 scale = BC_MAX(scale, b->rdx);
1769 scale = BC_MIN(a->rdx + b->rdx, scale);
1770 maxrdx = BC_MAX(maxrdx, scale);
1771
1772 bc_num_init(&cpa, a->len);
1773 bc_num_init(&cpb, b->len);
1774
1775 bc_num_copy(&cpa, a);
1776 bc_num_copy(&cpb, b);
1777 cpa.neg = cpb.neg = false;
1778
1779 s = bc_num_shift(&cpa, maxrdx);
1780 if (s) goto err;
1781 s = bc_num_shift(&cpb, maxrdx);
1782 if (s) goto err;
1783 s = bc_num_k(&cpa, &cpb, c);
1784 if (s) goto err;
1785
1786 maxrdx += scale;
1787 bc_num_expand(c, c->len + maxrdx);
1788
1789 if (c->len < maxrdx) {
1790 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
1791 c->len += maxrdx;
1792 }
1793
1794 c->rdx = maxrdx;
1795 bc_num_retireMul(c, scale, a->neg, b->neg);
1796
1797err:
1798 bc_num_free(&cpb);
1799 bc_num_free(&cpa);
1800 return s;
1801}
1802
1803static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1804{
1805 BcStatus s = BC_STATUS_SUCCESS;
1806 BcDig *n, *p, q;
1807 size_t len, end, i;
1808 BcNum cp;
1809 bool zero = true;
1810
1811 if (b->len == 0)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01001812 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06001813 else if (a->len == 0) {
1814 bc_num_setToZero(c, scale);
1815 return BC_STATUS_SUCCESS;
1816 }
1817 else if (BC_NUM_ONE(b)) {
1818 bc_num_copy(c, a);
1819 bc_num_retireMul(c, scale, a->neg, b->neg);
1820 return BC_STATUS_SUCCESS;
1821 }
1822
1823 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
1824 bc_num_copy(&cp, a);
1825 len = b->len;
1826
1827 if (len > cp.len) {
1828 bc_num_expand(&cp, len + 2);
1829 bc_num_extend(&cp, len - cp.len);
1830 }
1831
1832 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
1833 cp.rdx -= b->rdx;
1834 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
1835
1836 if (b->rdx == b->len) {
1837 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
1838 len -= i - 1;
1839 }
1840
1841 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
1842
1843 // We want an extra zero in front to make things simpler.
1844 cp.num[cp.len++] = 0;
1845 end = cp.len - len;
1846
1847 bc_num_expand(c, cp.len);
1848
1849 bc_num_zero(c);
1850 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
1851 c->rdx = cp.rdx;
1852 c->len = cp.len;
1853 p = b->num;
1854
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001855 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001856 n = cp.num + i;
1857 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001858 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001859 c->num[i] = q;
Denys Vlasenkof381a882018-12-05 19:21:34 +01001860 // a=2^100000
1861 // scale=40000
1862 // 1/a <- without check below, this will not be interruptible
1863 if (G_interrupt) {
1864 s = BC_STATUS_FAILURE;
1865 break;
1866 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001867 }
1868
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001869 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001870 bc_num_free(&cp);
1871
Denys Vlasenkof381a882018-12-05 19:21:34 +01001872 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001873}
1874
1875static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
1876 BcNum *restrict d, size_t scale, size_t ts)
1877{
1878 BcStatus s;
1879 BcNum temp;
1880 bool neg;
1881
Denys Vlasenko60cf7472018-12-04 20:05:28 +01001882 if (b->len == 0)
1883 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06001884
1885 if (a->len == 0) {
1886 bc_num_setToZero(d, ts);
1887 return BC_STATUS_SUCCESS;
1888 }
1889
1890 bc_num_init(&temp, d->cap);
Denys Vlasenkof381a882018-12-05 19:21:34 +01001891 s = bc_num_d(a, b, c, scale);
1892 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06001893
1894 if (scale != 0) scale = ts;
1895
1896 s = bc_num_m(c, b, &temp, scale);
1897 if (s) goto err;
1898 s = bc_num_sub(a, &temp, d, scale);
1899 if (s) goto err;
1900
1901 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
1902
1903 neg = d->neg;
1904 bc_num_retireMul(d, ts, a->neg, b->neg);
1905 d->neg = neg;
1906
1907err:
1908 bc_num_free(&temp);
1909 return s;
1910}
1911
1912static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1913{
1914 BcStatus s;
1915 BcNum c1;
1916 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
1917
1918 bc_num_init(&c1, len);
1919 s = bc_num_r(a, b, &c1, c, scale, ts);
1920 bc_num_free(&c1);
1921
1922 return s;
1923}
1924
1925static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1926{
1927 BcStatus s = BC_STATUS_SUCCESS;
1928 BcNum copy;
1929 unsigned long pow;
1930 size_t i, powrdx, resrdx;
1931 bool neg, zero;
1932
Denys Vlasenko60cf7472018-12-04 20:05:28 +01001933 if (b->rdx) return bc_error("non integer number");
Gavin Howard01055ba2018-11-03 11:00:21 -06001934
1935 if (b->len == 0) {
1936 bc_num_one(c);
1937 return BC_STATUS_SUCCESS;
1938 }
1939 else if (a->len == 0) {
1940 bc_num_setToZero(c, scale);
1941 return BC_STATUS_SUCCESS;
1942 }
1943 else if (BC_NUM_ONE(b)) {
1944 if (!b->neg)
1945 bc_num_copy(c, a);
1946 else
1947 s = bc_num_inv(a, c, scale);
1948 return s;
1949 }
1950
1951 neg = b->neg;
1952 b->neg = false;
1953
1954 s = bc_num_ulong(b, &pow);
1955 if (s) return s;
1956
1957 bc_num_init(&copy, a->len);
1958 bc_num_copy(&copy, a);
1959
1960 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
1961
1962 b->neg = neg;
1963
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001964 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001965 powrdx <<= 1;
1966 s = bc_num_mul(&copy, &copy, &copy, powrdx);
1967 if (s) goto err;
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001968 // Not needed: bc_num_mul() has a check for ^C:
1969 //if (G_interrupt) {
1970 // s = BC_STATUS_FAILURE;
1971 // goto err;
1972 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06001973 }
1974
Gavin Howard01055ba2018-11-03 11:00:21 -06001975 bc_num_copy(c, &copy);
1976
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001977 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001978
1979 powrdx <<= 1;
1980 s = bc_num_mul(&copy, &copy, &copy, powrdx);
1981 if (s) goto err;
1982
1983 if (pow & 1) {
1984 resrdx += powrdx;
1985 s = bc_num_mul(c, &copy, c, resrdx);
1986 if (s) goto err;
1987 }
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001988 // Not needed: bc_num_mul() has a check for ^C:
1989 //if (G_interrupt) {
1990 // s = BC_STATUS_FAILURE;
1991 // goto err;
1992 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06001993 }
1994
1995 if (neg) {
1996 s = bc_num_inv(c, c, scale);
1997 if (s) goto err;
1998 }
1999
Gavin Howard01055ba2018-11-03 11:00:21 -06002000 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2001
2002 // We can't use bc_num_clean() here.
2003 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2004 if (zero) bc_num_setToZero(c, scale);
2005
2006err:
2007 bc_num_free(&copy);
2008 return s;
2009}
2010
2011static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
2012 BcNumBinaryOp op, size_t req)
2013{
2014 BcStatus s;
2015 BcNum num2, *ptr_a, *ptr_b;
2016 bool init = false;
2017
2018 if (c == a) {
2019 ptr_a = &num2;
2020 memcpy(ptr_a, c, sizeof(BcNum));
2021 init = true;
2022 }
2023 else
2024 ptr_a = a;
2025
2026 if (c == b) {
2027 ptr_b = &num2;
2028 if (c != a) {
2029 memcpy(ptr_b, c, sizeof(BcNum));
2030 init = true;
2031 }
2032 }
2033 else
2034 ptr_b = b;
2035
2036 if (init)
2037 bc_num_init(c, req);
2038 else
2039 bc_num_expand(c, req);
2040
2041 s = op(ptr_a, ptr_b, c, scale);
2042
2043 if (init) bc_num_free(&num2);
2044
2045 return s;
2046}
2047
2048static bool bc_num_strValid(const char *val, size_t base)
2049{
2050 BcDig b;
2051 bool small, radix = false;
2052 size_t i, len = strlen(val);
2053
2054 if (!len) return true;
2055
2056 small = base <= 10;
2057 b = (BcDig)(small ? base + '0' : base - 10 + 'A');
2058
2059 for (i = 0; i < len; ++i) {
2060
2061 BcDig c = val[i];
2062
2063 if (c == '.') {
2064
2065 if (radix) return false;
2066
2067 radix = true;
2068 continue;
2069 }
2070
2071 if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
2072 return false;
2073 }
2074
2075 return true;
2076}
2077
2078static void bc_num_parseDecimal(BcNum *n, const char *val)
2079{
2080 size_t len, i;
2081 const char *ptr;
2082 bool zero = true;
2083
2084 for (i = 0; val[i] == '0'; ++i);
2085
2086 val += i;
2087 len = strlen(val);
2088 bc_num_zero(n);
2089
2090 if (len != 0) {
2091 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
2092 bc_num_expand(n, len);
2093 }
2094
2095 ptr = strchr(val, '.');
2096
Denys Vlasenkod5f77032018-12-04 21:37:56 +01002097 n->rdx = 0;
2098 if (ptr != NULL)
2099 n->rdx = (size_t)((val + len) - (ptr + 1));
Gavin Howard01055ba2018-11-03 11:00:21 -06002100
2101 if (!zero) {
2102 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
2103 n->num[n->len] = val[i] - '0';
2104 }
2105}
2106
2107static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2108{
2109 BcStatus s;
2110 BcNum temp, mult, result;
2111 BcDig c = '\0';
2112 bool zero = true;
2113 unsigned long v;
2114 size_t i, digits, len = strlen(val);
2115
2116 bc_num_zero(n);
2117
2118 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
2119 if (zero) return;
2120
2121 bc_num_init(&temp, BC_NUM_DEF_SIZE);
2122 bc_num_init(&mult, BC_NUM_DEF_SIZE);
2123
2124 for (i = 0; i < len; ++i) {
2125
2126 c = val[i];
2127 if (c == '.') break;
2128
2129 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2130
2131 s = bc_num_mul(n, base, &mult, 0);
2132 if (s) goto int_err;
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01002133 bc_num_ulong2num(&temp, v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002134 s = bc_num_add(&mult, &temp, n, 0);
2135 if (s) goto int_err;
2136 }
2137
2138 if (i == len) {
2139 c = val[i];
2140 if (c == 0) goto int_err;
2141 }
2142
2143 bc_num_init(&result, base->len);
2144 bc_num_zero(&result);
2145 bc_num_one(&mult);
2146
2147 for (i += 1, digits = 0; i < len; ++i, ++digits) {
2148
2149 c = val[i];
2150 if (c == 0) break;
2151
2152 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2153
2154 s = bc_num_mul(&result, base, &result, 0);
2155 if (s) goto err;
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01002156 bc_num_ulong2num(&temp, v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002157 s = bc_num_add(&result, &temp, &result, 0);
2158 if (s) goto err;
2159 s = bc_num_mul(&mult, base, &mult, 0);
2160 if (s) goto err;
2161 }
2162
2163 s = bc_num_div(&result, &mult, &result, digits);
2164 if (s) goto err;
2165 s = bc_num_add(n, &result, n, digits);
2166 if (s) goto err;
2167
2168 if (n->len != 0) {
2169 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2170 }
2171 else
2172 bc_num_zero(n);
2173
2174err:
2175 bc_num_free(&result);
2176int_err:
2177 bc_num_free(&mult);
2178 bc_num_free(&temp);
2179}
2180
2181static void bc_num_printNewline(size_t *nchars, size_t line_len)
2182{
2183 if (*nchars == line_len - 1) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002184 bb_putchar('\\');
2185 bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06002186 *nchars = 0;
2187 }
2188}
2189
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002190#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002191static void bc_num_printChar(size_t num, size_t width, bool radix,
2192 size_t *nchars, size_t line_len)
2193{
2194 (void) radix, (void) line_len;
Denys Vlasenko00d77792018-11-30 23:13:42 +01002195 bb_putchar((char) num);
Gavin Howard01055ba2018-11-03 11:00:21 -06002196 *nchars = *nchars + width;
2197}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002198#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002199
2200static void bc_num_printDigits(size_t num, size_t width, bool radix,
2201 size_t *nchars, size_t line_len)
2202{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002203 size_t exp, pow;
Gavin Howard01055ba2018-11-03 11:00:21 -06002204
2205 bc_num_printNewline(nchars, line_len);
Denys Vlasenko00d77792018-11-30 23:13:42 +01002206 bb_putchar(radix ? '.' : ' ');
Gavin Howard01055ba2018-11-03 11:00:21 -06002207 ++(*nchars);
2208
2209 bc_num_printNewline(nchars, line_len);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002210 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
2211 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002212
2213 for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002214 size_t dig;
Gavin Howard01055ba2018-11-03 11:00:21 -06002215 bc_num_printNewline(nchars, line_len);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002216 dig = num / pow;
2217 num -= dig * pow;
Denys Vlasenko00d77792018-11-30 23:13:42 +01002218 bb_putchar(((char) dig) + '0');
Gavin Howard01055ba2018-11-03 11:00:21 -06002219 }
2220}
2221
2222static void bc_num_printHex(size_t num, size_t width, bool radix,
2223 size_t *nchars, size_t line_len)
2224{
2225 if (radix) {
2226 bc_num_printNewline(nchars, line_len);
Denys Vlasenko00d77792018-11-30 23:13:42 +01002227 bb_putchar('.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002228 *nchars += 1;
2229 }
2230
2231 bc_num_printNewline(nchars, line_len);
Denys Vlasenko00d77792018-11-30 23:13:42 +01002232 bb_putchar(bb_hexdigits_upcase[num]);
Gavin Howard01055ba2018-11-03 11:00:21 -06002233 *nchars = *nchars + width;
2234}
2235
2236static void bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len)
2237{
2238 size_t i, rdx = n->rdx - 1;
2239
Denys Vlasenko00d77792018-11-30 23:13:42 +01002240 if (n->neg) bb_putchar('-');
Gavin Howard01055ba2018-11-03 11:00:21 -06002241 (*nchars) += n->neg;
2242
2243 for (i = n->len - 1; i < n->len; --i)
2244 bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars, len);
2245}
2246
2247static BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width,
2248 size_t *nchars, size_t len, BcNumDigitOp print)
2249{
2250 BcStatus s;
2251 BcVec stack;
2252 BcNum intp, fracp, digit, frac_len;
2253 unsigned long dig, *ptr;
2254 size_t i;
2255 bool radix;
2256
2257 if (n->len == 0) {
2258 print(0, width, false, nchars, len);
2259 return BC_STATUS_SUCCESS;
2260 }
2261
2262 bc_vec_init(&stack, sizeof(long), NULL);
2263 bc_num_init(&intp, n->len);
2264 bc_num_init(&fracp, n->rdx);
2265 bc_num_init(&digit, width);
2266 bc_num_init(&frac_len, BC_NUM_INT(n));
2267 bc_num_copy(&intp, n);
2268 bc_num_one(&frac_len);
2269
2270 bc_num_truncate(&intp, intp.rdx);
2271 s = bc_num_sub(n, &intp, &fracp, 0);
2272 if (s) goto err;
2273
2274 while (intp.len != 0) {
2275 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
2276 if (s) goto err;
2277 s = bc_num_ulong(&digit, &dig);
2278 if (s) goto err;
2279 bc_vec_push(&stack, &dig);
2280 }
2281
2282 for (i = 0; i < stack.len; ++i) {
2283 ptr = bc_vec_item_rev(&stack, i);
2284 print(*ptr, width, false, nchars, len);
2285 }
2286
2287 if (!n->rdx) goto err;
2288
2289 for (radix = true; frac_len.len <= n->rdx; radix = false) {
2290 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
2291 if (s) goto err;
2292 s = bc_num_ulong(&fracp, &dig);
2293 if (s) goto err;
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01002294 bc_num_ulong2num(&intp, dig);
Gavin Howard01055ba2018-11-03 11:00:21 -06002295 s = bc_num_sub(&fracp, &intp, &fracp, 0);
2296 if (s) goto err;
2297 print(dig, width, radix, nchars, len);
2298 s = bc_num_mul(&frac_len, base, &frac_len, 0);
2299 if (s) goto err;
2300 }
2301
2302err:
2303 bc_num_free(&frac_len);
2304 bc_num_free(&digit);
2305 bc_num_free(&fracp);
2306 bc_num_free(&intp);
2307 bc_vec_free(&stack);
2308 return s;
2309}
2310
2311static BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t,
2312 size_t *nchars, size_t line_len)
2313{
2314 BcStatus s;
2315 size_t width, i;
2316 BcNumDigitOp print;
2317 bool neg = n->neg;
2318
Denys Vlasenko00d77792018-11-30 23:13:42 +01002319 if (neg) bb_putchar('-');
Gavin Howard01055ba2018-11-03 11:00:21 -06002320 (*nchars) += neg;
2321
2322 n->neg = false;
2323
2324 if (base_t <= BC_NUM_MAX_IBASE) {
2325 width = 1;
2326 print = bc_num_printHex;
2327 }
2328 else {
2329 for (i = base_t - 1, width = 0; i != 0; i /= 10, ++width);
2330 print = bc_num_printDigits;
2331 }
2332
2333 s = bc_num_printNum(n, base, width, nchars, line_len, print);
2334 n->neg = neg;
2335
2336 return s;
2337}
2338
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002339#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002340static BcStatus bc_num_stream(BcNum *n, BcNum *base, size_t *nchars, size_t len)
2341{
2342 return bc_num_printNum(n, base, 1, nchars, len, bc_num_printChar);
2343}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002344#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002345
2346static void bc_num_init(BcNum *n, size_t req)
2347{
2348 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
2349 memset(n, 0, sizeof(BcNum));
2350 n->num = xmalloc(req);
2351 n->cap = req;
2352}
2353
2354static void bc_num_expand(BcNum *n, size_t req)
2355{
2356 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
2357 if (req > n->cap) {
2358 n->num = xrealloc(n->num, req);
2359 n->cap = req;
2360 }
2361}
2362
2363static void bc_num_free(void *num)
2364{
2365 free(((BcNum *) num)->num);
2366}
2367
2368static void bc_num_copy(BcNum *d, BcNum *s)
2369{
2370 if (d != s) {
2371 bc_num_expand(d, s->cap);
2372 d->len = s->len;
2373 d->neg = s->neg;
2374 d->rdx = s->rdx;
2375 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
2376 }
2377}
2378
2379static BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base,
2380 size_t base_t)
2381{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002382 if (!bc_num_strValid(val, base_t))
2383 return bc_error("bad number string");
Gavin Howard01055ba2018-11-03 11:00:21 -06002384
2385 if (base_t == 10)
2386 bc_num_parseDecimal(n, val);
2387 else
2388 bc_num_parseBase(n, val, base);
2389
2390 return BC_STATUS_SUCCESS;
2391}
2392
2393static BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
2394 size_t *nchars, size_t line_len)
2395{
2396 BcStatus s = BC_STATUS_SUCCESS;
2397
2398 bc_num_printNewline(nchars, line_len);
2399
2400 if (n->len == 0) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002401 bb_putchar('0');
Gavin Howard01055ba2018-11-03 11:00:21 -06002402 ++(*nchars);
2403 }
2404 else if (base_t == 10)
2405 bc_num_printDecimal(n, nchars, line_len);
2406 else
2407 s = bc_num_printBase(n, base, base_t, nchars, line_len);
2408
2409 if (newline) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01002410 bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06002411 *nchars = 0;
2412 }
2413
2414 return s;
2415}
2416
2417static BcStatus bc_num_ulong(BcNum *n, unsigned long *result)
2418{
2419 size_t i;
2420 unsigned long pow;
2421
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002422 if (n->neg) return bc_error("negative number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002423
2424 for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
2425
2426 unsigned long prev = *result, powprev = pow;
2427
2428 *result += ((unsigned long) n->num[i]) * pow;
2429 pow *= 10;
2430
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002431 if (*result < prev || pow < powprev)
2432 return bc_error("overflow");
Gavin Howard01055ba2018-11-03 11:00:21 -06002433 }
2434
2435 return BC_STATUS_SUCCESS;
2436}
2437
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01002438static void bc_num_ulong2num(BcNum *n, unsigned long val)
Gavin Howard01055ba2018-11-03 11:00:21 -06002439{
2440 size_t len;
2441 BcDig *ptr;
2442 unsigned long i;
2443
2444 bc_num_zero(n);
2445
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01002446 if (val == 0) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06002447
2448 for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len) bc_num_expand(n, len);
2449 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard01055ba2018-11-03 11:00:21 -06002450}
2451
2452static BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
2453{
2454 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
2455 (void) scale;
2456 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
2457}
2458
2459static BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
2460{
2461 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
2462 (void) scale;
2463 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
2464}
2465
2466static BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
2467{
2468 size_t req = BC_NUM_MREQ(a, b, scale);
2469 return bc_num_binary(a, b, c, scale, bc_num_m, req);
2470}
2471
2472static BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
2473{
2474 size_t req = BC_NUM_MREQ(a, b, scale);
2475 return bc_num_binary(a, b, c, scale, bc_num_d, req);
2476}
2477
2478static BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
2479{
2480 size_t req = BC_NUM_MREQ(a, b, scale);
2481 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
2482}
2483
2484static BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
2485{
2486 return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1);
2487}
2488
2489static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
2490{
2491 BcStatus s;
2492 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2493 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2494 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2495
2496 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2497 bc_num_expand(b, req);
2498
2499 if (a->len == 0) {
2500 bc_num_setToZero(b, scale);
2501 return BC_STATUS_SUCCESS;
2502 }
2503 else if (a->neg)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002504 return bc_error("negative number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002505 else if (BC_NUM_ONE(a)) {
2506 bc_num_one(b);
2507 bc_num_extend(b, scale);
2508 return BC_STATUS_SUCCESS;
2509 }
2510
2511 scale = BC_MAX(scale, a->rdx) + 1;
2512 len = a->len + scale;
2513
2514 bc_num_init(&num1, len);
2515 bc_num_init(&num2, len);
2516 bc_num_init(&half, BC_NUM_DEF_SIZE);
2517
2518 bc_num_one(&half);
2519 half.num[0] = 5;
2520 half.rdx = 1;
2521
2522 bc_num_init(&f, len);
2523 bc_num_init(&fprime, len);
2524
2525 x0 = &num1;
2526 x1 = &num2;
2527
2528 bc_num_one(x0);
2529 pow = BC_NUM_INT(a);
2530
2531 if (pow) {
2532
2533 if (pow & 1)
2534 x0->num[0] = 2;
2535 else
2536 x0->num[0] = 6;
2537
2538 pow -= 2 - (pow & 1);
2539
2540 bc_num_extend(x0, pow);
2541
2542 // Make sure to move the radix back.
2543 x0->rdx -= pow;
2544 }
2545
2546 x0->rdx = digs = digs1 = 0;
2547 resrdx = scale + 2;
2548 len = BC_NUM_INT(x0) + resrdx - 1;
2549
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002550 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002551
2552 s = bc_num_div(a, x0, &f, resrdx);
2553 if (s) goto err;
2554 s = bc_num_add(x0, &f, &fprime, resrdx);
2555 if (s) goto err;
2556 s = bc_num_mul(&fprime, &half, x1, resrdx);
2557 if (s) goto err;
2558
2559 cmp = bc_num_cmp(x1, x0);
2560 digs = x1->len - (unsigned long long) llabs(cmp);
2561
2562 if (cmp == cmp2 && digs == digs1)
2563 times += 1;
2564 else
2565 times = 0;
2566
2567 resrdx += times > 4;
2568
2569 cmp2 = cmp1;
2570 cmp1 = cmp;
2571 digs1 = digs;
2572
2573 temp = x0;
2574 x0 = x1;
2575 x1 = temp;
2576 }
2577
Gavin Howard01055ba2018-11-03 11:00:21 -06002578 bc_num_copy(b, x0);
2579 scale -= 1;
2580 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2581
2582err:
2583 bc_num_free(&fprime);
2584 bc_num_free(&f);
2585 bc_num_free(&half);
2586 bc_num_free(&num2);
2587 bc_num_free(&num1);
2588 return s;
2589}
2590
2591static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
2592 size_t scale)
2593{
2594 BcStatus s;
2595 BcNum num2, *ptr_a;
2596 bool init = false;
2597 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2598
2599 if (c == a) {
2600 memcpy(&num2, c, sizeof(BcNum));
2601 ptr_a = &num2;
2602 bc_num_init(c, len);
2603 init = true;
2604 }
2605 else {
2606 ptr_a = a;
2607 bc_num_expand(c, len);
2608 }
2609
2610 s = bc_num_r(ptr_a, b, c, d, scale, ts);
2611
2612 if (init) bc_num_free(&num2);
2613
2614 return s;
2615}
2616
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002617#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002618static BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
2619{
2620 BcStatus s;
2621 BcNum base, exp, two, temp;
2622
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002623 if (c->len == 0)
2624 return bc_error("divide by zero");
2625 if (a->rdx || b->rdx || c->rdx)
2626 return bc_error("non integer number");
2627 if (b->neg)
2628 return bc_error("negative number");
Gavin Howard01055ba2018-11-03 11:00:21 -06002629
2630 bc_num_expand(d, c->len);
2631 bc_num_init(&base, c->len);
2632 bc_num_init(&exp, b->len);
2633 bc_num_init(&two, BC_NUM_DEF_SIZE);
2634 bc_num_init(&temp, b->len);
2635
2636 bc_num_one(&two);
2637 two.num[0] = 2;
2638 bc_num_one(d);
2639
2640 s = bc_num_rem(a, c, &base, 0);
2641 if (s) goto err;
2642 bc_num_copy(&exp, b);
2643
2644 while (exp.len != 0) {
2645
2646 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
2647 if (s) goto err;
2648
2649 if (BC_NUM_ONE(&temp)) {
2650 s = bc_num_mul(d, &base, &temp, 0);
2651 if (s) goto err;
2652 s = bc_num_rem(&temp, c, d, 0);
2653 if (s) goto err;
2654 }
2655
2656 s = bc_num_mul(&base, &base, &temp, 0);
2657 if (s) goto err;
2658 s = bc_num_rem(&temp, c, &base, 0);
2659 if (s) goto err;
2660 }
2661
2662err:
2663 bc_num_free(&temp);
2664 bc_num_free(&two);
2665 bc_num_free(&exp);
2666 bc_num_free(&base);
2667 return s;
2668}
2669#endif // ENABLE_DC
2670
2671static int bc_id_cmp(const void *e1, const void *e2)
2672{
2673 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
2674}
2675
2676static void bc_id_free(void *id)
2677{
2678 free(((BcId *) id)->name);
2679}
2680
2681static BcStatus bc_func_insert(BcFunc *f, char *name, bool var)
2682{
2683 BcId a;
2684 size_t i;
2685
2686 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002687 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
2688 return bc_error("function parameter or auto var has the same name as another");
Gavin Howard01055ba2018-11-03 11:00:21 -06002689 }
2690
2691 a.idx = var;
2692 a.name = name;
2693
2694 bc_vec_push(&f->autos, &a);
2695
2696 return BC_STATUS_SUCCESS;
2697}
2698
2699static void bc_func_init(BcFunc *f)
2700{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002701 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002702 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2703 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2704 f->nparams = 0;
2705}
2706
2707static void bc_func_free(void *func)
2708{
2709 BcFunc *f = (BcFunc *) func;
2710 bc_vec_free(&f->code);
2711 bc_vec_free(&f->autos);
2712 bc_vec_free(&f->labels);
2713}
2714
2715static void bc_array_init(BcVec *a, bool nums)
2716{
2717 if (nums)
2718 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2719 else
2720 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2721 bc_array_expand(a, 1);
2722}
2723
2724static void bc_array_copy(BcVec *d, const BcVec *s)
2725{
2726 size_t i;
2727
Denys Vlasenko7d628012018-12-04 21:46:47 +01002728 bc_vec_pop_all(d);
Gavin Howard01055ba2018-11-03 11:00:21 -06002729 bc_vec_expand(d, s->cap);
2730 d->len = s->len;
2731
2732 for (i = 0; i < s->len; ++i) {
2733 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2734 bc_num_init(dnum, snum->len);
2735 bc_num_copy(dnum, snum);
2736 }
2737}
2738
2739static void bc_array_expand(BcVec *a, size_t len)
2740{
2741 BcResultData data;
2742
2743 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2744 while (len > a->len) {
2745 bc_num_init(&data.n, BC_NUM_DEF_SIZE);
2746 bc_vec_push(a, &data.n);
2747 }
2748 }
2749 else {
2750 while (len > a->len) {
2751 bc_array_init(&data.v, true);
2752 bc_vec_push(a, &data.v);
2753 }
2754 }
2755}
2756
2757static void bc_string_free(void *string)
2758{
2759 free(*((char **) string));
2760}
2761
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002762#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002763static void bc_result_copy(BcResult *d, BcResult *src)
2764{
2765 d->t = src->t;
2766
2767 switch (d->t) {
2768
2769 case BC_RESULT_TEMP:
2770 case BC_RESULT_IBASE:
2771 case BC_RESULT_SCALE:
2772 case BC_RESULT_OBASE:
2773 {
2774 bc_num_init(&d->d.n, src->d.n.len);
2775 bc_num_copy(&d->d.n, &src->d.n);
2776 break;
2777 }
2778
2779 case BC_RESULT_VAR:
2780 case BC_RESULT_ARRAY:
2781 case BC_RESULT_ARRAY_ELEM:
2782 {
2783 d->d.id.name = xstrdup(src->d.id.name);
2784 break;
2785 }
2786
2787 case BC_RESULT_CONSTANT:
2788 case BC_RESULT_LAST:
2789 case BC_RESULT_ONE:
2790 case BC_RESULT_STR:
2791 {
2792 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2793 break;
2794 }
2795 }
2796}
2797#endif // ENABLE_DC
2798
2799static void bc_result_free(void *result)
2800{
2801 BcResult *r = (BcResult *) result;
2802
2803 switch (r->t) {
2804
2805 case BC_RESULT_TEMP:
2806 case BC_RESULT_IBASE:
2807 case BC_RESULT_SCALE:
2808 case BC_RESULT_OBASE:
2809 {
2810 bc_num_free(&r->d.n);
2811 break;
2812 }
2813
2814 case BC_RESULT_VAR:
2815 case BC_RESULT_ARRAY:
2816 case BC_RESULT_ARRAY_ELEM:
2817 {
2818 free(r->d.id.name);
2819 break;
2820 }
2821
2822 default:
2823 {
2824 // Do nothing.
2825 break;
2826 }
2827 }
2828}
2829
2830static void bc_lex_lineComment(BcLex *l)
2831{
2832 l->t.t = BC_LEX_WHITESPACE;
2833 while (l->i < l->len && l->buf[l->i++] != '\n');
2834 --l->i;
2835}
2836
2837static void bc_lex_whitespace(BcLex *l)
2838{
2839 char c;
2840 l->t.t = BC_LEX_WHITESPACE;
2841 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
2842}
2843
2844static BcStatus bc_lex_number(BcLex *l, char start)
2845{
2846 const char *buf = l->buf + l->i;
2847 size_t len, hits = 0, bslashes = 0, i = 0, j;
2848 char c = buf[i];
2849 bool last_pt, pt = start == '.';
2850
2851 last_pt = pt;
2852 l->t.t = BC_LEX_NUMBER;
2853
2854 while (c != 0 && (isdigit(c) || (c >= 'A' && c <= 'F') ||
2855 (c == '.' && !pt) || (c == '\\' && buf[i + 1] == '\n')))
2856 {
2857 if (c != '\\') {
2858 last_pt = c == '.';
2859 pt = pt || last_pt;
2860 }
2861 else {
2862 ++i;
2863 bslashes += 1;
2864 }
2865
2866 c = buf[++i];
2867 }
2868
Denys Vlasenkod5f77032018-12-04 21:37:56 +01002869 len = i + !last_pt - bslashes * 2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002870 if (len > BC_MAX_NUM)
2871 return bc_error("number too long: must be [1, BC_NUM_MAX]");
Gavin Howard01055ba2018-11-03 11:00:21 -06002872
Denys Vlasenko7d628012018-12-04 21:46:47 +01002873 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002874 bc_vec_expand(&l->t.v, len + 1);
2875 bc_vec_push(&l->t.v, &start);
2876
2877 for (buf -= 1, j = 1; j < len + hits * 2; ++j) {
2878
2879 c = buf[j];
2880
2881 // If we have hit a backslash, skip it. We don't have
2882 // to check for a newline because it's guaranteed.
2883 if (hits < bslashes && c == '\\') {
2884 ++hits;
2885 ++j;
2886 continue;
2887 }
2888
2889 bc_vec_push(&l->t.v, &c);
2890 }
2891
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002892 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002893 l->i += i;
2894
2895 return BC_STATUS_SUCCESS;
2896}
2897
2898static BcStatus bc_lex_name(BcLex *l)
2899{
2900 size_t i = 0;
2901 const char *buf = l->buf + l->i - 1;
2902 char c = buf[i];
2903
2904 l->t.t = BC_LEX_NAME;
2905
2906 while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i];
2907
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002908 if (i > BC_MAX_STRING)
2909 return bc_error("name too long: must be [1, BC_NAME_MAX]");
Gavin Howard01055ba2018-11-03 11:00:21 -06002910 bc_vec_string(&l->t.v, i, buf);
2911
2912 // Increment the index. We minus 1 because it has already been incremented.
2913 l->i += i - 1;
2914
2915 return BC_STATUS_SUCCESS;
2916}
2917
2918static void bc_lex_init(BcLex *l, BcLexNext next)
2919{
2920 l->next = next;
Denys Vlasenko7d628012018-12-04 21:46:47 +01002921 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002922}
2923
2924static void bc_lex_free(BcLex *l)
2925{
2926 bc_vec_free(&l->t.v);
2927}
2928
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002929static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002930{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002931 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002932 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002933}
2934
2935static BcStatus bc_lex_next(BcLex *l)
2936{
2937 BcStatus s;
2938
2939 l->t.last = l->t.t;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002940 if (l->t.last == BC_LEX_EOF) return bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06002941
2942 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002943 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06002944 l->t.t = BC_LEX_EOF;
2945
2946 l->newline = (l->i == l->len);
2947 if (l->newline) return BC_STATUS_SUCCESS;
2948
2949 // Loop until failure or we don't have whitespace. This
2950 // is so the parser doesn't get inundated with whitespace.
2951 do {
2952 s = l->next(l);
2953 } while (!s && l->t.t == BC_LEX_WHITESPACE);
2954
2955 return s;
2956}
2957
2958static BcStatus bc_lex_text(BcLex *l, const char *text)
2959{
2960 l->buf = text;
2961 l->i = 0;
2962 l->len = strlen(text);
2963 l->t.t = l->t.last = BC_LEX_INVALID;
2964 return bc_lex_next(l);
2965}
2966
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002967#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06002968static BcStatus bc_lex_identifier(BcLex *l)
2969{
2970 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002971 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002972 const char *buf = l->buf + l->i - 1;
2973
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002974 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2975 const char *keyword8 = bc_lex_kws[i].name8;
2976 unsigned j = 0;
2977 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2978 j++;
2979 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002980 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002981 if (keyword8[j] != '\0')
2982 continue;
2983 match:
2984 // buf starts with keyword bc_lex_kws[i]
2985 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenko00646792018-12-05 18:12:27 +01002986 if (!((1 << i) & POSIX_KWORD_MASK)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002987 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002988 if (s) return s;
2989 }
2990
2991 // We minus 1 because the index has already been incremented.
2992 l->i += j - 1;
2993 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002994 }
2995
2996 s = bc_lex_name(l);
2997 if (s) return s;
2998
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002999 if (l->t.v.len > 2) {
3000 // Prevent this:
3001 // >>> qwe=1
3002 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3003 // '
3004 unsigned len = strchrnul(buf, '\n') - buf;
3005 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3006 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003007
3008 return s;
3009}
3010
3011static BcStatus bc_lex_string(BcLex *l)
3012{
3013 size_t len, nls = 0, i = l->i;
3014 char c;
3015
3016 l->t.t = BC_LEX_STR;
3017
3018 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n');
3019
3020 if (c == '\0') {
3021 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003022 return bc_error("string end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003023 }
3024
3025 len = i - l->i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003026 if (len > BC_MAX_STRING)
3027 return bc_error("string too long: must be [1, BC_STRING_MAX]");
Gavin Howard01055ba2018-11-03 11:00:21 -06003028 bc_vec_string(&l->t.v, len, l->buf + l->i);
3029
3030 l->i = i + 1;
3031 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003032 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003033
3034 return BC_STATUS_SUCCESS;
3035}
3036
3037static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3038{
3039 if (l->buf[l->i] == '=') {
3040 ++l->i;
3041 l->t.t = with;
3042 }
3043 else
3044 l->t.t = without;
3045}
3046
3047static BcStatus bc_lex_comment(BcLex *l)
3048{
3049 size_t i, nls = 0;
3050 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003051
3052 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003053 i = ++l->i;
3054 for (;;) {
3055 char c = buf[i];
3056 check_star:
3057 if (c == '*') {
3058 c = buf[++i];
3059 if (c == '/')
3060 break;
3061 goto check_star;
3062 }
3063 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003064 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003065 return bc_error("comment end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003066 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003067 nls += (c == '\n');
3068 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003069 }
3070
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003071 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003072 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003073 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003074
3075 return BC_STATUS_SUCCESS;
3076}
3077
3078static BcStatus bc_lex_token(BcLex *l)
3079{
3080 BcStatus s = BC_STATUS_SUCCESS;
3081 char c = l->buf[l->i++], c2;
3082
3083 // This is the workhorse of the lexer.
3084 switch (c) {
3085
3086 case '\0':
3087 case '\n':
3088 {
3089 l->newline = true;
3090 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3091 break;
3092 }
3093
3094 case '\t':
3095 case '\v':
3096 case '\f':
3097 case '\r':
3098 case ' ':
3099 {
3100 bc_lex_whitespace(l);
3101 break;
3102 }
3103
3104 case '!':
3105 {
3106 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
3107
3108 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003109 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Gavin Howard01055ba2018-11-03 11:00:21 -06003110 if (s) return s;
3111 }
3112
3113 break;
3114 }
3115
3116 case '"':
3117 {
3118 s = bc_lex_string(l);
3119 break;
3120 }
3121
3122 case '#':
3123 {
Denys Vlasenko00646792018-12-05 18:12:27 +01003124 s = bc_POSIX_does_not_allow("'#' script comments");
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 if (s) return s;
3126
3127 bc_lex_lineComment(l);
3128
3129 break;
3130 }
3131
3132 case '%':
3133 {
3134 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3135 break;
3136 }
3137
3138 case '&':
3139 {
3140 c2 = l->buf[l->i];
3141 if (c2 == '&') {
3142
Denys Vlasenko00646792018-12-05 18:12:27 +01003143 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Gavin Howard01055ba2018-11-03 11:00:21 -06003144 if (s) return s;
3145
3146 ++l->i;
3147 l->t.t = BC_LEX_OP_BOOL_AND;
3148 }
3149 else {
3150 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003151 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003152 }
3153
3154 break;
3155 }
3156
3157 case '(':
3158 case ')':
3159 {
3160 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3161 break;
3162 }
3163
3164 case '*':
3165 {
3166 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3167 break;
3168 }
3169
3170 case '+':
3171 {
3172 c2 = l->buf[l->i];
3173 if (c2 == '+') {
3174 ++l->i;
3175 l->t.t = BC_LEX_OP_INC;
3176 }
3177 else
3178 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3179 break;
3180 }
3181
3182 case ',':
3183 {
3184 l->t.t = BC_LEX_COMMA;
3185 break;
3186 }
3187
3188 case '-':
3189 {
3190 c2 = l->buf[l->i];
3191 if (c2 == '-') {
3192 ++l->i;
3193 l->t.t = BC_LEX_OP_DEC;
3194 }
3195 else
3196 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3197 break;
3198 }
3199
3200 case '.':
3201 {
3202 if (isdigit(l->buf[l->i]))
3203 s = bc_lex_number(l, c);
3204 else {
3205 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003206 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003207 }
3208 break;
3209 }
3210
3211 case '/':
3212 {
3213 c2 = l->buf[l->i];
3214 if (c2 == '*')
3215 s = bc_lex_comment(l);
3216 else
3217 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3218 break;
3219 }
3220
3221 case '0':
3222 case '1':
3223 case '2':
3224 case '3':
3225 case '4':
3226 case '5':
3227 case '6':
3228 case '7':
3229 case '8':
3230 case '9':
3231 case 'A':
3232 case 'B':
3233 case 'C':
3234 case 'D':
3235 case 'E':
3236 case 'F':
3237 {
3238 s = bc_lex_number(l, c);
3239 break;
3240 }
3241
3242 case ';':
3243 {
3244 l->t.t = BC_LEX_SCOLON;
3245 break;
3246 }
3247
3248 case '<':
3249 {
3250 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3251 break;
3252 }
3253
3254 case '=':
3255 {
3256 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3257 break;
3258 }
3259
3260 case '>':
3261 {
3262 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3263 break;
3264 }
3265
3266 case '[':
3267 case ']':
3268 {
3269 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3270 break;
3271 }
3272
3273 case '\\':
3274 {
3275 if (l->buf[l->i] == '\n') {
3276 l->t.t = BC_LEX_WHITESPACE;
3277 ++l->i;
3278 }
3279 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003280 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003281 break;
3282 }
3283
3284 case '^':
3285 {
3286 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3287 break;
3288 }
3289
3290 case 'a':
3291 case 'b':
3292 case 'c':
3293 case 'd':
3294 case 'e':
3295 case 'f':
3296 case 'g':
3297 case 'h':
3298 case 'i':
3299 case 'j':
3300 case 'k':
3301 case 'l':
3302 case 'm':
3303 case 'n':
3304 case 'o':
3305 case 'p':
3306 case 'q':
3307 case 'r':
3308 case 's':
3309 case 't':
3310 case 'u':
3311 case 'v':
3312 case 'w':
3313 case 'x':
3314 case 'y':
3315 case 'z':
3316 {
3317 s = bc_lex_identifier(l);
3318 break;
3319 }
3320
3321 case '{':
3322 case '}':
3323 {
3324 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3325 break;
3326 }
3327
3328 case '|':
3329 {
3330 c2 = l->buf[l->i];
3331
3332 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003333 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Gavin Howard01055ba2018-11-03 11:00:21 -06003334 if (s) return s;
3335
3336 ++l->i;
3337 l->t.t = BC_LEX_OP_BOOL_OR;
3338 }
3339 else {
3340 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003341 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003342 }
3343
3344 break;
3345 }
3346
3347 default:
3348 {
3349 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003350 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003351 break;
3352 }
3353 }
3354
3355 return s;
3356}
3357#endif // ENABLE_BC
3358
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003359#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06003360static BcStatus dc_lex_register(BcLex *l)
3361{
3362 BcStatus s = BC_STATUS_SUCCESS;
3363
3364 if (isspace(l->buf[l->i - 1])) {
3365 bc_lex_whitespace(l);
3366 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003367 if (!G_exreg)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003368 s = bc_error("extended register");
Gavin Howard01055ba2018-11-03 11:00:21 -06003369 else
3370 s = bc_lex_name(l);
3371 }
3372 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003373 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003374 bc_vec_pushByte(&l->t.v, l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003375 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003376 l->t.t = BC_LEX_NAME;
3377 }
3378
3379 return s;
3380}
3381
3382static BcStatus dc_lex_string(BcLex *l)
3383{
3384 size_t depth = 1, nls = 0, i = l->i;
3385 char c;
3386
3387 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003388 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003389
3390 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3391
3392 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3393 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3394 nls += (c == '\n');
3395
3396 if (depth) bc_vec_push(&l->t.v, &c);
3397 }
3398
3399 if (c == '\0') {
3400 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003401 return bc_error("string end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003402 }
3403
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003404 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003405 if (i - l->i > BC_MAX_STRING)
3406 return bc_error("string too long: must be [1, BC_STRING_MAX]");
Gavin Howard01055ba2018-11-03 11:00:21 -06003407
3408 l->i = i;
3409 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003410 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003411
3412 return BC_STATUS_SUCCESS;
3413}
3414
3415static BcStatus dc_lex_token(BcLex *l)
3416{
3417 BcStatus s = BC_STATUS_SUCCESS;
3418 char c = l->buf[l->i++], c2;
3419 size_t i;
3420
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003421 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3422 if (l->t.last == dc_lex_regs[i])
3423 return dc_lex_register(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003424 }
3425
3426 if (c >= '%' && c <= '~' &&
3427 (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID)
3428 {
3429 return s;
3430 }
3431
3432 // This is the workhorse of the lexer.
3433 switch (c) {
3434
3435 case '\0':
3436 {
3437 l->t.t = BC_LEX_EOF;
3438 break;
3439 }
3440
3441 case '\n':
3442 case '\t':
3443 case '\v':
3444 case '\f':
3445 case '\r':
3446 case ' ':
3447 {
3448 l->newline = (c == '\n');
3449 bc_lex_whitespace(l);
3450 break;
3451 }
3452
3453 case '!':
3454 {
3455 c2 = l->buf[l->i];
3456
3457 if (c2 == '=')
3458 l->t.t = BC_LEX_OP_REL_NE;
3459 else if (c2 == '<')
3460 l->t.t = BC_LEX_OP_REL_LE;
3461 else if (c2 == '>')
3462 l->t.t = BC_LEX_OP_REL_GE;
3463 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003464 return bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003465
3466 ++l->i;
3467 break;
3468 }
3469
3470 case '#':
3471 {
3472 bc_lex_lineComment(l);
3473 break;
3474 }
3475
3476 case '.':
3477 {
3478 if (isdigit(l->buf[l->i]))
3479 s = bc_lex_number(l, c);
3480 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003481 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003482 break;
3483 }
3484
3485 case '0':
3486 case '1':
3487 case '2':
3488 case '3':
3489 case '4':
3490 case '5':
3491 case '6':
3492 case '7':
3493 case '8':
3494 case '9':
3495 case 'A':
3496 case 'B':
3497 case 'C':
3498 case 'D':
3499 case 'E':
3500 case 'F':
3501 {
3502 s = bc_lex_number(l, c);
3503 break;
3504 }
3505
3506 case '[':
3507 {
3508 s = dc_lex_string(l);
3509 break;
3510 }
3511
3512 default:
3513 {
3514 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003515 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003516 break;
3517 }
3518 }
3519
3520 return s;
3521}
3522#endif // ENABLE_DC
3523
3524static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3525{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003526 bc_program_addFunc(name, idx);
3527 p->func = bc_vec_item(&G.prog.fns, p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003528}
3529
3530static void bc_parse_pushName(BcParse *p, char *name)
3531{
3532 size_t i = 0, len = strlen(name);
3533
3534 for (; i < len; ++i) bc_parse_push(p, name[i]);
3535 bc_parse_push(p, BC_PARSE_STREND);
3536
3537 free(name);
3538}
3539
3540static void bc_parse_pushIndex(BcParse *p, size_t idx)
3541{
3542 unsigned char amt, i, nums[sizeof(size_t)];
3543
3544 for (amt = 0; idx; ++amt) {
3545 nums[amt] = (char) idx;
3546 idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT;
3547 }
3548
3549 bc_parse_push(p, amt);
3550 for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]);
3551}
3552
3553static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3554{
3555 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003556 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003557
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003558 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003559
3560 bc_parse_push(p, BC_INST_NUM);
3561 bc_parse_pushIndex(p, idx);
3562
3563 ++(*nexs);
3564 (*prev) = BC_INST_NUM;
3565}
3566
3567static BcStatus bc_parse_text(BcParse *p, const char *text)
3568{
3569 BcStatus s;
3570
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003571 p->func = bc_vec_item(&G.prog.fns, p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003572
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003573 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003574 p->l.t.t = BC_LEX_INVALID;
3575 s = p->parse(p);
3576 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003577 if (!BC_PARSE_CAN_EXEC(p))
3578 return bc_error("file is not executable");
Gavin Howard01055ba2018-11-03 11:00:21 -06003579 }
3580
3581 return bc_lex_text(&p->l, text);
3582}
3583
Denys Vlasenkod38af482018-12-04 19:11:02 +01003584// Called when bc/dc_parse_parse() detects a failure,
3585// resets parsing structures.
3586static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003587{
3588 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003589 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003590 bc_vec_pop_all(&p->func->code);
3591 bc_vec_pop_all(&p->func->autos);
3592 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003593
3594 bc_parse_updateFunc(p, BC_PROG_MAIN);
3595 }
3596
3597 p->l.i = p->l.len;
3598 p->l.t.t = BC_LEX_EOF;
3599 p->auto_part = (p->nbraces = 0);
3600
3601 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003602 bc_vec_pop_all(&p->exits);
3603 bc_vec_pop_all(&p->conds);
3604 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003605
Denys Vlasenkod38af482018-12-04 19:11:02 +01003606 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003607}
3608
3609static void bc_parse_free(BcParse *p)
3610{
3611 bc_vec_free(&p->flags);
3612 bc_vec_free(&p->exits);
3613 bc_vec_free(&p->conds);
3614 bc_vec_free(&p->ops);
3615 bc_lex_free(&p->l);
3616}
3617
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003618static void bc_parse_create(BcParse *p, size_t func,
Gavin Howard01055ba2018-11-03 11:00:21 -06003619 BcParseParse parse, BcLexNext next)
3620{
3621 memset(p, 0, sizeof(BcParse));
3622
3623 bc_lex_init(&p->l, next);
3624 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3625 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3626 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003627 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003628 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3629
3630 p->parse = parse;
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003631 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003632 bc_parse_updateFunc(p, func);
3633}
3634
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003635#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06003636static BcStatus bc_parse_else(BcParse *p);
3637static BcStatus bc_parse_stmt(BcParse *p);
3638
3639static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start,
3640 size_t *nexprs, bool next)
3641{
3642 BcStatus s = BC_STATUS_SUCCESS;
3643 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003644 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3645 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003646
3647 while (p->ops.len > start) {
3648
3649 t = BC_PARSE_TOP_OP(p);
3650 if (t == BC_LEX_LPAREN) break;
3651
Denys Vlasenko65437582018-12-05 19:37:19 +01003652 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003653 if (l >= r && (l != r || !left)) break;
3654
3655 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3656 bc_vec_pop(&p->ops);
3657 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3658 }
3659
3660 bc_vec_push(&p->ops, &type);
3661 if (next) s = bc_lex_next(&p->l);
3662
3663 return s;
3664}
3665
3666static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
3667{
3668 BcLexType top;
3669
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003670 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003671 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003672 top = BC_PARSE_TOP_OP(p);
3673
3674 while (top != BC_LEX_LPAREN) {
3675
3676 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3677
3678 bc_vec_pop(&p->ops);
3679 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3680
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003681 if (p->ops.len <= ops_bgn)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003682 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003683 top = BC_PARSE_TOP_OP(p);
3684 }
3685
3686 bc_vec_pop(&p->ops);
3687
3688 return bc_lex_next(&p->l);
3689}
3690
3691static BcStatus bc_parse_params(BcParse *p, uint8_t flags)
3692{
3693 BcStatus s;
3694 bool comma = false;
3695 size_t nparams;
3696
3697 s = bc_lex_next(&p->l);
3698 if (s) return s;
3699
3700 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
3701
3702 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3703 s = bc_parse_expr(p, flags, bc_parse_next_param);
3704 if (s) return s;
3705
3706 comma = p->l.t.t == BC_LEX_COMMA;
3707 if (comma) {
3708 s = bc_lex_next(&p->l);
3709 if (s) return s;
3710 }
3711 }
3712
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003713 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003714 bc_parse_push(p, BC_INST_CALL);
3715 bc_parse_pushIndex(p, nparams);
3716
3717 return BC_STATUS_SUCCESS;
3718}
3719
3720static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags)
3721{
3722 BcStatus s;
3723 BcId entry, *entry_ptr;
3724 size_t idx;
3725
3726 entry.name = name;
3727
3728 s = bc_parse_params(p, flags);
3729 if (s) goto err;
3730
3731 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003732 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003733 goto err;
3734 }
3735
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003736 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003737
3738 if (idx == BC_VEC_INVALID_IDX) {
3739 name = xstrdup(entry.name);
3740 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003741 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003742 free(entry.name);
3743 }
3744 else
3745 free(name);
3746
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003747 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003748 bc_parse_pushIndex(p, entry_ptr->idx);
3749
3750 return bc_lex_next(&p->l);
3751
3752err:
3753 free(name);
3754 return s;
3755}
3756
3757static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
3758{
3759 BcStatus s;
3760 char *name;
3761
3762 name = xstrdup(p->l.t.v.v);
3763 s = bc_lex_next(&p->l);
3764 if (s) goto err;
3765
3766 if (p->l.t.t == BC_LEX_LBRACKET) {
3767
3768 s = bc_lex_next(&p->l);
3769 if (s) goto err;
3770
3771 if (p->l.t.t == BC_LEX_RBRACKET) {
3772
3773 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003774 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003775 goto err;
3776 }
3777
3778 *type = BC_INST_ARRAY;
3779 }
3780 else {
3781
3782 *type = BC_INST_ARRAY_ELEM;
3783
3784 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3785 s = bc_parse_expr(p, flags, bc_parse_next_elem);
3786 if (s) goto err;
3787 }
3788
3789 s = bc_lex_next(&p->l);
3790 if (s) goto err;
3791 bc_parse_push(p, *type);
3792 bc_parse_pushName(p, name);
3793 }
3794 else if (p->l.t.t == BC_LEX_LPAREN) {
3795
3796 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003797 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003798 goto err;
3799 }
3800
3801 *type = BC_INST_CALL;
3802 s = bc_parse_call(p, name, flags);
3803 }
3804 else {
3805 *type = BC_INST_VAR;
3806 bc_parse_push(p, BC_INST_VAR);
3807 bc_parse_pushName(p, name);
3808 }
3809
3810 return s;
3811
3812err:
3813 free(name);
3814 return s;
3815}
3816
3817static BcStatus bc_parse_read(BcParse *p)
3818{
3819 BcStatus s;
3820
3821 s = bc_lex_next(&p->l);
3822 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003823 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003824
3825 s = bc_lex_next(&p->l);
3826 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003827 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003828
3829 bc_parse_push(p, BC_INST_READ);
3830
3831 return bc_lex_next(&p->l);
3832}
3833
3834static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
3835 BcInst *prev)
3836{
3837 BcStatus s;
3838
3839 s = bc_lex_next(&p->l);
3840 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003841 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003842
3843 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3844
3845 s = bc_lex_next(&p->l);
3846 if (s) return s;
3847
3848 s = bc_parse_expr(p, flags, bc_parse_next_rel);
3849 if (s) return s;
3850
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003851 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003852
3853 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3854 bc_parse_push(p, *prev);
3855
3856 return bc_lex_next(&p->l);
3857}
3858
3859static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
3860{
3861 BcStatus s;
3862
3863 s = bc_lex_next(&p->l);
3864 if (s) return s;
3865
3866 if (p->l.t.t != BC_LEX_LPAREN) {
3867 *type = BC_INST_SCALE;
3868 bc_parse_push(p, BC_INST_SCALE);
3869 return BC_STATUS_SUCCESS;
3870 }
3871
3872 *type = BC_INST_SCALE_FUNC;
3873 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3874
3875 s = bc_lex_next(&p->l);
3876 if (s) return s;
3877
3878 s = bc_parse_expr(p, flags, bc_parse_next_rel);
3879 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003880 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003881 bc_parse_push(p, BC_INST_SCALE_FUNC);
3882
3883 return bc_lex_next(&p->l);
3884}
3885
3886static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
3887 size_t *nexprs, uint8_t flags)
3888{
3889 BcStatus s;
3890 BcLexType type;
3891 char inst;
3892 BcInst etype = *prev;
3893
3894 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
3895 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
3896 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
3897 {
3898 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3899 bc_parse_push(p, inst);
3900 s = bc_lex_next(&p->l);
3901 }
3902 else {
3903
3904 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3905 *paren_expr = true;
3906
3907 s = bc_lex_next(&p->l);
3908 if (s) return s;
3909 type = p->l.t.t;
3910
3911 // Because we parse the next part of the expression
3912 // right here, we need to increment this.
3913 *nexprs = *nexprs + 1;
3914
3915 switch (type) {
3916
3917 case BC_LEX_NAME:
3918 {
3919 s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
3920 break;
3921 }
3922
3923 case BC_LEX_KEY_IBASE:
3924 case BC_LEX_KEY_LAST:
3925 case BC_LEX_KEY_OBASE:
3926 {
3927 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
3928 s = bc_lex_next(&p->l);
3929 break;
3930 }
3931
3932 case BC_LEX_KEY_SCALE:
3933 {
3934 s = bc_lex_next(&p->l);
3935 if (s) return s;
3936 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003937 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003938 else
3939 bc_parse_push(p, BC_INST_SCALE);
3940 break;
3941 }
3942
3943 default:
3944 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003945 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003946 break;
3947 }
3948 }
3949
3950 if (!s) bc_parse_push(p, inst);
3951 }
3952
3953 return s;
3954}
3955
3956static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
3957 bool rparen, size_t *nexprs)
3958{
3959 BcStatus s;
3960 BcLexType type;
3961 BcInst etype = *prev;
3962
3963 s = bc_lex_next(&p->l);
3964 if (s) return s;
3965
3966 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
3967 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
3968 BC_LEX_OP_MINUS :
3969 BC_LEX_NEG;
3970 *prev = BC_PARSE_TOKEN_INST(type);
3971
3972 // We can just push onto the op stack because this is the largest
3973 // precedence operator that gets pushed. Inc/dec does not.
3974 if (type != BC_LEX_OP_MINUS)
3975 bc_vec_push(&p->ops, &type);
3976 else
3977 s = bc_parse_operator(p, type, ops_bgn, nexprs, false);
3978
3979 return s;
3980}
3981
3982static BcStatus bc_parse_string(BcParse *p, char inst)
3983{
3984 char *str = xstrdup(p->l.t.v.v);
3985
3986 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003987 bc_parse_pushIndex(p, G.prog.strs.len);
3988 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06003989 bc_parse_push(p, inst);
3990
3991 return bc_lex_next(&p->l);
3992}
3993
3994static BcStatus bc_parse_print(BcParse *p)
3995{
3996 BcStatus s;
3997 BcLexType type;
3998 bool comma = false;
3999
4000 s = bc_lex_next(&p->l);
4001 if (s) return s;
4002
4003 type = p->l.t.t;
4004
4005 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004006 return bc_error("bad print statement");
Gavin Howard01055ba2018-11-03 11:00:21 -06004007
4008 while (!s && type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
4009
4010 if (type == BC_LEX_STR)
4011 s = bc_parse_string(p, BC_INST_PRINT_POP);
4012 else {
4013 s = bc_parse_expr(p, 0, bc_parse_next_print);
4014 if (s) return s;
4015 bc_parse_push(p, BC_INST_PRINT_POP);
4016 }
4017
4018 if (s) return s;
4019
4020 comma = p->l.t.t == BC_LEX_COMMA;
4021 if (comma) s = bc_lex_next(&p->l);
4022 type = p->l.t.t;
4023 }
4024
4025 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004026 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004027
4028 return bc_lex_next(&p->l);
4029}
4030
4031static BcStatus bc_parse_return(BcParse *p)
4032{
4033 BcStatus s;
4034 BcLexType t;
4035 bool paren;
4036
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004037 if (!BC_PARSE_FUNC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004038
4039 s = bc_lex_next(&p->l);
4040 if (s) return s;
4041
4042 t = p->l.t.t;
4043 paren = t == BC_LEX_LPAREN;
4044
4045 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4046 bc_parse_push(p, BC_INST_RET0);
4047 else {
4048
4049 s = bc_parse_expr(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004050 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004051 bc_parse_push(p, BC_INST_RET0);
4052 s = bc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004053 }
Denys Vlasenko452df922018-12-05 20:28:26 +01004054 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06004055
4056 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01004057 s = bc_posix_error("POSIX requires parentheses around return expressions");
Gavin Howard01055ba2018-11-03 11:00:21 -06004058 if (s) return s;
4059 }
4060
4061 bc_parse_push(p, BC_INST_RET);
4062 }
4063
4064 return s;
4065}
4066
4067static BcStatus bc_parse_endBody(BcParse *p, bool brace)
4068{
4069 BcStatus s = BC_STATUS_SUCCESS;
4070
4071 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004072 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004073
4074 if (brace) {
4075
4076 if (p->l.t.t == BC_LEX_RBRACE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004077 if (!p->nbraces) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004078 --p->nbraces;
4079 s = bc_lex_next(&p->l);
4080 if (s) return s;
4081 }
4082 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004083 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004084 }
4085
4086 if (BC_PARSE_IF(p)) {
4087
4088 uint8_t *flag_ptr;
4089
4090 while (p->l.t.t == BC_LEX_NLINE) {
4091 s = bc_lex_next(&p->l);
4092 if (s) return s;
4093 }
4094
4095 bc_vec_pop(&p->flags);
4096
4097 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4098 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4099
4100 if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p);
4101 }
4102 else if (BC_PARSE_ELSE(p)) {
4103
4104 BcInstPtr *ip;
4105 size_t *label;
4106
4107 bc_vec_pop(&p->flags);
4108
4109 ip = bc_vec_top(&p->exits);
4110 label = bc_vec_item(&p->func->labels, ip->idx);
4111 *label = p->func->code.len;
4112
4113 bc_vec_pop(&p->exits);
4114 }
4115 else if (BC_PARSE_FUNC_INNER(p)) {
4116 bc_parse_push(p, BC_INST_RET0);
4117 bc_parse_updateFunc(p, BC_PROG_MAIN);
4118 bc_vec_pop(&p->flags);
4119 }
4120 else {
4121
4122 BcInstPtr *ip = bc_vec_top(&p->exits);
4123 size_t *label = bc_vec_top(&p->conds);
4124
4125 bc_parse_push(p, BC_INST_JUMP);
4126 bc_parse_pushIndex(p, *label);
4127
4128 label = bc_vec_item(&p->func->labels, ip->idx);
4129 *label = p->func->code.len;
4130
4131 bc_vec_pop(&p->flags);
4132 bc_vec_pop(&p->exits);
4133 bc_vec_pop(&p->conds);
4134 }
4135
4136 return s;
4137}
4138
4139static void bc_parse_startBody(BcParse *p, uint8_t flags)
4140{
4141 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4142 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4143 flags |= BC_PARSE_FLAG_BODY;
4144 bc_vec_push(&p->flags, &flags);
4145}
4146
4147static void bc_parse_noElse(BcParse *p)
4148{
4149 BcInstPtr *ip;
4150 size_t *label;
4151 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4152
4153 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4154
4155 ip = bc_vec_top(&p->exits);
4156 label = bc_vec_item(&p->func->labels, ip->idx);
4157 *label = p->func->code.len;
4158
4159 bc_vec_pop(&p->exits);
4160}
4161
4162static BcStatus bc_parse_if(BcParse *p)
4163{
4164 BcStatus s;
4165 BcInstPtr ip;
4166
4167 s = bc_lex_next(&p->l);
4168 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004169 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004170
4171 s = bc_lex_next(&p->l);
4172 if (s) return s;
4173 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4174 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004175 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004176
4177 s = bc_lex_next(&p->l);
4178 if (s) return s;
4179 bc_parse_push(p, BC_INST_JUMP_ZERO);
4180
4181 ip.idx = p->func->labels.len;
4182 ip.func = ip.len = 0;
4183
4184 bc_parse_pushIndex(p, ip.idx);
4185 bc_vec_push(&p->exits, &ip);
4186 bc_vec_push(&p->func->labels, &ip.idx);
4187 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4188
4189 return BC_STATUS_SUCCESS;
4190}
4191
4192static BcStatus bc_parse_else(BcParse *p)
4193{
4194 BcInstPtr ip;
4195
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004196 if (!BC_PARSE_IF_END(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004197
4198 ip.idx = p->func->labels.len;
4199 ip.func = ip.len = 0;
4200
4201 bc_parse_push(p, BC_INST_JUMP);
4202 bc_parse_pushIndex(p, ip.idx);
4203
4204 bc_parse_noElse(p);
4205
4206 bc_vec_push(&p->exits, &ip);
4207 bc_vec_push(&p->func->labels, &ip.idx);
4208 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4209
4210 return bc_lex_next(&p->l);
4211}
4212
4213static BcStatus bc_parse_while(BcParse *p)
4214{
4215 BcStatus s;
4216 BcInstPtr ip;
4217
4218 s = bc_lex_next(&p->l);
4219 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004220 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004221 s = bc_lex_next(&p->l);
4222 if (s) return s;
4223
4224 ip.idx = p->func->labels.len;
4225
4226 bc_vec_push(&p->func->labels, &p->func->code.len);
4227 bc_vec_push(&p->conds, &ip.idx);
4228
4229 ip.idx = p->func->labels.len;
4230 ip.func = 1;
4231 ip.len = 0;
4232
4233 bc_vec_push(&p->exits, &ip);
4234 bc_vec_push(&p->func->labels, &ip.idx);
4235
4236 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4237 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004238 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004239 s = bc_lex_next(&p->l);
4240 if (s) return s;
4241
4242 bc_parse_push(p, BC_INST_JUMP_ZERO);
4243 bc_parse_pushIndex(p, ip.idx);
4244 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4245
4246 return BC_STATUS_SUCCESS;
4247}
4248
4249static BcStatus bc_parse_for(BcParse *p)
4250{
4251 BcStatus s;
4252 BcInstPtr ip;
4253 size_t cond_idx, exit_idx, body_idx, update_idx;
4254
4255 s = bc_lex_next(&p->l);
4256 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004257 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004258 s = bc_lex_next(&p->l);
4259 if (s) return s;
4260
4261 if (p->l.t.t != BC_LEX_SCOLON)
4262 s = bc_parse_expr(p, 0, bc_parse_next_for);
4263 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004264 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004265
4266 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004267 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004268 s = bc_lex_next(&p->l);
4269 if (s) return s;
4270
4271 cond_idx = p->func->labels.len;
4272 update_idx = cond_idx + 1;
4273 body_idx = update_idx + 1;
4274 exit_idx = body_idx + 1;
4275
4276 bc_vec_push(&p->func->labels, &p->func->code.len);
4277
4278 if (p->l.t.t != BC_LEX_SCOLON)
4279 s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
4280 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004281 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004282
4283 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004284 if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004285
4286 s = bc_lex_next(&p->l);
4287 if (s) return s;
4288
4289 bc_parse_push(p, BC_INST_JUMP_ZERO);
4290 bc_parse_pushIndex(p, exit_idx);
4291 bc_parse_push(p, BC_INST_JUMP);
4292 bc_parse_pushIndex(p, body_idx);
4293
4294 ip.idx = p->func->labels.len;
4295
4296 bc_vec_push(&p->conds, &update_idx);
4297 bc_vec_push(&p->func->labels, &p->func->code.len);
4298
4299 if (p->l.t.t != BC_LEX_RPAREN)
4300 s = bc_parse_expr(p, 0, bc_parse_next_rel);
4301 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004302 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004303
4304 if (s) return s;
4305
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004306 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004307 bc_parse_push(p, BC_INST_JUMP);
4308 bc_parse_pushIndex(p, cond_idx);
4309 bc_vec_push(&p->func->labels, &p->func->code.len);
4310
4311 ip.idx = exit_idx;
4312 ip.func = 1;
4313 ip.len = 0;
4314
4315 bc_vec_push(&p->exits, &ip);
4316 bc_vec_push(&p->func->labels, &ip.idx);
4317 bc_lex_next(&p->l);
4318 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4319
4320 return BC_STATUS_SUCCESS;
4321}
4322
4323static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type)
4324{
4325 BcStatus s;
4326 size_t i;
4327 BcInstPtr *ip;
4328
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004329 if (!BC_PARSE_LOOP(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004330
4331 if (type == BC_LEX_KEY_BREAK) {
4332
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004333 if (p->exits.len == 0) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004334
4335 i = p->exits.len - 1;
4336 ip = bc_vec_item(&p->exits, i);
4337
4338 while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004339 if (i >= p->exits.len && !ip->func) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004340
4341 i = ip->idx;
4342 }
4343 else
4344 i = *((size_t *) bc_vec_top(&p->conds));
4345
4346 bc_parse_push(p, BC_INST_JUMP);
4347 bc_parse_pushIndex(p, i);
4348
4349 s = bc_lex_next(&p->l);
4350 if (s) return s;
4351
4352 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004353 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004354
4355 return bc_lex_next(&p->l);
4356}
4357
4358static BcStatus bc_parse_func(BcParse *p)
4359{
4360 BcStatus s;
4361 bool var, comma = false;
4362 uint8_t flags;
4363 char *name;
4364
4365 s = bc_lex_next(&p->l);
4366 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004367 if (p->l.t.t != BC_LEX_NAME)
4368 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004369
4370 name = xstrdup(p->l.t.v.v);
4371 bc_parse_addFunc(p, name, &p->fidx);
4372
4373 s = bc_lex_next(&p->l);
4374 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004375 if (p->l.t.t != BC_LEX_LPAREN)
4376 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004377 s = bc_lex_next(&p->l);
4378 if (s) return s;
4379
4380 while (p->l.t.t != BC_LEX_RPAREN) {
4381
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004382 if (p->l.t.t != BC_LEX_NAME)
4383 return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004384
4385 ++p->func->nparams;
4386
4387 name = xstrdup(p->l.t.v.v);
4388 s = bc_lex_next(&p->l);
4389 if (s) goto err;
4390
4391 var = p->l.t.t != BC_LEX_LBRACKET;
4392
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
4413 s = bc_func_insert(p->func, name, var);
4414 if (s) goto err;
4415 }
4416
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004417 if (comma) return bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004418
4419 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4420 bc_parse_startBody(p, flags);
4421
4422 s = bc_lex_next(&p->l);
4423 if (s) return s;
4424
4425 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01004426 s = bc_posix_error("POSIX requires the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004427
4428 return s;
4429
4430err:
4431 free(name);
4432 return s;
4433}
4434
4435static BcStatus bc_parse_auto(BcParse *p)
4436{
4437 BcStatus s;
4438 bool comma, var, one;
4439 char *name;
4440
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004441 if (!p->auto_part) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004442 s = bc_lex_next(&p->l);
4443 if (s) return s;
4444
4445 p->auto_part = comma = false;
4446 one = p->l.t.t == BC_LEX_NAME;
4447
4448 while (p->l.t.t == BC_LEX_NAME) {
4449
4450 name = xstrdup(p->l.t.v.v);
4451 s = bc_lex_next(&p->l);
4452 if (s) goto err;
4453
4454 var = p->l.t.t != BC_LEX_LBRACKET;
4455 if (!var) {
4456
4457 s = bc_lex_next(&p->l);
4458 if (s) goto err;
4459
4460 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004461 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004462 goto err;
4463 }
4464
4465 s = bc_lex_next(&p->l);
4466 if (s) goto err;
4467 }
4468
4469 comma = p->l.t.t == BC_LEX_COMMA;
4470 if (comma) {
4471 s = bc_lex_next(&p->l);
4472 if (s) goto err;
4473 }
4474
4475 s = bc_func_insert(p->func, name, var);
4476 if (s) goto err;
4477 }
4478
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004479 if (comma) return bc_error("bad function definition");
Denys Vlasenkoabbc4332018-12-03 21:46:41 +01004480 if (!one) return bc_error("no auto variable found");
Gavin Howard01055ba2018-11-03 11:00:21 -06004481
4482 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004483 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004484
4485 return bc_lex_next(&p->l);
4486
4487err:
4488 free(name);
4489 return s;
4490}
4491
4492static BcStatus bc_parse_body(BcParse *p, bool brace)
4493{
4494 BcStatus s = BC_STATUS_SUCCESS;
4495 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4496
4497 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4498
4499 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
4500
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004501 if (!brace) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004502 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4503
4504 if (!p->auto_part) {
4505 s = bc_parse_auto(p);
4506 if (s) return s;
4507 }
4508
4509 if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l);
4510 }
4511 else {
4512 s = bc_parse_stmt(p);
4513 if (!s && !brace) s = bc_parse_endBody(p, false);
4514 }
4515
4516 return s;
4517}
4518
4519static BcStatus bc_parse_stmt(BcParse *p)
4520{
4521 BcStatus s = BC_STATUS_SUCCESS;
4522
4523 switch (p->l.t.t) {
4524
4525 case BC_LEX_NLINE:
4526 {
4527 return bc_lex_next(&p->l);
4528 }
4529
4530 case BC_LEX_KEY_ELSE:
4531 {
4532 p->auto_part = false;
4533 break;
4534 }
4535
4536 case BC_LEX_LBRACE:
4537 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004538 if (!BC_PARSE_BODY(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004539
4540 ++p->nbraces;
4541 s = bc_lex_next(&p->l);
4542 if (s) return s;
4543
4544 return bc_parse_body(p, true);
4545 }
4546
4547 case BC_LEX_KEY_AUTO:
4548 {
4549 return bc_parse_auto(p);
4550 }
4551
4552 default:
4553 {
4554 p->auto_part = false;
4555
4556 if (BC_PARSE_IF_END(p)) {
4557 bc_parse_noElse(p);
4558 return BC_STATUS_SUCCESS;
4559 }
4560 else if (BC_PARSE_BODY(p))
4561 return bc_parse_body(p, false);
4562
4563 break;
4564 }
4565 }
4566
4567 switch (p->l.t.t) {
4568
4569 case BC_LEX_OP_INC:
4570 case BC_LEX_OP_DEC:
4571 case BC_LEX_OP_MINUS:
4572 case BC_LEX_OP_BOOL_NOT:
4573 case BC_LEX_LPAREN:
4574 case BC_LEX_NAME:
4575 case BC_LEX_NUMBER:
4576 case BC_LEX_KEY_IBASE:
4577 case BC_LEX_KEY_LAST:
4578 case BC_LEX_KEY_LENGTH:
4579 case BC_LEX_KEY_OBASE:
4580 case BC_LEX_KEY_READ:
4581 case BC_LEX_KEY_SCALE:
4582 case BC_LEX_KEY_SQRT:
4583 {
4584 s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
4585 break;
4586 }
4587
4588 case BC_LEX_KEY_ELSE:
4589 {
4590 s = bc_parse_else(p);
4591 break;
4592 }
4593
4594 case BC_LEX_SCOLON:
4595 {
4596 while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l);
4597 break;
4598 }
4599
4600 case BC_LEX_RBRACE:
4601 {
4602 s = bc_parse_endBody(p, true);
4603 break;
4604 }
4605
4606 case BC_LEX_STR:
4607 {
4608 s = bc_parse_string(p, BC_INST_PRINT_STR);
4609 break;
4610 }
4611
4612 case BC_LEX_KEY_BREAK:
4613 case BC_LEX_KEY_CONTINUE:
4614 {
4615 s = bc_parse_loopExit(p, p->l.t.t);
4616 break;
4617 }
4618
4619 case BC_LEX_KEY_FOR:
4620 {
4621 s = bc_parse_for(p);
4622 break;
4623 }
4624
4625 case BC_LEX_KEY_HALT:
4626 {
4627 bc_parse_push(p, BC_INST_HALT);
4628 s = bc_lex_next(&p->l);
4629 break;
4630 }
4631
4632 case BC_LEX_KEY_IF:
4633 {
4634 s = bc_parse_if(p);
4635 break;
4636 }
4637
4638 case BC_LEX_KEY_LIMITS:
4639 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004640 // "limits" is a compile-time command,
4641 // the output is produced at _parse time_.
Gavin Howard01055ba2018-11-03 11:00:21 -06004642 s = bc_lex_next(&p->l);
4643 if (s) return s;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004644 printf("BC_BASE_MAX = %u\n", BC_MAX_OBASE);
4645 printf("BC_DIM_MAX = %u\n", BC_MAX_DIM);
4646 printf("BC_SCALE_MAX = %u\n", BC_MAX_SCALE);
4647 printf("BC_STRING_MAX = %u\n", BC_MAX_STRING);
4648 printf("BC_NAME_MAX = %u\n", BC_MAX_NAME);
4649 printf("BC_NUM_MAX = %u\n", BC_MAX_NUM);
4650 printf("MAX Exponent = %lu\n", BC_MAX_EXP);
4651 printf("Number of vars = %lu\n", BC_MAX_VARS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004652 break;
4653 }
4654
4655 case BC_LEX_KEY_PRINT:
4656 {
4657 s = bc_parse_print(p);
4658 break;
4659 }
4660
4661 case BC_LEX_KEY_QUIT:
4662 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004663 // "quit" is a compile-time command. For example,
4664 // "if (0 == 1) quit" terminates when parsing the statement,
4665 // not when it is executed
4666 quit();
Gavin Howard01055ba2018-11-03 11:00:21 -06004667 }
4668
4669 case BC_LEX_KEY_RETURN:
4670 {
4671 s = bc_parse_return(p);
4672 break;
4673 }
4674
4675 case BC_LEX_KEY_WHILE:
4676 {
4677 s = bc_parse_while(p);
4678 break;
4679 }
4680
4681 default:
4682 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004683 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004684 break;
4685 }
4686 }
4687
4688 return s;
4689}
4690
4691static BcStatus bc_parse_parse(BcParse *p)
4692{
4693 BcStatus s;
4694
4695 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004696 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 -06004697 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004698 if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004699 s = bc_parse_func(p);
4700 }
4701 else
4702 s = bc_parse_stmt(p);
4703
Denys Vlasenkod38af482018-12-04 19:11:02 +01004704 if (s || G_interrupt) {
4705 bc_parse_reset(p);
4706 s = BC_STATUS_FAILURE;
4707 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004708
4709 return s;
4710}
4711
4712static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
4713{
4714 BcStatus s = BC_STATUS_SUCCESS;
4715 BcInst prev = BC_INST_PRINT;
4716 BcLexType top, t = p->l.t.t;
4717 size_t nexprs = 0, ops_bgn = p->ops.len;
4718 uint32_t i, nparens, nrelops;
4719 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4720
4721 paren_first = p->l.t.t == BC_LEX_LPAREN;
4722 nparens = nrelops = 0;
4723 paren_expr = rprn = done = get_token = assign = false;
4724 bin_last = true;
4725
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004726 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004727 switch (t) {
4728
4729 case BC_LEX_OP_INC:
4730 case BC_LEX_OP_DEC:
4731 {
4732 s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
4733 rprn = get_token = bin_last = false;
4734 break;
4735 }
4736
4737 case BC_LEX_OP_MINUS:
4738 {
4739 s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
4740 rprn = get_token = false;
4741 bin_last = prev == BC_INST_MINUS;
4742 break;
4743 }
4744
4745 case BC_LEX_OP_ASSIGN_POWER:
4746 case BC_LEX_OP_ASSIGN_MULTIPLY:
4747 case BC_LEX_OP_ASSIGN_DIVIDE:
4748 case BC_LEX_OP_ASSIGN_MODULUS:
4749 case BC_LEX_OP_ASSIGN_PLUS:
4750 case BC_LEX_OP_ASSIGN_MINUS:
4751 case BC_LEX_OP_ASSIGN:
4752 {
4753 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4754 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4755 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4756 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004757 s = bc_error("bad assignment:"
4758 " left side must be scale,"
4759 " ibase, obase, last, var,"
4760 " or array element"
4761 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004762 break;
4763 }
4764 }
4765 // Fallthrough.
4766 case BC_LEX_OP_POWER:
4767 case BC_LEX_OP_MULTIPLY:
4768 case BC_LEX_OP_DIVIDE:
4769 case BC_LEX_OP_MODULUS:
4770 case BC_LEX_OP_PLUS:
4771 case BC_LEX_OP_REL_EQ:
4772 case BC_LEX_OP_REL_LE:
4773 case BC_LEX_OP_REL_GE:
4774 case BC_LEX_OP_REL_NE:
4775 case BC_LEX_OP_REL_LT:
4776 case BC_LEX_OP_REL_GT:
4777 case BC_LEX_OP_BOOL_NOT:
4778 case BC_LEX_OP_BOOL_OR:
4779 case BC_LEX_OP_BOOL_AND:
4780 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004781 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4782 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4783 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004784 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004785 }
4786
4787 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4788 prev = BC_PARSE_TOKEN_INST(t);
4789 s = bc_parse_operator(p, t, ops_bgn, &nexprs, true);
4790 rprn = get_token = false;
4791 bin_last = t != BC_LEX_OP_BOOL_NOT;
4792
4793 break;
4794 }
4795
4796 case BC_LEX_LPAREN:
4797 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004798 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004799 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004800 ++nparens;
4801 paren_expr = rprn = bin_last = false;
4802 get_token = true;
4803 bc_vec_push(&p->ops, &t);
4804
4805 break;
4806 }
4807
4808 case BC_LEX_RPAREN:
4809 {
4810 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004811 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004812
4813 if (nparens == 0) {
4814 s = BC_STATUS_SUCCESS;
4815 done = true;
4816 get_token = false;
4817 break;
4818 }
4819 else if (!paren_expr)
4820 return BC_STATUS_PARSE_EMPTY_EXP;
4821
4822 --nparens;
4823 paren_expr = rprn = true;
4824 get_token = bin_last = false;
4825
4826 s = bc_parse_rightParen(p, ops_bgn, &nexprs);
4827
4828 break;
4829 }
4830
4831 case BC_LEX_NAME:
4832 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004833 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004834 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004835 paren_expr = true;
4836 rprn = get_token = bin_last = false;
4837 s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
4838 ++nexprs;
4839
4840 break;
4841 }
4842
4843 case BC_LEX_NUMBER:
4844 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004845 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004846 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004847 bc_parse_number(p, &prev, &nexprs);
4848 paren_expr = get_token = true;
4849 rprn = bin_last = false;
4850
4851 break;
4852 }
4853
4854 case BC_LEX_KEY_IBASE:
4855 case BC_LEX_KEY_LAST:
4856 case BC_LEX_KEY_OBASE:
4857 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004858 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004859 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004860 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4861 bc_parse_push(p, (char) prev);
4862
4863 paren_expr = get_token = true;
4864 rprn = bin_last = false;
4865 ++nexprs;
4866
4867 break;
4868 }
4869
4870 case BC_LEX_KEY_LENGTH:
4871 case BC_LEX_KEY_SQRT:
4872 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004873 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004874 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004875 s = bc_parse_builtin(p, t, flags, &prev);
4876 paren_expr = true;
4877 rprn = get_token = bin_last = false;
4878 ++nexprs;
4879
4880 break;
4881 }
4882
4883 case BC_LEX_KEY_READ:
4884 {
4885 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004886 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004887 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004888 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004889 else
4890 s = bc_parse_read(p);
4891
4892 paren_expr = true;
4893 rprn = get_token = bin_last = false;
4894 ++nexprs;
4895 prev = BC_INST_READ;
4896
4897 break;
4898 }
4899
4900 case BC_LEX_KEY_SCALE:
4901 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004902 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004903 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004904 s = bc_parse_scale(p, &prev, flags);
4905 paren_expr = true;
4906 rprn = get_token = bin_last = false;
4907 ++nexprs;
4908 prev = BC_INST_SCALE;
4909
4910 break;
4911 }
4912
4913 default:
4914 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004915 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004916 break;
4917 }
4918 }
4919
4920 if (!s && get_token) s = bc_lex_next(&p->l);
4921 }
4922
4923 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004924 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004925
4926 while (p->ops.len > ops_bgn) {
4927
4928 top = BC_PARSE_TOP_OP(p);
4929 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4930
4931 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004932 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004933
4934 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
4935
4936 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4937 bc_vec_pop(&p->ops);
4938 }
4939
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004940 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004941 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004942
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004943 for (i = 0; i < next.len; ++i)
4944 if (t == next.tokens[i])
4945 goto ok;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004946 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004947 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06004948
4949 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004950 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Gavin Howard01055ba2018-11-03 11:00:21 -06004951 if (s) return s;
4952 }
4953 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01004954 s = bc_posix_error("POSIX requires exactly one comparison operator per condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004955 if (s) return s;
4956 }
4957
4958 if (flags & BC_PARSE_PRINT) {
4959 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4960 bc_parse_push(p, BC_INST_POP);
4961 }
4962
4963 return s;
4964}
4965
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004966static void bc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06004967{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004968 bc_parse_create(p, func, bc_parse_parse, bc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06004969}
4970
4971static BcStatus bc_parse_expression(BcParse *p, uint8_t flags)
4972{
4973 return bc_parse_expr(p, flags, bc_parse_next_read);
4974}
4975#endif // ENABLE_BC
4976
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004977#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06004978static BcStatus dc_parse_register(BcParse *p)
4979{
4980 BcStatus s;
4981 char *name;
4982
4983 s = bc_lex_next(&p->l);
4984 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004985 if (p->l.t.t != BC_LEX_NAME) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004986
4987 name = xstrdup(p->l.t.v.v);
4988 bc_parse_pushName(p, name);
4989
4990 return s;
4991}
4992
4993static BcStatus dc_parse_string(BcParse *p)
4994{
4995 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004996 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004997
4998 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
4999 name = xstrdup(b);
5000
5001 str = xstrdup(p->l.t.v.v);
5002 bc_parse_push(p, BC_INST_STR);
5003 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005004 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06005005 bc_parse_addFunc(p, name, &idx);
5006
5007 return bc_lex_next(&p->l);
5008}
5009
5010static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
5011{
5012 BcStatus s;
5013
5014 bc_parse_push(p, inst);
5015 if (name) {
5016 s = dc_parse_register(p);
5017 if (s) return s;
5018 }
5019
5020 if (store) {
5021 bc_parse_push(p, BC_INST_SWAP);
5022 bc_parse_push(p, BC_INST_ASSIGN);
5023 bc_parse_push(p, BC_INST_POP);
5024 }
5025
5026 return bc_lex_next(&p->l);
5027}
5028
5029static BcStatus dc_parse_cond(BcParse *p, uint8_t inst)
5030{
5031 BcStatus s;
5032
5033 bc_parse_push(p, inst);
5034 bc_parse_push(p, BC_INST_EXEC_COND);
5035
5036 s = dc_parse_register(p);
5037 if (s) return s;
5038
5039 s = bc_lex_next(&p->l);
5040 if (s) return s;
5041
5042 if (p->l.t.t == BC_LEX_ELSE) {
5043 s = dc_parse_register(p);
5044 if (s) return s;
5045 s = bc_lex_next(&p->l);
5046 }
5047 else
5048 bc_parse_push(p, BC_PARSE_STREND);
5049
5050 return s;
5051}
5052
5053static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
5054{
5055 BcStatus s = BC_STATUS_SUCCESS;
5056 BcInst prev;
5057 uint8_t inst;
5058 bool assign, get_token = false;
5059
5060 switch (t) {
5061
5062 case BC_LEX_OP_REL_EQ:
5063 case BC_LEX_OP_REL_LE:
5064 case BC_LEX_OP_REL_GE:
5065 case BC_LEX_OP_REL_NE:
5066 case BC_LEX_OP_REL_LT:
5067 case BC_LEX_OP_REL_GT:
5068 {
5069 s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
5070 break;
5071 }
5072
5073 case BC_LEX_SCOLON:
5074 case BC_LEX_COLON:
5075 {
5076 s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
5077 break;
5078 }
5079
5080 case BC_LEX_STR:
5081 {
5082 s = dc_parse_string(p);
5083 break;
5084 }
5085
5086 case BC_LEX_NEG:
5087 case BC_LEX_NUMBER:
5088 {
5089 if (t == BC_LEX_NEG) {
5090 s = bc_lex_next(&p->l);
5091 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005092 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005093 return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005094 }
5095
5096 bc_parse_number(p, &prev, &p->nbraces);
5097
5098 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5099 get_token = true;
5100
5101 break;
5102 }
5103
5104 case BC_LEX_KEY_READ:
5105 {
5106 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005107 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005108 else
5109 bc_parse_push(p, BC_INST_READ);
5110 get_token = true;
5111 break;
5112 }
5113
5114 case BC_LEX_OP_ASSIGN:
5115 case BC_LEX_STORE_PUSH:
5116 {
5117 assign = t == BC_LEX_OP_ASSIGN;
5118 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
5119 s = dc_parse_mem(p, inst, true, assign);
5120 break;
5121 }
5122
5123 case BC_LEX_LOAD:
5124 case BC_LEX_LOAD_POP:
5125 {
5126 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
5127 s = dc_parse_mem(p, inst, true, false);
5128 break;
5129 }
5130
5131 case BC_LEX_STORE_IBASE:
5132 case BC_LEX_STORE_SCALE:
5133 case BC_LEX_STORE_OBASE:
5134 {
5135 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
5136 s = dc_parse_mem(p, inst, false, true);
5137 break;
5138 }
5139
5140 default:
5141 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005142 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005143 get_token = true;
5144 break;
5145 }
5146 }
5147
5148 if (!s && get_token) s = bc_lex_next(&p->l);
5149
5150 return s;
5151}
5152
5153static BcStatus dc_parse_expr(BcParse *p, uint8_t flags)
5154{
5155 BcStatus s = BC_STATUS_SUCCESS;
5156 BcInst inst;
5157 BcLexType t;
5158
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005159 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005160
5161 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
5162
5163 inst = dc_parse_insts[t];
5164
5165 if (inst != BC_INST_INVALID) {
5166 bc_parse_push(p, inst);
5167 s = bc_lex_next(&p->l);
5168 }
5169 else
5170 s = dc_parse_token(p, t, flags);
5171 }
5172
5173 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5174 bc_parse_push(p, BC_INST_POP_EXEC);
5175
5176 return s;
5177}
5178
5179static BcStatus dc_parse_parse(BcParse *p)
5180{
5181 BcStatus s;
5182
5183 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005184 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005185 else
5186 s = dc_parse_expr(p, 0);
5187
Denys Vlasenkod38af482018-12-04 19:11:02 +01005188 if (s || G_interrupt) {
5189 bc_parse_reset(p);
5190 s = BC_STATUS_FAILURE;
5191 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005192
5193 return s;
5194}
5195
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005196static void dc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005197{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005198 bc_parse_create(p, func, dc_parse_parse, dc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005199}
5200#endif // ENABLE_DC
5201
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005202static void common_parse_init(BcParse *p, size_t func)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005203{
5204 if (IS_BC) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005205 bc_parse_init(p, func);
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005206 } else {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005207 dc_parse_init(p, func);
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005208 }
5209}
5210
5211static BcStatus common_parse_expr(BcParse *p, uint8_t flags)
5212{
5213 if (IS_BC) {
5214 return bc_parse_expression(p, flags);
5215 } else {
5216 return dc_parse_expr(p, flags);
5217 }
5218}
5219
Denys Vlasenkodf515392018-12-02 19:27:48 +01005220static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005221{
Gavin Howard01055ba2018-11-03 11:00:21 -06005222 BcId e, *ptr;
5223 BcVec *v, *map;
5224 size_t i;
5225 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005226 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005227
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005228 v = var ? &G.prog.vars : &G.prog.arrs;
5229 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005230
5231 e.name = id;
5232 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005233 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005234
5235 if (new) {
5236 bc_array_init(&data.v, var);
5237 bc_vec_push(v, &data.v);
5238 }
5239
5240 ptr = bc_vec_item(map, i);
5241 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005242 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005243}
5244
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005245static BcStatus bc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005246{
5247 BcStatus s = BC_STATUS_SUCCESS;
5248
5249 switch (r->t) {
5250
5251 case BC_RESULT_STR:
5252 case BC_RESULT_TEMP:
5253 case BC_RESULT_IBASE:
5254 case BC_RESULT_SCALE:
5255 case BC_RESULT_OBASE:
5256 {
5257 *num = &r->d.n;
5258 break;
5259 }
5260
5261 case BC_RESULT_CONSTANT:
5262 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005263 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005264 size_t base_t, len = strlen(*str);
5265 BcNum *base;
5266
5267 bc_num_init(&r->d.n, len);
5268
5269 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005270 base = hex ? &G.prog.hexb : &G.prog.ib;
5271 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005272 s = bc_num_parse(&r->d.n, *str, base, base_t);
5273
5274 if (s) {
5275 bc_num_free(&r->d.n);
5276 return s;
5277 }
5278
5279 *num = &r->d.n;
5280 r->t = BC_RESULT_TEMP;
5281
5282 break;
5283 }
5284
5285 case BC_RESULT_VAR:
5286 case BC_RESULT_ARRAY:
5287 case BC_RESULT_ARRAY_ELEM:
5288 {
5289 BcVec *v;
5290
Denys Vlasenkodf515392018-12-02 19:27:48 +01005291 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005292
5293 if (r->t == BC_RESULT_ARRAY_ELEM) {
5294 v = bc_vec_top(v);
5295 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5296 *num = bc_vec_item(v, r->d.id.idx);
5297 }
5298 else
5299 *num = bc_vec_top(v);
5300
5301 break;
5302 }
5303
5304 case BC_RESULT_LAST:
5305 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005306 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005307 break;
5308 }
5309
5310 case BC_RESULT_ONE:
5311 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005312 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005313 break;
5314 }
5315 }
5316
5317 return s;
5318}
5319
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005320static BcStatus bc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005321 BcResult **r, BcNum **rn, bool assign)
5322{
5323 BcStatus s;
5324 bool hex;
5325 BcResultType lt, rt;
5326
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005327 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005328 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06005329
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005330 *r = bc_vec_item_rev(&G.prog.results, 0);
5331 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005332
5333 lt = (*l)->t;
5334 rt = (*r)->t;
5335 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5336
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005337 s = bc_program_num(*l, ln, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005338 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005339 s = bc_program_num(*r, rn, hex);
Gavin Howard01055ba2018-11-03 11:00:21 -06005340 if (s) return s;
5341
5342 // We run this again under these conditions in case any vector has been
5343 // reallocated out from under the BcNums or arrays we had.
5344 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005345 s = bc_program_num(*l, ln, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005346 if (s) return s;
5347 }
5348
5349 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005350 return bc_error_variable_is_wrong_type();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005351 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005352 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06005353
Gavin Howard01055ba2018-11-03 11:00:21 -06005354 return s;
5355}
5356
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005357static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005358{
5359 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005360 bc_vec_pop(&G.prog.results);
5361 bc_vec_pop(&G.prog.results);
5362 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005363}
5364
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005365static BcStatus bc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005366{
5367 BcStatus s;
5368
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005369 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005370 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005371 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005372
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005373 s = bc_program_num(*r, n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005374 if (s) return s;
5375
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005376 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005377 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06005378
5379 return s;
5380}
5381
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005382static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005383{
5384 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005385 bc_vec_pop(&G.prog.results);
5386 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005387}
5388
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005389static BcStatus bc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005390{
5391 BcStatus s;
5392 BcResult *opd1, *opd2, res;
5393 BcNum *n1, *n2 = NULL;
5394
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005395 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005396 if (s) return s;
5397 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
5398
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005399 s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005400 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005401 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005402
5403 return s;
5404
5405err:
5406 bc_num_free(&res.d.n);
5407 return s;
5408}
5409
Denys Vlasenko785e4b32018-12-02 17:18:52 +01005410static BcStatus bc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005411{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005412 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005413 BcStatus s;
5414 BcParse parse;
5415 BcVec buf;
5416 BcInstPtr ip;
5417 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005418 BcFunc *f = bc_vec_item(&G.prog.fns, BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005419
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005420 for (i = 0; i < G.prog.stack.len; ++i) {
5421 BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005422 if (ip_ptr->func == BC_PROG_READ)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005423 return bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005424 }
5425
Denys Vlasenko7d628012018-12-04 21:46:47 +01005426 bc_vec_pop_all(&f->code);
5427 bc_char_vec_init(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005428
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005429 sv_file = G.prog.file;
5430 G.prog.file = NULL;
5431
Gavin Howard01055ba2018-11-03 11:00:21 -06005432 s = bc_read_line(&buf, "read> ");
5433 if (s) goto io_err;
5434
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005435 common_parse_init(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005436 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005437
5438 s = bc_parse_text(&parse, buf.v);
5439 if (s) goto exec_err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005440 s = common_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005441 if (s) goto exec_err;
5442
5443 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005444 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005445 goto exec_err;
5446 }
5447
5448 ip.func = BC_PROG_READ;
5449 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005450 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005451
5452 // Update this pointer, just in case.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005453 f = bc_vec_item(&G.prog.fns, BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005454
5455 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005456 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005457
5458exec_err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005459 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005460 bc_parse_free(&parse);
5461io_err:
5462 bc_vec_free(&buf);
5463 return s;
5464}
5465
5466static size_t bc_program_index(char *code, size_t *bgn)
5467{
5468 char amt = code[(*bgn)++], i = 0;
5469 size_t res = 0;
5470
5471 for (; i < amt; ++i, ++(*bgn))
5472 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5473
5474 return res;
5475}
5476
5477static char *bc_program_name(char *code, size_t *bgn)
5478{
5479 size_t i;
5480 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5481
5482 s = xmalloc(ptr - str + 1);
5483 c = code[(*bgn)++];
5484
5485 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5486 s[i] = c;
5487
5488 s[i] = '\0';
5489
5490 return s;
5491}
5492
5493static void bc_program_printString(const char *str, size_t *nchars)
5494{
5495 size_t i, len = strlen(str);
5496
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005497#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005498 if (len == 0) {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005499 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005500 return;
5501 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005502#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005503
5504 for (i = 0; i < len; ++i, ++(*nchars)) {
5505
5506 int c = str[i];
5507
5508 if (c != '\\' || i == len - 1)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005509 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005510 else {
5511
5512 c = str[++i];
5513
5514 switch (c) {
5515
5516 case 'a':
5517 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005518 bb_putchar('\a');
Gavin Howard01055ba2018-11-03 11:00:21 -06005519 break;
5520 }
5521
5522 case 'b':
5523 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005524 bb_putchar('\b');
Gavin Howard01055ba2018-11-03 11:00:21 -06005525 break;
5526 }
5527
5528 case '\\':
5529 case 'e':
5530 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005531 bb_putchar('\\');
Gavin Howard01055ba2018-11-03 11:00:21 -06005532 break;
5533 }
5534
5535 case 'f':
5536 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005537 bb_putchar('\f');
Gavin Howard01055ba2018-11-03 11:00:21 -06005538 break;
5539 }
5540
5541 case 'n':
5542 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005543 bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005544 *nchars = SIZE_MAX;
5545 break;
5546 }
5547
5548 case 'r':
5549 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005550 bb_putchar('\r');
Gavin Howard01055ba2018-11-03 11:00:21 -06005551 break;
5552 }
5553
5554 case 'q':
5555 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005556 bb_putchar('"');
Gavin Howard01055ba2018-11-03 11:00:21 -06005557 break;
5558 }
5559
5560 case 't':
5561 {
Denys Vlasenko00d77792018-11-30 23:13:42 +01005562 bb_putchar('\t');
Gavin Howard01055ba2018-11-03 11:00:21 -06005563 break;
5564 }
5565
5566 default:
5567 {
5568 // Just print the backslash and following character.
Denys Vlasenko00d77792018-11-30 23:13:42 +01005569 bb_putchar('\\');
Gavin Howard01055ba2018-11-03 11:00:21 -06005570 ++(*nchars);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005571 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005572 break;
5573 }
5574 }
5575 }
5576 }
5577}
5578
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005579static BcStatus bc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005580{
5581 BcStatus s = BC_STATUS_SUCCESS;
5582 BcResult *r;
5583 size_t len, i;
5584 char *str;
5585 BcNum *num = NULL;
5586 bool pop = inst != BC_INST_PRINT;
5587
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005588 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005589 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06005590
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005591 r = bc_vec_item_rev(&G.prog.results, idx);
5592 s = bc_program_num(r, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005593 if (s) return s;
5594
5595 if (BC_PROG_NUM(r, num)) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005596 s = bc_num_print(num, &G.prog.ob, G.prog.ob_t, !pop, &G.prog.nchars, G.prog.len);
5597 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005598 }
5599 else {
5600
5601 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005602 str = *((char **) bc_vec_item(&G.prog.strs, idx));
Gavin Howard01055ba2018-11-03 11:00:21 -06005603
5604 if (inst == BC_INST_PRINT_STR) {
5605 for (i = 0, len = strlen(str); i < len; ++i) {
5606 char c = str[i];
Denys Vlasenko00d77792018-11-30 23:13:42 +01005607 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005608 if (c == '\n') G.prog.nchars = SIZE_MAX;
5609 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005610 }
5611 }
5612 else {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005613 bc_program_printString(str, &G.prog.nchars);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005614 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005615 }
5616 }
5617
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005618 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005619
5620 return s;
5621}
5622
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005623static BcStatus bc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005624{
5625 BcStatus s;
5626 BcResult res, *ptr;
5627 BcNum *num = NULL;
5628
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005629 s = bc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005630 if (s) return s;
5631
5632 bc_num_init(&res.d.n, num->len);
5633 bc_num_copy(&res.d.n, num);
5634 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5635
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005636 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005637
5638 return s;
5639}
5640
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005641static BcStatus bc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005642{
5643 BcStatus s;
5644 BcResult *opd1, *opd2, res;
5645 BcNum *n1, *n2;
5646 bool cond = 0;
5647 ssize_t cmp;
5648
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005649 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005650 if (s) return s;
5651 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
5652
5653 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005654 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005655 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005656 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005657 else {
5658
5659 cmp = bc_num_cmp(n1, n2);
5660
5661 switch (inst) {
5662
5663 case BC_INST_REL_EQ:
5664 {
5665 cond = cmp == 0;
5666 break;
5667 }
5668
5669 case BC_INST_REL_LE:
5670 {
5671 cond = cmp <= 0;
5672 break;
5673 }
5674
5675 case BC_INST_REL_GE:
5676 {
5677 cond = cmp >= 0;
5678 break;
5679 }
5680
5681 case BC_INST_REL_NE:
5682 {
5683 cond = cmp != 0;
5684 break;
5685 }
5686
5687 case BC_INST_REL_LT:
5688 {
5689 cond = cmp < 0;
5690 break;
5691 }
5692
5693 case BC_INST_REL_GT:
5694 {
5695 cond = cmp > 0;
5696 break;
5697 }
5698 }
5699 }
5700
5701 (cond ? bc_num_one : bc_num_zero)(&res.d.n);
5702
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005703 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005704
5705 return s;
5706}
5707
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005708#if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005709static BcStatus bc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005710 bool push)
5711{
5712 BcNum n2;
5713 BcResult res;
5714
5715 memset(&n2, 0, sizeof(BcNum));
5716 n2.rdx = res.d.id.idx = r->d.id.idx;
5717 res.t = BC_RESULT_STR;
5718
5719 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005720 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005721 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06005722 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005723 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005724 }
5725
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005726 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005727
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005728 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005729 bc_vec_push(v, &n2);
5730
5731 return BC_STATUS_SUCCESS;
5732}
5733#endif // ENABLE_DC
5734
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005735static BcStatus bc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005736{
5737 BcStatus s;
5738 BcResult *ptr, r;
5739 BcVec *v;
5740 BcNum *n;
5741
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005742 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005743 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06005744
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005745 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005746 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005747 return bc_error_variable_is_wrong_type();
Denys Vlasenkodf515392018-12-02 19:27:48 +01005748 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005749
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005750#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005751 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005752 return bc_error_variable_is_wrong_type();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005753 if (ptr->t == BC_RESULT_STR) return bc_program_assignStr(ptr, v, true);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005754#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005755
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005756 s = bc_program_num(ptr, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005757 if (s) return s;
5758
5759 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005760 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005761
5762 if (var) {
5763 bc_num_init(&r.d.n, BC_NUM_DEF_SIZE);
5764 bc_num_copy(&r.d.n, n);
5765 }
5766 else {
5767 bc_array_init(&r.d.v, true);
5768 bc_array_copy(&r.d.v, (BcVec *) n);
5769 }
5770
5771 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005772 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005773
5774 return s;
5775}
5776
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005777static BcStatus bc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005778{
5779 BcStatus s;
5780 BcResult *left, *right, res;
5781 BcNum *l = NULL, *r = NULL;
5782 unsigned long val, max;
5783 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5784
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005785 s = bc_program_binOpPrep(&left, &l, &right, &r, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005786 if (s) return s;
5787
5788 ib = left->t == BC_RESULT_IBASE;
5789 sc = left->t == BC_RESULT_SCALE;
5790
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005791#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005792
5793 if (right->t == BC_RESULT_STR) {
5794
5795 BcVec *v;
5796
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005797 if (left->t != BC_RESULT_VAR)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005798 return bc_error_variable_is_wrong_type();
Denys Vlasenkodf515392018-12-02 19:27:48 +01005799 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005800
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005801 return bc_program_assignStr(right, v, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005802 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005803#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005804
5805 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005806 return bc_error("bad assignment:"
5807 " left side must be scale,"
5808 " ibase, obase, last, var,"
5809 " or array element"
5810 );
Gavin Howard01055ba2018-11-03 11:00:21 -06005811
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005812#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005813 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005814 return bc_error("divide by zero");
Gavin Howard01055ba2018-11-03 11:00:21 -06005815
5816 if (assign)
5817 bc_num_copy(l, r);
5818 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005819 s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005820
5821 if (s) return s;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005822#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005823 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005824#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005825
5826 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005827 static const char *const msg[] = {
5828 "bad ibase; must be [2, 16]", //BC_RESULT_IBASE
5829 "bad scale; must be [0, BC_SCALE_MAX]", //BC_RESULT_SCALE
Denys Vlasenkoaad652a2018-12-05 20:33:23 +01005830 NULL, //can't happen //BC_RESULT_LAST
5831 NULL, //can't happen //BC_RESULT_CONSTANT
5832 NULL, //can't happen //BC_RESULT_ONE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005833 "bad obase; must be [2, BC_BASE_MAX]", //BC_RESULT_OBASE
5834 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005835 size_t *ptr;
5836
5837 s = bc_num_ulong(l, &val);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005838 if (s)
5839 return s;
5840 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005841 if (sc) {
5842 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005843 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06005844 }
5845 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005846 if (val < BC_NUM_MIN_BASE)
5847 return bc_error(msg[s]);
Gavin Howard01055ba2018-11-03 11:00:21 -06005848 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005849 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005850 }
5851
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005852 if (val > max)
5853 return bc_error(msg[s]);
5854 if (!sc)
5855 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005856
5857 *ptr = (size_t) val;
5858 s = BC_STATUS_SUCCESS;
5859 }
5860
5861 bc_num_init(&res.d.n, l->len);
5862 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005863 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005864
5865 return s;
5866}
5867
Denys Vlasenko416ce762018-12-02 20:57:17 +01005868#if !ENABLE_DC
5869#define bc_program_pushVar(code, bgn, pop, copy) \
5870 bc_program_pushVar(code, bgn)
5871// for bc, 'pop' and 'copy' are always false
5872#endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005873static BcStatus bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005874 bool pop, bool copy)
5875{
5876 BcStatus s = BC_STATUS_SUCCESS;
5877 BcResult r;
5878 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005879
5880 r.t = BC_RESULT_VAR;
5881 r.d.id.name = name;
5882
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005883#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01005884 {
5885 BcVec *v = bc_program_search(name, true);
5886 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005887
Denys Vlasenko416ce762018-12-02 20:57:17 +01005888 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005889
Denys Vlasenko416ce762018-12-02 20:57:17 +01005890 if (!BC_PROG_STACK(v, 2 - copy)) {
5891 free(name);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005892 return bc_error_stack_has_too_few_elements();
Denys Vlasenko416ce762018-12-02 20:57:17 +01005893 }
5894
Gavin Howard01055ba2018-11-03 11:00:21 -06005895 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01005896 name = NULL;
5897
5898 if (!BC_PROG_STR(num)) {
5899
5900 r.t = BC_RESULT_TEMP;
5901
5902 bc_num_init(&r.d.n, BC_NUM_DEF_SIZE);
5903 bc_num_copy(&r.d.n, num);
5904 }
5905 else {
5906 r.t = BC_RESULT_STR;
5907 r.d.id.idx = num->rdx;
5908 }
5909
5910 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005911 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005912 }
5913#endif // ENABLE_DC
5914
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005915 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005916
5917 return s;
5918}
5919
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005920static BcStatus bc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005921 char inst)
5922{
5923 BcStatus s = BC_STATUS_SUCCESS;
5924 BcResult r;
5925 BcNum *num;
5926
5927 r.d.id.name = bc_program_name(code, bgn);
5928
5929 if (inst == BC_INST_ARRAY) {
5930 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005931 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005932 }
5933 else {
5934
5935 BcResult *operand;
5936 unsigned long temp;
5937
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005938 s = bc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005939 if (s) goto err;
5940 s = bc_num_ulong(num, &temp);
5941 if (s) goto err;
5942
5943 if (temp > BC_MAX_DIM) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005944 s = bc_error("array too long; must be [1, BC_DIM_MAX]");
Gavin Howard01055ba2018-11-03 11:00:21 -06005945 goto err;
5946 }
5947
5948 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005949 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06005950 }
5951
5952err:
5953 if (s) free(r.d.id.name);
5954 return s;
5955}
5956
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005957#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005958static BcStatus bc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005959{
5960 BcStatus s;
5961 BcResult *ptr, res, copy;
5962 BcNum *num = NULL;
5963 char inst2 = inst;
5964
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005965 s = bc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005966 if (s) return s;
5967
5968 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
5969 copy.t = BC_RESULT_TEMP;
5970 bc_num_init(&copy.d.n, num->len);
5971 bc_num_copy(&copy.d.n, num);
5972 }
5973
5974 res.t = BC_RESULT_ONE;
5975 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
5976 BC_INST_ASSIGN_PLUS :
5977 BC_INST_ASSIGN_MINUS;
5978
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005979 bc_vec_push(&G.prog.results, &res);
5980 bc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06005981
5982 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005983 bc_vec_pop(&G.prog.results);
5984 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06005985 }
5986
5987 return s;
5988}
5989
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005990static BcStatus bc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005991{
5992 BcStatus s = BC_STATUS_SUCCESS;
5993 BcInstPtr ip;
5994 size_t i, nparams = bc_program_index(code, idx);
5995 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06005996 BcId *a;
5997 BcResultData param;
5998 BcResult *arg;
5999
6000 ip.idx = 0;
6001 ip.func = bc_program_index(code, idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006002 func = bc_vec_item(&G.prog.fns, ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006003
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006004 if (func->code.len == 0) {
6005 return bc_error("undefined function");
6006 }
6007 if (nparams != func->nparams) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006008 return bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams);
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006009 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006010 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006011
6012 for (i = 0; i < nparams; ++i) {
6013
6014 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006015 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006016
6017 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006018 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006019
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006020 s = bc_program_copyToVar(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006021 if (s) return s;
6022 }
6023
6024 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006025 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006026
6027 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006028 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006029
6030 if (a->idx) {
6031 bc_num_init(&param.n, BC_NUM_DEF_SIZE);
6032 bc_vec_push(v, &param.n);
6033 }
6034 else {
6035 bc_array_init(&param.v, true);
6036 bc_vec_push(v, &param.v);
6037 }
6038 }
6039
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006040 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006041
6042 return BC_STATUS_SUCCESS;
6043}
6044
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006045static BcStatus bc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006046{
6047 BcStatus s;
6048 BcResult res;
6049 BcFunc *f;
6050 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006051 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006052
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006053 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006054 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006055
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006056 f = bc_vec_item(&G.prog.fns, ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006057 res.t = BC_RESULT_TEMP;
6058
6059 if (inst == BC_INST_RET) {
6060
6061 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006062 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006063
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006064 s = bc_program_num(operand, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006065 if (s) return s;
6066 bc_num_init(&res.d.n, num->len);
6067 bc_num_copy(&res.d.n, num);
6068 }
6069 else {
6070 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
6071 bc_num_zero(&res.d.n);
6072 }
6073
6074 // We need to pop arguments as well, so this takes that into account.
6075 for (i = 0; i < f->autos.len; ++i) {
6076
6077 BcVec *v;
6078 BcId *a = bc_vec_item(&f->autos, i);
6079
Denys Vlasenkodf515392018-12-02 19:27:48 +01006080 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006081 bc_vec_pop(v);
6082 }
6083
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006084 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6085 bc_vec_push(&G.prog.results, &res);
6086 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006087
6088 return BC_STATUS_SUCCESS;
6089}
6090#endif // ENABLE_BC
6091
6092static unsigned long bc_program_scale(BcNum *n)
6093{
6094 return (unsigned long) n->rdx;
6095}
6096
6097static unsigned long bc_program_len(BcNum *n)
6098{
6099 unsigned long len = n->len;
6100 size_t i;
6101
6102 if (n->rdx != n->len) return len;
6103 for (i = n->len - 1; i < n->len && n->num[i] == 0; --len, --i);
6104
6105 return len;
6106}
6107
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006108static BcStatus bc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006109{
6110 BcStatus s;
6111 BcResult *opnd;
6112 BcNum *num = NULL;
6113 BcResult res;
6114 bool len = inst == BC_INST_LENGTH;
6115
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006116 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006117 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006118 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006119
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006120 s = bc_program_num(opnd, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006121 if (s) return s;
6122
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006123#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006124 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006125 return bc_error_variable_is_wrong_type();
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006126#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006127
6128 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
6129
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006130 if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006131#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006132 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006133 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006134 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006135#endif
6136#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006137 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
6138
6139 char **str;
6140 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6141
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006142 str = bc_vec_item(&G.prog.strs, idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006143 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006144 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006145#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006146 else {
6147 BcProgramBuiltIn f = len ? bc_program_len : bc_program_scale;
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006148 bc_num_ulong2num(&res.d.n, f(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006149 }
6150
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006151 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006152
6153 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006154}
6155
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006156#if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006157static BcStatus bc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006158{
6159 BcStatus s;
6160 BcResult *opd1, *opd2, res, res2;
6161 BcNum *n1, *n2 = NULL;
6162
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006163 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006164 if (s) return s;
6165
6166 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
6167 bc_num_init(&res2.d.n, n2->len);
6168
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006169 s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006170 if (s) goto err;
6171
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006172 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006173 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006174 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006175
6176 return s;
6177
6178err:
6179 bc_num_free(&res2.d.n);
6180 bc_num_free(&res.d.n);
6181 return s;
6182}
6183
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006184static BcStatus bc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006185{
6186 BcStatus s;
6187 BcResult *r1, *r2, *r3, res;
6188 BcNum *n1, *n2, *n3;
6189
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006190 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006191 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006192 s = bc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006193 if (s) return s;
6194
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006195 r1 = bc_vec_item_rev(&G.prog.results, 2);
6196 s = bc_program_num(r1, &n1, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006197 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006198 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006199 return bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006200
6201 // Make sure that the values have their pointers updated, if necessary.
6202 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6203
6204 if (r1->t == r2->t) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006205 s = bc_program_num(r2, &n2, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006206 if (s) return s;
6207 }
6208
6209 if (r1->t == r3->t) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006210 s = bc_program_num(r3, &n3, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006211 if (s) return s;
6212 }
6213 }
6214
6215 bc_num_init(&res.d.n, n3->len);
6216 s = bc_num_modexp(n1, n2, n3, &res.d.n);
6217 if (s) goto err;
6218
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006219 bc_vec_pop(&G.prog.results);
6220 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006221
6222 return s;
6223
6224err:
6225 bc_num_free(&res.d.n);
6226 return s;
6227}
6228
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006229static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006230{
Gavin Howard01055ba2018-11-03 11:00:21 -06006231 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006232 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006233
6234 res.t = BC_RESULT_TEMP;
6235
6236 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006237 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006238 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006239}
6240
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006241static BcStatus bc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006242{
6243 BcStatus s;
6244 BcResult *r, res;
6245 BcNum *num = NULL, n;
6246 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006247 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006248 unsigned long val;
6249
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006250 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006251 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006252 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006253
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006254 s = bc_program_num(r, &num, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006255 if (s) return s;
6256
6257 if (BC_PROG_NUM(r, num)) {
6258
6259 bc_num_init(&n, BC_NUM_DEF_SIZE);
6260 bc_num_copy(&n, num);
6261 bc_num_truncate(&n, n.rdx);
6262
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006263 s = bc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006264 if (s) goto num_err;
6265 s = bc_num_ulong(&n, &val);
6266 if (s) goto num_err;
6267
6268 c = (char) val;
6269
6270 bc_num_free(&n);
6271 }
6272 else {
6273 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006274 str2 = *((char **) bc_vec_item(&G.prog.strs, idx));
Gavin Howard01055ba2018-11-03 11:00:21 -06006275 c = str2[0];
6276 }
6277
6278 str = xmalloc(2);
6279 str[0] = c;
6280 str[1] = '\0';
6281
6282 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006283 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006284
6285 if (idx != len + BC_PROG_REQ_FUNCS) {
6286
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006287 for (idx = 0; idx < G.prog.strs.len; ++idx) {
6288 if (!strcmp(*((char **) bc_vec_item(&G.prog.strs, idx)), str)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006289 len = idx;
6290 break;
6291 }
6292 }
6293
6294 free(str);
6295 }
6296 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006297 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006298
6299 res.t = BC_RESULT_STR;
6300 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006301 bc_vec_pop(&G.prog.results);
6302 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006303
6304 return BC_STATUS_SUCCESS;
6305
6306num_err:
6307 bc_num_free(&n);
6308 return s;
6309}
6310
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006311static BcStatus bc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006312{
6313 BcStatus s;
6314 BcResult *r;
6315 BcNum *n = NULL;
6316 size_t idx;
6317 char *str;
6318
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006319 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006320 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006321 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006322
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006323 s = bc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006324 if (s) return s;
6325
6326 if (BC_PROG_NUM(r, n))
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006327 s = bc_num_stream(n, &G.prog.strmb, &G.prog.nchars, G.prog.len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006328 else {
6329 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006330 str = *((char **) bc_vec_item(&G.prog.strs, idx));
Denys Vlasenko00d77792018-11-30 23:13:42 +01006331 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006332 }
6333
6334 return s;
6335}
6336
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006337static BcStatus bc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006338{
6339 BcStatus s;
6340 BcResult *opnd;
6341 BcNum *num = NULL;
6342 unsigned long val;
6343
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006344 s = bc_program_prep(&opnd, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006345 if (s) return s;
6346 s = bc_num_ulong(num, &val);
6347 if (s) return s;
6348
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006349 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006350
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006351 if (G.prog.stack.len < val)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006352 return bc_error_stack_has_too_few_elements();
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006353 if (G.prog.stack.len == val)
6354 quit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006355
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006356 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006357
6358 return s;
6359}
6360
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006361static BcStatus bc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006362 bool cond)
6363{
6364 BcStatus s = BC_STATUS_SUCCESS;
6365 BcResult *r;
6366 char **str;
6367 BcFunc *f;
6368 BcParse prs;
6369 BcInstPtr ip;
6370 size_t fidx, sidx;
6371 BcNum *n;
6372 bool exec;
6373
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006374 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006375 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006376
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006377 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006378
6379 if (cond) {
6380
Gavin Howard01055ba2018-11-03 11:00:21 -06006381 char *name, *then_name = bc_program_name(code, bgn), *else_name = NULL;
6382
6383 if (code[*bgn] == BC_PARSE_STREND)
6384 (*bgn) += 1;
6385 else
6386 else_name = bc_program_name(code, bgn);
6387
6388 exec = r->d.n.len != 0;
6389
6390 if (exec)
6391 name = then_name;
6392 else if (else_name != NULL) {
6393 exec = true;
6394 name = else_name;
6395 }
6396
6397 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006398 BcVec *v;
6399 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006400 n = bc_vec_top(v);
6401 }
6402
6403 free(then_name);
6404 free(else_name);
6405
6406 if (!exec) goto exit;
6407 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006408 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006409 goto exit;
6410 }
6411
6412 sidx = n->rdx;
6413 }
6414 else {
6415
6416 if (r->t == BC_RESULT_STR)
6417 sidx = r->d.id.idx;
6418 else if (r->t == BC_RESULT_VAR) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006419 s = bc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006420 if (s || !BC_PROG_STR(n)) goto exit;
6421 sidx = n->rdx;
6422 }
6423 else
6424 goto exit;
6425 }
6426
6427 fidx = sidx + BC_PROG_REQ_FUNCS;
6428
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006429 str = bc_vec_item(&G.prog.strs, sidx);
6430 f = bc_vec_item(&G.prog.fns, fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006431
6432 if (f->code.len == 0) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006433 common_parse_init(&prs, fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006434 s = bc_parse_text(&prs, *str);
6435 if (s) goto err;
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01006436 s = common_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006437 if (s) goto err;
6438
6439 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006440 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006441 goto err;
6442 }
6443
6444 bc_parse_free(&prs);
6445 }
6446
6447 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006448 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006449 ip.func = fidx;
6450
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006451 bc_vec_pop(&G.prog.results);
6452 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006453
6454 return BC_STATUS_SUCCESS;
6455
6456err:
6457 bc_parse_free(&prs);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006458 f = bc_vec_item(&G.prog.fns, fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006459 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006460exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006461 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006462 return s;
6463}
6464#endif // ENABLE_DC
6465
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006466static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006467{
Gavin Howard01055ba2018-11-03 11:00:21 -06006468 BcResult res;
6469 unsigned long val;
6470
6471 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6472 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006473 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006474 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006475 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006476 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006477 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006478
6479 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006480 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006481 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006482}
6483
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006484static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006485{
Gavin Howard01055ba2018-11-03 11:00:21 -06006486 BcId entry, *entry_ptr;
6487 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006488 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006489
6490 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006491 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006492
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006493 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6494 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006495
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006496 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006497 *idx = entry_ptr->idx;
6498
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006499 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006500
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006501 BcFunc *func = bc_vec_item(&G.prog.fns, entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006502
6503 // We need to reset these, so the function can be repopulated.
6504 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006505 bc_vec_pop_all(&func->autos);
6506 bc_vec_pop_all(&func->code);
6507 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006508 }
6509 else {
6510 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006511 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006512 }
6513}
6514
Denys Vlasenkod38af482018-12-04 19:11:02 +01006515// Called when parsing or execution detects a failure,
6516// resets execution structures.
6517static void bc_program_reset(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006518{
6519 BcFunc *f;
6520 BcInstPtr *ip;
6521
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006522 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006523 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006524
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006525 f = bc_vec_item(&G.prog.fns, 0);
6526 ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006527 ip->idx = f->code.len;
6528
Denys Vlasenkod38af482018-12-04 19:11:02 +01006529 // If !tty, no need to check for ^C: we don't have ^C handler,
6530 // we would be killed by a signal and won't reach this place
Gavin Howard01055ba2018-11-03 11:00:21 -06006531}
6532
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006533static BcStatus bc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006534{
6535 BcStatus s = BC_STATUS_SUCCESS;
6536 size_t idx;
6537 BcResult r, *ptr;
6538 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006539 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
6540 BcFunc *func = bc_vec_item(&G.prog.fns, ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006541 char *code = func->code.v;
6542 bool cond = false;
6543
6544 while (!s && ip->idx < func->code.len) {
6545
6546 char inst = code[(ip->idx)++];
6547
6548 switch (inst) {
6549
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006550#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006551 case BC_INST_JUMP_ZERO:
6552 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006553 s = bc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006554 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006555 cond = !bc_num_cmp(num, &G.prog.zero);
6556 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006557 }
6558 // Fallthrough.
6559 case BC_INST_JUMP:
6560 {
6561 size_t *addr;
6562 idx = bc_program_index(code, &ip->idx);
6563 addr = bc_vec_item(&func->labels, idx);
6564 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6565 break;
6566 }
6567
6568 case BC_INST_CALL:
6569 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006570 s = bc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006571 break;
6572 }
6573
6574 case BC_INST_INC_PRE:
6575 case BC_INST_DEC_PRE:
6576 case BC_INST_INC_POST:
6577 case BC_INST_DEC_POST:
6578 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006579 s = bc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006580 break;
6581 }
6582
6583 case BC_INST_HALT:
6584 {
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006585 quit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006586 break;
6587 }
6588
6589 case BC_INST_RET:
6590 case BC_INST_RET0:
6591 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006592 s = bc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006593 break;
6594 }
6595
6596 case BC_INST_BOOL_OR:
6597 case BC_INST_BOOL_AND:
6598#endif // ENABLE_BC
6599 case BC_INST_REL_EQ:
6600 case BC_INST_REL_LE:
6601 case BC_INST_REL_GE:
6602 case BC_INST_REL_NE:
6603 case BC_INST_REL_LT:
6604 case BC_INST_REL_GT:
6605 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006606 s = bc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006607 break;
6608 }
6609
6610 case BC_INST_READ:
6611 {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006612 s = bc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006613 break;
6614 }
6615
6616 case BC_INST_VAR:
6617 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006618 s = bc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006619 break;
6620 }
6621
6622 case BC_INST_ARRAY_ELEM:
6623 case BC_INST_ARRAY:
6624 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006625 s = bc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006626 break;
6627 }
6628
6629 case BC_INST_LAST:
6630 {
6631 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006632 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006633 break;
6634 }
6635
6636 case BC_INST_IBASE:
6637 case BC_INST_SCALE:
6638 case BC_INST_OBASE:
6639 {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006640 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006641 break;
6642 }
6643
6644 case BC_INST_SCALE_FUNC:
6645 case BC_INST_LENGTH:
6646 case BC_INST_SQRT:
6647 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006648 s = bc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006649 break;
6650 }
6651
6652 case BC_INST_NUM:
6653 {
6654 r.t = BC_RESULT_CONSTANT;
6655 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006656 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006657 break;
6658 }
6659
6660 case BC_INST_POP:
6661 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006662 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006663 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006664 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006665 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006666 break;
6667 }
6668
6669 case BC_INST_POP_EXEC:
6670 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006671 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006672 break;
6673 }
6674
6675 case BC_INST_PRINT:
6676 case BC_INST_PRINT_POP:
6677 case BC_INST_PRINT_STR:
6678 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006679 s = bc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006680 break;
6681 }
6682
6683 case BC_INST_STR:
6684 {
6685 r.t = BC_RESULT_STR;
6686 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006687 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006688 break;
6689 }
6690
6691 case BC_INST_POWER:
6692 case BC_INST_MULTIPLY:
6693 case BC_INST_DIVIDE:
6694 case BC_INST_MODULUS:
6695 case BC_INST_PLUS:
6696 case BC_INST_MINUS:
6697 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006698 s = bc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006699 break;
6700 }
6701
6702 case BC_INST_BOOL_NOT:
6703 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006704 s = bc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006705 if (s) return s;
6706
6707 bc_num_init(&r.d.n, BC_NUM_DEF_SIZE);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006708 (!bc_num_cmp(num, &G.prog.zero) ? bc_num_one : bc_num_zero)(&r.d.n);
6709 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006710
6711 break;
6712 }
6713
6714 case BC_INST_NEG:
6715 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006716 s = bc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006717 break;
6718 }
6719
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006720#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006721 case BC_INST_ASSIGN_POWER:
6722 case BC_INST_ASSIGN_MULTIPLY:
6723 case BC_INST_ASSIGN_DIVIDE:
6724 case BC_INST_ASSIGN_MODULUS:
6725 case BC_INST_ASSIGN_PLUS:
6726 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006727#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006728 case BC_INST_ASSIGN:
6729 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006730 s = bc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006731 break;
6732 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006733#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006734 case BC_INST_MODEXP:
6735 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006736 s = bc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006737 break;
6738 }
6739
6740 case BC_INST_DIVMOD:
6741 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006742 s = bc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006743 break;
6744 }
6745
6746 case BC_INST_EXECUTE:
6747 case BC_INST_EXEC_COND:
6748 {
6749 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006750 s = bc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006751 break;
6752 }
6753
6754 case BC_INST_PRINT_STACK:
6755 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006756 for (idx = 0; !s && idx < G.prog.results.len; ++idx)
6757 s = bc_program_print(BC_INST_PRINT, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006758 break;
6759 }
6760
6761 case BC_INST_CLEAR_STACK:
6762 {
Denys Vlasenko7d628012018-12-04 21:46:47 +01006763 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006764 break;
6765 }
6766
6767 case BC_INST_STACK_LEN:
6768 {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006769 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006770 break;
6771 }
6772
6773 case BC_INST_DUPLICATE:
6774 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006775 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006776 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006777 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006778 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006779 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006780 break;
6781 }
6782
6783 case BC_INST_SWAP:
6784 {
6785 BcResult *ptr2;
6786
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006787 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006788 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006789
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006790 ptr = bc_vec_item_rev(&G.prog.results, 0);
6791 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006792 memcpy(&r, ptr, sizeof(BcResult));
6793 memcpy(ptr, ptr2, sizeof(BcResult));
6794 memcpy(ptr2, &r, sizeof(BcResult));
6795
6796 break;
6797 }
6798
6799 case BC_INST_ASCIIFY:
6800 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006801 s = bc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006802 break;
6803 }
6804
6805 case BC_INST_PRINT_STREAM:
6806 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006807 s = bc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006808 break;
6809 }
6810
6811 case BC_INST_LOAD:
6812 case BC_INST_PUSH_VAR:
6813 {
6814 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006815 s = bc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006816 break;
6817 }
6818
6819 case BC_INST_PUSH_TO_VAR:
6820 {
6821 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006822 s = bc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006823 free(name);
6824 break;
6825 }
6826
6827 case BC_INST_QUIT:
6828 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006829 if (G.prog.stack.len <= 2)
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006830 quit();
6831 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006832 break;
6833 }
6834
6835 case BC_INST_NQUIT:
6836 {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006837 s = bc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006838 break;
6839 }
6840#endif // ENABLE_DC
6841 }
6842
Denys Vlasenkod38af482018-12-04 19:11:02 +01006843 if (s || G_interrupt) {
6844 bc_program_reset();
6845 break;
6846 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006847
6848 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006849 ip = bc_vec_top(&G.prog.stack);
6850 func = bc_vec_item(&G.prog.fns, ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006851 code = func->code.v;
6852 }
6853
6854 return s;
6855}
6856
Denys Vlasenko00d77792018-11-30 23:13:42 +01006857static void bc_vm_info(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006858{
Denys Vlasenko00d77792018-11-30 23:13:42 +01006859 printf("%s "BB_VER"\n"
6860 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01006861 "Report bugs at: https://github.com/gavinhoward/bc\n"
Denys Vlasenko00d77792018-11-30 23:13:42 +01006862 "This is free software with ABSOLUTELY NO WARRANTY\n"
6863 , applet_name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006864}
6865
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006866#if ENABLE_BC
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006867static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006868{
Denys Vlasenko1f67e932018-12-03 00:08:59 +01006869 static const char* const bc_args_env_name = "BC_ENV_ARGS";
6870
Gavin Howard01055ba2018-11-03 11:00:21 -06006871 BcVec v;
6872 char *env_args = getenv(bc_args_env_name), *buf;
6873
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01006874 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06006875
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006876 G.env_args = xstrdup(env_args);
6877 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06006878
6879 bc_vec_init(&v, sizeof(char *), NULL);
6880 bc_vec_push(&v, &bc_args_env_name);
6881
6882 while (*buf != 0) {
6883 if (!isspace(*buf)) {
6884 bc_vec_push(&v, &buf);
6885 while (*buf != 0 && !isspace(*buf)) ++buf;
6886 if (*buf != 0) (*(buf++)) = '\0';
6887 }
6888 else
6889 ++buf;
6890 }
6891
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006892 bc_args((int) v.len, (char **) v.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006893
6894 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006895}
6896#endif // ENABLE_BC
6897
6898static size_t bc_vm_envLen(const char *var)
6899{
6900 char *lenv = getenv(var);
6901 size_t i, len = BC_NUM_PRINT_WIDTH;
6902 int num;
6903
6904 if (!lenv) return len;
6905
6906 len = strlen(lenv);
6907
6908 for (num = 1, i = 0; num && i < len; ++i) num = isdigit(lenv[i]);
6909 if (num) {
6910 len = (size_t) atoi(lenv) - 1;
6911 if (len < 2 || len >= INT32_MAX) len = BC_NUM_PRINT_WIDTH;
6912 }
6913 else
6914 len = BC_NUM_PRINT_WIDTH;
6915
6916 return len;
6917}
6918
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006919static BcStatus bc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006920{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006921 BcStatus s = bc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06006922
Gavin Howard01055ba2018-11-03 11:00:21 -06006923 if (s) return s;
6924
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006925 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006926 s = G.prs.parse(&G.prs);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006927 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006928 }
6929
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006930 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006931 s = bc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01006932 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01006933 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01006934 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06006935 }
6936
6937 return s;
6938}
6939
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006940static BcStatus bc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06006941{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006942 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06006943 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006944 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006945 BcFunc *main_func;
6946 BcInstPtr *ip;
6947
Denys Vlasenkodf515392018-12-02 19:27:48 +01006948 data = bc_read_file(file);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006949 if (!data) return bc_error_fmt("file '%s' is not text", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06006950
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006951 sv_file = G.prog.file;
6952 G.prog.file = file;
6953 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006954 s = bc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06006955 if (s) goto err;
6956
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006957 main_func = bc_vec_item(&G.prog.fns, BC_PROG_MAIN);
6958 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006959
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006960 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006961 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06006962
6963err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006964 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06006965 free(data);
6966 return s;
6967}
6968
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006969static BcStatus bc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006970{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01006971 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006972 BcVec buf, buffer;
Gavin Howard01055ba2018-11-03 11:00:21 -06006973 size_t len, i, str = 0;
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01006974 bool comment = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06006975
Denys Vlasenko0409ad32018-12-05 16:39:22 +01006976 G.prog.file = NULL;
6977 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006978
Denys Vlasenko7d628012018-12-04 21:46:47 +01006979 bc_char_vec_init(&buffer);
6980 bc_char_vec_init(&buf);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01006981 bc_vec_pushZeroByte(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06006982
6983 // This loop is complex because the vm tries not to send any lines that end
6984 // with a backslash to the parser. The reason for that is because the parser
6985 // treats a backslash+newline combo as whitespace, per the bc spec. In that
6986 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko657d6bb2018-12-05 20:25:03 +01006987 s = BC_STATUS_SUCCESS;
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01006988 while (!G.eof && (s = bc_read_line(&buf, ">>> ")) == BC_STATUS_SUCCESS) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006989
6990 char *string = buf.v;
6991
6992 len = buf.len - 1;
6993
6994 if (len == 1) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006995 if (str && buf.v[0] == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06006996 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006997 else if (buf.v[0] == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06006998 str += 1;
6999 }
7000 else if (len > 1 || comment) {
7001
7002 for (i = 0; i < len; ++i) {
7003
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007004 bool notend = len > i + 1;
7005 char c = string[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06007006
7007 if (i - 1 > len || string[i - 1] != '\\') {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007008 if (G.sbgn == G.send)
7009 str ^= c == G.sbgn;
7010 else if (c == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007011 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007012 else if (c == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007013 str += 1;
7014 }
7015
7016 if (c == '/' && notend && !comment && string[i + 1] == '*') {
7017 comment = true;
7018 break;
7019 }
7020 else if (c == '*' && notend && comment && string[i + 1] == '/')
7021 comment = false;
7022 }
7023
7024 if (str || comment || string[len - 2] == '\\') {
7025 bc_vec_concat(&buffer, buf.v);
7026 continue;
7027 }
7028 }
7029
7030 bc_vec_concat(&buffer, buf.v);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007031 s = bc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007032 if (s) {
7033 fflush_and_check();
7034 fputs("ready for more input\n", stderr);
7035 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007036
Denys Vlasenko7d628012018-12-04 21:46:47 +01007037 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007038 }
7039
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007040 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007041 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007042 }
7043 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007044 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007045 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007046
Gavin Howard01055ba2018-11-03 11:00:21 -06007047 bc_vec_free(&buf);
7048 bc_vec_free(&buffer);
7049 return s;
7050}
7051
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007052#if ENABLE_BC
7053static const char bc_lib[] = {
7054 "scale=20"
7055"\n" "define e(x){"
7056"\n" "auto b,s,n,r,d,i,p,f,v"
7057"\n" "b=ibase"
7058"\n" "ibase=A"
7059"\n" "if(x<0){"
7060"\n" "n=1"
7061"\n" "x=-x"
7062"\n" "}"
7063"\n" "s=scale"
7064"\n" "r=6+s+0.44*x"
7065"\n" "scale=scale(x)+1"
7066"\n" "while(x>1){"
7067"\n" "d+=1"
7068"\n" "x/=2"
7069"\n" "scale+=1"
7070"\n" "}"
7071"\n" "scale=r"
7072"\n" "r=x+1"
7073"\n" "p=x"
7074"\n" "f=v=1"
7075"\n" "for(i=2;v!=0;++i){"
7076"\n" "p*=x"
7077"\n" "f*=i"
7078"\n" "v=p/f"
7079"\n" "r+=v"
7080"\n" "}"
7081"\n" "while((d--)!=0)r*=r"
7082"\n" "scale=s"
7083"\n" "ibase=b"
7084"\n" "if(n!=0)return(1/r)"
7085"\n" "return(r/1)"
7086"\n" "}"
7087"\n" "define l(x){"
7088"\n" "auto b,s,r,p,a,q,i,v"
7089"\n" "b=ibase"
7090"\n" "ibase=A"
7091"\n" "if(x<=0){"
7092"\n" "r=(1-10^scale)/1"
7093"\n" "ibase=b"
7094"\n" "return(r)"
7095"\n" "}"
7096"\n" "s=scale"
7097"\n" "scale+=6"
7098"\n" "p=2"
7099"\n" "while(x>=2){"
7100"\n" "p*=2"
7101"\n" "x=sqrt(x)"
7102"\n" "}"
7103"\n" "while(x<=0.5){"
7104"\n" "p*=2"
7105"\n" "x=sqrt(x)"
7106"\n" "}"
7107"\n" "r=a=(x-1)/(x+1)"
7108"\n" "q=a*a"
7109"\n" "v=1"
7110"\n" "for(i=3;v!=0;i+=2){"
7111"\n" "a*=q"
7112"\n" "v=a/i"
7113"\n" "r+=v"
7114"\n" "}"
7115"\n" "r*=p"
7116"\n" "scale=s"
7117"\n" "ibase=b"
7118"\n" "return(r/1)"
7119"\n" "}"
7120"\n" "define s(x){"
7121"\n" "auto b,s,r,n,a,q,i"
7122"\n" "b=ibase"
7123"\n" "ibase=A"
7124"\n" "s=scale"
7125"\n" "scale=1.1*s+2"
7126"\n" "a=a(1)"
7127"\n" "if(x<0){"
7128"\n" "n=1"
7129"\n" "x=-x"
7130"\n" "}"
7131"\n" "scale=0"
7132"\n" "q=(x/a+2)/4"
7133"\n" "x=x-4*q*a"
7134"\n" "if(q%2!=0)x=-x"
7135"\n" "scale=s+2"
7136"\n" "r=a=x"
7137"\n" "q=-x*x"
7138"\n" "for(i=3;a!=0;i+=2){"
7139"\n" "a*=q/(i*(i-1))"
7140"\n" "r+=a"
7141"\n" "}"
7142"\n" "scale=s"
7143"\n" "ibase=b"
7144"\n" "if(n!=0)return(-r/1)"
7145"\n" "return(r/1)"
7146"\n" "}"
7147"\n" "define c(x){"
7148"\n" "auto b,s"
7149"\n" "b=ibase"
7150"\n" "ibase=A"
7151"\n" "s=scale"
7152"\n" "scale*=1.2"
7153"\n" "x=s(2*a(1)+x)"
7154"\n" "scale=s"
7155"\n" "ibase=b"
7156"\n" "return(x/1)"
7157"\n" "}"
7158"\n" "define a(x){"
7159"\n" "auto b,s,r,n,a,m,t,f,i,u"
7160"\n" "b=ibase"
7161"\n" "ibase=A"
7162"\n" "n=1"
7163"\n" "if(x<0){"
7164"\n" "n=-1"
7165"\n" "x=-x"
7166"\n" "}"
7167"\n" "if(x==1){"
7168"\n" "if(scale<65){"
7169"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7170"\n" "}"
7171"\n" "}"
7172"\n" "if(x==.2){"
7173"\n" "if(scale<65){"
7174"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7175"\n" "}"
7176"\n" "}"
7177"\n" "s=scale"
7178"\n" "if(x>.2){"
7179"\n" "scale+=5"
7180"\n" "a=a(.2)"
7181"\n" "}"
7182"\n" "scale=s+3"
7183"\n" "while(x>.2){"
7184"\n" "m+=1"
7185"\n" "x=(x-.2)/(1+.2*x)"
7186"\n" "}"
7187"\n" "r=u=x"
7188"\n" "f=-x*x"
7189"\n" "t=1"
7190"\n" "for(i=3;t!=0;i+=2){"
7191"\n" "u*=f"
7192"\n" "t=u/i"
7193"\n" "r+=t"
7194"\n" "}"
7195"\n" "scale=s"
7196"\n" "ibase=b"
7197"\n" "return((m*a+r)/n)"
7198"\n" "}"
7199"\n" "define j(n,x){"
7200"\n" "auto b,s,o,a,i,v,f"
7201"\n" "b=ibase"
7202"\n" "ibase=A"
7203"\n" "s=scale"
7204"\n" "scale=0"
7205"\n" "n/=1"
7206"\n" "if(n<0){"
7207"\n" "n=-n"
7208"\n" "if(n%2==1)o=1"
7209"\n" "}"
7210"\n" "a=1"
7211"\n" "for(i=2;i<=n;++i)a*=i"
7212"\n" "scale=1.5*s"
7213"\n" "a=(x^n)/2^n/a"
7214"\n" "r=v=1"
7215"\n" "f=-x*x/4"
7216"\n" "scale=scale+length(a)-scale(a)"
7217"\n" "for(i=1;v!=0;++i){"
7218"\n" "v=v*f/i/(n+i)"
7219"\n" "r+=v"
7220"\n" "}"
7221"\n" "scale=s"
7222"\n" "ibase=b"
7223"\n" "if(o!=0)a=-a"
7224"\n" "return(a*r/1)"
7225"\n" "}"
7226};
7227#endif // ENABLE_BC
7228
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007229static BcStatus bc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007230{
7231 BcStatus s = BC_STATUS_SUCCESS;
7232 size_t i;
7233
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007234#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007235 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007236
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007237 // We know that internal library is not buggy,
7238 // thus error checking is normally disabled.
7239# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007240 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007241 s = bc_parse_text(&G.prs, bc_lib);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007242 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007243
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007244 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007245 s = G.prs.parse(&G.prs);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007246 if (DEBUG_LIB && s) return s;
7247 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007248 s = bc_program_exec();
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007249 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007250 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007251#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007252
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007253 for (i = 0; !s && i < G.files.len; ++i)
7254 s = bc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007255 if (s) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007256 fflush_and_check();
7257 fputs("ready for more input\n", stderr);
7258 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007259
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007260 if (IS_BC || !G.files.len)
7261 s = bc_vm_stdin();
7262 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
7263 s = bc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007264
Denys Vlasenko00d77792018-11-30 23:13:42 +01007265 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007266}
7267
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007268#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007269static void bc_program_free()
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007270{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007271 bc_num_free(&G.prog.ib);
7272 bc_num_free(&G.prog.ob);
7273 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007274# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007275 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007276# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007277 bc_vec_free(&G.prog.fns);
7278 bc_vec_free(&G.prog.fn_map);
7279 bc_vec_free(&G.prog.vars);
7280 bc_vec_free(&G.prog.var_map);
7281 bc_vec_free(&G.prog.arrs);
7282 bc_vec_free(&G.prog.arr_map);
7283 bc_vec_free(&G.prog.strs);
7284 bc_vec_free(&G.prog.consts);
7285 bc_vec_free(&G.prog.results);
7286 bc_vec_free(&G.prog.stack);
7287 bc_num_free(&G.prog.last);
7288 bc_num_free(&G.prog.zero);
7289 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007290}
7291
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007292static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007293{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007294 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007295 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007296 bc_parse_free(&G.prs);
7297 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007298}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007299#endif
7300
7301static void bc_program_init(size_t line_len)
7302{
7303 size_t idx;
7304 BcInstPtr ip;
7305
7306 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7307 memset(&ip, 0, sizeof(BcInstPtr));
7308
7309 /* G.prog.nchars = G.prog.scale = 0; - already is */
7310 G.prog.len = line_len;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007311
7312 bc_num_init(&G.prog.ib, BC_NUM_DEF_SIZE);
7313 bc_num_ten(&G.prog.ib);
7314 G.prog.ib_t = 10;
7315
7316 bc_num_init(&G.prog.ob, BC_NUM_DEF_SIZE);
7317 bc_num_ten(&G.prog.ob);
7318 G.prog.ob_t = 10;
7319
7320 bc_num_init(&G.prog.hexb, BC_NUM_DEF_SIZE);
7321 bc_num_ten(&G.prog.hexb);
7322 G.prog.hexb.num[0] = 6;
7323
7324#if ENABLE_DC
7325 bc_num_init(&G.prog.strmb, BC_NUM_DEF_SIZE);
7326 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7327#endif
7328
7329 bc_num_init(&G.prog.last, BC_NUM_DEF_SIZE);
7330 bc_num_zero(&G.prog.last);
7331
7332 bc_num_init(&G.prog.zero, BC_NUM_DEF_SIZE);
7333 bc_num_zero(&G.prog.zero);
7334
7335 bc_num_init(&G.prog.one, BC_NUM_DEF_SIZE);
7336 bc_num_one(&G.prog.one);
7337
7338 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007339 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007340
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007341 bc_program_addFunc(xstrdup("(main)"), &idx);
7342 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007343
7344 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007345 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007346
7347 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007348 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007349
7350 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7351 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7352 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7353 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7354 bc_vec_push(&G.prog.stack, &ip);
7355}
Gavin Howard01055ba2018-11-03 11:00:21 -06007356
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007357static void bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007358{
Gavin Howard01055ba2018-11-03 11:00:21 -06007359 size_t len = bc_vm_envLen(env_len);
Gavin Howard01055ba2018-11-03 11:00:21 -06007360
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007361 bc_vec_init(&G.files, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06007362
Denys Vlasenkoab3c5682018-12-02 16:32:36 +01007363 if (IS_BC) {
Denys Vlasenkoab3c5682018-12-02 16:32:36 +01007364 bc_vm_envArgs();
7365 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007366
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007367 bc_program_init(len);
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007368 if (IS_BC) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007369 bc_parse_init(&G.prs, BC_PROG_MAIN);
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007370 } else {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007371 dc_parse_init(&G.prs, BC_PROG_MAIN);
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01007372 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007373}
7374
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007375static BcStatus bc_vm_run(int argc, char *argv[],
Gavin Howard01055ba2018-11-03 11:00:21 -06007376 const char *env_len)
7377{
7378 BcStatus st;
Gavin Howard01055ba2018-11-03 11:00:21 -06007379
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007380 bc_vm_init(env_len);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007381 bc_args(argc, argv);
Gavin Howard01055ba2018-11-03 11:00:21 -06007382
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007383 G.ttyin = isatty(0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007384
Denys Vlasenkod38af482018-12-04 19:11:02 +01007385 if (G.ttyin) {
7386#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko17c54722018-12-04 21:21:32 +01007387 // With SA_RESTART, most system calls will restart
7388 // (IOW: they won't fail with EINTR).
7389 // In particular, this means ^C won't cause
7390 // stdout to get into "error state" if SIGINT hits
7391 // within write() syscall.
7392 // The downside is that ^C while line input is taken
7393 // will only be handled after [Enter] since read()
7394 // from stdin is not interrupted by ^C either,
7395 // it restarts, thus fgetc() does not return on ^C.
7396 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7397
7398 // Without SA_RESTART, this exhibits a bug:
7399 // "while (1) print 1" and try ^C-ing it.
7400 // Intermittently, instead of returning to input line,
7401 // you'll get "output error: Interrupted system call"
7402 // and exit.
7403 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007404#endif
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007405 if (!(option_mask32 & BC_FLAG_Q))
Denys Vlasenkod38af482018-12-04 19:11:02 +01007406 bc_vm_info();
7407 }
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007408 st = bc_vm_exec();
Gavin Howard01055ba2018-11-03 11:00:21 -06007409
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007410#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007411 bc_vm_free();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007412#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007413 return st;
7414}
7415
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007416#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007417int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
7418int bc_main(int argc, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007419{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007420 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007421 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007422
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007423 return bc_vm_run(argc, argv, "BC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007424}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007425#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007426
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007427#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007428int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
7429int dc_main(int argc, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007430{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007431 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007432 G.sbgn = '[';
7433 G.send = ']';
Gavin Howard01055ba2018-11-03 11:00:21 -06007434
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007435 return bc_vm_run(argc, argv, "DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007436}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007437#endif