blob: f942d42acff23fe6897cefb7d748c4a8298584c2 [file] [log] [blame]
Gavin Howard01055ba2018-11-03 11:00:21 -06001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 * Copyright (c) 2018 Gavin D. Howard and contributors.
Gavin Howard01055ba2018-11-03 11:00:21 -06005 */
6//config:config BC
7//config: bool "bc (45 kb; 49 kb when combined with dc)"
8//config: default y
9//config: help
10//config: bc is a command-line, arbitrary-precision calculator with a
11//config: Turing-complete language. See the GNU bc manual
12//config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec
13//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
14//config: for details.
15//config:
16//config: This bc has four differences to the GNU bc:
17//config:
18//config: 1) The period (.) can also be used as a shortcut for "last", as in
19//config: the BSD bc.
20//config: 2) Arrays are copied before being passed as arguments to
21//config: functions. This behavior is required by the bc spec.
22//config: 3) Arrays can be passed to the builtin "length" function to get
23//config: the number of elements currently in the array. The following
24//config: example prints "1":
25//config:
26//config: a[0] = 0
27//config: length(a[])
28//config:
29//config: 4) The precedence of the boolean "not" operator (!) is equal to
30//config: that of the unary minus (-), or negation, operator. This still
31//config: allows POSIX-compliant scripts to work while somewhat
32//config: preserving expected behavior (versus C) and making parsing
33//config: easier.
34//config:
35//config: Options:
36//config:
37//config: -i --interactive force interactive mode
38//config: -l --mathlib use predefined math routines:
39//config:
40//config: s(expr) = sine of expr in radians
41//config: c(expr) = cosine of expr in radians
42//config: a(expr) = arctangent of expr, returning
43//config: radians
44//config: l(expr) = natural log of expr
45//config: e(expr) = raises e to the power of expr
46//config: j(n, x) = Bessel function of integer order
47//config: n of x
48//config:
49//config: -q --quiet don't print version and copyright.
50//config: -s --standard error if any non-POSIX extensions are used.
51//config: -w --warn warn if any non-POSIX extensions are used.
52//config: -v --version print version and copyright and exit.
53//config:
54//config: Long options are only available if FEATURE_BC_LONG_OPTIONS is
55//config: enabled.
56//config:
57//config:config DC
58//config: bool "dc (38 kb; 49 kb when combined with bc)"
59//config: default y
60//config: help
61//config: dc is a reverse-polish notation command-line calculator which
62//config: supports unlimited precision arithmetic. See the FreeBSD man page
63//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
64//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html)
65//config: for details.
66//config:
67//config: This dc has a few differences from the two above:
68//config:
69//config: 1) When printing a byte stream (command "P"), this bc follows what
70//config: the FreeBSD dc does.
71//config: 2) This dc implements the GNU extensions for divmod ("~") and
72//config: modular exponentiation ("|").
73//config: 3) This dc implements all FreeBSD extensions, except for "J" and
74//config: "M".
75//config: 4) Like the FreeBSD dc, this dc supports extended registers.
76//config: However, they are implemented differently. When it encounters
77//config: whitespace where a register should be, it skips the whitespace.
78//config: If the character following is not a lowercase letter, an error
79//config: is issued. Otherwise, the register name is parsed by the
80//config: following regex:
81//config:
82//config: [a-z][a-z0-9_]*
83//config:
84//config: This generally means that register names will be surrounded by
85//config: whitespace.
86//config:
87//config: Examples:
88//config:
89//config: l idx s temp L index S temp2 < do_thing
90//config:
91//config: Also note that, like the FreeBSD dc, extended registers are not
92//config: allowed unless the "-x" option is given.
93//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010094//config:config FEATURE_DC_SMALL
95//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
96//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010097//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010098//config:
99//config:config FEATURE_DC_LIBM
100//config: bool "Enable power and exp functions (requires libm)"
101//config: default y
102//config: depends on FEATURE_DC_SMALL
103//config: help
104//config: Enable power and exp functions.
105//config: NOTE: This will require libm to be present for linking.
106//config:
Gavin Howard01055ba2018-11-03 11:00:21 -0600107//config:config FEATURE_BC_SIGNALS
108//config: bool "Enable bc/dc signal handling"
109//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100110//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600111//config: help
112//config: Enable signal handling for bc and dc.
113//config:
114//config:config FEATURE_BC_LONG_OPTIONS
115//config: bool "Enable bc/dc long options"
116//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100117//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600118//config: help
119//config: Enable long options for bc and dc.
120
121//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
122//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
123
124//kbuild:lib-$(CONFIG_BC) += bc.o
125//kbuild:lib-$(CONFIG_DC) += bc.o
126
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100127//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600128//usage:#define bc_trivial_usage
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100129//usage: "[-sqliw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600130//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100131//usage:#define bc_full_usage "\n"
132//usage: "\nArbitrary precision calculator"
133//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100134///////: "\n -i Interactive" - has no effect for now
135//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100136//usage: "\n -l Load standard math library"
137//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100138//usage: "\n -w Warn if extensions are used"
139///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100140//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100141//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600142//usage:
143//usage:#define bc_example_usage
144//usage: "3 + 4.129\n"
145//usage: "1903 - 2893\n"
146//usage: "-129 * 213.28935\n"
147//usage: "12 / -1932\n"
148//usage: "12 % 12\n"
149//usage: "34 ^ 189\n"
150//usage: "scale = 13\n"
151//usage: "ibase = 2\n"
152//usage: "obase = A\n"
153//usage:
154//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100155//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600156//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100157//usage:#define dc_full_usage "\n"
158//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100159//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "\np - print top of the stack (without popping)"
161//usage: "\nf - print entire stack"
162//usage: "\nk - pop the value and set the precision"
163//usage: "\ni - pop the value and set input radix"
164//usage: "\no - pop the value and set output radix"
165//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600166//usage:
167//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100168//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600169//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100170//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600171//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100172//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600173//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100174//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600175//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100176//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600177//usage: "64\n"
178
179#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100180#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600181
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100182#if ENABLE_FEATURE_DC_SMALL
183# include "dc.c"
184#else
185
Gavin Howard01055ba2018-11-03 11:00:21 -0600186typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100187 BC_STATUS_SUCCESS = 0,
188 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100189 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Denys Vlasenkof522dd92018-12-07 16:35:43 +0100190 BC_STATUS_EOF = 3, // bc_vm_stdin() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600191} BcStatus;
192
Gavin Howard01055ba2018-11-03 11:00:21 -0600193#define BC_VEC_INVALID_IDX ((size_t) -1)
194#define BC_VEC_START_CAP (1 << 5)
195
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100196typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600197
198typedef struct BcVec {
199 char *v;
200 size_t len;
201 size_t cap;
202 size_t size;
203 BcVecFree dtor;
204} BcVec;
205
Gavin Howard01055ba2018-11-03 11:00:21 -0600206typedef signed char BcDig;
207
208typedef struct BcNum {
209 BcDig *restrict num;
210 size_t rdx;
211 size_t len;
212 size_t cap;
213 bool neg;
214} BcNum;
215
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100216#define BC_NUM_MIN_BASE ((unsigned long) 2)
217#define BC_NUM_MAX_IBASE ((unsigned long) 16)
218// larger value might speed up BIGNUM calculations a bit:
219#define BC_NUM_DEF_SIZE (16)
220#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600221
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100222#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600223
Gavin Howard01055ba2018-11-03 11:00:21 -0600224typedef enum BcInst {
225
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100226#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600227 BC_INST_INC_PRE,
228 BC_INST_DEC_PRE,
229 BC_INST_INC_POST,
230 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100231#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600232
233 BC_INST_NEG,
234
235 BC_INST_POWER,
236 BC_INST_MULTIPLY,
237 BC_INST_DIVIDE,
238 BC_INST_MODULUS,
239 BC_INST_PLUS,
240 BC_INST_MINUS,
241
242 BC_INST_REL_EQ,
243 BC_INST_REL_LE,
244 BC_INST_REL_GE,
245 BC_INST_REL_NE,
246 BC_INST_REL_LT,
247 BC_INST_REL_GT,
248
249 BC_INST_BOOL_NOT,
250 BC_INST_BOOL_OR,
251 BC_INST_BOOL_AND,
252
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100253#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600254 BC_INST_ASSIGN_POWER,
255 BC_INST_ASSIGN_MULTIPLY,
256 BC_INST_ASSIGN_DIVIDE,
257 BC_INST_ASSIGN_MODULUS,
258 BC_INST_ASSIGN_PLUS,
259 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100260#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600261 BC_INST_ASSIGN,
262
263 BC_INST_NUM,
264 BC_INST_VAR,
265 BC_INST_ARRAY_ELEM,
266 BC_INST_ARRAY,
267
268 BC_INST_SCALE_FUNC,
269 BC_INST_IBASE,
270 BC_INST_SCALE,
271 BC_INST_LAST,
272 BC_INST_LENGTH,
273 BC_INST_READ,
274 BC_INST_OBASE,
275 BC_INST_SQRT,
276
277 BC_INST_PRINT,
278 BC_INST_PRINT_POP,
279 BC_INST_STR,
280 BC_INST_PRINT_STR,
281
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100282#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600283 BC_INST_JUMP,
284 BC_INST_JUMP_ZERO,
285
286 BC_INST_CALL,
287
288 BC_INST_RET,
289 BC_INST_RET0,
290
291 BC_INST_HALT,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100292#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600293
294 BC_INST_POP,
295 BC_INST_POP_EXEC,
296
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100297#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600298 BC_INST_MODEXP,
299 BC_INST_DIVMOD,
300
301 BC_INST_EXECUTE,
302 BC_INST_EXEC_COND,
303
304 BC_INST_ASCIIFY,
305 BC_INST_PRINT_STREAM,
306
307 BC_INST_PRINT_STACK,
308 BC_INST_CLEAR_STACK,
309 BC_INST_STACK_LEN,
310 BC_INST_DUPLICATE,
311 BC_INST_SWAP,
312
313 BC_INST_LOAD,
314 BC_INST_PUSH_VAR,
315 BC_INST_PUSH_TO_VAR,
316
317 BC_INST_QUIT,
318 BC_INST_NQUIT,
319
320 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100321#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600322
323} BcInst;
324
325typedef struct BcId {
326 char *name;
327 size_t idx;
328} BcId;
329
330typedef struct BcFunc {
331 BcVec code;
332 BcVec labels;
333 size_t nparams;
334 BcVec autos;
335} BcFunc;
336
337typedef enum BcResultType {
338
339 BC_RESULT_TEMP,
340
341 BC_RESULT_VAR,
342 BC_RESULT_ARRAY_ELEM,
343 BC_RESULT_ARRAY,
344
345 BC_RESULT_STR,
346
347 BC_RESULT_IBASE,
348 BC_RESULT_SCALE,
349 BC_RESULT_LAST,
350
351 // These are between to calculate ibase, obase, and last from instructions.
352 BC_RESULT_CONSTANT,
353 BC_RESULT_ONE,
354
355 BC_RESULT_OBASE,
356
357} BcResultType;
358
359typedef union BcResultData {
360 BcNum n;
361 BcVec v;
362 BcId id;
363} BcResultData;
364
365typedef struct BcResult {
366 BcResultType t;
367 BcResultData d;
368} BcResult;
369
370typedef struct BcInstPtr {
371 size_t func;
372 size_t idx;
373 size_t len;
374} BcInstPtr;
375
Gavin Howard01055ba2018-11-03 11:00:21 -0600376// BC_LEX_NEG is not used in lexing; it is only for parsing.
377typedef enum BcLexType {
378
379 BC_LEX_EOF,
380 BC_LEX_INVALID,
381
382 BC_LEX_OP_INC,
383 BC_LEX_OP_DEC,
384
385 BC_LEX_NEG,
386
387 BC_LEX_OP_POWER,
388 BC_LEX_OP_MULTIPLY,
389 BC_LEX_OP_DIVIDE,
390 BC_LEX_OP_MODULUS,
391 BC_LEX_OP_PLUS,
392 BC_LEX_OP_MINUS,
393
394 BC_LEX_OP_REL_EQ,
395 BC_LEX_OP_REL_LE,
396 BC_LEX_OP_REL_GE,
397 BC_LEX_OP_REL_NE,
398 BC_LEX_OP_REL_LT,
399 BC_LEX_OP_REL_GT,
400
401 BC_LEX_OP_BOOL_NOT,
402 BC_LEX_OP_BOOL_OR,
403 BC_LEX_OP_BOOL_AND,
404
405 BC_LEX_OP_ASSIGN_POWER,
406 BC_LEX_OP_ASSIGN_MULTIPLY,
407 BC_LEX_OP_ASSIGN_DIVIDE,
408 BC_LEX_OP_ASSIGN_MODULUS,
409 BC_LEX_OP_ASSIGN_PLUS,
410 BC_LEX_OP_ASSIGN_MINUS,
411 BC_LEX_OP_ASSIGN,
412
413 BC_LEX_NLINE,
414 BC_LEX_WHITESPACE,
415
416 BC_LEX_LPAREN,
417 BC_LEX_RPAREN,
418
419 BC_LEX_LBRACKET,
420 BC_LEX_COMMA,
421 BC_LEX_RBRACKET,
422
423 BC_LEX_LBRACE,
424 BC_LEX_SCOLON,
425 BC_LEX_RBRACE,
426
427 BC_LEX_STR,
428 BC_LEX_NAME,
429 BC_LEX_NUMBER,
430
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100431 BC_LEX_KEY_1st_keyword,
432 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600433 BC_LEX_KEY_BREAK,
434 BC_LEX_KEY_CONTINUE,
435 BC_LEX_KEY_DEFINE,
436 BC_LEX_KEY_ELSE,
437 BC_LEX_KEY_FOR,
438 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100439 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
440 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600441 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100442 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600443 BC_LEX_KEY_LENGTH,
444 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100445 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600446 BC_LEX_KEY_PRINT,
447 BC_LEX_KEY_QUIT,
448 BC_LEX_KEY_READ,
449 BC_LEX_KEY_RETURN,
450 BC_LEX_KEY_SCALE,
451 BC_LEX_KEY_SQRT,
452 BC_LEX_KEY_WHILE,
453
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100454#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600455 BC_LEX_EQ_NO_REG,
456 BC_LEX_OP_MODEXP,
457 BC_LEX_OP_DIVMOD,
458
459 BC_LEX_COLON,
460 BC_LEX_ELSE,
461 BC_LEX_EXECUTE,
462 BC_LEX_PRINT_STACK,
463 BC_LEX_CLEAR_STACK,
464 BC_LEX_STACK_LEVEL,
465 BC_LEX_DUPLICATE,
466 BC_LEX_SWAP,
467 BC_LEX_POP,
468
469 BC_LEX_ASCIIFY,
470 BC_LEX_PRINT_STREAM,
471
472 BC_LEX_STORE_IBASE,
473 BC_LEX_STORE_SCALE,
474 BC_LEX_LOAD,
475 BC_LEX_LOAD_POP,
476 BC_LEX_STORE_PUSH,
477 BC_LEX_STORE_OBASE,
478 BC_LEX_PRINT_POP,
479 BC_LEX_NQUIT,
480 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100481#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600482} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100483// must match order of BC_LEX_KEY_foo etc above
484#if ENABLE_BC
485struct BcLexKeyword {
486 char name8[8];
487};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100488#define BC_LEX_KW_ENTRY(a, b) \
489 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100490static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100491 BC_LEX_KW_ENTRY("auto" , 1), // 0
492 BC_LEX_KW_ENTRY("break" , 1), // 1
493 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
494 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100495
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100496 BC_LEX_KW_ENTRY("else" , 0), // 4
497 BC_LEX_KW_ENTRY("for" , 1), // 5
498 BC_LEX_KW_ENTRY("halt" , 0), // 6
499 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100500
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100501 BC_LEX_KW_ENTRY("if" , 1), // 8
502 BC_LEX_KW_ENTRY("last" , 0), // 9
503 BC_LEX_KW_ENTRY("length" , 1), // 10
504 BC_LEX_KW_ENTRY("limits" , 0), // 11
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100505
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100506 BC_LEX_KW_ENTRY("obase" , 1), // 12
507 BC_LEX_KW_ENTRY("print" , 0), // 13
508 BC_LEX_KW_ENTRY("quit" , 1), // 14
509 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100510
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100511 BC_LEX_KW_ENTRY("return" , 1), // 16
512 BC_LEX_KW_ENTRY("scale" , 1), // 17
513 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
514 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100515};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100516#undef BC_LEX_KW_ENTRY
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100517enum {
518 POSIX_KWORD_MASK = 0
519 | (1 << 0)
520 | (1 << 1)
521 | (0 << 2)
522 | (1 << 3)
523 \
524 | (0 << 4)
525 | (1 << 5)
526 | (0 << 6)
527 | (1 << 7)
528 \
529 | (1 << 8)
530 | (0 << 9)
531 | (1 << 10)
532 | (0 << 11)
533 \
534 | (1 << 12)
535 | (0 << 13)
536 | (1 << 14)
537 | (0 << 15)
538 \
539 | (1 << 16)
540 | (1 << 17)
541 | (1 << 18)
542 | (1 << 19)
543};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100544#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100545#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600546
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100547#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
548# define BC_STATUS BcStatus
549#else
550# define BC_STATUS void
551#endif
552
Gavin Howard01055ba2018-11-03 11:00:21 -0600553struct BcLex;
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100554typedef BC_STATUS (*BcLexNext)(struct BcLex *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600555
556typedef struct BcLex {
557
558 const char *buf;
559 size_t i;
560 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600561 size_t len;
562 bool newline;
563
564 struct {
565 BcLexType t;
566 BcLexType last;
567 BcVec v;
568 } t;
569
570 BcLexNext next;
571
572} BcLex;
573
574#define BC_PARSE_STREND ((char) UCHAR_MAX)
575
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100576#define BC_PARSE_REL (1 << 0)
577#define BC_PARSE_PRINT (1 << 1)
Gavin Howard01055ba2018-11-03 11:00:21 -0600578#define BC_PARSE_NOCALL (1 << 2)
579#define BC_PARSE_NOREAD (1 << 3)
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100580#define BC_PARSE_ARRAY (1 << 4)
Gavin Howard01055ba2018-11-03 11:00:21 -0600581
582#define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags))
583#define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse)))
584
585#define BC_PARSE_FLAG_FUNC_INNER (1 << 0)
586#define BC_PARSE_FUNC_INNER(parse) \
587 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER)
588
589#define BC_PARSE_FLAG_FUNC (1 << 1)
590#define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC)
591
592#define BC_PARSE_FLAG_BODY (1 << 2)
593#define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY)
594
595#define BC_PARSE_FLAG_LOOP (1 << 3)
596#define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP)
597
598#define BC_PARSE_FLAG_LOOP_INNER (1 << 4)
599#define BC_PARSE_LOOP_INNER(parse) \
600 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER)
601
602#define BC_PARSE_FLAG_IF (1 << 5)
603#define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF)
604
605#define BC_PARSE_FLAG_ELSE (1 << 6)
606#define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE)
607
608#define BC_PARSE_FLAG_IF_END (1 << 7)
609#define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END)
610
611#define BC_PARSE_CAN_EXEC(parse) \
612 (!(BC_PARSE_TOP_FLAG(parse) & \
613 (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \
614 BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \
615 BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END)))
616
Gavin Howard01055ba2018-11-03 11:00:21 -0600617struct BcParse;
618
619struct BcProgram;
620
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100621typedef BcStatus (*BcParseParse)(struct BcParse *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600622
623typedef struct BcParse {
624
625 BcParseParse parse;
626
627 BcLex l;
628
629 BcVec flags;
630
631 BcVec exits;
632 BcVec conds;
633
634 BcVec ops;
635
Gavin Howard01055ba2018-11-03 11:00:21 -0600636 BcFunc *func;
637 size_t fidx;
638
639 size_t nbraces;
640 bool auto_part;
641
642} BcParse;
643
Gavin Howard01055ba2018-11-03 11:00:21 -0600644typedef struct BcProgram {
645
646 size_t len;
647 size_t scale;
648
649 BcNum ib;
650 size_t ib_t;
651 BcNum ob;
652 size_t ob_t;
653
654 BcNum hexb;
655
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100656#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600657 BcNum strmb;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100658#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600659
660 BcVec results;
661 BcVec stack;
662
663 BcVec fns;
664 BcVec fn_map;
665
666 BcVec vars;
667 BcVec var_map;
668
669 BcVec arrs;
670 BcVec arr_map;
671
672 BcVec strs;
673 BcVec consts;
674
675 const char *file;
676
677 BcNum last;
678 BcNum zero;
679 BcNum one;
680
681 size_t nchars;
682
Gavin Howard01055ba2018-11-03 11:00:21 -0600683} BcProgram;
684
685#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
686
687#define BC_PROG_MAIN (0)
688#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100689#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600690#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100691#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600692
693#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
694#define BC_PROG_NUM(r, n) \
695 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
696
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100697#define BC_FLAG_W (1 << 0)
698#define BC_FLAG_V (1 << 1)
699#define BC_FLAG_S (1 << 2)
700#define BC_FLAG_Q (1 << 3)
701#define BC_FLAG_L (1 << 4)
702#define BC_FLAG_I (1 << 5)
703#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600704
705#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
706#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
707
Denys Vlasenko64074a12018-12-07 15:50:14 +0100708#define BC_MAX_OBASE ((unsigned) 999)
709#define BC_MAX_DIM ((unsigned) INT_MAX)
710#define BC_MAX_SCALE ((unsigned) UINT_MAX)
711#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
712#define BC_MAX_NUM BC_MAX_STRING
713// Unused apart from "limits" message. Just show a "biggish number" there.
714//#define BC_MAX_NAME BC_MAX_STRING
715//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
716//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
717#define BC_MAX_NAME_STR "999999999"
718#define BC_MAX_EXP_STR "999999999"
719#define BC_MAX_VARS_STR "999999999"
720
721#define BC_MAX_OBASE_STR "999"
722
723#if INT_MAX == 2147483647
724# define BC_MAX_DIM_STR "2147483647"
725#elif INT_MAX == 9223372036854775807
726# define BC_MAX_DIM_STR "9223372036854775807"
727#else
728# error Strange INT_MAX
729#endif
730
731#if UINT_MAX == 4294967295
732# define BC_MAX_SCALE_STR "4294967295"
733# define BC_MAX_STRING_STR "4294967294"
734#elif UINT_MAX == 18446744073709551615
735# define BC_MAX_SCALE_STR "18446744073709551615"
736# define BC_MAX_STRING_STR "18446744073709551614"
737#else
738# error Strange UINT_MAX
739#endif
740#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600741
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100742struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100743 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100744 IF_FEATURE_CLEAN_UP(smallint exiting;)
Denys Vlasenko69171dc2018-12-12 00:29:24 +0100745 smallint in_read;
Gavin Howard01055ba2018-11-03 11:00:21 -0600746 char sbgn;
747 char send;
Gavin Howard01055ba2018-11-03 11:00:21 -0600748
749 BcParse prs;
750 BcProgram prog;
751
Denys Vlasenko5318f812018-12-05 17:48:01 +0100752 // For error messages. Can be set to current parsed line,
753 // or [TODO] to current executing line (can be before last parsed one)
754 unsigned err_line;
755
Gavin Howard01055ba2018-11-03 11:00:21 -0600756 BcVec files;
757
758 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100759
760#if ENABLE_FEATURE_EDITING
761 line_input_t *line_input_state;
762#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100763} FIX_ALIASING;
764#define G (*ptr_to_globals)
765#define INIT_G() do { \
766 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
767} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100768#define FREE_G() do { \
769 FREE_PTR_TO_GLOBALS(); \
770} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100771#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
772#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100773#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100774#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100775# define G_interrupt bb_got_signal
776# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100777#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100778# define G_interrupt 0
779# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100780#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100781#if ENABLE_FEATURE_CLEAN_UP
782# define G_exiting G.exiting
783#else
784# define G_exiting 0
785#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100786#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
787
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100788#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600789
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100790// This is a bit array that corresponds to token types. An entry is
Gavin Howard01055ba2018-11-03 11:00:21 -0600791// true if the token is valid in an expression, false otherwise.
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100792enum {
793 BC_PARSE_EXPRS_BITS = 0
794 + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
795 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
796 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
797 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
798 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
799 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
800 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
801 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8))
Gavin Howard01055ba2018-11-03 11:00:21 -0600802};
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100803static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
804{
805#if ULONG_MAX > 0xffffffff
806 // 64-bit version (will not work correctly for 32-bit longs!)
807 return BC_PARSE_EXPRS_BITS & (1UL << i);
808#else
809 // 32-bit version
810 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
811 if (i >= 32) {
812 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
813 i &= 31;
814 }
815 return m & (1UL << i);
816#endif
817}
Gavin Howard01055ba2018-11-03 11:00:21 -0600818
819// This is an array of data for operators that correspond to token types.
Denys Vlasenko65437582018-12-05 19:37:19 +0100820static const uint8_t bc_parse_ops[] = {
821#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100822 OP(0, false), OP( 0, false ), // inc dec
823 OP(1, false), // neg
Denys Vlasenko65437582018-12-05 19:37:19 +0100824 OP(2, false),
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100825 OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div
826 OP(4, true ), OP( 4, true ), // mod + -
827 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
828 OP(1, false), // not
829 OP(7, true ), OP( 7, true ), // or and
830 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
831 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenko65437582018-12-05 19:37:19 +0100832#undef OP
Gavin Howard01055ba2018-11-03 11:00:21 -0600833};
Denys Vlasenko65437582018-12-05 19:37:19 +0100834#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
835#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
Gavin Howard01055ba2018-11-03 11:00:21 -0600836
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100837// Byte array of up to 4 BC_LEX's, packed into 32-bit word
838typedef uint32_t BcParseNext;
839
Gavin Howard01055ba2018-11-03 11:00:21 -0600840// These identify what tokens can come after expressions in certain cases.
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100841enum {
842#define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) )
843#define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff)
844#define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff)
845 bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF),
846 bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA),
847 bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF),
848 bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN),
849 bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET),
850 bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON),
851 bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF),
852#undef BC_PARSE_NEXT4
853#undef BC_PARSE_NEXT2
854#undef BC_PARSE_NEXT1
855};
Gavin Howard01055ba2018-11-03 11:00:21 -0600856#endif // ENABLE_BC
857
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100858#if ENABLE_DC
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100859static const //BcLexType - should be this type, but narrower type saves size:
860uint8_t
861dc_lex_regs[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600862 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
863 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
864 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
865 BC_LEX_STORE_PUSH,
866};
867
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100868static const //BcLexType - should be this type
869uint8_t
870dc_lex_tokens[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600871 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
872 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
873 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
874 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
875 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
876 BC_LEX_INVALID, BC_LEX_INVALID,
877 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
878 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
879 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
880 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
881 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
882 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
883 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
884 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
885 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
886 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
887 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
888 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
889 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
890 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
891 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
892 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
893 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
894 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
895 BC_LEX_INVALID
896};
897
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100898static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
899int8_t
900dc_parse_insts[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600901 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
902 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
903 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
904 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
905 BC_INST_INVALID, BC_INST_INVALID,
906 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
907 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
908 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
909 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
910 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
911 BC_INST_INVALID, BC_INST_INVALID,
912 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
913 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
914 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
915 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
916 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
917 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
918 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
919 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
920 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
921 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
922 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
923 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
924};
925#endif // ENABLE_DC
926
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100927// In configurations where errors abort instead of propagating error
928// return code up the call chain, functions returning BC_STATUS
929// actually don't return anything, they always succeed and return "void".
930// A macro wrapper is provided, which makes this statement work:
931// s = zbc_func(...)
932// and makes it visible to the compiler that s is always zero,
933// allowing compiler to optimize dead code after the statement.
934//
935// To make code more readable, each such function has a "z"
936// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
937//
938#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100939# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100940# define ERRORFUNC /*nothing*/
941# define ERROR_RETURN(a) a
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100942//moved up: # define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100943# define RETURN_STATUS(v) return (v)
944#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100945# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100946# define ERRORFUNC NORETURN
947# define ERROR_RETURN(a) /*nothing*/
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100948//moved up: # define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100949# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
950#endif
951
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100952#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
953#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
954#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
955//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
956static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
957{
958 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
959}
960//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
961static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
962{
963 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
964}
965
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100966typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
967
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100968typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100969
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100970static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
971 BcNumBinaryOp op, size_t req);
972static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
973static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
974static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
975static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
976static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
977static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
978
979static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
980{
981 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
982 (void) scale;
983 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
984}
985
986static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
987{
988 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
989 (void) scale;
990 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
991}
992
993static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
994{
995 size_t req = BC_NUM_MREQ(a, b, scale);
996 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
997}
998
999static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1000{
1001 size_t req = BC_NUM_MREQ(a, b, scale);
1002 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
1003}
1004
1005static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1006{
1007 size_t req = BC_NUM_MREQ(a, b, scale);
1008 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
1009}
1010
1011static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
1012{
1013 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
1014}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +01001015
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001016static const BcNumBinaryOp zbc_program_ops[] = {
1017 zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub,
Gavin Howard01055ba2018-11-03 11:00:21 -06001018};
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001019#if ERRORS_ARE_FATAL
1020# define zbc_num_add(...) (zbc_num_add(__VA_ARGS__), BC_STATUS_SUCCESS)
1021# define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__), BC_STATUS_SUCCESS)
1022# define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__), BC_STATUS_SUCCESS)
1023# define zbc_num_div(...) (zbc_num_div(__VA_ARGS__), BC_STATUS_SUCCESS)
1024# define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__), BC_STATUS_SUCCESS)
1025# define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__), BC_STATUS_SUCCESS)
1026#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001027
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001028static void fflush_and_check(void)
1029{
1030 fflush_all();
1031 if (ferror(stdout) || ferror(stderr))
1032 bb_perror_msg_and_die("output error");
1033}
1034
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001035#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001036#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001037do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001038 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001039 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001040 return BC_STATUS_FAILURE; \
1041} while (0)
1042#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001043#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001044#endif
1045
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001046static void quit(void) NORETURN;
1047static void quit(void)
1048{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001049 if (ferror(stdin))
1050 bb_perror_msg_and_die("input error");
1051 fflush_and_check();
1052 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001053}
1054
Denys Vlasenko5318f812018-12-05 17:48:01 +01001055static void bc_verror_msg(const char *fmt, va_list p)
1056{
1057 const char *sv = sv; /* for compiler */
1058 if (G.prog.file) {
1059 sv = applet_name;
1060 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
1061 }
1062 bb_verror_msg(fmt, p, NULL);
1063 if (G.prog.file) {
1064 free((char*)applet_name);
1065 applet_name = sv;
1066 }
1067}
1068
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001069static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001070{
1071 va_list p;
1072
1073 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001074 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001075 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01001076
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001077 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001078 exit(1);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001079 ERROR_RETURN(return BC_STATUS_FAILURE;)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001080}
1081
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001082#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001083static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001084{
1085 va_list p;
1086
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001087 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001088 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001089 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001090
1091 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001092 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001093 va_end(p);
1094
1095 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001096 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001097 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001098 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001099 exit(1);
1100 return BC_STATUS_FAILURE;
1101}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001102#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001103
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001104// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1105// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001106// function must not have caller-cleaned parameters on stack.
1107// Unfortunately, vararg function API does exactly that on most arches.
1108// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001109static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001110{
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001111 ERROR_RETURN(return) bc_error_fmt("%s", msg);
1112}
1113static ERRORFUNC int bc_error_bad_character(char c)
1114{
1115 ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c);
1116}
1117static ERRORFUNC int bc_error_bad_expression(void)
1118{
1119 ERROR_RETURN(return) bc_error("bad expression");
1120}
1121static ERRORFUNC int bc_error_bad_token(void)
1122{
1123 ERROR_RETURN(return) bc_error("bad token");
1124}
1125static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1126{
1127 ERROR_RETURN(return) bc_error("stack has too few elements");
1128}
1129static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1130{
1131 ERROR_RETURN(return) bc_error("variable is wrong type");
1132}
1133static ERRORFUNC int bc_error_nested_read_call(void)
1134{
1135 ERROR_RETURN(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001136}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001137#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001138static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001139{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001140 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001141}
Denys Vlasenko00646792018-12-05 18:12:27 +01001142static int bc_POSIX_does_not_allow(const char *msg)
1143{
1144 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1145}
1146static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1147{
1148 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1149}
1150static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1151{
1152 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1153}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001154#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001155
Gavin Howard01055ba2018-11-03 11:00:21 -06001156static void bc_vec_grow(BcVec *v, size_t n)
1157{
1158 size_t cap = v->cap * 2;
1159 while (cap < v->len + n) cap *= 2;
1160 v->v = xrealloc(v->v, v->size * cap);
1161 v->cap = cap;
1162}
1163
1164static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1165{
1166 v->size = esize;
1167 v->cap = BC_VEC_START_CAP;
1168 v->len = 0;
1169 v->dtor = dtor;
1170 v->v = xmalloc(esize * BC_VEC_START_CAP);
1171}
1172
Denys Vlasenko7d628012018-12-04 21:46:47 +01001173static void bc_char_vec_init(BcVec *v)
1174{
1175 bc_vec_init(v, sizeof(char), NULL);
1176}
1177
Gavin Howard01055ba2018-11-03 11:00:21 -06001178static void bc_vec_expand(BcVec *v, size_t req)
1179{
1180 if (v->cap < req) {
1181 v->v = xrealloc(v->v, v->size * req);
1182 v->cap = req;
1183 }
1184}
1185
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001186static void bc_vec_pop(BcVec *v)
1187{
1188 v->len--;
1189 if (v->dtor)
1190 v->dtor(v->v + (v->size * v->len));
1191}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001192
Gavin Howard01055ba2018-11-03 11:00:21 -06001193static void bc_vec_npop(BcVec *v, size_t n)
1194{
1195 if (!v->dtor)
1196 v->len -= n;
1197 else {
1198 size_t len = v->len - n;
1199 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1200 }
1201}
1202
Denys Vlasenko7d628012018-12-04 21:46:47 +01001203static void bc_vec_pop_all(BcVec *v)
1204{
1205 bc_vec_npop(v, v->len);
1206}
1207
Gavin Howard01055ba2018-11-03 11:00:21 -06001208static void bc_vec_push(BcVec *v, const void *data)
1209{
1210 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1211 memmove(v->v + (v->size * v->len), data, v->size);
1212 v->len += 1;
1213}
1214
1215static void bc_vec_pushByte(BcVec *v, char data)
1216{
1217 bc_vec_push(v, &data);
1218}
1219
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001220static void bc_vec_pushZeroByte(BcVec *v)
1221{
1222 //bc_vec_pushByte(v, '\0');
1223 // better:
1224 bc_vec_push(v, &const_int_0);
1225}
1226
Gavin Howard01055ba2018-11-03 11:00:21 -06001227static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1228{
1229 if (idx == v->len)
1230 bc_vec_push(v, data);
1231 else {
1232
1233 char *ptr;
1234
1235 if (v->len == v->cap) bc_vec_grow(v, 1);
1236
1237 ptr = v->v + v->size * idx;
1238
1239 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1240 memmove(ptr, data, v->size);
1241 }
1242}
1243
1244static void bc_vec_string(BcVec *v, size_t len, const char *str)
1245{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001246 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001247 bc_vec_expand(v, len + 1);
1248 memcpy(v->v, str, len);
1249 v->len = len;
1250
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001251 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001252}
1253
1254static void bc_vec_concat(BcVec *v, const char *str)
1255{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001256 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001257
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001258 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001259
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001260 slen = strlen(str);
1261 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001262
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001263 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001264 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001265
1266 v->len = len;
1267}
1268
1269static void *bc_vec_item(const BcVec *v, size_t idx)
1270{
1271 return v->v + v->size * idx;
1272}
1273
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01001274static char** bc_program_str(size_t idx)
1275{
1276 return bc_vec_item(&G.prog.strs, idx);
1277}
1278
1279static BcFunc* bc_program_func(size_t idx)
1280{
1281 return bc_vec_item(&G.prog.fns, idx);
1282}
1283
Gavin Howard01055ba2018-11-03 11:00:21 -06001284static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1285{
1286 return v->v + v->size * (v->len - idx - 1);
1287}
1288
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001289static void *bc_vec_top(const BcVec *v)
1290{
1291 return v->v + v->size * (v->len - 1);
1292}
1293
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001294static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001295{
1296 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001297 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001298 free(v->v);
1299}
1300
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001301static int bc_id_cmp(const void *e1, const void *e2)
1302{
1303 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1304}
1305
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001306static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001307{
1308 free(((BcId *) id)->name);
1309}
1310
Gavin Howard01055ba2018-11-03 11:00:21 -06001311static size_t bc_map_find(const BcVec *v, const void *ptr)
1312{
1313 size_t low = 0, high = v->len;
1314
1315 while (low < high) {
1316
1317 size_t mid = (low + high) / 2;
1318 BcId *id = bc_vec_item(v, mid);
1319 int result = bc_id_cmp(ptr, id);
1320
1321 if (result == 0)
1322 return mid;
1323 else if (result < 0)
1324 high = mid;
1325 else
1326 low = mid + 1;
1327 }
1328
1329 return low;
1330}
1331
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001332static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001333{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001334 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001335
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001336 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001337 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001338 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1339 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001340 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001341 bc_vec_pushAt(v, ptr, n);
1342 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001343}
1344
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001345#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06001346static size_t bc_map_index(const BcVec *v, const void *ptr)
1347{
1348 size_t i = bc_map_find(v, ptr);
1349 if (i >= v->len) return BC_VEC_INVALID_IDX;
1350 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1351}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001352#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001353
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001354static int push_input_byte(BcVec *vec, char c)
1355{
1356 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1357 || c > 0x7e
1358 ) {
1359 // Bad chars on this line, ignore entire line
1360 bc_error_fmt("illegal character 0x%02x", c);
1361 return 1;
1362 }
1363 bc_vec_pushByte(vec, (char)c);
1364 return 0;
1365}
1366
Denys Vlasenkob402ff82018-12-11 15:45:15 +01001367// This is not a "z" function: can also return BC_STATUS_EOF
Denys Vlasenko4dd36522018-12-11 22:26:38 +01001368// Can return success (0) or BC_STATUS_EOF.
1369// Exits with error message if read error is detected.
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001370static BcStatus bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001371{
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001372 BcStatus s;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001373 bool bad_chars;
Gavin Howard01055ba2018-11-03 11:00:21 -06001374
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001375 s = BC_STATUS_SUCCESS;
Denys Vlasenko00d77792018-11-30 23:13:42 +01001376 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001377 int c;
Gavin Howard01055ba2018-11-03 11:00:21 -06001378
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001379 bad_chars = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001380 bc_vec_pop_all(vec);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001381
1382 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001383
Gavin Howard01055ba2018-11-03 11:00:21 -06001384#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001385 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001386 intr:
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001387 G_interrupt = 0;
Denys Vlasenkoac6ed112018-12-08 21:39:10 +01001388 // GNU bc says "interrupted execution."
1389 // GNU dc says "Interrupt!"
1390 fputs("\ninterrupted execution\n", stderr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001391 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001392# if ENABLE_FEATURE_EDITING
1393 if (G_ttyin) {
1394 int n, i;
1395# define line_buf bb_common_bufsiz1
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001396 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001397 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1398 if (n == 0) // ^C
1399 goto intr;
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001400 s = BC_STATUS_EOF;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001401 break;
1402 }
1403 i = 0;
1404 for (;;) {
1405 c = line_buf[i++];
1406 if (!c) break;
1407 bad_chars |= push_input_byte(vec, c);
1408 }
1409# undef line_buf
1410 } else
1411# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001412#endif
Denys Vlasenkoed849352018-12-06 10:26:13 +01001413 {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001414 IF_FEATURE_BC_SIGNALS(errno = 0;)
1415 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001416 c = fgetc(stdin);
1417#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
1418 // Both conditions appear simultaneously, check both just in case
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001419 if (errno == EINTR || G_interrupt) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001420 // ^C was pressed
1421 clearerr(stdin);
1422 goto intr;
1423 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001424#endif
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001425 if (c == EOF) {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001426 if (ferror(stdin))
1427 quit(); // this emits error message
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001428 s = BC_STATUS_EOF;
Denys Vlasenkoed849352018-12-06 10:26:13 +01001429 // Note: EOF does not append '\n', therefore:
1430 // printf 'print 123\n' | bc - works
1431 // printf 'print 123' | bc - fails (syntax error)
1432 break;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001433 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001434 bad_chars |= push_input_byte(vec, c);
1435 } while (c != '\n');
Denys Vlasenkoed849352018-12-06 10:26:13 +01001436 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001437 } while (bad_chars);
Gavin Howard01055ba2018-11-03 11:00:21 -06001438
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001439 bc_vec_pushZeroByte(vec);
Gavin Howard01055ba2018-11-03 11:00:21 -06001440
Denys Vlasenkof522dd92018-12-07 16:35:43 +01001441 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06001442}
1443
Denys Vlasenkodf515392018-12-02 19:27:48 +01001444static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001445{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001446 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001447 size_t size = ((size_t) -1);
1448 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001449
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001450 // Never returns NULL (dies on errors)
1451 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001452
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001453 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001454 char c = buf[i];
1455 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1456 || c > 0x7e
1457 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001458 free(buf);
1459 buf = NULL;
1460 break;
1461 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001462 }
1463
Denys Vlasenkodf515392018-12-02 19:27:48 +01001464 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001465}
1466
Gavin Howard01055ba2018-11-03 11:00:21 -06001467static void bc_num_setToZero(BcNum *n, size_t scale)
1468{
1469 n->len = 0;
1470 n->neg = false;
1471 n->rdx = scale;
1472}
1473
1474static void bc_num_zero(BcNum *n)
1475{
1476 bc_num_setToZero(n, 0);
1477}
1478
1479static void bc_num_one(BcNum *n)
1480{
1481 bc_num_setToZero(n, 0);
1482 n->len = 1;
1483 n->num[0] = 1;
1484}
1485
1486static void bc_num_ten(BcNum *n)
1487{
1488 bc_num_setToZero(n, 0);
1489 n->len = 2;
1490 n->num[0] = 0;
1491 n->num[1] = 1;
1492}
1493
Denys Vlasenko3129f702018-12-09 12:04:44 +01001494// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001495static void bc_num_init(BcNum *n, size_t req)
1496{
1497 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001498 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001499 n->num = xmalloc(req);
1500 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001501 n->rdx = 0;
1502 n->len = 0;
1503 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001504}
1505
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001506static void bc_num_init_DEF_SIZE(BcNum *n)
1507{
1508 bc_num_init(n, BC_NUM_DEF_SIZE);
1509}
1510
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001511static void bc_num_expand(BcNum *n, size_t req)
1512{
1513 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1514 if (req > n->cap) {
1515 n->num = xrealloc(n->num, req);
1516 n->cap = req;
1517 }
1518}
1519
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001520static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001521{
1522 free(((BcNum *) num)->num);
1523}
1524
1525static void bc_num_copy(BcNum *d, BcNum *s)
1526{
1527 if (d != s) {
1528 bc_num_expand(d, s->cap);
1529 d->len = s->len;
1530 d->neg = s->neg;
1531 d->rdx = s->rdx;
1532 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1533 }
1534}
1535
Denys Vlasenko29301232018-12-11 15:29:32 +01001536static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001537{
1538 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001539 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001540
Denys Vlasenko29301232018-12-11 15:29:32 +01001541 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001542
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001543 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001544
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001545 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001546
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001547 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001548 pow *= 10;
1549
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001550 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001551 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001552 prev = result;
1553 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001554 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001555 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001556
Denys Vlasenko29301232018-12-11 15:29:32 +01001557 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001558}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001559#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001560# define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001561#endif
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001562
1563static void bc_num_ulong2num(BcNum *n, unsigned long val)
1564{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001565 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001566
1567 bc_num_zero(n);
1568
1569 if (val == 0) return;
1570
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001571 if (ULONG_MAX == 0xffffffffUL)
1572 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001573 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001574 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001575 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001576
1577 ptr = n->num;
1578 for (;;) {
1579 n->len++;
1580 *ptr++ = val % 10;
1581 val /= 10;
1582 if (val == 0) break;
1583 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001584}
1585
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001586static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001587 size_t len)
1588{
1589 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001590 for (i = 0; i < len; ++i) {
1591 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001592 a[i + j++] += 10;
1593 a[i + j] -= 1;
1594 }
1595 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001596}
1597
1598static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1599{
1600 size_t i;
1601 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001602 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001603 return BC_NUM_NEG(i + 1, c < 0);
1604}
1605
1606static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1607{
1608 size_t i, min, a_int, b_int, diff;
1609 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001610 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001611 ssize_t cmp;
1612
1613 if (a == b) return 0;
1614 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1615 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001616
1617 if (a->neg != b->neg) // signs of a and b differ
1618 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1619 return (int)b->neg - (int)a->neg;
1620 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001621
1622 a_int = BC_NUM_INT(a);
1623 b_int = BC_NUM_INT(b);
1624 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001625
1626 if (a_int != 0) return (ssize_t) a_int;
1627
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001628 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001629 if (a_max) {
1630 min = b->rdx;
1631 diff = a->rdx - b->rdx;
1632 max_num = a->num + diff;
1633 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001634 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1635 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001636 min = a->rdx;
1637 diff = b->rdx - a->rdx;
1638 max_num = b->num + diff;
1639 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001640 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001641 }
1642
1643 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001644 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001645
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001646 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001647 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001648 }
1649
1650 return 0;
1651}
1652
1653static void bc_num_truncate(BcNum *n, size_t places)
1654{
1655 if (places == 0) return;
1656
1657 n->rdx -= places;
1658
1659 if (n->len != 0) {
1660 n->len -= places;
1661 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1662 }
1663}
1664
1665static void bc_num_extend(BcNum *n, size_t places)
1666{
1667 size_t len = n->len + places;
1668
1669 if (places != 0) {
1670
1671 if (n->cap < len) bc_num_expand(n, len);
1672
1673 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1674 memset(n->num, 0, sizeof(BcDig) * places);
1675
1676 n->len += places;
1677 n->rdx += places;
1678 }
1679}
1680
1681static void bc_num_clean(BcNum *n)
1682{
1683 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1684 if (n->len == 0)
1685 n->neg = false;
1686 else if (n->len < n->rdx)
1687 n->len = n->rdx;
1688}
1689
1690static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1691{
1692 if (n->rdx < scale)
1693 bc_num_extend(n, scale - n->rdx);
1694 else
1695 bc_num_truncate(n, n->rdx - scale);
1696
1697 bc_num_clean(n);
1698 if (n->len != 0) n->neg = !neg1 != !neg2;
1699}
1700
1701static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1702 BcNum *restrict b)
1703{
1704 if (idx < n->len) {
1705
1706 b->len = n->len - idx;
1707 a->len = idx;
1708 a->rdx = b->rdx = 0;
1709
1710 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1711 memcpy(a->num, n->num, idx * sizeof(BcDig));
1712 }
1713 else {
1714 bc_num_zero(b);
1715 bc_num_copy(a, n);
1716 }
1717
1718 bc_num_clean(a);
1719 bc_num_clean(b);
1720}
1721
Denys Vlasenko29301232018-12-11 15:29:32 +01001722static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001723{
Denys Vlasenko29301232018-12-11 15:29:32 +01001724 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001725
1726 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1727 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1728 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001729 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001730 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001731
1732 if (n->rdx >= places)
1733 n->rdx -= places;
1734 else {
1735 bc_num_extend(n, places - n->rdx);
1736 n->rdx = 0;
1737 }
1738
1739 bc_num_clean(n);
1740
Denys Vlasenko29301232018-12-11 15:29:32 +01001741 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001742}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001743#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001744# define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001745#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001746
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001747static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001748{
1749 BcNum one;
1750 BcDig num[2];
1751
1752 one.cap = 2;
1753 one.num = num;
1754 bc_num_one(&one);
1755
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001756 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001757}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001758#if ERRORS_ARE_FATAL
1759# define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS)
1760#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001761
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001762static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001763{
1764 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1765 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1766 int carry, in;
1767
1768 // Because this function doesn't need to use scale (per the bc spec),
1769 // I am hijacking it to say whether it's doing an add or a subtract.
1770
1771 if (a->len == 0) {
1772 bc_num_copy(c, b);
1773 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001774 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001775 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001776 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001777 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001778 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001779 }
1780
1781 c->neg = a->neg;
1782 c->rdx = BC_MAX(a->rdx, b->rdx);
1783 min_rdx = BC_MIN(a->rdx, b->rdx);
1784 c->len = 0;
1785
1786 if (a->rdx > b->rdx) {
1787 diff = a->rdx - b->rdx;
1788 ptr = a->num;
1789 ptr_a = a->num + diff;
1790 ptr_b = b->num;
1791 }
1792 else {
1793 diff = b->rdx - a->rdx;
1794 ptr = b->num;
1795 ptr_a = a->num;
1796 ptr_b = b->num + diff;
1797 }
1798
1799 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1800
1801 ptr_c += diff;
1802 a_int = BC_NUM_INT(a);
1803 b_int = BC_NUM_INT(b);
1804
1805 if (a_int > b_int) {
1806 min_int = b_int;
1807 max = a_int;
1808 ptr = ptr_a;
1809 }
1810 else {
1811 min_int = a_int;
1812 max = b_int;
1813 ptr = ptr_b;
1814 }
1815
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001816 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001817 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1818 carry = in / 10;
1819 ptr_c[i] = (BcDig)(in % 10);
1820 }
1821
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001822 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001823 in = ((int) ptr[i]) + carry;
1824 carry = in / 10;
1825 ptr_c[i] = (BcDig)(in % 10);
1826 }
1827
1828 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1829
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001830 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001831}
1832
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001833static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001834{
Gavin Howard01055ba2018-11-03 11:00:21 -06001835 ssize_t cmp;
1836 BcNum *minuend, *subtrahend;
1837 size_t start;
1838 bool aneg, bneg, neg;
1839
1840 // Because this function doesn't need to use scale (per the bc spec),
1841 // I am hijacking it to say whether it's doing an add or a subtract.
1842
1843 if (a->len == 0) {
1844 bc_num_copy(c, b);
1845 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001846 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001847 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001848 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001849 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001850 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001851 }
1852
1853 aneg = a->neg;
1854 bneg = b->neg;
1855 a->neg = b->neg = false;
1856
1857 cmp = bc_num_cmp(a, b);
1858
1859 a->neg = aneg;
1860 b->neg = bneg;
1861
1862 if (cmp == 0) {
1863 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001864 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001865 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001866 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001867 neg = a->neg;
1868 minuend = a;
1869 subtrahend = b;
1870 }
1871 else {
1872 neg = b->neg;
1873 if (sub) neg = !neg;
1874 minuend = b;
1875 subtrahend = a;
1876 }
1877
1878 bc_num_copy(c, minuend);
1879 c->neg = neg;
1880
1881 if (c->rdx < subtrahend->rdx) {
1882 bc_num_extend(c, subtrahend->rdx - c->rdx);
1883 start = 0;
1884 }
1885 else
1886 start = c->rdx - subtrahend->rdx;
1887
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001888 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001889
1890 bc_num_clean(c);
1891
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001892 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001893}
1894
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001895static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001896 BcNum *restrict c)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001897#if ERRORS_ARE_FATAL
1898# define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS)
1899#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001900{
1901 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001902 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001903 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001904 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001905
Gavin Howard01055ba2018-11-03 11:00:21 -06001906 if (a->len == 0 || b->len == 0) {
1907 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001908 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001909 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001910 aone = BC_NUM_ONE(a);
1911 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001912 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001913 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001914 }
1915
1916 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1917 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1918 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001919 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001920 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001921
Gavin Howard01055ba2018-11-03 11:00:21 -06001922 bc_num_expand(c, a->len + b->len + 1);
1923
1924 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001925 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001926
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001927 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001928
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001929 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001930 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001931 unsigned in = c->num[i + j];
1932 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1933 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001934 carry = in / 10;
1935 c->num[i + j] = (BcDig)(in % 10);
1936 }
1937
1938 c->num[i + j] += (BcDig) carry;
1939 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001940
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001941#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001942 // a=2^1000000
1943 // a*a <- without check below, this will not be interruptible
1944 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001945#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001946 }
1947
1948 c->len = len;
1949
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001950 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001951 }
1952
1953 bc_num_init(&l1, max);
1954 bc_num_init(&h1, max);
1955 bc_num_init(&l2, max);
1956 bc_num_init(&h2, max);
1957 bc_num_init(&m1, max);
1958 bc_num_init(&m2, max);
1959 bc_num_init(&z0, max);
1960 bc_num_init(&z1, max);
1961 bc_num_init(&z2, max);
1962 bc_num_init(&temp, max + max);
1963
1964 bc_num_split(a, max2, &l1, &h1);
1965 bc_num_split(b, max2, &l2, &h2);
1966
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001967 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001968 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001969 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001970 if (s) goto err;
1971
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001972 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001973 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001974 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001975 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001976 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001977 if (s) goto err;
1978
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001979 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001980 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001981 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001982 if (s) goto err;
1983
Denys Vlasenko29301232018-12-11 15:29:32 +01001984 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001985 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001986 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001987 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001988 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001989 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001990 s = zbc_num_add(&temp, &z2, c, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001991
1992err:
1993 bc_num_free(&temp);
1994 bc_num_free(&z2);
1995 bc_num_free(&z1);
1996 bc_num_free(&z0);
1997 bc_num_free(&m2);
1998 bc_num_free(&m1);
1999 bc_num_free(&h2);
2000 bc_num_free(&l2);
2001 bc_num_free(&h1);
2002 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002003 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002004}
2005
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002006static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002007{
2008 BcStatus s;
2009 BcNum cpa, cpb;
2010 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
2011
2012 scale = BC_MAX(scale, a->rdx);
2013 scale = BC_MAX(scale, b->rdx);
2014 scale = BC_MIN(a->rdx + b->rdx, scale);
2015 maxrdx = BC_MAX(maxrdx, scale);
2016
2017 bc_num_init(&cpa, a->len);
2018 bc_num_init(&cpb, b->len);
2019
2020 bc_num_copy(&cpa, a);
2021 bc_num_copy(&cpb, b);
2022 cpa.neg = cpb.neg = false;
2023
Denys Vlasenko29301232018-12-11 15:29:32 +01002024 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002025 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01002026 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002027 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002028 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06002029 if (s) goto err;
2030
2031 maxrdx += scale;
2032 bc_num_expand(c, c->len + maxrdx);
2033
2034 if (c->len < maxrdx) {
2035 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
2036 c->len += maxrdx;
2037 }
2038
2039 c->rdx = maxrdx;
2040 bc_num_retireMul(c, scale, a->neg, b->neg);
2041
2042err:
2043 bc_num_free(&cpb);
2044 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002045 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002046}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002047#if ERRORS_ARE_FATAL
2048# define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS)
2049#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002050
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002051static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002052{
2053 BcStatus s = BC_STATUS_SUCCESS;
2054 BcDig *n, *p, q;
2055 size_t len, end, i;
2056 BcNum cp;
2057 bool zero = true;
2058
2059 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002060 RETURN_STATUS(bc_error("divide by zero"));
2061 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002062 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002063 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002064 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002065 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002066 bc_num_copy(c, a);
2067 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002068 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002069 }
2070
2071 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2072 bc_num_copy(&cp, a);
2073 len = b->len;
2074
2075 if (len > cp.len) {
2076 bc_num_expand(&cp, len + 2);
2077 bc_num_extend(&cp, len - cp.len);
2078 }
2079
2080 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2081 cp.rdx -= b->rdx;
2082 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2083
2084 if (b->rdx == b->len) {
2085 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
2086 len -= i - 1;
2087 }
2088
2089 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2090
2091 // We want an extra zero in front to make things simpler.
2092 cp.num[cp.len++] = 0;
2093 end = cp.len - len;
2094
2095 bc_num_expand(c, cp.len);
2096
2097 bc_num_zero(c);
2098 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2099 c->rdx = cp.rdx;
2100 c->len = cp.len;
2101 p = b->num;
2102
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002103 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002104 n = cp.num + i;
2105 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002106 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002107 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002108#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002109 // a=2^100000
2110 // scale=40000
2111 // 1/a <- without check below, this will not be interruptible
2112 if (G_interrupt) {
2113 s = BC_STATUS_FAILURE;
2114 break;
2115 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002116#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 }
2118
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002119 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002120 bc_num_free(&cp);
2121
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002122 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002123}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002124#if ERRORS_ARE_FATAL
2125# define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS)
2126#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002127
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002128static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002129 BcNum *restrict d, size_t scale, size_t ts)
2130{
2131 BcStatus s;
2132 BcNum temp;
2133 bool neg;
2134
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002135 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002136 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002137
2138 if (a->len == 0) {
2139 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002140 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002141 }
2142
2143 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002144 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002145 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002146
2147 if (scale != 0) scale = ts;
2148
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002149 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002150 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002151 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002152 if (s) goto err;
2153
2154 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2155
2156 neg = d->neg;
2157 bc_num_retireMul(d, ts, a->neg, b->neg);
2158 d->neg = neg;
2159
2160err:
2161 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002162 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002163}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002164#if ERRORS_ARE_FATAL
2165# define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS)
2166#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002167
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002168static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002169{
2170 BcStatus s;
2171 BcNum c1;
2172 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2173
2174 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002175 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002176 bc_num_free(&c1);
2177
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002178 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002179}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002180#if ERRORS_ARE_FATAL
2181# define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS)
2182#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002183
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002184static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002185{
2186 BcStatus s = BC_STATUS_SUCCESS;
2187 BcNum copy;
2188 unsigned long pow;
2189 size_t i, powrdx, resrdx;
2190 bool neg, zero;
2191
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002192 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002193
2194 if (b->len == 0) {
2195 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002196 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002197 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002198 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002199 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002200 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002201 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002202 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002203 if (!b->neg)
2204 bc_num_copy(c, a);
2205 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002206 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002207 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002208 }
2209
2210 neg = b->neg;
2211 b->neg = false;
2212
Denys Vlasenko29301232018-12-11 15:29:32 +01002213 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002214 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002215
2216 bc_num_init(&copy, a->len);
2217 bc_num_copy(&copy, a);
2218
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002219 if (!neg) {
2220 if (a->rdx > scale)
2221 scale = a->rdx;
2222 if (a->rdx * pow < scale)
2223 scale = a->rdx * pow;
2224 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002225
2226 b->neg = neg;
2227
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002228 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002229 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002230 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002231 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002232 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002233 //if (G_interrupt) {
2234 // s = BC_STATUS_FAILURE;
2235 // goto err;
2236 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002237 }
2238
Gavin Howard01055ba2018-11-03 11:00:21 -06002239 bc_num_copy(c, &copy);
2240
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002241 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002242
2243 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002244 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002245 if (s) goto err;
2246
2247 if (pow & 1) {
2248 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002249 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002250 if (s) goto err;
2251 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002252 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002253 //if (G_interrupt) {
2254 // s = BC_STATUS_FAILURE;
2255 // goto err;
2256 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002257 }
2258
2259 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002260 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002261 if (s) goto err;
2262 }
2263
Gavin Howard01055ba2018-11-03 11:00:21 -06002264 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2265
2266 // We can't use bc_num_clean() here.
2267 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2268 if (zero) bc_num_setToZero(c, scale);
2269
2270err:
2271 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002272 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002273}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002274#if ERRORS_ARE_FATAL
2275# define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS)
2276#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002277
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002278static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002279 BcNumBinaryOp op, size_t req)
2280{
2281 BcStatus s;
2282 BcNum num2, *ptr_a, *ptr_b;
2283 bool init = false;
2284
2285 if (c == a) {
2286 ptr_a = &num2;
2287 memcpy(ptr_a, c, sizeof(BcNum));
2288 init = true;
2289 }
2290 else
2291 ptr_a = a;
2292
2293 if (c == b) {
2294 ptr_b = &num2;
2295 if (c != a) {
2296 memcpy(ptr_b, c, sizeof(BcNum));
2297 init = true;
2298 }
2299 }
2300 else
2301 ptr_b = b;
2302
2303 if (init)
2304 bc_num_init(c, req);
2305 else
2306 bc_num_expand(c, req);
2307
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002308#if !ERRORS_ARE_FATAL
Gavin Howard01055ba2018-11-03 11:00:21 -06002309 s = op(ptr_a, ptr_b, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002310#else
2311 op(ptr_a, ptr_b, c, scale);
2312 s = BC_STATUS_SUCCESS;
2313#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002314
2315 if (init) bc_num_free(&num2);
2316
Denys Vlasenko29301232018-12-11 15:29:32 +01002317 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002318}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002319#if ERRORS_ARE_FATAL
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002320# define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002321#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002322
Denys Vlasenko9311e012018-12-10 11:54:18 +01002323static bool bc_num_strValid(const char *val, size_t base)
2324{
2325 BcDig b;
2326 bool radix;
2327
2328 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2329 radix = false;
2330 for (;;) {
2331 BcDig c = *val++;
2332 if (c == '\0')
2333 break;
2334 if (c == '.') {
2335 if (radix) return false;
2336 radix = true;
2337 continue;
2338 }
2339 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2340 return false;
2341 }
2342 return true;
2343}
2344
2345// Note: n is already "bc_num_zero()"ed,
2346// leading zeroes in "val" are removed
2347static void bc_num_parseDecimal(BcNum *n, const char *val)
2348{
2349 size_t len, i;
2350 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002351
2352 len = strlen(val);
2353 if (len == 0)
2354 return;
2355
Denys Vlasenko9311e012018-12-10 11:54:18 +01002356 bc_num_expand(n, len);
2357
2358 ptr = strchr(val, '.');
2359
2360 n->rdx = 0;
2361 if (ptr != NULL)
2362 n->rdx = (size_t)((val + len) - (ptr + 1));
2363
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002364 for (i = 0; val[i]; ++i) {
2365 if (val[i] != '0' && val[i] != '.') {
2366 // Not entirely zero value - convert it, and exit
2367 i = len - 1;
2368 for (;;) {
2369 n->num[n->len] = val[i] - '0';
2370 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002371 skip_dot:
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002372 if ((ssize_t)--i == (ssize_t)-1) break;
2373 if (val[i] == '.') goto skip_dot;
2374 }
2375 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002376 }
2377 }
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002378 // if this is reached, the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002379}
2380
2381// Note: n is already "bc_num_zero()"ed,
2382// leading zeroes in "val" are removed
2383static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2384{
2385 BcStatus s;
2386 BcNum temp, mult, result;
2387 BcDig c = '\0';
2388 unsigned long v;
2389 size_t i, digits;
2390
2391 for (i = 0; ; ++i) {
2392 if (val[i] == '\0')
2393 return;
2394 if (val[i] != '.' && val[i] != '0')
2395 break;
2396 }
2397
2398 bc_num_init_DEF_SIZE(&temp);
2399 bc_num_init_DEF_SIZE(&mult);
2400
2401 for (;;) {
2402 c = *val++;
2403 if (c == '\0') goto int_err;
2404 if (c == '.') break;
2405
2406 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2407
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002408 s = zbc_num_mul(n, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002409 if (s) goto int_err;
2410 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002411 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002412 if (s) goto int_err;
2413 }
2414
2415 bc_num_init(&result, base->len);
2416 //bc_num_zero(&result); - already is
2417 bc_num_one(&mult);
2418
2419 digits = 0;
2420 for (;;) {
2421 c = *val++;
2422 if (c == '\0') break;
2423 digits++;
2424
2425 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2426
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002427 s = zbc_num_mul(&result, base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002428 if (s) goto err;
2429 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002430 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002431 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002432 s = zbc_num_mul(&mult, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002433 if (s) goto err;
2434 }
2435
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002436 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002437 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002438 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002439 if (s) goto err;
2440
2441 if (n->len != 0) {
2442 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2443 } else
2444 bc_num_zero(n);
2445
2446err:
2447 bc_num_free(&result);
2448int_err:
2449 bc_num_free(&mult);
2450 bc_num_free(&temp);
2451}
2452
Denys Vlasenko29301232018-12-11 15:29:32 +01002453static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002454 size_t base_t)
2455{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002456 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002457 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002458
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002459 bc_num_zero(n);
2460 while (*val == '0') val++;
2461
Gavin Howard01055ba2018-11-03 11:00:21 -06002462 if (base_t == 10)
2463 bc_num_parseDecimal(n, val);
2464 else
2465 bc_num_parseBase(n, val, base);
2466
Denys Vlasenko29301232018-12-11 15:29:32 +01002467 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002468}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002469#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002470# define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002471#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002472
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002473static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002474{
2475 BcStatus s;
2476 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2477 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2478 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2479
2480 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2481 bc_num_expand(b, req);
2482
2483 if (a->len == 0) {
2484 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002485 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002486 }
2487 else if (a->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002488 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002489 else if (BC_NUM_ONE(a)) {
2490 bc_num_one(b);
2491 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002492 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002493 }
2494
2495 scale = BC_MAX(scale, a->rdx) + 1;
2496 len = a->len + scale;
2497
2498 bc_num_init(&num1, len);
2499 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002500 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002501
2502 bc_num_one(&half);
2503 half.num[0] = 5;
2504 half.rdx = 1;
2505
2506 bc_num_init(&f, len);
2507 bc_num_init(&fprime, len);
2508
2509 x0 = &num1;
2510 x1 = &num2;
2511
2512 bc_num_one(x0);
2513 pow = BC_NUM_INT(a);
2514
2515 if (pow) {
2516
2517 if (pow & 1)
2518 x0->num[0] = 2;
2519 else
2520 x0->num[0] = 6;
2521
2522 pow -= 2 - (pow & 1);
2523
2524 bc_num_extend(x0, pow);
2525
2526 // Make sure to move the radix back.
2527 x0->rdx -= pow;
2528 }
2529
2530 x0->rdx = digs = digs1 = 0;
2531 resrdx = scale + 2;
2532 len = BC_NUM_INT(x0) + resrdx - 1;
2533
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002534 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002535
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002536 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002537 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002538 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002539 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002540 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002541 if (s) goto err;
2542
2543 cmp = bc_num_cmp(x1, x0);
2544 digs = x1->len - (unsigned long long) llabs(cmp);
2545
2546 if (cmp == cmp2 && digs == digs1)
2547 times += 1;
2548 else
2549 times = 0;
2550
2551 resrdx += times > 4;
2552
2553 cmp2 = cmp1;
2554 cmp1 = cmp;
2555 digs1 = digs;
2556
2557 temp = x0;
2558 x0 = x1;
2559 x1 = temp;
2560 }
2561
Gavin Howard01055ba2018-11-03 11:00:21 -06002562 bc_num_copy(b, x0);
2563 scale -= 1;
2564 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2565
2566err:
2567 bc_num_free(&fprime);
2568 bc_num_free(&f);
2569 bc_num_free(&half);
2570 bc_num_free(&num2);
2571 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002572 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002573}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002574#if ERRORS_ARE_FATAL
2575# define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS)
2576#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002577
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002578static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002579 size_t scale)
2580{
2581 BcStatus s;
2582 BcNum num2, *ptr_a;
2583 bool init = false;
2584 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2585
2586 if (c == a) {
2587 memcpy(&num2, c, sizeof(BcNum));
2588 ptr_a = &num2;
2589 bc_num_init(c, len);
2590 init = true;
2591 }
2592 else {
2593 ptr_a = a;
2594 bc_num_expand(c, len);
2595 }
2596
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002597 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002598
2599 if (init) bc_num_free(&num2);
2600
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002601 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002602}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002603#if ERRORS_ARE_FATAL
2604# define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
2605#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002606
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002607#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002608static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002609{
2610 BcStatus s;
2611 BcNum base, exp, two, temp;
2612
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002613 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002614 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002615 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002616 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002617 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002618 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002619
2620 bc_num_expand(d, c->len);
2621 bc_num_init(&base, c->len);
2622 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002623 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002624 bc_num_init(&temp, b->len);
2625
2626 bc_num_one(&two);
2627 two.num[0] = 2;
2628 bc_num_one(d);
2629
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002630 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002631 if (s) goto err;
2632 bc_num_copy(&exp, b);
2633
2634 while (exp.len != 0) {
2635
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002636 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002637 if (s) goto err;
2638
2639 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002640 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002641 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002642 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002643 if (s) goto err;
2644 }
2645
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002646 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002647 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002648 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002649 if (s) goto err;
2650 }
2651
2652err:
2653 bc_num_free(&temp);
2654 bc_num_free(&two);
2655 bc_num_free(&exp);
2656 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002657 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002658}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002659#if ERRORS_ARE_FATAL
2660# define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
2661#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002662#endif // ENABLE_DC
2663
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002664#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002665static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002666{
2667 BcId a;
2668 size_t i;
2669
2670 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002671 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002672 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002673 }
2674
2675 a.idx = var;
2676 a.name = name;
2677
2678 bc_vec_push(&f->autos, &a);
2679
Denys Vlasenko29301232018-12-11 15:29:32 +01002680 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002681}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002682#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002683# define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002684#endif
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002685#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002686
2687static void bc_func_init(BcFunc *f)
2688{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002689 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002690 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2691 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2692 f->nparams = 0;
2693}
2694
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002695static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002696{
2697 BcFunc *f = (BcFunc *) func;
2698 bc_vec_free(&f->code);
2699 bc_vec_free(&f->autos);
2700 bc_vec_free(&f->labels);
2701}
2702
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002703static void bc_array_expand(BcVec *a, size_t len);
2704
Gavin Howard01055ba2018-11-03 11:00:21 -06002705static void bc_array_init(BcVec *a, bool nums)
2706{
2707 if (nums)
2708 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2709 else
2710 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2711 bc_array_expand(a, 1);
2712}
2713
Gavin Howard01055ba2018-11-03 11:00:21 -06002714static void bc_array_expand(BcVec *a, size_t len)
2715{
2716 BcResultData data;
2717
2718 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2719 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002720 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002721 bc_vec_push(a, &data.n);
2722 }
2723 }
2724 else {
2725 while (len > a->len) {
2726 bc_array_init(&data.v, true);
2727 bc_vec_push(a, &data.v);
2728 }
2729 }
2730}
2731
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002732static void bc_array_copy(BcVec *d, const BcVec *s)
2733{
2734 size_t i;
2735
2736 bc_vec_pop_all(d);
2737 bc_vec_expand(d, s->cap);
2738 d->len = s->len;
2739
2740 for (i = 0; i < s->len; ++i) {
2741 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2742 bc_num_init(dnum, snum->len);
2743 bc_num_copy(dnum, snum);
2744 }
2745}
2746
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002747static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002748{
2749 free(*((char **) string));
2750}
2751
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002752#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002753static void bc_result_copy(BcResult *d, BcResult *src)
2754{
2755 d->t = src->t;
2756
2757 switch (d->t) {
2758
2759 case BC_RESULT_TEMP:
2760 case BC_RESULT_IBASE:
2761 case BC_RESULT_SCALE:
2762 case BC_RESULT_OBASE:
2763 {
2764 bc_num_init(&d->d.n, src->d.n.len);
2765 bc_num_copy(&d->d.n, &src->d.n);
2766 break;
2767 }
2768
2769 case BC_RESULT_VAR:
2770 case BC_RESULT_ARRAY:
2771 case BC_RESULT_ARRAY_ELEM:
2772 {
2773 d->d.id.name = xstrdup(src->d.id.name);
2774 break;
2775 }
2776
2777 case BC_RESULT_CONSTANT:
2778 case BC_RESULT_LAST:
2779 case BC_RESULT_ONE:
2780 case BC_RESULT_STR:
2781 {
2782 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2783 break;
2784 }
2785 }
2786}
2787#endif // ENABLE_DC
2788
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002789static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002790{
2791 BcResult *r = (BcResult *) result;
2792
2793 switch (r->t) {
2794
2795 case BC_RESULT_TEMP:
2796 case BC_RESULT_IBASE:
2797 case BC_RESULT_SCALE:
2798 case BC_RESULT_OBASE:
2799 {
2800 bc_num_free(&r->d.n);
2801 break;
2802 }
2803
2804 case BC_RESULT_VAR:
2805 case BC_RESULT_ARRAY:
2806 case BC_RESULT_ARRAY_ELEM:
2807 {
2808 free(r->d.id.name);
2809 break;
2810 }
2811
2812 default:
2813 {
2814 // Do nothing.
2815 break;
2816 }
2817 }
2818}
2819
2820static void bc_lex_lineComment(BcLex *l)
2821{
2822 l->t.t = BC_LEX_WHITESPACE;
2823 while (l->i < l->len && l->buf[l->i++] != '\n');
2824 --l->i;
2825}
2826
2827static void bc_lex_whitespace(BcLex *l)
2828{
2829 char c;
2830 l->t.t = BC_LEX_WHITESPACE;
2831 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
2832}
2833
Denys Vlasenko29301232018-12-11 15:29:32 +01002834static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002835{
2836 const char *buf = l->buf + l->i;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002837 size_t len, bslashes, i, ccnt;
2838 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002839
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002840 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002841 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002842 bslashes = 0;
2843 ccnt = i = 0;
2844 for (;;) {
2845 char c = buf[i];
2846 if (c == '\0')
2847 break;
2848 if (c == '\\' && buf[i + 1] == '\n') {
2849 i += 2;
2850 bslashes++;
2851 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002852 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002853 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2854 if (c != '.') break;
2855 // if '.' was already seen, stop on second one:
2856 if (pt) break;
2857 pt = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002858 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002859 // buf[i] is one of "0-9A-F."
2860 i++;
2861 if (c != '.')
2862 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002863 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002864 //i is buf[i] index of the first not-yet-parsed char
2865 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002866
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002867 //ccnt is the number of chars in the number string, excluding possible
2868 //trailing "." and possible following trailing "\<newline>"(s).
2869 len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination
2870
Denys Vlasenko64074a12018-12-07 15:50:14 +01002871 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2872 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2873 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002874 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002875 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002876
Denys Vlasenko7d628012018-12-04 21:46:47 +01002877 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002878 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002879 bc_vec_push(&l->t.v, &start);
2880
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002881 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002882 // If we have hit a backslash, skip it. We don't have
2883 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002884 if (*buf == '\\') {
2885 buf += 2;
2886 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002887 continue;
2888 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002889 bc_vec_push(&l->t.v, buf);
2890 buf++;
2891 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002892 }
2893
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002894 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002895
Denys Vlasenko29301232018-12-11 15:29:32 +01002896 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002897}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002898#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002899# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002900#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002901
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002902static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002903{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002904 size_t i;
2905 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002906
2907 l->t.t = BC_LEX_NAME;
2908
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002909 i = 0;
2910 buf = l->buf + l->i - 1;
2911 for (;;) {
2912 char c = buf[i];
2913 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2914 i++;
2915 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002916
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002917#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002918 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2919 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2920 if (i > BC_MAX_STRING)
2921 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2922 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002923#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002924 bc_vec_string(&l->t.v, i, buf);
2925
2926 // Increment the index. We minus 1 because it has already been incremented.
2927 l->i += i - 1;
2928
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002929 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002930}
2931
2932static void bc_lex_init(BcLex *l, BcLexNext next)
2933{
2934 l->next = next;
Denys Vlasenko7d628012018-12-04 21:46:47 +01002935 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002936}
2937
2938static void bc_lex_free(BcLex *l)
2939{
2940 bc_vec_free(&l->t.v);
2941}
2942
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002943static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002944{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002945 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002946 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002947}
2948
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002949static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002950{
2951 BcStatus s;
2952
2953 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002954 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002955
2956 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002957 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06002958 l->t.t = BC_LEX_EOF;
2959
2960 l->newline = (l->i == l->len);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002961 if (l->newline) RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002962
2963 // Loop until failure or we don't have whitespace. This
2964 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002965 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002966 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002967//TODO: replace pointer with if(IS_BC)
2968 ERROR_RETURN(s =) l->next(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002969 } while (!s && l->t.t == BC_LEX_WHITESPACE);
2970
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002971 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002972}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002973#if ERRORS_ARE_FATAL
2974# define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__), BC_STATUS_SUCCESS)
2975#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002976
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002977static BC_STATUS zbc_lex_text(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002978{
2979 l->buf = text;
2980 l->i = 0;
2981 l->len = strlen(text);
2982 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002983 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002984}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002985#if ERRORS_ARE_FATAL
2986# define zbc_lex_text(...) (zbc_lex_text(__VA_ARGS__), BC_STATUS_SUCCESS)
2987#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002988
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002989#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002990static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002991{
2992 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002993 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002994 const char *buf = l->buf + l->i - 1;
2995
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002996 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2997 const char *keyword8 = bc_lex_kws[i].name8;
2998 unsigned j = 0;
2999 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
3000 j++;
3001 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06003002 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003003 if (keyword8[j] != '\0')
3004 continue;
3005 match:
3006 // buf starts with keyword bc_lex_kws[i]
3007 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01003008 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003009 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003010 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003011 }
3012
3013 // We minus 1 because the index has already been incremented.
3014 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003015 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003016 }
3017
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003018 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003019
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01003020 if (l->t.v.len > 2) {
3021 // Prevent this:
3022 // >>> qwe=1
3023 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
3024 // '
3025 unsigned len = strchrnul(buf, '\n') - buf;
3026 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
3027 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003028
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003029 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003030}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003031#if ERRORS_ARE_FATAL
3032# define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__), BC_STATUS_SUCCESS)
3033#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003034
3035static BcStatus bc_lex_string(BcLex *l)
3036{
3037 size_t len, nls = 0, i = l->i;
3038 char c;
3039
3040 l->t.t = BC_LEX_STR;
3041
3042 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n');
3043
3044 if (c == '\0') {
3045 l->i = i;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003046 return bc_error("string end could not be found");
Gavin Howard01055ba2018-11-03 11:00:21 -06003047 }
3048
3049 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003050 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3051 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3052 if (len > BC_MAX_STRING)
3053 return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]");
3054 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003055 bc_vec_string(&l->t.v, len, l->buf + l->i);
3056
3057 l->i = i + 1;
3058 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003059 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003060
3061 return BC_STATUS_SUCCESS;
3062}
3063
3064static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3065{
3066 if (l->buf[l->i] == '=') {
3067 ++l->i;
3068 l->t.t = with;
3069 }
3070 else
3071 l->t.t = without;
3072}
3073
Denys Vlasenko29301232018-12-11 15:29:32 +01003074static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003075{
3076 size_t i, nls = 0;
3077 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003078
3079 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003080 i = ++l->i;
3081 for (;;) {
3082 char c = buf[i];
3083 check_star:
3084 if (c == '*') {
3085 c = buf[++i];
3086 if (c == '/')
3087 break;
3088 goto check_star;
3089 }
3090 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003091 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003092 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003093 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003094 nls += (c == '\n');
3095 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003096 }
3097
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003098 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003099 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003100 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003101
Denys Vlasenko29301232018-12-11 15:29:32 +01003102 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003103}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003104#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003105# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003106#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003107
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003108static FAST_FUNC BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003109{
3110 BcStatus s = BC_STATUS_SUCCESS;
3111 char c = l->buf[l->i++], c2;
3112
3113 // This is the workhorse of the lexer.
3114 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003115 case '\0':
3116 case '\n':
Gavin Howard01055ba2018-11-03 11:00:21 -06003117 l->newline = true;
3118 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3119 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003120 case '\t':
3121 case '\v':
3122 case '\f':
3123 case '\r':
3124 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 bc_lex_whitespace(l);
3126 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003127 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003128 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003129 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003130 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003131 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003132 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003133 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003134 case '"':
Gavin Howard01055ba2018-11-03 11:00:21 -06003135 s = bc_lex_string(l);
3136 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003138 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003139 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003140 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003141 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003142 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003143 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3144 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003146 c2 = l->buf[l->i];
3147 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003148 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003149 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 ++l->i;
3151 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003152 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003153 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003154 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003155 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003157 case '(':
3158 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003159 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3160 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003161 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003162 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3163 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003165 c2 = l->buf[l->i];
3166 if (c2 == '+') {
3167 ++l->i;
3168 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003169 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003170 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3171 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003172 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003173 l->t.t = BC_LEX_COMMA;
3174 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003175 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003176 c2 = l->buf[l->i];
3177 if (c2 == '-') {
3178 ++l->i;
3179 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003180 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003181 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3182 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003183 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003184 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003185 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003186 else {
3187 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003188 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003189 }
3190 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003191 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003192 c2 = l->buf[l->i];
3193 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003194 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003195 else
3196 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3197 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003198 case '0':
3199 case '1':
3200 case '2':
3201 case '3':
3202 case '4':
3203 case '5':
3204 case '6':
3205 case '7':
3206 case '8':
3207 case '9':
3208 case 'A':
3209 case 'B':
3210 case 'C':
3211 case 'D':
3212 case 'E':
3213 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003214 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003215 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003216 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003217 l->t.t = BC_LEX_SCOLON;
3218 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003219 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003220 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3221 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003222 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003223 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3224 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003225 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003226 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3227 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003228 case '[':
3229 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003230 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3231 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003232 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003233 if (l->buf[l->i] == '\n') {
3234 l->t.t = BC_LEX_WHITESPACE;
3235 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003236 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003237 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003238 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003239 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003240 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3241 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003242 case 'a':
3243 case 'b':
3244 case 'c':
3245 case 'd':
3246 case 'e':
3247 case 'f':
3248 case 'g':
3249 case 'h':
3250 case 'i':
3251 case 'j':
3252 case 'k':
3253 case 'l':
3254 case 'm':
3255 case 'n':
3256 case 'o':
3257 case 'p':
3258 case 'q':
3259 case 'r':
3260 case 's':
3261 case 't':
3262 case 'u':
3263 case 'v':
3264 case 'w':
3265 case 'x':
3266 case 'y':
3267 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003268 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003269 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003270 case '{':
3271 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003272 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3273 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003274 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003275 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003276 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003277 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003278 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003279 ++l->i;
3280 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003281 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003282 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003283 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003284 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003286 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003287 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003288 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003289 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003290 }
3291
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003292 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003293}
3294#endif // ENABLE_BC
3295
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003296#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003297static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003298{
Gavin Howard01055ba2018-11-03 11:00:21 -06003299 if (isspace(l->buf[l->i - 1])) {
3300 bc_lex_whitespace(l);
3301 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003302 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003303 RETURN_STATUS(bc_error("extended register"));
3304 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003305 }
3306 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003307 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003308 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003309 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003310 l->t.t = BC_LEX_NAME;
3311 }
3312
Denys Vlasenko29301232018-12-11 15:29:32 +01003313 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003314}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003315#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003316# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003317#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003318
Denys Vlasenko29301232018-12-11 15:29:32 +01003319static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003320{
3321 size_t depth = 1, nls = 0, i = l->i;
3322 char c;
3323
3324 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003325 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003326
3327 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3328
3329 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3330 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3331 nls += (c == '\n');
3332
3333 if (depth) bc_vec_push(&l->t.v, &c);
3334 }
3335
3336 if (c == '\0') {
3337 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003338 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003339 }
3340
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003341 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003342 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3343 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3344 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003345 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003346 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003347
3348 l->i = i;
3349 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003350 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003351
Denys Vlasenko29301232018-12-11 15:29:32 +01003352 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003353}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003354#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003355# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003356#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003357
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003358static FAST_FUNC BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003359{
3360 BcStatus s = BC_STATUS_SUCCESS;
3361 char c = l->buf[l->i++], c2;
3362 size_t i;
3363
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003364 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3365 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003366 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003367 }
3368
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003369 if (c >= '%' && c <= '~'
3370 && (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID
3371 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003372 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003373 }
3374
3375 // This is the workhorse of the lexer.
3376 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003377 case '\0':
Gavin Howard01055ba2018-11-03 11:00:21 -06003378 l->t.t = BC_LEX_EOF;
3379 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003380 case '\n':
3381 case '\t':
3382 case '\v':
3383 case '\f':
3384 case '\r':
3385 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003386 l->newline = (c == '\n');
3387 bc_lex_whitespace(l);
3388 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003389 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003390 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003391 if (c2 == '=')
3392 l->t.t = BC_LEX_OP_REL_NE;
3393 else if (c2 == '<')
3394 l->t.t = BC_LEX_OP_REL_LE;
3395 else if (c2 == '>')
3396 l->t.t = BC_LEX_OP_REL_GE;
3397 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003398 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003399 ++l->i;
3400 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003401 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003402 bc_lex_lineComment(l);
3403 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003404 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003405 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003406 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003407 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003408 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003409 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003410 case '0':
3411 case '1':
3412 case '2':
3413 case '3':
3414 case '4':
3415 case '5':
3416 case '6':
3417 case '7':
3418 case '8':
3419 case '9':
3420 case 'A':
3421 case 'B':
3422 case 'C':
3423 case 'D':
3424 case 'E':
3425 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003426 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003427 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003428 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003429 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003430 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003431 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003432 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003433 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003434 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003435 }
3436
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003437 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003438}
3439#endif // ENABLE_DC
3440
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003441static void bc_program_addFunc(char *name, size_t *idx);
3442
Gavin Howard01055ba2018-11-03 11:00:21 -06003443static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3444{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003445 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003446 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003447}
3448
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003449#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3450
Gavin Howard01055ba2018-11-03 11:00:21 -06003451static void bc_parse_pushName(BcParse *p, char *name)
3452{
3453 size_t i = 0, len = strlen(name);
3454
3455 for (; i < len; ++i) bc_parse_push(p, name[i]);
3456 bc_parse_push(p, BC_PARSE_STREND);
3457
3458 free(name);
3459}
3460
3461static void bc_parse_pushIndex(BcParse *p, size_t idx)
3462{
3463 unsigned char amt, i, nums[sizeof(size_t)];
3464
3465 for (amt = 0; idx; ++amt) {
3466 nums[amt] = (char) idx;
3467 idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT;
3468 }
3469
3470 bc_parse_push(p, amt);
3471 for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]);
3472}
3473
3474static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3475{
3476 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003477 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003478
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003479 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003480
3481 bc_parse_push(p, BC_INST_NUM);
3482 bc_parse_pushIndex(p, idx);
3483
3484 ++(*nexs);
3485 (*prev) = BC_INST_NUM;
3486}
3487
3488static BcStatus bc_parse_text(BcParse *p, const char *text)
3489{
3490 BcStatus s;
3491
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003492 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003493
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003494 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003495 p->l.t.t = BC_LEX_INVALID;
3496 s = p->parse(p);
3497 if (s) return s;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003498 if (!BC_PARSE_CAN_EXEC(p))
3499 return bc_error("file is not executable");
Gavin Howard01055ba2018-11-03 11:00:21 -06003500 }
3501
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003502 return zbc_lex_text(&p->l, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06003503}
3504
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003505// Called when parsing or execution detects a failure,
3506// resets execution structures.
3507static void bc_program_reset(void)
3508{
3509 BcFunc *f;
3510 BcInstPtr *ip;
3511
3512 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3513 bc_vec_pop_all(&G.prog.results);
3514
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003515 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003516 ip = bc_vec_top(&G.prog.stack);
3517 ip->idx = f->code.len;
3518}
3519
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003520#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003521 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003522
Denys Vlasenkod38af482018-12-04 19:11:02 +01003523// Called when bc/dc_parse_parse() detects a failure,
3524// resets parsing structures.
3525static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003526{
3527 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003528 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003529 bc_vec_pop_all(&p->func->code);
3530 bc_vec_pop_all(&p->func->autos);
3531 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003532
3533 bc_parse_updateFunc(p, BC_PROG_MAIN);
3534 }
3535
3536 p->l.i = p->l.len;
3537 p->l.t.t = BC_LEX_EOF;
3538 p->auto_part = (p->nbraces = 0);
3539
3540 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003541 bc_vec_pop_all(&p->exits);
3542 bc_vec_pop_all(&p->conds);
3543 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003544
Denys Vlasenkod38af482018-12-04 19:11:02 +01003545 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003546}
3547
3548static void bc_parse_free(BcParse *p)
3549{
3550 bc_vec_free(&p->flags);
3551 bc_vec_free(&p->exits);
3552 bc_vec_free(&p->conds);
3553 bc_vec_free(&p->ops);
3554 bc_lex_free(&p->l);
3555}
3556
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003557static void bc_parse_create(BcParse *p, size_t func,
Gavin Howard01055ba2018-11-03 11:00:21 -06003558 BcParseParse parse, BcLexNext next)
3559{
3560 memset(p, 0, sizeof(BcParse));
3561
3562 bc_lex_init(&p->l, next);
3563 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3564 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3565 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003566 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003567 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3568
3569 p->parse = parse;
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003570 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003571 bc_parse_updateFunc(p, func);
3572}
3573
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003574#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003575
3576#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3577#define BC_PARSE_LEAF(p, rparen) \
3578 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3579 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3580
3581// We can calculate the conversion between tokens and exprs by subtracting the
3582// position of the first operator in the lex enum and adding the position of the
3583// first in the expr enum. Note: This only works for binary operators.
3584#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3585
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003586static BC_STATUS zbc_parse_else(BcParse *p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003587static BC_STATUS zbc_parse_stmt(BcParse *p);
3588static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003589static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003590#if ERRORS_ARE_FATAL
3591# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
3592# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
3593# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
3594#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003595
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003596static BC_STATUS zbc_parse_operator(BcParse *p, BcLexType type, size_t start,
Gavin Howard01055ba2018-11-03 11:00:21 -06003597 size_t *nexprs, bool next)
3598{
3599 BcStatus s = BC_STATUS_SUCCESS;
3600 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003601 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3602 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003603
3604 while (p->ops.len > start) {
3605
3606 t = BC_PARSE_TOP_OP(p);
3607 if (t == BC_LEX_LPAREN) break;
3608
Denys Vlasenko65437582018-12-05 19:37:19 +01003609 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003610 if (l >= r && (l != r || !left)) break;
3611
3612 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3613 bc_vec_pop(&p->ops);
3614 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3615 }
3616
3617 bc_vec_push(&p->ops, &type);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003618 if (next) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003619
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003620 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003621}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003622#if ERRORS_ARE_FATAL
3623# define zbc_parse_operator(...) (zbc_parse_operator(__VA_ARGS__), BC_STATUS_SUCCESS)
3624#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003625
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003626static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003627{
3628 BcLexType top;
3629
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003630 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003631 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003632 top = BC_PARSE_TOP_OP(p);
3633
3634 while (top != BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003635 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3636
3637 bc_vec_pop(&p->ops);
3638 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3639
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003640 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003641 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003642 top = BC_PARSE_TOP_OP(p);
3643 }
3644
3645 bc_vec_pop(&p->ops);
3646
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003647 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003648}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003649#if ERRORS_ARE_FATAL
3650# define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__), BC_STATUS_SUCCESS)
3651#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003652
3653static BcStatus bc_parse_params(BcParse *p, uint8_t flags)
3654{
3655 BcStatus s;
3656 bool comma = false;
3657 size_t nparams;
3658
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003659 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003660 if (s) return s;
3661
3662 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
3663
3664 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003665 s = zbc_parse_expr(p, flags, bc_parse_next_param);
Gavin Howard01055ba2018-11-03 11:00:21 -06003666 if (s) return s;
3667
3668 comma = p->l.t.t == BC_LEX_COMMA;
3669 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003670 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003671 if (s) return s;
3672 }
3673 }
3674
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003675 if (comma) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003676 bc_parse_push(p, BC_INST_CALL);
3677 bc_parse_pushIndex(p, nparams);
3678
3679 return BC_STATUS_SUCCESS;
3680}
3681
3682static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags)
3683{
3684 BcStatus s;
3685 BcId entry, *entry_ptr;
3686 size_t idx;
3687
3688 entry.name = name;
3689
3690 s = bc_parse_params(p, flags);
3691 if (s) goto err;
3692
3693 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003694 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003695 goto err;
3696 }
3697
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003698 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003699
3700 if (idx == BC_VEC_INVALID_IDX) {
3701 name = xstrdup(entry.name);
3702 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003703 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003704 free(entry.name);
3705 }
3706 else
3707 free(name);
3708
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003709 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003710 bc_parse_pushIndex(p, entry_ptr->idx);
3711
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003712 return zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003713
3714err:
3715 free(name);
3716 return s;
3717}
3718
3719static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
3720{
3721 BcStatus s;
3722 char *name;
3723
3724 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003725 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003726 if (s) goto err;
3727
3728 if (p->l.t.t == BC_LEX_LBRACKET) {
3729
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003730 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003731 if (s) goto err;
3732
3733 if (p->l.t.t == BC_LEX_RBRACKET) {
3734
3735 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003736 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003737 goto err;
3738 }
3739
3740 *type = BC_INST_ARRAY;
3741 }
3742 else {
3743
3744 *type = BC_INST_ARRAY_ELEM;
3745
3746 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003747 s = zbc_parse_expr(p, flags, bc_parse_next_elem);
Gavin Howard01055ba2018-11-03 11:00:21 -06003748 if (s) goto err;
3749 }
3750
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003751 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003752 if (s) goto err;
3753 bc_parse_push(p, *type);
3754 bc_parse_pushName(p, name);
3755 }
3756 else if (p->l.t.t == BC_LEX_LPAREN) {
3757
3758 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003759 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003760 goto err;
3761 }
3762
3763 *type = BC_INST_CALL;
3764 s = bc_parse_call(p, name, flags);
3765 }
3766 else {
3767 *type = BC_INST_VAR;
3768 bc_parse_push(p, BC_INST_VAR);
3769 bc_parse_pushName(p, name);
3770 }
3771
3772 return s;
3773
3774err:
3775 free(name);
3776 return s;
3777}
3778
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003779static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003780{
3781 BcStatus s;
3782
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003783 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003784 if (s) RETURN_STATUS(s);
3785 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003786
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003787 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003788 if (s) RETURN_STATUS(s);
3789 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003790
3791 bc_parse_push(p, BC_INST_READ);
3792
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003793 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003794}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003795#if ERRORS_ARE_FATAL
3796# define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__), BC_STATUS_SUCCESS)
3797#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003798
3799static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
3800 BcInst *prev)
3801{
3802 BcStatus s;
3803
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003804 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003805 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003806 if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003807
3808 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3809
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003810 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003811 if (s) return s;
3812
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003813 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Gavin Howard01055ba2018-11-03 11:00:21 -06003814 if (s) return s;
3815
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003816 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003817
3818 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3819 bc_parse_push(p, *prev);
3820
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003821 return zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003822}
3823
3824static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
3825{
3826 BcStatus s;
3827
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003828 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003829 if (s) return s;
3830
3831 if (p->l.t.t != BC_LEX_LPAREN) {
3832 *type = BC_INST_SCALE;
3833 bc_parse_push(p, BC_INST_SCALE);
3834 return BC_STATUS_SUCCESS;
3835 }
3836
3837 *type = BC_INST_SCALE_FUNC;
3838 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3839
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003840 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003841 if (s) return s;
3842
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003843 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Gavin Howard01055ba2018-11-03 11:00:21 -06003844 if (s) return s;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003845 if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003846 bc_parse_push(p, BC_INST_SCALE_FUNC);
3847
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003848 return zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003849}
3850
3851static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
3852 size_t *nexprs, uint8_t flags)
3853{
3854 BcStatus s;
3855 BcLexType type;
3856 char inst;
3857 BcInst etype = *prev;
3858
3859 if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM ||
3860 etype == BC_INST_SCALE || etype == BC_INST_LAST ||
3861 etype == BC_INST_IBASE || etype == BC_INST_OBASE)
3862 {
3863 *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC);
3864 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003865 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003866 }
3867 else {
3868
3869 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3870 *paren_expr = true;
3871
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003872 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003873 if (s) return s;
3874 type = p->l.t.t;
3875
3876 // Because we parse the next part of the expression
3877 // right here, we need to increment this.
3878 *nexprs = *nexprs + 1;
3879
3880 switch (type) {
3881
3882 case BC_LEX_NAME:
3883 {
3884 s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
3885 break;
3886 }
3887
3888 case BC_LEX_KEY_IBASE:
3889 case BC_LEX_KEY_LAST:
3890 case BC_LEX_KEY_OBASE:
3891 {
3892 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003893 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 break;
3895 }
3896
3897 case BC_LEX_KEY_SCALE:
3898 {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003899 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003900 if (s) return s;
3901 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003902 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003903 else
3904 bc_parse_push(p, BC_INST_SCALE);
3905 break;
3906 }
3907
3908 default:
3909 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003910 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003911 break;
3912 }
3913 }
3914
3915 if (!s) bc_parse_push(p, inst);
3916 }
3917
3918 return s;
3919}
3920
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003921static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06003922 bool rparen, size_t *nexprs)
3923{
3924 BcStatus s;
3925 BcLexType type;
3926 BcInst etype = *prev;
3927
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003928 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003929 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003930
3931 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
3932 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
3933 BC_LEX_OP_MINUS :
3934 BC_LEX_NEG;
3935 *prev = BC_PARSE_TOKEN_INST(type);
3936
3937 // We can just push onto the op stack because this is the largest
3938 // precedence operator that gets pushed. Inc/dec does not.
3939 if (type != BC_LEX_OP_MINUS)
3940 bc_vec_push(&p->ops, &type);
3941 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003942 s = zbc_parse_operator(p, type, ops_bgn, nexprs, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06003943
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003944 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003945}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003946#if ERRORS_ARE_FATAL
3947# define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__), BC_STATUS_SUCCESS)
3948#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003949
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003950static BC_STATUS zbc_parse_string(BcParse *p, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06003951{
3952 char *str = xstrdup(p->l.t.v.v);
3953
3954 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003955 bc_parse_pushIndex(p, G.prog.strs.len);
3956 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06003957 bc_parse_push(p, inst);
3958
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003959 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003960}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003961#if ERRORS_ARE_FATAL
3962# define zbc_parse_string(...) (zbc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3963#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003964
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003965static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003966{
3967 BcStatus s;
3968 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003969 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06003970
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003971 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003972 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003973
3974 type = p->l.t.t;
3975
3976 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003977 RETURN_STATUS(bc_error("bad print statement"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003978
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003979 comma = false;
3980 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003981 if (type == BC_LEX_STR) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003982 s = zbc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003983 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003984 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003985 s = zbc_parse_expr(p, 0, bc_parse_next_print);
3986 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003987 bc_parse_push(p, BC_INST_PRINT_POP);
3988 }
3989
Gavin Howard01055ba2018-11-03 11:00:21 -06003990 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003991 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003992 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003993 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003994 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003995 type = p->l.t.t;
3996 }
3997
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003998 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003999
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004000 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004001}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004002#if ERRORS_ARE_FATAL
4003# define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__), BC_STATUS_SUCCESS)
4004#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004005
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004006static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004007{
4008 BcStatus s;
4009 BcLexType t;
4010 bool paren;
4011
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004012 if (!BC_PARSE_FUNC(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004013
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004014 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004015 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004016
4017 t = p->l.t.t;
4018 paren = t == BC_LEX_LPAREN;
4019
4020 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4021 bc_parse_push(p, BC_INST_RET0);
4022 else {
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004023 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004024 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004025 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004026 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004027 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004028 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004029
4030 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004031 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004032 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06004033 }
4034
4035 bc_parse_push(p, BC_INST_RET);
4036 }
4037
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004038 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004039}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004040#if ERRORS_ARE_FATAL
4041# define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__), BC_STATUS_SUCCESS)
4042#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004043
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004044static BC_STATUS zbc_parse_endBody(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004045{
4046 BcStatus s = BC_STATUS_SUCCESS;
4047
4048 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004049 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004050
4051 if (brace) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004052 if (p->l.t.t != BC_LEX_RBRACE)
4053 RETURN_STATUS(bc_error_bad_token());
4054 if (!p->nbraces)
4055 RETURN_STATUS(bc_error_bad_token());
4056 --p->nbraces;
4057 s = zbc_lex_next(&p->l);
4058 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004059 }
4060
4061 if (BC_PARSE_IF(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004062 uint8_t *flag_ptr;
4063
4064 while (p->l.t.t == BC_LEX_NLINE) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004065 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004066 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004067 }
4068
4069 bc_vec_pop(&p->flags);
4070
4071 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4072 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4073
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004074 if (p->l.t.t == BC_LEX_KEY_ELSE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004075 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004076 }
4077 else if (BC_PARSE_ELSE(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004078 BcInstPtr *ip;
4079 size_t *label;
4080
4081 bc_vec_pop(&p->flags);
4082
4083 ip = bc_vec_top(&p->exits);
4084 label = bc_vec_item(&p->func->labels, ip->idx);
4085 *label = p->func->code.len;
4086
4087 bc_vec_pop(&p->exits);
4088 }
4089 else if (BC_PARSE_FUNC_INNER(p)) {
4090 bc_parse_push(p, BC_INST_RET0);
4091 bc_parse_updateFunc(p, BC_PROG_MAIN);
4092 bc_vec_pop(&p->flags);
4093 }
4094 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004095 BcInstPtr *ip = bc_vec_top(&p->exits);
4096 size_t *label = bc_vec_top(&p->conds);
4097
4098 bc_parse_push(p, BC_INST_JUMP);
4099 bc_parse_pushIndex(p, *label);
4100
4101 label = bc_vec_item(&p->func->labels, ip->idx);
4102 *label = p->func->code.len;
4103
4104 bc_vec_pop(&p->flags);
4105 bc_vec_pop(&p->exits);
4106 bc_vec_pop(&p->conds);
4107 }
4108
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004109 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004110}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004111#if ERRORS_ARE_FATAL
4112# define zbc_parse_endBody(...) (zbc_parse_endBody(__VA_ARGS__), BC_STATUS_SUCCESS)
4113#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004114
4115static void bc_parse_startBody(BcParse *p, uint8_t flags)
4116{
4117 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4118 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4119 flags |= BC_PARSE_FLAG_BODY;
4120 bc_vec_push(&p->flags, &flags);
4121}
4122
4123static void bc_parse_noElse(BcParse *p)
4124{
4125 BcInstPtr *ip;
4126 size_t *label;
4127 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4128
4129 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4130
4131 ip = bc_vec_top(&p->exits);
4132 label = bc_vec_item(&p->func->labels, ip->idx);
4133 *label = p->func->code.len;
4134
4135 bc_vec_pop(&p->exits);
4136}
4137
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004138static BC_STATUS zbc_parse_if(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004139{
4140 BcStatus s;
4141 BcInstPtr ip;
4142
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004143 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004144 if (s) RETURN_STATUS(s);
4145 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004146
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004147 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004148 if (s) RETURN_STATUS(s);
4149 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4150 if (s) RETURN_STATUS(s);
4151 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004152
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004153 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004154 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004155 bc_parse_push(p, BC_INST_JUMP_ZERO);
4156
4157 ip.idx = p->func->labels.len;
4158 ip.func = ip.len = 0;
4159
4160 bc_parse_pushIndex(p, ip.idx);
4161 bc_vec_push(&p->exits, &ip);
4162 bc_vec_push(&p->func->labels, &ip.idx);
4163 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4164
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004165 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004166}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004167#if ERRORS_ARE_FATAL
4168# define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__), BC_STATUS_SUCCESS)
4169#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004170
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004171#undef zbc_parse_else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004172static BC_STATUS zbc_parse_else(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004173{
4174 BcInstPtr ip;
4175
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004176 if (!BC_PARSE_IF_END(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004177
4178 ip.idx = p->func->labels.len;
4179 ip.func = ip.len = 0;
4180
4181 bc_parse_push(p, BC_INST_JUMP);
4182 bc_parse_pushIndex(p, ip.idx);
4183
4184 bc_parse_noElse(p);
4185
4186 bc_vec_push(&p->exits, &ip);
4187 bc_vec_push(&p->func->labels, &ip.idx);
4188 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4189
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004190 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004191}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004192#if ERRORS_ARE_FATAL
4193# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
4194#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004195
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004196static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004197{
4198 BcStatus s;
4199 BcInstPtr ip;
4200
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004201 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004202 if (s) RETURN_STATUS(s);
4203 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004204 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004205 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004206
4207 ip.idx = p->func->labels.len;
4208
4209 bc_vec_push(&p->func->labels, &p->func->code.len);
4210 bc_vec_push(&p->conds, &ip.idx);
4211
4212 ip.idx = p->func->labels.len;
4213 ip.func = 1;
4214 ip.len = 0;
4215
4216 bc_vec_push(&p->exits, &ip);
4217 bc_vec_push(&p->func->labels, &ip.idx);
4218
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004219 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4220 if (s) RETURN_STATUS(s);
4221 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004222 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004223 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004224
4225 bc_parse_push(p, BC_INST_JUMP_ZERO);
4226 bc_parse_pushIndex(p, ip.idx);
4227 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4228
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004229 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004230}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004231#if ERRORS_ARE_FATAL
4232# define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__), BC_STATUS_SUCCESS)
4233#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004234
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004235static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004236{
4237 BcStatus s;
4238 BcInstPtr ip;
4239 size_t cond_idx, exit_idx, body_idx, update_idx;
4240
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004241 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004242 if (s) RETURN_STATUS(s);
4243 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004244 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004245 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004246
4247 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004248 s = zbc_parse_expr(p, 0, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004249 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004250 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004251
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004252 if (s) RETURN_STATUS(s);
4253 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004254 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004255 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004256
4257 cond_idx = p->func->labels.len;
4258 update_idx = cond_idx + 1;
4259 body_idx = update_idx + 1;
4260 exit_idx = body_idx + 1;
4261
4262 bc_vec_push(&p->func->labels, &p->func->code.len);
4263
4264 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004265 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004266 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004267 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004268
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004269 if (s) RETURN_STATUS(s);
4270 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004271
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004272 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004273 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004274
4275 bc_parse_push(p, BC_INST_JUMP_ZERO);
4276 bc_parse_pushIndex(p, exit_idx);
4277 bc_parse_push(p, BC_INST_JUMP);
4278 bc_parse_pushIndex(p, body_idx);
4279
4280 ip.idx = p->func->labels.len;
4281
4282 bc_vec_push(&p->conds, &update_idx);
4283 bc_vec_push(&p->func->labels, &p->func->code.len);
4284
4285 if (p->l.t.t != BC_LEX_RPAREN)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004286 s = zbc_parse_expr(p, 0, bc_parse_next_rel);
Gavin Howard01055ba2018-11-03 11:00:21 -06004287 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004288 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004289
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004290 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004291
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004292 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004293 bc_parse_push(p, BC_INST_JUMP);
4294 bc_parse_pushIndex(p, cond_idx);
4295 bc_vec_push(&p->func->labels, &p->func->code.len);
4296
4297 ip.idx = exit_idx;
4298 ip.func = 1;
4299 ip.len = 0;
4300
4301 bc_vec_push(&p->exits, &ip);
4302 bc_vec_push(&p->func->labels, &ip.idx);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004303 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004304 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004305 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4306
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004307 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004308}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004309#if ERRORS_ARE_FATAL
4310# define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__), BC_STATUS_SUCCESS)
4311#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004312
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004313static BC_STATUS zbc_parse_loopExit(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004314{
4315 BcStatus s;
4316 size_t i;
4317 BcInstPtr *ip;
4318
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004319 if (!BC_PARSE_LOOP(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004320
4321 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004322 if (p->exits.len == 0) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004323
4324 i = p->exits.len - 1;
4325 ip = bc_vec_item(&p->exits, i);
4326
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004327 while (!ip->func && i < p->exits.len)
4328 ip = bc_vec_item(&p->exits, i--);
4329 if (i >= p->exits.len && !ip->func)
4330 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004331
4332 i = ip->idx;
4333 }
4334 else
4335 i = *((size_t *) bc_vec_top(&p->conds));
4336
4337 bc_parse_push(p, BC_INST_JUMP);
4338 bc_parse_pushIndex(p, i);
4339
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004340 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004341 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004342
4343 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004344 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004345
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004346 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004347}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004348#if ERRORS_ARE_FATAL
4349# define zbc_parse_loopExit(...) (zbc_parse_loopExit(__VA_ARGS__), BC_STATUS_SUCCESS)
4350#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004351
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004352static BC_STATUS zbc_parse_func(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004353{
4354 BcStatus s;
4355 bool var, comma = false;
4356 uint8_t flags;
4357 char *name;
4358
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004359 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004360 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004361 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004362 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004363
4364 name = xstrdup(p->l.t.v.v);
4365 bc_parse_addFunc(p, name, &p->fidx);
4366
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004367 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004368 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004369 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004370 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004371 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004372 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004373
4374 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004375 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004376 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004377
4378 ++p->func->nparams;
4379
4380 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004381 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004382 if (s) goto err;
4383
4384 var = p->l.t.t != BC_LEX_LBRACKET;
4385
4386 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004387 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004388 if (s) goto err;
4389
4390 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004391 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004392 goto err;
4393 }
4394
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004395 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004396 if (s) goto err;
4397 }
4398
4399 comma = p->l.t.t == BC_LEX_COMMA;
4400 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004401 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004402 if (s) goto err;
4403 }
4404
Denys Vlasenko29301232018-12-11 15:29:32 +01004405 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004406 if (s) goto err;
4407 }
4408
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004409 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004410
4411 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4412 bc_parse_startBody(p, flags);
4413
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004414 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004415 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004416
4417 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004418 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004419
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004420 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004421
4422err:
4423 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004424 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004425}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004426#if ERRORS_ARE_FATAL
4427# define zbc_parse_func(...) (zbc_parse_func(__VA_ARGS__), BC_STATUS_SUCCESS)
4428#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004429
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004430static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004431{
4432 BcStatus s;
4433 bool comma, var, one;
4434 char *name;
4435
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004436 if (!p->auto_part) RETURN_STATUS(bc_error_bad_token());
4437
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004438 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004439 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004440
4441 p->auto_part = comma = false;
4442 one = p->l.t.t == BC_LEX_NAME;
4443
4444 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004445 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004446 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004447 if (s) goto err;
4448
4449 var = p->l.t.t != BC_LEX_LBRACKET;
4450 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004451 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004452 if (s) goto err;
4453
4454 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004455 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004456 goto err;
4457 }
4458
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004459 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004460 if (s) goto err;
4461 }
4462
4463 comma = p->l.t.t == BC_LEX_COMMA;
4464 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004465 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004466 if (s) goto err;
4467 }
4468
Denys Vlasenko29301232018-12-11 15:29:32 +01004469 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004470 if (s) goto err;
4471 }
4472
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004473 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4474 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004475
4476 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004477 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004478
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004479 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004480
4481err:
4482 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004483 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004484}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004485#if ERRORS_ARE_FATAL
4486# define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__), BC_STATUS_SUCCESS)
4487#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004488
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004489static BC_STATUS zbc_parse_body(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004490{
4491 BcStatus s = BC_STATUS_SUCCESS;
4492 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4493
4494 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4495
4496 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004497 if (!brace) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004498
Gavin Howard01055ba2018-11-03 11:00:21 -06004499 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4500
4501 if (!p->auto_part) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004502 s = zbc_parse_auto(p);
4503 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004504 }
4505
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004506 if (p->l.t.t == BC_LEX_NLINE) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004507 }
4508 else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004509 s = zbc_parse_stmt(p);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004510 if (!s && !brace) s = zbc_parse_endBody(p, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004511 }
4512
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004513 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004514}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004515#if ERRORS_ARE_FATAL
4516# define zbc_parse_body(...) (zbc_parse_body(__VA_ARGS__), BC_STATUS_SUCCESS)
4517#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004518
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004519#undef zbc_parse_stmt
4520static BC_STATUS zbc_parse_stmt(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004521{
4522 BcStatus s = BC_STATUS_SUCCESS;
4523
4524 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004525 case BC_LEX_NLINE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004526 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004527
4528 case BC_LEX_KEY_ELSE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004529 p->auto_part = false;
4530 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004531
4532 case BC_LEX_LBRACE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004533 if (!BC_PARSE_BODY(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004534 ++p->nbraces;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004535 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004536 if (s) RETURN_STATUS(s);
4537 RETURN_STATUS(zbc_parse_body(p, true));
Gavin Howard01055ba2018-11-03 11:00:21 -06004538
4539 case BC_LEX_KEY_AUTO:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004540 RETURN_STATUS(zbc_parse_auto(p));
Gavin Howard01055ba2018-11-03 11:00:21 -06004541
4542 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06004543 p->auto_part = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004544 if (BC_PARSE_IF_END(p)) {
4545 bc_parse_noElse(p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004546 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004547 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004548 if (BC_PARSE_BODY(p))
4549 RETURN_STATUS(zbc_parse_body(p, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06004550 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004551 }
4552
4553 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004554 case BC_LEX_OP_INC:
4555 case BC_LEX_OP_DEC:
4556 case BC_LEX_OP_MINUS:
4557 case BC_LEX_OP_BOOL_NOT:
4558 case BC_LEX_LPAREN:
4559 case BC_LEX_NAME:
4560 case BC_LEX_NUMBER:
4561 case BC_LEX_KEY_IBASE:
4562 case BC_LEX_KEY_LAST:
4563 case BC_LEX_KEY_LENGTH:
4564 case BC_LEX_KEY_OBASE:
4565 case BC_LEX_KEY_READ:
4566 case BC_LEX_KEY_SCALE:
4567 case BC_LEX_KEY_SQRT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004568 s = zbc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
Gavin Howard01055ba2018-11-03 11:00:21 -06004569 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004570 case BC_LEX_KEY_ELSE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004571 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004572 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004573 case BC_LEX_SCOLON:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004574 while (!s && p->l.t.t == BC_LEX_SCOLON) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 case BC_LEX_RBRACE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004577 s = zbc_parse_endBody(p, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004578 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004579 case BC_LEX_STR:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004580 s = zbc_parse_string(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004581 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004582 case BC_LEX_KEY_BREAK:
4583 case BC_LEX_KEY_CONTINUE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004584 s = zbc_parse_loopExit(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004585 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004586 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004587 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004588 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004589 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004590 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004591 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004592 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004593 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004594 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004595 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004596 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004597 // "limits" is a compile-time command,
4598 // the output is produced at _parse time_.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004599 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004600 if (s) RETURN_STATUS(s);
Denys Vlasenko64074a12018-12-07 15:50:14 +01004601 printf(
4602 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4603 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4604 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4605 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4606 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4607 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4608 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4609 "Number of vars = "BC_MAX_VARS_STR "\n"
4610 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004611 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004612 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004613 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004614 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004615 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004616 // "quit" is a compile-time command. For example,
4617 // "if (0 == 1) quit" terminates when parsing the statement,
4618 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004619 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004620 case BC_LEX_KEY_RETURN:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004621 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004622 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004623 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004624 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004625 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004626 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004627 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004628 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004629 }
4630
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004631 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004632}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004633#if ERRORS_ARE_FATAL
4634# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
4635#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004636
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01004637static FAST_FUNC BcStatus bc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004638{
4639 BcStatus s;
4640
4641 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004642 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 -06004643 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004644 if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token();
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004645 s = zbc_parse_func(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004646 }
4647 else
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004648 s = zbc_parse_stmt(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004649
Denys Vlasenkod38af482018-12-04 19:11:02 +01004650 if (s || G_interrupt) {
4651 bc_parse_reset(p);
4652 s = BC_STATUS_FAILURE;
4653 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004654
4655 return s;
4656}
4657
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004658// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004659static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004660{
4661 BcStatus s = BC_STATUS_SUCCESS;
4662 BcInst prev = BC_INST_PRINT;
4663 BcLexType top, t = p->l.t.t;
4664 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004665 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004666 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4667
4668 paren_first = p->l.t.t == BC_LEX_LPAREN;
4669 nparens = nrelops = 0;
4670 paren_expr = rprn = done = get_token = assign = false;
4671 bin_last = true;
4672
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004673 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004674 switch (t) {
4675
4676 case BC_LEX_OP_INC:
4677 case BC_LEX_OP_DEC:
4678 {
4679 s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
4680 rprn = get_token = bin_last = false;
4681 break;
4682 }
4683
4684 case BC_LEX_OP_MINUS:
4685 {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004686 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004687 rprn = get_token = false;
4688 bin_last = prev == BC_INST_MINUS;
4689 break;
4690 }
4691
4692 case BC_LEX_OP_ASSIGN_POWER:
4693 case BC_LEX_OP_ASSIGN_MULTIPLY:
4694 case BC_LEX_OP_ASSIGN_DIVIDE:
4695 case BC_LEX_OP_ASSIGN_MODULUS:
4696 case BC_LEX_OP_ASSIGN_PLUS:
4697 case BC_LEX_OP_ASSIGN_MINUS:
4698 case BC_LEX_OP_ASSIGN:
4699 {
4700 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4701 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4702 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4703 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004704 s = bc_error("bad assignment:"
4705 " left side must be scale,"
4706 " ibase, obase, last, var,"
4707 " or array element"
4708 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004709 break;
4710 }
4711 }
4712 // Fallthrough.
4713 case BC_LEX_OP_POWER:
4714 case BC_LEX_OP_MULTIPLY:
4715 case BC_LEX_OP_DIVIDE:
4716 case BC_LEX_OP_MODULUS:
4717 case BC_LEX_OP_PLUS:
4718 case BC_LEX_OP_REL_EQ:
4719 case BC_LEX_OP_REL_LE:
4720 case BC_LEX_OP_REL_GE:
4721 case BC_LEX_OP_REL_NE:
4722 case BC_LEX_OP_REL_LT:
4723 case BC_LEX_OP_REL_GT:
4724 case BC_LEX_OP_BOOL_NOT:
4725 case BC_LEX_OP_BOOL_OR:
4726 case BC_LEX_OP_BOOL_AND:
4727 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004728 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4729 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4730 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004731 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004732 }
4733
4734 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4735 prev = BC_PARSE_TOKEN_INST(t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004736 s = zbc_parse_operator(p, t, ops_bgn, &nexprs, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004737 rprn = get_token = false;
4738 bin_last = t != BC_LEX_OP_BOOL_NOT;
4739
4740 break;
4741 }
4742
4743 case BC_LEX_LPAREN:
4744 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004745 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004746 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004747 ++nparens;
4748 paren_expr = rprn = bin_last = false;
4749 get_token = true;
4750 bc_vec_push(&p->ops, &t);
4751
4752 break;
4753 }
4754
4755 case BC_LEX_RPAREN:
4756 {
4757 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004758 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004759
4760 if (nparens == 0) {
4761 s = BC_STATUS_SUCCESS;
4762 done = true;
4763 get_token = false;
4764 break;
4765 }
4766 else if (!paren_expr)
4767 return BC_STATUS_PARSE_EMPTY_EXP;
4768
4769 --nparens;
4770 paren_expr = rprn = true;
4771 get_token = bin_last = false;
4772
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004773 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004774
4775 break;
4776 }
4777
4778 case BC_LEX_NAME:
4779 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004780 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004781 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004782 paren_expr = true;
4783 rprn = get_token = bin_last = false;
4784 s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
4785 ++nexprs;
4786
4787 break;
4788 }
4789
4790 case BC_LEX_NUMBER:
4791 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004792 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004793 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004794 bc_parse_number(p, &prev, &nexprs);
4795 paren_expr = get_token = true;
4796 rprn = bin_last = false;
4797
4798 break;
4799 }
4800
4801 case BC_LEX_KEY_IBASE:
4802 case BC_LEX_KEY_LAST:
4803 case BC_LEX_KEY_OBASE:
4804 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004805 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004806 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004807 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4808 bc_parse_push(p, (char) prev);
4809
4810 paren_expr = get_token = true;
4811 rprn = bin_last = false;
4812 ++nexprs;
4813
4814 break;
4815 }
4816
4817 case BC_LEX_KEY_LENGTH:
4818 case BC_LEX_KEY_SQRT:
4819 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004820 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004821 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004822 s = bc_parse_builtin(p, t, flags, &prev);
4823 paren_expr = true;
4824 rprn = get_token = bin_last = false;
4825 ++nexprs;
4826
4827 break;
4828 }
4829
4830 case BC_LEX_KEY_READ:
4831 {
4832 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004833 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004834 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004835 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004836 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004837 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004838
4839 paren_expr = true;
4840 rprn = get_token = bin_last = false;
4841 ++nexprs;
4842 prev = BC_INST_READ;
4843
4844 break;
4845 }
4846
4847 case BC_LEX_KEY_SCALE:
4848 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004849 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004850 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004851 s = bc_parse_scale(p, &prev, flags);
4852 paren_expr = true;
4853 rprn = get_token = bin_last = false;
4854 ++nexprs;
4855 prev = BC_INST_SCALE;
4856
4857 break;
4858 }
4859
4860 default:
4861 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004862 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004863 break;
4864 }
4865 }
4866
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004867 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004868 }
4869
4870 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004871 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004872
4873 while (p->ops.len > ops_bgn) {
4874
4875 top = BC_PARSE_TOP_OP(p);
4876 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4877
4878 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004879 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004880
4881 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
4882
4883 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4884 bc_vec_pop(&p->ops);
4885 }
4886
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004887 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004888 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004889
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004890 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
4891 for (;;) {
4892 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004893 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004894 if (next & 0x80) // last element?
4895 break;
4896 next >>= 8;
4897 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004898 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004899 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06004900
4901 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004902 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004903 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004904 }
4905 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004906 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004907 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004908 }
4909
4910 if (flags & BC_PARSE_PRINT) {
4911 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4912 bc_parse_push(p, BC_INST_POP);
4913 }
4914
4915 return s;
4916}
4917
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004918#undef zbc_parse_expr
4919static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004920{
4921 BcStatus s;
4922
4923 s = bc_parse_expr_empty_ok(p, flags, next);
4924 if (s == BC_STATUS_PARSE_EMPTY_EXP)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004925 RETURN_STATUS(bc_error("empty expression"));
4926 RETURN_STATUS(s);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004927}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004928#if ERRORS_ARE_FATAL
4929# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
4930#endif
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004931
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004932static void bc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06004933{
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004934 bc_parse_create(p, func, bc_parse_parse, zbc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06004935}
4936
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004937static BC_STATUS zbc_parse_expression(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004938{
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004939 RETURN_STATUS(zbc_parse_expr(p, flags, bc_parse_next_read));
Gavin Howard01055ba2018-11-03 11:00:21 -06004940}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004941#if ERRORS_ARE_FATAL
4942# define zbc_parse_expression(...) (zbc_parse_expression(__VA_ARGS__), BC_STATUS_SUCCESS)
4943#endif
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004944
Gavin Howard01055ba2018-11-03 11:00:21 -06004945#endif // ENABLE_BC
4946
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004947#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004948
4949#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
4950
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004951static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004952{
4953 BcStatus s;
4954 char *name;
4955
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004956 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004957 if (s) RETURN_STATUS(s);
4958 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004959
4960 name = xstrdup(p->l.t.v.v);
4961 bc_parse_pushName(p, name);
4962
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004963 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004964}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004965#if ERRORS_ARE_FATAL
4966# define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__), BC_STATUS_SUCCESS)
4967#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004968
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004969static BC_STATUS zdc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004970{
4971 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004972 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004973
4974 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
4975 name = xstrdup(b);
4976
4977 str = xstrdup(p->l.t.v.v);
4978 bc_parse_push(p, BC_INST_STR);
4979 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004980 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004981 bc_parse_addFunc(p, name, &idx);
4982
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004983 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004984}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004985#if ERRORS_ARE_FATAL
4986# define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
4987#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004988
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004989static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004990{
4991 BcStatus s;
4992
4993 bc_parse_push(p, inst);
4994 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004995 s = zdc_parse_register(p);
4996 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004997 }
4998
4999 if (store) {
5000 bc_parse_push(p, BC_INST_SWAP);
5001 bc_parse_push(p, BC_INST_ASSIGN);
5002 bc_parse_push(p, BC_INST_POP);
5003 }
5004
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005005 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06005006}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005007#if ERRORS_ARE_FATAL
5008# define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__), BC_STATUS_SUCCESS)
5009#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005010
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005011static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005012{
5013 BcStatus s;
5014
5015 bc_parse_push(p, inst);
5016 bc_parse_push(p, BC_INST_EXEC_COND);
5017
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005018 s = zdc_parse_register(p);
5019 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005020
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005021 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005022 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005023
5024 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005025 s = zdc_parse_register(p);
5026 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005027 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005028 }
5029 else
5030 bc_parse_push(p, BC_PARSE_STREND);
5031
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005032 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005033}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005034#if ERRORS_ARE_FATAL
5035# define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__), BC_STATUS_SUCCESS)
5036#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005037
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005038static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005039{
5040 BcStatus s = BC_STATUS_SUCCESS;
5041 BcInst prev;
5042 uint8_t inst;
5043 bool assign, get_token = false;
5044
5045 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005046 case BC_LEX_OP_REL_EQ:
5047 case BC_LEX_OP_REL_LE:
5048 case BC_LEX_OP_REL_GE:
5049 case BC_LEX_OP_REL_NE:
5050 case BC_LEX_OP_REL_LT:
5051 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005052 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005053 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005054 case BC_LEX_SCOLON:
5055 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005056 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06005057 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005058 case BC_LEX_STR:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005059 s = zdc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06005060 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005061 case BC_LEX_NEG:
5062 case BC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06005063 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005064 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005065 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005066 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005067 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005068 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005069 bc_parse_number(p, &prev, &p->nbraces);
Gavin Howard01055ba2018-11-03 11:00:21 -06005070 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5071 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005072 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005073 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06005074 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005075 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005076 else
5077 bc_parse_push(p, BC_INST_READ);
5078 get_token = true;
5079 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005080 case BC_LEX_OP_ASSIGN:
5081 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06005082 assign = t == BC_LEX_OP_ASSIGN;
5083 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005084 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005085 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005086 case BC_LEX_LOAD:
5087 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06005088 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005089 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005090 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005091 case BC_LEX_STORE_IBASE:
5092 case BC_LEX_STORE_SCALE:
5093 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005094 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005095 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005096 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005097 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005098 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005099 get_token = true;
5100 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005101 }
5102
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005103 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005104
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005105 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005106}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005107#if ERRORS_ARE_FATAL
5108# define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__), BC_STATUS_SUCCESS)
5109#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005110
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005111static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005112{
5113 BcStatus s = BC_STATUS_SUCCESS;
5114 BcInst inst;
5115 BcLexType t;
5116
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005117 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005118
5119 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005120 inst = dc_parse_insts[t];
5121
5122 if (inst != BC_INST_INVALID) {
5123 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005124 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005125 } else
5126 s = zdc_parse_token(p, t, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06005127 }
5128
5129 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5130 bc_parse_push(p, BC_INST_POP_EXEC);
5131
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005132 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005133}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005134#if ERRORS_ARE_FATAL
5135# define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5136#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005137
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01005138static FAST_FUNC BcStatus dc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005139{
5140 BcStatus s;
5141
5142 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005143 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005144 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005145 s = zdc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005146
Denys Vlasenkod38af482018-12-04 19:11:02 +01005147 if (s || G_interrupt) {
5148 bc_parse_reset(p);
5149 s = BC_STATUS_FAILURE;
5150 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005151
5152 return s;
5153}
5154
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005155static void dc_parse_init(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06005156{
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005157 bc_parse_create(p, func, dc_parse_parse, zdc_lex_token);
Gavin Howard01055ba2018-11-03 11:00:21 -06005158}
Denys Vlasenkocca79a02018-12-05 21:15:46 +01005159
Gavin Howard01055ba2018-11-03 11:00:21 -06005160#endif // ENABLE_DC
5161
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005162static void common_parse_init(BcParse *p, size_t func)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005163{
5164 if (IS_BC) {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005165 IF_BC(bc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005166 } else {
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01005167 IF_DC(dc_parse_init(p, func);)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005168 }
5169}
5170
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005171static BC_STATUS zcommon_parse_expr(BcParse *p, uint8_t flags)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005172{
5173 if (IS_BC) {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005174 IF_BC(RETURN_STATUS(zbc_parse_expression(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005175 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005176 IF_DC(RETURN_STATUS(zdc_parse_expr(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005177 }
5178}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005179#if ERRORS_ARE_FATAL
5180# define zcommon_parse_expr(...) (zcommon_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5181#endif
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005182
Denys Vlasenkodf515392018-12-02 19:27:48 +01005183static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005184{
Gavin Howard01055ba2018-11-03 11:00:21 -06005185 BcId e, *ptr;
5186 BcVec *v, *map;
5187 size_t i;
5188 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005189 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005190
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005191 v = var ? &G.prog.vars : &G.prog.arrs;
5192 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005193
5194 e.name = id;
5195 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005196 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005197
5198 if (new) {
5199 bc_array_init(&data.v, var);
5200 bc_vec_push(v, &data.v);
5201 }
5202
5203 ptr = bc_vec_item(map, i);
5204 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005205 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005206}
5207
Denys Vlasenko29301232018-12-11 15:29:32 +01005208static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005209{
Gavin Howard01055ba2018-11-03 11:00:21 -06005210 switch (r->t) {
5211
5212 case BC_RESULT_STR:
5213 case BC_RESULT_TEMP:
5214 case BC_RESULT_IBASE:
5215 case BC_RESULT_SCALE:
5216 case BC_RESULT_OBASE:
5217 {
5218 *num = &r->d.n;
5219 break;
5220 }
5221
5222 case BC_RESULT_CONSTANT:
5223 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005224 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005225 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005226 size_t base_t, len = strlen(*str);
5227 BcNum *base;
5228
5229 bc_num_init(&r->d.n, len);
5230
5231 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005232 base = hex ? &G.prog.hexb : &G.prog.ib;
5233 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005234 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005235
5236 if (s) {
5237 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005238 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005239 }
5240
5241 *num = &r->d.n;
5242 r->t = BC_RESULT_TEMP;
5243
5244 break;
5245 }
5246
5247 case BC_RESULT_VAR:
5248 case BC_RESULT_ARRAY:
5249 case BC_RESULT_ARRAY_ELEM:
5250 {
5251 BcVec *v;
5252
Denys Vlasenkodf515392018-12-02 19:27:48 +01005253 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005254
5255 if (r->t == BC_RESULT_ARRAY_ELEM) {
5256 v = bc_vec_top(v);
5257 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5258 *num = bc_vec_item(v, r->d.id.idx);
5259 }
5260 else
5261 *num = bc_vec_top(v);
5262
5263 break;
5264 }
5265
5266 case BC_RESULT_LAST:
5267 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005268 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005269 break;
5270 }
5271
5272 case BC_RESULT_ONE:
5273 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005274 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005275 break;
5276 }
5277 }
5278
Denys Vlasenko29301232018-12-11 15:29:32 +01005279 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005280}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005281#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005282# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005283#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005284
Denys Vlasenko29301232018-12-11 15:29:32 +01005285static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005286 BcResult **r, BcNum **rn, bool assign)
5287{
5288 BcStatus s;
5289 bool hex;
5290 BcResultType lt, rt;
5291
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005292 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005293 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005294
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005295 *r = bc_vec_item_rev(&G.prog.results, 0);
5296 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005297
5298 lt = (*l)->t;
5299 rt = (*r)->t;
5300 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5301
Denys Vlasenko29301232018-12-11 15:29:32 +01005302 s = zbc_program_num(*l, ln, false);
5303 if (s) RETURN_STATUS(s);
5304 s = zbc_program_num(*r, rn, hex);
5305 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005306
5307 // We run this again under these conditions in case any vector has been
5308 // reallocated out from under the BcNums or arrays we had.
5309 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005310 s = zbc_program_num(*l, ln, false);
5311 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005312 }
5313
5314 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005315 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005316 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005317 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005318
Denys Vlasenko29301232018-12-11 15:29:32 +01005319 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005320}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005321#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005322# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005323#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005324
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005325static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005326{
5327 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005328 bc_vec_pop(&G.prog.results);
5329 bc_vec_pop(&G.prog.results);
5330 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005331}
5332
Denys Vlasenko29301232018-12-11 15:29:32 +01005333static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005334{
5335 BcStatus s;
5336
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005337 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005338 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005339 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005340
Denys Vlasenko29301232018-12-11 15:29:32 +01005341 s = zbc_program_num(*r, n, false);
5342 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005343
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005344 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005345 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005346
Denys Vlasenko29301232018-12-11 15:29:32 +01005347 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005348}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005349#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005350# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005351#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005352
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005353static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005354{
5355 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005356 bc_vec_pop(&G.prog.results);
5357 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005358}
5359
Denys Vlasenko259137d2018-12-11 19:42:05 +01005360static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005361{
5362 BcStatus s;
5363 BcResult *opd1, *opd2, res;
5364 BcNum *n1, *n2 = NULL;
5365
Denys Vlasenko29301232018-12-11 15:29:32 +01005366 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005367 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005368 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005369
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005370 s = BC_STATUS_SUCCESS;
5371#if !ERRORS_ARE_FATAL
5372 s =
5373#endif
5374 zbc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005375 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005376 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005377
Denys Vlasenko259137d2018-12-11 19:42:05 +01005378 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005379
5380err:
5381 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005382 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005383}
Denys Vlasenko259137d2018-12-11 19:42:05 +01005384#if ERRORS_ARE_FATAL
5385# define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS)
5386#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005387
Denys Vlasenko785e4b32018-12-02 17:18:52 +01005388static BcStatus bc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005389{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005390 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005391 BcStatus s;
5392 BcParse parse;
5393 BcVec buf;
5394 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005395 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005396
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005397 if (G.in_read)
5398 return bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005399
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005400 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005401 bc_vec_pop_all(&f->code);
5402 bc_char_vec_init(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005403
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005404 sv_file = G.prog.file;
5405 G.prog.file = NULL;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005406 G.in_read = 1;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005407
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01005408 s = bc_read_line(&buf);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005409 //if (s) goto io_err; - wrong, nonzero return means EOF, not error
Gavin Howard01055ba2018-11-03 11:00:21 -06005410
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005411 common_parse_init(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005412 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005413
5414 s = bc_parse_text(&parse, buf.v);
5415 if (s) goto exec_err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005416 s = zcommon_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005417 if (s) goto exec_err;
5418
5419 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005420 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005421 goto exec_err;
5422 }
5423
5424 ip.func = BC_PROG_READ;
5425 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005426 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005427
5428 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005429 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005430
5431 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005432 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005433
5434exec_err:
5435 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005436//io_err:
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005437 G.in_read = 0;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005438 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005439 bc_vec_free(&buf);
5440 return s;
5441}
5442
5443static size_t bc_program_index(char *code, size_t *bgn)
5444{
5445 char amt = code[(*bgn)++], i = 0;
5446 size_t res = 0;
5447
5448 for (; i < amt; ++i, ++(*bgn))
5449 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5450
5451 return res;
5452}
5453
5454static char *bc_program_name(char *code, size_t *bgn)
5455{
5456 size_t i;
5457 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5458
5459 s = xmalloc(ptr - str + 1);
5460 c = code[(*bgn)++];
5461
5462 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5463 s[i] = c;
5464
5465 s[i] = '\0';
5466
5467 return s;
5468}
5469
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005470static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005471{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005472#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005473 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005474 // Example: echo '[]ap' | dc
5475 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005476 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005477 return;
5478 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005479#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005480 while (*str) {
5481 int c = *str++;
5482 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005483 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005484 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005485 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005486 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005487 case 'a':
5488 bb_putchar('\a');
5489 break;
5490 case 'b':
5491 bb_putchar('\b');
5492 break;
5493 case '\\':
5494 case 'e':
5495 bb_putchar('\\');
5496 break;
5497 case 'f':
5498 bb_putchar('\f');
5499 break;
5500 case 'n':
5501 bb_putchar('\n');
5502 G.prog.nchars = SIZE_MAX;
5503 break;
5504 case 'r':
5505 bb_putchar('\r');
5506 break;
5507 case 'q':
5508 bb_putchar('"');
5509 break;
5510 case 't':
5511 bb_putchar('\t');
5512 break;
5513 default:
5514 // Just print the backslash and following character.
5515 bb_putchar('\\');
5516 ++G.prog.nchars;
5517 bb_putchar(c);
5518 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005519 }
5520 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005521 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005522 }
5523}
5524
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005525static void bc_num_printNewline(void)
5526{
5527 if (G.prog.nchars == G.prog.len - 1) {
5528 bb_putchar('\\');
5529 bb_putchar('\n');
5530 G.prog.nchars = 0;
5531 }
5532}
5533
5534#if ENABLE_DC
5535static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
5536{
5537 (void) radix;
5538 bb_putchar((char) num);
5539 G.prog.nchars += width;
5540}
5541#endif
5542
5543static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5544{
5545 size_t exp, pow;
5546
5547 bc_num_printNewline();
5548 bb_putchar(radix ? '.' : ' ');
5549 ++G.prog.nchars;
5550
5551 bc_num_printNewline();
5552 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5553 continue;
5554
5555 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5556 size_t dig;
5557 bc_num_printNewline();
5558 dig = num / pow;
5559 num -= dig * pow;
5560 bb_putchar(((char) dig) + '0');
5561 }
5562}
5563
5564static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5565{
5566 if (radix) {
5567 bc_num_printNewline();
5568 bb_putchar('.');
5569 G.prog.nchars += 1;
5570 }
5571
5572 bc_num_printNewline();
5573 bb_putchar(bb_hexdigits_upcase[num]);
5574 G.prog.nchars += width;
5575}
5576
5577static void bc_num_printDecimal(BcNum *n)
5578{
5579 size_t i, rdx = n->rdx - 1;
5580
5581 if (n->neg) bb_putchar('-');
5582 G.prog.nchars += n->neg;
5583
5584 for (i = n->len - 1; i < n->len; --i)
5585 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5586}
5587
5588static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
5589{
5590 BcStatus s;
5591 BcVec stack;
5592 BcNum intp, fracp, digit, frac_len;
5593 unsigned long dig, *ptr;
5594 size_t i;
5595 bool radix;
5596
5597 if (n->len == 0) {
5598 print(0, width, false);
5599 RETURN_STATUS(BC_STATUS_SUCCESS);
5600 }
5601
5602 bc_vec_init(&stack, sizeof(long), NULL);
5603 bc_num_init(&intp, n->len);
5604 bc_num_init(&fracp, n->rdx);
5605 bc_num_init(&digit, width);
5606 bc_num_init(&frac_len, BC_NUM_INT(n));
5607 bc_num_copy(&intp, n);
5608 bc_num_one(&frac_len);
5609
5610 bc_num_truncate(&intp, intp.rdx);
5611 s = zbc_num_sub(n, &intp, &fracp, 0);
5612 if (s) goto err;
5613
5614 while (intp.len != 0) {
5615 s = zbc_num_divmod(&intp, base, &intp, &digit, 0);
5616 if (s) goto err;
5617 s = zbc_num_ulong(&digit, &dig);
5618 if (s) goto err;
5619 bc_vec_push(&stack, &dig);
5620 }
5621
5622 for (i = 0; i < stack.len; ++i) {
5623 ptr = bc_vec_item_rev(&stack, i);
5624 print(*ptr, width, false);
5625 }
5626
5627 if (!n->rdx) goto err;
5628
5629 for (radix = true; frac_len.len <= n->rdx; radix = false) {
5630 s = zbc_num_mul(&fracp, base, &fracp, n->rdx);
5631 if (s) goto err;
5632 s = zbc_num_ulong(&fracp, &dig);
5633 if (s) goto err;
5634 bc_num_ulong2num(&intp, dig);
5635 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5636 if (s) goto err;
5637 print(dig, width, radix);
5638 s = zbc_num_mul(&frac_len, base, &frac_len, 0);
5639 if (s) goto err;
5640 }
5641
5642err:
5643 bc_num_free(&frac_len);
5644 bc_num_free(&digit);
5645 bc_num_free(&fracp);
5646 bc_num_free(&intp);
5647 bc_vec_free(&stack);
5648 RETURN_STATUS(s);
5649}
5650#if ERRORS_ARE_FATAL
5651# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
5652#endif
5653
5654static BC_STATUS zbc_num_printBase(BcNum *n)
5655{
5656 BcStatus s;
5657 size_t width, i;
5658 BcNumDigitOp print;
5659 bool neg = n->neg;
5660
5661 if (neg) {
5662 bb_putchar('-');
5663 G.prog.nchars++;
5664 }
5665
5666 n->neg = false;
5667
5668 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5669 width = 1;
5670 print = bc_num_printHex;
5671 }
5672 else {
5673 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
5674 continue;
5675 print = bc_num_printDigits;
5676 }
5677
5678 s = zbc_num_printNum(n, &G.prog.ob, width, print);
5679 n->neg = neg;
5680
5681 RETURN_STATUS(s);
5682}
5683#if ERRORS_ARE_FATAL
5684# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
5685#endif
5686
5687#if ENABLE_DC
5688static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
5689{
5690 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
5691}
5692#if ERRORS_ARE_FATAL
5693# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
5694#endif
5695#endif
5696
5697static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5698{
5699 BcStatus s = BC_STATUS_SUCCESS;
5700
5701 bc_num_printNewline();
5702
5703 if (n->len == 0) {
5704 bb_putchar('0');
5705 ++G.prog.nchars;
5706 }
5707 else if (G.prog.ob_t == 10)
5708 bc_num_printDecimal(n);
5709 else
5710 s = zbc_num_printBase(n);
5711
5712 if (newline) {
5713 bb_putchar('\n');
5714 G.prog.nchars = 0;
5715 }
5716
5717 RETURN_STATUS(s);
5718}
5719#if ERRORS_ARE_FATAL
5720# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
5721#endif
5722
Denys Vlasenko29301232018-12-11 15:29:32 +01005723static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005724{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005725 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005726 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005727 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005728 bool pop = inst != BC_INST_PRINT;
5729
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005730 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005731 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005732
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005733 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005734 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005735 s = zbc_program_num(r, &num, false);
5736 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005737
5738 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005739 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005740 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005741 }
5742 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005743 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005744
5745 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005746 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005747
5748 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005749 for (;;) {
5750 char c = *str++;
5751 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005752 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005753 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005754 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005755 }
5756 }
5757 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005758 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005759 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005760 }
5761 }
5762
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005763 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005764
Denys Vlasenko29301232018-12-11 15:29:32 +01005765 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005766}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005767#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005768# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005769#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005770
Denys Vlasenko29301232018-12-11 15:29:32 +01005771static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005772{
5773 BcStatus s;
5774 BcResult res, *ptr;
5775 BcNum *num = NULL;
5776
Denys Vlasenko29301232018-12-11 15:29:32 +01005777 s = zbc_program_prep(&ptr, &num);
5778 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005779
5780 bc_num_init(&res.d.n, num->len);
5781 bc_num_copy(&res.d.n, num);
5782 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5783
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005784 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005785
Denys Vlasenko29301232018-12-11 15:29:32 +01005786 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005787}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005788#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005789# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005790#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005791
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005792static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005793{
5794 BcStatus s;
5795 BcResult *opd1, *opd2, res;
5796 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005797 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005798
Denys Vlasenko29301232018-12-11 15:29:32 +01005799 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005800 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005801
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005802 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005803
5804 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005805 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005806 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005807 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005808 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005809 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005810 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005811 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005812 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005813 break;
5814 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005815 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005816 break;
5817 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005818 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005819 break;
5820 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005821 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005822 break;
5823 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005824 cond = (cond > 0);
5825 break;
5826 default: // = case BC_INST_REL_NE:
5827 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005828 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005829 }
5830 }
5831
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005832 if (cond) bc_num_one(&res.d.n);
5833 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005834
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005835 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005836
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005837 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005838}
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005839#if ERRORS_ARE_FATAL
5840# define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS)
5841#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005842
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005843#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005844static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005845 bool push)
5846{
5847 BcNum n2;
5848 BcResult res;
5849
5850 memset(&n2, 0, sizeof(BcNum));
5851 n2.rdx = res.d.id.idx = r->d.id.idx;
5852 res.t = BC_RESULT_STR;
5853
5854 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005855 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005856 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005857 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005858 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005859 }
5860
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005861 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005862
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005863 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005864 bc_vec_push(v, &n2);
5865
Denys Vlasenko29301232018-12-11 15:29:32 +01005866 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005867}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005868#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005869# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005870#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005871#endif // ENABLE_DC
5872
Denys Vlasenko29301232018-12-11 15:29:32 +01005873static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005874{
5875 BcStatus s;
5876 BcResult *ptr, r;
5877 BcVec *v;
5878 BcNum *n;
5879
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005880 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005881 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005882
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005883 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005884 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005885 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005886 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005887
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005888#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005889 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005890 RETURN_STATUS(bc_error_variable_is_wrong_type());
5891 if (ptr->t == BC_RESULT_STR)
5892 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005893#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005894
Denys Vlasenko29301232018-12-11 15:29:32 +01005895 s = zbc_program_num(ptr, &n, false);
5896 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005897
5898 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005899 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005900
5901 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005902 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005903 bc_num_copy(&r.d.n, n);
5904 }
5905 else {
5906 bc_array_init(&r.d.v, true);
5907 bc_array_copy(&r.d.v, (BcVec *) n);
5908 }
5909
5910 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005911 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005912
Denys Vlasenko29301232018-12-11 15:29:32 +01005913 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005914}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005915#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005916# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005917#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005918
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005919static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005920{
5921 BcStatus s;
5922 BcResult *left, *right, res;
5923 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005924 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5925
Denys Vlasenko29301232018-12-11 15:29:32 +01005926 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005927 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005928
5929 ib = left->t == BC_RESULT_IBASE;
5930 sc = left->t == BC_RESULT_SCALE;
5931
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005932#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005933
5934 if (right->t == BC_RESULT_STR) {
5935
5936 BcVec *v;
5937
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005938 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005939 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005940 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005941
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005942 RETURN_STATUS(zbc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005943 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005944#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005945
5946 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005947 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005948 " left side must be scale,"
5949 " ibase, obase, last, var,"
5950 " or array element"
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005951 ));
Gavin Howard01055ba2018-11-03 11:00:21 -06005952
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005953#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005954 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005955 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005956
5957 if (assign)
5958 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005959 else {
5960 s = BC_STATUS_SUCCESS;
5961#if !ERRORS_ARE_FATAL
5962 s =
5963#endif
5964 zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
5965 }
5966 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005967#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005968 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005969#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005970
5971 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005972 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005973 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5974 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5975 NULL, //can't happen //BC_RESULT_LAST
5976 NULL, //can't happen //BC_RESULT_CONSTANT
5977 NULL, //can't happen //BC_RESULT_ONE
5978 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005979 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005980 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01005981 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06005982
Denys Vlasenko29301232018-12-11 15:29:32 +01005983 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005984 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005985 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005986 if (sc) {
5987 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005988 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06005989 }
5990 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005991 if (val < BC_NUM_MIN_BASE)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005992 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005993 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005994 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005995 }
5996
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005997 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005998 RETURN_STATUS(bc_error(msg[s]));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005999 if (!sc)
6000 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06006001
6002 *ptr = (size_t) val;
6003 s = BC_STATUS_SUCCESS;
6004 }
6005
6006 bc_num_init(&res.d.n, l->len);
6007 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006008 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006009
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006010 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006011}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006012#if ERRORS_ARE_FATAL
6013# define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS)
6014#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006015
Denys Vlasenko416ce762018-12-02 20:57:17 +01006016#if !ENABLE_DC
6017#define bc_program_pushVar(code, bgn, pop, copy) \
6018 bc_program_pushVar(code, bgn)
6019// for bc, 'pop' and 'copy' are always false
6020#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006021static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006022 bool pop, bool copy)
6023{
Gavin Howard01055ba2018-11-03 11:00:21 -06006024 BcResult r;
6025 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06006026
6027 r.t = BC_RESULT_VAR;
6028 r.d.id.name = name;
6029
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006030#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006031 {
6032 BcVec *v = bc_program_search(name, true);
6033 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006034
Denys Vlasenko416ce762018-12-02 20:57:17 +01006035 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006036
Denys Vlasenko416ce762018-12-02 20:57:17 +01006037 if (!BC_PROG_STACK(v, 2 - copy)) {
6038 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006039 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006040 }
6041
Gavin Howard01055ba2018-11-03 11:00:21 -06006042 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006043 name = NULL;
6044
6045 if (!BC_PROG_STR(num)) {
6046
6047 r.t = BC_RESULT_TEMP;
6048
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006049 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006050 bc_num_copy(&r.d.n, num);
6051 }
6052 else {
6053 r.t = BC_RESULT_STR;
6054 r.d.id.idx = num->rdx;
6055 }
6056
6057 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006058 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006059 }
6060#endif // ENABLE_DC
6061
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006062 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006063
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006064 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006065}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006066#if ERRORS_ARE_FATAL
6067# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6068#else
6069# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6070#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006071
Denys Vlasenko29301232018-12-11 15:29:32 +01006072static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006073 char inst)
6074{
6075 BcStatus s = BC_STATUS_SUCCESS;
6076 BcResult r;
6077 BcNum *num;
6078
6079 r.d.id.name = bc_program_name(code, bgn);
6080
6081 if (inst == BC_INST_ARRAY) {
6082 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006083 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006084 }
6085 else {
6086
6087 BcResult *operand;
6088 unsigned long temp;
6089
Denys Vlasenko29301232018-12-11 15:29:32 +01006090 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006091 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006092 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006093 if (s) goto err;
6094
6095 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006096 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006097 goto err;
6098 }
6099
6100 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006101 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006102 }
6103
6104err:
6105 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006106 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006107}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006108#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006109# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006110#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006111
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006112#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006113static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006114{
6115 BcStatus s;
6116 BcResult *ptr, res, copy;
6117 BcNum *num = NULL;
6118 char inst2 = inst;
6119
Denys Vlasenko29301232018-12-11 15:29:32 +01006120 s = zbc_program_prep(&ptr, &num);
6121 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006122
6123 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6124 copy.t = BC_RESULT_TEMP;
6125 bc_num_init(&copy.d.n, num->len);
6126 bc_num_copy(&copy.d.n, num);
6127 }
6128
6129 res.t = BC_RESULT_ONE;
6130 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6131 BC_INST_ASSIGN_PLUS :
6132 BC_INST_ASSIGN_MINUS;
6133
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006134 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006135 s = zbc_program_assign(inst);
6136 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006137
6138 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006139 bc_vec_pop(&G.prog.results);
6140 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006141 }
6142
Denys Vlasenko29301232018-12-11 15:29:32 +01006143 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006144}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006145#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006146# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006147#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006148
Denys Vlasenko29301232018-12-11 15:29:32 +01006149static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006150{
Gavin Howard01055ba2018-11-03 11:00:21 -06006151 BcInstPtr ip;
6152 size_t i, nparams = bc_program_index(code, idx);
6153 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006154 BcId *a;
6155 BcResultData param;
6156 BcResult *arg;
6157
6158 ip.idx = 0;
6159 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006160 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006161
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006162 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006163 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006164 }
6165 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006166 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006167 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006168 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006169
6170 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006171 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006172
6173 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006174 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006175
6176 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006177 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006178
Denys Vlasenko29301232018-12-11 15:29:32 +01006179 s = zbc_program_copyToVar(a->name, a->idx);
6180 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006181 }
6182
6183 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006184 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006185
6186 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006187 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006188
6189 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006190 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006191 bc_vec_push(v, &param.n);
6192 }
6193 else {
6194 bc_array_init(&param.v, true);
6195 bc_vec_push(v, &param.v);
6196 }
6197 }
6198
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006199 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006200
Denys Vlasenko29301232018-12-11 15:29:32 +01006201 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006202}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006203#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006204# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006205#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006206
Denys Vlasenko29301232018-12-11 15:29:32 +01006207static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006208{
Gavin Howard01055ba2018-11-03 11:00:21 -06006209 BcResult res;
6210 BcFunc *f;
6211 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006212 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006213
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006214 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006215 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006216
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006217 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006218 res.t = BC_RESULT_TEMP;
6219
6220 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006221 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006222 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006223 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006224
Denys Vlasenko29301232018-12-11 15:29:32 +01006225 s = zbc_program_num(operand, &num, false);
6226 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006227 bc_num_init(&res.d.n, num->len);
6228 bc_num_copy(&res.d.n, num);
6229 }
6230 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006231 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006232 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006233 }
6234
6235 // We need to pop arguments as well, so this takes that into account.
6236 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006237 BcVec *v;
6238 BcId *a = bc_vec_item(&f->autos, i);
6239
Denys Vlasenkodf515392018-12-02 19:27:48 +01006240 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006241 bc_vec_pop(v);
6242 }
6243
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006244 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6245 bc_vec_push(&G.prog.results, &res);
6246 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006247
Denys Vlasenko29301232018-12-11 15:29:32 +01006248 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006249}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006250#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006251# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006252#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006253#endif // ENABLE_BC
6254
6255static unsigned long bc_program_scale(BcNum *n)
6256{
6257 return (unsigned long) n->rdx;
6258}
6259
6260static unsigned long bc_program_len(BcNum *n)
6261{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006262 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006263
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006264 if (n->rdx != len) return len;
6265 for (;;) {
6266 if (len == 0) break;
6267 len--;
6268 if (n->num[len] != 0) break;
6269 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006270 return len;
6271}
6272
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006273static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006274{
6275 BcStatus s;
6276 BcResult *opnd;
6277 BcNum *num = NULL;
6278 BcResult res;
6279 bool len = inst == BC_INST_LENGTH;
6280
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006281 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006282 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006283 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006284
Denys Vlasenko29301232018-12-11 15:29:32 +01006285 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006286 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006287
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006288#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006289 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006290 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006291#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006292
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006293 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006294
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006295 if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006296#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006297 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006298 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006299 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006300#endif
6301#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006302 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006303 char **str;
6304 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6305
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006306 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006307 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006308 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006309#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006310 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006311 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006312 }
6313
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006314 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006315
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006316 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006317}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006318#if ERRORS_ARE_FATAL
6319# define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
6320#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006321
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006322#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006323static BC_STATUS zbc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006324{
6325 BcStatus s;
6326 BcResult *opd1, *opd2, res, res2;
6327 BcNum *n1, *n2 = NULL;
6328
Denys Vlasenko29301232018-12-11 15:29:32 +01006329 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006330 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006331
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006332 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006333 bc_num_init(&res2.d.n, n2->len);
6334
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006335 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006336 if (s) goto err;
6337
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006338 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006339 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006340 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006341
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006342 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006343
6344err:
6345 bc_num_free(&res2.d.n);
6346 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006347 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006348}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006349#if ERRORS_ARE_FATAL
6350# define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
6351#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006352
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006353static BC_STATUS zbc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006354{
6355 BcStatus s;
6356 BcResult *r1, *r2, *r3, res;
6357 BcNum *n1, *n2, *n3;
6358
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006359 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006360 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006361 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006362 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006363
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006364 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006365 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006366 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006367 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006368 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006369
6370 // Make sure that the values have their pointers updated, if necessary.
6371 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6372
6373 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006374 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006375 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006376 }
6377
6378 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006379 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006380 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006381 }
6382 }
6383
6384 bc_num_init(&res.d.n, n3->len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006385 s = zbc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006386 if (s) goto err;
6387
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006388 bc_vec_pop(&G.prog.results);
6389 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006390
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006391 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006392
6393err:
6394 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006395 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006396}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006397#if ERRORS_ARE_FATAL
6398# define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
6399#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006400
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006401static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006402{
Gavin Howard01055ba2018-11-03 11:00:21 -06006403 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006404 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006405
6406 res.t = BC_RESULT_TEMP;
6407
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006408 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006409 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006410 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006411}
6412
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006413static BC_STATUS zbc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006414{
6415 BcStatus s;
6416 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006417 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006418 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006419 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006420 unsigned long val;
6421
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006422 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006423 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006424 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006425
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006426 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006427 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006428 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006429
6430 if (BC_PROG_NUM(r, num)) {
6431
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006432 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006433 bc_num_copy(&n, num);
6434 bc_num_truncate(&n, n.rdx);
6435
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006436 s = zbc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006437 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006438 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006439 if (s) goto num_err;
6440
6441 c = (char) val;
6442
6443 bc_num_free(&n);
6444 }
6445 else {
6446 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006447 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006448 c = str2[0];
6449 }
6450
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006451 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006452 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006453 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006454
6455 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006456 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006457
6458 if (idx != len + BC_PROG_REQ_FUNCS) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006459 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006460 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006461 len = idx;
6462 break;
6463 }
6464 }
6465
6466 free(str);
6467 }
6468 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006469 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006470
6471 res.t = BC_RESULT_STR;
6472 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006473 bc_vec_pop(&G.prog.results);
6474 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006475
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006476 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006477
6478num_err:
6479 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006480 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006481}
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006482#if ERRORS_ARE_FATAL
6483# define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__), BC_STATUS_SUCCESS)
6484#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006485
Denys Vlasenko29301232018-12-11 15:29:32 +01006486static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006487{
6488 BcStatus s;
6489 BcResult *r;
6490 BcNum *n = NULL;
6491 size_t idx;
6492 char *str;
6493
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006494 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006495 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006496 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006497
Denys Vlasenko29301232018-12-11 15:29:32 +01006498 s = zbc_program_num(r, &n, false);
6499 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006500
6501 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006502 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006503 else {
6504 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006505 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006506 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006507 }
6508
Denys Vlasenko29301232018-12-11 15:29:32 +01006509 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006510}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006511#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006512# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006513#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006514
Denys Vlasenko29301232018-12-11 15:29:32 +01006515static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006516{
6517 BcStatus s;
6518 BcResult *opnd;
6519 BcNum *num = NULL;
6520 unsigned long val;
6521
Denys Vlasenko29301232018-12-11 15:29:32 +01006522 s = zbc_program_prep(&opnd, &num);
6523 if (s) RETURN_STATUS(s);
6524 s = zbc_num_ulong(num, &val);
6525 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006526
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006527 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006528
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006529 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006530 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006531 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006532 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006533 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006534
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006535 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006536
Denys Vlasenko29301232018-12-11 15:29:32 +01006537 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006538}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006539#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006540# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006541#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006542
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006543static BcStatus bc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006544 bool cond)
6545{
6546 BcStatus s = BC_STATUS_SUCCESS;
6547 BcResult *r;
6548 char **str;
6549 BcFunc *f;
6550 BcParse prs;
6551 BcInstPtr ip;
6552 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006553
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006554 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006555 return bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006556
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006557 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006558
6559 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006560 BcNum *n = n; // for compiler
6561 bool exec;
6562 char *name;
6563 char *then_name = bc_program_name(code, bgn);
6564 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006565
6566 if (code[*bgn] == BC_PARSE_STREND)
6567 (*bgn) += 1;
6568 else
6569 else_name = bc_program_name(code, bgn);
6570
6571 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006572 name = then_name;
6573 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006574 exec = true;
6575 name = else_name;
6576 }
6577
6578 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006579 BcVec *v;
6580 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006581 n = bc_vec_top(v);
6582 }
6583
6584 free(then_name);
6585 free(else_name);
6586
6587 if (!exec) goto exit;
6588 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006589 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006590 goto exit;
6591 }
6592
6593 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006594 } else {
6595 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006596 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006597 } else if (r->t == BC_RESULT_VAR) {
6598 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006599 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006600 if (s || !BC_PROG_STR(n)) goto exit;
6601 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006602 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006603 goto exit;
6604 }
6605
6606 fidx = sidx + BC_PROG_REQ_FUNCS;
6607
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006608 str = bc_program_str(sidx);
6609 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006610
6611 if (f->code.len == 0) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006612 common_parse_init(&prs, fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006613 s = bc_parse_text(&prs, *str);
6614 if (s) goto err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01006615 s = zcommon_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006616 if (s) goto err;
6617
6618 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006619 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006620 goto err;
6621 }
6622
6623 bc_parse_free(&prs);
6624 }
6625
6626 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006627 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006628 ip.func = fidx;
6629
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006630 bc_vec_pop(&G.prog.results);
6631 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006632
6633 return BC_STATUS_SUCCESS;
6634
6635err:
6636 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006637 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006638 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006639exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006640 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006641 return s;
6642}
6643#endif // ENABLE_DC
6644
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006645static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006646{
Gavin Howard01055ba2018-11-03 11:00:21 -06006647 BcResult res;
6648 unsigned long val;
6649
6650 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6651 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006652 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006653 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006654 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006655 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006656 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006657
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006658 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006659 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006660 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006661}
6662
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006663static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006664{
Gavin Howard01055ba2018-11-03 11:00:21 -06006665 BcId entry, *entry_ptr;
6666 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006667 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006668
6669 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006670 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006671
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006672 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6673 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006674
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006675 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006676 *idx = entry_ptr->idx;
6677
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006678 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006679
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006680 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006681
6682 // We need to reset these, so the function can be repopulated.
6683 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006684 bc_vec_pop_all(&func->autos);
6685 bc_vec_pop_all(&func->code);
6686 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006687 }
6688 else {
6689 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006690 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006691 }
6692}
6693
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006694static BcStatus bc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006695{
Gavin Howard01055ba2018-11-03 11:00:21 -06006696 BcResult r, *ptr;
6697 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006698 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006699 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006700 char *code = func->code.v;
6701 bool cond = false;
6702
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006703 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006704 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006705 char inst = code[(ip->idx)++];
6706
6707 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006708#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006709 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006710 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006711 if (s) return s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006712 cond = !bc_num_cmp(num, &G.prog.zero);
6713 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006714 // Fallthrough.
6715 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006716 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006717 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006718 addr = bc_vec_item(&func->labels, idx);
6719 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6720 break;
6721 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006722 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006723 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006724 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006725 case BC_INST_INC_PRE:
6726 case BC_INST_DEC_PRE:
6727 case BC_INST_INC_POST:
6728 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006729 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006730 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006731 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006732 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006733 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006734 case BC_INST_RET:
6735 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006736 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006737 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006738 case BC_INST_BOOL_OR:
6739 case BC_INST_BOOL_AND:
6740#endif // ENABLE_BC
6741 case BC_INST_REL_EQ:
6742 case BC_INST_REL_LE:
6743 case BC_INST_REL_GE:
6744 case BC_INST_REL_NE:
6745 case BC_INST_REL_LT:
6746 case BC_INST_REL_GT:
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006747 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006748 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006749 case BC_INST_READ:
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006750 s = bc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006751 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006752 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006753 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006754 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006755 case BC_INST_ARRAY_ELEM:
6756 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006757 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006758 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006759 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006760 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006761 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006762 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006763 case BC_INST_IBASE:
6764 case BC_INST_SCALE:
6765 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006766 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006767 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006768 case BC_INST_SCALE_FUNC:
6769 case BC_INST_LENGTH:
6770 case BC_INST_SQRT:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006771 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006772 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006773 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006774 r.t = BC_RESULT_CONSTANT;
6775 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006776 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006777 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006778 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006779 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006780 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006781 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006782 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006783 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006784 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006785 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006786 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006787 case BC_INST_PRINT:
6788 case BC_INST_PRINT_POP:
6789 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006790 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006791 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006792 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006793 r.t = BC_RESULT_STR;
6794 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006795 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006796 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006797 case BC_INST_POWER:
6798 case BC_INST_MULTIPLY:
6799 case BC_INST_DIVIDE:
6800 case BC_INST_MODULUS:
6801 case BC_INST_PLUS:
6802 case BC_INST_MINUS:
Denys Vlasenko259137d2018-12-11 19:42:05 +01006803 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006804 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006805 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006806 s = zbc_program_prep(&ptr, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006807 if (s) return s;
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006808 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006809 if (!bc_num_cmp(num, &G.prog.zero))
6810 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006811 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006812 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006813 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006814 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006815 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006816 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006817#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006818 case BC_INST_ASSIGN_POWER:
6819 case BC_INST_ASSIGN_MULTIPLY:
6820 case BC_INST_ASSIGN_DIVIDE:
6821 case BC_INST_ASSIGN_MODULUS:
6822 case BC_INST_ASSIGN_PLUS:
6823 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006824#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006825 case BC_INST_ASSIGN:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006826 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006827 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006828#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006829 case BC_INST_MODEXP:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006830 s = zbc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006831 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006832 case BC_INST_DIVMOD:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006833 s = zbc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006834 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006835 case BC_INST_EXECUTE:
6836 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006837 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenko785e4b32018-12-02 17:18:52 +01006838 s = bc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006839 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006840 case BC_INST_PRINT_STACK: {
6841 size_t idx;
6842 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006843 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006844 if (s) break;
6845 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006846 break;
6847 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006848 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006849 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006850 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006851 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006852 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006853 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006854 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006855 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006856 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006857 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006858 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006859 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006860 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006861 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006862 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006863 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006864 return bc_error_stack_has_too_few_elements();
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006865 ptr = bc_vec_item_rev(&G.prog.results, 0);
6866 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006867 memcpy(&r, ptr, sizeof(BcResult));
6868 memcpy(ptr, ptr2, sizeof(BcResult));
6869 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006870 break;
6871 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006872 case BC_INST_ASCIIFY:
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006873 s = zbc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006874 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006875 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006876 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006877 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006878 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006879 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006880 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006881 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006882 break;
6883 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006884 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006885 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006886 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006887 free(name);
6888 break;
6889 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006890 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006891 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006892 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006893 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006894 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006895 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006896 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006897 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006898#endif // ENABLE_DC
6899 }
6900
Denys Vlasenkod38af482018-12-04 19:11:02 +01006901 if (s || G_interrupt) {
6902 bc_program_reset();
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006903 return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01006904 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006905
6906 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006907 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006908 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006909 code = func->code.v;
6910 }
6911
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006912 return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006913}
6914
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006915#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01006916static void bc_vm_info(void)
6917{
6918 printf("%s "BB_VER"\n"
6919 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01006920 , applet_name);
6921}
6922
6923static void bc_args(char **argv)
6924{
6925 unsigned opts;
6926 int i;
6927
6928 GETOPT_RESET();
6929#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006930 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01006931 "warn\0" No_argument "w"
6932 "version\0" No_argument "v"
6933 "standard\0" No_argument "s"
6934 "quiet\0" No_argument "q"
6935 "mathlib\0" No_argument "l"
6936 "interactive\0" No_argument "i"
6937 );
6938#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006939 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01006940#endif
6941 if (getenv("POSIXLY_CORRECT"))
6942 option_mask32 |= BC_FLAG_S;
6943
Denys Vlasenko54214c32018-12-06 09:07:06 +01006944 if (opts & BC_FLAG_V) {
6945 bc_vm_info();
6946 exit(0);
6947 }
6948
6949 for (i = optind; argv[i]; ++i)
6950 bc_vec_push(&G.files, argv + i);
6951}
6952
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006953static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006954{
Gavin Howard01055ba2018-11-03 11:00:21 -06006955 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006956 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01006957 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06006958
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01006959 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06006960
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006961 G.env_args = xstrdup(env_args);
6962 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06006963
6964 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006965
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006966 while (*(buf = skip_whitespace(buf)) != '\0') {
6967 bc_vec_push(&v, &buf);
6968 buf = skip_non_whitespace(buf);
6969 if (!*buf)
6970 break;
6971 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06006972 }
6973
Denys Vlasenko54214c32018-12-06 09:07:06 +01006974 // NULL terminate, and pass argv[] so that first arg is argv[1]
6975 if (sizeof(int) == sizeof(char*)) {
6976 bc_vec_push(&v, &const_int_0);
6977 } else {
6978 static char *const nullptr = NULL;
6979 bc_vec_push(&v, &nullptr);
6980 }
6981 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006982
6983 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006984}
6985#endif // ENABLE_BC
6986
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006987static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006988{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006989 char *lenv;
6990 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006991
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006992 lenv = getenv(var);
6993 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006994 if (!lenv) return len;
6995
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006996 len = bb_strtou(lenv, NULL, 10) - 1;
6997 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006998 len = BC_NUM_PRINT_WIDTH;
6999
7000 return len;
7001}
7002
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007003static BcStatus bc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06007004{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007005 BcStatus s = bc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06007006
Gavin Howard01055ba2018-11-03 11:00:21 -06007007 if (s) return s;
7008
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007009 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007010 s = G.prs.parse(&G.prs);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01007011 if (s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007012 }
7013
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007014 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007015 s = bc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01007016 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007017 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01007018 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06007019 }
7020
7021 return s;
7022}
7023
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007024static BcStatus bc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007025{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007026 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007027 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007028 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007029 BcFunc *main_func;
7030 BcInstPtr *ip;
7031
Denys Vlasenkodf515392018-12-02 19:27:48 +01007032 data = bc_read_file(file);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007033 if (!data) return bc_error_fmt("file '%s' is not text", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007034
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007035 sv_file = G.prog.file;
7036 G.prog.file = file;
7037 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007038 s = bc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007039 if (s) goto err;
7040
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007041 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007042 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007043
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007044 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007045 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007046
7047err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007048 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007049 free(data);
7050 return s;
7051}
7052
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007053static BcStatus bc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007054{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007055 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007056 BcVec buf, buffer;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007057 size_t str;
7058 bool comment;
Gavin Howard01055ba2018-11-03 11:00:21 -06007059
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007060 G.prog.file = NULL;
7061 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007062
Denys Vlasenko7d628012018-12-04 21:46:47 +01007063 bc_char_vec_init(&buffer);
7064 bc_char_vec_init(&buf);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01007065 bc_vec_pushZeroByte(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007066
7067 // This loop is complex because the vm tries not to send any lines that end
7068 // with a backslash to the parser. The reason for that is because the parser
7069 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7070 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007071 comment = false;
7072 str = 0;
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007073 while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) {
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007074 size_t len;
Gavin Howard01055ba2018-11-03 11:00:21 -06007075 char *string = buf.v;
7076
7077 len = buf.len - 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06007078 if (len == 1) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007079 if (str && buf.v[0] == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007080 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007081 else if (buf.v[0] == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007082 str += 1;
7083 }
7084 else if (len > 1 || comment) {
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007085 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06007086 for (i = 0; i < len; ++i) {
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007087 bool notend = len > i + 1;
7088 char c = string[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06007089
7090 if (i - 1 > len || string[i - 1] != '\\') {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007091 if (G.sbgn == G.send)
7092 str ^= c == G.sbgn;
7093 else if (c == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007094 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007095 else if (c == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007096 str += 1;
7097 }
7098
7099 if (c == '/' && notend && !comment && string[i + 1] == '*') {
7100 comment = true;
7101 break;
7102 }
7103 else if (c == '*' && notend && comment && string[i + 1] == '/')
7104 comment = false;
7105 }
7106
7107 if (str || comment || string[len - 2] == '\\') {
7108 bc_vec_concat(&buffer, buf.v);
7109 continue;
7110 }
7111 }
7112
7113 bc_vec_concat(&buffer, buf.v);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007114 s = bc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007115 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007116 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007117 // Debug config, non-interactive mode:
7118 // return all the way back to main.
7119 // Non-debug builds do not come here, they exit.
7120 break;
7121 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007122 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007123
Denys Vlasenko7d628012018-12-04 21:46:47 +01007124 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007125 }
Denys Vlasenkof522dd92018-12-07 16:35:43 +01007126 if (s == BC_STATUS_EOF) // input EOF (^D) is not an error
7127 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06007128
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007129 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007130 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007131 }
7132 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007133 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007134 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007135
Gavin Howard01055ba2018-11-03 11:00:21 -06007136 bc_vec_free(&buf);
7137 bc_vec_free(&buffer);
7138 return s;
7139}
7140
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007141#if ENABLE_BC
7142static const char bc_lib[] = {
7143 "scale=20"
7144"\n" "define e(x){"
7145"\n" "auto b,s,n,r,d,i,p,f,v"
7146"\n" "b=ibase"
7147"\n" "ibase=A"
7148"\n" "if(x<0){"
7149"\n" "n=1"
7150"\n" "x=-x"
7151"\n" "}"
7152"\n" "s=scale"
7153"\n" "r=6+s+0.44*x"
7154"\n" "scale=scale(x)+1"
7155"\n" "while(x>1){"
7156"\n" "d+=1"
7157"\n" "x/=2"
7158"\n" "scale+=1"
7159"\n" "}"
7160"\n" "scale=r"
7161"\n" "r=x+1"
7162"\n" "p=x"
7163"\n" "f=v=1"
7164"\n" "for(i=2;v!=0;++i){"
7165"\n" "p*=x"
7166"\n" "f*=i"
7167"\n" "v=p/f"
7168"\n" "r+=v"
7169"\n" "}"
7170"\n" "while((d--)!=0)r*=r"
7171"\n" "scale=s"
7172"\n" "ibase=b"
7173"\n" "if(n!=0)return(1/r)"
7174"\n" "return(r/1)"
7175"\n" "}"
7176"\n" "define l(x){"
7177"\n" "auto b,s,r,p,a,q,i,v"
7178"\n" "b=ibase"
7179"\n" "ibase=A"
7180"\n" "if(x<=0){"
7181"\n" "r=(1-10^scale)/1"
7182"\n" "ibase=b"
7183"\n" "return(r)"
7184"\n" "}"
7185"\n" "s=scale"
7186"\n" "scale+=6"
7187"\n" "p=2"
7188"\n" "while(x>=2){"
7189"\n" "p*=2"
7190"\n" "x=sqrt(x)"
7191"\n" "}"
7192"\n" "while(x<=0.5){"
7193"\n" "p*=2"
7194"\n" "x=sqrt(x)"
7195"\n" "}"
7196"\n" "r=a=(x-1)/(x+1)"
7197"\n" "q=a*a"
7198"\n" "v=1"
7199"\n" "for(i=3;v!=0;i+=2){"
7200"\n" "a*=q"
7201"\n" "v=a/i"
7202"\n" "r+=v"
7203"\n" "}"
7204"\n" "r*=p"
7205"\n" "scale=s"
7206"\n" "ibase=b"
7207"\n" "return(r/1)"
7208"\n" "}"
7209"\n" "define s(x){"
7210"\n" "auto b,s,r,n,a,q,i"
7211"\n" "b=ibase"
7212"\n" "ibase=A"
7213"\n" "s=scale"
7214"\n" "scale=1.1*s+2"
7215"\n" "a=a(1)"
7216"\n" "if(x<0){"
7217"\n" "n=1"
7218"\n" "x=-x"
7219"\n" "}"
7220"\n" "scale=0"
7221"\n" "q=(x/a+2)/4"
7222"\n" "x=x-4*q*a"
7223"\n" "if(q%2!=0)x=-x"
7224"\n" "scale=s+2"
7225"\n" "r=a=x"
7226"\n" "q=-x*x"
7227"\n" "for(i=3;a!=0;i+=2){"
7228"\n" "a*=q/(i*(i-1))"
7229"\n" "r+=a"
7230"\n" "}"
7231"\n" "scale=s"
7232"\n" "ibase=b"
7233"\n" "if(n!=0)return(-r/1)"
7234"\n" "return(r/1)"
7235"\n" "}"
7236"\n" "define c(x){"
7237"\n" "auto b,s"
7238"\n" "b=ibase"
7239"\n" "ibase=A"
7240"\n" "s=scale"
7241"\n" "scale*=1.2"
7242"\n" "x=s(2*a(1)+x)"
7243"\n" "scale=s"
7244"\n" "ibase=b"
7245"\n" "return(x/1)"
7246"\n" "}"
7247"\n" "define a(x){"
7248"\n" "auto b,s,r,n,a,m,t,f,i,u"
7249"\n" "b=ibase"
7250"\n" "ibase=A"
7251"\n" "n=1"
7252"\n" "if(x<0){"
7253"\n" "n=-1"
7254"\n" "x=-x"
7255"\n" "}"
7256"\n" "if(x==1){"
7257"\n" "if(scale<65){"
7258"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7259"\n" "}"
7260"\n" "}"
7261"\n" "if(x==.2){"
7262"\n" "if(scale<65){"
7263"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7264"\n" "}"
7265"\n" "}"
7266"\n" "s=scale"
7267"\n" "if(x>.2){"
7268"\n" "scale+=5"
7269"\n" "a=a(.2)"
7270"\n" "}"
7271"\n" "scale=s+3"
7272"\n" "while(x>.2){"
7273"\n" "m+=1"
7274"\n" "x=(x-.2)/(1+.2*x)"
7275"\n" "}"
7276"\n" "r=u=x"
7277"\n" "f=-x*x"
7278"\n" "t=1"
7279"\n" "for(i=3;t!=0;i+=2){"
7280"\n" "u*=f"
7281"\n" "t=u/i"
7282"\n" "r+=t"
7283"\n" "}"
7284"\n" "scale=s"
7285"\n" "ibase=b"
7286"\n" "return((m*a+r)/n)"
7287"\n" "}"
7288"\n" "define j(n,x){"
7289"\n" "auto b,s,o,a,i,v,f"
7290"\n" "b=ibase"
7291"\n" "ibase=A"
7292"\n" "s=scale"
7293"\n" "scale=0"
7294"\n" "n/=1"
7295"\n" "if(n<0){"
7296"\n" "n=-n"
7297"\n" "if(n%2==1)o=1"
7298"\n" "}"
7299"\n" "a=1"
7300"\n" "for(i=2;i<=n;++i)a*=i"
7301"\n" "scale=1.5*s"
7302"\n" "a=(x^n)/2^n/a"
7303"\n" "r=v=1"
7304"\n" "f=-x*x/4"
7305"\n" "scale=scale+length(a)-scale(a)"
7306"\n" "for(i=1;v!=0;++i){"
7307"\n" "v=v*f/i/(n+i)"
7308"\n" "r+=v"
7309"\n" "}"
7310"\n" "scale=s"
7311"\n" "ibase=b"
7312"\n" "if(o!=0)a=-a"
7313"\n" "return(a*r/1)"
7314"\n" "}"
7315};
7316#endif // ENABLE_BC
7317
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007318static BcStatus bc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007319{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007320 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007321 size_t i;
7322
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007323#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007324 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007325
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007326 // We know that internal library is not buggy,
7327 // thus error checking is normally disabled.
7328# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007329 bc_lex_file(&G.prs.l);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007330 s = bc_parse_text(&G.prs, bc_lib);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007331 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007332
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007333 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007334 s = G.prs.parse(&G.prs);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007335 if (DEBUG_LIB && s) return s;
7336 }
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007337 s = bc_program_exec();
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007338 if (DEBUG_LIB && s) return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007339 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007340#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007341
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007342 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007343 for (i = 0; !s && i < G.files.len; ++i)
7344 s = bc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007345 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7346 // Debug config, non-interactive mode:
7347 // return all the way back to main.
7348 // Non-debug builds do not come here, they exit.
7349 return s;
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007350 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007351
Denys Vlasenko91cde952018-12-10 20:56:08 +01007352 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007353 s = bc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007354
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007355 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
7356 s = bc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007357
Denys Vlasenko00d77792018-11-30 23:13:42 +01007358 return s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007359}
7360
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007361#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007362static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007363{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007364 bc_num_free(&G.prog.ib);
7365 bc_num_free(&G.prog.ob);
7366 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007367# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007368 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007369# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007370 bc_vec_free(&G.prog.fns);
7371 bc_vec_free(&G.prog.fn_map);
7372 bc_vec_free(&G.prog.vars);
7373 bc_vec_free(&G.prog.var_map);
7374 bc_vec_free(&G.prog.arrs);
7375 bc_vec_free(&G.prog.arr_map);
7376 bc_vec_free(&G.prog.strs);
7377 bc_vec_free(&G.prog.consts);
7378 bc_vec_free(&G.prog.results);
7379 bc_vec_free(&G.prog.stack);
7380 bc_num_free(&G.prog.last);
7381 bc_num_free(&G.prog.zero);
7382 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007383}
7384
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007385static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007386{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007387 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007388 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007389 bc_parse_free(&G.prs);
7390 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007391}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007392#endif
7393
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007394static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007395{
7396 size_t idx;
7397 BcInstPtr ip;
7398
7399 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7400 memset(&ip, 0, sizeof(BcInstPtr));
7401
7402 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007403 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007404 bc_num_ten(&G.prog.ib);
7405 G.prog.ib_t = 10;
7406
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007407 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007408 bc_num_ten(&G.prog.ob);
7409 G.prog.ob_t = 10;
7410
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007411 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007412 bc_num_ten(&G.prog.hexb);
7413 G.prog.hexb.num[0] = 6;
7414
7415#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007416 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007417 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7418#endif
7419
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007420 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007421 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007422
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007423 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007424 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007425
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007426 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007427 bc_num_one(&G.prog.one);
7428
7429 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007430 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007431
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007432 bc_program_addFunc(xstrdup("(main)"), &idx);
7433 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007434
7435 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007436 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007437
7438 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007439 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007440
7441 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7442 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7443 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7444 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7445 bc_vec_push(&G.prog.stack, &ip);
7446}
Gavin Howard01055ba2018-11-03 11:00:21 -06007447
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007448static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007449{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007450#if ENABLE_FEATURE_EDITING
7451 G.line_input_state = new_line_input_t(DO_HISTORY);
7452#endif
7453 G.prog.len = bc_vm_envLen(env_len);
7454
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007455 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007456 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007457 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007458 bc_program_init();
Denys Vlasenkof0f069b2018-12-11 23:22:52 +01007459 common_parse_init(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007460
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007461 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007462#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007463 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007464 // With SA_RESTART, most system calls will restart
7465 // (IOW: they won't fail with EINTR).
7466 // In particular, this means ^C won't cause
7467 // stdout to get into "error state" if SIGINT hits
7468 // within write() syscall.
7469 // The downside is that ^C while line input is taken
7470 // will only be handled after [Enter] since read()
7471 // from stdin is not interrupted by ^C either,
7472 // it restarts, thus fgetc() does not return on ^C.
7473 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7474
7475 // Without SA_RESTART, this exhibits a bug:
7476 // "while (1) print 1" and try ^C-ing it.
7477 // Intermittently, instead of returning to input line,
7478 // you'll get "output error: Interrupted system call"
7479 // and exit.
7480 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007481#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007482 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007483 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007484 return 0; // "not a tty"
7485}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007486
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007487static BcStatus bc_vm_run(void)
7488{
7489 BcStatus st = bc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007490#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007491 if (G_exiting) // it was actually "halt" or "quit"
7492 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007493 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007494# if ENABLE_FEATURE_EDITING
7495 free_line_input_t(G.line_input_state);
7496# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007497 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007498#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007499 return st;
7500}
7501
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007502#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007503int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007504int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007505{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007506 int is_tty;
7507
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007508 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007509 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007510
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007511 is_tty = bc_vm_init("BC_LINE_LENGTH");
7512
7513 bc_args(argv);
7514
7515 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7516 bc_vm_info();
7517
7518 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007519}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007520#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007521
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007522#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007523int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007524int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007525{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007526 int noscript;
7527
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007528 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007529 G.sbgn = '[';
7530 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007531 /*
7532 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7533 * 1 char wider than bc from the same package.
7534 * Both default width, and xC_LINE_LENGTH=N are wider:
7535 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7536 * 1234\
7537 * 56
7538 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7539 * 123\
7540 * 456
7541 * Do the same, or it's a bug?
7542 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007543 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007544
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007545 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7546 noscript = BC_FLAG_I;
7547 for (;;) {
7548 int n = getopt(argc, argv, "e:f:x");
7549 if (n <= 0)
7550 break;
7551 switch (n) {
7552 case 'e':
7553 noscript = 0;
7554 n = bc_vm_process(optarg);
7555 if (n) return n;
7556 break;
7557 case 'f':
7558 noscript = 0;
7559 bc_vm_file(optarg);
7560 break;
7561 case 'x':
7562 option_mask32 |= DC_FLAG_X;
7563 break;
7564 default:
7565 bb_show_usage();
7566 }
7567 }
7568 argv += optind;
7569
7570 while (*argv) {
7571 noscript = 0;
7572 bc_vec_push(&G.files, argv++);
7573 }
7574
7575 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7576
7577 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007578}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007579#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007580
7581#endif // not DC_SMALL