blob: 47acd7fc35516ebe1e30249fa43d520de931953c [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
Denys Vlasenko89e785a2018-12-13 16:35:52 +010013//config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060014//config:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010015//config: This bc has five differences to the GNU bc:
16//config: 1) The period (.) is a shortcut for "last", as in the BSD bc.
Gavin Howard01055ba2018-11-03 11:00:21 -060017//config: 2) Arrays are copied before being passed as arguments to
18//config: functions. This behavior is required by the bc spec.
19//config: 3) Arrays can be passed to the builtin "length" function to get
Denys Vlasenko89e785a2018-12-13 16:35:52 +010020//config: the number of elements in the array. This prints "1":
21//config: a[0] = 0; length(a[])
Gavin Howard01055ba2018-11-03 11:00:21 -060022//config: 4) The precedence of the boolean "not" operator (!) is equal to
Denys Vlasenko89e785a2018-12-13 16:35:52 +010023//config: that of the unary minus (-) negation operator. This still
Gavin Howard01055ba2018-11-03 11:00:21 -060024//config: allows POSIX-compliant scripts to work while somewhat
25//config: preserving expected behavior (versus C) and making parsing
26//config: easier.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010027//config: 5) "read()" accepts expressions, not only numeric literals.
Gavin Howard01055ba2018-11-03 11:00:21 -060028//config:
29//config: Options:
Gavin Howard01055ba2018-11-03 11:00:21 -060030//config: -i --interactive force interactive mode
Denys Vlasenko89e785a2018-12-13 16:35:52 +010031//config: -q --quiet don't print version and copyright
32//config: -s --standard error if any non-POSIX extensions are used
33//config: -w --warn warn if any non-POSIX extensions are used
Gavin Howard01055ba2018-11-03 11:00:21 -060034//config: -l --mathlib use predefined math routines:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010035//config: s(expr) sine in radians
36//config: c(expr) cosine in radians
37//config: a(expr) arctangent, returning radians
38//config: l(expr) natural log
39//config: e(expr) raises e to the power of expr
40//config: j(n, x) Bessel function of integer order n of x
Gavin Howard01055ba2018-11-03 11:00:21 -060041//config:
42//config:config DC
43//config: bool "dc (38 kb; 49 kb when combined with bc)"
44//config: default y
45//config: help
46//config: dc is a reverse-polish notation command-line calculator which
47//config: supports unlimited precision arithmetic. See the FreeBSD man page
48//config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual
Denys Vlasenko89e785a2018-12-13 16:35:52 +010049//config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html).
Gavin Howard01055ba2018-11-03 11:00:21 -060050//config:
51//config: This dc has a few differences from the two above:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010052//config: 1) When printing a byte stream (command "P"), this dc follows what
Gavin Howard01055ba2018-11-03 11:00:21 -060053//config: the FreeBSD dc does.
Denys Vlasenko89e785a2018-12-13 16:35:52 +010054//config: 2) Implements the GNU extensions for divmod ("~") and
Gavin Howard01055ba2018-11-03 11:00:21 -060055//config: modular exponentiation ("|").
Denys Vlasenko89e785a2018-12-13 16:35:52 +010056//config: 3) Implements all FreeBSD extensions, except for "J" and "M".
Gavin Howard01055ba2018-11-03 11:00:21 -060057//config: 4) Like the FreeBSD dc, this dc supports extended registers.
58//config: However, they are implemented differently. When it encounters
59//config: whitespace where a register should be, it skips the whitespace.
60//config: If the character following is not a lowercase letter, an error
61//config: is issued. Otherwise, the register name is parsed by the
62//config: following regex:
Denys Vlasenko89e785a2018-12-13 16:35:52 +010063//config: [a-z][a-z0-9_]*
Gavin Howard01055ba2018-11-03 11:00:21 -060064//config: This generally means that register names will be surrounded by
Denys Vlasenko89e785a2018-12-13 16:35:52 +010065//config: whitespace. Examples:
66//config: l idx s temp L index S temp2 < do_thing
Gavin Howard01055ba2018-11-03 11:00:21 -060067//config: Also note that, like the FreeBSD dc, extended registers are not
68//config: allowed unless the "-x" option is given.
69//config:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010070//config:config FEATURE_DC_SMALL
71//config: bool "Minimal dc implementation (4.2 kb), not using bc code base"
72//config: depends on DC && !BC
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +010073//config: default n
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010074//config:
75//config:config FEATURE_DC_LIBM
76//config: bool "Enable power and exp functions (requires libm)"
77//config: default y
78//config: depends on FEATURE_DC_SMALL
79//config: help
80//config: Enable power and exp functions.
81//config: NOTE: This will require libm to be present for linking.
82//config:
Gavin Howard01055ba2018-11-03 11:00:21 -060083//config:config FEATURE_BC_SIGNALS
Denys Vlasenko89e785a2018-12-13 16:35:52 +010084//config: bool "Interactive mode (+4kb)"
Gavin Howard01055ba2018-11-03 11:00:21 -060085//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +010086//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -060087//config: help
Denys Vlasenko89e785a2018-12-13 16:35:52 +010088//config: Enable interactive mode: when started on a tty,
89//config: ^C interrupts execution and returns to command line,
90//config: errors also return to command line instead of exiting,
91//config: line editing with history is available.
92//config:
93//config: With this option off, input can still be taken from tty,
94//config: but all errors are fatal, ^C is fatal,
95//config: tty is treated exactly the same as any other
96//config: standard input (IOW: no line editing).
Gavin Howard01055ba2018-11-03 11:00:21 -060097//config:
98//config:config FEATURE_BC_LONG_OPTIONS
99//config: bool "Enable bc/dc long options"
100//config: default y
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100101//config: depends on (BC || DC) && !FEATURE_DC_SMALL
Gavin Howard01055ba2018-11-03 11:00:21 -0600102//config: help
103//config: Enable long options for bc and dc.
104
105//applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP))
106//applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP))
107
108//kbuild:lib-$(CONFIG_BC) += bc.o
109//kbuild:lib-$(CONFIG_DC) += bc.o
110
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100111//See www.gnu.org/software/bc/manual/bc.html
Gavin Howard01055ba2018-11-03 11:00:21 -0600112//usage:#define bc_trivial_usage
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100113//usage: "[-sqliw] FILE..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600114//usage:
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100115//usage:#define bc_full_usage "\n"
116//usage: "\nArbitrary precision calculator"
117//usage: "\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100118///////: "\n -i Interactive" - has no effect for now
119//usage: "\n -q Quiet"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100120//usage: "\n -l Load standard math library"
121//usage: "\n -s Be POSIX compatible"
Denys Vlasenko9721f6c2018-12-02 20:34:03 +0100122//usage: "\n -w Warn if extensions are used"
123///////: "\n -v Version"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100124//usage: "\n"
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100125//usage: "\n$BC_LINE_LENGTH changes output width"
Gavin Howard01055ba2018-11-03 11:00:21 -0600126//usage:
127//usage:#define bc_example_usage
128//usage: "3 + 4.129\n"
129//usage: "1903 - 2893\n"
130//usage: "-129 * 213.28935\n"
131//usage: "12 / -1932\n"
132//usage: "12 % 12\n"
133//usage: "34 ^ 189\n"
134//usage: "scale = 13\n"
135//usage: "ibase = 2\n"
136//usage: "obase = A\n"
137//usage:
138//usage:#define dc_trivial_usage
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100139//usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..."
Gavin Howard01055ba2018-11-03 11:00:21 -0600140//usage:
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100141//usage:#define dc_full_usage "\n"
142//usage: "\nTiny RPN calculator. Operations:"
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +0100143//usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,")
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100144//usage: "\np - print top of the stack (without popping)"
145//usage: "\nf - print entire stack"
146//usage: "\nk - pop the value and set the precision"
147//usage: "\ni - pop the value and set input radix"
148//usage: "\no - pop the value and set output radix"
149//usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16"
Gavin Howard01055ba2018-11-03 11:00:21 -0600150//usage:
151//usage:#define dc_example_usage
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100152//usage: "$ dc -e'2 2 + p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600153//usage: "4\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100154//usage: "$ dc -e'8 8 \\* 2 2 + / p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600155//usage: "16\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100156//usage: "$ dc -e'0 1 & p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600157//usage: "0\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100158//usage: "$ dc -e'0 1 | p'\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600159//usage: "1\n"
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100160//usage: "$ echo '72 9 / 8 * p' | dc\n"
Gavin Howard01055ba2018-11-03 11:00:21 -0600161//usage: "64\n"
162
163#include "libbb.h"
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100164#include "common_bufsiz.h"
Gavin Howard01055ba2018-11-03 11:00:21 -0600165
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +0100166#if ENABLE_FEATURE_DC_SMALL
167# include "dc.c"
168#else
169
Gavin Howard01055ba2018-11-03 11:00:21 -0600170typedef enum BcStatus {
Denys Vlasenko60cf7472018-12-04 20:05:28 +0100171 BC_STATUS_SUCCESS = 0,
172 BC_STATUS_FAILURE = 1,
Denys Vlasenkob402ff82018-12-11 15:45:15 +0100173 BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this
Gavin Howard01055ba2018-11-03 11:00:21 -0600174} BcStatus;
175
Gavin Howard01055ba2018-11-03 11:00:21 -0600176#define BC_VEC_INVALID_IDX ((size_t) -1)
177#define BC_VEC_START_CAP (1 << 5)
178
Denys Vlasenko5ba55f12018-12-10 15:37:14 +0100179typedef void (*BcVecFree)(void *) FAST_FUNC;
Gavin Howard01055ba2018-11-03 11:00:21 -0600180
181typedef struct BcVec {
182 char *v;
183 size_t len;
184 size_t cap;
185 size_t size;
186 BcVecFree dtor;
187} BcVec;
188
Gavin Howard01055ba2018-11-03 11:00:21 -0600189typedef signed char BcDig;
190
191typedef struct BcNum {
192 BcDig *restrict num;
193 size_t rdx;
194 size_t len;
195 size_t cap;
196 bool neg;
197} BcNum;
198
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100199#define BC_NUM_MIN_BASE ((unsigned long) 2)
200#define BC_NUM_MAX_IBASE ((unsigned long) 16)
201// larger value might speed up BIGNUM calculations a bit:
202#define BC_NUM_DEF_SIZE (16)
203#define BC_NUM_PRINT_WIDTH (69)
Gavin Howard01055ba2018-11-03 11:00:21 -0600204
Denys Vlasenko2fa11b62018-12-06 12:34:39 +0100205#define BC_NUM_KARATSUBA_LEN (32)
Gavin Howard01055ba2018-11-03 11:00:21 -0600206
Gavin Howard01055ba2018-11-03 11:00:21 -0600207typedef enum BcInst {
208
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100209#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600210 BC_INST_INC_PRE,
211 BC_INST_DEC_PRE,
212 BC_INST_INC_POST,
213 BC_INST_DEC_POST,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100214#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600215
216 BC_INST_NEG,
217
218 BC_INST_POWER,
219 BC_INST_MULTIPLY,
220 BC_INST_DIVIDE,
221 BC_INST_MODULUS,
222 BC_INST_PLUS,
223 BC_INST_MINUS,
224
225 BC_INST_REL_EQ,
226 BC_INST_REL_LE,
227 BC_INST_REL_GE,
228 BC_INST_REL_NE,
229 BC_INST_REL_LT,
230 BC_INST_REL_GT,
231
232 BC_INST_BOOL_NOT,
233 BC_INST_BOOL_OR,
234 BC_INST_BOOL_AND,
235
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100236#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600237 BC_INST_ASSIGN_POWER,
238 BC_INST_ASSIGN_MULTIPLY,
239 BC_INST_ASSIGN_DIVIDE,
240 BC_INST_ASSIGN_MODULUS,
241 BC_INST_ASSIGN_PLUS,
242 BC_INST_ASSIGN_MINUS,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100243#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600244 BC_INST_ASSIGN,
245
246 BC_INST_NUM,
247 BC_INST_VAR,
248 BC_INST_ARRAY_ELEM,
249 BC_INST_ARRAY,
250
251 BC_INST_SCALE_FUNC,
252 BC_INST_IBASE,
253 BC_INST_SCALE,
254 BC_INST_LAST,
255 BC_INST_LENGTH,
256 BC_INST_READ,
257 BC_INST_OBASE,
258 BC_INST_SQRT,
259
260 BC_INST_PRINT,
261 BC_INST_PRINT_POP,
262 BC_INST_STR,
263 BC_INST_PRINT_STR,
264
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100265#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600266 BC_INST_JUMP,
267 BC_INST_JUMP_ZERO,
268
269 BC_INST_CALL,
270
271 BC_INST_RET,
272 BC_INST_RET0,
273
274 BC_INST_HALT,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100275#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600276
277 BC_INST_POP,
278 BC_INST_POP_EXEC,
279
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100280#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600281 BC_INST_MODEXP,
282 BC_INST_DIVMOD,
283
284 BC_INST_EXECUTE,
285 BC_INST_EXEC_COND,
286
287 BC_INST_ASCIIFY,
288 BC_INST_PRINT_STREAM,
289
290 BC_INST_PRINT_STACK,
291 BC_INST_CLEAR_STACK,
292 BC_INST_STACK_LEN,
293 BC_INST_DUPLICATE,
294 BC_INST_SWAP,
295
296 BC_INST_LOAD,
297 BC_INST_PUSH_VAR,
298 BC_INST_PUSH_TO_VAR,
299
300 BC_INST_QUIT,
301 BC_INST_NQUIT,
302
303 BC_INST_INVALID = -1,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100304#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600305
306} BcInst;
307
308typedef struct BcId {
309 char *name;
310 size_t idx;
311} BcId;
312
313typedef struct BcFunc {
314 BcVec code;
315 BcVec labels;
316 size_t nparams;
317 BcVec autos;
318} BcFunc;
319
320typedef enum BcResultType {
321
322 BC_RESULT_TEMP,
323
324 BC_RESULT_VAR,
325 BC_RESULT_ARRAY_ELEM,
326 BC_RESULT_ARRAY,
327
328 BC_RESULT_STR,
329
330 BC_RESULT_IBASE,
331 BC_RESULT_SCALE,
332 BC_RESULT_LAST,
333
334 // These are between to calculate ibase, obase, and last from instructions.
335 BC_RESULT_CONSTANT,
336 BC_RESULT_ONE,
337
338 BC_RESULT_OBASE,
339
340} BcResultType;
341
342typedef union BcResultData {
343 BcNum n;
344 BcVec v;
345 BcId id;
346} BcResultData;
347
348typedef struct BcResult {
349 BcResultType t;
350 BcResultData d;
351} BcResult;
352
353typedef struct BcInstPtr {
354 size_t func;
355 size_t idx;
356 size_t len;
357} BcInstPtr;
358
Gavin Howard01055ba2018-11-03 11:00:21 -0600359// BC_LEX_NEG is not used in lexing; it is only for parsing.
360typedef enum BcLexType {
361
362 BC_LEX_EOF,
363 BC_LEX_INVALID,
364
365 BC_LEX_OP_INC,
366 BC_LEX_OP_DEC,
367
368 BC_LEX_NEG,
369
370 BC_LEX_OP_POWER,
371 BC_LEX_OP_MULTIPLY,
372 BC_LEX_OP_DIVIDE,
373 BC_LEX_OP_MODULUS,
374 BC_LEX_OP_PLUS,
375 BC_LEX_OP_MINUS,
376
377 BC_LEX_OP_REL_EQ,
378 BC_LEX_OP_REL_LE,
379 BC_LEX_OP_REL_GE,
380 BC_LEX_OP_REL_NE,
381 BC_LEX_OP_REL_LT,
382 BC_LEX_OP_REL_GT,
383
384 BC_LEX_OP_BOOL_NOT,
385 BC_LEX_OP_BOOL_OR,
386 BC_LEX_OP_BOOL_AND,
387
388 BC_LEX_OP_ASSIGN_POWER,
389 BC_LEX_OP_ASSIGN_MULTIPLY,
390 BC_LEX_OP_ASSIGN_DIVIDE,
391 BC_LEX_OP_ASSIGN_MODULUS,
392 BC_LEX_OP_ASSIGN_PLUS,
393 BC_LEX_OP_ASSIGN_MINUS,
394 BC_LEX_OP_ASSIGN,
395
396 BC_LEX_NLINE,
397 BC_LEX_WHITESPACE,
398
399 BC_LEX_LPAREN,
400 BC_LEX_RPAREN,
401
402 BC_LEX_LBRACKET,
403 BC_LEX_COMMA,
404 BC_LEX_RBRACKET,
405
406 BC_LEX_LBRACE,
407 BC_LEX_SCOLON,
408 BC_LEX_RBRACE,
409
410 BC_LEX_STR,
411 BC_LEX_NAME,
412 BC_LEX_NUMBER,
413
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100414 BC_LEX_KEY_1st_keyword,
415 BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword,
Gavin Howard01055ba2018-11-03 11:00:21 -0600416 BC_LEX_KEY_BREAK,
417 BC_LEX_KEY_CONTINUE,
418 BC_LEX_KEY_DEFINE,
419 BC_LEX_KEY_ELSE,
420 BC_LEX_KEY_FOR,
421 BC_LEX_KEY_HALT,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100422 // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct,
423 BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600424 BC_LEX_KEY_IF,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100425 BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST
Gavin Howard01055ba2018-11-03 11:00:21 -0600426 BC_LEX_KEY_LENGTH,
427 BC_LEX_KEY_LIMITS,
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100428 BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE
Gavin Howard01055ba2018-11-03 11:00:21 -0600429 BC_LEX_KEY_PRINT,
430 BC_LEX_KEY_QUIT,
431 BC_LEX_KEY_READ,
432 BC_LEX_KEY_RETURN,
433 BC_LEX_KEY_SCALE,
434 BC_LEX_KEY_SQRT,
435 BC_LEX_KEY_WHILE,
436
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100437#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600438 BC_LEX_EQ_NO_REG,
439 BC_LEX_OP_MODEXP,
440 BC_LEX_OP_DIVMOD,
441
442 BC_LEX_COLON,
443 BC_LEX_ELSE,
444 BC_LEX_EXECUTE,
445 BC_LEX_PRINT_STACK,
446 BC_LEX_CLEAR_STACK,
447 BC_LEX_STACK_LEVEL,
448 BC_LEX_DUPLICATE,
449 BC_LEX_SWAP,
450 BC_LEX_POP,
451
452 BC_LEX_ASCIIFY,
453 BC_LEX_PRINT_STREAM,
454
455 BC_LEX_STORE_IBASE,
456 BC_LEX_STORE_SCALE,
457 BC_LEX_LOAD,
458 BC_LEX_LOAD_POP,
459 BC_LEX_STORE_PUSH,
460 BC_LEX_STORE_OBASE,
461 BC_LEX_PRINT_POP,
462 BC_LEX_NQUIT,
463 BC_LEX_SCALE_FACTOR,
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100464#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600465} BcLexType;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100466// must match order of BC_LEX_KEY_foo etc above
467#if ENABLE_BC
468struct BcLexKeyword {
469 char name8[8];
470};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100471#define BC_LEX_KW_ENTRY(a, b) \
472 { .name8 = a /*, .posix = b */ }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100473static const struct BcLexKeyword bc_lex_kws[20] = {
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100474 BC_LEX_KW_ENTRY("auto" , 1), // 0
475 BC_LEX_KW_ENTRY("break" , 1), // 1
476 BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL
477 BC_LEX_KW_ENTRY("define" , 1), // 3
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100478
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100479 BC_LEX_KW_ENTRY("else" , 0), // 4
480 BC_LEX_KW_ENTRY("for" , 1), // 5
481 BC_LEX_KW_ENTRY("halt" , 0), // 6
482 BC_LEX_KW_ENTRY("ibase" , 1), // 7
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100483
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100484 BC_LEX_KW_ENTRY("if" , 1), // 8
485 BC_LEX_KW_ENTRY("last" , 0), // 9
486 BC_LEX_KW_ENTRY("length" , 1), // 10
487 BC_LEX_KW_ENTRY("limits" , 0), // 11
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100488
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100489 BC_LEX_KW_ENTRY("obase" , 1), // 12
490 BC_LEX_KW_ENTRY("print" , 0), // 13
491 BC_LEX_KW_ENTRY("quit" , 1), // 14
492 BC_LEX_KW_ENTRY("read" , 0), // 15
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100493
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100494 BC_LEX_KW_ENTRY("return" , 1), // 16
495 BC_LEX_KW_ENTRY("scale" , 1), // 17
496 BC_LEX_KW_ENTRY("sqrt" , 1), // 18
497 BC_LEX_KW_ENTRY("while" , 1), // 19
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100498};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100499#undef BC_LEX_KW_ENTRY
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100500enum {
501 POSIX_KWORD_MASK = 0
502 | (1 << 0)
503 | (1 << 1)
504 | (0 << 2)
505 | (1 << 3)
506 \
507 | (0 << 4)
508 | (1 << 5)
509 | (0 << 6)
510 | (1 << 7)
511 \
512 | (1 << 8)
513 | (0 << 9)
514 | (1 << 10)
515 | (0 << 11)
516 \
517 | (1 << 12)
518 | (0 << 13)
519 | (1 << 14)
520 | (0 << 15)
521 \
522 | (1 << 16)
523 | (1 << 17)
524 | (1 << 18)
525 | (1 << 19)
526};
Denys Vlasenkod00d2f92018-12-06 12:59:40 +0100527#define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +0100528#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600529
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100530#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
531# define BC_STATUS BcStatus
532#else
533# define BC_STATUS void
534#endif
535
Gavin Howard01055ba2018-11-03 11:00:21 -0600536typedef struct BcLex {
537
538 const char *buf;
539 size_t i;
540 size_t line;
Gavin Howard01055ba2018-11-03 11:00:21 -0600541 size_t len;
542 bool newline;
543
544 struct {
545 BcLexType t;
546 BcLexType last;
547 BcVec v;
548 } t;
549
Gavin Howard01055ba2018-11-03 11:00:21 -0600550} BcLex;
551
552#define BC_PARSE_STREND ((char) UCHAR_MAX)
553
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100554#define BC_PARSE_REL (1 << 0)
555#define BC_PARSE_PRINT (1 << 1)
Gavin Howard01055ba2018-11-03 11:00:21 -0600556#define BC_PARSE_NOCALL (1 << 2)
557#define BC_PARSE_NOREAD (1 << 3)
Denys Vlasenkoe55a5722018-12-06 12:47:17 +0100558#define BC_PARSE_ARRAY (1 << 4)
Gavin Howard01055ba2018-11-03 11:00:21 -0600559
560#define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags))
561#define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse)))
562
563#define BC_PARSE_FLAG_FUNC_INNER (1 << 0)
564#define BC_PARSE_FUNC_INNER(parse) \
565 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER)
566
567#define BC_PARSE_FLAG_FUNC (1 << 1)
568#define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC)
569
570#define BC_PARSE_FLAG_BODY (1 << 2)
571#define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY)
572
573#define BC_PARSE_FLAG_LOOP (1 << 3)
574#define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP)
575
576#define BC_PARSE_FLAG_LOOP_INNER (1 << 4)
577#define BC_PARSE_LOOP_INNER(parse) \
578 (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER)
579
580#define BC_PARSE_FLAG_IF (1 << 5)
581#define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF)
582
583#define BC_PARSE_FLAG_ELSE (1 << 6)
584#define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE)
585
586#define BC_PARSE_FLAG_IF_END (1 << 7)
587#define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END)
588
589#define BC_PARSE_CAN_EXEC(parse) \
590 (!(BC_PARSE_TOP_FLAG(parse) & \
591 (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \
592 BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \
593 BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END)))
594
Gavin Howard01055ba2018-11-03 11:00:21 -0600595struct BcParse;
596
597struct BcProgram;
598
Gavin Howard01055ba2018-11-03 11:00:21 -0600599typedef struct BcParse {
600
Gavin Howard01055ba2018-11-03 11:00:21 -0600601 BcLex l;
602
603 BcVec flags;
604
605 BcVec exits;
606 BcVec conds;
607
608 BcVec ops;
609
Gavin Howard01055ba2018-11-03 11:00:21 -0600610 BcFunc *func;
611 size_t fidx;
612
613 size_t nbraces;
614 bool auto_part;
615
616} BcParse;
617
Gavin Howard01055ba2018-11-03 11:00:21 -0600618typedef struct BcProgram {
619
620 size_t len;
621 size_t scale;
622
623 BcNum ib;
624 size_t ib_t;
625 BcNum ob;
626 size_t ob_t;
627
628 BcNum hexb;
629
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100630#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600631 BcNum strmb;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100632#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600633
634 BcVec results;
635 BcVec stack;
636
637 BcVec fns;
638 BcVec fn_map;
639
640 BcVec vars;
641 BcVec var_map;
642
643 BcVec arrs;
644 BcVec arr_map;
645
646 BcVec strs;
647 BcVec consts;
648
649 const char *file;
650
651 BcNum last;
652 BcNum zero;
653 BcNum one;
654
655 size_t nchars;
656
Gavin Howard01055ba2018-11-03 11:00:21 -0600657} BcProgram;
658
659#define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n))
660
661#define BC_PROG_MAIN (0)
662#define BC_PROG_READ (1)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100663#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -0600664#define BC_PROG_REQ_FUNCS (2)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100665#endif
Gavin Howard01055ba2018-11-03 11:00:21 -0600666
667#define BC_PROG_STR(n) (!(n)->num && !(n)->cap)
668#define BC_PROG_NUM(r, n) \
669 ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n))
670
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100671#define BC_FLAG_W (1 << 0)
672#define BC_FLAG_V (1 << 1)
673#define BC_FLAG_S (1 << 2)
674#define BC_FLAG_Q (1 << 3)
675#define BC_FLAG_L (1 << 4)
676#define BC_FLAG_I (1 << 5)
677#define DC_FLAG_X (1 << 6)
Gavin Howard01055ba2018-11-03 11:00:21 -0600678
679#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
680#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
681
Denys Vlasenko64074a12018-12-07 15:50:14 +0100682#define BC_MAX_OBASE ((unsigned) 999)
683#define BC_MAX_DIM ((unsigned) INT_MAX)
684#define BC_MAX_SCALE ((unsigned) UINT_MAX)
685#define BC_MAX_STRING ((unsigned) UINT_MAX - 1)
686#define BC_MAX_NUM BC_MAX_STRING
687// Unused apart from "limits" message. Just show a "biggish number" there.
688//#define BC_MAX_NAME BC_MAX_STRING
689//#define BC_MAX_EXP ((unsigned long) LONG_MAX)
690//#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1)
691#define BC_MAX_NAME_STR "999999999"
692#define BC_MAX_EXP_STR "999999999"
693#define BC_MAX_VARS_STR "999999999"
694
695#define BC_MAX_OBASE_STR "999"
696
697#if INT_MAX == 2147483647
698# define BC_MAX_DIM_STR "2147483647"
699#elif INT_MAX == 9223372036854775807
700# define BC_MAX_DIM_STR "9223372036854775807"
701#else
702# error Strange INT_MAX
703#endif
704
705#if UINT_MAX == 4294967295
706# define BC_MAX_SCALE_STR "4294967295"
707# define BC_MAX_STRING_STR "4294967294"
708#elif UINT_MAX == 18446744073709551615
709# define BC_MAX_SCALE_STR "18446744073709551615"
710# define BC_MAX_STRING_STR "18446744073709551614"
711#else
712# error Strange UINT_MAX
713#endif
714#define BC_MAX_NUM_STR BC_MAX_STRING_STR
Gavin Howard01055ba2018-11-03 11:00:21 -0600715
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100716struct globals {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100717 IF_FEATURE_BC_SIGNALS(smallint ttyin;)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100718 IF_FEATURE_CLEAN_UP(smallint exiting;)
Denys Vlasenko69171dc2018-12-12 00:29:24 +0100719 smallint in_read;
Gavin Howard01055ba2018-11-03 11:00:21 -0600720 char sbgn;
721 char send;
Gavin Howard01055ba2018-11-03 11:00:21 -0600722
723 BcParse prs;
724 BcProgram prog;
725
Denys Vlasenko5318f812018-12-05 17:48:01 +0100726 // For error messages. Can be set to current parsed line,
727 // or [TODO] to current executing line (can be before last parsed one)
728 unsigned err_line;
729
Gavin Howard01055ba2018-11-03 11:00:21 -0600730 BcVec files;
731
732 char *env_args;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +0100733
734#if ENABLE_FEATURE_EDITING
735 line_input_t *line_input_state;
736#endif
Denys Vlasenko6d9146a2018-12-02 15:48:37 +0100737} FIX_ALIASING;
738#define G (*ptr_to_globals)
739#define INIT_G() do { \
740 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
741} while (0)
Denys Vlasenkoe873ff92018-12-06 00:29:22 +0100742#define FREE_G() do { \
743 FREE_PTR_TO_GLOBALS(); \
744} while (0)
Denys Vlasenkod70d4a02018-12-04 20:58:40 +0100745#define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S))
746#define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W))
Denys Vlasenko6d0be102018-12-06 18:41:59 +0100747#define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X))
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100748#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100749# define G_interrupt bb_got_signal
750# define G_ttyin G.ttyin
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100751#else
Denys Vlasenkob9c321d2018-12-07 12:41:42 +0100752# define G_interrupt 0
753# define G_ttyin 0
Denys Vlasenko1a6a4822018-12-06 09:20:32 +0100754#endif
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +0100755#if ENABLE_FEATURE_CLEAN_UP
756# define G_exiting G.exiting
757#else
758# define G_exiting 0
759#endif
Denys Vlasenko00d77792018-11-30 23:13:42 +0100760#define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
761
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100762#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -0600763
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100764// This is a bit array that corresponds to token types. An entry is
Gavin Howard01055ba2018-11-03 11:00:21 -0600765// true if the token is valid in an expression, false otherwise.
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100766enum {
767 BC_PARSE_EXPRS_BITS = 0
768 + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8))
769 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8))
770 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8))
771 + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8))
772 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8))
773 + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8))
774 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8))
775 + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8))
Gavin Howard01055ba2018-11-03 11:00:21 -0600776};
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100777static ALWAYS_INLINE long bc_parse_exprs(unsigned i)
778{
779#if ULONG_MAX > 0xffffffff
780 // 64-bit version (will not work correctly for 32-bit longs!)
781 return BC_PARSE_EXPRS_BITS & (1UL << i);
782#else
783 // 32-bit version
784 unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS;
785 if (i >= 32) {
786 m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32);
787 i &= 31;
788 }
789 return m & (1UL << i);
790#endif
791}
Gavin Howard01055ba2018-11-03 11:00:21 -0600792
793// This is an array of data for operators that correspond to token types.
Denys Vlasenko65437582018-12-05 19:37:19 +0100794static const uint8_t bc_parse_ops[] = {
795#define OP(p,l) ((int)(l) * 0x10 + (p))
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100796 OP(0, false), OP( 0, false ), // inc dec
797 OP(1, false), // neg
Denys Vlasenko65437582018-12-05 19:37:19 +0100798 OP(2, false),
Denys Vlasenkobcb62a72018-12-05 20:17:48 +0100799 OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div
800 OP(4, true ), OP( 4, true ), // mod + -
801 OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < >
802 OP(1, false), // not
803 OP(7, true ), OP( 7, true ), // or and
804 OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= +=
805 OP(5, false), OP( 5, false ), // -= =
Denys Vlasenko65437582018-12-05 19:37:19 +0100806#undef OP
Gavin Howard01055ba2018-11-03 11:00:21 -0600807};
Denys Vlasenko65437582018-12-05 19:37:19 +0100808#define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f)
809#define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10)
Gavin Howard01055ba2018-11-03 11:00:21 -0600810
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100811// Byte array of up to 4 BC_LEX's, packed into 32-bit word
812typedef uint32_t BcParseNext;
813
Gavin Howard01055ba2018-11-03 11:00:21 -0600814// These identify what tokens can come after expressions in certain cases.
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100815enum {
816#define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) )
817#define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff)
818#define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff)
819 bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF),
820 bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA),
821 bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF),
822 bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN),
823 bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET),
824 bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON),
825 bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF),
826#undef BC_PARSE_NEXT4
827#undef BC_PARSE_NEXT2
828#undef BC_PARSE_NEXT1
829};
Gavin Howard01055ba2018-11-03 11:00:21 -0600830#endif // ENABLE_BC
831
Denys Vlasenkoef869ec2018-12-02 18:49:16 +0100832#if ENABLE_DC
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100833static const //BcLexType - should be this type, but narrower type saves size:
834uint8_t
835dc_lex_regs[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600836 BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE,
837 BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON,
838 BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN,
839 BC_LEX_STORE_PUSH,
840};
841
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100842static const //BcLexType - should be this type
843uint8_t
844dc_lex_tokens[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600845 BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN,
846 BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID,
847 BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE,
848 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
849 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
850 BC_LEX_INVALID, BC_LEX_INVALID,
851 BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ,
852 BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID,
853 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
854 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID,
855 BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP,
856 BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM,
857 BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID,
858 BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID,
859 BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID,
860 BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID,
861 BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE,
862 BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID,
863 BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD,
864 BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT,
865 BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID,
866 BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE,
867 BC_LEX_INVALID, BC_LEX_STACK_LEVEL,
868 BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
869 BC_LEX_INVALID
870};
871
Denys Vlasenko18c6b542018-12-07 12:57:32 +0100872static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1
873int8_t
874dc_parse_insts[] = {
Gavin Howard01055ba2018-11-03 11:00:21 -0600875 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
876 BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
877 BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
878 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
879 BC_INST_INVALID, BC_INST_INVALID,
880 BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID,
881 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
882 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
883 BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID,
884 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE,
885 BC_INST_INVALID, BC_INST_INVALID,
886 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
887 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
888 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
889 BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
890 BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID,
891 BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
892 BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID,
893 BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK,
894 BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP,
895 BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID,
896 BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
897 BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC,
898};
899#endif // ENABLE_DC
900
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100901// In configurations where errors abort instead of propagating error
902// return code up the call chain, functions returning BC_STATUS
903// actually don't return anything, they always succeed and return "void".
904// A macro wrapper is provided, which makes this statement work:
905// s = zbc_func(...)
906// and makes it visible to the compiler that s is always zero,
907// allowing compiler to optimize dead code after the statement.
908//
909// To make code more readable, each such function has a "z"
910// ("always returning zero") prefix, i.e. zbc_foo or zdc_foo.
911//
912#if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100913# define ERRORS_ARE_FATAL 0
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100914# define ERRORFUNC /*nothing*/
915# define ERROR_RETURN(a) a
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100916//moved up: # define BC_STATUS BcStatus
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100917# define RETURN_STATUS(v) return (v)
918#else
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100919# define ERRORS_ARE_FATAL 1
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100920# define ERRORFUNC NORETURN
921# define ERROR_RETURN(a) /*nothing*/
Denys Vlasenko9a34e892018-12-12 13:58:55 +0100922//moved up: # define BC_STATUS void
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100923# define RETURN_STATUS(v) do { ((void)(v)); return; } while (0)
924#endif
925
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100926#define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg))
927#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
928#define BC_NUM_INT(n) ((n)->len - (n)->rdx)
929//#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1)
930static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b)
931{
932 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
933}
934//#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1)
935static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale)
936{
937 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
938}
939
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100940typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC;
941
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100942typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC;
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100943
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100944static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
945 BcNumBinaryOp op, size_t req);
946static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
947static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
948static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
949static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
950static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
951static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
952
953static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale)
954{
955 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s;
956 (void) scale;
957 RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)));
958}
959
960static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale)
961{
962 BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a;
963 (void) scale;
964 RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)));
965}
966
967static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale)
968{
969 size_t req = BC_NUM_MREQ(a, b, scale);
970 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req));
971}
972
973static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale)
974{
975 size_t req = BC_NUM_MREQ(a, b, scale);
976 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req));
977}
978
979static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale)
980{
981 size_t req = BC_NUM_MREQ(a, b, scale);
982 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req));
983}
984
985static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale)
986{
987 RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1));
988}
Denys Vlasenkoc2d15df2018-12-11 17:56:09 +0100989
Denys Vlasenko1aeacef2018-12-11 19:12:13 +0100990static const BcNumBinaryOp zbc_program_ops[] = {
991 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 -0600992};
Denys Vlasenko7f4daa42018-12-11 19:04:44 +0100993#if ERRORS_ARE_FATAL
994# define zbc_num_add(...) (zbc_num_add(__VA_ARGS__), BC_STATUS_SUCCESS)
995# define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__), BC_STATUS_SUCCESS)
996# define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__), BC_STATUS_SUCCESS)
997# define zbc_num_div(...) (zbc_num_div(__VA_ARGS__), BC_STATUS_SUCCESS)
998# define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__), BC_STATUS_SUCCESS)
999# define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__), BC_STATUS_SUCCESS)
1000#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001001
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001002static void fflush_and_check(void)
1003{
1004 fflush_all();
1005 if (ferror(stdout) || ferror(stderr))
1006 bb_perror_msg_and_die("output error");
1007}
1008
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001009#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001010#define QUIT_OR_RETURN_TO_MAIN \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001011do { \
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001012 IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001013 G_exiting = 1; \
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001014 return BC_STATUS_FAILURE; \
1015} while (0)
1016#else
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01001017#define QUIT_OR_RETURN_TO_MAIN quit()
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01001018#endif
1019
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001020static void quit(void) NORETURN;
1021static void quit(void)
1022{
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01001023 if (ferror(stdin))
1024 bb_perror_msg_and_die("input error");
1025 fflush_and_check();
1026 exit(0);
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01001027}
1028
Denys Vlasenko5318f812018-12-05 17:48:01 +01001029static void bc_verror_msg(const char *fmt, va_list p)
1030{
1031 const char *sv = sv; /* for compiler */
1032 if (G.prog.file) {
1033 sv = applet_name;
1034 applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line);
1035 }
1036 bb_verror_msg(fmt, p, NULL);
1037 if (G.prog.file) {
1038 free((char*)applet_name);
1039 applet_name = sv;
1040 }
1041}
1042
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001043static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001044{
1045 va_list p;
1046
1047 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001048 bc_verror_msg(fmt, p);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001049 va_end(p);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01001050
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001051 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001052 exit(1);
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001053 ERROR_RETURN(return BC_STATUS_FAILURE;)
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001054}
1055
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001056#if ENABLE_BC
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001057static NOINLINE int bc_posix_error_fmt(const char *fmt, ...)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001058{
1059 va_list p;
1060
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001061 // Are non-POSIX constructs totally ok?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001062 if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W)))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001063 return BC_STATUS_SUCCESS; // yes
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001064
1065 va_start(p, fmt);
Denys Vlasenko5318f812018-12-05 17:48:01 +01001066 bc_verror_msg(fmt, p);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001067 va_end(p);
1068
1069 // Do we treat non-POSIX constructs as errors?
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01001070 if (!(option_mask32 & BC_FLAG_S))
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001071 return BC_STATUS_SUCCESS; // no, it's a warning
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01001072 if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin)
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001073 exit(1);
1074 return BC_STATUS_FAILURE;
1075}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001076#endif
Denys Vlasenko9b70f192018-12-04 20:51:40 +01001077
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001078// We use error functions with "return bc_error(FMT[, PARAMS])" idiom.
1079// This idiom begs for tail-call optimization, but for it to work,
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001080// function must not have caller-cleaned parameters on stack.
1081// Unfortunately, vararg function API does exactly that on most arches.
1082// Thus, use these shims for the cases when we have no vararg PARAMS:
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001083static ERRORFUNC int bc_error(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001084{
Denys Vlasenko86e63cd2018-12-10 19:46:53 +01001085 ERROR_RETURN(return) bc_error_fmt("%s", msg);
1086}
1087static ERRORFUNC int bc_error_bad_character(char c)
1088{
1089 ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c);
1090}
1091static ERRORFUNC int bc_error_bad_expression(void)
1092{
1093 ERROR_RETURN(return) bc_error("bad expression");
1094}
1095static ERRORFUNC int bc_error_bad_token(void)
1096{
1097 ERROR_RETURN(return) bc_error("bad token");
1098}
1099static ERRORFUNC int bc_error_stack_has_too_few_elements(void)
1100{
1101 ERROR_RETURN(return) bc_error("stack has too few elements");
1102}
1103static ERRORFUNC int bc_error_variable_is_wrong_type(void)
1104{
1105 ERROR_RETURN(return) bc_error("variable is wrong type");
1106}
1107static ERRORFUNC int bc_error_nested_read_call(void)
1108{
1109 ERROR_RETURN(return) bc_error("read() call inside of a read() call");
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001110}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001111#if ENABLE_BC
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001112static int bc_POSIX_requires(const char *msg)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001113{
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01001114 return bc_posix_error_fmt("POSIX requires %s", msg);
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001115}
Denys Vlasenko00646792018-12-05 18:12:27 +01001116static int bc_POSIX_does_not_allow(const char *msg)
1117{
1118 return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg);
1119}
1120static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg)
1121{
1122 return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg);
1123}
1124static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg)
1125{
1126 return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg);
1127}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001128#endif
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01001129
Gavin Howard01055ba2018-11-03 11:00:21 -06001130static void bc_vec_grow(BcVec *v, size_t n)
1131{
1132 size_t cap = v->cap * 2;
1133 while (cap < v->len + n) cap *= 2;
1134 v->v = xrealloc(v->v, v->size * cap);
1135 v->cap = cap;
1136}
1137
1138static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor)
1139{
1140 v->size = esize;
1141 v->cap = BC_VEC_START_CAP;
1142 v->len = 0;
1143 v->dtor = dtor;
1144 v->v = xmalloc(esize * BC_VEC_START_CAP);
1145}
1146
Denys Vlasenko7d628012018-12-04 21:46:47 +01001147static void bc_char_vec_init(BcVec *v)
1148{
1149 bc_vec_init(v, sizeof(char), NULL);
1150}
1151
Gavin Howard01055ba2018-11-03 11:00:21 -06001152static void bc_vec_expand(BcVec *v, size_t req)
1153{
1154 if (v->cap < req) {
1155 v->v = xrealloc(v->v, v->size * req);
1156 v->cap = req;
1157 }
1158}
1159
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001160static void bc_vec_pop(BcVec *v)
1161{
1162 v->len--;
1163 if (v->dtor)
1164 v->dtor(v->v + (v->size * v->len));
1165}
Denys Vlasenko2fa11b62018-12-06 12:34:39 +01001166
Gavin Howard01055ba2018-11-03 11:00:21 -06001167static void bc_vec_npop(BcVec *v, size_t n)
1168{
1169 if (!v->dtor)
1170 v->len -= n;
1171 else {
1172 size_t len = v->len - n;
1173 while (v->len > len) v->dtor(v->v + (v->size * --v->len));
1174 }
1175}
1176
Denys Vlasenko7d628012018-12-04 21:46:47 +01001177static void bc_vec_pop_all(BcVec *v)
1178{
1179 bc_vec_npop(v, v->len);
1180}
1181
Gavin Howard01055ba2018-11-03 11:00:21 -06001182static void bc_vec_push(BcVec *v, const void *data)
1183{
1184 if (v->len + 1 > v->cap) bc_vec_grow(v, 1);
1185 memmove(v->v + (v->size * v->len), data, v->size);
1186 v->len += 1;
1187}
1188
1189static void bc_vec_pushByte(BcVec *v, char data)
1190{
1191 bc_vec_push(v, &data);
1192}
1193
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001194static void bc_vec_pushZeroByte(BcVec *v)
1195{
1196 //bc_vec_pushByte(v, '\0');
1197 // better:
1198 bc_vec_push(v, &const_int_0);
1199}
1200
Gavin Howard01055ba2018-11-03 11:00:21 -06001201static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx)
1202{
1203 if (idx == v->len)
1204 bc_vec_push(v, data);
1205 else {
1206
1207 char *ptr;
1208
1209 if (v->len == v->cap) bc_vec_grow(v, 1);
1210
1211 ptr = v->v + v->size * idx;
1212
1213 memmove(ptr + v->size, ptr, v->size * (v->len++ - idx));
1214 memmove(ptr, data, v->size);
1215 }
1216}
1217
1218static void bc_vec_string(BcVec *v, size_t len, const char *str)
1219{
Denys Vlasenko7d628012018-12-04 21:46:47 +01001220 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001221 bc_vec_expand(v, len + 1);
1222 memcpy(v->v, str, len);
1223 v->len = len;
1224
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001225 bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001226}
1227
1228static void bc_vec_concat(BcVec *v, const char *str)
1229{
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001230 size_t len, slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001231
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001232 if (v->len == 0) bc_vec_pushZeroByte(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001233
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001234 slen = strlen(str);
1235 len = v->len + slen;
Gavin Howard01055ba2018-11-03 11:00:21 -06001236
Denys Vlasenko8b4cf0d2018-12-10 15:12:58 +01001237 if (v->cap < len) bc_vec_grow(v, slen);
Denys Vlasenko1ff88622018-12-06 12:06:16 +01001238 strcpy(v->v + v->len - 1, str);
Gavin Howard01055ba2018-11-03 11:00:21 -06001239
1240 v->len = len;
1241}
1242
1243static void *bc_vec_item(const BcVec *v, size_t idx)
1244{
1245 return v->v + v->size * idx;
1246}
1247
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01001248static char** bc_program_str(size_t idx)
1249{
1250 return bc_vec_item(&G.prog.strs, idx);
1251}
1252
1253static BcFunc* bc_program_func(size_t idx)
1254{
1255 return bc_vec_item(&G.prog.fns, idx);
1256}
1257
Gavin Howard01055ba2018-11-03 11:00:21 -06001258static void *bc_vec_item_rev(const BcVec *v, size_t idx)
1259{
1260 return v->v + v->size * (v->len - idx - 1);
1261}
1262
Denys Vlasenkob23ac512018-12-06 13:10:56 +01001263static void *bc_vec_top(const BcVec *v)
1264{
1265 return v->v + v->size * (v->len - 1);
1266}
1267
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001268static FAST_FUNC void bc_vec_free(void *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001269{
1270 BcVec *v = (BcVec *) vec;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001271 bc_vec_pop_all(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06001272 free(v->v);
1273}
1274
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001275static int bc_id_cmp(const void *e1, const void *e2)
1276{
1277 return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name);
1278}
1279
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001280static FAST_FUNC void bc_id_free(void *id)
Denys Vlasenkocca79a02018-12-05 21:15:46 +01001281{
1282 free(((BcId *) id)->name);
1283}
1284
Gavin Howard01055ba2018-11-03 11:00:21 -06001285static size_t bc_map_find(const BcVec *v, const void *ptr)
1286{
1287 size_t low = 0, high = v->len;
1288
1289 while (low < high) {
1290
1291 size_t mid = (low + high) / 2;
1292 BcId *id = bc_vec_item(v, mid);
1293 int result = bc_id_cmp(ptr, id);
1294
1295 if (result == 0)
1296 return mid;
1297 else if (result < 0)
1298 high = mid;
1299 else
1300 low = mid + 1;
1301 }
1302
1303 return low;
1304}
1305
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001306static int bc_map_insert(BcVec *v, const void *ptr, size_t *i)
Gavin Howard01055ba2018-11-03 11:00:21 -06001307{
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001308 size_t n = *i = bc_map_find(v, ptr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001309
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001310 if (n == v->len)
Gavin Howard01055ba2018-11-03 11:00:21 -06001311 bc_vec_push(v, ptr);
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001312 else if (!bc_id_cmp(ptr, bc_vec_item(v, n)))
1313 return 0; // "was not inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001314 else
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01001315 bc_vec_pushAt(v, ptr, n);
1316 return 1; // "was inserted"
Gavin Howard01055ba2018-11-03 11:00:21 -06001317}
1318
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001319#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06001320static size_t bc_map_index(const BcVec *v, const void *ptr)
1321{
1322 size_t i = bc_map_find(v, ptr);
1323 if (i >= v->len) return BC_VEC_INVALID_IDX;
1324 return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i;
1325}
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01001326#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001327
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001328static int push_input_byte(BcVec *vec, char c)
1329{
1330 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1331 || c > 0x7e
1332 ) {
1333 // Bad chars on this line, ignore entire line
1334 bc_error_fmt("illegal character 0x%02x", c);
1335 return 1;
1336 }
1337 bc_vec_pushByte(vec, (char)c);
1338 return 0;
1339}
1340
Denys Vlasenko8a892472018-12-12 22:43:58 +01001341static void bc_read_line(BcVec *vec)
Gavin Howard01055ba2018-11-03 11:00:21 -06001342{
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001343 bool bad_chars;
Gavin Howard01055ba2018-11-03 11:00:21 -06001344
Denys Vlasenko00d77792018-11-30 23:13:42 +01001345 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001346 int c;
Gavin Howard01055ba2018-11-03 11:00:21 -06001347
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001348 bad_chars = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01001349 bc_vec_pop_all(vec);
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001350
1351 fflush_and_check();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001352
Gavin Howard01055ba2018-11-03 11:00:21 -06001353#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001354 if (G_interrupt) { // ^C was pressed
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001355 intr:
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001356 G_interrupt = 0;
Denys Vlasenkoac6ed112018-12-08 21:39:10 +01001357 // GNU bc says "interrupted execution."
1358 // GNU dc says "Interrupt!"
1359 fputs("\ninterrupted execution\n", stderr);
Gavin Howard01055ba2018-11-03 11:00:21 -06001360 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001361# if ENABLE_FEATURE_EDITING
1362 if (G_ttyin) {
1363 int n, i;
1364# define line_buf bb_common_bufsiz1
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01001365 n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE);
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001366 if (n <= 0) { // read errors or EOF, or ^D, or ^C
1367 if (n == 0) // ^C
1368 goto intr;
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001369 break;
1370 }
1371 i = 0;
1372 for (;;) {
1373 c = line_buf[i++];
1374 if (!c) break;
1375 bad_chars |= push_input_byte(vec, c);
1376 }
1377# undef line_buf
1378 } else
1379# endif
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001380#endif
Denys Vlasenkoed849352018-12-06 10:26:13 +01001381 {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001382 IF_FEATURE_BC_SIGNALS(errno = 0;)
1383 do {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001384 c = fgetc(stdin);
1385#if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING
1386 // Both conditions appear simultaneously, check both just in case
Denys Vlasenkob9c321d2018-12-07 12:41:42 +01001387 if (errno == EINTR || G_interrupt) {
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001388 // ^C was pressed
1389 clearerr(stdin);
1390 goto intr;
1391 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001392#endif
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001393 if (c == EOF) {
Denys Vlasenkoed849352018-12-06 10:26:13 +01001394 if (ferror(stdin))
1395 quit(); // this emits error message
Denys Vlasenkoed849352018-12-06 10:26:13 +01001396 // Note: EOF does not append '\n', therefore:
1397 // printf 'print 123\n' | bc - works
1398 // printf 'print 123' | bc - fails (syntax error)
1399 break;
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001400 }
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01001401 bad_chars |= push_input_byte(vec, c);
1402 } while (c != '\n');
Denys Vlasenkoed849352018-12-06 10:26:13 +01001403 }
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001404 } while (bad_chars);
Gavin Howard01055ba2018-11-03 11:00:21 -06001405
Denys Vlasenko08c033c2018-12-05 16:55:08 +01001406 bc_vec_pushZeroByte(vec);
Gavin Howard01055ba2018-11-03 11:00:21 -06001407}
1408
Denys Vlasenkodf515392018-12-02 19:27:48 +01001409static char* bc_read_file(const char *path)
Gavin Howard01055ba2018-11-03 11:00:21 -06001410{
Denys Vlasenkodf515392018-12-02 19:27:48 +01001411 char *buf;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001412 size_t size = ((size_t) -1);
1413 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06001414
Denys Vlasenko4c9455f2018-12-06 15:21:39 +01001415 // Never returns NULL (dies on errors)
1416 buf = xmalloc_xopen_read_close(path, &size);
Gavin Howard01055ba2018-11-03 11:00:21 -06001417
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01001418 for (i = 0; i < size; ++i) {
Denys Vlasenkoc1c24702018-12-03 16:06:02 +01001419 char c = buf[i];
1420 if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'?
1421 || c > 0x7e
1422 ) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01001423 free(buf);
1424 buf = NULL;
1425 break;
1426 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001427 }
1428
Denys Vlasenkodf515392018-12-02 19:27:48 +01001429 return buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06001430}
1431
Gavin Howard01055ba2018-11-03 11:00:21 -06001432static void bc_num_setToZero(BcNum *n, size_t scale)
1433{
1434 n->len = 0;
1435 n->neg = false;
1436 n->rdx = scale;
1437}
1438
1439static void bc_num_zero(BcNum *n)
1440{
1441 bc_num_setToZero(n, 0);
1442}
1443
1444static void bc_num_one(BcNum *n)
1445{
1446 bc_num_setToZero(n, 0);
1447 n->len = 1;
1448 n->num[0] = 1;
1449}
1450
1451static void bc_num_ten(BcNum *n)
1452{
1453 bc_num_setToZero(n, 0);
1454 n->len = 2;
1455 n->num[0] = 0;
1456 n->num[1] = 1;
1457}
1458
Denys Vlasenko3129f702018-12-09 12:04:44 +01001459// Note: this also sets BcNum to zero
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001460static void bc_num_init(BcNum *n, size_t req)
1461{
1462 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001463 //memset(n, 0, sizeof(BcNum)); - cleared by assignments below
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001464 n->num = xmalloc(req);
1465 n->cap = req;
Denys Vlasenko3129f702018-12-09 12:04:44 +01001466 n->rdx = 0;
1467 n->len = 0;
1468 n->neg = false;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001469}
1470
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01001471static void bc_num_init_DEF_SIZE(BcNum *n)
1472{
1473 bc_num_init(n, BC_NUM_DEF_SIZE);
1474}
1475
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001476static void bc_num_expand(BcNum *n, size_t req)
1477{
1478 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1479 if (req > n->cap) {
1480 n->num = xrealloc(n->num, req);
1481 n->cap = req;
1482 }
1483}
1484
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01001485static FAST_FUNC void bc_num_free(void *num)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001486{
1487 free(((BcNum *) num)->num);
1488}
1489
1490static void bc_num_copy(BcNum *d, BcNum *s)
1491{
1492 if (d != s) {
1493 bc_num_expand(d, s->cap);
1494 d->len = s->len;
1495 d->neg = s->neg;
1496 d->rdx = s->rdx;
1497 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1498 }
1499}
1500
Denys Vlasenko29301232018-12-11 15:29:32 +01001501static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001502{
1503 size_t i;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001504 unsigned long pow, result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001505
Denys Vlasenko29301232018-12-11 15:29:32 +01001506 if (n->neg) RETURN_STATUS(bc_error("negative number"));
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001507
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001508 for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001509
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001510 unsigned long prev = result, powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001511
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001512 result += ((unsigned long) n->num[i]) * pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001513 pow *= 10;
1514
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001515 if (result < prev || pow < powprev)
Denys Vlasenko29301232018-12-11 15:29:32 +01001516 RETURN_STATUS(bc_error("overflow"));
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001517 prev = result;
1518 powprev = pow;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001519 }
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01001520 *result_p = result;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001521
Denys Vlasenko29301232018-12-11 15:29:32 +01001522 RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001523}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001524#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001525# define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001526#endif
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001527
1528static void bc_num_ulong2num(BcNum *n, unsigned long val)
1529{
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001530 BcDig *ptr;
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001531
1532 bc_num_zero(n);
1533
1534 if (val == 0) return;
1535
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001536 if (ULONG_MAX == 0xffffffffUL)
1537 bc_num_expand(n, 10); // 10 digits: 4294967295
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001538 if (ULONG_MAX == 0xffffffffffffffffULL)
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001539 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
Denys Vlasenkoc665c182018-12-10 15:15:42 +01001540 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
Denys Vlasenkob696d9e2018-12-10 12:22:15 +01001541
1542 ptr = n->num;
1543 for (;;) {
1544 n->len++;
1545 *ptr++ = val % 10;
1546 val /= 10;
1547 if (val == 0) break;
1548 }
Denys Vlasenkob0e37612018-12-05 21:03:16 +01001549}
1550
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001551static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001552 size_t len)
1553{
1554 size_t i, j;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001555 for (i = 0; i < len; ++i) {
1556 for (a[i] -= b[i], j = 0; a[i + j] < 0;) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001557 a[i + j++] += 10;
1558 a[i + j] -= 1;
1559 }
1560 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001561}
1562
1563static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len)
1564{
1565 size_t i;
1566 int c = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001567 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i);
Gavin Howard01055ba2018-11-03 11:00:21 -06001568 return BC_NUM_NEG(i + 1, c < 0);
1569}
1570
1571static ssize_t bc_num_cmp(BcNum *a, BcNum *b)
1572{
1573 size_t i, min, a_int, b_int, diff;
1574 BcDig *max_num, *min_num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001575 bool a_max, neg;
Gavin Howard01055ba2018-11-03 11:00:21 -06001576 ssize_t cmp;
1577
1578 if (a == b) return 0;
1579 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
1580 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001581
1582 if (a->neg != b->neg) // signs of a and b differ
1583 // +a,-b = a>b = 1 or -a,+b = a<b = -1
1584 return (int)b->neg - (int)a->neg;
1585 neg = a->neg; // 1 if both negative, 0 if both positive
Gavin Howard01055ba2018-11-03 11:00:21 -06001586
1587 a_int = BC_NUM_INT(a);
1588 b_int = BC_NUM_INT(b);
1589 a_int -= b_int;
Gavin Howard01055ba2018-11-03 11:00:21 -06001590
1591 if (a_int != 0) return (ssize_t) a_int;
1592
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001593 a_max = (a->rdx > b->rdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001594 if (a_max) {
1595 min = b->rdx;
1596 diff = a->rdx - b->rdx;
1597 max_num = a->num + diff;
1598 min_num = b->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001599 // neg = (a_max == neg); - NOP (maps 1->1 and 0->0)
1600 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06001601 min = a->rdx;
1602 diff = b->rdx - a->rdx;
1603 max_num = b->num + diff;
1604 min_num = a->num;
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001605 neg = !neg; // same as "neg = (a_max == neg)"
Gavin Howard01055ba2018-11-03 11:00:21 -06001606 }
1607
1608 cmp = bc_num_compare(max_num, min_num, b_int + min);
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001609 if (cmp != 0) return BC_NUM_NEG(cmp, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001610
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001611 for (max_num -= diff, i = diff - 1; i < diff; --i) {
Denys Vlasenko251fbb52018-12-12 11:51:32 +01001612 if (max_num[i]) return BC_NUM_NEG(1, neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06001613 }
1614
1615 return 0;
1616}
1617
1618static void bc_num_truncate(BcNum *n, size_t places)
1619{
1620 if (places == 0) return;
1621
1622 n->rdx -= places;
1623
1624 if (n->len != 0) {
1625 n->len -= places;
1626 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
1627 }
1628}
1629
1630static void bc_num_extend(BcNum *n, size_t places)
1631{
1632 size_t len = n->len + places;
1633
1634 if (places != 0) {
1635
1636 if (n->cap < len) bc_num_expand(n, len);
1637
1638 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
1639 memset(n->num, 0, sizeof(BcDig) * places);
1640
1641 n->len += places;
1642 n->rdx += places;
1643 }
1644}
1645
1646static void bc_num_clean(BcNum *n)
1647{
1648 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
1649 if (n->len == 0)
1650 n->neg = false;
1651 else if (n->len < n->rdx)
1652 n->len = n->rdx;
1653}
1654
1655static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2)
1656{
1657 if (n->rdx < scale)
1658 bc_num_extend(n, scale - n->rdx);
1659 else
1660 bc_num_truncate(n, n->rdx - scale);
1661
1662 bc_num_clean(n);
1663 if (n->len != 0) n->neg = !neg1 != !neg2;
1664}
1665
1666static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
1667 BcNum *restrict b)
1668{
1669 if (idx < n->len) {
1670
1671 b->len = n->len - idx;
1672 a->len = idx;
1673 a->rdx = b->rdx = 0;
1674
1675 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
1676 memcpy(a->num, n->num, idx * sizeof(BcDig));
1677 }
1678 else {
1679 bc_num_zero(b);
1680 bc_num_copy(a, n);
1681 }
1682
1683 bc_num_clean(a);
1684 bc_num_clean(b);
1685}
1686
Denys Vlasenko29301232018-12-11 15:29:32 +01001687static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
Gavin Howard01055ba2018-11-03 11:00:21 -06001688{
Denys Vlasenko29301232018-12-11 15:29:32 +01001689 if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS);
Denys Vlasenko64074a12018-12-07 15:50:14 +01001690
1691 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
1692 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
1693 if (places + n->len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01001694 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01001695 }
Gavin Howard01055ba2018-11-03 11:00:21 -06001696
1697 if (n->rdx >= places)
1698 n->rdx -= places;
1699 else {
1700 bc_num_extend(n, places - n->rdx);
1701 n->rdx = 0;
1702 }
1703
1704 bc_num_clean(n);
1705
Denys Vlasenko29301232018-12-11 15:29:32 +01001706 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001707}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001708#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01001709# define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01001710#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001711
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001712static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001713{
1714 BcNum one;
1715 BcDig num[2];
1716
1717 one.cap = 2;
1718 one.num = num;
1719 bc_num_one(&one);
1720
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001721 RETURN_STATUS(zbc_num_div(&one, a, b, scale));
Gavin Howard01055ba2018-11-03 11:00:21 -06001722}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001723#if ERRORS_ARE_FATAL
1724# define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS)
1725#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001726
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001727static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001728{
1729 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
1730 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
1731 int carry, in;
1732
1733 // Because this function doesn't need to use scale (per the bc spec),
1734 // I am hijacking it to say whether it's doing an add or a subtract.
1735
1736 if (a->len == 0) {
1737 bc_num_copy(c, b);
1738 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001739 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001740 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001741 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001742 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001743 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001744 }
1745
1746 c->neg = a->neg;
1747 c->rdx = BC_MAX(a->rdx, b->rdx);
1748 min_rdx = BC_MIN(a->rdx, b->rdx);
1749 c->len = 0;
1750
1751 if (a->rdx > b->rdx) {
1752 diff = a->rdx - b->rdx;
1753 ptr = a->num;
1754 ptr_a = a->num + diff;
1755 ptr_b = b->num;
1756 }
1757 else {
1758 diff = b->rdx - a->rdx;
1759 ptr = b->num;
1760 ptr_a = a->num;
1761 ptr_b = b->num + diff;
1762 }
1763
1764 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
1765
1766 ptr_c += diff;
1767 a_int = BC_NUM_INT(a);
1768 b_int = BC_NUM_INT(b);
1769
1770 if (a_int > b_int) {
1771 min_int = b_int;
1772 max = a_int;
1773 ptr = ptr_a;
1774 }
1775 else {
1776 min_int = a_int;
1777 max = b_int;
1778 ptr = ptr_b;
1779 }
1780
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001781 for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001782 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
1783 carry = in / 10;
1784 ptr_c[i] = (BcDig)(in % 10);
1785 }
1786
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001787 for (; i < max + min_rdx; ++i, ++c->len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001788 in = ((int) ptr[i]) + carry;
1789 carry = in / 10;
1790 ptr_c[i] = (BcDig)(in % 10);
1791 }
1792
1793 if (carry != 0) c->num[c->len++] = (BcDig) carry;
1794
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001795 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001796}
1797
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001798static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub)
Gavin Howard01055ba2018-11-03 11:00:21 -06001799{
Gavin Howard01055ba2018-11-03 11:00:21 -06001800 ssize_t cmp;
1801 BcNum *minuend, *subtrahend;
1802 size_t start;
1803 bool aneg, bneg, neg;
1804
1805 // Because this function doesn't need to use scale (per the bc spec),
1806 // I am hijacking it to say whether it's doing an add or a subtract.
1807
1808 if (a->len == 0) {
1809 bc_num_copy(c, b);
1810 if (sub && c->len) c->neg = !c->neg;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001811 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001812 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001813 if (b->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001814 bc_num_copy(c, a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001815 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001816 }
1817
1818 aneg = a->neg;
1819 bneg = b->neg;
1820 a->neg = b->neg = false;
1821
1822 cmp = bc_num_cmp(a, b);
1823
1824 a->neg = aneg;
1825 b->neg = bneg;
1826
1827 if (cmp == 0) {
1828 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001829 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001830 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001831 if (cmp > 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001832 neg = a->neg;
1833 minuend = a;
1834 subtrahend = b;
1835 }
1836 else {
1837 neg = b->neg;
1838 if (sub) neg = !neg;
1839 minuend = b;
1840 subtrahend = a;
1841 }
1842
1843 bc_num_copy(c, minuend);
1844 c->neg = neg;
1845
1846 if (c->rdx < subtrahend->rdx) {
1847 bc_num_extend(c, subtrahend->rdx - c->rdx);
1848 start = 0;
1849 }
1850 else
1851 start = c->rdx - subtrahend->rdx;
1852
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001853 bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06001854
1855 bc_num_clean(c);
1856
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001857 RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary()
Gavin Howard01055ba2018-11-03 11:00:21 -06001858}
1859
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001860static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b,
Gavin Howard01055ba2018-11-03 11:00:21 -06001861 BcNum *restrict c)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001862#if ERRORS_ARE_FATAL
1863# define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS)
1864#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001865{
1866 BcStatus s;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001867 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06001868 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001869 bool aone;
Gavin Howard01055ba2018-11-03 11:00:21 -06001870
Gavin Howard01055ba2018-11-03 11:00:21 -06001871 if (a->len == 0 || b->len == 0) {
1872 bc_num_zero(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001873 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001874 }
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001875 aone = BC_NUM_ONE(a);
1876 if (aone || BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001877 bc_num_copy(c, aone ? b : a);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001878 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001879 }
1880
1881 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
1882 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
1883 {
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001884 size_t i, j, len;
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001885 unsigned carry;
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001886
Gavin Howard01055ba2018-11-03 11:00:21 -06001887 bc_num_expand(c, a->len + b->len + 1);
1888
1889 memset(c->num, 0, sizeof(BcDig) * c->cap);
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001890 c->len = len = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06001891
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001892 for (i = 0; i < b->len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06001893
Denys Vlasenkob692c2f2018-12-05 18:56:14 +01001894 carry = 0;
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01001895 for (j = 0; j < a->len; ++j) {
Denys Vlasenkob3cb9012018-12-05 19:05:32 +01001896 unsigned in = c->num[i + j];
1897 in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry;
1898 // note: compilers prefer _unsigned_ div/const
Gavin Howard01055ba2018-11-03 11:00:21 -06001899 carry = in / 10;
1900 c->num[i + j] = (BcDig)(in % 10);
1901 }
1902
1903 c->num[i + j] += (BcDig) carry;
1904 len = BC_MAX(len, i + j + !!carry);
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001905
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001906#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01001907 // a=2^1000000
1908 // a*a <- without check below, this will not be interruptible
1909 if (G_interrupt) return BC_STATUS_FAILURE;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001910#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06001911 }
1912
1913 c->len = len;
1914
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001915 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06001916 }
1917
1918 bc_num_init(&l1, max);
1919 bc_num_init(&h1, max);
1920 bc_num_init(&l2, max);
1921 bc_num_init(&h2, max);
1922 bc_num_init(&m1, max);
1923 bc_num_init(&m2, max);
1924 bc_num_init(&z0, max);
1925 bc_num_init(&z1, max);
1926 bc_num_init(&z2, max);
1927 bc_num_init(&temp, max + max);
1928
1929 bc_num_split(a, max2, &l1, &h1);
1930 bc_num_split(b, max2, &l2, &h2);
1931
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001932 s = zbc_num_add(&h1, &l1, &m1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001933 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001934 s = zbc_num_add(&h2, &l2, &m2, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001935 if (s) goto err;
1936
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001937 s = zbc_num_k(&h1, &h2, &z0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001938 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001939 s = zbc_num_k(&m1, &m2, &z1);
Gavin Howard01055ba2018-11-03 11:00:21 -06001940 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001941 s = zbc_num_k(&l1, &l2, &z2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001942 if (s) goto err;
1943
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001944 s = zbc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001945 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001946 s = zbc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001947 if (s) goto err;
1948
Denys Vlasenko29301232018-12-11 15:29:32 +01001949 s = zbc_num_shift(&z0, max2 * 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001950 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001951 s = zbc_num_shift(&z1, max2);
Gavin Howard01055ba2018-11-03 11:00:21 -06001952 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001953 s = zbc_num_add(&z0, &z1, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001954 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001955 s = zbc_num_add(&temp, &z2, c, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06001956
1957err:
1958 bc_num_free(&temp);
1959 bc_num_free(&z2);
1960 bc_num_free(&z1);
1961 bc_num_free(&z0);
1962 bc_num_free(&m2);
1963 bc_num_free(&m1);
1964 bc_num_free(&h2);
1965 bc_num_free(&l2);
1966 bc_num_free(&h1);
1967 bc_num_free(&l1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001968 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06001969}
1970
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01001971static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06001972{
1973 BcStatus s;
1974 BcNum cpa, cpb;
1975 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
1976
1977 scale = BC_MAX(scale, a->rdx);
1978 scale = BC_MAX(scale, b->rdx);
1979 scale = BC_MIN(a->rdx + b->rdx, scale);
1980 maxrdx = BC_MAX(maxrdx, scale);
1981
1982 bc_num_init(&cpa, a->len);
1983 bc_num_init(&cpb, b->len);
1984
1985 bc_num_copy(&cpa, a);
1986 bc_num_copy(&cpb, b);
1987 cpa.neg = cpb.neg = false;
1988
Denys Vlasenko29301232018-12-11 15:29:32 +01001989 s = zbc_num_shift(&cpa, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001990 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01001991 s = zbc_num_shift(&cpb, maxrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06001992 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01001993 s = zbc_num_k(&cpa, &cpb, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06001994 if (s) goto err;
1995
1996 maxrdx += scale;
1997 bc_num_expand(c, c->len + maxrdx);
1998
1999 if (c->len < maxrdx) {
2000 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
2001 c->len += maxrdx;
2002 }
2003
2004 c->rdx = maxrdx;
2005 bc_num_retireMul(c, scale, a->neg, b->neg);
2006
2007err:
2008 bc_num_free(&cpb);
2009 bc_num_free(&cpa);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002010 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002011}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002012#if ERRORS_ARE_FATAL
2013# define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS)
2014#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002015
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002016static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002017{
2018 BcStatus s = BC_STATUS_SUCCESS;
2019 BcDig *n, *p, q;
2020 size_t len, end, i;
2021 BcNum cp;
2022 bool zero = true;
2023
2024 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002025 RETURN_STATUS(bc_error("divide by zero"));
2026 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002027 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002028 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002029 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002030 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002031 bc_num_copy(c, a);
2032 bc_num_retireMul(c, scale, a->neg, b->neg);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002033 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002034 }
2035
2036 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
2037 bc_num_copy(&cp, a);
2038 len = b->len;
2039
2040 if (len > cp.len) {
2041 bc_num_expand(&cp, len + 2);
2042 bc_num_extend(&cp, len - cp.len);
2043 }
2044
2045 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
2046 cp.rdx -= b->rdx;
2047 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
2048
2049 if (b->rdx == b->len) {
2050 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
2051 len -= i - 1;
2052 }
2053
2054 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
2055
2056 // We want an extra zero in front to make things simpler.
2057 cp.num[cp.len++] = 0;
2058 end = cp.len - len;
2059
2060 bc_num_expand(c, cp.len);
2061
2062 bc_num_zero(c);
2063 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
2064 c->rdx = cp.rdx;
2065 c->len = cp.len;
2066 p = b->num;
2067
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002068 for (i = end - 1; !s && i < end; --i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002069 n = cp.num + i;
2070 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002071 bc_num_subArrays(n, p, len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002072 c->num[i] = q;
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002073#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenkof381a882018-12-05 19:21:34 +01002074 // a=2^100000
2075 // scale=40000
2076 // 1/a <- without check below, this will not be interruptible
2077 if (G_interrupt) {
2078 s = BC_STATUS_FAILURE;
2079 break;
2080 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002081#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002082 }
2083
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002084 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard01055ba2018-11-03 11:00:21 -06002085 bc_num_free(&cp);
2086
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002087 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002088}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002089#if ERRORS_ARE_FATAL
2090# define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS)
2091#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002092
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002093static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
Gavin Howard01055ba2018-11-03 11:00:21 -06002094 BcNum *restrict d, size_t scale, size_t ts)
2095{
2096 BcStatus s;
2097 BcNum temp;
2098 bool neg;
2099
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002100 if (b->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002101 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002102
2103 if (a->len == 0) {
2104 bc_num_setToZero(d, ts);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002105 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002106 }
2107
2108 bc_num_init(&temp, d->cap);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002109 s = zbc_num_d(a, b, c, scale);
Denys Vlasenkof381a882018-12-05 19:21:34 +01002110 if (s) goto err;
Gavin Howard01055ba2018-11-03 11:00:21 -06002111
2112 if (scale != 0) scale = ts;
2113
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002114 s = zbc_num_m(c, b, &temp, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002115 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002116 s = zbc_num_sub(a, &temp, d, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002117 if (s) goto err;
2118
2119 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
2120
2121 neg = d->neg;
2122 bc_num_retireMul(d, ts, a->neg, b->neg);
2123 d->neg = neg;
2124
2125err:
2126 bc_num_free(&temp);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002127 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002128}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002129#if ERRORS_ARE_FATAL
2130# define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS)
2131#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002132
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002133static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002134{
2135 BcStatus s;
2136 BcNum c1;
2137 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2138
2139 bc_num_init(&c1, len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002140 s = zbc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002141 bc_num_free(&c1);
2142
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002143 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002144}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002145#if ERRORS_ARE_FATAL
2146# define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS)
2147#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002148
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002149static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002150{
2151 BcStatus s = BC_STATUS_SUCCESS;
2152 BcNum copy;
2153 unsigned long pow;
2154 size_t i, powrdx, resrdx;
2155 bool neg, zero;
2156
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002157 if (b->rdx) RETURN_STATUS(bc_error("non integer number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002158
2159 if (b->len == 0) {
2160 bc_num_one(c);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002161 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002162 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002163 if (a->len == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002164 bc_num_setToZero(c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002165 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002166 }
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002167 if (BC_NUM_ONE(b)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002168 if (!b->neg)
2169 bc_num_copy(c, a);
2170 else
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002171 s = zbc_num_inv(a, c, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002172 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002173 }
2174
2175 neg = b->neg;
2176 b->neg = false;
2177
Denys Vlasenko29301232018-12-11 15:29:32 +01002178 s = zbc_num_ulong(b, &pow);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002179 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002180
2181 bc_num_init(&copy, a->len);
2182 bc_num_copy(&copy, a);
2183
Denys Vlasenko2d615fe2018-12-07 16:22:45 +01002184 if (!neg) {
2185 if (a->rdx > scale)
2186 scale = a->rdx;
2187 if (a->rdx * pow < scale)
2188 scale = a->rdx * pow;
2189 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002190
2191 b->neg = neg;
2192
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002193 for (powrdx = a->rdx; !(pow & 1); pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002194 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002195 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002196 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002197 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002198 //if (G_interrupt) {
2199 // s = BC_STATUS_FAILURE;
2200 // goto err;
2201 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002202 }
2203
Gavin Howard01055ba2018-11-03 11:00:21 -06002204 bc_num_copy(c, &copy);
2205
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002206 for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002207
2208 powrdx <<= 1;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002209 s = zbc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002210 if (s) goto err;
2211
2212 if (pow & 1) {
2213 resrdx += powrdx;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002214 s = zbc_num_mul(c, &copy, c, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002215 if (s) goto err;
2216 }
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002217 // Not needed: zbc_num_mul() has a check for ^C:
Denys Vlasenko06fa65b2018-12-05 19:00:58 +01002218 //if (G_interrupt) {
2219 // s = BC_STATUS_FAILURE;
2220 // goto err;
2221 //}
Gavin Howard01055ba2018-11-03 11:00:21 -06002222 }
2223
2224 if (neg) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002225 s = zbc_num_inv(c, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002226 if (s) goto err;
2227 }
2228
Gavin Howard01055ba2018-11-03 11:00:21 -06002229 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
2230
2231 // We can't use bc_num_clean() here.
2232 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
2233 if (zero) bc_num_setToZero(c, scale);
2234
2235err:
2236 bc_num_free(&copy);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002237 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002238}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002239#if ERRORS_ARE_FATAL
2240# define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS)
2241#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002242
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002243static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard01055ba2018-11-03 11:00:21 -06002244 BcNumBinaryOp op, size_t req)
2245{
2246 BcStatus s;
2247 BcNum num2, *ptr_a, *ptr_b;
2248 bool init = false;
2249
2250 if (c == a) {
2251 ptr_a = &num2;
2252 memcpy(ptr_a, c, sizeof(BcNum));
2253 init = true;
2254 }
2255 else
2256 ptr_a = a;
2257
2258 if (c == b) {
2259 ptr_b = &num2;
2260 if (c != a) {
2261 memcpy(ptr_b, c, sizeof(BcNum));
2262 init = true;
2263 }
2264 }
2265 else
2266 ptr_b = b;
2267
2268 if (init)
2269 bc_num_init(c, req);
2270 else
2271 bc_num_expand(c, req);
2272
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002273 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01002274 ERROR_RETURN(s =) op(ptr_a, ptr_b, c, scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06002275
2276 if (init) bc_num_free(&num2);
2277
Denys Vlasenko29301232018-12-11 15:29:32 +01002278 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002279}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002280#if ERRORS_ARE_FATAL
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002281# define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002282#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002283
Denys Vlasenko9311e012018-12-10 11:54:18 +01002284static bool bc_num_strValid(const char *val, size_t base)
2285{
2286 BcDig b;
2287 bool radix;
2288
2289 b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A');
2290 radix = false;
2291 for (;;) {
2292 BcDig c = *val++;
2293 if (c == '\0')
2294 break;
2295 if (c == '.') {
2296 if (radix) return false;
2297 radix = true;
2298 continue;
2299 }
2300 if (c < '0' || c >= b || (c > '9' && c < 'A'))
2301 return false;
2302 }
2303 return true;
2304}
2305
2306// Note: n is already "bc_num_zero()"ed,
2307// leading zeroes in "val" are removed
2308static void bc_num_parseDecimal(BcNum *n, const char *val)
2309{
2310 size_t len, i;
2311 const char *ptr;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002312
2313 len = strlen(val);
2314 if (len == 0)
2315 return;
2316
Denys Vlasenko9311e012018-12-10 11:54:18 +01002317 bc_num_expand(n, len);
2318
2319 ptr = strchr(val, '.');
2320
2321 n->rdx = 0;
2322 if (ptr != NULL)
2323 n->rdx = (size_t)((val + len) - (ptr + 1));
2324
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002325 for (i = 0; val[i]; ++i) {
2326 if (val[i] != '0' && val[i] != '.') {
2327 // Not entirely zero value - convert it, and exit
2328 i = len - 1;
2329 for (;;) {
2330 n->num[n->len] = val[i] - '0';
2331 ++n->len;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002332 skip_dot:
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002333 if ((ssize_t)--i == (ssize_t)-1) break;
2334 if (val[i] == '.') goto skip_dot;
2335 }
2336 break;
Denys Vlasenko9311e012018-12-10 11:54:18 +01002337 }
2338 }
Denys Vlasenkodafbc2c2018-12-10 15:38:52 +01002339 // if this is reached, the value is entirely zero
Denys Vlasenko9311e012018-12-10 11:54:18 +01002340}
2341
2342// Note: n is already "bc_num_zero()"ed,
2343// leading zeroes in "val" are removed
2344static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2345{
2346 BcStatus s;
2347 BcNum temp, mult, result;
2348 BcDig c = '\0';
2349 unsigned long v;
2350 size_t i, digits;
2351
2352 for (i = 0; ; ++i) {
2353 if (val[i] == '\0')
2354 return;
2355 if (val[i] != '.' && val[i] != '0')
2356 break;
2357 }
2358
2359 bc_num_init_DEF_SIZE(&temp);
2360 bc_num_init_DEF_SIZE(&mult);
2361
2362 for (;;) {
2363 c = *val++;
2364 if (c == '\0') goto int_err;
2365 if (c == '.') break;
2366
2367 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2368
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002369 s = zbc_num_mul(n, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002370 if (s) goto int_err;
2371 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002372 s = zbc_num_add(&mult, &temp, n, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002373 if (s) goto int_err;
2374 }
2375
2376 bc_num_init(&result, base->len);
2377 //bc_num_zero(&result); - already is
2378 bc_num_one(&mult);
2379
2380 digits = 0;
2381 for (;;) {
2382 c = *val++;
2383 if (c == '\0') break;
2384 digits++;
2385
2386 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
2387
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002388 s = zbc_num_mul(&result, base, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002389 if (s) goto err;
2390 bc_num_ulong2num(&temp, v);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002391 s = zbc_num_add(&result, &temp, &result, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002392 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002393 s = zbc_num_mul(&mult, base, &mult, 0);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002394 if (s) goto err;
2395 }
2396
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002397 s = zbc_num_div(&result, &mult, &result, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002398 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002399 s = zbc_num_add(n, &result, n, digits);
Denys Vlasenko9311e012018-12-10 11:54:18 +01002400 if (s) goto err;
2401
2402 if (n->len != 0) {
2403 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
2404 } else
2405 bc_num_zero(n);
2406
2407err:
2408 bc_num_free(&result);
2409int_err:
2410 bc_num_free(&mult);
2411 bc_num_free(&temp);
2412}
2413
Denys Vlasenko29301232018-12-11 15:29:32 +01002414static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
Gavin Howard01055ba2018-11-03 11:00:21 -06002415 size_t base_t)
2416{
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002417 if (!bc_num_strValid(val, base_t))
Denys Vlasenko29301232018-12-11 15:29:32 +01002418 RETURN_STATUS(bc_error("bad number string"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002419
Denys Vlasenko4a024c72018-12-09 13:21:54 +01002420 bc_num_zero(n);
2421 while (*val == '0') val++;
2422
Gavin Howard01055ba2018-11-03 11:00:21 -06002423 if (base_t == 10)
2424 bc_num_parseDecimal(n, val);
2425 else
2426 bc_num_parseBase(n, val, base);
2427
Denys Vlasenko29301232018-12-11 15:29:32 +01002428 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002429}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002430#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002431# define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002432#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002433
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002434static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
Gavin Howard01055ba2018-11-03 11:00:21 -06002435{
2436 BcStatus s;
2437 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
2438 size_t pow, len, digs, digs1, resrdx, req, times = 0;
2439 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
2440
2441 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
2442 bc_num_expand(b, req);
2443
2444 if (a->len == 0) {
2445 bc_num_setToZero(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002446 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002447 }
2448 else if (a->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002449 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002450 else if (BC_NUM_ONE(a)) {
2451 bc_num_one(b);
2452 bc_num_extend(b, scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002453 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002454 }
2455
2456 scale = BC_MAX(scale, a->rdx) + 1;
2457 len = a->len + scale;
2458
2459 bc_num_init(&num1, len);
2460 bc_num_init(&num2, len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002461 bc_num_init_DEF_SIZE(&half);
Gavin Howard01055ba2018-11-03 11:00:21 -06002462
2463 bc_num_one(&half);
2464 half.num[0] = 5;
2465 half.rdx = 1;
2466
2467 bc_num_init(&f, len);
2468 bc_num_init(&fprime, len);
2469
2470 x0 = &num1;
2471 x1 = &num2;
2472
2473 bc_num_one(x0);
2474 pow = BC_NUM_INT(a);
2475
2476 if (pow) {
2477
2478 if (pow & 1)
2479 x0->num[0] = 2;
2480 else
2481 x0->num[0] = 6;
2482
2483 pow -= 2 - (pow & 1);
2484
2485 bc_num_extend(x0, pow);
2486
2487 // Make sure to move the radix back.
2488 x0->rdx -= pow;
2489 }
2490
2491 x0->rdx = digs = digs1 = 0;
2492 resrdx = scale + 2;
2493 len = BC_NUM_INT(x0) + resrdx - 1;
2494
Denys Vlasenko71e1fc62018-12-02 19:43:34 +01002495 while (cmp != 0 || digs < len) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002496
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002497 s = zbc_num_div(a, x0, &f, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002498 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002499 s = zbc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002500 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002501 s = zbc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard01055ba2018-11-03 11:00:21 -06002502 if (s) goto err;
2503
2504 cmp = bc_num_cmp(x1, x0);
2505 digs = x1->len - (unsigned long long) llabs(cmp);
2506
2507 if (cmp == cmp2 && digs == digs1)
2508 times += 1;
2509 else
2510 times = 0;
2511
2512 resrdx += times > 4;
2513
2514 cmp2 = cmp1;
2515 cmp1 = cmp;
2516 digs1 = digs;
2517
2518 temp = x0;
2519 x0 = x1;
2520 x1 = temp;
2521 }
2522
Gavin Howard01055ba2018-11-03 11:00:21 -06002523 bc_num_copy(b, x0);
2524 scale -= 1;
2525 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
2526
2527err:
2528 bc_num_free(&fprime);
2529 bc_num_free(&f);
2530 bc_num_free(&half);
2531 bc_num_free(&num2);
2532 bc_num_free(&num1);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002533 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002534}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002535#if ERRORS_ARE_FATAL
2536# define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS)
2537#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002538
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002539static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d,
Gavin Howard01055ba2018-11-03 11:00:21 -06002540 size_t scale)
2541{
2542 BcStatus s;
2543 BcNum num2, *ptr_a;
2544 bool init = false;
2545 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
2546
2547 if (c == a) {
2548 memcpy(&num2, c, sizeof(BcNum));
2549 ptr_a = &num2;
2550 bc_num_init(c, len);
2551 init = true;
2552 }
2553 else {
2554 ptr_a = a;
2555 bc_num_expand(c, len);
2556 }
2557
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002558 s = zbc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard01055ba2018-11-03 11:00:21 -06002559
2560 if (init) bc_num_free(&num2);
2561
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002562 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002563}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002564#if ERRORS_ARE_FATAL
2565# define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
2566#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002567
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002568#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002569static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
Gavin Howard01055ba2018-11-03 11:00:21 -06002570{
2571 BcStatus s;
2572 BcNum base, exp, two, temp;
2573
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002574 if (c->len == 0)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002575 RETURN_STATUS(bc_error("divide by zero"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002576 if (a->rdx || b->rdx || c->rdx)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002577 RETURN_STATUS(bc_error("non integer number"));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002578 if (b->neg)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002579 RETURN_STATUS(bc_error("negative number"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002580
2581 bc_num_expand(d, c->len);
2582 bc_num_init(&base, c->len);
2583 bc_num_init(&exp, b->len);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002584 bc_num_init_DEF_SIZE(&two);
Gavin Howard01055ba2018-11-03 11:00:21 -06002585 bc_num_init(&temp, b->len);
2586
2587 bc_num_one(&two);
2588 two.num[0] = 2;
2589 bc_num_one(d);
2590
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002591 s = zbc_num_rem(a, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002592 if (s) goto err;
2593 bc_num_copy(&exp, b);
2594
2595 while (exp.len != 0) {
2596
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002597 s = zbc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002598 if (s) goto err;
2599
2600 if (BC_NUM_ONE(&temp)) {
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002601 s = zbc_num_mul(d, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002602 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002603 s = zbc_num_rem(&temp, c, d, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002604 if (s) goto err;
2605 }
2606
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002607 s = zbc_num_mul(&base, &base, &temp, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002608 if (s) goto err;
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01002609 s = zbc_num_rem(&temp, c, &base, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06002610 if (s) goto err;
2611 }
2612
2613err:
2614 bc_num_free(&temp);
2615 bc_num_free(&two);
2616 bc_num_free(&exp);
2617 bc_num_free(&base);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002618 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002619}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01002620#if ERRORS_ARE_FATAL
2621# define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
2622#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002623#endif // ENABLE_DC
2624
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002625#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01002626static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06002627{
2628 BcId a;
2629 size_t i;
2630
2631 for (i = 0; i < f->autos.len; ++i) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01002632 if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0)
Denys Vlasenko29301232018-12-11 15:29:32 +01002633 RETURN_STATUS(bc_error("function parameter or auto var has the same name as another"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002634 }
2635
2636 a.idx = var;
2637 a.name = name;
2638
2639 bc_vec_push(&f->autos, &a);
2640
Denys Vlasenko29301232018-12-11 15:29:32 +01002641 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002642}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002643#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002644# define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002645#endif
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01002646#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002647
2648static void bc_func_init(BcFunc *f)
2649{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002650 bc_char_vec_init(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06002651 bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);
2652 bc_vec_init(&f->labels, sizeof(size_t), NULL);
2653 f->nparams = 0;
2654}
2655
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002656static FAST_FUNC void bc_func_free(void *func)
Gavin Howard01055ba2018-11-03 11:00:21 -06002657{
2658 BcFunc *f = (BcFunc *) func;
2659 bc_vec_free(&f->code);
2660 bc_vec_free(&f->autos);
2661 bc_vec_free(&f->labels);
2662}
2663
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002664static void bc_array_expand(BcVec *a, size_t len);
2665
Gavin Howard01055ba2018-11-03 11:00:21 -06002666static void bc_array_init(BcVec *a, bool nums)
2667{
2668 if (nums)
2669 bc_vec_init(a, sizeof(BcNum), bc_num_free);
2670 else
2671 bc_vec_init(a, sizeof(BcVec), bc_vec_free);
2672 bc_array_expand(a, 1);
2673}
2674
Gavin Howard01055ba2018-11-03 11:00:21 -06002675static void bc_array_expand(BcVec *a, size_t len)
2676{
2677 BcResultData data;
2678
2679 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2680 while (len > a->len) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01002681 bc_num_init_DEF_SIZE(&data.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06002682 bc_vec_push(a, &data.n);
2683 }
2684 }
2685 else {
2686 while (len > a->len) {
2687 bc_array_init(&data.v, true);
2688 bc_vec_push(a, &data.v);
2689 }
2690 }
2691}
2692
Denys Vlasenkob0e37612018-12-05 21:03:16 +01002693static void bc_array_copy(BcVec *d, const BcVec *s)
2694{
2695 size_t i;
2696
2697 bc_vec_pop_all(d);
2698 bc_vec_expand(d, s->cap);
2699 d->len = s->len;
2700
2701 for (i = 0; i < s->len; ++i) {
2702 BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
2703 bc_num_init(dnum, snum->len);
2704 bc_num_copy(dnum, snum);
2705 }
2706}
2707
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002708static FAST_FUNC void bc_string_free(void *string)
Gavin Howard01055ba2018-11-03 11:00:21 -06002709{
2710 free(*((char **) string));
2711}
2712
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002713#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06002714static void bc_result_copy(BcResult *d, BcResult *src)
2715{
2716 d->t = src->t;
2717
2718 switch (d->t) {
2719
2720 case BC_RESULT_TEMP:
2721 case BC_RESULT_IBASE:
2722 case BC_RESULT_SCALE:
2723 case BC_RESULT_OBASE:
2724 {
2725 bc_num_init(&d->d.n, src->d.n.len);
2726 bc_num_copy(&d->d.n, &src->d.n);
2727 break;
2728 }
2729
2730 case BC_RESULT_VAR:
2731 case BC_RESULT_ARRAY:
2732 case BC_RESULT_ARRAY_ELEM:
2733 {
2734 d->d.id.name = xstrdup(src->d.id.name);
2735 break;
2736 }
2737
2738 case BC_RESULT_CONSTANT:
2739 case BC_RESULT_LAST:
2740 case BC_RESULT_ONE:
2741 case BC_RESULT_STR:
2742 {
2743 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
2744 break;
2745 }
2746 }
2747}
2748#endif // ENABLE_DC
2749
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01002750static FAST_FUNC void bc_result_free(void *result)
Gavin Howard01055ba2018-11-03 11:00:21 -06002751{
2752 BcResult *r = (BcResult *) result;
2753
2754 switch (r->t) {
2755
2756 case BC_RESULT_TEMP:
2757 case BC_RESULT_IBASE:
2758 case BC_RESULT_SCALE:
2759 case BC_RESULT_OBASE:
2760 {
2761 bc_num_free(&r->d.n);
2762 break;
2763 }
2764
2765 case BC_RESULT_VAR:
2766 case BC_RESULT_ARRAY:
2767 case BC_RESULT_ARRAY_ELEM:
2768 {
2769 free(r->d.id.name);
2770 break;
2771 }
2772
2773 default:
2774 {
2775 // Do nothing.
2776 break;
2777 }
2778 }
2779}
2780
2781static void bc_lex_lineComment(BcLex *l)
2782{
2783 l->t.t = BC_LEX_WHITESPACE;
2784 while (l->i < l->len && l->buf[l->i++] != '\n');
2785 --l->i;
2786}
2787
2788static void bc_lex_whitespace(BcLex *l)
2789{
2790 char c;
2791 l->t.t = BC_LEX_WHITESPACE;
2792 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
2793}
2794
Denys Vlasenko29301232018-12-11 15:29:32 +01002795static BC_STATUS zbc_lex_number(BcLex *l, char start)
Gavin Howard01055ba2018-11-03 11:00:21 -06002796{
2797 const char *buf = l->buf + l->i;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002798 size_t len, bslashes, i, ccnt;
2799 bool pt;
Gavin Howard01055ba2018-11-03 11:00:21 -06002800
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002801 pt = (start == '.');
Gavin Howard01055ba2018-11-03 11:00:21 -06002802 l->t.t = BC_LEX_NUMBER;
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002803 bslashes = 0;
2804 ccnt = i = 0;
2805 for (;;) {
2806 char c = buf[i];
2807 if (c == '\0')
2808 break;
2809 if (c == '\\' && buf[i + 1] == '\n') {
2810 i += 2;
2811 bslashes++;
2812 continue;
Gavin Howard01055ba2018-11-03 11:00:21 -06002813 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002814 if (!isdigit(c) && (c < 'A' || c > 'F')) {
2815 if (c != '.') break;
2816 // if '.' was already seen, stop on second one:
2817 if (pt) break;
2818 pt = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002819 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002820 // buf[i] is one of "0-9A-F."
2821 i++;
2822 if (c != '.')
2823 ccnt = i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002824 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002825 //i is buf[i] index of the first not-yet-parsed char
2826 l->i += i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002827
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002828 //ccnt is the number of chars in the number string, excluding possible
2829 //trailing "." and possible following trailing "\<newline>"(s).
2830 len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination
2831
Denys Vlasenko64074a12018-12-07 15:50:14 +01002832 // This check makes sense only if size_t is (much) larger than BC_MAX_NUM.
2833 if (SIZE_MAX > (BC_MAX_NUM | 0xff)) {
2834 if (len > BC_MAX_NUM)
Denys Vlasenko29301232018-12-11 15:29:32 +01002835 RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01002836 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002837
Denys Vlasenko7d628012018-12-04 21:46:47 +01002838 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002839 bc_vec_expand(&l->t.v, 1 + len);
Gavin Howard01055ba2018-11-03 11:00:21 -06002840 bc_vec_push(&l->t.v, &start);
2841
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002842 while (ccnt != 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06002843 // If we have hit a backslash, skip it. We don't have
2844 // to check for a newline because it's guaranteed.
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002845 if (*buf == '\\') {
2846 buf += 2;
2847 ccnt -= 2;
Gavin Howard01055ba2018-11-03 11:00:21 -06002848 continue;
2849 }
Denys Vlasenkoc355c4a2018-12-11 17:36:21 +01002850 bc_vec_push(&l->t.v, buf);
2851 buf++;
2852 ccnt--;
Gavin Howard01055ba2018-11-03 11:00:21 -06002853 }
2854
Denys Vlasenko08c033c2018-12-05 16:55:08 +01002855 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002856
Denys Vlasenko29301232018-12-11 15:29:32 +01002857 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002858}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002859#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01002860# define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01002861#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002862
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002863static void bc_lex_name(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002864{
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002865 size_t i;
2866 const char *buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06002867
2868 l->t.t = BC_LEX_NAME;
2869
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002870 i = 0;
2871 buf = l->buf + l->i - 1;
2872 for (;;) {
2873 char c = buf[i];
2874 if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break;
2875 i++;
2876 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002877
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002878#if 0 // We do not protect against people with gigabyte-long names
Denys Vlasenko64074a12018-12-07 15:50:14 +01002879 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
2880 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
2881 if (i > BC_MAX_STRING)
2882 return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]");
2883 }
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002884#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002885 bc_vec_string(&l->t.v, i, buf);
2886
2887 // Increment the index. We minus 1 because it has already been incremented.
2888 l->i += i - 1;
2889
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002890 //return BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002891}
2892
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002893static void bc_lex_init(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002894{
Denys Vlasenko7d628012018-12-04 21:46:47 +01002895 bc_char_vec_init(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06002896}
2897
2898static void bc_lex_free(BcLex *l)
2899{
2900 bc_vec_free(&l->t.v);
2901}
2902
Denys Vlasenko0409ad32018-12-05 16:39:22 +01002903static void bc_lex_file(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002904{
Denys Vlasenko5318f812018-12-05 17:48:01 +01002905 G.err_line = l->line = 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06002906 l->newline = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06002907}
2908
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002909static BC_STATUS zbc_lex_token(BcLex *l);
2910static BC_STATUS zdc_lex_token(BcLex *l);
2911
2912static BC_STATUS zcommon_lex_token(BcLex *l)
2913{
2914 if (IS_BC) {
2915 IF_BC(RETURN_STATUS(zbc_lex_token(l));)
2916 }
2917 IF_DC(RETURN_STATUS(zdc_lex_token(l));)
2918}
2919
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002920static BC_STATUS zbc_lex_next(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002921{
2922 BcStatus s;
2923
2924 l->t.last = l->t.t;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002925 if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file"));
Gavin Howard01055ba2018-11-03 11:00:21 -06002926
2927 l->line += l->newline;
Denys Vlasenko5318f812018-12-05 17:48:01 +01002928 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06002929 l->t.t = BC_LEX_EOF;
2930
2931 l->newline = (l->i == l->len);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002932 if (l->newline) RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002933
2934 // Loop until failure or we don't have whitespace. This
2935 // is so the parser doesn't get inundated with whitespace.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002936 s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06002937 do {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002938//TODO: replace pointer with if(IS_BC)
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01002939 ERROR_RETURN(s =) zcommon_lex_token(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002940 } while (!s && l->t.t == BC_LEX_WHITESPACE);
2941
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002942 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06002943}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002944#if ERRORS_ARE_FATAL
2945# define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__), BC_STATUS_SUCCESS)
2946#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002947
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002948static BC_STATUS zbc_lex_text(BcLex *l, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06002949{
2950 l->buf = text;
2951 l->i = 0;
2952 l->len = strlen(text);
2953 l->t.t = l->t.last = BC_LEX_INVALID;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002954 RETURN_STATUS(zbc_lex_next(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06002955}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002956#if ERRORS_ARE_FATAL
2957# define zbc_lex_text(...) (zbc_lex_text(__VA_ARGS__), BC_STATUS_SUCCESS)
2958#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06002959
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01002960#if ENABLE_BC
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002961static BC_STATUS zbc_lex_identifier(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06002962{
2963 BcStatus s;
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002964 unsigned i;
Gavin Howard01055ba2018-11-03 11:00:21 -06002965 const char *buf = l->buf + l->i - 1;
2966
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002967 for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) {
2968 const char *keyword8 = bc_lex_kws[i].name8;
2969 unsigned j = 0;
2970 while (buf[j] != '\0' && buf[j] == keyword8[j]) {
2971 j++;
2972 if (j == 8) goto match;
Gavin Howard01055ba2018-11-03 11:00:21 -06002973 }
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002974 if (keyword8[j] != '\0')
2975 continue;
2976 match:
2977 // buf starts with keyword bc_lex_kws[i]
2978 l->t.t = BC_LEX_KEY_1st_keyword + i;
Denys Vlasenkod00d2f92018-12-06 12:59:40 +01002979 if (!bc_lex_kws_POSIX(i)) {
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002980 s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002981 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01002982 }
2983
2984 // We minus 1 because the index has already been incremented.
2985 l->i += j - 1;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01002986 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06002987 }
2988
Denys Vlasenko88cfea62018-12-10 20:26:04 +01002989 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06002990
Denys Vlasenko0d7e46b2018-12-05 18:31:19 +01002991 if (l->t.v.len > 2) {
2992 // Prevent this:
2993 // >>> qwe=1
2994 // bc: POSIX only allows one character names; the following is bad: 'qwe=1
2995 // '
2996 unsigned len = strchrnul(buf, '\n') - buf;
2997 s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf);
2998 }
Gavin Howard01055ba2018-11-03 11:00:21 -06002999
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003000 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003001}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003002#if ERRORS_ARE_FATAL
3003# define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__), BC_STATUS_SUCCESS)
3004#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003005
Denys Vlasenko26819db2018-12-12 16:08:46 +01003006static BC_STATUS zbc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003007{
3008 size_t len, nls = 0, i = l->i;
3009 char c;
3010
3011 l->t.t = BC_LEX_STR;
3012
Denys Vlasenko26819db2018-12-12 16:08:46 +01003013 for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i])
3014 nls += (c == '\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06003015
3016 if (c == '\0') {
3017 l->i = i;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003018 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003019 }
3020
3021 len = i - l->i;
Denys Vlasenko64074a12018-12-07 15:50:14 +01003022 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3023 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3024 if (len > BC_MAX_STRING)
Denys Vlasenko9811ad02018-12-12 23:25:13 +01003025 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003026 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003027 bc_vec_string(&l->t.v, len, l->buf + l->i);
3028
3029 l->i = i + 1;
3030 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003031 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003032
Denys Vlasenko26819db2018-12-12 16:08:46 +01003033 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003034}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003035#if ERRORS_ARE_FATAL
3036# define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3037#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003038
3039static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without)
3040{
3041 if (l->buf[l->i] == '=') {
3042 ++l->i;
3043 l->t.t = with;
3044 }
3045 else
3046 l->t.t = without;
3047}
3048
Denys Vlasenko29301232018-12-11 15:29:32 +01003049static BC_STATUS zbc_lex_comment(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003050{
3051 size_t i, nls = 0;
3052 const char *buf = l->buf;
Gavin Howard01055ba2018-11-03 11:00:21 -06003053
3054 l->t.t = BC_LEX_WHITESPACE;
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003055 i = ++l->i;
3056 for (;;) {
3057 char c = buf[i];
3058 check_star:
3059 if (c == '*') {
3060 c = buf[++i];
3061 if (c == '/')
3062 break;
3063 goto check_star;
3064 }
3065 if (c == '\0') {
Gavin Howard01055ba2018-11-03 11:00:21 -06003066 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003067 RETURN_STATUS(bc_error("comment end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003068 }
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003069 nls += (c == '\n');
3070 i++;
Gavin Howard01055ba2018-11-03 11:00:21 -06003071 }
3072
Denys Vlasenkobc5ce662018-12-03 19:12:29 +01003073 l->i = i + 1;
Gavin Howard01055ba2018-11-03 11:00:21 -06003074 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003075 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003076
Denys Vlasenko29301232018-12-11 15:29:32 +01003077 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003078}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003079#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003080# define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01003081#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003082
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003083static BC_STATUS zbc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003084{
3085 BcStatus s = BC_STATUS_SUCCESS;
3086 char c = l->buf[l->i++], c2;
3087
3088 // This is the workhorse of the lexer.
3089 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003090 case '\0':
3091 case '\n':
Gavin Howard01055ba2018-11-03 11:00:21 -06003092 l->newline = true;
3093 l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE;
3094 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003095 case '\t':
3096 case '\v':
3097 case '\f':
3098 case '\r':
3099 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003100 bc_lex_whitespace(l);
3101 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003102 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003103 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
Gavin Howard01055ba2018-11-03 11:00:21 -06003104 if (l->t.t == BC_LEX_OP_BOOL_NOT) {
Denys Vlasenko00646792018-12-05 18:12:27 +01003105 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003106 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003107 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003108 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003109 case '"':
Denys Vlasenko26819db2018-12-12 16:08:46 +01003110 s = zbc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003111 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003112 case '#':
Denys Vlasenko00646792018-12-05 18:12:27 +01003113 s = bc_POSIX_does_not_allow("'#' script comments");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003114 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003115 bc_lex_lineComment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003116 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003117 case '%':
Gavin Howard01055ba2018-11-03 11:00:21 -06003118 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
3119 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003120 case '&':
Gavin Howard01055ba2018-11-03 11:00:21 -06003121 c2 = l->buf[l->i];
3122 if (c2 == '&') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003123 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003124 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003125 ++l->i;
3126 l->t.t = BC_LEX_OP_BOOL_AND;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003127 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003128 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003129 s = bc_error_bad_character('&');
Gavin Howard01055ba2018-11-03 11:00:21 -06003130 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003131 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003132 case '(':
3133 case ')':
Gavin Howard01055ba2018-11-03 11:00:21 -06003134 l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN);
3135 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003136 case '*':
Gavin Howard01055ba2018-11-03 11:00:21 -06003137 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
3138 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003139 case '+':
Gavin Howard01055ba2018-11-03 11:00:21 -06003140 c2 = l->buf[l->i];
3141 if (c2 == '+') {
3142 ++l->i;
3143 l->t.t = BC_LEX_OP_INC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003144 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003145 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
3146 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003147 case ',':
Gavin Howard01055ba2018-11-03 11:00:21 -06003148 l->t.t = BC_LEX_COMMA;
3149 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003150 case '-':
Gavin Howard01055ba2018-11-03 11:00:21 -06003151 c2 = l->buf[l->i];
3152 if (c2 == '-') {
3153 ++l->i;
3154 l->t.t = BC_LEX_OP_DEC;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003155 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003156 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
3157 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003158 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003159 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003160 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003161 else {
3162 l->t.t = BC_LEX_KEY_LAST;
Denys Vlasenko00646792018-12-05 18:12:27 +01003163 s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result");
Gavin Howard01055ba2018-11-03 11:00:21 -06003164 }
3165 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003166 case '/':
Gavin Howard01055ba2018-11-03 11:00:21 -06003167 c2 = l->buf[l->i];
3168 if (c2 == '*')
Denys Vlasenko29301232018-12-11 15:29:32 +01003169 s = zbc_lex_comment(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003170 else
3171 bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
3172 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003173 case '0':
3174 case '1':
3175 case '2':
3176 case '3':
3177 case '4':
3178 case '5':
3179 case '6':
3180 case '7':
3181 case '8':
3182 case '9':
3183 case 'A':
3184 case 'B':
3185 case 'C':
3186 case 'D':
3187 case 'E':
3188 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003189 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003190 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003191 case ';':
Gavin Howard01055ba2018-11-03 11:00:21 -06003192 l->t.t = BC_LEX_SCOLON;
3193 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003194 case '<':
Gavin Howard01055ba2018-11-03 11:00:21 -06003195 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
3196 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003197 case '=':
Gavin Howard01055ba2018-11-03 11:00:21 -06003198 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
3199 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003200 case '>':
Gavin Howard01055ba2018-11-03 11:00:21 -06003201 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
3202 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003203 case '[':
3204 case ']':
Gavin Howard01055ba2018-11-03 11:00:21 -06003205 l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET);
3206 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003207 case '\\':
Gavin Howard01055ba2018-11-03 11:00:21 -06003208 if (l->buf[l->i] == '\n') {
3209 l->t.t = BC_LEX_WHITESPACE;
3210 ++l->i;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003211 } else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003212 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003213 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003214 case '^':
Gavin Howard01055ba2018-11-03 11:00:21 -06003215 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
3216 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003217 case 'a':
3218 case 'b':
3219 case 'c':
3220 case 'd':
3221 case 'e':
3222 case 'f':
3223 case 'g':
3224 case 'h':
3225 case 'i':
3226 case 'j':
3227 case 'k':
3228 case 'l':
3229 case 'm':
3230 case 'n':
3231 case 'o':
3232 case 'p':
3233 case 'q':
3234 case 'r':
3235 case 's':
3236 case 't':
3237 case 'u':
3238 case 'v':
3239 case 'w':
3240 case 'x':
3241 case 'y':
3242 case 'z':
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003243 s = zbc_lex_identifier(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003244 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003245 case '{':
3246 case '}':
Gavin Howard01055ba2018-11-03 11:00:21 -06003247 l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE);
3248 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003249 case '|':
Gavin Howard01055ba2018-11-03 11:00:21 -06003250 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003251 if (c2 == '|') {
Denys Vlasenko00646792018-12-05 18:12:27 +01003252 s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||");
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003253 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06003254 ++l->i;
3255 l->t.t = BC_LEX_OP_BOOL_OR;
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003256 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003257 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003258 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003259 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003260 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003261 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003262 l->t.t = BC_LEX_INVALID;
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003263 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003264 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003265 }
3266
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003267 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003268}
3269#endif // ENABLE_BC
3270
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003271#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01003272static BC_STATUS zdc_lex_register(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003273{
Gavin Howard01055ba2018-11-03 11:00:21 -06003274 if (isspace(l->buf[l->i - 1])) {
3275 bc_lex_whitespace(l);
3276 ++l->i;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01003277 if (!G_exreg)
Denys Vlasenko29301232018-12-11 15:29:32 +01003278 RETURN_STATUS(bc_error("extended register"));
3279 bc_lex_name(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003280 }
3281 else {
Denys Vlasenko7d628012018-12-04 21:46:47 +01003282 bc_vec_pop_all(&l->t.v);
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003283 bc_vec_push(&l->t.v, &l->buf[l->i - 1]);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003284 bc_vec_pushZeroByte(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003285 l->t.t = BC_LEX_NAME;
3286 }
3287
Denys Vlasenko29301232018-12-11 15:29:32 +01003288 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003289}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003290#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003291# define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003292#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003293
Denys Vlasenko29301232018-12-11 15:29:32 +01003294static BC_STATUS zdc_lex_string(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003295{
3296 size_t depth = 1, nls = 0, i = l->i;
3297 char c;
3298
3299 l->t.t = BC_LEX_STR;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003300 bc_vec_pop_all(&l->t.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06003301
3302 for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) {
3303
3304 depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\'));
3305 depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\'));
3306 nls += (c == '\n');
3307
3308 if (depth) bc_vec_push(&l->t.v, &c);
3309 }
3310
3311 if (c == '\0') {
3312 l->i = i;
Denys Vlasenko29301232018-12-11 15:29:32 +01003313 RETURN_STATUS(bc_error("string end could not be found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003314 }
3315
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003316 bc_vec_pushZeroByte(&l->t.v);
Denys Vlasenko64074a12018-12-07 15:50:14 +01003317 // This check makes sense only if size_t is (much) larger than BC_MAX_STRING.
3318 if (SIZE_MAX > (BC_MAX_STRING | 0xff)) {
3319 if (i - l->i > BC_MAX_STRING)
Denys Vlasenko29301232018-12-11 15:29:32 +01003320 RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"));
Denys Vlasenko64074a12018-12-07 15:50:14 +01003321 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003322
3323 l->i = i;
3324 l->line += nls;
Denys Vlasenko5318f812018-12-05 17:48:01 +01003325 G.err_line = l->line;
Gavin Howard01055ba2018-11-03 11:00:21 -06003326
Denys Vlasenko29301232018-12-11 15:29:32 +01003327 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003328}
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003329#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01003330# define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko88cfea62018-12-10 20:26:04 +01003331#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003332
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003333static BC_STATUS zdc_lex_token(BcLex *l)
Gavin Howard01055ba2018-11-03 11:00:21 -06003334{
3335 BcStatus s = BC_STATUS_SUCCESS;
3336 char c = l->buf[l->i++], c2;
3337 size_t i;
3338
Denys Vlasenko51fb8aa2018-12-05 00:22:34 +01003339 for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) {
3340 if (l->t.last == dc_lex_regs[i])
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003341 RETURN_STATUS(zdc_lex_register(l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003342 }
3343
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01003344 if (c >= '%' && c <= '~'
3345 && (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID
3346 ) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003347 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003348 }
3349
3350 // This is the workhorse of the lexer.
3351 switch (c) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003352 case '\0':
Gavin Howard01055ba2018-11-03 11:00:21 -06003353 l->t.t = BC_LEX_EOF;
3354 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003355 case '\n':
3356 case '\t':
3357 case '\v':
3358 case '\f':
3359 case '\r':
3360 case ' ':
Gavin Howard01055ba2018-11-03 11:00:21 -06003361 l->newline = (c == '\n');
3362 bc_lex_whitespace(l);
3363 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003364 case '!':
Gavin Howard01055ba2018-11-03 11:00:21 -06003365 c2 = l->buf[l->i];
Gavin Howard01055ba2018-11-03 11:00:21 -06003366 if (c2 == '=')
3367 l->t.t = BC_LEX_OP_REL_NE;
3368 else if (c2 == '<')
3369 l->t.t = BC_LEX_OP_REL_LE;
3370 else if (c2 == '>')
3371 l->t.t = BC_LEX_OP_REL_GE;
3372 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003373 RETURN_STATUS(bc_error_bad_character(c));
Gavin Howard01055ba2018-11-03 11:00:21 -06003374 ++l->i;
3375 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003376 case '#':
Gavin Howard01055ba2018-11-03 11:00:21 -06003377 bc_lex_lineComment(l);
3378 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003379 case '.':
Gavin Howard01055ba2018-11-03 11:00:21 -06003380 if (isdigit(l->buf[l->i]))
Denys Vlasenko29301232018-12-11 15:29:32 +01003381 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003382 else
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003383 s = bc_error_bad_character(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003384 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003385 case '0':
3386 case '1':
3387 case '2':
3388 case '3':
3389 case '4':
3390 case '5':
3391 case '6':
3392 case '7':
3393 case '8':
3394 case '9':
3395 case 'A':
3396 case 'B':
3397 case 'C':
3398 case 'D':
3399 case 'E':
3400 case 'F':
Denys Vlasenko29301232018-12-11 15:29:32 +01003401 s = zbc_lex_number(l, c);
Gavin Howard01055ba2018-11-03 11:00:21 -06003402 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003403 case '[':
Denys Vlasenko29301232018-12-11 15:29:32 +01003404 s = zdc_lex_string(l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003405 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003406 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06003407 l->t.t = BC_LEX_INVALID;
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 }
3411
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003412 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003413}
3414#endif // ENABLE_DC
3415
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003416static void bc_program_addFunc(char *name, size_t *idx);
3417
Gavin Howard01055ba2018-11-03 11:00:21 -06003418static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx)
3419{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003420 bc_program_addFunc(name, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003421 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003422}
3423
Denys Vlasenkob23ac512018-12-06 13:10:56 +01003424#define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i))
3425
Gavin Howard01055ba2018-11-03 11:00:21 -06003426static void bc_parse_pushName(BcParse *p, char *name)
3427{
3428 size_t i = 0, len = strlen(name);
3429
3430 for (; i < len; ++i) bc_parse_push(p, name[i]);
3431 bc_parse_push(p, BC_PARSE_STREND);
3432
3433 free(name);
3434}
3435
3436static void bc_parse_pushIndex(BcParse *p, size_t idx)
3437{
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003438 size_t mask;
3439 unsigned amt;
Gavin Howard01055ba2018-11-03 11:00:21 -06003440
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003441 mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8);
3442 amt = sizeof(idx);
3443 do {
3444 if (idx & mask) break;
3445 mask >>= 8;
3446 amt--;
3447 } while (amt != 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06003448
3449 bc_parse_push(p, amt);
Denys Vlasenkoc2da68e2018-12-12 16:44:34 +01003450
3451 while (idx != 0) {
3452 bc_parse_push(p, (unsigned char)idx);
3453 idx >>= 8;
3454 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003455}
3456
3457static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs)
3458{
3459 char *num = xstrdup(p->l.t.v.v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003460 size_t idx = G.prog.consts.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06003461
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003462 bc_vec_push(&G.prog.consts, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06003463
3464 bc_parse_push(p, BC_INST_NUM);
3465 bc_parse_pushIndex(p, idx);
3466
3467 ++(*nexs);
3468 (*prev) = BC_INST_NUM;
3469}
3470
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003471static BC_STATUS zbc_parse_parse(BcParse *p);
3472static BC_STATUS zdc_parse_parse(BcParse *p);
3473
3474static BC_STATUS zcommon_parse(BcParse *p)
3475{
3476 if (IS_BC) {
3477 IF_BC(RETURN_STATUS(zbc_parse_parse(p));)
3478 }
3479 IF_DC(RETURN_STATUS(zdc_parse_parse(p));)
3480}
3481
Denys Vlasenko26819db2018-12-12 16:08:46 +01003482static BC_STATUS zbc_parse_text(BcParse *p, const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06003483{
3484 BcStatus s;
3485
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003486 p->func = bc_program_func(p->fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003487
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003488 if (!text[0] && !BC_PARSE_CAN_EXEC(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003489 p->l.t.t = BC_LEX_INVALID;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003490 s = BC_STATUS_SUCCESS;
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01003491 ERROR_RETURN(s =) zcommon_parse(p);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003492 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003493 if (!BC_PARSE_CAN_EXEC(p))
Denys Vlasenko26819db2018-12-12 16:08:46 +01003494 RETURN_STATUS(bc_error("file is not executable"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003495 }
3496
Denys Vlasenko26819db2018-12-12 16:08:46 +01003497 RETURN_STATUS(zbc_lex_text(&p->l, text));
Gavin Howard01055ba2018-11-03 11:00:21 -06003498}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003499#if ERRORS_ARE_FATAL
3500# define zbc_parse_text(...) (zbc_parse_text(__VA_ARGS__), BC_STATUS_SUCCESS)
3501#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003502
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003503// Called when parsing or execution detects a failure,
3504// resets execution structures.
3505static void bc_program_reset(void)
3506{
3507 BcFunc *f;
3508 BcInstPtr *ip;
3509
3510 bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1);
3511 bc_vec_pop_all(&G.prog.results);
3512
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003513 f = bc_program_func(0);
Denys Vlasenkob6f60862018-12-06 12:54:26 +01003514 ip = bc_vec_top(&G.prog.stack);
3515 ip->idx = f->code.len;
3516}
3517
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003518#define bc_parse_updateFunc(p, f) \
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01003519 ((p)->func = bc_program_func((p)->fidx = (f)))
Denys Vlasenkoe55a5722018-12-06 12:47:17 +01003520
Denys Vlasenko26819db2018-12-12 16:08:46 +01003521// Called when zbc/zdc_parse_parse() detects a failure,
Denys Vlasenkod38af482018-12-04 19:11:02 +01003522// resets parsing structures.
3523static void bc_parse_reset(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003524{
3525 if (p->fidx != BC_PROG_MAIN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003526 p->func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01003527 bc_vec_pop_all(&p->func->code);
3528 bc_vec_pop_all(&p->func->autos);
3529 bc_vec_pop_all(&p->func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06003530
3531 bc_parse_updateFunc(p, BC_PROG_MAIN);
3532 }
3533
3534 p->l.i = p->l.len;
3535 p->l.t.t = BC_LEX_EOF;
3536 p->auto_part = (p->nbraces = 0);
3537
3538 bc_vec_npop(&p->flags, p->flags.len - 1);
Denys Vlasenko7d628012018-12-04 21:46:47 +01003539 bc_vec_pop_all(&p->exits);
3540 bc_vec_pop_all(&p->conds);
3541 bc_vec_pop_all(&p->ops);
Gavin Howard01055ba2018-11-03 11:00:21 -06003542
Denys Vlasenkod38af482018-12-04 19:11:02 +01003543 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06003544}
3545
3546static void bc_parse_free(BcParse *p)
3547{
3548 bc_vec_free(&p->flags);
3549 bc_vec_free(&p->exits);
3550 bc_vec_free(&p->conds);
3551 bc_vec_free(&p->ops);
3552 bc_lex_free(&p->l);
3553}
3554
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003555static void bc_parse_create(BcParse *p, size_t func)
Gavin Howard01055ba2018-11-03 11:00:21 -06003556{
3557 memset(p, 0, sizeof(BcParse));
3558
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01003559 bc_lex_init(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003560 bc_vec_init(&p->flags, sizeof(uint8_t), NULL);
3561 bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL);
3562 bc_vec_init(&p->conds, sizeof(size_t), NULL);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01003563 bc_vec_pushZeroByte(&p->flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003564 bc_vec_init(&p->ops, sizeof(BcLexType), NULL);
3565
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01003566 // p->auto_part = p->nbraces = 0; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06003567 bc_parse_updateFunc(p, func);
3568}
3569
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01003570#if ENABLE_BC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01003571
3572#define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops)))
3573#define BC_PARSE_LEAF(p, rparen) \
3574 (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \
3575 (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST)
3576
3577// We can calculate the conversion between tokens and exprs by subtracting the
3578// position of the first operator in the lex enum and adding the position of the
3579// first in the expr enum. Note: This only works for binary operators.
3580#define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG))
3581
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003582static BC_STATUS zbc_parse_else(BcParse *p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003583static BC_STATUS zbc_parse_stmt(BcParse *p);
3584static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01003585static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003586#if ERRORS_ARE_FATAL
3587# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
3588# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
3589# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
3590#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003591
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003592static BC_STATUS zbc_parse_operator(BcParse *p, BcLexType type, size_t start,
Gavin Howard01055ba2018-11-03 11:00:21 -06003593 size_t *nexprs, bool next)
3594{
3595 BcStatus s = BC_STATUS_SUCCESS;
3596 BcLexType t;
Denys Vlasenko65437582018-12-05 19:37:19 +01003597 char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC);
3598 bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003599
3600 while (p->ops.len > start) {
3601
3602 t = BC_PARSE_TOP_OP(p);
3603 if (t == BC_LEX_LPAREN) break;
3604
Denys Vlasenko65437582018-12-05 19:37:19 +01003605 l = bc_parse_op_PREC(t - BC_LEX_OP_INC);
Gavin Howard01055ba2018-11-03 11:00:21 -06003606 if (l >= r && (l != r || !left)) break;
3607
3608 bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
3609 bc_vec_pop(&p->ops);
3610 *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
3611 }
3612
3613 bc_vec_push(&p->ops, &type);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003614 if (next) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003615
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003616 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003617}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003618#if ERRORS_ARE_FATAL
3619# define zbc_parse_operator(...) (zbc_parse_operator(__VA_ARGS__), BC_STATUS_SUCCESS)
3620#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003621
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003622static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs)
Gavin Howard01055ba2018-11-03 11:00:21 -06003623{
3624 BcLexType top;
3625
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003626 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003627 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003628 top = BC_PARSE_TOP_OP(p);
3629
3630 while (top != BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003631 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
3632
3633 bc_vec_pop(&p->ops);
3634 *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
3635
Denys Vlasenko60cf7472018-12-04 20:05:28 +01003636 if (p->ops.len <= ops_bgn)
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003637 RETURN_STATUS(bc_error_bad_expression());
Gavin Howard01055ba2018-11-03 11:00:21 -06003638 top = BC_PARSE_TOP_OP(p);
3639 }
3640
3641 bc_vec_pop(&p->ops);
3642
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003643 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003644}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003645#if ERRORS_ARE_FATAL
3646# define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__), BC_STATUS_SUCCESS)
3647#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003648
Denys Vlasenko26819db2018-12-12 16:08:46 +01003649static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003650{
3651 BcStatus s;
3652 bool comma = false;
3653 size_t nparams;
3654
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003655 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003656 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003657
3658 for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003659 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003660 s = zbc_parse_expr(p, flags, bc_parse_next_param);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003661 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003662
3663 comma = p->l.t.t == BC_LEX_COMMA;
3664 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003665 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003666 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003667 }
3668 }
3669
Denys Vlasenko26819db2018-12-12 16:08:46 +01003670 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003671 bc_parse_push(p, BC_INST_CALL);
3672 bc_parse_pushIndex(p, nparams);
3673
Denys Vlasenko26819db2018-12-12 16:08:46 +01003674 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003675}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003676#if ERRORS_ARE_FATAL
3677# define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__), BC_STATUS_SUCCESS)
3678#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003679
Denys Vlasenko26819db2018-12-12 16:08:46 +01003680static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003681{
3682 BcStatus s;
3683 BcId entry, *entry_ptr;
3684 size_t idx;
3685
3686 entry.name = name;
3687
Denys Vlasenko26819db2018-12-12 16:08:46 +01003688 s = zbc_parse_params(p, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06003689 if (s) goto err;
3690
3691 if (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003692 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003693 goto err;
3694 }
3695
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003696 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003697
3698 if (idx == BC_VEC_INVALID_IDX) {
3699 name = xstrdup(entry.name);
3700 bc_parse_addFunc(p, name, &idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003701 idx = bc_map_index(&G.prog.fn_map, &entry);
Gavin Howard01055ba2018-11-03 11:00:21 -06003702 free(entry.name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003703 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06003704 free(name);
3705
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003706 entry_ptr = bc_vec_item(&G.prog.fn_map, idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06003707 bc_parse_pushIndex(p, entry_ptr->idx);
3708
Denys Vlasenko26819db2018-12-12 16:08:46 +01003709 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003710
3711err:
3712 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003713 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003714}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003715#if ERRORS_ARE_FATAL
3716# define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__), BC_STATUS_SUCCESS)
3717#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003718
Denys Vlasenko26819db2018-12-12 16:08:46 +01003719static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003720{
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) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003729 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003730 if (s) goto err;
3731
3732 if (p->l.t.t == BC_LEX_RBRACKET) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003733 if (!(flags & BC_PARSE_ARRAY)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003734 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06003735 goto err;
3736 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003737 *type = BC_INST_ARRAY;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003738 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003739 *type = BC_INST_ARRAY_ELEM;
Gavin Howard01055ba2018-11-03 11:00:21 -06003740 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003741 s = zbc_parse_expr(p, flags, bc_parse_next_elem);
Gavin Howard01055ba2018-11-03 11:00:21 -06003742 if (s) goto err;
3743 }
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003744 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003745 if (s) goto err;
3746 bc_parse_push(p, *type);
3747 bc_parse_pushName(p, name);
3748 }
3749 else if (p->l.t.t == BC_LEX_LPAREN) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003750 if (flags & BC_PARSE_NOCALL) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003751 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003752 goto err;
3753 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003754 *type = BC_INST_CALL;
Denys Vlasenko26819db2018-12-12 16:08:46 +01003755 s = zbc_parse_call(p, name, flags);
3756 } else {
Gavin Howard01055ba2018-11-03 11:00:21 -06003757 *type = BC_INST_VAR;
3758 bc_parse_push(p, BC_INST_VAR);
3759 bc_parse_pushName(p, name);
3760 }
3761
Denys Vlasenko26819db2018-12-12 16:08:46 +01003762 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003763
3764err:
3765 free(name);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003766 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003767}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003768#if ERRORS_ARE_FATAL
3769# define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__), BC_STATUS_SUCCESS)
3770#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003771
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003772static BC_STATUS zbc_parse_read(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003773{
3774 BcStatus s;
3775
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003776 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003777 if (s) RETURN_STATUS(s);
3778 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003779
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003780 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003781 if (s) RETURN_STATUS(s);
3782 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003783
3784 bc_parse_push(p, BC_INST_READ);
3785
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003786 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003787}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003788#if ERRORS_ARE_FATAL
3789# define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__), BC_STATUS_SUCCESS)
3790#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003791
Denys Vlasenko26819db2018-12-12 16:08:46 +01003792static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
Gavin Howard01055ba2018-11-03 11:00:21 -06003793 BcInst *prev)
3794{
3795 BcStatus s;
3796
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003797 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003798 if (s) RETURN_STATUS(s);
3799 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003800
3801 flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY;
3802
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003803 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003804 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003805
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003806 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003807 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003808
Denys Vlasenko26819db2018-12-12 16:08:46 +01003809 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003810
3811 *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT;
3812 bc_parse_push(p, *prev);
3813
Denys Vlasenko26819db2018-12-12 16:08:46 +01003814 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003815}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003816#if ERRORS_ARE_FATAL
3817# define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
3818#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003819
Denys Vlasenko26819db2018-12-12 16:08:46 +01003820static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06003821{
3822 BcStatus s;
3823
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003824 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003825 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003826
3827 if (p->l.t.t != BC_LEX_LPAREN) {
3828 *type = BC_INST_SCALE;
3829 bc_parse_push(p, BC_INST_SCALE);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003830 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06003831 }
3832
3833 *type = BC_INST_SCALE_FUNC;
3834 flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
3835
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003836 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003837 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003838
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003839 s = zbc_parse_expr(p, flags, bc_parse_next_rel);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003840 if (s) RETURN_STATUS(s);
3841 if (p->l.t.t != BC_LEX_RPAREN)
3842 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003843 bc_parse_push(p, BC_INST_SCALE_FUNC);
3844
Denys Vlasenko26819db2018-12-12 16:08:46 +01003845 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003846}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003847#if ERRORS_ARE_FATAL
3848# define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__), BC_STATUS_SUCCESS)
3849#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003850
Denys Vlasenko26819db2018-12-12 16:08:46 +01003851static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr,
Gavin Howard01055ba2018-11-03 11:00:21 -06003852 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 {
Gavin Howard01055ba2018-11-03 11:00:21 -06003868 *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC);
3869 *paren_expr = true;
3870
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003871 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003872 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003873 type = p->l.t.t;
3874
3875 // Because we parse the next part of the expression
3876 // right here, we need to increment this.
3877 *nexprs = *nexprs + 1;
3878
3879 switch (type) {
Gavin Howard01055ba2018-11-03 11:00:21 -06003880 case BC_LEX_NAME:
Denys Vlasenko26819db2018-12-12 16:08:46 +01003881 s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06003882 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003883 case BC_LEX_KEY_IBASE:
3884 case BC_LEX_KEY_LAST:
3885 case BC_LEX_KEY_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06003886 bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003887 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06003888 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003889 case BC_LEX_KEY_SCALE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003890 s = zbc_lex_next(&p->l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01003891 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003892 if (p->l.t.t == BC_LEX_LPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003893 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003894 else
3895 bc_parse_push(p, BC_INST_SCALE);
3896 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003897 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01003898 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06003899 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06003900 }
3901
3902 if (!s) bc_parse_push(p, inst);
3903 }
3904
Denys Vlasenko26819db2018-12-12 16:08:46 +01003905 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003906}
Denys Vlasenko26819db2018-12-12 16:08:46 +01003907#if ERRORS_ARE_FATAL
3908# define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
3909#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003910
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003911static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06003912 bool rparen, size_t *nexprs)
3913{
3914 BcStatus s;
3915 BcLexType type;
3916 BcInst etype = *prev;
3917
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003918 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003919 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003920
3921 type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
3922 (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ?
3923 BC_LEX_OP_MINUS :
3924 BC_LEX_NEG;
3925 *prev = BC_PARSE_TOKEN_INST(type);
3926
3927 // We can just push onto the op stack because this is the largest
3928 // precedence operator that gets pushed. Inc/dec does not.
3929 if (type != BC_LEX_OP_MINUS)
3930 bc_vec_push(&p->ops, &type);
3931 else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003932 s = zbc_parse_operator(p, type, ops_bgn, nexprs, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06003933
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003934 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003935}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003936#if ERRORS_ARE_FATAL
3937# define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__), BC_STATUS_SUCCESS)
3938#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003939
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003940static BC_STATUS zbc_parse_string(BcParse *p, char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06003941{
3942 char *str = xstrdup(p->l.t.v.v);
3943
3944 bc_parse_push(p, BC_INST_STR);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01003945 bc_parse_pushIndex(p, G.prog.strs.len);
3946 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06003947 bc_parse_push(p, inst);
3948
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003949 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003950}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003951#if ERRORS_ARE_FATAL
3952# define zbc_parse_string(...) (zbc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
3953#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003954
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003955static BC_STATUS zbc_parse_print(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003956{
3957 BcStatus s;
3958 BcLexType type;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003959 bool comma;
Gavin Howard01055ba2018-11-03 11:00:21 -06003960
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003961 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003962 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003963
3964 type = p->l.t.t;
3965
3966 if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003967 RETURN_STATUS(bc_error("bad print statement"));
Gavin Howard01055ba2018-11-03 11:00:21 -06003968
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003969 comma = false;
3970 while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) {
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003971 if (type == BC_LEX_STR) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01003972 s = zbc_parse_string(p, BC_INST_PRINT_POP);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003973 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003974 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003975 s = zbc_parse_expr(p, 0, bc_parse_next_print);
3976 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06003977 bc_parse_push(p, BC_INST_PRINT_POP);
3978 }
3979
Gavin Howard01055ba2018-11-03 11:00:21 -06003980 comma = p->l.t.t == BC_LEX_COMMA;
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003981 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01003982 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003983 if (s) RETURN_STATUS(s);
Denys Vlasenkoebc41c92018-12-08 23:36:28 +01003984 }
Gavin Howard01055ba2018-11-03 11:00:21 -06003985 type = p->l.t.t;
3986 }
3987
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003988 if (comma) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06003989
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003990 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06003991}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003992#if ERRORS_ARE_FATAL
3993# define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__), BC_STATUS_SUCCESS)
3994#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06003995
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01003996static BC_STATUS zbc_parse_return(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06003997{
3998 BcStatus s;
3999 BcLexType t;
4000 bool paren;
4001
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004002 if (!BC_PARSE_FUNC(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004003
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004004 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004005 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004006
4007 t = p->l.t.t;
4008 paren = t == BC_LEX_LPAREN;
4009
4010 if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON)
4011 bc_parse_push(p, BC_INST_RET0);
4012 else {
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004013 s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004014 if (s == BC_STATUS_PARSE_EMPTY_EXP) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004015 bc_parse_push(p, BC_INST_RET0);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004016 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004017 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004018 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004019
4020 if (!paren || p->l.t.last != BC_LEX_RPAREN) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004021 s = bc_POSIX_requires("parentheses around return expressions");
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004022 ERROR_RETURN(if (s) RETURN_STATUS(s);)
Gavin Howard01055ba2018-11-03 11:00:21 -06004023 }
4024
4025 bc_parse_push(p, BC_INST_RET);
4026 }
4027
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004028 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004029}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004030#if ERRORS_ARE_FATAL
4031# define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__), BC_STATUS_SUCCESS)
4032#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004033
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004034static BC_STATUS zbc_parse_endBody(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004035{
4036 BcStatus s = BC_STATUS_SUCCESS;
4037
4038 if (p->flags.len <= 1 || (brace && p->nbraces == 0))
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004039 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004040
4041 if (brace) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004042 if (p->l.t.t != BC_LEX_RBRACE)
4043 RETURN_STATUS(bc_error_bad_token());
4044 if (!p->nbraces)
4045 RETURN_STATUS(bc_error_bad_token());
4046 --p->nbraces;
4047 s = zbc_lex_next(&p->l);
4048 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004049 }
4050
4051 if (BC_PARSE_IF(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004052 uint8_t *flag_ptr;
4053
4054 while (p->l.t.t == BC_LEX_NLINE) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004055 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004056 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004057 }
4058
4059 bc_vec_pop(&p->flags);
4060
4061 flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4062 *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
4063
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004064 if (p->l.t.t == BC_LEX_KEY_ELSE)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004065 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004066 }
4067 else if (BC_PARSE_ELSE(p)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004068 BcInstPtr *ip;
4069 size_t *label;
4070
4071 bc_vec_pop(&p->flags);
4072
4073 ip = bc_vec_top(&p->exits);
4074 label = bc_vec_item(&p->func->labels, ip->idx);
4075 *label = p->func->code.len;
4076
4077 bc_vec_pop(&p->exits);
4078 }
4079 else if (BC_PARSE_FUNC_INNER(p)) {
4080 bc_parse_push(p, BC_INST_RET0);
4081 bc_parse_updateFunc(p, BC_PROG_MAIN);
4082 bc_vec_pop(&p->flags);
4083 }
4084 else {
Gavin Howard01055ba2018-11-03 11:00:21 -06004085 BcInstPtr *ip = bc_vec_top(&p->exits);
4086 size_t *label = bc_vec_top(&p->conds);
4087
4088 bc_parse_push(p, BC_INST_JUMP);
4089 bc_parse_pushIndex(p, *label);
4090
4091 label = bc_vec_item(&p->func->labels, ip->idx);
4092 *label = p->func->code.len;
4093
4094 bc_vec_pop(&p->flags);
4095 bc_vec_pop(&p->exits);
4096 bc_vec_pop(&p->conds);
4097 }
4098
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004099 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004100}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004101#if ERRORS_ARE_FATAL
4102# define zbc_parse_endBody(...) (zbc_parse_endBody(__VA_ARGS__), BC_STATUS_SUCCESS)
4103#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004104
4105static void bc_parse_startBody(BcParse *p, uint8_t flags)
4106{
4107 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4108 flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
4109 flags |= BC_PARSE_FLAG_BODY;
4110 bc_vec_push(&p->flags, &flags);
4111}
4112
4113static void bc_parse_noElse(BcParse *p)
4114{
4115 BcInstPtr *ip;
4116 size_t *label;
4117 uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
4118
4119 *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
4120
4121 ip = bc_vec_top(&p->exits);
4122 label = bc_vec_item(&p->func->labels, ip->idx);
4123 *label = p->func->code.len;
4124
4125 bc_vec_pop(&p->exits);
4126}
4127
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004128static BC_STATUS zbc_parse_if(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004129{
4130 BcStatus s;
4131 BcInstPtr ip;
4132
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004133 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004134 if (s) RETURN_STATUS(s);
4135 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004136
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004137 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004138 if (s) RETURN_STATUS(s);
4139 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4140 if (s) RETURN_STATUS(s);
4141 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004142
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);
Gavin Howard01055ba2018-11-03 11:00:21 -06004145 bc_parse_push(p, BC_INST_JUMP_ZERO);
4146
4147 ip.idx = p->func->labels.len;
4148 ip.func = ip.len = 0;
4149
4150 bc_parse_pushIndex(p, ip.idx);
4151 bc_vec_push(&p->exits, &ip);
4152 bc_vec_push(&p->func->labels, &ip.idx);
4153 bc_parse_startBody(p, BC_PARSE_FLAG_IF);
4154
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004155 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004156}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004157#if ERRORS_ARE_FATAL
4158# define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__), BC_STATUS_SUCCESS)
4159#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004160
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004161#undef zbc_parse_else
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004162static BC_STATUS zbc_parse_else(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004163{
4164 BcInstPtr ip;
4165
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004166 if (!BC_PARSE_IF_END(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004167
4168 ip.idx = p->func->labels.len;
4169 ip.func = ip.len = 0;
4170
4171 bc_parse_push(p, BC_INST_JUMP);
4172 bc_parse_pushIndex(p, ip.idx);
4173
4174 bc_parse_noElse(p);
4175
4176 bc_vec_push(&p->exits, &ip);
4177 bc_vec_push(&p->func->labels, &ip.idx);
4178 bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
4179
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004180 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004181}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004182#if ERRORS_ARE_FATAL
4183# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
4184#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004185
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004186static BC_STATUS zbc_parse_while(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004187{
4188 BcStatus s;
4189 BcInstPtr ip;
4190
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004191 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004192 if (s) RETURN_STATUS(s);
4193 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004194 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004195 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004196
4197 ip.idx = p->func->labels.len;
4198
4199 bc_vec_push(&p->func->labels, &p->func->code.len);
4200 bc_vec_push(&p->conds, &ip.idx);
4201
4202 ip.idx = p->func->labels.len;
4203 ip.func = 1;
4204 ip.len = 0;
4205
4206 bc_vec_push(&p->exits, &ip);
4207 bc_vec_push(&p->func->labels, &ip.idx);
4208
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004209 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel);
4210 if (s) RETURN_STATUS(s);
4211 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004212 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004213 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004214
4215 bc_parse_push(p, BC_INST_JUMP_ZERO);
4216 bc_parse_pushIndex(p, ip.idx);
4217 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4218
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004219 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004220}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004221#if ERRORS_ARE_FATAL
4222# define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__), BC_STATUS_SUCCESS)
4223#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004224
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004225static BC_STATUS zbc_parse_for(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004226{
4227 BcStatus s;
4228 BcInstPtr ip;
4229 size_t cond_idx, exit_idx, body_idx, update_idx;
4230
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004231 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004232 if (s) RETURN_STATUS(s);
4233 if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004234 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004235 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004236
4237 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004238 s = zbc_parse_expr(p, 0, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004239 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004240 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init");
Gavin Howard01055ba2018-11-03 11:00:21 -06004241
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004242 if (s) RETURN_STATUS(s);
4243 if (p->l.t.t != BC_LEX_SCOLON) 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 cond_idx = p->func->labels.len;
4248 update_idx = cond_idx + 1;
4249 body_idx = update_idx + 1;
4250 exit_idx = body_idx + 1;
4251
4252 bc_vec_push(&p->func->labels, &p->func->code.len);
4253
4254 if (p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004255 s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for);
Gavin Howard01055ba2018-11-03 11:00:21 -06004256 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004257 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004258
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004259 if (s) RETURN_STATUS(s);
4260 if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004261
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004262 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004263 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004264
4265 bc_parse_push(p, BC_INST_JUMP_ZERO);
4266 bc_parse_pushIndex(p, exit_idx);
4267 bc_parse_push(p, BC_INST_JUMP);
4268 bc_parse_pushIndex(p, body_idx);
4269
4270 ip.idx = p->func->labels.len;
4271
4272 bc_vec_push(&p->conds, &update_idx);
4273 bc_vec_push(&p->func->labels, &p->func->code.len);
4274
4275 if (p->l.t.t != BC_LEX_RPAREN)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004276 s = zbc_parse_expr(p, 0, bc_parse_next_rel);
Gavin Howard01055ba2018-11-03 11:00:21 -06004277 else
Denys Vlasenko00646792018-12-05 18:12:27 +01004278 s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update");
Gavin Howard01055ba2018-11-03 11:00:21 -06004279
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004280 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004281
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004282 if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004283 bc_parse_push(p, BC_INST_JUMP);
4284 bc_parse_pushIndex(p, cond_idx);
4285 bc_vec_push(&p->func->labels, &p->func->code.len);
4286
4287 ip.idx = exit_idx;
4288 ip.func = 1;
4289 ip.len = 0;
4290
4291 bc_vec_push(&p->exits, &ip);
4292 bc_vec_push(&p->func->labels, &ip.idx);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004293 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004294 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004295 bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
4296
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004297 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004298}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004299#if ERRORS_ARE_FATAL
4300# define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__), BC_STATUS_SUCCESS)
4301#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004302
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004303static BC_STATUS zbc_parse_loopExit(BcParse *p, BcLexType type)
Gavin Howard01055ba2018-11-03 11:00:21 -06004304{
4305 BcStatus s;
4306 size_t i;
4307 BcInstPtr *ip;
4308
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004309 if (!BC_PARSE_LOOP(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004310
4311 if (type == BC_LEX_KEY_BREAK) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004312 if (p->exits.len == 0) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004313
4314 i = p->exits.len - 1;
4315 ip = bc_vec_item(&p->exits, i);
4316
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004317 while (!ip->func && i < p->exits.len)
4318 ip = bc_vec_item(&p->exits, i--);
4319 if (i >= p->exits.len && !ip->func)
4320 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004321
4322 i = ip->idx;
4323 }
4324 else
4325 i = *((size_t *) bc_vec_top(&p->conds));
4326
4327 bc_parse_push(p, BC_INST_JUMP);
4328 bc_parse_pushIndex(p, i);
4329
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004330 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004331 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004332
4333 if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004334 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004335
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004336 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004337}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004338#if ERRORS_ARE_FATAL
4339# define zbc_parse_loopExit(...) (zbc_parse_loopExit(__VA_ARGS__), BC_STATUS_SUCCESS)
4340#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004341
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004342static BC_STATUS zbc_parse_func(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004343{
4344 BcStatus s;
4345 bool var, comma = false;
4346 uint8_t flags;
4347 char *name;
4348
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004349 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004350 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004351 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004352 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004353
4354 name = xstrdup(p->l.t.v.v);
4355 bc_parse_addFunc(p, name, &p->fidx);
4356
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004357 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004358 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004359 if (p->l.t.t != BC_LEX_LPAREN)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004360 RETURN_STATUS(bc_error("bad function definition"));
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004361 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004362 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004363
4364 while (p->l.t.t != BC_LEX_RPAREN) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004365 if (p->l.t.t != BC_LEX_NAME)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004366 RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004367
4368 ++p->func->nparams;
4369
4370 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004371 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004372 if (s) goto err;
4373
4374 var = p->l.t.t != BC_LEX_LBRACKET;
4375
4376 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004377 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004378 if (s) goto err;
4379
4380 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004381 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004382 goto err;
4383 }
4384
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004385 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004386 if (s) goto err;
4387 }
4388
4389 comma = p->l.t.t == BC_LEX_COMMA;
4390 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004391 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004392 if (s) goto err;
4393 }
4394
Denys Vlasenko29301232018-12-11 15:29:32 +01004395 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004396 if (s) goto err;
4397 }
4398
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004399 if (comma) RETURN_STATUS(bc_error("bad function definition"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004400
4401 flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
4402 bc_parse_startBody(p, flags);
4403
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004404 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004405 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004406
4407 if (p->l.t.t != BC_LEX_LBRACE)
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004408 s = bc_POSIX_requires("the left brace be on the same line as the function header");
Gavin Howard01055ba2018-11-03 11:00:21 -06004409
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004410 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004411
4412err:
4413 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004414 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004415}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004416#if ERRORS_ARE_FATAL
4417# define zbc_parse_func(...) (zbc_parse_func(__VA_ARGS__), BC_STATUS_SUCCESS)
4418#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004419
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004420static BC_STATUS zbc_parse_auto(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004421{
4422 BcStatus s;
4423 bool comma, var, one;
4424 char *name;
4425
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004426 if (!p->auto_part) RETURN_STATUS(bc_error_bad_token());
4427
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004428 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004429 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004430
4431 p->auto_part = comma = false;
4432 one = p->l.t.t == BC_LEX_NAME;
4433
4434 while (p->l.t.t == BC_LEX_NAME) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004435 name = xstrdup(p->l.t.v.v);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004436 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004437 if (s) goto err;
4438
4439 var = p->l.t.t != BC_LEX_LBRACKET;
4440 if (!var) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004441 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004442 if (s) goto err;
4443
4444 if (p->l.t.t != BC_LEX_RBRACKET) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004445 s = bc_error("bad function definition");
Gavin Howard01055ba2018-11-03 11:00:21 -06004446 goto err;
4447 }
4448
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004449 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004450 if (s) goto err;
4451 }
4452
4453 comma = p->l.t.t == BC_LEX_COMMA;
4454 if (comma) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004455 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004456 if (s) goto err;
4457 }
4458
Denys Vlasenko29301232018-12-11 15:29:32 +01004459 s = zbc_func_insert(p->func, name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06004460 if (s) goto err;
4461 }
4462
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004463 if (comma) RETURN_STATUS(bc_error("bad function definition"));
4464 if (!one) RETURN_STATUS(bc_error("no auto variable found"));
Gavin Howard01055ba2018-11-03 11:00:21 -06004465
4466 if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004467 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004468
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004469 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004470
4471err:
4472 free(name);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004473 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004474}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004475#if ERRORS_ARE_FATAL
4476# define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__), BC_STATUS_SUCCESS)
4477#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004478
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004479static BC_STATUS zbc_parse_body(BcParse *p, bool brace)
Gavin Howard01055ba2018-11-03 11:00:21 -06004480{
4481 BcStatus s = BC_STATUS_SUCCESS;
4482 uint8_t *flag_ptr = bc_vec_top(&p->flags);
4483
4484 *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
4485
4486 if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004487 if (!brace) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004488
Gavin Howard01055ba2018-11-03 11:00:21 -06004489 p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO;
4490
4491 if (!p->auto_part) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004492 s = zbc_parse_auto(p);
4493 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004494 }
4495
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004496 if (p->l.t.t == BC_LEX_NLINE) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004497 }
4498 else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004499 s = zbc_parse_stmt(p);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004500 if (!s && !brace) s = zbc_parse_endBody(p, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06004501 }
4502
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004503 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004504}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004505#if ERRORS_ARE_FATAL
4506# define zbc_parse_body(...) (zbc_parse_body(__VA_ARGS__), BC_STATUS_SUCCESS)
4507#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004508
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004509#undef zbc_parse_stmt
4510static BC_STATUS zbc_parse_stmt(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004511{
4512 BcStatus s = BC_STATUS_SUCCESS;
4513
4514 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004515 case BC_LEX_NLINE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004516 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004517
4518 case BC_LEX_KEY_ELSE:
Gavin Howard01055ba2018-11-03 11:00:21 -06004519 p->auto_part = false;
4520 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004521
4522 case BC_LEX_LBRACE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004523 if (!BC_PARSE_BODY(p)) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004524 ++p->nbraces;
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004525 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004526 if (s) RETURN_STATUS(s);
4527 RETURN_STATUS(zbc_parse_body(p, true));
Gavin Howard01055ba2018-11-03 11:00:21 -06004528
4529 case BC_LEX_KEY_AUTO:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004530 RETURN_STATUS(zbc_parse_auto(p));
Gavin Howard01055ba2018-11-03 11:00:21 -06004531
4532 default:
Gavin Howard01055ba2018-11-03 11:00:21 -06004533 p->auto_part = false;
Gavin Howard01055ba2018-11-03 11:00:21 -06004534 if (BC_PARSE_IF_END(p)) {
4535 bc_parse_noElse(p);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004536 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06004537 }
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004538 if (BC_PARSE_BODY(p))
4539 RETURN_STATUS(zbc_parse_body(p, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06004540 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004541 }
4542
4543 switch (p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004544 case BC_LEX_OP_INC:
4545 case BC_LEX_OP_DEC:
4546 case BC_LEX_OP_MINUS:
4547 case BC_LEX_OP_BOOL_NOT:
4548 case BC_LEX_LPAREN:
4549 case BC_LEX_NAME:
4550 case BC_LEX_NUMBER:
4551 case BC_LEX_KEY_IBASE:
4552 case BC_LEX_KEY_LAST:
4553 case BC_LEX_KEY_LENGTH:
4554 case BC_LEX_KEY_OBASE:
4555 case BC_LEX_KEY_READ:
4556 case BC_LEX_KEY_SCALE:
4557 case BC_LEX_KEY_SQRT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004558 s = zbc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr);
Gavin Howard01055ba2018-11-03 11:00:21 -06004559 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004560 case BC_LEX_KEY_ELSE:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004561 s = zbc_parse_else(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004562 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004563 case BC_LEX_SCOLON:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004564 while (!s && p->l.t.t == BC_LEX_SCOLON) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004565 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004566 case BC_LEX_RBRACE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004567 s = zbc_parse_endBody(p, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004568 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004569 case BC_LEX_STR:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004570 s = zbc_parse_string(p, BC_INST_PRINT_STR);
Gavin Howard01055ba2018-11-03 11:00:21 -06004571 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004572 case BC_LEX_KEY_BREAK:
4573 case BC_LEX_KEY_CONTINUE:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004574 s = zbc_parse_loopExit(p, p->l.t.t);
Gavin Howard01055ba2018-11-03 11:00:21 -06004575 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004576 case BC_LEX_KEY_FOR:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004577 s = zbc_parse_for(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004578 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004579 case BC_LEX_KEY_HALT:
Gavin Howard01055ba2018-11-03 11:00:21 -06004580 bc_parse_push(p, BC_INST_HALT);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004581 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004582 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004583 case BC_LEX_KEY_IF:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004584 s = zbc_parse_if(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004585 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004586 case BC_LEX_KEY_LIMITS:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004587 // "limits" is a compile-time command,
4588 // the output is produced at _parse time_.
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004589 s = zbc_lex_next(&p->l);
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004590 if (s) RETURN_STATUS(s);
Denys Vlasenko64074a12018-12-07 15:50:14 +01004591 printf(
4592 "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n"
4593 "BC_DIM_MAX = "BC_MAX_DIM_STR "\n"
4594 "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n"
4595 "BC_STRING_MAX = "BC_MAX_STRING_STR"\n"
4596 "BC_NAME_MAX = "BC_MAX_NAME_STR "\n"
4597 "BC_NUM_MAX = "BC_MAX_NUM_STR "\n"
4598 "MAX Exponent = "BC_MAX_EXP_STR "\n"
4599 "Number of vars = "BC_MAX_VARS_STR "\n"
4600 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004601 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004602 case BC_LEX_KEY_PRINT:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004603 s = zbc_parse_print(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004604 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004605 case BC_LEX_KEY_QUIT:
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01004606 // "quit" is a compile-time command. For example,
4607 // "if (0 == 1) quit" terminates when parsing the statement,
4608 // not when it is executed
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01004609 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06004610 case BC_LEX_KEY_RETURN:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004611 s = zbc_parse_return(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004612 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004613 case BC_LEX_KEY_WHILE:
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004614 s = zbc_parse_while(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004615 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004616 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004617 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004618 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06004619 }
4620
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004621 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004622}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004623#if ERRORS_ARE_FATAL
4624# define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS)
4625#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004626
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01004627static BC_STATUS zbc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004628{
4629 BcStatus s;
4630
4631 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004632 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 -06004633 else if (p->l.t.t == BC_LEX_KEY_DEFINE) {
Denys Vlasenko26819db2018-12-12 16:08:46 +01004634 if (!BC_PARSE_CAN_EXEC(p))
4635 RETURN_STATUS(bc_error_bad_token());
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004636 s = zbc_parse_func(p);
Denys Vlasenko26819db2018-12-12 16:08:46 +01004637 } else
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004638 s = zbc_parse_stmt(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004639
Denys Vlasenkod38af482018-12-04 19:11:02 +01004640 if (s || G_interrupt) {
4641 bc_parse_reset(p);
4642 s = BC_STATUS_FAILURE;
4643 }
Gavin Howard01055ba2018-11-03 11:00:21 -06004644
Denys Vlasenko26819db2018-12-12 16:08:46 +01004645 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004646}
Denys Vlasenko26819db2018-12-12 16:08:46 +01004647#if ERRORS_ARE_FATAL
4648# define zbc_parse_parse(...) (zbc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
4649#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004650
Denys Vlasenkob402ff82018-12-11 15:45:15 +01004651// This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004652static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next)
Gavin Howard01055ba2018-11-03 11:00:21 -06004653{
4654 BcStatus s = BC_STATUS_SUCCESS;
4655 BcInst prev = BC_INST_PRINT;
4656 BcLexType top, t = p->l.t.t;
4657 size_t nexprs = 0, ops_bgn = p->ops.len;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004658 unsigned nparens, nrelops;
Gavin Howard01055ba2018-11-03 11:00:21 -06004659 bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last;
4660
4661 paren_first = p->l.t.t == BC_LEX_LPAREN;
4662 nparens = nrelops = 0;
4663 paren_expr = rprn = done = get_token = assign = false;
4664 bin_last = true;
4665
Denys Vlasenkobcb62a72018-12-05 20:17:48 +01004666 for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06004667 switch (t) {
4668
4669 case BC_LEX_OP_INC:
4670 case BC_LEX_OP_DEC:
4671 {
Denys Vlasenko26819db2018-12-12 16:08:46 +01004672 s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004673 rprn = get_token = bin_last = false;
4674 break;
4675 }
4676
4677 case BC_LEX_OP_MINUS:
4678 {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004679 s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004680 rprn = get_token = false;
4681 bin_last = prev == BC_INST_MINUS;
4682 break;
4683 }
4684
4685 case BC_LEX_OP_ASSIGN_POWER:
4686 case BC_LEX_OP_ASSIGN_MULTIPLY:
4687 case BC_LEX_OP_ASSIGN_DIVIDE:
4688 case BC_LEX_OP_ASSIGN_MODULUS:
4689 case BC_LEX_OP_ASSIGN_PLUS:
4690 case BC_LEX_OP_ASSIGN_MINUS:
4691 case BC_LEX_OP_ASSIGN:
4692 {
4693 if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM &&
4694 prev != BC_INST_SCALE && prev != BC_INST_IBASE &&
4695 prev != BC_INST_OBASE && prev != BC_INST_LAST)
4696 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004697 s = bc_error("bad assignment:"
4698 " left side must be scale,"
4699 " ibase, obase, last, var,"
4700 " or array element"
4701 );
Gavin Howard01055ba2018-11-03 11:00:21 -06004702 break;
4703 }
4704 }
4705 // Fallthrough.
4706 case BC_LEX_OP_POWER:
4707 case BC_LEX_OP_MULTIPLY:
4708 case BC_LEX_OP_DIVIDE:
4709 case BC_LEX_OP_MODULUS:
4710 case BC_LEX_OP_PLUS:
4711 case BC_LEX_OP_REL_EQ:
4712 case BC_LEX_OP_REL_LE:
4713 case BC_LEX_OP_REL_GE:
4714 case BC_LEX_OP_REL_NE:
4715 case BC_LEX_OP_REL_LT:
4716 case BC_LEX_OP_REL_GT:
4717 case BC_LEX_OP_BOOL_NOT:
4718 case BC_LEX_OP_BOOL_OR:
4719 case BC_LEX_OP_BOOL_AND:
4720 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004721 if (((t == BC_LEX_OP_BOOL_NOT) != bin_last)
4722 || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT)
4723 ) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004724 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004725 }
4726
4727 nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
4728 prev = BC_PARSE_TOKEN_INST(t);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004729 s = zbc_parse_operator(p, t, ops_bgn, &nexprs, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06004730 rprn = get_token = false;
4731 bin_last = t != BC_LEX_OP_BOOL_NOT;
4732
4733 break;
4734 }
4735
4736 case BC_LEX_LPAREN:
4737 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004738 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004739 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004740 ++nparens;
4741 paren_expr = rprn = bin_last = false;
4742 get_token = true;
4743 bc_vec_push(&p->ops, &t);
4744
4745 break;
4746 }
4747
4748 case BC_LEX_RPAREN:
4749 {
4750 if (bin_last || prev == BC_INST_BOOL_NOT)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004751 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004752
4753 if (nparens == 0) {
4754 s = BC_STATUS_SUCCESS;
4755 done = true;
4756 get_token = false;
4757 break;
4758 }
4759 else if (!paren_expr)
4760 return BC_STATUS_PARSE_EMPTY_EXP;
4761
4762 --nparens;
4763 paren_expr = rprn = true;
4764 get_token = bin_last = false;
4765
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004766 s = zbc_parse_rightParen(p, ops_bgn, &nexprs);
Gavin Howard01055ba2018-11-03 11:00:21 -06004767
4768 break;
4769 }
4770
4771 case BC_LEX_NAME:
4772 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004773 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004774 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004775 paren_expr = true;
4776 rprn = get_token = bin_last = false;
Denys Vlasenko26819db2018-12-12 16:08:46 +01004777 s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06004778 ++nexprs;
4779
4780 break;
4781 }
4782
4783 case BC_LEX_NUMBER:
4784 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004785 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004786 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004787 bc_parse_number(p, &prev, &nexprs);
4788 paren_expr = get_token = true;
4789 rprn = bin_last = false;
4790
4791 break;
4792 }
4793
4794 case BC_LEX_KEY_IBASE:
4795 case BC_LEX_KEY_LAST:
4796 case BC_LEX_KEY_OBASE:
4797 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004798 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004799 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004800 prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE);
4801 bc_parse_push(p, (char) prev);
4802
4803 paren_expr = get_token = true;
4804 rprn = bin_last = false;
4805 ++nexprs;
4806
4807 break;
4808 }
4809
4810 case BC_LEX_KEY_LENGTH:
4811 case BC_LEX_KEY_SQRT:
4812 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004813 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004814 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004815 s = zbc_parse_builtin(p, t, flags, &prev);
Gavin Howard01055ba2018-11-03 11:00:21 -06004816 paren_expr = true;
4817 rprn = get_token = bin_last = false;
4818 ++nexprs;
4819
4820 break;
4821 }
4822
4823 case BC_LEX_KEY_READ:
4824 {
4825 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004826 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004827 else if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004828 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06004829 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004830 s = zbc_parse_read(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06004831
4832 paren_expr = true;
4833 rprn = get_token = bin_last = false;
4834 ++nexprs;
4835 prev = BC_INST_READ;
4836
4837 break;
4838 }
4839
4840 case BC_LEX_KEY_SCALE:
4841 {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004842 if (BC_PARSE_LEAF(prev, rprn))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004843 return bc_error_bad_expression();
Denys Vlasenko26819db2018-12-12 16:08:46 +01004844 s = zbc_parse_scale(p, &prev, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06004845 paren_expr = true;
4846 rprn = get_token = bin_last = false;
4847 ++nexprs;
4848 prev = BC_INST_SCALE;
4849
4850 break;
4851 }
4852
4853 default:
4854 {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004855 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06004856 break;
4857 }
4858 }
4859
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004860 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06004861 }
4862
4863 if (s) return s;
Denys Vlasenkod38af482018-12-04 19:11:02 +01004864 if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing
Gavin Howard01055ba2018-11-03 11:00:21 -06004865
4866 while (p->ops.len > ops_bgn) {
4867
4868 top = BC_PARSE_TOP_OP(p);
4869 assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
4870
4871 if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004872 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004873
4874 bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
4875
4876 nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
4877 bc_vec_pop(&p->ops);
4878 }
4879
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004880 if (prev == BC_INST_BOOL_NOT || nexprs != 1)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004881 return bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06004882
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004883 // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word
4884 for (;;) {
4885 if (t == (next & 0x7f))
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004886 goto ok;
Denys Vlasenko18c6b542018-12-07 12:57:32 +01004887 if (next & 0x80) // last element?
4888 break;
4889 next >>= 8;
4890 }
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01004891 return bc_error_bad_expression();
Denys Vlasenko60cf7472018-12-04 20:05:28 +01004892 ok:
Gavin Howard01055ba2018-11-03 11:00:21 -06004893
4894 if (!(flags & BC_PARSE_REL) && nrelops) {
Denys Vlasenko00646792018-12-05 18:12:27 +01004895 s = bc_POSIX_does_not_allow("comparison operators outside if or loops");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004896 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004897 }
4898 else if ((flags & BC_PARSE_REL) && nrelops > 1) {
Denys Vlasenkoa6f84e12018-12-06 11:10:11 +01004899 s = bc_POSIX_requires("exactly one comparison operator per condition");
Denys Vlasenko12b9eaf2018-12-11 23:50:14 +01004900 ERROR_RETURN(if (s) return s;)
Gavin Howard01055ba2018-11-03 11:00:21 -06004901 }
4902
4903 if (flags & BC_PARSE_PRINT) {
4904 if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT);
4905 bc_parse_push(p, BC_INST_POP);
4906 }
4907
4908 return s;
4909}
4910
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004911#undef zbc_parse_expr
4912static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next)
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004913{
4914 BcStatus s;
4915
4916 s = bc_parse_expr_empty_ok(p, flags, next);
4917 if (s == BC_STATUS_PARSE_EMPTY_EXP)
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004918 RETURN_STATUS(bc_error("empty expression"));
4919 RETURN_STATUS(s);
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004920}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004921#if ERRORS_ARE_FATAL
4922# define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
4923#endif
Denys Vlasenko050b0fe2018-12-05 22:40:44 +01004924
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004925static BC_STATUS zbc_parse_expression(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06004926{
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004927 RETURN_STATUS(zbc_parse_expr(p, flags, bc_parse_next_read));
Gavin Howard01055ba2018-11-03 11:00:21 -06004928}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01004929#if ERRORS_ARE_FATAL
4930# define zbc_parse_expression(...) (zbc_parse_expression(__VA_ARGS__), BC_STATUS_SUCCESS)
4931#endif
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004932
Gavin Howard01055ba2018-11-03 11:00:21 -06004933#endif // ENABLE_BC
4934
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01004935#if ENABLE_DC
Denys Vlasenkocca79a02018-12-05 21:15:46 +01004936
4937#define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT))
4938
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004939static BC_STATUS zdc_parse_register(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004940{
4941 BcStatus s;
4942 char *name;
4943
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004944 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004945 if (s) RETURN_STATUS(s);
4946 if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06004947
4948 name = xstrdup(p->l.t.v.v);
4949 bc_parse_pushName(p, name);
4950
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004951 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004952}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004953#if ERRORS_ARE_FATAL
4954# define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__), BC_STATUS_SUCCESS)
4955#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004956
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004957static BC_STATUS zdc_parse_string(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06004958{
4959 char *str, *name, b[DC_PARSE_BUF_LEN + 1];
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004960 size_t idx, len = G.prog.strs.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06004961
4962 sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len);
4963 name = xstrdup(b);
4964
4965 str = xstrdup(p->l.t.v.v);
4966 bc_parse_push(p, BC_INST_STR);
4967 bc_parse_pushIndex(p, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01004968 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06004969 bc_parse_addFunc(p, name, &idx);
4970
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004971 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004972}
Denys Vlasenko9a34e892018-12-12 13:58:55 +01004973#if ERRORS_ARE_FATAL
4974# define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS)
4975#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004976
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004977static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store)
Gavin Howard01055ba2018-11-03 11:00:21 -06004978{
4979 BcStatus s;
4980
4981 bc_parse_push(p, inst);
4982 if (name) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004983 s = zdc_parse_register(p);
4984 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06004985 }
4986
4987 if (store) {
4988 bc_parse_push(p, BC_INST_SWAP);
4989 bc_parse_push(p, BC_INST_ASSIGN);
4990 bc_parse_push(p, BC_INST_POP);
4991 }
4992
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004993 RETURN_STATUS(zbc_lex_next(&p->l));
Gavin Howard01055ba2018-11-03 11:00:21 -06004994}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004995#if ERRORS_ARE_FATAL
4996# define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__), BC_STATUS_SUCCESS)
4997#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06004998
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01004999static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005000{
5001 BcStatus s;
5002
5003 bc_parse_push(p, inst);
5004 bc_parse_push(p, BC_INST_EXEC_COND);
5005
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005006 s = zdc_parse_register(p);
5007 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005008
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005009 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005010 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005011
5012 if (p->l.t.t == BC_LEX_ELSE) {
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005013 s = zdc_parse_register(p);
5014 if (s) RETURN_STATUS(s);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005015 s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005016 }
5017 else
5018 bc_parse_push(p, BC_PARSE_STREND);
5019
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005020 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005021}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005022#if ERRORS_ARE_FATAL
5023# define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__), BC_STATUS_SUCCESS)
5024#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005025
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005026static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005027{
5028 BcStatus s = BC_STATUS_SUCCESS;
5029 BcInst prev;
5030 uint8_t inst;
5031 bool assign, get_token = false;
5032
5033 switch (t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005034 case BC_LEX_OP_REL_EQ:
5035 case BC_LEX_OP_REL_LE:
5036 case BC_LEX_OP_REL_GE:
5037 case BC_LEX_OP_REL_NE:
5038 case BC_LEX_OP_REL_LT:
5039 case BC_LEX_OP_REL_GT:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005040 s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005041 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005042 case BC_LEX_SCOLON:
5043 case BC_LEX_COLON:
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005044 s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON);
Gavin Howard01055ba2018-11-03 11:00:21 -06005045 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005046 case BC_LEX_STR:
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005047 s = zdc_parse_string(p);
Gavin Howard01055ba2018-11-03 11:00:21 -06005048 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005049 case BC_LEX_NEG:
5050 case BC_LEX_NUMBER:
Gavin Howard01055ba2018-11-03 11:00:21 -06005051 if (t == BC_LEX_NEG) {
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005052 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005053 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005054 if (p->l.t.t != BC_LEX_NUMBER)
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005055 RETURN_STATUS(bc_error_bad_token());
Gavin Howard01055ba2018-11-03 11:00:21 -06005056 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005057 bc_parse_number(p, &prev, &p->nbraces);
Gavin Howard01055ba2018-11-03 11:00:21 -06005058 if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG);
5059 get_token = true;
Gavin Howard01055ba2018-11-03 11:00:21 -06005060 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005061 case BC_LEX_KEY_READ:
Gavin Howard01055ba2018-11-03 11:00:21 -06005062 if (flags & BC_PARSE_NOREAD)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005063 s = bc_error_nested_read_call();
Gavin Howard01055ba2018-11-03 11:00:21 -06005064 else
5065 bc_parse_push(p, BC_INST_READ);
5066 get_token = true;
5067 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005068 case BC_LEX_OP_ASSIGN:
5069 case BC_LEX_STORE_PUSH:
Gavin Howard01055ba2018-11-03 11:00:21 -06005070 assign = t == BC_LEX_OP_ASSIGN;
5071 inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005072 s = zdc_parse_mem(p, inst, true, assign);
Gavin Howard01055ba2018-11-03 11:00:21 -06005073 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005074 case BC_LEX_LOAD:
5075 case BC_LEX_LOAD_POP:
Gavin Howard01055ba2018-11-03 11:00:21 -06005076 inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005077 s = zdc_parse_mem(p, inst, true, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06005078 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005079 case BC_LEX_STORE_IBASE:
5080 case BC_LEX_STORE_SCALE:
5081 case BC_LEX_STORE_OBASE:
Gavin Howard01055ba2018-11-03 11:00:21 -06005082 inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE;
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005083 s = zdc_parse_mem(p, inst, false, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005084 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005085 default:
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01005086 s = bc_error_bad_token();
Gavin Howard01055ba2018-11-03 11:00:21 -06005087 get_token = true;
5088 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005089 }
5090
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005091 if (!s && get_token) s = zbc_lex_next(&p->l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005092
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005093 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005094}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005095#if ERRORS_ARE_FATAL
5096# define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__), BC_STATUS_SUCCESS)
5097#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005098
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005099static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags)
Gavin Howard01055ba2018-11-03 11:00:21 -06005100{
5101 BcStatus s = BC_STATUS_SUCCESS;
5102 BcInst inst;
5103 BcLexType t;
5104
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005105 if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005106
5107 for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) {
Gavin Howard01055ba2018-11-03 11:00:21 -06005108 inst = dc_parse_insts[t];
5109
5110 if (inst != BC_INST_INVALID) {
5111 bc_parse_push(p, inst);
Denys Vlasenko9a34e892018-12-12 13:58:55 +01005112 s = zbc_lex_next(&p->l);
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005113 } else
5114 s = zdc_parse_token(p, t, flags);
Gavin Howard01055ba2018-11-03 11:00:21 -06005115 }
5116
5117 if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL))
5118 bc_parse_push(p, BC_INST_POP_EXEC);
5119
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005120 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005121}
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005122#if ERRORS_ARE_FATAL
5123# define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5124#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005125
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01005126static BC_STATUS zdc_parse_parse(BcParse *p)
Gavin Howard01055ba2018-11-03 11:00:21 -06005127{
5128 BcStatus s;
5129
5130 if (p->l.t.t == BC_LEX_EOF)
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005131 s = bc_error("end of file");
Gavin Howard01055ba2018-11-03 11:00:21 -06005132 else
Denys Vlasenko8cd468f2018-12-12 14:54:38 +01005133 s = zdc_parse_expr(p, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06005134
Denys Vlasenkod38af482018-12-04 19:11:02 +01005135 if (s || G_interrupt) {
5136 bc_parse_reset(p);
5137 s = BC_STATUS_FAILURE;
5138 }
Gavin Howard01055ba2018-11-03 11:00:21 -06005139
Denys Vlasenko26819db2018-12-12 16:08:46 +01005140 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005141}
Denys Vlasenko26819db2018-12-12 16:08:46 +01005142#if ERRORS_ARE_FATAL
5143# define zdc_parse_parse(...) (zdc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS)
5144#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005145
Gavin Howard01055ba2018-11-03 11:00:21 -06005146#endif // ENABLE_DC
5147
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005148static BC_STATUS zcommon_parse_expr(BcParse *p, uint8_t flags)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005149{
5150 if (IS_BC) {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005151 IF_BC(RETURN_STATUS(zbc_parse_expression(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005152 } else {
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005153 IF_DC(RETURN_STATUS(zdc_parse_expr(p, flags));)
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005154 }
5155}
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005156#if ERRORS_ARE_FATAL
5157# define zcommon_parse_expr(...) (zcommon_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS)
5158#endif
Denys Vlasenkof6c1da52018-12-02 17:36:00 +01005159
Denys Vlasenkodf515392018-12-02 19:27:48 +01005160static BcVec* bc_program_search(char *id, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005161{
Gavin Howard01055ba2018-11-03 11:00:21 -06005162 BcId e, *ptr;
5163 BcVec *v, *map;
5164 size_t i;
5165 BcResultData data;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005166 int new;
Gavin Howard01055ba2018-11-03 11:00:21 -06005167
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005168 v = var ? &G.prog.vars : &G.prog.arrs;
5169 map = var ? &G.prog.var_map : &G.prog.arr_map;
Gavin Howard01055ba2018-11-03 11:00:21 -06005170
5171 e.name = id;
5172 e.idx = v->len;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01005173 new = bc_map_insert(map, &e, &i); // 1 if insertion was successful
Gavin Howard01055ba2018-11-03 11:00:21 -06005174
5175 if (new) {
5176 bc_array_init(&data.v, var);
5177 bc_vec_push(v, &data.v);
5178 }
5179
5180 ptr = bc_vec_item(map, i);
5181 if (new) ptr->name = xstrdup(e.name);
Denys Vlasenkodf515392018-12-02 19:27:48 +01005182 return bc_vec_item(v, ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005183}
5184
Denys Vlasenko29301232018-12-11 15:29:32 +01005185static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
Gavin Howard01055ba2018-11-03 11:00:21 -06005186{
Gavin Howard01055ba2018-11-03 11:00:21 -06005187 switch (r->t) {
5188
5189 case BC_RESULT_STR:
5190 case BC_RESULT_TEMP:
5191 case BC_RESULT_IBASE:
5192 case BC_RESULT_SCALE:
5193 case BC_RESULT_OBASE:
5194 {
5195 *num = &r->d.n;
5196 break;
5197 }
5198
5199 case BC_RESULT_CONSTANT:
5200 {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005201 BcStatus s;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005202 char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005203 size_t base_t, len = strlen(*str);
5204 BcNum *base;
5205
5206 bc_num_init(&r->d.n, len);
5207
5208 hex = hex && len == 1;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005209 base = hex ? &G.prog.hexb : &G.prog.ib;
5210 base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t;
Denys Vlasenko29301232018-12-11 15:29:32 +01005211 s = zbc_num_parse(&r->d.n, *str, base, base_t);
Gavin Howard01055ba2018-11-03 11:00:21 -06005212
5213 if (s) {
5214 bc_num_free(&r->d.n);
Denys Vlasenko29301232018-12-11 15:29:32 +01005215 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005216 }
5217
5218 *num = &r->d.n;
5219 r->t = BC_RESULT_TEMP;
5220
5221 break;
5222 }
5223
5224 case BC_RESULT_VAR:
5225 case BC_RESULT_ARRAY:
5226 case BC_RESULT_ARRAY_ELEM:
5227 {
5228 BcVec *v;
5229
Denys Vlasenkodf515392018-12-02 19:27:48 +01005230 v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR);
Gavin Howard01055ba2018-11-03 11:00:21 -06005231
5232 if (r->t == BC_RESULT_ARRAY_ELEM) {
5233 v = bc_vec_top(v);
5234 if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1);
5235 *num = bc_vec_item(v, r->d.id.idx);
5236 }
5237 else
5238 *num = bc_vec_top(v);
5239
5240 break;
5241 }
5242
5243 case BC_RESULT_LAST:
5244 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005245 *num = &G.prog.last;
Gavin Howard01055ba2018-11-03 11:00:21 -06005246 break;
5247 }
5248
5249 case BC_RESULT_ONE:
5250 {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005251 *num = &G.prog.one;
Gavin Howard01055ba2018-11-03 11:00:21 -06005252 break;
5253 }
5254 }
5255
Denys Vlasenko29301232018-12-11 15:29:32 +01005256 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005257}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005258#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005259# define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005260#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005261
Denys Vlasenko29301232018-12-11 15:29:32 +01005262static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln,
Gavin Howard01055ba2018-11-03 11:00:21 -06005263 BcResult **r, BcNum **rn, bool assign)
5264{
5265 BcStatus s;
5266 bool hex;
5267 BcResultType lt, rt;
5268
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005269 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005270 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005271
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005272 *r = bc_vec_item_rev(&G.prog.results, 0);
5273 *l = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06005274
5275 lt = (*l)->t;
5276 rt = (*r)->t;
5277 hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE);
5278
Denys Vlasenko29301232018-12-11 15:29:32 +01005279 s = zbc_program_num(*l, ln, false);
5280 if (s) RETURN_STATUS(s);
5281 s = zbc_program_num(*r, rn, hex);
5282 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005283
5284 // We run this again under these conditions in case any vector has been
5285 // reallocated out from under the BcNums or arrays we had.
5286 if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005287 s = zbc_program_num(*l, ln, false);
5288 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005289 }
5290
5291 if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR))
Denys Vlasenko29301232018-12-11 15:29:32 +01005292 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005293 if (!assign && !BC_PROG_NUM((*r), (*ln)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005294 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005295
Denys Vlasenko29301232018-12-11 15:29:32 +01005296 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005297}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005298#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005299# define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005300#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005301
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005302static void bc_program_binOpRetire(BcResult *r)
Gavin Howard01055ba2018-11-03 11:00:21 -06005303{
5304 r->t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005305 bc_vec_pop(&G.prog.results);
5306 bc_vec_pop(&G.prog.results);
5307 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005308}
5309
Denys Vlasenko29301232018-12-11 15:29:32 +01005310static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
Gavin Howard01055ba2018-11-03 11:00:21 -06005311{
5312 BcStatus s;
5313
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005314 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005315 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005316 *r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005317
Denys Vlasenko29301232018-12-11 15:29:32 +01005318 s = zbc_program_num(*r, n, false);
5319 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005320
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005321 if (!BC_PROG_NUM((*r), (*n)))
Denys Vlasenko29301232018-12-11 15:29:32 +01005322 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06005323
Denys Vlasenko29301232018-12-11 15:29:32 +01005324 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005325}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005326#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005327# define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005328#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005329
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005330static void bc_program_retire(BcResult *r, BcResultType t)
Gavin Howard01055ba2018-11-03 11:00:21 -06005331{
5332 r->t = t;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005333 bc_vec_pop(&G.prog.results);
5334 bc_vec_push(&G.prog.results, r);
Gavin Howard01055ba2018-11-03 11:00:21 -06005335}
5336
Denys Vlasenko259137d2018-12-11 19:42:05 +01005337static BC_STATUS zbc_program_op(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005338{
5339 BcStatus s;
5340 BcResult *opd1, *opd2, res;
5341 BcNum *n1, *n2 = NULL;
5342
Denys Vlasenko29301232018-12-11 15:29:32 +01005343 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005344 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005345 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005346
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005347 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01005348 ERROR_RETURN(s =) zbc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06005349 if (s) goto err;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005350 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005351
Denys Vlasenko259137d2018-12-11 19:42:05 +01005352 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005353
5354err:
5355 bc_num_free(&res.d.n);
Denys Vlasenko259137d2018-12-11 19:42:05 +01005356 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005357}
Denys Vlasenko259137d2018-12-11 19:42:05 +01005358#if ERRORS_ARE_FATAL
5359# define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS)
5360#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005361
Denys Vlasenko26819db2018-12-12 16:08:46 +01005362static BC_STATUS zbc_program_read(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005363{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005364 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005365 BcStatus s;
5366 BcParse parse;
5367 BcVec buf;
5368 BcInstPtr ip;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005369 BcFunc *f;
Gavin Howard01055ba2018-11-03 11:00:21 -06005370
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005371 if (G.in_read)
Denys Vlasenko26819db2018-12-12 16:08:46 +01005372 RETURN_STATUS(bc_error_nested_read_call());
Gavin Howard01055ba2018-11-03 11:00:21 -06005373
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005374 f = bc_program_func(BC_PROG_READ);
Denys Vlasenko7d628012018-12-04 21:46:47 +01005375 bc_vec_pop_all(&f->code);
5376 bc_char_vec_init(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005377
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005378 sv_file = G.prog.file;
5379 G.prog.file = NULL;
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005380 G.in_read = 1;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005381
Denys Vlasenko8a892472018-12-12 22:43:58 +01005382 bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06005383
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01005384 bc_parse_create(&parse, BC_PROG_READ);
Denys Vlasenko0409ad32018-12-05 16:39:22 +01005385 bc_lex_file(&parse.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005386
Denys Vlasenko26819db2018-12-12 16:08:46 +01005387 s = zbc_parse_text(&parse, buf.v);
Gavin Howard01055ba2018-11-03 11:00:21 -06005388 if (s) goto exec_err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01005389 s = zcommon_parse_expr(&parse, BC_PARSE_NOREAD);
Gavin Howard01055ba2018-11-03 11:00:21 -06005390 if (s) goto exec_err;
5391
5392 if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005393 s = bc_error("bad read() expression");
Gavin Howard01055ba2018-11-03 11:00:21 -06005394 goto exec_err;
5395 }
5396
5397 ip.func = BC_PROG_READ;
5398 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005399 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06005400
5401 // Update this pointer, just in case.
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005402 f = bc_program_func(BC_PROG_READ);
Gavin Howard01055ba2018-11-03 11:00:21 -06005403
5404 bc_vec_pushByte(&f->code, BC_INST_POP_EXEC);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005405 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06005406
5407exec_err:
5408 bc_parse_free(&parse);
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005409//io_err:
Denys Vlasenko69171dc2018-12-12 00:29:24 +01005410 G.in_read = 0;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01005411 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06005412 bc_vec_free(&buf);
Denys Vlasenko26819db2018-12-12 16:08:46 +01005413 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005414}
Denys Vlasenko26819db2018-12-12 16:08:46 +01005415#if ERRORS_ARE_FATAL
5416# define zbc_program_read(...) (zbc_program_read(__VA_ARGS__), BC_STATUS_SUCCESS)
5417#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005418
5419static size_t bc_program_index(char *code, size_t *bgn)
5420{
5421 char amt = code[(*bgn)++], i = 0;
5422 size_t res = 0;
5423
5424 for (; i < amt; ++i, ++(*bgn))
5425 res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT));
5426
5427 return res;
5428}
5429
5430static char *bc_program_name(char *code, size_t *bgn)
5431{
5432 size_t i;
5433 char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND);
5434
5435 s = xmalloc(ptr - str + 1);
5436 c = code[(*bgn)++];
5437
5438 for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i)
5439 s[i] = c;
5440
5441 s[i] = '\0';
5442
5443 return s;
5444}
5445
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005446static void bc_program_printString(const char *str)
Gavin Howard01055ba2018-11-03 11:00:21 -06005447{
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005448#if ENABLE_DC
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005449 if (!str[0]) {
Denys Vlasenko2c6f5632018-12-11 21:21:14 +01005450 // Example: echo '[]ap' | dc
5451 // should print two bytes: 0x00, 0x0A
Denys Vlasenko00d77792018-11-30 23:13:42 +01005452 bb_putchar('\0');
Gavin Howard01055ba2018-11-03 11:00:21 -06005453 return;
5454 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005455#endif
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005456 while (*str) {
5457 int c = *str++;
5458 if (c != '\\' || !*str)
Denys Vlasenko00d77792018-11-30 23:13:42 +01005459 bb_putchar(c);
Gavin Howard01055ba2018-11-03 11:00:21 -06005460 else {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005461 c = *str++;
Gavin Howard01055ba2018-11-03 11:00:21 -06005462 switch (c) {
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005463 case 'a':
5464 bb_putchar('\a');
5465 break;
5466 case 'b':
5467 bb_putchar('\b');
5468 break;
5469 case '\\':
5470 case 'e':
5471 bb_putchar('\\');
5472 break;
5473 case 'f':
5474 bb_putchar('\f');
5475 break;
5476 case 'n':
5477 bb_putchar('\n');
5478 G.prog.nchars = SIZE_MAX;
5479 break;
5480 case 'r':
5481 bb_putchar('\r');
5482 break;
5483 case 'q':
5484 bb_putchar('"');
5485 break;
5486 case 't':
5487 bb_putchar('\t');
5488 break;
5489 default:
5490 // Just print the backslash and following character.
5491 bb_putchar('\\');
5492 ++G.prog.nchars;
5493 bb_putchar(c);
5494 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005495 }
5496 }
Denys Vlasenko9f657e02018-12-11 19:52:25 +01005497 ++G.prog.nchars;
Gavin Howard01055ba2018-11-03 11:00:21 -06005498 }
5499}
5500
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005501static void bc_num_printNewline(void)
5502{
5503 if (G.prog.nchars == G.prog.len - 1) {
5504 bb_putchar('\\');
5505 bb_putchar('\n');
5506 G.prog.nchars = 0;
5507 }
5508}
5509
5510#if ENABLE_DC
5511static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix)
5512{
5513 (void) radix;
5514 bb_putchar((char) num);
5515 G.prog.nchars += width;
5516}
5517#endif
5518
5519static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix)
5520{
5521 size_t exp, pow;
5522
5523 bc_num_printNewline();
5524 bb_putchar(radix ? '.' : ' ');
5525 ++G.prog.nchars;
5526
5527 bc_num_printNewline();
5528 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10)
5529 continue;
5530
5531 for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) {
5532 size_t dig;
5533 bc_num_printNewline();
5534 dig = num / pow;
5535 num -= dig * pow;
5536 bb_putchar(((char) dig) + '0');
5537 }
5538}
5539
5540static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix)
5541{
5542 if (radix) {
5543 bc_num_printNewline();
5544 bb_putchar('.');
5545 G.prog.nchars += 1;
5546 }
5547
5548 bc_num_printNewline();
5549 bb_putchar(bb_hexdigits_upcase[num]);
5550 G.prog.nchars += width;
5551}
5552
5553static void bc_num_printDecimal(BcNum *n)
5554{
5555 size_t i, rdx = n->rdx - 1;
5556
5557 if (n->neg) bb_putchar('-');
5558 G.prog.nchars += n->neg;
5559
5560 for (i = n->len - 1; i < n->len; --i)
5561 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
5562}
5563
5564static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print)
5565{
5566 BcStatus s;
5567 BcVec stack;
5568 BcNum intp, fracp, digit, frac_len;
5569 unsigned long dig, *ptr;
5570 size_t i;
5571 bool radix;
5572
5573 if (n->len == 0) {
5574 print(0, width, false);
5575 RETURN_STATUS(BC_STATUS_SUCCESS);
5576 }
5577
5578 bc_vec_init(&stack, sizeof(long), NULL);
5579 bc_num_init(&intp, n->len);
5580 bc_num_init(&fracp, n->rdx);
5581 bc_num_init(&digit, width);
5582 bc_num_init(&frac_len, BC_NUM_INT(n));
5583 bc_num_copy(&intp, n);
5584 bc_num_one(&frac_len);
5585
5586 bc_num_truncate(&intp, intp.rdx);
5587 s = zbc_num_sub(n, &intp, &fracp, 0);
5588 if (s) goto err;
5589
5590 while (intp.len != 0) {
5591 s = zbc_num_divmod(&intp, base, &intp, &digit, 0);
5592 if (s) goto err;
5593 s = zbc_num_ulong(&digit, &dig);
5594 if (s) goto err;
5595 bc_vec_push(&stack, &dig);
5596 }
5597
5598 for (i = 0; i < stack.len; ++i) {
5599 ptr = bc_vec_item_rev(&stack, i);
5600 print(*ptr, width, false);
5601 }
5602
5603 if (!n->rdx) goto err;
5604
5605 for (radix = true; frac_len.len <= n->rdx; radix = false) {
5606 s = zbc_num_mul(&fracp, base, &fracp, n->rdx);
5607 if (s) goto err;
5608 s = zbc_num_ulong(&fracp, &dig);
5609 if (s) goto err;
5610 bc_num_ulong2num(&intp, dig);
5611 s = zbc_num_sub(&fracp, &intp, &fracp, 0);
5612 if (s) goto err;
5613 print(dig, width, radix);
5614 s = zbc_num_mul(&frac_len, base, &frac_len, 0);
5615 if (s) goto err;
5616 }
5617
5618err:
5619 bc_num_free(&frac_len);
5620 bc_num_free(&digit);
5621 bc_num_free(&fracp);
5622 bc_num_free(&intp);
5623 bc_vec_free(&stack);
5624 RETURN_STATUS(s);
5625}
5626#if ERRORS_ARE_FATAL
5627# define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS)
5628#endif
5629
5630static BC_STATUS zbc_num_printBase(BcNum *n)
5631{
5632 BcStatus s;
5633 size_t width, i;
5634 BcNumDigitOp print;
5635 bool neg = n->neg;
5636
5637 if (neg) {
5638 bb_putchar('-');
5639 G.prog.nchars++;
5640 }
5641
5642 n->neg = false;
5643
5644 if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
5645 width = 1;
5646 print = bc_num_printHex;
5647 }
5648 else {
5649 for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
5650 continue;
5651 print = bc_num_printDigits;
5652 }
5653
5654 s = zbc_num_printNum(n, &G.prog.ob, width, print);
5655 n->neg = neg;
5656
5657 RETURN_STATUS(s);
5658}
5659#if ERRORS_ARE_FATAL
5660# define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS)
5661#endif
5662
5663#if ENABLE_DC
5664static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base)
5665{
5666 RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar));
5667}
5668#if ERRORS_ARE_FATAL
5669# define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS)
5670#endif
5671#endif
5672
5673static BC_STATUS zbc_num_print(BcNum *n, bool newline)
5674{
5675 BcStatus s = BC_STATUS_SUCCESS;
5676
5677 bc_num_printNewline();
5678
5679 if (n->len == 0) {
5680 bb_putchar('0');
5681 ++G.prog.nchars;
5682 }
5683 else if (G.prog.ob_t == 10)
5684 bc_num_printDecimal(n);
5685 else
5686 s = zbc_num_printBase(n);
5687
5688 if (newline) {
5689 bb_putchar('\n');
5690 G.prog.nchars = 0;
5691 }
5692
5693 RETURN_STATUS(s);
5694}
5695#if ERRORS_ARE_FATAL
5696# define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS)
5697#endif
5698
Denys Vlasenko29301232018-12-11 15:29:32 +01005699static BC_STATUS zbc_program_print(char inst, size_t idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06005700{
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005701 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06005702 BcResult *r;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005703 BcNum *num;
Gavin Howard01055ba2018-11-03 11:00:21 -06005704 bool pop = inst != BC_INST_PRINT;
5705
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005706 if (!BC_PROG_STACK(&G.prog.results, idx + 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005707 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005708
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005709 r = bc_vec_item_rev(&G.prog.results, idx);
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005710 num = NULL; // is this NULL necessary?
Denys Vlasenko29301232018-12-11 15:29:32 +01005711 s = zbc_program_num(r, &num, false);
5712 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005713
5714 if (BC_PROG_NUM(r, num)) {
Denys Vlasenko29301232018-12-11 15:29:32 +01005715 s = zbc_num_print(num, !pop);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005716 if (!s) bc_num_copy(&G.prog.last, num);
Gavin Howard01055ba2018-11-03 11:00:21 -06005717 }
5718 else {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005719 char *str;
Gavin Howard01055ba2018-11-03 11:00:21 -06005720
5721 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01005722 str = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06005723
5724 if (inst == BC_INST_PRINT_STR) {
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005725 for (;;) {
5726 char c = *str++;
5727 if (c == '\0') break;
Denys Vlasenko00d77792018-11-30 23:13:42 +01005728 bb_putchar(c);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005729 ++G.prog.nchars;
Denys Vlasenko44d79d82018-12-10 12:33:40 +01005730 if (c == '\n') G.prog.nchars = 0;
Gavin Howard01055ba2018-11-03 11:00:21 -06005731 }
5732 }
5733 else {
Denys Vlasenko5f1b90b2018-12-08 23:18:06 +01005734 bc_program_printString(str);
Denys Vlasenko00d77792018-11-30 23:13:42 +01005735 if (inst == BC_INST_PRINT) bb_putchar('\n');
Gavin Howard01055ba2018-11-03 11:00:21 -06005736 }
5737 }
5738
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005739 if (!s && pop) bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005740
Denys Vlasenko29301232018-12-11 15:29:32 +01005741 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005742}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005743#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005744# define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005745#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005746
Denys Vlasenko29301232018-12-11 15:29:32 +01005747static BC_STATUS zbc_program_negate(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06005748{
5749 BcStatus s;
5750 BcResult res, *ptr;
5751 BcNum *num = NULL;
5752
Denys Vlasenko29301232018-12-11 15:29:32 +01005753 s = zbc_program_prep(&ptr, &num);
5754 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005755
5756 bc_num_init(&res.d.n, num->len);
5757 bc_num_copy(&res.d.n, num);
5758 if (res.d.n.len) res.d.n.neg = !res.d.n.neg;
5759
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005760 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06005761
Denys Vlasenko29301232018-12-11 15:29:32 +01005762 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005763}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005764#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005765# define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01005766#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005767
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005768static BC_STATUS zbc_program_logical(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005769{
5770 BcStatus s;
5771 BcResult *opd1, *opd2, res;
5772 BcNum *n1, *n2;
Denys Vlasenko16494f52018-12-12 00:50:23 +01005773 ssize_t cond;
Gavin Howard01055ba2018-11-03 11:00:21 -06005774
Denys Vlasenko29301232018-12-11 15:29:32 +01005775 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005776 if (s) RETURN_STATUS(s);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005777
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005778 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005779
5780 if (inst == BC_INST_BOOL_AND)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005781 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005782 else if (inst == BC_INST_BOOL_OR)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005783 cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero);
Gavin Howard01055ba2018-11-03 11:00:21 -06005784 else {
Denys Vlasenko16494f52018-12-12 00:50:23 +01005785 cond = bc_num_cmp(n1, n2);
Gavin Howard01055ba2018-11-03 11:00:21 -06005786 switch (inst) {
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005787 case BC_INST_REL_EQ:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005788 cond = (cond == 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005789 break;
5790 case BC_INST_REL_LE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005791 cond = (cond <= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005792 break;
5793 case BC_INST_REL_GE:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005794 cond = (cond >= 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005795 break;
5796 case BC_INST_REL_LT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005797 cond = (cond < 0);
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005798 break;
5799 case BC_INST_REL_GT:
Denys Vlasenko16494f52018-12-12 00:50:23 +01005800 cond = (cond > 0);
5801 break;
5802 default: // = case BC_INST_REL_NE:
5803 //cond = (cond != 0); - not needed
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005804 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06005805 }
5806 }
5807
Denys Vlasenko09d8df82018-12-11 19:29:35 +01005808 if (cond) bc_num_one(&res.d.n);
5809 //else bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06005810
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005811 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005812
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005813 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005814}
Denys Vlasenko728e7c92018-12-11 19:37:00 +01005815#if ERRORS_ARE_FATAL
5816# define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS)
5817#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005818
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005819#if ENABLE_DC
Denys Vlasenko29301232018-12-11 15:29:32 +01005820static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v,
Gavin Howard01055ba2018-11-03 11:00:21 -06005821 bool push)
5822{
5823 BcNum n2;
5824 BcResult res;
5825
5826 memset(&n2, 0, sizeof(BcNum));
5827 n2.rdx = res.d.id.idx = r->d.id.idx;
5828 res.t = BC_RESULT_STR;
5829
5830 if (!push) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005831 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenko29301232018-12-11 15:29:32 +01005832 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005833 bc_vec_pop(v);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005834 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005835 }
5836
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005837 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005838
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005839 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005840 bc_vec_push(v, &n2);
5841
Denys Vlasenko29301232018-12-11 15:29:32 +01005842 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06005843}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005844#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005845# define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005846#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005847#endif // ENABLE_DC
5848
Denys Vlasenko29301232018-12-11 15:29:32 +01005849static BC_STATUS zbc_program_copyToVar(char *name, bool var)
Gavin Howard01055ba2018-11-03 11:00:21 -06005850{
5851 BcStatus s;
5852 BcResult *ptr, r;
5853 BcVec *v;
5854 BcNum *n;
5855
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005856 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01005857 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06005858
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005859 ptr = bc_vec_top(&G.prog.results);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005860 if ((ptr->t == BC_RESULT_ARRAY) != !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005861 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005862 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005863
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005864#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005865 if (ptr->t == BC_RESULT_STR && !var)
Denys Vlasenko29301232018-12-11 15:29:32 +01005866 RETURN_STATUS(bc_error_variable_is_wrong_type());
5867 if (ptr->t == BC_RESULT_STR)
5868 RETURN_STATUS(zbc_program_assignStr(ptr, v, true));
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005869#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005870
Denys Vlasenko29301232018-12-11 15:29:32 +01005871 s = zbc_program_num(ptr, &n, false);
5872 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005873
5874 // Do this once more to make sure that pointers were not invalidated.
Denys Vlasenkodf515392018-12-02 19:27:48 +01005875 v = bc_program_search(name, var);
Gavin Howard01055ba2018-11-03 11:00:21 -06005876
5877 if (var) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01005878 bc_num_init_DEF_SIZE(&r.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06005879 bc_num_copy(&r.d.n, n);
5880 }
5881 else {
5882 bc_array_init(&r.d.v, true);
5883 bc_array_copy(&r.d.v, (BcVec *) n);
5884 }
5885
5886 bc_vec_push(v, &r.d);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005887 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06005888
Denys Vlasenko29301232018-12-11 15:29:32 +01005889 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005890}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005891#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01005892# define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01005893#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005894
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005895static BC_STATUS zbc_program_assign(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06005896{
5897 BcStatus s;
5898 BcResult *left, *right, res;
5899 BcNum *l = NULL, *r = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06005900 bool assign = inst == BC_INST_ASSIGN, ib, sc;
5901
Denys Vlasenko29301232018-12-11 15:29:32 +01005902 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005903 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005904
5905 ib = left->t == BC_RESULT_IBASE;
5906 sc = left->t == BC_RESULT_SCALE;
5907
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005908#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06005909
5910 if (right->t == BC_RESULT_STR) {
5911
5912 BcVec *v;
5913
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005914 if (left->t != BC_RESULT_VAR)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005915 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkodf515392018-12-02 19:27:48 +01005916 v = bc_program_search(left->d.id.name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06005917
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005918 RETURN_STATUS(zbc_program_assignStr(right, v, false));
Gavin Howard01055ba2018-11-03 11:00:21 -06005919 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005920#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005921
5922 if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP)
Denys Vlasenko259137d2018-12-11 19:42:05 +01005923 RETURN_STATUS(bc_error("bad assignment:"
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005924 " left side must be scale,"
5925 " ibase, obase, last, var,"
5926 " or array element"
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005927 ));
Gavin Howard01055ba2018-11-03 11:00:21 -06005928
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005929#if ENABLE_BC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005930 if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005931 RETURN_STATUS(bc_error("divide by zero"));
Gavin Howard01055ba2018-11-03 11:00:21 -06005932
5933 if (assign)
5934 bc_num_copy(l, r);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005935 else {
5936 s = BC_STATUS_SUCCESS;
Denys Vlasenko26819db2018-12-12 16:08:46 +01005937 ERROR_RETURN(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005938 }
5939 if (s) RETURN_STATUS(s);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005940#else
Gavin Howard01055ba2018-11-03 11:00:21 -06005941 bc_num_copy(l, r);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01005942#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005943
5944 if (ib || sc || left->t == BC_RESULT_OBASE) {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005945 static const char *const msg[] = {
Denys Vlasenko64074a12018-12-07 15:50:14 +01005946 "bad ibase; must be [2,16]", //BC_RESULT_IBASE
5947 "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE
5948 NULL, //can't happen //BC_RESULT_LAST
5949 NULL, //can't happen //BC_RESULT_CONSTANT
5950 NULL, //can't happen //BC_RESULT_ONE
5951 "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005952 };
Gavin Howard01055ba2018-11-03 11:00:21 -06005953 size_t *ptr;
Denys Vlasenkoffdcebd2018-12-07 15:10:05 +01005954 unsigned long val, max;
Gavin Howard01055ba2018-11-03 11:00:21 -06005955
Denys Vlasenko29301232018-12-11 15:29:32 +01005956 s = zbc_num_ulong(l, &val);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005957 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005958 s = left->t - BC_RESULT_IBASE;
Gavin Howard01055ba2018-11-03 11:00:21 -06005959 if (sc) {
5960 max = BC_MAX_SCALE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005961 ptr = &G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06005962 }
5963 else {
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005964 if (val < BC_NUM_MIN_BASE)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005965 RETURN_STATUS(bc_error(msg[s]));
Gavin Howard01055ba2018-11-03 11:00:21 -06005966 max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005967 ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06005968 }
5969
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005970 if (val > max)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005971 RETURN_STATUS(bc_error(msg[s]));
Denys Vlasenko60cf7472018-12-04 20:05:28 +01005972 if (!sc)
5973 bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l);
Gavin Howard01055ba2018-11-03 11:00:21 -06005974
5975 *ptr = (size_t) val;
5976 s = BC_STATUS_SUCCESS;
5977 }
5978
5979 bc_num_init(&res.d.n, l->len);
5980 bc_num_copy(&res.d.n, l);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01005981 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06005982
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005983 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06005984}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01005985#if ERRORS_ARE_FATAL
5986# define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS)
5987#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06005988
Denys Vlasenko416ce762018-12-02 20:57:17 +01005989#if !ENABLE_DC
5990#define bc_program_pushVar(code, bgn, pop, copy) \
5991 bc_program_pushVar(code, bgn)
5992// for bc, 'pop' and 'copy' are always false
5993#endif
Denys Vlasenkob402ff82018-12-11 15:45:15 +01005994static BC_STATUS bc_program_pushVar(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06005995 bool pop, bool copy)
5996{
Gavin Howard01055ba2018-11-03 11:00:21 -06005997 BcResult r;
5998 char *name = bc_program_name(code, bgn);
Gavin Howard01055ba2018-11-03 11:00:21 -06005999
6000 r.t = BC_RESULT_VAR;
6001 r.d.id.name = name;
6002
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006003#if ENABLE_DC
Denys Vlasenko416ce762018-12-02 20:57:17 +01006004 {
6005 BcVec *v = bc_program_search(name, true);
6006 BcNum *num = bc_vec_top(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006007
Denys Vlasenko416ce762018-12-02 20:57:17 +01006008 if (pop || copy) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006009
Denys Vlasenko416ce762018-12-02 20:57:17 +01006010 if (!BC_PROG_STACK(v, 2 - copy)) {
6011 free(name);
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006012 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko416ce762018-12-02 20:57:17 +01006013 }
6014
Gavin Howard01055ba2018-11-03 11:00:21 -06006015 free(name);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006016 name = NULL;
6017
6018 if (!BC_PROG_STR(num)) {
6019
6020 r.t = BC_RESULT_TEMP;
6021
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006022 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko416ce762018-12-02 20:57:17 +01006023 bc_num_copy(&r.d.n, num);
6024 }
6025 else {
6026 r.t = BC_RESULT_STR;
6027 r.d.id.idx = num->rdx;
6028 }
6029
6030 if (!copy) bc_vec_pop(v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006031 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006032 }
6033#endif // ENABLE_DC
6034
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006035 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006036
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006037 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006038}
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006039#if ERRORS_ARE_FATAL
6040# define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS)
6041#else
6042# define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__)
6043#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006044
Denys Vlasenko29301232018-12-11 15:29:32 +01006045static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006046 char inst)
6047{
6048 BcStatus s = BC_STATUS_SUCCESS;
6049 BcResult r;
6050 BcNum *num;
6051
6052 r.d.id.name = bc_program_name(code, bgn);
6053
6054 if (inst == BC_INST_ARRAY) {
6055 r.t = BC_RESULT_ARRAY;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006056 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006057 }
6058 else {
6059
6060 BcResult *operand;
6061 unsigned long temp;
6062
Denys Vlasenko29301232018-12-11 15:29:32 +01006063 s = zbc_program_prep(&operand, &num);
Gavin Howard01055ba2018-11-03 11:00:21 -06006064 if (s) goto err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006065 s = zbc_num_ulong(num, &temp);
Gavin Howard01055ba2018-11-03 11:00:21 -06006066 if (s) goto err;
6067
6068 if (temp > BC_MAX_DIM) {
Denys Vlasenko64074a12018-12-07 15:50:14 +01006069 s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]");
Gavin Howard01055ba2018-11-03 11:00:21 -06006070 goto err;
6071 }
6072
6073 r.d.id.idx = (size_t) temp;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006074 bc_program_retire(&r, BC_RESULT_ARRAY_ELEM);
Gavin Howard01055ba2018-11-03 11:00:21 -06006075 }
6076
6077err:
6078 if (s) free(r.d.id.name);
Denys Vlasenko29301232018-12-11 15:29:32 +01006079 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006080}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006081#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006082# define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006083#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006084
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006085#if ENABLE_BC
Denys Vlasenko29301232018-12-11 15:29:32 +01006086static BC_STATUS zbc_program_incdec(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006087{
6088 BcStatus s;
6089 BcResult *ptr, res, copy;
6090 BcNum *num = NULL;
6091 char inst2 = inst;
6092
Denys Vlasenko29301232018-12-11 15:29:32 +01006093 s = zbc_program_prep(&ptr, &num);
6094 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006095
6096 if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
6097 copy.t = BC_RESULT_TEMP;
6098 bc_num_init(&copy.d.n, num->len);
6099 bc_num_copy(&copy.d.n, num);
6100 }
6101
6102 res.t = BC_RESULT_ONE;
6103 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
6104 BC_INST_ASSIGN_PLUS :
6105 BC_INST_ASSIGN_MINUS;
6106
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006107 bc_vec_push(&G.prog.results, &res);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006108 s = zbc_program_assign(inst);
6109 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006110
6111 if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006112 bc_vec_pop(&G.prog.results);
6113 bc_vec_push(&G.prog.results, &copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006114 }
6115
Denys Vlasenko29301232018-12-11 15:29:32 +01006116 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006117}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006118#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006119# define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006120#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006121
Denys Vlasenko29301232018-12-11 15:29:32 +01006122static BC_STATUS zbc_program_call(char *code, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006123{
Gavin Howard01055ba2018-11-03 11:00:21 -06006124 BcInstPtr ip;
6125 size_t i, nparams = bc_program_index(code, idx);
6126 BcFunc *func;
Gavin Howard01055ba2018-11-03 11:00:21 -06006127 BcId *a;
6128 BcResultData param;
6129 BcResult *arg;
6130
6131 ip.idx = 0;
6132 ip.func = bc_program_index(code, idx);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006133 func = bc_program_func(ip.func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006134
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006135 if (func->code.len == 0) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006136 RETURN_STATUS(bc_error("undefined function"));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006137 }
6138 if (nparams != func->nparams) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006139 RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams));
Denys Vlasenko04a1c762018-12-03 21:10:57 +01006140 }
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006141 ip.len = G.prog.results.len - nparams;
Gavin Howard01055ba2018-11-03 11:00:21 -06006142
6143 for (i = 0; i < nparams; ++i) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006144 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006145
6146 a = bc_vec_item(&func->autos, nparams - 1 - i);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006147 arg = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006148
6149 if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR)
Denys Vlasenko29301232018-12-11 15:29:32 +01006150 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006151
Denys Vlasenko29301232018-12-11 15:29:32 +01006152 s = zbc_program_copyToVar(a->name, a->idx);
6153 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006154 }
6155
6156 for (; i < func->autos.len; ++i) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006157 BcVec *v;
Gavin Howard01055ba2018-11-03 11:00:21 -06006158
6159 a = bc_vec_item(&func->autos, i);
Denys Vlasenkodf515392018-12-02 19:27:48 +01006160 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006161
6162 if (a->idx) {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006163 bc_num_init_DEF_SIZE(&param.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006164 bc_vec_push(v, &param.n);
6165 }
6166 else {
6167 bc_array_init(&param.v, true);
6168 bc_vec_push(v, &param.v);
6169 }
6170 }
6171
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006172 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006173
Denys Vlasenko29301232018-12-11 15:29:32 +01006174 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006175}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006176#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006177# define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006178#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006179
Denys Vlasenko29301232018-12-11 15:29:32 +01006180static BC_STATUS zbc_program_return(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006181{
Gavin Howard01055ba2018-11-03 11:00:21 -06006182 BcResult res;
6183 BcFunc *f;
6184 size_t i;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006185 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006186
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006187 if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET))
Denys Vlasenko29301232018-12-11 15:29:32 +01006188 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006189
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006190 f = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006191 res.t = BC_RESULT_TEMP;
6192
6193 if (inst == BC_INST_RET) {
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006194 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06006195 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006196 BcResult *operand = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006197
Denys Vlasenko29301232018-12-11 15:29:32 +01006198 s = zbc_program_num(operand, &num, false);
6199 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006200 bc_num_init(&res.d.n, num->len);
6201 bc_num_copy(&res.d.n, num);
6202 }
6203 else {
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006204 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006205 //bc_num_zero(&res.d.n); - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006206 }
6207
6208 // We need to pop arguments as well, so this takes that into account.
6209 for (i = 0; i < f->autos.len; ++i) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006210 BcVec *v;
6211 BcId *a = bc_vec_item(&f->autos, i);
6212
Denys Vlasenkodf515392018-12-02 19:27:48 +01006213 v = bc_program_search(a->name, a->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006214 bc_vec_pop(v);
6215 }
6216
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006217 bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len);
6218 bc_vec_push(&G.prog.results, &res);
6219 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006220
Denys Vlasenko29301232018-12-11 15:29:32 +01006221 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006222}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006223#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006224# define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006225#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006226#endif // ENABLE_BC
6227
6228static unsigned long bc_program_scale(BcNum *n)
6229{
6230 return (unsigned long) n->rdx;
6231}
6232
6233static unsigned long bc_program_len(BcNum *n)
6234{
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006235 size_t len = n->len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006236
Denys Vlasenkoa7f1a362018-12-10 12:57:01 +01006237 if (n->rdx != len) return len;
6238 for (;;) {
6239 if (len == 0) break;
6240 len--;
6241 if (n->num[len] != 0) break;
6242 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006243 return len;
6244}
6245
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006246static BC_STATUS zbc_program_builtin(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006247{
6248 BcStatus s;
6249 BcResult *opnd;
6250 BcNum *num = NULL;
6251 BcResult res;
6252 bool len = inst == BC_INST_LENGTH;
6253
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006254 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006255 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006256 opnd = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006257
Denys Vlasenko29301232018-12-11 15:29:32 +01006258 s = zbc_program_num(opnd, &num, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006259 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006260
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006261#if ENABLE_DC
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006262 if (!BC_PROG_NUM(opnd, num) && !len)
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006263 RETURN_STATUS(bc_error_variable_is_wrong_type());
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006264#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006265
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006266 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006267
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006268 if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006269#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006270 else if (len != 0 && opnd->t == BC_RESULT_ARRAY) {
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006271 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
Gavin Howard01055ba2018-11-03 11:00:21 -06006272 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006273#endif
6274#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006275 else if (len != 0 && !BC_PROG_NUM(opnd, num)) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006276 char **str;
6277 size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6278
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006279 str = bc_program_str(idx);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006280 bc_num_ulong2num(&res.d.n, strlen(*str));
Gavin Howard01055ba2018-11-03 11:00:21 -06006281 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006282#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006283 else {
Denys Vlasenko5ba55f12018-12-10 15:37:14 +01006284 bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num));
Gavin Howard01055ba2018-11-03 11:00:21 -06006285 }
6286
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006287 bc_program_retire(&res, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006288
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006289 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006290}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006291#if ERRORS_ARE_FATAL
6292# define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS)
6293#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006294
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006295#if ENABLE_DC
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006296static BC_STATUS zbc_program_divmod(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006297{
6298 BcStatus s;
6299 BcResult *opd1, *opd2, res, res2;
6300 BcNum *n1, *n2 = NULL;
6301
Denys Vlasenko29301232018-12-11 15:29:32 +01006302 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006303 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006304
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006305 bc_num_init_DEF_SIZE(&res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006306 bc_num_init(&res2.d.n, n2->len);
6307
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006308 s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
Gavin Howard01055ba2018-11-03 11:00:21 -06006309 if (s) goto err;
6310
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006311 bc_program_binOpRetire(&res2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006312 res.t = BC_RESULT_TEMP;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006313 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006314
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006315 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006316
6317err:
6318 bc_num_free(&res2.d.n);
6319 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006320 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006321}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006322#if ERRORS_ARE_FATAL
6323# define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS)
6324#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006325
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006326static BC_STATUS zbc_program_modexp(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006327{
6328 BcStatus s;
6329 BcResult *r1, *r2, *r3, res;
6330 BcNum *n1, *n2, *n3;
6331
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006332 if (!BC_PROG_STACK(&G.prog.results, 3))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006333 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenko29301232018-12-11 15:29:32 +01006334 s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006335 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006336
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006337 r1 = bc_vec_item_rev(&G.prog.results, 2);
Denys Vlasenko29301232018-12-11 15:29:32 +01006338 s = zbc_program_num(r1, &n1, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006339 if (s) RETURN_STATUS(s);
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006340 if (!BC_PROG_NUM(r1, n1))
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006341 RETURN_STATUS(bc_error_variable_is_wrong_type());
Gavin Howard01055ba2018-11-03 11:00:21 -06006342
6343 // Make sure that the values have their pointers updated, if necessary.
6344 if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) {
6345
6346 if (r1->t == r2->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006347 s = zbc_program_num(r2, &n2, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006348 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006349 }
6350
6351 if (r1->t == r3->t) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006352 s = zbc_program_num(r3, &n3, false);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006353 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006354 }
6355 }
6356
6357 bc_num_init(&res.d.n, n3->len);
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006358 s = zbc_num_modexp(n1, n2, n3, &res.d.n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006359 if (s) goto err;
6360
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006361 bc_vec_pop(&G.prog.results);
6362 bc_program_binOpRetire(&res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006363
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006364 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006365
6366err:
6367 bc_num_free(&res.d.n);
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006368 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006369}
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006370#if ERRORS_ARE_FATAL
6371# define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS)
6372#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006373
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006374static void bc_program_stackLen(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006375{
Gavin Howard01055ba2018-11-03 11:00:21 -06006376 BcResult res;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006377 size_t len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006378
6379 res.t = BC_RESULT_TEMP;
6380
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006381 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006382 bc_num_ulong2num(&res.d.n, len);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006383 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006384}
6385
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006386static BC_STATUS zbc_program_asciify(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006387{
6388 BcStatus s;
6389 BcResult *r, res;
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006390 BcNum *num, n;
Gavin Howard01055ba2018-11-03 11:00:21 -06006391 char *str, *str2, c;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006392 size_t len = G.prog.strs.len, idx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006393 unsigned long val;
6394
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006395 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006396 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006397 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006398
Denys Vlasenko4a024c72018-12-09 13:21:54 +01006399 num = NULL; // TODO: is this NULL needed?
Denys Vlasenko29301232018-12-11 15:29:32 +01006400 s = zbc_program_num(r, &num, false);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006401 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006402
6403 if (BC_PROG_NUM(r, num)) {
6404
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006405 bc_num_init_DEF_SIZE(&n);
Gavin Howard01055ba2018-11-03 11:00:21 -06006406 bc_num_copy(&n, num);
6407 bc_num_truncate(&n, n.rdx);
6408
Denys Vlasenko1aeacef2018-12-11 19:12:13 +01006409 s = zbc_num_mod(&n, &G.prog.strmb, &n, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006410 if (s) goto num_err;
Denys Vlasenko29301232018-12-11 15:29:32 +01006411 s = zbc_num_ulong(&n, &val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006412 if (s) goto num_err;
6413
6414 c = (char) val;
6415
6416 bc_num_free(&n);
6417 }
6418 else {
6419 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006420 str2 = *bc_program_str(idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006421 c = str2[0];
6422 }
6423
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006424 str = xzalloc(2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006425 str[0] = c;
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006426 //str[1] = '\0'; - already is
Gavin Howard01055ba2018-11-03 11:00:21 -06006427
6428 str2 = xstrdup(str);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006429 bc_program_addFunc(str2, &idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006430
6431 if (idx != len + BC_PROG_REQ_FUNCS) {
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006432 for (idx = 0; idx < G.prog.strs.len; ++idx) {
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006433 if (strcmp(*bc_program_str(idx), str) == 0) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006434 len = idx;
6435 break;
6436 }
6437 }
6438
6439 free(str);
6440 }
6441 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006442 bc_vec_push(&G.prog.strs, &str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006443
6444 res.t = BC_RESULT_STR;
6445 res.d.id.idx = len;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006446 bc_vec_pop(&G.prog.results);
6447 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006448
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006449 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006450
6451num_err:
6452 bc_num_free(&n);
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006453 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006454}
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006455#if ERRORS_ARE_FATAL
6456# define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__), BC_STATUS_SUCCESS)
6457#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006458
Denys Vlasenko29301232018-12-11 15:29:32 +01006459static BC_STATUS zbc_program_printStream(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006460{
6461 BcStatus s;
6462 BcResult *r;
6463 BcNum *n = NULL;
6464 size_t idx;
6465 char *str;
6466
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006467 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko29301232018-12-11 15:29:32 +01006468 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006469 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006470
Denys Vlasenko29301232018-12-11 15:29:32 +01006471 s = zbc_program_num(r, &n, false);
6472 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006473
6474 if (BC_PROG_NUM(r, n))
Denys Vlasenko29301232018-12-11 15:29:32 +01006475 s = zbc_num_stream(n, &G.prog.strmb);
Gavin Howard01055ba2018-11-03 11:00:21 -06006476 else {
6477 idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx;
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006478 str = *bc_program_str(idx);
Denys Vlasenko00d77792018-11-30 23:13:42 +01006479 printf("%s", str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006480 }
6481
Denys Vlasenko29301232018-12-11 15:29:32 +01006482 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006483}
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006484#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006485# define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenko628bf1b2018-12-10 20:41:05 +01006486#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006487
Denys Vlasenko29301232018-12-11 15:29:32 +01006488static BC_STATUS zbc_program_nquit(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006489{
6490 BcStatus s;
6491 BcResult *opnd;
6492 BcNum *num = NULL;
6493 unsigned long val;
6494
Denys Vlasenko29301232018-12-11 15:29:32 +01006495 s = zbc_program_prep(&opnd, &num);
6496 if (s) RETURN_STATUS(s);
6497 s = zbc_num_ulong(num, &val);
6498 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006499
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006500 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006501
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006502 if (G.prog.stack.len < val)
Denys Vlasenko29301232018-12-11 15:29:32 +01006503 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006504 if (G.prog.stack.len == val) {
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006505 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01006506 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006507
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006508 bc_vec_npop(&G.prog.stack, val);
Gavin Howard01055ba2018-11-03 11:00:21 -06006509
Denys Vlasenko29301232018-12-11 15:29:32 +01006510 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006511}
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006512#if ERRORS_ARE_FATAL
Denys Vlasenko29301232018-12-11 15:29:32 +01006513# define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS)
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006514#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006515
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006516static BC_STATUS zbc_program_execStr(char *code, size_t *bgn,
Gavin Howard01055ba2018-11-03 11:00:21 -06006517 bool cond)
6518{
6519 BcStatus s = BC_STATUS_SUCCESS;
6520 BcResult *r;
6521 char **str;
6522 BcFunc *f;
6523 BcParse prs;
6524 BcInstPtr ip;
6525 size_t fidx, sidx;
Gavin Howard01055ba2018-11-03 11:00:21 -06006526
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006527 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006528 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Gavin Howard01055ba2018-11-03 11:00:21 -06006529
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006530 r = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006531
6532 if (cond) {
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006533 BcNum *n = n; // for compiler
6534 bool exec;
6535 char *name;
6536 char *then_name = bc_program_name(code, bgn);
6537 char *else_name = NULL;
Gavin Howard01055ba2018-11-03 11:00:21 -06006538
6539 if (code[*bgn] == BC_PARSE_STREND)
6540 (*bgn) += 1;
6541 else
6542 else_name = bc_program_name(code, bgn);
6543
6544 exec = r->d.n.len != 0;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006545 name = then_name;
6546 if (!exec && else_name != NULL) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006547 exec = true;
6548 name = else_name;
6549 }
6550
6551 if (exec) {
Denys Vlasenkodf515392018-12-02 19:27:48 +01006552 BcVec *v;
6553 v = bc_program_search(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006554 n = bc_vec_top(v);
6555 }
6556
6557 free(then_name);
6558 free(else_name);
6559
6560 if (!exec) goto exit;
6561 if (!BC_PROG_STR(n)) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006562 s = bc_error_variable_is_wrong_type();
Gavin Howard01055ba2018-11-03 11:00:21 -06006563 goto exit;
6564 }
6565
6566 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006567 } else {
6568 if (r->t == BC_RESULT_STR) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006569 sidx = r->d.id.idx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006570 } else if (r->t == BC_RESULT_VAR) {
6571 BcNum *n;
Denys Vlasenko29301232018-12-11 15:29:32 +01006572 s = zbc_program_num(r, &n, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006573 if (s || !BC_PROG_STR(n)) goto exit;
6574 sidx = n->rdx;
Denys Vlasenko5ec4b492018-12-09 02:54:06 +01006575 } else
Gavin Howard01055ba2018-11-03 11:00:21 -06006576 goto exit;
6577 }
6578
6579 fidx = sidx + BC_PROG_REQ_FUNCS;
6580
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006581 str = bc_program_str(sidx);
6582 f = bc_program_func(fidx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006583
6584 if (f->code.len == 0) {
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01006585 bc_parse_create(&prs, fidx);
Denys Vlasenko26819db2018-12-12 16:08:46 +01006586 s = zbc_parse_text(&prs, *str);
Gavin Howard01055ba2018-11-03 11:00:21 -06006587 if (s) goto err;
Denys Vlasenkoae0faf92018-12-12 15:19:54 +01006588 s = zcommon_parse_expr(&prs, BC_PARSE_NOCALL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006589 if (s) goto err;
6590
6591 if (prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006592 s = bc_error_bad_expression();
Gavin Howard01055ba2018-11-03 11:00:21 -06006593 goto err;
6594 }
6595
6596 bc_parse_free(&prs);
6597 }
6598
6599 ip.idx = 0;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006600 ip.len = G.prog.results.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006601 ip.func = fidx;
6602
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006603 bc_vec_pop(&G.prog.results);
6604 bc_vec_push(&G.prog.stack, &ip);
Gavin Howard01055ba2018-11-03 11:00:21 -06006605
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006606 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006607
6608err:
6609 bc_parse_free(&prs);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006610 f = bc_program_func(fidx);
Denys Vlasenko7d628012018-12-04 21:46:47 +01006611 bc_vec_pop_all(&f->code);
Gavin Howard01055ba2018-11-03 11:00:21 -06006612exit:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006613 bc_vec_pop(&G.prog.results);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006614 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006615}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006616#if ERRORS_ARE_FATAL
6617# define zbc_program_execStr(...) (zbc_program_execStr(__VA_ARGS__), BC_STATUS_SUCCESS)
6618#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006619#endif // ENABLE_DC
6620
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006621static void bc_program_pushGlobal(char inst)
Gavin Howard01055ba2018-11-03 11:00:21 -06006622{
Gavin Howard01055ba2018-11-03 11:00:21 -06006623 BcResult res;
6624 unsigned long val;
6625
6626 res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
6627 if (inst == BC_INST_IBASE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006628 val = (unsigned long) G.prog.ib_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006629 else if (inst == BC_INST_SCALE)
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006630 val = (unsigned long) G.prog.scale;
Gavin Howard01055ba2018-11-03 11:00:21 -06006631 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006632 val = (unsigned long) G.prog.ob_t;
Gavin Howard01055ba2018-11-03 11:00:21 -06006633
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006634 bc_num_init_DEF_SIZE(&res.d.n);
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006635 bc_num_ulong2num(&res.d.n, val);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006636 bc_vec_push(&G.prog.results, &res);
Gavin Howard01055ba2018-11-03 11:00:21 -06006637}
6638
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006639static void bc_program_addFunc(char *name, size_t *idx)
Gavin Howard01055ba2018-11-03 11:00:21 -06006640{
Gavin Howard01055ba2018-11-03 11:00:21 -06006641 BcId entry, *entry_ptr;
6642 BcFunc f;
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006643 int inserted;
Gavin Howard01055ba2018-11-03 11:00:21 -06006644
6645 entry.name = name;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006646 entry.idx = G.prog.fns.len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006647
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006648 inserted = bc_map_insert(&G.prog.fn_map, &entry, idx);
6649 if (!inserted) free(name);
Gavin Howard01055ba2018-11-03 11:00:21 -06006650
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006651 entry_ptr = bc_vec_item(&G.prog.fn_map, *idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006652 *idx = entry_ptr->idx;
6653
Denys Vlasenkoa02f8442018-12-03 20:35:16 +01006654 if (!inserted) {
Gavin Howard01055ba2018-11-03 11:00:21 -06006655
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006656 BcFunc *func = bc_program_func(entry_ptr->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006657
6658 // We need to reset these, so the function can be repopulated.
6659 func->nparams = 0;
Denys Vlasenko7d628012018-12-04 21:46:47 +01006660 bc_vec_pop_all(&func->autos);
6661 bc_vec_pop_all(&func->code);
6662 bc_vec_pop_all(&func->labels);
Gavin Howard01055ba2018-11-03 11:00:21 -06006663 }
6664 else {
6665 bc_func_init(&f);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006666 bc_vec_push(&G.prog.fns, &f);
Gavin Howard01055ba2018-11-03 11:00:21 -06006667 }
6668}
6669
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006670static BC_STATUS zbc_program_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006671{
Gavin Howard01055ba2018-11-03 11:00:21 -06006672 BcResult r, *ptr;
6673 BcNum *num;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006674 BcInstPtr *ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006675 BcFunc *func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006676 char *code = func->code.v;
6677 bool cond = false;
6678
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006679 while (ip->idx < func->code.len) {
Denys Vlasenkofa35e592018-12-10 20:17:24 +01006680 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard01055ba2018-11-03 11:00:21 -06006681 char inst = code[(ip->idx)++];
6682
6683 switch (inst) {
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006684#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006685 case BC_INST_JUMP_ZERO:
Denys Vlasenko29301232018-12-11 15:29:32 +01006686 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006687 if (s) RETURN_STATUS(s);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006688 cond = !bc_num_cmp(num, &G.prog.zero);
6689 bc_vec_pop(&G.prog.results);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006690 // Fallthrough.
6691 case BC_INST_JUMP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006692 size_t *addr;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006693 size_t idx = bc_program_index(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006694 addr = bc_vec_item(&func->labels, idx);
6695 if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
6696 break;
6697 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006698 case BC_INST_CALL:
Denys Vlasenko29301232018-12-11 15:29:32 +01006699 s = zbc_program_call(code, &ip->idx);
Gavin Howard01055ba2018-11-03 11:00:21 -06006700 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006701 case BC_INST_INC_PRE:
6702 case BC_INST_DEC_PRE:
6703 case BC_INST_INC_POST:
6704 case BC_INST_DEC_POST:
Denys Vlasenko29301232018-12-11 15:29:32 +01006705 s = zbc_program_incdec(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006706 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006707 case BC_INST_HALT:
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006708 QUIT_OR_RETURN_TO_MAIN;
Gavin Howard01055ba2018-11-03 11:00:21 -06006709 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006710 case BC_INST_RET:
6711 case BC_INST_RET0:
Denys Vlasenko29301232018-12-11 15:29:32 +01006712 s = zbc_program_return(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006713 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006714 case BC_INST_BOOL_OR:
6715 case BC_INST_BOOL_AND:
6716#endif // ENABLE_BC
6717 case BC_INST_REL_EQ:
6718 case BC_INST_REL_LE:
6719 case BC_INST_REL_GE:
6720 case BC_INST_REL_NE:
6721 case BC_INST_REL_LT:
6722 case BC_INST_REL_GT:
Denys Vlasenko728e7c92018-12-11 19:37:00 +01006723 s = zbc_program_logical(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006724 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006725 case BC_INST_READ:
Denys Vlasenko26819db2018-12-12 16:08:46 +01006726 s = zbc_program_read();
Gavin Howard01055ba2018-11-03 11:00:21 -06006727 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006728 case BC_INST_VAR:
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006729 s = zbc_program_pushVar(code, &ip->idx, false, false);
Gavin Howard01055ba2018-11-03 11:00:21 -06006730 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006731 case BC_INST_ARRAY_ELEM:
6732 case BC_INST_ARRAY:
Denys Vlasenko29301232018-12-11 15:29:32 +01006733 s = zbc_program_pushArray(code, &ip->idx, inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006734 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006735 case BC_INST_LAST:
Gavin Howard01055ba2018-11-03 11:00:21 -06006736 r.t = BC_RESULT_LAST;
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006737 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006738 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006739 case BC_INST_IBASE:
6740 case BC_INST_SCALE:
6741 case BC_INST_OBASE:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006742 bc_program_pushGlobal(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006743 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006744 case BC_INST_SCALE_FUNC:
6745 case BC_INST_LENGTH:
6746 case BC_INST_SQRT:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006747 s = zbc_program_builtin(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006748 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006749 case BC_INST_NUM:
Gavin Howard01055ba2018-11-03 11:00:21 -06006750 r.t = BC_RESULT_CONSTANT;
6751 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006752 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006753 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006754 case BC_INST_POP:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006755 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01006756 s = bc_error_stack_has_too_few_elements();
Gavin Howard01055ba2018-11-03 11:00:21 -06006757 else
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006758 bc_vec_pop(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006759 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006760 case BC_INST_POP_EXEC:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006761 bc_vec_pop(&G.prog.stack);
Gavin Howard01055ba2018-11-03 11:00:21 -06006762 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006763 case BC_INST_PRINT:
6764 case BC_INST_PRINT_POP:
6765 case BC_INST_PRINT_STR:
Denys Vlasenko29301232018-12-11 15:29:32 +01006766 s = zbc_program_print(inst, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06006767 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006768 case BC_INST_STR:
Gavin Howard01055ba2018-11-03 11:00:21 -06006769 r.t = BC_RESULT_STR;
6770 r.d.id.idx = bc_program_index(code, &ip->idx);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006771 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006772 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006773 case BC_INST_POWER:
6774 case BC_INST_MULTIPLY:
6775 case BC_INST_DIVIDE:
6776 case BC_INST_MODULUS:
6777 case BC_INST_PLUS:
6778 case BC_INST_MINUS:
Denys Vlasenko259137d2018-12-11 19:42:05 +01006779 s = zbc_program_op(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006780 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006781 case BC_INST_BOOL_NOT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006782 s = zbc_program_prep(&ptr, &num);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006783 if (s) RETURN_STATUS(s);
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01006784 bc_num_init_DEF_SIZE(&r.d.n);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006785 if (!bc_num_cmp(num, &G.prog.zero))
6786 bc_num_one(&r.d.n);
Denys Vlasenko3129f702018-12-09 12:04:44 +01006787 //else bc_num_zero(&r.d.n); - already is
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006788 bc_program_retire(&r, BC_RESULT_TEMP);
Gavin Howard01055ba2018-11-03 11:00:21 -06006789 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006790 case BC_INST_NEG:
Denys Vlasenko29301232018-12-11 15:29:32 +01006791 s = zbc_program_negate();
Gavin Howard01055ba2018-11-03 11:00:21 -06006792 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006793#if ENABLE_BC
Gavin Howard01055ba2018-11-03 11:00:21 -06006794 case BC_INST_ASSIGN_POWER:
6795 case BC_INST_ASSIGN_MULTIPLY:
6796 case BC_INST_ASSIGN_DIVIDE:
6797 case BC_INST_ASSIGN_MODULUS:
6798 case BC_INST_ASSIGN_PLUS:
6799 case BC_INST_ASSIGN_MINUS:
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006800#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006801 case BC_INST_ASSIGN:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006802 s = zbc_program_assign(inst);
Gavin Howard01055ba2018-11-03 11:00:21 -06006803 break;
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01006804#if ENABLE_DC
Gavin Howard01055ba2018-11-03 11:00:21 -06006805 case BC_INST_MODEXP:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006806 s = zbc_program_modexp();
Gavin Howard01055ba2018-11-03 11:00:21 -06006807 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006808 case BC_INST_DIVMOD:
Denys Vlasenko7f4daa42018-12-11 19:04:44 +01006809 s = zbc_program_divmod();
Gavin Howard01055ba2018-11-03 11:00:21 -06006810 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006811 case BC_INST_EXECUTE:
6812 case BC_INST_EXEC_COND:
Gavin Howard01055ba2018-11-03 11:00:21 -06006813 cond = inst == BC_INST_EXEC_COND;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006814 s = zbc_program_execStr(code, &ip->idx, cond);
Gavin Howard01055ba2018-11-03 11:00:21 -06006815 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006816 case BC_INST_PRINT_STACK: {
6817 size_t idx;
6818 for (idx = 0; idx < G.prog.results.len; ++idx) {
Denys Vlasenko29301232018-12-11 15:29:32 +01006819 s = zbc_program_print(BC_INST_PRINT, idx);
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006820 if (s) break;
6821 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006822 break;
6823 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006824 case BC_INST_CLEAR_STACK:
Denys Vlasenko7d628012018-12-04 21:46:47 +01006825 bc_vec_pop_all(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006826 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006827 case BC_INST_STACK_LEN:
Denys Vlasenkoe3b4f232018-12-02 18:44:40 +01006828 bc_program_stackLen();
Gavin Howard01055ba2018-11-03 11:00:21 -06006829 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006830 case BC_INST_DUPLICATE:
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006831 if (!BC_PROG_STACK(&G.prog.results, 1))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006832 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006833 ptr = bc_vec_top(&G.prog.results);
Gavin Howard01055ba2018-11-03 11:00:21 -06006834 bc_result_copy(&r, ptr);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006835 bc_vec_push(&G.prog.results, &r);
Gavin Howard01055ba2018-11-03 11:00:21 -06006836 break;
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006837 case BC_INST_SWAP: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006838 BcResult *ptr2;
Denys Vlasenko60cf7472018-12-04 20:05:28 +01006839 if (!BC_PROG_STACK(&G.prog.results, 2))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006840 RETURN_STATUS(bc_error_stack_has_too_few_elements());
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006841 ptr = bc_vec_item_rev(&G.prog.results, 0);
6842 ptr2 = bc_vec_item_rev(&G.prog.results, 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006843 memcpy(&r, ptr, sizeof(BcResult));
6844 memcpy(ptr, ptr2, sizeof(BcResult));
6845 memcpy(ptr2, &r, sizeof(BcResult));
Gavin Howard01055ba2018-11-03 11:00:21 -06006846 break;
6847 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006848 case BC_INST_ASCIIFY:
Denys Vlasenkoc008a732018-12-11 20:57:53 +01006849 s = zbc_program_asciify();
Gavin Howard01055ba2018-11-03 11:00:21 -06006850 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006851 case BC_INST_PRINT_STREAM:
Denys Vlasenko29301232018-12-11 15:29:32 +01006852 s = zbc_program_printStream();
Gavin Howard01055ba2018-11-03 11:00:21 -06006853 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006854 case BC_INST_LOAD:
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006855 case BC_INST_PUSH_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006856 bool copy = inst == BC_INST_LOAD;
Denys Vlasenkob402ff82018-12-11 15:45:15 +01006857 s = zbc_program_pushVar(code, &ip->idx, true, copy);
Gavin Howard01055ba2018-11-03 11:00:21 -06006858 break;
6859 }
Denys Vlasenko927a7d62018-12-09 02:24:14 +01006860 case BC_INST_PUSH_TO_VAR: {
Gavin Howard01055ba2018-11-03 11:00:21 -06006861 char *name = bc_program_name(code, &ip->idx);
Denys Vlasenko29301232018-12-11 15:29:32 +01006862 s = zbc_program_copyToVar(name, true);
Gavin Howard01055ba2018-11-03 11:00:21 -06006863 free(name);
6864 break;
6865 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006866 case BC_INST_QUIT:
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006867 if (G.prog.stack.len <= 2)
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01006868 QUIT_OR_RETURN_TO_MAIN;
Denys Vlasenkocfdc1332018-12-03 14:02:35 +01006869 bc_vec_npop(&G.prog.stack, 2);
Gavin Howard01055ba2018-11-03 11:00:21 -06006870 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006871 case BC_INST_NQUIT:
Denys Vlasenko29301232018-12-11 15:29:32 +01006872 s = zbc_program_nquit();
Gavin Howard01055ba2018-11-03 11:00:21 -06006873 break;
Gavin Howard01055ba2018-11-03 11:00:21 -06006874#endif // ENABLE_DC
6875 }
6876
Denys Vlasenkod38af482018-12-04 19:11:02 +01006877 if (s || G_interrupt) {
6878 bc_program_reset();
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006879 RETURN_STATUS(s);
Denys Vlasenkod38af482018-12-04 19:11:02 +01006880 }
Gavin Howard01055ba2018-11-03 11:00:21 -06006881
6882 // If the stack has changed, pointers may be invalid.
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01006883 ip = bc_vec_top(&G.prog.stack);
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01006884 func = bc_program_func(ip->func);
Gavin Howard01055ba2018-11-03 11:00:21 -06006885 code = func->code.v;
6886 }
6887
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006888 RETURN_STATUS(BC_STATUS_SUCCESS);
Gavin Howard01055ba2018-11-03 11:00:21 -06006889}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006890#if ERRORS_ARE_FATAL
6891# define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__), BC_STATUS_SUCCESS)
6892#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06006893
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006894#if ENABLE_BC
Denys Vlasenko54214c32018-12-06 09:07:06 +01006895static void bc_vm_info(void)
6896{
6897 printf("%s "BB_VER"\n"
6898 "Copyright (c) 2018 Gavin D. Howard and contributors\n"
Denys Vlasenko54214c32018-12-06 09:07:06 +01006899 , applet_name);
6900}
6901
6902static void bc_args(char **argv)
6903{
6904 unsigned opts;
6905 int i;
6906
6907 GETOPT_RESET();
6908#if ENABLE_FEATURE_BC_LONG_OPTIONS
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006909 opts = option_mask32 |= getopt32long(argv, "wvsqli",
Denys Vlasenko54214c32018-12-06 09:07:06 +01006910 "warn\0" No_argument "w"
6911 "version\0" No_argument "v"
6912 "standard\0" No_argument "s"
6913 "quiet\0" No_argument "q"
6914 "mathlib\0" No_argument "l"
6915 "interactive\0" No_argument "i"
6916 );
6917#else
Denys Vlasenko6d0be102018-12-06 18:41:59 +01006918 opts = option_mask32 |= getopt32(argv, "wvsqli");
Denys Vlasenko54214c32018-12-06 09:07:06 +01006919#endif
6920 if (getenv("POSIXLY_CORRECT"))
6921 option_mask32 |= BC_FLAG_S;
6922
Denys Vlasenko54214c32018-12-06 09:07:06 +01006923 if (opts & BC_FLAG_V) {
6924 bc_vm_info();
6925 exit(0);
6926 }
6927
6928 for (i = optind; argv[i]; ++i)
6929 bc_vec_push(&G.files, argv + i);
6930}
6931
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006932static void bc_vm_envArgs(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06006933{
Gavin Howard01055ba2018-11-03 11:00:21 -06006934 BcVec v;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006935 char *buf;
Denys Vlasenko54214c32018-12-06 09:07:06 +01006936 char *env_args = getenv("BC_ENV_ARGS");
Gavin Howard01055ba2018-11-03 11:00:21 -06006937
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01006938 if (!env_args) return;
Gavin Howard01055ba2018-11-03 11:00:21 -06006939
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006940 G.env_args = xstrdup(env_args);
6941 buf = G.env_args;
Gavin Howard01055ba2018-11-03 11:00:21 -06006942
6943 bc_vec_init(&v, sizeof(char *), NULL);
Gavin Howard01055ba2018-11-03 11:00:21 -06006944
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006945 while (*(buf = skip_whitespace(buf)) != '\0') {
6946 bc_vec_push(&v, &buf);
6947 buf = skip_non_whitespace(buf);
6948 if (!*buf)
6949 break;
6950 *buf++ = '\0';
Gavin Howard01055ba2018-11-03 11:00:21 -06006951 }
6952
Denys Vlasenko54214c32018-12-06 09:07:06 +01006953 // NULL terminate, and pass argv[] so that first arg is argv[1]
6954 if (sizeof(int) == sizeof(char*)) {
6955 bc_vec_push(&v, &const_int_0);
6956 } else {
6957 static char *const nullptr = NULL;
6958 bc_vec_push(&v, &nullptr);
6959 }
6960 bc_args(((char **)v.v) - 1);
Gavin Howard01055ba2018-11-03 11:00:21 -06006961
6962 bc_vec_free(&v);
Gavin Howard01055ba2018-11-03 11:00:21 -06006963}
6964#endif // ENABLE_BC
6965
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006966static unsigned bc_vm_envLen(const char *var)
Gavin Howard01055ba2018-11-03 11:00:21 -06006967{
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006968 char *lenv;
6969 unsigned len;
Gavin Howard01055ba2018-11-03 11:00:21 -06006970
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006971 lenv = getenv(var);
6972 len = BC_NUM_PRINT_WIDTH;
Gavin Howard01055ba2018-11-03 11:00:21 -06006973 if (!lenv) return len;
6974
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01006975 len = bb_strtou(lenv, NULL, 10) - 1;
6976 if (errno || len < 2 || len >= INT_MAX)
Gavin Howard01055ba2018-11-03 11:00:21 -06006977 len = BC_NUM_PRINT_WIDTH;
6978
6979 return len;
6980}
6981
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006982static BC_STATUS zbc_vm_process(const char *text)
Gavin Howard01055ba2018-11-03 11:00:21 -06006983{
Denys Vlasenko26819db2018-12-12 16:08:46 +01006984 BcStatus s = zbc_parse_text(&G.prs, text);
Gavin Howard01055ba2018-11-03 11:00:21 -06006985
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006986 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006987
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006988 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01006989 ERROR_RETURN(s =) zcommon_parse(&G.prs);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006990 if (s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06006991 }
6992
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01006993 if (BC_PARSE_CAN_EXEC(&G.prs)) {
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01006994 s = zbc_program_exec();
Denys Vlasenkod4744ad2018-12-03 14:28:51 +01006995 fflush_and_check();
Denys Vlasenko9b70f192018-12-04 20:51:40 +01006996 if (s)
Denys Vlasenkod38af482018-12-04 19:11:02 +01006997 bc_program_reset();
Gavin Howard01055ba2018-11-03 11:00:21 -06006998 }
6999
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007000 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007001}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007002#if ERRORS_ARE_FATAL
7003# define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__), BC_STATUS_SUCCESS)
7004#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007005
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007006static BC_STATUS zbc_vm_file(const char *file)
Gavin Howard01055ba2018-11-03 11:00:21 -06007007{
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007008 const char *sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007009 char *data;
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007010 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007011 BcFunc *main_func;
7012 BcInstPtr *ip;
7013
Denys Vlasenkodf515392018-12-02 19:27:48 +01007014 data = bc_read_file(file);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007015 if (!data) RETURN_STATUS(bc_error_fmt("file '%s' is not text", file));
Gavin Howard01055ba2018-11-03 11:00:21 -06007016
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007017 sv_file = G.prog.file;
7018 G.prog.file = file;
7019 bc_lex_file(&G.prs.l);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007020 s = zbc_vm_process(data);
Gavin Howard01055ba2018-11-03 11:00:21 -06007021 if (s) goto err;
7022
Denys Vlasenko8fa1e8e2018-12-09 00:03:57 +01007023 main_func = bc_program_func(BC_PROG_MAIN);
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007024 ip = bc_vec_item(&G.prog.stack, 0);
Gavin Howard01055ba2018-11-03 11:00:21 -06007025
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007026 if (main_func->code.len < ip->idx)
Denys Vlasenko24fb2cd2018-12-05 16:03:46 +01007027 s = bc_error_fmt("file '%s' is not executable", file);
Gavin Howard01055ba2018-11-03 11:00:21 -06007028
7029err:
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007030 G.prog.file = sv_file;
Gavin Howard01055ba2018-11-03 11:00:21 -06007031 free(data);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007032 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007033}
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007034#if ERRORS_ARE_FATAL
7035# define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__), BC_STATUS_SUCCESS)
7036#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007037
Denys Vlasenko19f11072018-12-12 22:48:19 +01007038static BC_STATUS zbc_vm_stdin(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007039{
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007040 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007041 BcVec buf, buffer;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007042 size_t str;
7043 bool comment;
Gavin Howard01055ba2018-11-03 11:00:21 -06007044
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007045 G.prog.file = NULL;
7046 bc_lex_file(&G.prs.l);
Gavin Howard01055ba2018-11-03 11:00:21 -06007047
Denys Vlasenko7d628012018-12-04 21:46:47 +01007048 bc_char_vec_init(&buffer);
7049 bc_char_vec_init(&buf);
Denys Vlasenko08c033c2018-12-05 16:55:08 +01007050 bc_vec_pushZeroByte(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007051
7052 // This loop is complex because the vm tries not to send any lines that end
7053 // with a backslash to the parser. The reason for that is because the parser
7054 // treats a backslash+newline combo as whitespace, per the bc spec. In that
7055 // case, and for strings and comments, the parser will expect more stuff.
Denys Vlasenko8a892472018-12-12 22:43:58 +01007056 s = BC_STATUS_SUCCESS;
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007057 comment = false;
7058 str = 0;
Denys Vlasenko8a892472018-12-12 22:43:58 +01007059 for (;;) {
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007060 size_t len;
Denys Vlasenko8a892472018-12-12 22:43:58 +01007061 char *string;
Gavin Howard01055ba2018-11-03 11:00:21 -06007062
Denys Vlasenko8a892472018-12-12 22:43:58 +01007063 bc_read_line(&buf);
Gavin Howard01055ba2018-11-03 11:00:21 -06007064 len = buf.len - 1;
Denys Vlasenko8a892472018-12-12 22:43:58 +01007065 if (len == 0) // "" buf means EOF
7066 break;
7067 string = buf.v;
Gavin Howard01055ba2018-11-03 11:00:21 -06007068 if (len == 1) {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007069 if (str && buf.v[0] == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007070 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007071 else if (buf.v[0] == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007072 str += 1;
7073 }
7074 else if (len > 1 || comment) {
Denys Vlasenko4dd36522018-12-11 22:26:38 +01007075 size_t i;
Gavin Howard01055ba2018-11-03 11:00:21 -06007076 for (i = 0; i < len; ++i) {
Denys Vlasenkoa0c421c2018-12-02 20:16:52 +01007077 bool notend = len > i + 1;
7078 char c = string[i];
Gavin Howard01055ba2018-11-03 11:00:21 -06007079
7080 if (i - 1 > len || string[i - 1] != '\\') {
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007081 if (G.sbgn == G.send)
7082 str ^= c == G.sbgn;
7083 else if (c == G.send)
Gavin Howard01055ba2018-11-03 11:00:21 -06007084 str -= 1;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007085 else if (c == G.sbgn)
Gavin Howard01055ba2018-11-03 11:00:21 -06007086 str += 1;
7087 }
7088
7089 if (c == '/' && notend && !comment && string[i + 1] == '*') {
7090 comment = true;
7091 break;
7092 }
7093 else if (c == '*' && notend && comment && string[i + 1] == '/')
7094 comment = false;
7095 }
7096
7097 if (str || comment || string[len - 2] == '\\') {
7098 bc_vec_concat(&buffer, buf.v);
7099 continue;
7100 }
7101 }
7102
7103 bc_vec_concat(&buffer, buf.v);
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007104 s = zbc_vm_process(buffer.v);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007105 if (s) {
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007106 if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) {
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007107 // Debug config, non-interactive mode:
7108 // return all the way back to main.
7109 // Non-debug builds do not come here, they exit.
7110 break;
7111 }
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007112 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007113
Denys Vlasenko7d628012018-12-04 21:46:47 +01007114 bc_vec_pop_all(&buffer);
Gavin Howard01055ba2018-11-03 11:00:21 -06007115 }
7116
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007117 if (str) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007118 s = bc_error("string end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007119 }
7120 else if (comment) {
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007121 s = bc_error("comment end could not be found");
Denys Vlasenko60cf7472018-12-04 20:05:28 +01007122 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007123
Gavin Howard01055ba2018-11-03 11:00:21 -06007124 bc_vec_free(&buf);
7125 bc_vec_free(&buffer);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007126 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007127}
Denys Vlasenko19f11072018-12-12 22:48:19 +01007128#if ERRORS_ARE_FATAL
7129# define zbc_vm_stdin(...) (zbc_vm_stdin(__VA_ARGS__), BC_STATUS_SUCCESS)
7130#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007131
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007132#if ENABLE_BC
7133static const char bc_lib[] = {
7134 "scale=20"
7135"\n" "define e(x){"
7136"\n" "auto b,s,n,r,d,i,p,f,v"
7137"\n" "b=ibase"
7138"\n" "ibase=A"
7139"\n" "if(x<0){"
7140"\n" "n=1"
7141"\n" "x=-x"
7142"\n" "}"
7143"\n" "s=scale"
7144"\n" "r=6+s+0.44*x"
7145"\n" "scale=scale(x)+1"
7146"\n" "while(x>1){"
7147"\n" "d+=1"
7148"\n" "x/=2"
7149"\n" "scale+=1"
7150"\n" "}"
7151"\n" "scale=r"
7152"\n" "r=x+1"
7153"\n" "p=x"
7154"\n" "f=v=1"
7155"\n" "for(i=2;v!=0;++i){"
7156"\n" "p*=x"
7157"\n" "f*=i"
7158"\n" "v=p/f"
7159"\n" "r+=v"
7160"\n" "}"
7161"\n" "while((d--)!=0)r*=r"
7162"\n" "scale=s"
7163"\n" "ibase=b"
7164"\n" "if(n!=0)return(1/r)"
7165"\n" "return(r/1)"
7166"\n" "}"
7167"\n" "define l(x){"
7168"\n" "auto b,s,r,p,a,q,i,v"
7169"\n" "b=ibase"
7170"\n" "ibase=A"
7171"\n" "if(x<=0){"
7172"\n" "r=(1-10^scale)/1"
7173"\n" "ibase=b"
7174"\n" "return(r)"
7175"\n" "}"
7176"\n" "s=scale"
7177"\n" "scale+=6"
7178"\n" "p=2"
7179"\n" "while(x>=2){"
7180"\n" "p*=2"
7181"\n" "x=sqrt(x)"
7182"\n" "}"
7183"\n" "while(x<=0.5){"
7184"\n" "p*=2"
7185"\n" "x=sqrt(x)"
7186"\n" "}"
7187"\n" "r=a=(x-1)/(x+1)"
7188"\n" "q=a*a"
7189"\n" "v=1"
7190"\n" "for(i=3;v!=0;i+=2){"
7191"\n" "a*=q"
7192"\n" "v=a/i"
7193"\n" "r+=v"
7194"\n" "}"
7195"\n" "r*=p"
7196"\n" "scale=s"
7197"\n" "ibase=b"
7198"\n" "return(r/1)"
7199"\n" "}"
7200"\n" "define s(x){"
7201"\n" "auto b,s,r,n,a,q,i"
7202"\n" "b=ibase"
7203"\n" "ibase=A"
7204"\n" "s=scale"
7205"\n" "scale=1.1*s+2"
7206"\n" "a=a(1)"
7207"\n" "if(x<0){"
7208"\n" "n=1"
7209"\n" "x=-x"
7210"\n" "}"
7211"\n" "scale=0"
7212"\n" "q=(x/a+2)/4"
7213"\n" "x=x-4*q*a"
7214"\n" "if(q%2!=0)x=-x"
7215"\n" "scale=s+2"
7216"\n" "r=a=x"
7217"\n" "q=-x*x"
7218"\n" "for(i=3;a!=0;i+=2){"
7219"\n" "a*=q/(i*(i-1))"
7220"\n" "r+=a"
7221"\n" "}"
7222"\n" "scale=s"
7223"\n" "ibase=b"
7224"\n" "if(n!=0)return(-r/1)"
7225"\n" "return(r/1)"
7226"\n" "}"
7227"\n" "define c(x){"
7228"\n" "auto b,s"
7229"\n" "b=ibase"
7230"\n" "ibase=A"
7231"\n" "s=scale"
7232"\n" "scale*=1.2"
7233"\n" "x=s(2*a(1)+x)"
7234"\n" "scale=s"
7235"\n" "ibase=b"
7236"\n" "return(x/1)"
7237"\n" "}"
7238"\n" "define a(x){"
7239"\n" "auto b,s,r,n,a,m,t,f,i,u"
7240"\n" "b=ibase"
7241"\n" "ibase=A"
7242"\n" "n=1"
7243"\n" "if(x<0){"
7244"\n" "n=-1"
7245"\n" "x=-x"
7246"\n" "}"
7247"\n" "if(x==1){"
7248"\n" "if(scale<65){"
7249"\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)"
7250"\n" "}"
7251"\n" "}"
7252"\n" "if(x==.2){"
7253"\n" "if(scale<65){"
7254"\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)"
7255"\n" "}"
7256"\n" "}"
7257"\n" "s=scale"
7258"\n" "if(x>.2){"
7259"\n" "scale+=5"
7260"\n" "a=a(.2)"
7261"\n" "}"
7262"\n" "scale=s+3"
7263"\n" "while(x>.2){"
7264"\n" "m+=1"
7265"\n" "x=(x-.2)/(1+.2*x)"
7266"\n" "}"
7267"\n" "r=u=x"
7268"\n" "f=-x*x"
7269"\n" "t=1"
7270"\n" "for(i=3;t!=0;i+=2){"
7271"\n" "u*=f"
7272"\n" "t=u/i"
7273"\n" "r+=t"
7274"\n" "}"
7275"\n" "scale=s"
7276"\n" "ibase=b"
7277"\n" "return((m*a+r)/n)"
7278"\n" "}"
7279"\n" "define j(n,x){"
7280"\n" "auto b,s,o,a,i,v,f"
7281"\n" "b=ibase"
7282"\n" "ibase=A"
7283"\n" "s=scale"
7284"\n" "scale=0"
7285"\n" "n/=1"
7286"\n" "if(n<0){"
7287"\n" "n=-n"
7288"\n" "if(n%2==1)o=1"
7289"\n" "}"
7290"\n" "a=1"
7291"\n" "for(i=2;i<=n;++i)a*=i"
7292"\n" "scale=1.5*s"
7293"\n" "a=(x^n)/2^n/a"
7294"\n" "r=v=1"
7295"\n" "f=-x*x/4"
7296"\n" "scale=scale+length(a)-scale(a)"
7297"\n" "for(i=1;v!=0;++i){"
7298"\n" "v=v*f/i/(n+i)"
7299"\n" "r+=v"
7300"\n" "}"
7301"\n" "scale=s"
7302"\n" "ibase=b"
7303"\n" "if(o!=0)a=-a"
7304"\n" "return(a*r/1)"
7305"\n" "}"
7306};
7307#endif // ENABLE_BC
7308
Denys Vlasenko19f11072018-12-12 22:48:19 +01007309static BC_STATUS zbc_vm_exec(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007310{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007311 BcStatus s;
Gavin Howard01055ba2018-11-03 11:00:21 -06007312 size_t i;
7313
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007314#if ENABLE_BC
Denys Vlasenkod70d4a02018-12-04 20:58:40 +01007315 if (option_mask32 & BC_FLAG_L) {
Gavin Howard01055ba2018-11-03 11:00:21 -06007316
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007317 // We know that internal library is not buggy,
7318 // thus error checking is normally disabled.
7319# define DEBUG_LIB 0
Denys Vlasenko0409ad32018-12-05 16:39:22 +01007320 bc_lex_file(&G.prs.l);
Denys Vlasenko26819db2018-12-12 16:08:46 +01007321 s = zbc_parse_text(&G.prs, bc_lib);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007322 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007323
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007324 while (G.prs.l.t.t != BC_LEX_EOF) {
Denys Vlasenkoc0ef23c2018-12-12 23:03:10 +01007325 ERROR_RETURN(s =) zcommon_parse(&G.prs);
Denys Vlasenko19f11072018-12-12 22:48:19 +01007326 if (DEBUG_LIB && s) RETURN_STATUS(s);
Denys Vlasenko0ad36c42018-12-05 16:21:43 +01007327 }
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007328 s = zbc_program_exec();
Denys Vlasenko19f11072018-12-12 22:48:19 +01007329 if (DEBUG_LIB && s) RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007330 }
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007331#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007332
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007333 s = BC_STATUS_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007334 for (i = 0; !s && i < G.files.len; ++i)
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007335 s = zbc_vm_file(*((char **) bc_vec_item(&G.files, i)));
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007336 if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) {
7337 // Debug config, non-interactive mode:
7338 // return all the way back to main.
7339 // Non-debug builds do not come here, they exit.
Denys Vlasenko19f11072018-12-12 22:48:19 +01007340 RETURN_STATUS(s);
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007341 }
Gavin Howard01055ba2018-11-03 11:00:21 -06007342
Denys Vlasenko91cde952018-12-10 20:56:08 +01007343 if (IS_BC || (option_mask32 & BC_FLAG_I))
Denys Vlasenko19f11072018-12-12 22:48:19 +01007344 s = zbc_vm_stdin();
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007345
Denys Vlasenko9b70f192018-12-04 20:51:40 +01007346 if (!s && !BC_PARSE_CAN_EXEC(&G.prs))
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007347 s = zbc_vm_process("");
Gavin Howard01055ba2018-11-03 11:00:21 -06007348
Denys Vlasenko19f11072018-12-12 22:48:19 +01007349 RETURN_STATUS(s);
Gavin Howard01055ba2018-11-03 11:00:21 -06007350}
Denys Vlasenko19f11072018-12-12 22:48:19 +01007351#if ERRORS_ARE_FATAL
7352# define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__), BC_STATUS_SUCCESS)
7353#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007354
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007355#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007356static void bc_program_free(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007357{
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007358 bc_num_free(&G.prog.ib);
7359 bc_num_free(&G.prog.ob);
7360 bc_num_free(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007361# if ENABLE_DC
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007362 bc_num_free(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007363# endif
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007364 bc_vec_free(&G.prog.fns);
7365 bc_vec_free(&G.prog.fn_map);
7366 bc_vec_free(&G.prog.vars);
7367 bc_vec_free(&G.prog.var_map);
7368 bc_vec_free(&G.prog.arrs);
7369 bc_vec_free(&G.prog.arr_map);
7370 bc_vec_free(&G.prog.strs);
7371 bc_vec_free(&G.prog.consts);
7372 bc_vec_free(&G.prog.results);
7373 bc_vec_free(&G.prog.stack);
7374 bc_num_free(&G.prog.last);
7375 bc_num_free(&G.prog.zero);
7376 bc_num_free(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007377}
7378
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007379static void bc_vm_free(void)
Gavin Howard01055ba2018-11-03 11:00:21 -06007380{
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007381 bc_vec_free(&G.files);
Denys Vlasenkoa1d3ca22018-12-02 18:26:38 +01007382 bc_program_free();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007383 bc_parse_free(&G.prs);
7384 free(G.env_args);
Gavin Howard01055ba2018-11-03 11:00:21 -06007385}
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007386#endif
7387
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007388static void bc_program_init(void)
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007389{
7390 size_t idx;
7391 BcInstPtr ip;
7392
7393 /* memset(&G.prog, 0, sizeof(G.prog)); - already is */
7394 memset(&ip, 0, sizeof(BcInstPtr));
7395
7396 /* G.prog.nchars = G.prog.scale = 0; - already is */
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007397 bc_num_init_DEF_SIZE(&G.prog.ib);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007398 bc_num_ten(&G.prog.ib);
7399 G.prog.ib_t = 10;
7400
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007401 bc_num_init_DEF_SIZE(&G.prog.ob);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007402 bc_num_ten(&G.prog.ob);
7403 G.prog.ob_t = 10;
7404
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007405 bc_num_init_DEF_SIZE(&G.prog.hexb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007406 bc_num_ten(&G.prog.hexb);
7407 G.prog.hexb.num[0] = 6;
7408
7409#if ENABLE_DC
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007410 bc_num_init_DEF_SIZE(&G.prog.strmb);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007411 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7412#endif
7413
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007414 bc_num_init_DEF_SIZE(&G.prog.last);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007415 //bc_num_zero(&G.prog.last); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007416
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007417 bc_num_init_DEF_SIZE(&G.prog.zero);
Denys Vlasenko3129f702018-12-09 12:04:44 +01007418 //bc_num_zero(&G.prog.zero); - already is
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007419
Denys Vlasenkoe20e00d2018-12-09 11:44:20 +01007420 bc_num_init_DEF_SIZE(&G.prog.one);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007421 bc_num_one(&G.prog.one);
7422
7423 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007424 bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007425
Denys Vlasenko1f67e932018-12-03 00:08:59 +01007426 bc_program_addFunc(xstrdup("(main)"), &idx);
7427 bc_program_addFunc(xstrdup("(read)"), &idx);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007428
7429 bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007430 bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007431
7432 bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free);
Denys Vlasenkocb9a99f2018-12-04 21:54:33 +01007433 bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free);
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007434
7435 bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);
7436 bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);
7437 bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free);
7438 bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL);
7439 bc_vec_push(&G.prog.stack, &ip);
7440}
Gavin Howard01055ba2018-11-03 11:00:21 -06007441
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007442static int bc_vm_init(const char *env_len)
Gavin Howard01055ba2018-11-03 11:00:21 -06007443{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007444#if ENABLE_FEATURE_EDITING
7445 G.line_input_state = new_line_input_t(DO_HISTORY);
7446#endif
7447 G.prog.len = bc_vm_envLen(env_len);
7448
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007449 bc_vec_init(&G.files, sizeof(char *), NULL);
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007450 if (IS_BC)
Denys Vlasenko23c2e9f2018-12-06 11:43:17 +01007451 IF_BC(bc_vm_envArgs();)
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007452 bc_program_init();
Denys Vlasenkoc2265f52018-12-12 23:10:08 +01007453 bc_parse_create(&G.prs, BC_PROG_MAIN);
Gavin Howard01055ba2018-11-03 11:00:21 -06007454
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007455//TODO: in GNU bc, the check is (isatty(0) && isatty(1)),
7456//-i option unconditionally enables this regardless of isatty():
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007457 if (isatty(0)) {
Denys Vlasenkod38af482018-12-04 19:11:02 +01007458#if ENABLE_FEATURE_BC_SIGNALS
Denys Vlasenko1a6a4822018-12-06 09:20:32 +01007459 G_ttyin = 1;
Denys Vlasenko17c54722018-12-04 21:21:32 +01007460 // With SA_RESTART, most system calls will restart
7461 // (IOW: they won't fail with EINTR).
7462 // In particular, this means ^C won't cause
7463 // stdout to get into "error state" if SIGINT hits
7464 // within write() syscall.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007465 //
7466 // The downside is that ^C while tty input is taken
Denys Vlasenko17c54722018-12-04 21:21:32 +01007467 // will only be handled after [Enter] since read()
7468 // from stdin is not interrupted by ^C either,
7469 // it restarts, thus fgetc() does not return on ^C.
Denys Vlasenko89e785a2018-12-13 16:35:52 +01007470 // (This problem manifests only if line editing is disabled)
Denys Vlasenko17c54722018-12-04 21:21:32 +01007471 signal_SA_RESTART_empty_mask(SIGINT, record_signo);
7472
7473 // Without SA_RESTART, this exhibits a bug:
7474 // "while (1) print 1" and try ^C-ing it.
7475 // Intermittently, instead of returning to input line,
7476 // you'll get "output error: Interrupted system call"
7477 // and exit.
7478 //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
Denys Vlasenkod38af482018-12-04 19:11:02 +01007479#endif
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007480 return 1; // "tty"
Denys Vlasenkod38af482018-12-04 19:11:02 +01007481 }
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007482 return 0; // "not a tty"
7483}
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007484
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007485static BcStatus bc_vm_run(void)
7486{
Denys Vlasenko19f11072018-12-12 22:48:19 +01007487 BcStatus st = zbc_vm_exec();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007488#if ENABLE_FEATURE_CLEAN_UP
Denys Vlasenkoc7a7ce02018-12-06 23:06:57 +01007489 if (G_exiting) // it was actually "halt" or "quit"
7490 st = EXIT_SUCCESS;
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007491 bc_vm_free();
Denys Vlasenko95f93bd2018-12-06 10:29:12 +01007492# if ENABLE_FEATURE_EDITING
7493 free_line_input_t(G.line_input_state);
7494# endif
Denys Vlasenkoe873ff92018-12-06 00:29:22 +01007495 FREE_G();
Denys Vlasenko785e4b32018-12-02 17:18:52 +01007496#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007497 return st;
7498}
7499
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007500#if ENABLE_BC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007501int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007502int bc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007503{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007504 int is_tty;
7505
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007506 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007507 G.sbgn = G.send = '"';
Gavin Howard01055ba2018-11-03 11:00:21 -06007508
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007509 is_tty = bc_vm_init("BC_LINE_LENGTH");
7510
7511 bc_args(argv);
7512
7513 if (is_tty && !(option_mask32 & BC_FLAG_Q))
7514 bc_vm_info();
7515
7516 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007517}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007518#endif
Gavin Howard01055ba2018-11-03 11:00:21 -06007519
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007520#if ENABLE_DC
Denys Vlasenko5a9fef52018-12-02 14:35:32 +01007521int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko1ff1c702018-12-06 00:46:09 +01007522int dc_main(int argc UNUSED_PARAM, char **argv)
Gavin Howard01055ba2018-11-03 11:00:21 -06007523{
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007524 int noscript;
7525
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007526 INIT_G();
Denys Vlasenko6d9146a2018-12-02 15:48:37 +01007527 G.sbgn = '[';
7528 G.send = ']';
Denys Vlasenko6e7c65f2018-12-08 19:34:35 +01007529 /*
7530 * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width
7531 * 1 char wider than bc from the same package.
7532 * Both default width, and xC_LINE_LENGTH=N are wider:
7533 * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints:
7534 * 1234\
7535 * 56
7536 * "echo '123456' | BC_LINE_LENGTH=5 bc" prints:
7537 * 123\
7538 * 456
7539 * Do the same, or it's a bug?
7540 */
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007541 bc_vm_init("DC_LINE_LENGTH");
Gavin Howard01055ba2018-11-03 11:00:21 -06007542
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007543 // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs
7544 noscript = BC_FLAG_I;
7545 for (;;) {
7546 int n = getopt(argc, argv, "e:f:x");
7547 if (n <= 0)
7548 break;
7549 switch (n) {
7550 case 'e':
7551 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007552 n = zbc_vm_process(optarg);
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007553 if (n) return n;
7554 break;
7555 case 'f':
7556 noscript = 0;
Denys Vlasenkod6ad3662018-12-12 21:39:10 +01007557 n = zbc_vm_file(optarg);
7558 if (n) return n;
Denys Vlasenko6d0be102018-12-06 18:41:59 +01007559 break;
7560 case 'x':
7561 option_mask32 |= DC_FLAG_X;
7562 break;
7563 default:
7564 bb_show_usage();
7565 }
7566 }
7567 argv += optind;
7568
7569 while (*argv) {
7570 noscript = 0;
7571 bc_vec_push(&G.files, argv++);
7572 }
7573
7574 option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin
7575
7576 return bc_vm_run();
Gavin Howard01055ba2018-11-03 11:00:21 -06007577}
Denys Vlasenkoef869ec2018-12-02 18:49:16 +01007578#endif
Denys Vlasenko9ca9ef22018-12-06 11:31:14 +01007579
7580#endif // not DC_SMALL